Skip to content
Permalink
Browse files

export mesh contours

  • Loading branch information
vcloarec authored and PeterPetrik committed Nov 10, 2020
1 parent d9fa1a6 commit ceb7c2f9bfc795f0c8b34a8af43b1469a3795746
@@ -45,13 +45,21 @@ QgsMeshContours::QgsMeshContours( QgsMeshLayer *layer )
if ( mMeshLayer->dataProvider()->contains( QgsMesh::ElementType::Edge ) )
return;

mNativeMesh.reset( new QgsMesh() );
mMeshLayer->dataProvider()->populateMesh( mNativeMesh.get() );

mTriangularMesh.reset( new QgsTriangularMesh );
mTriangularMesh->update( mNativeMesh.get() );
mMeshLayer->dataProvider()->populateMesh( &mNativeMesh );
mTriangularMesh.update( &mNativeMesh );
}

QgsMeshContours::QgsMeshContours(
const QgsTriangularMesh &triangularMesh,
const QgsMesh &nativeMesh,
const QVector<double> &datasetValues,
QgsMeshDataBlock scalarActiveFaceFlagValues ):
mTriangularMesh( triangularMesh )
, mNativeMesh( nativeMesh )
, mDatasetValues( datasetValues )
, mScalarActiveFaceFlagValues( scalarActiveFaceFlagValues )
{}

QgsMeshContours::~QgsMeshContours() = default;

