|
25 | 25 | #include "qgsfeatureid.h"
|
26 | 26 | #include "qgsgeometry.h"
|
27 | 27 | #include "qgsrectangle.h"
|
| 28 | +#include "qgsfeatureiterator.h" |
| 29 | + |
| 30 | +///@cond PRIVATE |
| 31 | + |
| 32 | +QgsMeshFeatureIterator::QgsMeshFeatureIterator( QgsMesh *mesh ) |
| 33 | + : QgsAbstractFeatureIterator( QgsFeatureRequest() ) |
| 34 | + , mMesh( mesh ) |
| 35 | +{} |
| 36 | + |
| 37 | +QgsMeshFeatureIterator::~QgsMeshFeatureIterator() = default; |
| 38 | + |
| 39 | +bool QgsMeshFeatureIterator::rewind() |
| 40 | +{ |
| 41 | + it = 0; |
| 42 | + return true; |
| 43 | +} |
| 44 | +bool QgsMeshFeatureIterator::close() |
| 45 | +{ |
| 46 | + mMesh = nullptr; |
| 47 | + return true; |
| 48 | +} |
| 49 | + |
| 50 | +bool QgsMeshFeatureIterator::fetchFeature( QgsFeature &f ) |
| 51 | +{ |
| 52 | + if ( !mMesh || mMesh->faces.size() <= it ) |
| 53 | + return false; |
| 54 | + |
| 55 | + const QgsMeshFace &face = mMesh->faces.at( it ) ; |
| 56 | + QgsGeometry geom = QgsMeshUtils::toGeometry( face, mMesh->vertices ); |
| 57 | + f.setGeometry( geom ); |
| 58 | + ++it; |
| 59 | + return true; |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +///@endcond |
28 | 64 |
|
29 | 65 | static void ENP_centroid_step( const QPolygonF &pX, double &cx, double &cy, double &signedArea, int i, int i1 )
|
30 | 66 | {
|
@@ -75,21 +111,28 @@ void QgsTriangularMesh::update( QgsMesh *nativeMesh, QgsRenderContext *context )
|
75 | 111 | Q_ASSERT( nativeMesh );
|
76 | 112 | Q_ASSERT( context );
|
77 | 113 |
|
78 |
| - mSpatialIndex = QgsSpatialIndex(); |
| 114 | + // FIND OUT IF UPDATE IS NEEDED |
| 115 | + if ( mTriangularMesh.vertices.size() >= nativeMesh->vertices.size() && |
| 116 | + mTriangularMesh.faces.size() >= nativeMesh->faces.size() && |
| 117 | + mCoordinateTransform.sourceCrs() == context->coordinateTransform().sourceCrs() && |
| 118 | + mCoordinateTransform.destinationCrs() == context->coordinateTransform().destinationCrs() ) |
| 119 | + return; |
| 120 | + |
| 121 | + // CLEAN-UP |
79 | 122 | mTriangularMesh.vertices.clear();
|
80 | 123 | mTriangularMesh.faces.clear();
|
81 | 124 | mTrianglesToNativeFaces.clear();
|
82 | 125 | mNativeMeshFaceCentroids.clear();
|
83 | 126 |
|
84 | 127 | // TRANSFORM VERTICES
|
85 |
| - QgsCoordinateTransform transform = context->coordinateTransform(); |
| 128 | + mCoordinateTransform = context->coordinateTransform(); |
86 | 129 | mTriangularMesh.vertices.resize( nativeMesh->vertices.size() );
|
87 | 130 | for ( int i = 0; i < nativeMesh->vertices.size(); ++i )
|
88 | 131 | {
|
89 | 132 | const QgsMeshVertex &vertex = nativeMesh->vertices.at( i );
|
90 |
| - if ( transform.isValid() ) |
| 133 | + if ( mCoordinateTransform.isValid() ) |
91 | 134 | {
|
92 |
| - QgsPointXY mapPoint = transform.transform( QgsPointXY( vertex.x(), vertex.y() ) ); |
| 135 | + QgsPointXY mapPoint = mCoordinateTransform.transform( QgsPointXY( vertex.x(), vertex.y() ) ); |
93 | 136 | QgsMeshVertex mapVertex( mapPoint );
|
94 | 137 | mapVertex.setZ( vertex.z() );
|
95 | 138 | mapVertex.setM( vertex.m() );
|
@@ -152,12 +195,7 @@ void QgsTriangularMesh::update( QgsMesh *nativeMesh, QgsRenderContext *context )
|
152 | 195 | }
|
153 | 196 |
|
154 | 197 | // CALCULATE SPATIAL INDEX
|
155 |
| - for ( int i = 0; i < mTriangularMesh.faces.size(); ++i ) |
156 |
| - { |
157 |
| - const QgsMeshFace &face = mTriangularMesh.faces.at( i ) ; |
158 |
| - QgsGeometry geom = QgsMeshUtils::toGeometry( face, mTriangularMesh.vertices ); |
159 |
| - ( void )mSpatialIndex.insertFeature( i, geom.boundingBox() ); |
160 |
| - } |
| 198 | + mSpatialIndex = QgsSpatialIndex( new QgsMeshFeatureIterator( &mTriangularMesh ) ); |
161 | 199 | }
|
162 | 200 |
|
163 | 201 | const QVector<QgsMeshVertex> &QgsTriangularMesh::vertices() const
|
|
0 commit comments