mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-02 16:36:53 +01:00
perf(world): FastGetTFromS reads the curve's t and slope of a point side by side
TSegment kept the curve parameter t at equal steps of s (fTsBuffer) and the Hermite slopes at the same
points (fTsSlope) in two vectors, and FastGetTFromS(), called for every axle of every vehicle on every
step, read [i] and [i + 1] of both: four loads from two separate allocations. The two now go together in
one vector of {t, slope} (fTs), so the interpolation touches one or two cache lines. The values and the
arithmetic, in the same order, are unchanged.
Tomaszewo (raj40821) 600 s in --test, seed 12345: FastGetTFromS was 1.2% of the main thread; sim 259.7 s
median of 3 (255.0-271.3) against 268.3 s (262.5-279.2) before, which is about the run-to-run spread.
The 14 physics logs and the event log are byte-identical, unit tests 1205/1205 with exact golden traces.
This commit is contained in:
+29
-28
@@ -187,11 +187,11 @@ bool TSegment::InitChain(std::vector<std::shared_ptr<TSegment>> Pieces)
|
||||
fDirection = -std::atan2(Point2.x - Point1.x, Point2.z - Point1.z);
|
||||
fStoop = std::atan2(Point2.y - Point1.y, fLength);
|
||||
fStep = fLength / iSegCount;
|
||||
fTsBuffer.resize( iSegCount + 1 );
|
||||
fTs.resize( iSegCount + 1 );
|
||||
for( int i = 0; i <= iSegCount; ++i ) {
|
||||
fTsBuffer[ i ] = static_cast<double>( i ) / iSegCount;
|
||||
fTs[ i ].t = static_cast<double>( i ) / iSegCount;
|
||||
fTs[ i ].slope = fStep / fLength;
|
||||
}
|
||||
fTsSlope.assign( iSegCount + 1, fStep / fLength );
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -204,28 +204,27 @@ std::size_t TSegment::piece_at(double const s) const
|
||||
void TSegment::InitTFromSInterpolateData()
|
||||
{
|
||||
// pre-compute the curve parameter t at equal distance intervals
|
||||
fTsBuffer.resize( iSegCount + 1 );
|
||||
fTsBuffer[ 0 ] = 0.0;
|
||||
fTs.resize( iSegCount + 1 );
|
||||
fTs[ 0 ].t = 0.0;
|
||||
for( int i = 1; i < iSegCount; ++i ) {
|
||||
fTsBuffer[ i ] = GetTFromS( i * fStep );
|
||||
fTs[ i ].t = GetTFromS( i * fStep );
|
||||
}
|
||||
fTsBuffer[ iSegCount ] = 1.0;
|
||||
fTs[ iSegCount ].t = 1.0;
|
||||
|
||||
// pre-compute Hermite slopes dt/du = fStep / |B'(t)| for FastGetTFromS
|
||||
// (dt/ds = 1/|B'(t)|, scaled by fStep to the interval coordinate u)
|
||||
fTsSlope.resize( iSegCount + 1 );
|
||||
for( int i = 0; i <= iSegCount; ++i ) {
|
||||
// compute ds/dt derivative = |B'(t)|
|
||||
double const speed = glm::length( GetFirstDerivative( fTsBuffer[ i ] ) );
|
||||
double const speed = glm::length( GetFirstDerivative( fTs[ i ].t ) );
|
||||
// compute dt/du derivative = fStep / (ds/dt)
|
||||
fTsSlope[ i ] = speed > 0.0 ? fStep / speed : 0.0;
|
||||
fTs[ i ].slope = speed > 0.0 ? fStep / speed : 0.0;
|
||||
}
|
||||
// clamp slopes to keep t(s) monotone (PCHIP / Fritsch-Carlson condition)
|
||||
for( int i = 0; i <= iSegCount; ++i ) {
|
||||
double limit = std::numeric_limits<double>::max();
|
||||
if( i > 0 ) { limit = std::min( limit, 3.0 * ( fTsBuffer[ i ] - fTsBuffer[ i - 1 ] ) ); }
|
||||
if( i < iSegCount ) { limit = std::min( limit, 3.0 * ( fTsBuffer[ i + 1 ] - fTsBuffer[ i ] ) ); }
|
||||
fTsSlope[ i ] = std::min( fTsSlope[ i ], limit );
|
||||
if( i > 0 ) { limit = std::min( limit, 3.0 * ( fTs[ i ].t - fTs[ i - 1 ].t ) ); }
|
||||
if( i < iSegCount ) { limit = std::min( limit, 3.0 * ( fTs[ i + 1 ].t - fTs[ i ].t ) ); }
|
||||
fTs[ i ].slope = std::min( fTs[ i ].slope, limit );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -296,7 +295,7 @@ double TSegment::GetTFromS(double const s) const
|
||||
|
||||
double TSegment::FastGetTFromS(double const s) const
|
||||
{ // Faster version of GetTFromS: cubic Hermite interpolation using
|
||||
// the precomputed values in fTsBuffer and slopes in fTsSlope. It
|
||||
// the precomputed values and slopes in fTs. It
|
||||
// avoids the expensive RombergIntegral+Newton per call. Hermite
|
||||
// interpolation is C1-continuous, so no sudden speed changes when
|
||||
// moving from one interval to the next.
|
||||
@@ -310,10 +309,12 @@ double TSegment::FastGetTFromS(double const s) const
|
||||
double const u2 = u * u;
|
||||
double const u3 = u2 * u;
|
||||
double const h01 = -2.0 * u3 + 3.0 * u2;
|
||||
double const t = ( ( 1.0 - h01 ) * fTsBuffer[ i ] +
|
||||
( u3 - 2.0 * u2 + u ) * fTsSlope [ i ] +
|
||||
h01 * fTsBuffer[ i + 1 ] +
|
||||
( u3 - u2 ) * fTsSlope [ i + 1 ] );
|
||||
auto const &from { fTs[ i ] };
|
||||
auto const &to { fTs[ i + 1 ] };
|
||||
double const t = ( ( 1.0 - h01 ) * from.t +
|
||||
( u3 - 2.0 * u2 + u ) * from.slope +
|
||||
h01 * to.t +
|
||||
( u3 - u2 ) * to.slope );
|
||||
return t;
|
||||
}
|
||||
|
||||
@@ -375,11 +376,11 @@ TSegment::find_nearest_point( glm::dvec3 const &Point ) const {
|
||||
auto const segmentpoint =
|
||||
std::clamp(
|
||||
nearest_segment_point(
|
||||
glm::dvec3{ FastGetPoint( fTsBuffer[ segmentidx ] ) },
|
||||
glm::dvec3{ FastGetPoint( fTsBuffer[ segmentidx + 1 ] ) },
|
||||
glm::dvec3{ FastGetPoint( fTs[ segmentidx ].t ) },
|
||||
glm::dvec3{ FastGetPoint( fTs[ segmentidx + 1 ].t ) },
|
||||
Point ) // point in range 0-1 on current segment
|
||||
* ( fTsBuffer[ segmentidx + 1 ] - fTsBuffer[ segmentidx ] ) // segment length
|
||||
+ fTsBuffer[ segmentidx ], // segment offset
|
||||
* ( fTs[ segmentidx + 1 ].t - fTs[ segmentidx ].t ) // segment length
|
||||
+ fTs[ segmentidx ].t, // segment offset
|
||||
0.0, 1.0 ); // we clamp the range in case there's some floating point math inaccuracies
|
||||
|
||||
auto const segmentdistance = glm::length2( Point - glm::dvec3{ FastGetPoint( segmentpoint ) } );
|
||||
@@ -545,7 +546,7 @@ void TSegment::RaPositionGet(double const fDistance, glm::dvec3 &position, glm::
|
||||
};
|
||||
|
||||
void TSegment::plan_frame( double const Distance, glm::dvec3 &Point, glm::dvec3 &Across ) const {
|
||||
if( fTsBuffer.empty() ) {
|
||||
if( fTs.empty() ) {
|
||||
// never made into a path: tt_Cross and the like
|
||||
Point = Point1;
|
||||
Across = glm::dvec3 { 0.0 };
|
||||
@@ -573,7 +574,7 @@ bool TSegment::RenderLoft( gfx::vertex_array &Output, glm::dvec3 const &Origin,
|
||||
// standardowo tworzy triangle_strip dla prostego albo ich zestaw dla łuku
|
||||
// po modyfikacji - dla ujemnego (iNumShapePoints) w dodatkowych polach tabeli podany jest przekrój końcowy
|
||||
|
||||
if( fTsBuffer.empty() )
|
||||
if( fTs.empty() )
|
||||
return false; // prowizoryczne zabezpieczenie przed wysypem - ustalić faktyczną przyczynę
|
||||
|
||||
glm::vec3 pos1;
|
||||
@@ -602,7 +603,7 @@ bool TSegment::RenderLoft( gfx::vertex_array &Output, glm::dvec3 const &Origin,
|
||||
tv1 = 1.0; // Ra: to by można było wyliczać dla odcinka, wyglądało by lepiej
|
||||
s = static_cast<float>(fStep * iSkip); // iSkip - ile odcinków z początku pominąć
|
||||
int i = iSkip; // domyślnie 0
|
||||
t = static_cast<float>(fTsBuffer[ i ]); // tabela wattości t dla segmentów
|
||||
t = static_cast<float>(fTs[ i ].t); // tabela wattości t dla segmentów
|
||||
// BUG: length of spline can be 0, we should skip geometry generation for such cases
|
||||
fOffset = static_cast<float>(0.1 / fLength); // pierwsze 10cm
|
||||
pos1 = glm::dvec3{ FastGetPoint( t ) - Origin }; // wektor początku segmentu
|
||||
@@ -653,7 +654,7 @@ bool TSegment::RenderLoft( gfx::vertex_array &Output, glm::dvec3 const &Origin,
|
||||
tv1 = clamp_circular( tv1, 1.0f );
|
||||
tv2 = tv1 - step / texturelength; // mapowanie na końcu segmentu
|
||||
|
||||
t = static_cast<float>(fTsBuffer[ i ]); // szybsze od GetTFromS(s);
|
||||
t = static_cast<float>(fTs[ i ].t); // szybsze od GetTFromS(s);
|
||||
pos2 = glm::dvec3{ FastGetPoint( t ) - Origin };
|
||||
dir = glm::dvec3{ FastGetDirection( t, fOffset ) }; // nowy wektor kierunku
|
||||
parallel2 = glm::vec3{ -dir.z, 0.f, dir.x }; // wektor poprzeczny
|
||||
@@ -769,7 +770,7 @@ void TSegment::roll_vertex( gfx::basic_vertex &Vertex, glm::vec3 const &Position
|
||||
|
||||
bool TSegment::RenderLoftSteps( gfx::vertex_array &Output, glm::dvec3 const &Origin, gfx::vertex_array const &ShapePoints, double fTextureLength, int const Steps ) const
|
||||
{
|
||||
if( fTsBuffer.empty() || Steps < 1 || fLength <= 0.0 )
|
||||
if( fTs.empty() || Steps < 1 || fLength <= 0.0 )
|
||||
return false;
|
||||
|
||||
auto const shapepoints { ShapePoints.size() / 2 };
|
||||
@@ -1431,7 +1432,7 @@ gfx::vertex_array const &low_section::profile_at( gfx::vertex_array const &Shape
|
||||
|
||||
bool TSegment::RenderLoftSpan( gfx::vertex_array &Output, glm::dvec3 const &Origin, gfx::vertex_array const &ShapePoints, loft_span const &Span ) const {
|
||||
|
||||
if( fTsBuffer.empty() || Span.to - Span.from < 0.001 || ShapePoints.empty() )
|
||||
if( fTs.empty() || Span.to - Span.from < 0.001 || ShapePoints.empty() )
|
||||
return false;
|
||||
|
||||
auto const stations { loft_stations( Span, ShapePoints, std::max( fStep, 0.2 ), m_dense ) };
|
||||
|
||||
+7
-3
@@ -116,8 +116,12 @@ class TSegment
|
||||
};
|
||||
setting m_setting;
|
||||
double fLength { -1.0 }; // długość policzona
|
||||
std::vector<double> fTsBuffer; // wartości parametru krzywej t dla równych odcinków s
|
||||
std::vector<double> fTsSlope; // nachylenie dt/du (= dt/ds * fStep) w tych samych punktach
|
||||
// the curve at equal intervals of s, for FastGetTFromS(): the two it reads of a point side by side, in one cache line
|
||||
struct ts_point {
|
||||
double t; // wartość parametru krzywej t dla równych odcinków s
|
||||
double slope; // nachylenie dt/du (= dt/ds * fStep) w tym punkcie
|
||||
};
|
||||
std::vector<ts_point> fTs;
|
||||
double fStep = 0.0;
|
||||
int iSegCount = 0; // ilość odcinków do rysowania krzywej
|
||||
double fDirection = 0.0; // Ra: kąt prostego w planie; dla łuku kąt od Point1
|
||||
@@ -275,7 +279,7 @@ public:
|
||||
inline
|
||||
int
|
||||
RaSegCount() const {
|
||||
return fTsBuffer.empty() ? 1 : iSegCount; };
|
||||
return fTs.empty() ? 1 : iSegCount; };
|
||||
|
||||
void
|
||||
render_lines(std::vector<gfx::basic_vertex> &out, float quality = 1.0f) const;
|
||||
|
||||
Reference in New Issue
Block a user