21 #include <QLinearGradient>
47 , mFeedback( new QgsMeshLayerRendererFeedback )
48 , mRendererSettings( layer->rendererSettings() )
65 copyTriangularMeshes( layer, context );
68 copyScalarDatasetValues( layer );
69 copyVectorDatasetValues( layer );
71 calculateOutputSize();
82 mIsMeshSimplificationActive =
true;
95 void QgsMeshLayerRenderer::calculateOutputSize()
103 int width = int( bottomright.
x() - topleft.
x() );
104 int height = int( bottomright.
y() - topleft.
y() );
108 void QgsMeshLayerRenderer::copyScalarDatasetValues(
QgsMeshLayer *layer )
120 if ( ( cache->mDatasetGroupsCount == datasetGroupCount ) &&
121 ( cache->mActiveScalarDatasetIndex == datasetIndex ) &&
122 ( cache->mDataInterpolationMethod == method ) &&
167 if (
mScalarDataType == QgsMeshDatasetGroupMetadata::DataType::DataOnFaces )
169 mScalarDataType = QgsMeshDatasetGroupMetadata::DataType::DataOnVertices;
178 else if (
mScalarDataType == QgsMeshDatasetGroupMetadata::DataType::DataOnVertices )
198 cache->mDatasetGroupsCount = datasetGroupCount;
199 cache->mActiveScalarDatasetIndex = datasetIndex;
200 cache->mDataInterpolationMethod = method;
210 void QgsMeshLayerRenderer::copyVectorDatasetValues(
QgsMeshLayer *layer )
221 if ( ( cache->mDatasetGroupsCount == datasetGroupCount ) &&
222 ( cache->mActiveVectorDatasetIndex == datasetIndex ) &&
241 bool isScalar = metadata.
isScalar();
244 QgsDebugMsg( QStringLiteral(
"Dataset has no vector values" ) );
272 cache->mDatasetGroupsCount = datasetGroupCount;
273 cache->mActiveVectorDatasetIndex = datasetIndex;
286 renderScalarDataset();
288 renderVectorDataset();
292 void QgsMeshLayerRenderer::renderMesh()
318 nativeFacesInExtent.values() );
332 QPainter *painter = context.
painter();
335 painter->setRenderHint( QPainter::Antialiasing,
true );
337 QPen pen = painter->pen();
338 pen.setCapStyle( Qt::FlatCap );
339 pen.setJoinStyle( Qt::MiterJoin );
342 QgsUnitTypes::RenderUnit::RenderMillimeters );
343 pen.setWidthF( penWidth );
344 pen.setColor( settings.
color() );
345 painter->setPen( pen );
357 QPainter *painter = _painterForMeshFrame( context, settings );
362 for (
const int i : edgesInExtent )
367 if ( i >= edges.size() )
371 const int startVertexIndex = edge.first;
372 const int endVertexIndex = edge.second;
374 if ( ( startVertexIndex >= vertices.size() ) || endVertexIndex >= vertices.size() )
377 const QgsMeshVertex &startVertex = vertices[startVertexIndex];
386 void QgsMeshLayerRenderer::renderFaceMesh(
388 const QVector<QgsMeshFace> &faces,
389 const QList<int> &facesInExtent )
397 QPainter *painter = _painterForMeshFrame( context, settings );
400 QSet<QPair<int, int>> drawnEdges;
402 for (
const int i : facesInExtent )
408 if ( face.size() < 2 )
411 for (
int j = 0; j < face.size(); ++j )
413 const int startVertexId = face[j];
414 const int endVertexId = face[( j + 1 ) % face.size()];
415 const QPair<int, int> thisEdge( startVertexId, endVertexId );
416 const QPair<int, int> thisEdgeReversed( endVertexId, startVertexId );
417 if ( drawnEdges.contains( thisEdge ) || drawnEdges.contains( thisEdgeReversed ) )
419 drawnEdges.insert( thisEdge );
420 drawnEdges.insert( thisEdgeReversed );
433 void QgsMeshLayerRenderer::renderScalarDataset()
442 if ( groupIndex < 0 )
448 (
mScalarDataType != QgsMeshDatasetGroupMetadata::DataType::DataOnEdges ) )
450 renderScalarDatasetOnFaces( scalarSettings );
454 (
mScalarDataType != QgsMeshDatasetGroupMetadata::DataType::DataOnFaces ) )
456 renderScalarDatasetOnEdges( scalarSettings );
474 for (
const int i : egdesInExtent )
479 if ( i >= edges.size() )
483 const int startVertexIndex = edge.first;
484 const int endVertexIndex = edge.second;
486 if ( ( startVertexIndex >= vertices.size() ) || endVertexIndex >= vertices.size() )
489 const QgsMeshVertex &startVertex = vertices[startVertexIndex];
492 if (
mScalarDataType == QgsMeshDatasetGroupMetadata::DataType::DataOnEdges )
506 if ( shader->
shade( val, &r, &g, &b, &a ) )
508 return QColor( r, g, b, a );
515 const QgsPointXY pt( p1.
x() + fraction * ( p2.
x() - p1.
x() ),
516 p1.
y() + fraction * ( p2.
y() - p1.
y() ) );
533 interpolator.setSpatialIndexActive( mIsMeshSimplificationActive );
537 renderer.setOpacity( scalarSettings.
opacity() );
540 QImage img = bl->image();
542 context.
painter()->drawImage( 0, 0, img );
545 void QgsMeshLayerRenderer::renderVectorDataset()
548 if ( groupIndex < 0 )
560 std::unique_ptr<QgsMeshVectorRenderer> renderer( QgsMeshVectorRenderer::makeVectorRenderer(