Skip to content

Commit

Permalink
Optimise storage/calculation of geometry bounding boxes
Browse files Browse the repository at this point in the history
- removes storage of bounding box from QgsPointV2 (gives significant
decrease in size of object)
- more efficient calculation of bounding box for linestrings

Additionally, this commit moves the bounding box invalidation to a
virtual QgsAbstractGeometryV2::clearCache() method, so that other
non-bounding box caches can also be cleared when the geometry
is modified.
  • Loading branch information
nyalldawson committed Feb 21, 2016
1 parent fef9c93 commit 50f01a2
Show file tree
Hide file tree
Showing 26 changed files with 196 additions and 84 deletions.
16 changes: 10 additions & 6 deletions python/core/geometry/qgsabstractgeometryv2.sip
Expand Up @@ -85,12 +85,7 @@ class QgsAbstractGeometryV2

/** Returns the minimal bounding box for the geometry
*/
QgsRectangle boundingBox() const;

/** Calculates the minimal bounding box for the geometry. Derived classes should override this method
* to return the correct bounding box.
*/
virtual QgsRectangle calculateBoundingBox() const;
virtual QgsRectangle boundingBox() const = 0;

//mm-sql interface
/** Returns the inherent dimension of the geometry. For example, this is 0 for a point geometry,
Expand Down Expand Up @@ -373,4 +368,13 @@ class QgsAbstractGeometryV2
/** Updates the geometry type based on whether sub geometries contain z or m values.
*/
void setZMTypeFromSubGeometry( const QgsAbstractGeometryV2* subggeom, QgsWKBTypes::Type baseGeomType );

/** Default calculator for the minimal bounding box for the geometry. Derived classes should override this method
* if a more efficient bounding box calculation is available.
*/
virtual QgsRectangle calculateBoundingBox() const;

/** Clears any cached parameters associated with the geometry, eg bounding boxes
*/
virtual void clearCache() const;
};
6 changes: 4 additions & 2 deletions python/core/geometry/qgscircularstringv2.sip
Expand Up @@ -16,8 +16,6 @@ class QgsCircularStringV2: public QgsCurveV2
virtual QgsCircularStringV2* clone() const;
virtual void clear();

virtual QgsRectangle calculateBoundingBox() const;

virtual bool fromWkb( QgsConstWkbPtr wkb );
virtual bool fromWkt( const QString& wkt );

Expand Down Expand Up @@ -109,6 +107,10 @@ class QgsCircularStringV2: public QgsCurveV2
virtual bool dropZValue();
virtual bool dropMValue();

protected:

virtual QgsRectangle calculateBoundingBox() const;

private:
//helper methods for curveToLine
void segmentize( const QgsPointV2& p1, const QgsPointV2& p2, const QgsPointV2& p3, QList<QgsPointV2>& points ) const;
Expand Down
6 changes: 4 additions & 2 deletions python/core/geometry/qgscompoundcurvev2.sip
Expand Up @@ -18,8 +18,6 @@ class QgsCompoundCurveV2: public QgsCurveV2
virtual QgsCompoundCurveV2* clone() const;
virtual void clear();

virtual QgsRectangle calculateBoundingBox() const;

virtual bool fromWkb( QgsConstWkbPtr wkb );
virtual bool fromWkt( const QString& wkt );

Expand Down Expand Up @@ -96,4 +94,8 @@ class QgsCompoundCurveV2: public QgsCurveV2
virtual bool dropZValue();
virtual bool dropMValue();

protected:

virtual QgsRectangle calculateBoundingBox() const;

};
6 changes: 4 additions & 2 deletions python/core/geometry/qgscurvepolygonv2.sip
Expand Up @@ -15,8 +15,6 @@ class QgsCurvePolygonV2: public QgsSurfaceV2
virtual QgsCurvePolygonV2* clone() const;
void clear();


virtual QgsRectangle calculateBoundingBox() const;
virtual bool fromWkb( QgsConstWkbPtr wkb );
virtual bool fromWkt( const QString& wkt );

Expand Down Expand Up @@ -88,4 +86,8 @@ class QgsCurvePolygonV2: public QgsSurfaceV2
virtual bool addMValue( double mValue = 0 );
virtual bool dropZValue();
virtual bool dropMValue();

protected:

virtual QgsRectangle calculateBoundingBox() const;
};
7 changes: 7 additions & 0 deletions python/core/geometry/qgscurvev2.sip
Expand Up @@ -81,6 +81,8 @@ class QgsCurveV2: public QgsAbstractGeometryV2
virtual int partCount() const;
virtual QgsPointV2 vertexAt( QgsVertexId id ) const;

