Skip to content
Permalink
Browse files

update spatial index only when mesh or CRS changed

  • Loading branch information
PeterPetrik committed Sep 24, 2018
1 parent 7ad1b71 commit eda339686451d6ed9504f59d04984b49f19f2dcc
Showing with 85 additions and 10 deletions.
  1. +48 −10 src/core/mesh/qgstriangularmesh.cpp
  2. +37 −0 src/core/mesh/qgstriangularmesh.h
@@ -25,6 +25,42 @@
#include "qgsfeatureid.h"
#include "qgsgeometry.h"
#include "qgsrectangle.h"
#include "qgsfeatureiterator.h"

///@cond PRIVATE

QgsMeshFeatureIterator::QgsMeshFeatureIterator( QgsMesh *mesh )
: QgsAbstractFeatureIterator( QgsFeatureRequest() )
, mMesh( mesh )
{}

QgsMeshFeatureIterator::~QgsMeshFeatureIterator() = default;

bool QgsMeshFeatureIterator::rewind()
{
it = 0;
return true;
}
bool QgsMeshFeatureIterator::close()
{
mMesh = nullptr;
return true;
}

bool QgsMeshFeatureIterator::fetchFeature( QgsFeature &f )
{
if ( !mMesh || mMesh->faces.size() <= it )
return false;

const QgsMeshFace &face = mMesh->faces.at( it ) ;
QgsGeometry geom = QgsMeshUtils::toGeometry( face, mMesh->vertices );
f.setGeometry( geom );
++it;
return true;
}


///@endcond

static void ENP_centroid_step( const QPolygonF &pX, double &cx, double &cy, double &signedArea, int i, int i1 )
{
@@ -75,21 +111,28 @@ void QgsTriangularMesh::update( QgsMesh *nativeMesh, QgsRenderContext *context )
Q_ASSERT( nativeMesh );
Q_ASSERT( context );

mSpatialIndex = QgsSpatialIndex();
// FIND OUT IF UPDATE IS NEEDED
if ( mTriangularMesh.vertices.size() >= nativeMesh->vertices.size() &&
mTriangularMesh.faces.size() >= nativeMesh->faces.size() &&
mCoordinateTransform.sourceCrs() == context->coordinateTransform().sourceCrs() &&
mCoordinateTransform.destinationCrs() == context->coordinateTransform().destinationCrs() )
return;

// CLEAN-UP
mTriangularMesh.vertices.clear();
mTriangularMesh.faces.clear();
mTrianglesToNativeFaces.clear();
mNativeMeshFaceCentroids.clear();

// TRANSFORM VERTICES
QgsCoordinateTransform transform = context->coordinateTransform();
mCoordinateTransform = context->coordinateTransform();
mTriangularMesh.vertices.resize( nativeMesh->vertices.size() );
for ( int i = 0; i < nativeMesh->vertices.size(); ++i )
{
const QgsMeshVertex &vertex = nativeMesh->vertices.at( i );
if ( transform.isValid() )
if ( mCoordinateTransform.isValid() )
{
QgsPointXY mapPoint = transform.transform( QgsPointXY( vertex.x(), vertex.y() ) );
QgsPointXY mapPoint = mCoordinateTransform.transform( QgsPointXY( vertex.x(), vertex.y() ) );
QgsMeshVertex mapVertex( mapPoint );
mapVertex.setZ( vertex.z() );
mapVertex.setM( vertex.m() );
@@ -152,12 +195,7 @@ void QgsTriangularMesh::update( QgsMesh *nativeMesh, QgsRenderContext *context )
}

// CALCULATE SPATIAL INDEX
for ( int i = 0; i < mTriangularMesh.faces.size(); ++i )
{
const QgsMeshFace &face = mTriangularMesh.faces.at( i ) ;
QgsGeometry geom = QgsMeshUtils::toGeometry( face, mTriangularMesh.vertices );
( void )mSpatialIndex.insertFeature( i, geom.boundingBox() );
}
mSpatialIndex = QgsSpatialIndex( new QgsMeshFeatureIterator( &mTriangularMesh ) );
}

const QVector<QgsMeshVertex> &QgsTriangularMesh::vertices() const
@@ -27,8 +27,10 @@
#include "qgsgeometry.h"
#include "qgsfeatureid.h"
#include "qgsspatialindex.h"
#include "qgsfeatureiterator.h"

class QgsRenderContext;
class QgsCoordinateTransform;

//! Mesh - vertices and faces
struct CORE_EXPORT QgsMesh
@@ -39,6 +41,40 @@ struct CORE_EXPORT QgsMesh
QVector<QgsMeshFace> faces;
};

///@cond PRIVATE

/**
* \ingroup core
*
* Delivers features from mesh
*
* \since QGIS 3.4
*/
class CORE_NO_EXPORT QgsMeshFeatureIterator : public QgsAbstractFeatureIterator
{
public:

/**
* This constructor creates a feature iterator, that delivers all features
*
* \param mesh The mesh to use
*/
QgsMeshFeatureIterator( QgsMesh *mesh );
~QgsMeshFeatureIterator();

bool rewind() override;
bool close() override;

protected:
bool fetchFeature( QgsFeature &f ) override;

private:
QgsMesh *mMesh = nullptr;
int it = 0;
};

///@endcond

/**
* \ingroup core
*
@@ -102,6 +138,7 @@ class CORE_EXPORT QgsTriangularMesh
QVector<QgsMeshVertex> mNativeMeshFaceCentroids;

QgsSpatialIndex mSpatialIndex;
QgsCoordinateTransform mCoordinateTransform; //coordinate transform used to convert native mesh vertices to map vertices
};

namespace QgsMeshUtils

0 comments on commit eda3396

Please sign in to comment.
You can’t perform that action at this time.