From 712b82008f2f102247c4badc04d0b6f942f7cfef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Bartoletti?= Date: Tue, 14 Jan 2025 11:35:39 +0100 Subject: [PATCH] chore(optimization): Tiny optimization with pre-computed cos/sin from angle for QgsEllipse --- src/core/geometry/qgsellipse.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/core/geometry/qgsellipse.cpp b/src/core/geometry/qgsellipse.cpp index 43775447bf94..57b0430fbc24 100644 --- a/src/core/geometry/qgsellipse.cpp +++ b/src/core/geometry/qgsellipse.cpp @@ -221,12 +221,17 @@ void QgsEllipse::pointsInternal( unsigned int segments, QVector &x, QVec const bool hasZ = mCenter.is3D(); const bool hasM = mCenter.isMeasure(); - std::vector t( segments ); const QgsPoint p1 = mCenter.project( mSemiMajorAxis, mAzimuth ); const double azimuth = std::atan2( p1.y() - mCenter.y(), p1.x() - mCenter.x() ); + const double cosAzimuth = std::cos( azimuth ); + const double sinAzimuth = std::sin( azimuth ); + + std::vector cosT( segments ), sinT( segments ); for ( unsigned int i = 0; i < segments; ++i ) { - t[i] = 2 * M_PI - ( ( 2 * M_PI ) / segments * i ); // Since the algorithm used rotates in the trigonometric direction (counterclockwise) + double angle = 2 * M_PI - ( ( 2 * M_PI ) / segments * i ); // Since the algorithm used rotates in the trigonometric direction (counterclockwise) + cosT[i] = std::cos( angle ); + sinT[i] = std::sin( angle ); } x.resize( segments ); @@ -235,21 +240,20 @@ void QgsEllipse::pointsInternal( unsigned int segments, QVector &x, QVec z.resize( segments ); if ( hasM ) m.resize( segments ); + double *xOut = x.data(); double *yOut = y.data(); double *zOut = hasZ ? z.data() : nullptr; double *mOut = hasM ? m.data() : nullptr; - const double cosAzimuth = std::cos( azimuth ); - const double sinAzimuth = std::sin( azimuth ); - for ( double it : t ) + for ( unsigned int i = 0; i < segments; ++i ) { *xOut++ = centerX + - mSemiMajorAxis * std::cos( it ) * cosAzimuth - - mSemiMinorAxis * std::sin( it ) * sinAzimuth; + mSemiMajorAxis * cosT[i] * cosAzimuth - + mSemiMinorAxis * sinT[i] * sinAzimuth; *yOut++ = centerY + - mSemiMajorAxis * std::cos( it ) * sinAzimuth + - mSemiMinorAxis * std::sin( it ) * cosAzimuth; + mSemiMajorAxis * cosT[i] * sinAzimuth + + mSemiMinorAxis * sinT[i] * cosAzimuth; if ( zOut ) *zOut++ = centerZ; if ( mOut )