virtual QgsRectangle boundingBox() const;

/** Drops any Z dimensions which exist in the geometry.
* @returns true if Z values were present and have been removed
* @see dropMValue()
Expand All @@ -94,4 +96,9 @@ class QgsCurveV2: public QgsAbstractGeometryV2
* @note added in QGIS 2.14
*/
virtual bool dropMValue() = 0;

protected:

virtual void clearCache() const;

};
5 changes: 4 additions & 1 deletion python/core/geometry/qgsgeometrycollectionv2.sip
Expand Up @@ -66,7 +66,7 @@ class QgsGeometryCollectionV2: public QgsAbstractGeometryV2
QDomElement asGML3( QDomDocument& doc, int precision = 17, const QString& ns = "gml" ) const;
QString asJSON( int precision = 17 ) const;

virtual QgsRectangle calculateBoundingBox() const;
virtual QgsRectangle boundingBox() const;

virtual void coordinateSequence( QList< QList< QList< QgsPointV2 > > >& coord /Out/ ) const;
virtual double closestSegment( const QgsPointV2& pt, QgsPointV2& segmentPt, QgsVertexId& vertexAfter, bool* leftOf, double epsilon ) const;
Expand Down Expand Up @@ -113,4 +113,7 @@ class QgsGeometryCollectionV2: public QgsAbstractGeometryV2
*/
bool fromCollectionWkt( const QString &wkt, const QList<QgsAbstractGeometryV2*>& subtypes, const QString& defaultChildWkbType = QString() );

virtual QgsRectangle calculateBoundingBox() const;
virtual void clearCache() const;

};
4 changes: 4 additions & 0 deletions python/core/geometry/qgslinestringv2.sip
Expand Up @@ -162,4 +162,8 @@ class QgsLineStringV2: public QgsCurveV2

virtual bool convertTo( QgsWKBTypes::Type type );

protected:

virtual QgsRectangle calculateBoundingBox() const;

};
2 changes: 1 addition & 1 deletion python/core/geometry/qgspointv2.sip
Expand Up @@ -139,6 +139,7 @@ class QgsPointV2: public QgsAbstractGeometryV2
QPointF toQPointF() const;

//implementation of inherited methods
virtual QgsRectangle boundingBox() const;
virtual QString geometryType() const;
virtual int dimension() const;
virtual QgsPointV2* clone() const /Factory/;
Expand All @@ -151,7 +152,6 @@ class QgsPointV2: public QgsAbstractGeometryV2
QDomElement asGML2( QDomDocument& doc, int precision = 17, const QString& ns = "gml" ) const;
QDomElement asGML3( QDomDocument& doc, int precision = 17, const QString& ns = "gml" ) const;
QString asJSON( int precision = 17 ) const;
virtual QgsRectangle calculateBoundingBox() const;
void draw( QPainter& p ) const;
void transform( const QgsCoordinateTransform& ct, QgsCoordinateTransform::TransformDirection d = QgsCoordinateTransform::ForwardTransform );
void transform( const QTransform& t );
Expand Down
7 changes: 7 additions & 0 deletions python/core/geometry/qgssurfacev2.sip
Expand Up @@ -7,4 +7,11 @@ class QgsSurfaceV2: public QgsAbstractGeometryV2
public:

virtual QgsPolygonV2* surfaceToPolygon() const = 0;

virtual QgsRectangle boundingBox() const;

protected:

virtual void clearCache() const;

};
9 changes: 0 additions & 9 deletions src/core/geometry/qgsabstractgeometryv2.cpp
Expand Up @@ -45,15 +45,6 @@ QgsAbstractGeometryV2& QgsAbstractGeometryV2::operator=( const QgsAbstractGeomet
return *this;
}

QgsRectangle QgsAbstractGeometryV2::boundingBox() const
{
if ( mBoundingBox.isNull() )
{
mBoundingBox = calculateBoundingBox();
}
return mBoundingBox;
}