QgsGeometry QgsMeshContours::exportPolygons(
@@ -62,10 +70,16 @@ QgsGeometry QgsMeshContours::exportPolygons(
QgsFeedback *feedback
)
{
// Check if the layer/mesh is valid
if ( !mTriangularMesh )
if ( !mMeshLayer )
return QgsGeometry();

populateCache( index, method );

return exportPolygons( min_value, max_value, feedback );
}

QgsGeometry QgsMeshContours::exportPolygons( double min_value, double max_value, QgsFeedback *feedback )
{
if ( min_value > max_value )
{
double tmp = max_value;
@@ -76,12 +90,11 @@ QgsGeometry QgsMeshContours::exportPolygons(
QVector<QgsGeometry> multiPolygon;

// STEP 1: Get Data
populateCache( index, method );
const QVector<QgsMeshVertex> vertices = mTriangularMesh->vertices();
const QVector<int> &trianglesToNativeFaces = mTriangularMesh->trianglesToNativeFaces();
const QVector<QgsMeshVertex> vertices = mTriangularMesh.vertices();
const QVector<int> &trianglesToNativeFaces = mTriangularMesh.trianglesToNativeFaces();

// STEP 2: For each triangle get the contour line
for ( int i = 0; i < mTriangularMesh->triangles().size(); ++i )
for ( int i = 0; i < mTriangularMesh.triangles().size(); ++i )
{
if ( feedback && feedback->isCanceled() )
break;
@@ -90,7 +103,7 @@ QgsGeometry QgsMeshContours::exportPolygons(
if ( !mScalarActiveFaceFlagValues.active( nativeIndex ) )
continue;

const QgsMeshFace &triangle = mTriangularMesh->triangles().at( i );
const QgsMeshFace &triangle = mTriangularMesh.triangles().at( i );
const int indices[3] =
{
triangle.at( 0 ),
@@ -258,19 +271,25 @@ QgsGeometry QgsMeshContours::exportLines( const QgsMeshDatasetIndex &index,
QgsFeedback *feedback )
{
// Check if the layer/mesh is valid
if ( !mTriangularMesh )
if ( !mMeshLayer )
return QgsGeometry();

populateCache( index, method );

return exportLines( value, feedback );
}

QgsGeometry QgsMeshContours::exportLines( double value, QgsFeedback *feedback )
{
std::unique_ptr<QgsMultiLineString> multiLineString( new QgsMultiLineString() );
QSet<QPair<int, int>> exactEdges;

// STEP 1: Get Data
populateCache( index, method );
QVector<QgsMeshVertex> vertices = mTriangularMesh->vertices();
const QVector<int> &trianglesToNativeFaces = mTriangularMesh->trianglesToNativeFaces();
QVector<QgsMeshVertex> vertices = mTriangularMesh.vertices();
const QVector<int> &trianglesToNativeFaces = mTriangularMesh.trianglesToNativeFaces();

// STEP 2: For each triangle get the contour line
for ( int i = 0; i < mTriangularMesh->triangles().size(); ++i )
for ( int i = 0; i < mTriangularMesh.triangles().size(); ++i )
{
if ( feedback && feedback->isCanceled() )
break;
@@ -279,7 +298,7 @@ QgsGeometry QgsMeshContours::exportLines( const QgsMeshDatasetIndex &index,
if ( !mScalarActiveFaceFlagValues.active( nativeIndex ) )
continue;

const QgsMeshFace &triangle = mTriangularMesh->triangles().at( i );
const QgsMeshFace &triangle = mTriangularMesh.triangles().at( i );

const int indices[3] =
{
@@ -382,10 +401,13 @@ QgsGeometry QgsMeshContours::exportLines( const QgsMeshDatasetIndex &index,

void QgsMeshContours::populateCache( const QgsMeshDatasetIndex &index, QgsMeshRendererScalarSettings::DataResamplingMethod method )
{
if ( !mMeshLayer )
return;

if ( mCachedIndex != index )
{
bool scalarDataOnVertices = mMeshLayer->dataProvider()->datasetGroupMetadata( index ).dataType() == QgsMeshDatasetGroupMetadata::DataOnVertices;
int count = scalarDataOnVertices ? mNativeMesh->vertices.count() : mNativeMesh->faces.count();
int count = scalarDataOnVertices ? mNativeMesh.vertices.count() : mNativeMesh.faces.count();

// populate scalar values
QgsMeshDataBlock vals = QgsMeshLayerUtils::datasetValues(
@@ -407,15 +429,15 @@ void QgsMeshContours::populateCache( const QgsMeshDatasetIndex &index, QgsMeshRe
mScalarActiveFaceFlagValues = mMeshLayer->dataProvider()->areFacesActive(
index,
0,
mNativeMesh->faces.count() );
mNativeMesh.faces.count() );

// for data on faces, there could be request to interpolate the data to vertices
if ( ( !scalarDataOnVertices ) )
{
mDatasetValues = QgsMeshLayerUtils::interpolateFromFacesData(
mDatasetValues,
mNativeMesh.get(),
mTriangularMesh.get(),
&mNativeMesh,
&mTriangularMesh,
&mScalarActiveFaceFlagValues,
method
);
@@ -57,21 +57,46 @@ class ANALYSIS_EXPORT QgsMeshContours
* \param layer mesh layer to be associated with this exporter
*/
QgsMeshContours( QgsMeshLayer *layer );

/**
* Constructs the mesh contours exporter directly with triangular mesh and dataset values to populate the cache with them.
* The cache can't be changed.
* This instance can be used in a thread safe way.
* \param layer mesh layer to be associated with this exporter
*/
QgsMeshContours( const QgsTriangularMesh &triangularMesh,
const QgsMesh &nativeMesh,
const QVector<double> &datasetValues,
QgsMeshDataBlock scalarActiveFaceFlagValues );


~QgsMeshContours();

/**
* Exports multi line string containing the contour line for particular dataset and value
* \param index dataset index
* Exports multi line string containing the contour line for particular dataset and value.
* \param index dataset index that is used to update the cache
* \param value value of the contour line
* \param method for datasets defined on faces, the method will be used to convert data to vertices
* \param feedback optional feedback object for progress and cancellation support
* \returns MultiLineString geometry containing contour lines
*
* \note not thread safe
*/
QgsGeometry exportLines( const QgsMeshDatasetIndex &index,
double value,
QgsMeshRendererScalarSettings::DataResamplingMethod method,
QgsFeedback *feedback = nullptr );

/**
* Exports multi line string containing the contour line for particular value using the data stored in the cache
* \param value value of the contour line
* \param feedback optional feedback object for progress and cancellation support
* \returns MultiLineString geometry containing contour lines
*
* \note thread safe
*/
QgsGeometry exportLines( double value, QgsFeedback *feedback = nullptr );

/**
* Exports multi polygons representing the areas with values in range for particular dataset
* \param index dataset index
@@ -80,13 +105,27 @@ class ANALYSIS_EXPORT QgsMeshContours
* \param method for datasets defined on faces, the method will be used to convert data to vertices
* \param feedback optional feedback object for progress and cancellation support
* \returns MultiPolygon geometry containing contour polygons
*
* \note not thread safe
*/
QgsGeometry exportPolygons( const QgsMeshDatasetIndex &index,
double min_value,
double max_value,
QgsMeshRendererScalarSettings::DataResamplingMethod method,
QgsFeedback *feedback = nullptr );

/**
* Exports multi polygons representing the areas with values in range using the data stored in the cache
* \param min_value minimum of the value interval for contour polygon
* \param max_value maximum of the value interval for contour polygon
* \param feedback optional feedback object for progress and cancellation support
* \returns MultiPolygon geometry containing contour polygons
*
* \note thread safe
*/
QgsGeometry exportPolygons( double min_value, double max_value, QgsFeedback *feedback = nullptr );


private:
void populateCache(
const QgsMeshDatasetIndex &index,
@@ -96,10 +135,10 @@ class ANALYSIS_EXPORT QgsMeshContours

// cached values for particular index & interpolation method & layer
QgsMeshDatasetIndex mCachedIndex;
std::shared_ptr<QgsTriangularMesh> mTriangularMesh;
std::shared_ptr<QgsMesh> mNativeMesh;
QgsMeshDataBlock mScalarActiveFaceFlagValues;
QgsTriangularMesh mTriangularMesh;
QgsMesh mNativeMesh;
QVector<double> mDatasetValues;
QgsMeshDataBlock mScalarActiveFaceFlagValues;
};

#endif // QGSMESHCONTOURS_H

0 comments on commit ceb7c2f

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