Skip to content

Commit 1c05421

Browse files
committedSep 28, 2022
Cache summed up area for curve geometry classes
Avoids recalculation when area is retrieved multiple times
1 parent 14324b5 commit 1c05421

File tree

6 files changed

+40
-7
lines changed

6 files changed

+40
-7
lines changed
 

‎python/core/auto_generated/geometry/qgscurve.sip.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ Scrolls the curve vertices so that they start with the vertex at the given index
323323

324324

325325

326+
326327
};
327328

328329
/************************************************************************

‎src/core/geometry/qgscircularstring.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,8 +1347,14 @@ bool QgsCircularString::pointAt( int node, QgsPoint &point, Qgis::VertexType &ty
13471347

13481348
void QgsCircularString::sumUpArea( double &sum ) const
13491349
{
1350-
int maxIndex = numPoints() - 2;
1350+
if ( mHasCachedSummedUpArea )
1351+
{
1352+
sum += mSummedUpArea;
1353+
return;
1354+
}
13511355

1356+
int maxIndex = numPoints() - 2;
1357+
mSummedUpArea = 0;
13521358
for ( int i = 0; i < maxIndex; i += 2 )
13531359
{
13541360
QgsPoint p1( mX[i], mY[i] );
@@ -1359,11 +1365,11 @@ void QgsCircularString::sumUpArea( double &sum ) const
13591365
if ( p1 == p3 )
13601366
{
13611367
double r2 = QgsGeometryUtils::sqrDistance2D( p1, p2 ) / 4.0;
1362-
sum += M_PI * r2;
1368+
mSummedUpArea += M_PI * r2;
13631369
continue;
13641370
}
13651371

1366-
sum += 0.5 * ( mX[i] * mY[i + 2] - mY[i] * mX[i + 2] );
1372+
mSummedUpArea += 0.5 * ( mX[i] * mY[i + 2] - mY[i] * mX[i + 2] );
13671373

13681374
//calculate area between circle and chord, then sum / subtract from total area
13691375
double midPointX = ( p1.x() + p3.x() ) / 2.0;
@@ -1397,13 +1403,16 @@ void QgsCircularString::sumUpArea( double &sum ) const
13971403

13981404
if ( !circlePointLeftOfLine )
13991405
{
1400-
sum += circleChordArea;
1406+
mSummedUpArea += circleChordArea;
14011407
}
14021408
else
14031409
{
1404-
sum -= circleChordArea;
1410+
mSummedUpArea -= circleChordArea;
14051411
}
14061412
}
1413+
1414+
mHasCachedSummedUpArea = true;
1415+
sum += mSummedUpArea;
14071416
}
14081417

14091418
bool QgsCircularString::hasCurvedSegments() const

‎src/core/geometry/qgscompoundcurve.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1148,10 +1148,19 @@ std::tuple<std::unique_ptr<QgsCurve>, std::unique_ptr<QgsCurve> > QgsCompoundCur
11481148

11491149
void QgsCompoundCurve::sumUpArea( double &sum ) const
11501150
{
1151+
if ( mHasCachedSummedUpArea )
1152+
{
1153+
sum += mSummedUpArea;
1154+
return;
1155+
}
1156+
1157+
mSummedUpArea = 0;
11511158
for ( const QgsCurve *curve : mCurves )
11521159
{
1153-
curve->sumUpArea( sum );
1160+
curve->sumUpArea( mSummedUpArea );
11541161
}
1162+
mHasCachedSummedUpArea = true;
1163+
sum += mSummedUpArea;
11551164
}
11561165

11571166
void QgsCompoundCurve::close()

‎src/core/geometry/qgscurve.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ void QgsCurve::clearCache() const
295295
mBoundingBox = QgsRectangle();
296296
mHasCachedValidity = false;
297297
mValidityFailureReason.clear();
298+
mHasCachedSummedUpArea = false;
298299
QgsAbstractGeometry::clearCache();
299300
}
300301

‎src/core/geometry/qgscurve.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ class CORE_EXPORT QgsCurve: public QgsAbstractGeometry SIP_ABSTRACT
341341
*/
342342
mutable QgsRectangle mBoundingBox;
343343

344+
mutable bool mHasCachedSummedUpArea = false;
345+
mutable double mSummedUpArea = 0;
346+
344347
private:
345348

346349
mutable bool mHasCachedValidity = false;

‎src/core/geometry/qgslinestring.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,13 @@ QgsPoint QgsLineString::centroid() const
19361936

19371937
void QgsLineString::sumUpArea( double &sum ) const
19381938
{
1939+
if ( mHasCachedSummedUpArea )
1940+
{
1941+
sum += mSummedUpArea;
1942+
return;
1943+
}
1944+
1945+
mSummedUpArea = 0;
19391946
const int maxIndex = mX.size();
19401947
if ( maxIndex == 0 )
19411948
return;
@@ -1946,10 +1953,13 @@ void QgsLineString::sumUpArea( double &sum ) const
19461953
double prevY = *y++;
19471954
for ( int i = 1; i < maxIndex; ++i )
19481955
{
1949-
sum += 0.5 * ( prevX * ( *y ) - prevY * ( *x ) );
1956+
mSummedUpArea += 0.5 * ( prevX * ( *y ) - prevY * ( *x ) );
19501957
prevX = *x++;
19511958
prevY = *y++;
19521959
}
1960+
1961+
mHasCachedSummedUpArea = true;
1962+
sum += mSummedUpArea;
19531963
}
19541964

19551965
void QgsLineString::importVerticesFromWkb( const QgsConstWkbPtr &wkb )

0 commit comments

Comments
 (0)
Please sign in to comment.