bool QgsAbstractGeometryV2::is3D() const
{
return QgsWKBTypes::hasZ( mWkbType );
Expand Down
18 changes: 11 additions & 7 deletions src/core/geometry/qgsabstractgeometryv2.h
Expand Up @@ -56,12 +56,7 @@ class CORE_EXPORT QgsAbstractGeometryV2

/** Returns the minimal bounding box for the geometry
*/
QgsRectangle boundingBox() const;

/** Calculates the minimal bounding box for the geometry. Derived classes should override this method
* to return the correct bounding box.
*/
virtual QgsRectangle calculateBoundingBox() const;
virtual QgsRectangle boundingBox() const = 0;

//mm-sql interface
/** Returns the inherent dimension of the geometry. For example, this is 0 for a point geometry,
Expand Down Expand Up @@ -353,11 +348,20 @@ class CORE_EXPORT QgsAbstractGeometryV2

protected:
QgsWKBTypes::Type mWkbType;
mutable QgsRectangle mBoundingBox;

/** Updates the geometry type based on whether sub geometries contain z or m values.
*/
void setZMTypeFromSubGeometry( const QgsAbstractGeometryV2* subggeom, QgsWKBTypes::Type baseGeomType );

/** Default calculator for the minimal bounding box for the geometry. Derived classes should override this method
* if a more efficient bounding box calculation is available.
*/
virtual QgsRectangle calculateBoundingBox() const;

/** Clears any cached parameters associated with the geometry, eg bounding boxes
*/
virtual void clearCache() const {}

};


Expand Down
6 changes: 3 additions & 3 deletions src/core/geometry/qgscircularstringv2.cpp
Expand Up @@ -723,7 +723,7 @@ bool QgsCircularStringV2::insertVertex( QgsVertexId position, const QgsPointV2&
{
insertVertexBetween( position.vertex, position.vertex + 1, position.vertex - 1 );
}
mBoundingBox = QgsRectangle(); //set bounding box invalid
clearCache(); //set bounding box invalid
return true;
}

Expand All @@ -744,7 +744,7 @@ bool QgsCircularStringV2::moveVertex( QgsVertexId position, const QgsPointV2& ne
{
mM[position.vertex] = newPos.m();
}
mBoundingBox = QgsRectangle(); //set bounding box invalid
clearCache(); //set bounding box invalid
return true;
}

Expand Down Expand Up @@ -772,7 +772,7 @@ bool QgsCircularStringV2::deleteVertex( QgsVertexId position )
deleteVertex( position.vertex - 1 );
}

mBoundingBox = QgsRectangle(); //set bounding box invalid
clearCache(); //set bounding box invalid
return true;
}

Expand Down
7 changes: 5 additions & 2 deletions src/core/geometry/qgscircularstringv2.h
Expand Up @@ -41,8 +41,6 @@ class CORE_EXPORT QgsCircularStringV2: public QgsCurveV2
virtual QgsCircularStringV2* clone() const override;
virtual void clear() override;

virtual QgsRectangle calculateBoundingBox() const override;

virtual bool fromWkb( QgsConstWkbPtr wkb ) override;
virtual bool fromWkt( const QString& wkt ) override;

Expand Down Expand Up @@ -134,6 +132,10 @@ class CORE_EXPORT QgsCircularStringV2: public QgsCurveV2
virtual bool dropZValue() override;
virtual bool dropMValue() override;

protected:

virtual QgsRectangle calculateBoundingBox() const override;

private:
QVector<double> mX;
QVector<double> mY;
Expand All @@ -152,6 +154,7 @@ class CORE_EXPORT QgsCircularStringV2: public QgsCurveV2
const QgsPointV2& pt, QgsPointV2& segmentPt, QgsVertexId& vertexAfter, bool* leftOf, double epsilon );
void insertVertexBetween( int after, int before, int pointOnCircle );
void deleteVertex( int i );

};

#endif // QGSCIRCULARSTRING_H
10 changes: 5 additions & 5 deletions src/core/geometry/qgscompoundcurvev2.cpp
Expand Up @@ -89,10 +89,10 @@ QgsRectangle QgsCompoundCurveV2::calculateBoundingBox() const
return QgsRectangle();
}

QgsRectangle bbox = mCurves.at( 0 )->calculateBoundingBox();
QgsRectangle bbox = mCurves.at( 0 )->boundingBox();
for ( int i = 1; i < mCurves.size(); ++i )
{
QgsRectangle curveBox = mCurves.at( i )->calculateBoundingBox();
QgsRectangle curveBox = mCurves.at( i )->boundingBox();
bbox.combineExtentWith( &curveBox );
}
return bbox;
Expand Down Expand Up @@ -497,7 +497,7 @@ bool QgsCompoundCurveV2::insertVertex( QgsVertexId position, const QgsPointV2& v
bool success = mCurves.at( curveId )->insertVertex( curveIds.at( 0 ).second, vertex );
if ( success )
{
mBoundingBox = QgsRectangle(); //bbox changed
clearCache(); //bbox changed
}
return success;
}
Expand All @@ -514,7 +514,7 @@ bool QgsCompoundCurveV2::moveVertex( QgsVertexId position, const QgsPointV2& new
bool success = !curveIds.isEmpty();
if ( success )
{
mBoundingBox = QgsRectangle(); //bbox changed
clearCache(); //bbox changed
}
return success;
}
Expand All @@ -531,7 +531,7 @@ bool QgsCompoundCurveV2::deleteVertex( QgsVertexId position )
bool success = !curveIds.isEmpty();
if ( success )
{
mBoundingBox = QgsRectangle(); //bbox changed
clearCache(); //bbox changed
}
return success;
}
Expand Down
7 changes: 5 additions & 2 deletions src/core/geometry/qgscompoundcurvev2.h
Expand Up @@ -42,8 +42,6 @@ class CORE_EXPORT QgsCompoundCurveV2: public QgsCurveV2
virtual QgsCompoundCurveV2* clone() const override;
virtual void clear() override;

