Skip to content
Permalink
Browse files

implement simple cache for rendering of mesh layer

  • Loading branch information
PeterPetrik committed Sep 24, 2018
1 parent eda3396 commit 11645ac7442f71ab54340578561a56dedbec0511
@@ -129,6 +129,7 @@ Returns the provider type for this layer




QgsMeshRendererSettings rendererSettings() const;
%Docstring
Returns renderer settings
@@ -105,6 +105,11 @@ QgsTriangularMesh *QgsMeshLayer::triangularMesh() SIP_SKIP
return mTriangularMesh.get();
}

QgsMeshLayerRendererCache *QgsMeshLayer::rendererCache()
{
return mRendererCache.get();
}

QgsMeshRendererSettings QgsMeshLayer::rendererSettings() const
{
return mRendererSettings;
@@ -229,16 +234,23 @@ void QgsMeshLayer::assignDefaultStyleToDatasetGroup( int groupIndex )

QgsMapLayerRenderer *QgsMeshLayer::createMapRenderer( QgsRenderContext &rendererContext )
{
// Native mesh
if ( !mNativeMesh )
{
// lazy loading of mesh data
fillNativeMesh();
}

// Triangular mesh
if ( !mTriangularMesh )
mTriangularMesh.reset( new QgsTriangularMesh() );

mTriangularMesh->update( mNativeMesh.get(), &rendererContext );

// Cache
if ( !mRendererCache )
mRendererCache.reset( new QgsMeshLayerRendererCache() );

return new QgsMeshLayerRenderer( this, rendererContext );
}

@@ -27,6 +27,7 @@
#include "qgsmeshrenderersettings.h"

class QgsMapLayerRenderer;
struct QgsMeshLayerRendererCache;
class QgsSymbol;
class QgsTriangularMesh;
struct QgsMesh;
@@ -143,6 +144,9 @@ class CORE_EXPORT QgsMeshLayer : public QgsMapLayer
//! Returns triangular mesh (nullptr before rendering)
QgsTriangularMesh *triangularMesh() SIP_SKIP;

//! Returns native mesh (nullptr before rendering)
QgsMeshLayerRendererCache *rendererCache() SIP_SKIP;

//! Returns renderer settings
QgsMeshRendererSettings rendererSettings() const;
//! Sets new renderer settings
@@ -219,6 +223,9 @@ class CORE_EXPORT QgsMeshLayer : public QgsMapLayer
//! Pointer to derived mesh structure
std::unique_ptr<QgsTriangularMesh> mTriangularMesh;

//! Pointer to the cache with data used for last rendering
std::unique_ptr<QgsMeshLayerRendererCache> mRendererCache;

//! Renderer configuration
QgsMeshRendererSettings mRendererSettings;
};
@@ -45,6 +45,9 @@ QgsMeshLayerRenderer::QgsMeshLayerRenderer( QgsMeshLayer *layer, QgsRenderContex
// make copies for mesh data
Q_ASSERT( layer->nativeMesh() );
Q_ASSERT( layer->triangularMesh() );
Q_ASSERT( layer->rendererCache() );
Q_ASSERT( layer->dataProvider() );

mNativeMesh = *( layer->nativeMesh() );
mTriangularMesh = *( layer->triangularMesh() );

@@ -93,6 +96,22 @@ void QgsMeshLayerRenderer::createMeshSymbol( std::unique_ptr<QgsSymbol> &symbol,
void QgsMeshLayerRenderer::copyScalarDatasetValues( QgsMeshLayer *layer )
{
const QgsMeshDatasetIndex datasetIndex = mRendererSettings.activeScalarDataset();

// Find out if we can use cache up to date. If yes, use it and return
const int datasetGroupCount = layer->dataProvider()->datasetGroupCount();
QgsMeshLayerRendererCache *cache = layer->rendererCache();
if ( ( cache->mDatasetGroupsCount == datasetGroupCount ) &&
( cache->mActiveScalarDatasetIndex == datasetIndex ) )
{
mScalarDatasetValues = cache->mScalarDatasetValues;
mScalarActiveFaceFlagValues = cache->mScalarActiveFaceFlagValues;
mScalarDataOnVertices = cache->mScalarDataOnVertices;
mScalarDatasetMinimum = cache->mScalarDatasetMinimum;
mScalarDatasetMaximum = cache->mScalarDatasetMaximum;
return;
}

// Cache is not up-to-date, gather data
if ( datasetIndex.isValid() )
{
const QgsMeshDatasetGroupMetadata metadata = layer->dataProvider()->datasetGroupMetadata( datasetIndex );
@@ -120,11 +139,38 @@ void QgsMeshLayerRenderer::copyScalarDatasetValues( QgsMeshLayer *layer )

QgsMeshLayerUtils::calculateMinimumMaximum( mScalarDatasetMinimum, mScalarDatasetMaximum, mScalarDatasetValues );
}

// update cache
cache->mDatasetGroupsCount = datasetGroupCount;
cache->mActiveScalarDatasetIndex = datasetIndex;
cache->mScalarDatasetValues = mScalarDatasetValues;
cache->mScalarActiveFaceFlagValues = mScalarActiveFaceFlagValues;
cache->mScalarDataOnVertices = mScalarDataOnVertices;
cache->mScalarDatasetMinimum = mScalarDatasetMinimum;
cache->mScalarDatasetMaximum = mScalarDatasetMaximum;
}

void QgsMeshLayerRenderer::copyVectorDatasetValues( QgsMeshLayer *layer )
{
const QgsMeshDatasetIndex datasetIndex = mRendererSettings.activeVectorDataset();

// Find out if we can use cache up to date. If yes, use it and return
const int datasetGroupCount = layer->dataProvider()->datasetGroupCount();
QgsMeshLayerRendererCache *cache = layer->rendererCache();
if ( ( cache->mDatasetGroupsCount == datasetGroupCount ) &&
( cache->mActiveVectorDatasetIndex == datasetIndex ) )
{
mVectorDatasetValuesX = cache->mVectorDatasetValuesX;
mVectorDatasetValuesY = cache->mVectorDatasetValuesY;
mVectorDatasetValuesMag = cache->mVectorDatasetValuesMag;
mVectorDatasetMagMinimum = cache->mVectorDatasetMagMinimum;
mVectorDatasetMagMaximum = cache->mVectorDatasetMagMaximum;
mVectorDataOnVertices = cache->mVectorDataOnVertices;
return;
}


// Cache is not up-to-date, gather data
if ( datasetIndex.isValid() )
{
const QgsMeshDatasetGroupMetadata metadata = layer->dataProvider()->datasetGroupMetadata( datasetIndex );
@@ -161,6 +207,16 @@ void QgsMeshLayerRenderer::copyVectorDatasetValues( QgsMeshLayer *layer )

QgsMeshLayerUtils::calculateMinimumMaximum( mVectorDatasetMagMinimum, mVectorDatasetMagMaximum, mVectorDatasetValuesMag );
}

// update cache
cache->mDatasetGroupsCount = datasetGroupCount;
cache->mActiveVectorDatasetIndex = datasetIndex;
cache->mVectorDatasetValuesX = mVectorDatasetValuesX;
cache->mVectorDatasetValuesY = mVectorDatasetValuesY;
cache->mVectorDatasetValuesMag = mVectorDatasetValuesMag;
cache->mVectorDatasetMagMinimum = mVectorDatasetMagMinimum;
cache->mVectorDatasetMagMaximum = mVectorDatasetMagMaximum;
cache->mVectorDataOnVertices = mVectorDataOnVertices;
}

bool QgsMeshLayerRenderer::render()
@@ -46,6 +46,32 @@ class QgsMeshLayerRendererFeedback : public QgsRasterBlockFeedback
{
};


/**
* Cache for data needed to render active datasets
*/
struct CORE_NO_EXPORT QgsMeshLayerRendererCache
{
int mDatasetGroupsCount = 0;

// scalar dataset
QgsMeshDatasetIndex mActiveScalarDatasetIndex;
QVector<double> mScalarDatasetValues;
QVector<bool> mScalarActiveFaceFlagValues;
bool mScalarDataOnVertices = true;
double mScalarDatasetMinimum = std::numeric_limits<double>::quiet_NaN();
double mScalarDatasetMaximum = std::numeric_limits<double>::quiet_NaN();

// vector dataset
QgsMeshDatasetIndex mActiveVectorDatasetIndex;
QVector<double> mVectorDatasetValuesX;
QVector<double> mVectorDatasetValuesY;
QVector<double> mVectorDatasetValuesMag;
double mVectorDatasetMagMinimum = std::numeric_limits<double>::quiet_NaN();
double mVectorDatasetMagMaximum = std::numeric_limits<double>::quiet_NaN();
bool mVectorDataOnVertices = true;
};

///@endcond

/**
@@ -44,11 +44,7 @@ struct CORE_EXPORT QgsMesh
///@cond PRIVATE

/**
* \ingroup core
*
* Delivers features from mesh
*
* \since QGIS 3.4
* Delivers mesh faces as features
*/
class CORE_NO_EXPORT QgsMeshFeatureIterator : public QgsAbstractFeatureIterator
{
@@ -60,7 +56,7 @@ class CORE_NO_EXPORT QgsMeshFeatureIterator : public QgsAbstractFeatureIterator
* \param mesh The mesh to use
*/
QgsMeshFeatureIterator( QgsMesh *mesh );
~QgsMeshFeatureIterator();
~QgsMeshFeatureIterator() override;

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

0 comments on commit 11645ac

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