simplix: Replace arrays with std::vector

Former-commit-id: 8b4302b8b5bbd9c7aed3d29fdf2f3c820c1573c5
Former-commit-id: 3c72d5837afce3fd38aaf2055ef9b3d2a45a134b
This commit is contained in:
Xavier Del Campo Romero 2024-12-04 08:02:20 +01:00
parent 4a36fd8de7
commit 8a6b2aadc7
6 changed files with 66 additions and 188 deletions

View file

@ -49,9 +49,6 @@
//--------------------------------------------------------------------------*
TCubicSpline::TCubicSpline()
{
oCount = 0;
oSegs = NULL;
oCubics = NULL;
}
//==========================================================================*
@ -61,51 +58,18 @@ TCubicSpline::TCubicSpline()
TCubicSpline::TCubicSpline
(int Count, const double* X, const double* Y, const double* S)
{
oCount = Count;
oSegs = new double[oCount];
oCubics = new TCubic[oCount - 1];
oSegs.reserve(Count);
oCubics.reserve(Count - 1);
for (int I = 0; I < oCount; I++)
for (int I = 0; I < Count; I++)
{
oSegs[I] = X[I];
if (I + 1 < oCount)
oCubics[I].Set( X[I], Y[I], S[I], X[I+1], Y[I+1], S[I+1]);
oSegs.push_back(X[I]);
if (I + 1 < Count)
oCubics.push_back(TCubic(X[I], Y[I], S[I], X[I+1], Y[I+1], S[I+1]));
}
}
//==========================================================================*
//==========================================================================*
// Initialization
//--------------------------------------------------------------------------*
void TCubicSpline::Init
(int Count, const double* X, const double* Y, const double* S)
{
delete [] oSegs;
delete [] oCubics;
oCount = Count;
oSegs = new double[oCount];
oCubics = new TCubic[oCount - 1];
for (int I = 0; I < oCount; I++)
{
oSegs[I] = X[I];
if (I + 1 < oCount)
oCubics[I].Set( X[I], Y[I], S[I], X[I+1], Y[I+1], S[I+1]);
}
}
//==========================================================================*
//==========================================================================*
// Destructor
//--------------------------------------------------------------------------*
TCubicSpline::~TCubicSpline()
{
delete [] oSegs;
delete [] oCubics;
}
//==========================================================================*
//==========================================================================*
// Get offset
//--------------------------------------------------------------------------*
@ -131,7 +95,7 @@ double TCubicSpline::CalcGradient(double X) const
//--------------------------------------------------------------------------*
bool TCubicSpline::IsValidX(double X) const
{
return X >= oSegs[0] && X <= oSegs[oCount - 1];
return X >= oSegs[0] && X < oSegs.size();
}
//==========================================================================*
@ -142,7 +106,7 @@ int TCubicSpline::FindSeg(double X) const
{
// binary chop search for interval.
int Lo = 0;
int Hi = oCount;
int Hi = oSegs.size();
while (Lo + 1 < Hi)
{

View file

@ -36,6 +36,7 @@
#define _UNITCUBICSPLINE_H_
#include "unitcubic.h"
#include <vector>
//==========================================================================*
// Deklaration der Klasse TCubicSpline
@ -47,10 +48,6 @@ class TCubicSpline
TCubicSpline // Constructor
(int Count, const double* X,
const double* Y, const double* S);
~TCubicSpline();
void Init(int Count, const double* X,
const double* Y, const double* S);
double CalcOffset(double X) const; // Get offset
double CalcGradient(double X) const; // Get gradient
@ -61,9 +58,8 @@ class TCubicSpline
int FindSeg(double X) const; // Find seg to x
private:
int oCount; // Nbr of segemnts
double* oSegs; // Segments
TCubic* oCubics; // Cubics
std::vector<double> oSegs; // Segments
std::vector<TCubic> oCubics; // Cubics
};
//==========================================================================*
#endif // _UNITCUBICSPLINE_H_

View file

@ -604,7 +604,7 @@ void TDriver::AdjustCarCharacteristic(PCarHandle Handle)
sprintf(buf, "%s/%s/%d", TDriver::SECT_PRIV, PRV_CAR_CHARACTER, I+1);
Y[I] = GfParmGetNum(Handle, buf, PRV_PERFORMANCE, (char*) NULL, 1.0);
}
CarCharacteristic.Init(ControlPoints, X, Y, S);
CarCharacteristic = TCubicSpline(ControlPoints, X, Y, S);
snprintf(buf, BUFLEN, "%sCharacteristic-%s.txt",
GetLocalDir(), oDriverName.c_str());