virtual QgsRectangle calculateBoundingBox() const override;

virtual bool fromWkb( QgsConstWkbPtr wkb ) override;
virtual bool fromWkt( const QString& wkt ) override;

Expand Down Expand Up @@ -120,11 +118,16 @@ class CORE_EXPORT QgsCompoundCurveV2: public QgsCurveV2
virtual bool dropZValue() override;
virtual bool dropMValue() override;

protected:

virtual QgsRectangle calculateBoundingBox() const override;

private:
QList< QgsCurveV2* > mCurves;
/** Turns a vertex id for the compound curve into one or more ids for the subcurves
@return the index of the subcurve or -1 in case of error*/
QList< QPair<int, QgsVertexId> > curveVertexId( QgsVertexId id ) const;

};

#endif // QGSCOMPOUNDCURVEV2_H
10 changes: 5 additions & 5 deletions src/core/geometry/qgscurvepolygonv2.cpp
Expand Up @@ -209,7 +209,7 @@ QgsRectangle QgsCurvePolygonV2::calculateBoundingBox() const
{
if ( mExteriorRing )
{
return mExteriorRing->calculateBoundingBox();
return mExteriorRing->boundingBox();
}
return QgsRectangle();
}
Expand Down Expand Up @@ -666,7 +666,7 @@ bool QgsCurvePolygonV2::insertVertex( QgsVertexId vId, const QgsPointV2& vertex
else if ( vId.vertex == n )
ring->moveVertex( QgsVertexId( 0, 0, 0 ), vertex );

mBoundingBox = QgsRectangle();
clearCache();
return true;
}

Expand All @@ -687,7 +687,7 @@ bool QgsCurvePolygonV2::moveVertex( QgsVertexId vId, const QgsPointV2& newPos )
ring->moveVertex( QgsVertexId( vId.part, vId.ring, n - 1 ), newPos );
else if ( vId.vertex == n - 1 )
ring->moveVertex( QgsVertexId( vId.part, vId.ring, 0 ), newPos );
mBoundingBox = QgsRectangle();
clearCache();
}
return success;
}
Expand Down Expand Up @@ -717,7 +717,7 @@ bool QgsCurvePolygonV2::deleteVertex( QgsVertexId vId )
{
removeInteriorRing( vId.ring - 1 );
}
mBoundingBox = QgsRectangle();
clearCache();
return true;
}

Expand All @@ -729,7 +729,7 @@ bool QgsCurvePolygonV2::deleteVertex( QgsVertexId vId )
ring->moveVertex( QgsVertexId( 0, 0, n - 2 ), ring->vertexAt( QgsVertexId( 0, 0, 0 ) ) );
else if ( vId.vertex == n - 1 )
ring->moveVertex( QgsVertexId( 0, 0, 0 ), ring->vertexAt( QgsVertexId( 0, 0, n - 2 ) ) );
mBoundingBox = QgsRectangle();
clearCache();
}
return success;
}
Expand Down
3 changes: 1 addition & 2 deletions src/core/geometry/qgscurvepolygonv2.h
Expand Up @@ -41,8 +41,6 @@ class CORE_EXPORT QgsCurvePolygonV2: public QgsSurfaceV2
virtual QgsCurvePolygonV2* clone() const override;
void clear() override;


virtual QgsRectangle calculateBoundingBox() const override;
virtual bool fromWkb( QgsConstWkbPtr wkb ) override;
virtual bool fromWkt( const QString& wkt ) override;

Expand Down Expand Up @@ -120,6 +118,7 @@ class CORE_EXPORT QgsCurvePolygonV2: public QgsSurfaceV2
QgsCurveV2* mExteriorRing;
QList<QgsCurveV2*> mInteriorRings;

virtual QgsRectangle calculateBoundingBox() const override;
};

#endif // QGSCURVEPOLYGONV2_H

0 comments on commit 50f01a2

Please sign in to comment.