View file

@ -72,60 +72,12 @@
// Default constructor
//--------------------------------------------------------------------------*
TLane::TLane(TDriver &driver):
oPathPoints(NULL),
oTrack(NULL),
driver(driver)
{
}
//==========================================================================*
//==========================================================================*
// Destructor
//--------------------------------------------------------------------------*
TLane::~TLane()
{
delete [] oPathPoints;
}
//==========================================================================*
//==========================================================================*
// Set operator (Sets lane)
//--------------------------------------------------------------------------*
TLane& TLane::operator= (const TLane& Lane)
{
SetLane(Lane);
return *this;
}
//==========================================================================*
//==========================================================================*
// Set lane
//--------------------------------------------------------------------------*
void TLane::SetLane(const TLane& Lane)
{
oTrack = Lane.oTrack;
oFixCarParam = Lane.oFixCarParam;
oCarParam = Lane.oCarParam;
const int Count = oTrack->Count();
delete [] oPathPoints;
oPathPoints = new TPathPt[Count];
for (int i = 0; i < Count; ++i)
oPathPoints[i] = Lane.oPathPoints[i];
//memcpy(oPathPoints, Lane.oPathPoints, Count * sizeof(*oPathPoints));
for (int I = 0; I < TA_N; I++)
{
TA_X[I] = Lane.TA_X[I];
TA_Y[I] = Lane.TA_Y[I];
TA_S[I] = Lane.TA_S[I];
}
oTurnScale.Init(TA_N,TA_X,TA_Y,TA_S);
}
//==========================================================================*
//==========================================================================*
// Check wether position is in lane
//--------------------------------------------------------------------------*
@ -215,98 +167,41 @@ void TLane::Initialise
const TCarParam& CarParam,
double MaxLeft, double MaxRight)
{
delete [] oPathPoints;
oPathPoints.clear();
oTrack = Track;
oPathPoints = new TPathPt[Track->Count()];
oCarParam = CarParam; // Copy car params
oFixCarParam = FixCarParam; // Copy car params
// To avoid uninitialized alignment bytes within the allocated memory for
// linux compilers not doing this initialization without our explizit
// request we should fill it with zeros. Otherwise we would get valgrind
// warnings writing the data to file using only one memory block per
// path point.
//memset(oPathPoints, 0, Track->Count() * sizeof(*oPathPoints));
for (int i = 0; i < static_cast<int>(Track->Count()); ++i)
oPathPoints[i].Sec = nullptr;
oPathPoints.reserve(Track->Count());
if (MaxLeft < 999.0)
{
for (int I = 0; I < Track->Count(); I++)
{
const TSection& Sec = (*oTrack)[I];
oPathPoints[I].Sec = &Sec;
oPathPoints[I].Center = Sec.Center;
oPathPoints[I].Crv = 0;
oPathPoints[I].CrvZ = 0;
oPathPoints[I].Offset = 0.0;
oPathPoints[I].Point = oPathPoints[I].CalcPt();
oPathPoints[I].MaxSpeed = 10;
oPathPoints[I].Speed = 10;
oPathPoints[I].AccSpd = 10;
oPathPoints[I].FlyHeight = 0;
// oPathPoints[I].BufL = 0;
// oPathPoints[I].BufR = 0;
oPathPoints[I].NextCrv = 0.0;
oPathPoints[I].WToL = (float) MaxLeft;
oPathPoints[I].WToR = (float) Sec.WidthToRight;
oPathPoints[I].WPitToL = (float) Sec.PitWidthToLeft;
oPathPoints[I].WPitToR = (float) Sec.PitWidthToRight;
oPathPoints[I].Fix = false;
oPathPoints.push_back(TPathPt(Sec, MaxLeft, Sec.WidthToRight));
}
oPathPoints[0].WToL = oPathPoints[1].WToL;
oPathPoints[0].WToR = oPathPoints[1].WToR;
oPathPoints[0].WToL = oPathPoints.at(1).WToL;
oPathPoints[0].WToR = oPathPoints.at(1).WToR;
}
else if (MaxRight < 999.0)
{
for (int I = 0; I < Track->Count(); I++)
{
const TSection& Sec = (*oTrack)[I];
oPathPoints[I].Sec = &Sec;
oPathPoints[I].Center = Sec.Center;
oPathPoints[I].Crv = 0;
oPathPoints[I].CrvZ = 0;
oPathPoints[I].Offset = 0.0;
oPathPoints[I].Point = oPathPoints[I].CalcPt();
oPathPoints[I].MaxSpeed = 10;
oPathPoints[I].Speed = 10;
oPathPoints[I].AccSpd = 10;
oPathPoints[I].FlyHeight = 0;
// oPathPoints[I].BufL = 0;
// oPathPoints[I].BufR = 0;
oPathPoints[I].NextCrv = 0.0;
oPathPoints[I].WToL = (float) Sec.WidthToLeft;
oPathPoints[I].WToR = (float) MaxRight;
oPathPoints[I].WPitToL = (float) Sec.PitWidthToLeft;
oPathPoints[I].WPitToR = (float) Sec.PitWidthToRight;
oPathPoints[I].Fix = false;
oPathPoints.push_back(TPathPt(Sec, Sec.WidthToLeft, MaxRight));
}
oPathPoints[0].WToL = oPathPoints[1].WToL;
oPathPoints[0].WToR = oPathPoints[1].WToR;
oPathPoints[0].WToL = oPathPoints.at(1).WToL;
oPathPoints[0].WToR = oPathPoints.at(1).WToR;
}
else
{
for (int I = 0; I < Track->Count(); I++)
{
const TSection& Sec = (*oTrack)[I];
oPathPoints[I].Sec = &Sec;
oPathPoints[I].Center = Sec.Center;
oPathPoints[I].Crv = 0;
oPathPoints[I].CrvZ = 0;
oPathPoints[I].Offset = 0.0;
oPathPoints[I].Point = oPathPoints[I].CalcPt();
oPathPoints[I].MaxSpeed = 10;
oPathPoints[I].Speed = 10;
oPathPoints[I].AccSpd = 10;
oPathPoints[I].FlyHeight = 0;
// oPathPoints[I].BufL = 0;
// oPathPoints[I].BufR = 0;
oPathPoints[I].NextCrv = 0.0;
oPathPoints[I].WToL = (float) Sec.WidthToLeft;
oPathPoints[I].WToR = (float) Sec.WidthToRight;
oPathPoints[I].WPitToL = (float) Sec.PitWidthToLeft;
oPathPoints[I].WPitToR = (float) Sec.PitWidthToRight;
oPathPoints[I].Fix = false;
oPathPoints.push_back(TPathPt(Sec, Sec.WidthToLeft, Sec.WidthToRight));
}
oPathPoints[0].WToL = oPathPoints[1].WToL;
oPathPoints[0].WToR = oPathPoints[1].WToR;
@ -351,7 +246,7 @@ void TLane::Initialise
TA_S[0] = 0.0;
TA_S[9] = 0.0;
oTurnScale.Init(TA_N,TA_X,TA_Y,TA_S);
oTurnScale = TCubicSpline(TA_N,TA_X,TA_Y,TA_S);
}
//==========================================================================*
@ -360,8 +255,14 @@ void TLane::Initialise
//--------------------------------------------------------------------------*
const TLane::TPathPt& TLane::PathPoints(int Index) const
{
return oPathPoints[Index];
return oPathPoints.at(Index);
}
const std::vector<TLane::TPathPt> &TLane::PathPoints() const
{
return oPathPoints;
}
//==========================================================================*
//==========================================================================*

View file

@ -60,6 +60,7 @@
#include "unitcarparam.h"
#include "unitfixcarparam.h"
#include "unitcubicspline.h"
#include <vector>
//==========================================================================*
// Class TLane
@ -69,10 +70,35 @@ class TLane
public:
struct TPathPt
{
// Part 1: These data will be stored and reused from others as well
TPathPt(const TSection &sec, float left, float right) :
DistFromStart(sec.DistFromStart),
ToRight(sec.ToRight),
Offset(0),
Center(sec.Center),
Point(CalcPt()),
Crv(0),
CrvZ(0),
NextCrv(0),
WToL(left),
WToR(right),
WPitToL(sec.PitWidthToLeft),
WPitToR(sec.PitWidthToRight),
Fix(false),
MaxSpeed(10),
AccSpd(10),
Speed(10),
FlyHeight(0)
{}
TPathPt(const TSection &sec) :
TPathPt(sec, 0, 0)
{}
float DistFromStart;
TVec3d ToRight;
float Offset; // Offset from centre point
TVec3d Center; // Lane specific center
TVec3d Point; // Actual point (same as CalcPt())
float Offset; // Offset from centre point
float Crv; // Curvature in xy
float CrvZ; // Curvature in z direction... e.g. bumps
float NextCrv; // Cuvature comming next
@ -81,23 +107,17 @@ class TLane
float WPitToL; // Lane specfic width to left
float WPitToR; // Lane specfic width to right
bool Fix;
// Part 2: These data could be stored, but is recalculated from others
// (So we don't have to store it)
double MaxSpeed; // Max speed through this point
double AccSpd; // Speed through this point, with modelled accel
double Speed; // Speed through this point (braking only)
double FlyHeight; // Predicted height of car above track (flying)
// Part 3: These data may not be used from others (pointers)
const TSection* Sec; // Track seg that contains this Seg
double Dist() const {return Sec->DistFromStart;}
double Dist() const {return DistFromStart;}
double WtoL() const {return WToL;}
double WtoR() const {return WToR;}
const TVec3d& Pt() const {return Center;}
const TVec3d& Norm() const {return Sec->ToRight;}
TVec3d CalcPt() const {return Center + Sec->ToRight * Offset;}
const TVec3d& Norm() const {return ToRight;}
TVec3d CalcPt() const {return Center + ToRight * Offset;}
};
public:
@ -106,19 +126,15 @@ class TLane
double TA_Y[TA_N]; // Y-coordinates
double TA_S[TA_N]; // Directions
TPathPt* oPathPoints; // Points in this lane
std::vector<TPathPt> oPathPoints; // Points in this lane
TLane(TDriver &driver);
virtual ~TLane();
virtual TLane& operator= (const TLane& Lane);
virtual bool ContainsPos
(double TrackPos) const;
virtual bool GetLanePoint
(double TrackPos, TLanePoint& LanePoint) const;
void SetLane(const TLane& Lane);
void Initialise
(TTrackDescription* pTrack,
const TFixCarParam& FixCarParam,
@ -128,6 +144,7 @@ class TLane
void SmmothLane();
const TPathPt& PathPoints(int Index) const;
const std::vector<TLane::TPathPt> &PathPoints() const;
void Dump();
void SmoothSpeeds();

View file

@ -356,7 +356,7 @@ void TPitLane::MakePath
bool FirstPit = false; // Reset flag
TCarParam CarParam;
CarParam = Param.oCarParam3; // Copy parameters
TLane::SetLane(*BasePath); // Copy Pathpoints
*static_cast<TLane *>(this) = *static_cast<const TLane *>(BasePath);
const tTrackPitInfo* PitInfo = // Get pit infos
&oTrack->Track()->pits;