QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsmeshlayer3drenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmeshlayer3drenderer.cpp
3  --------------------------
4  Date : January 2019
5  Copyright : (C) 2019 by Peter Petrik
6  Email : zilolv at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsmeshlayer3drenderer.h"
17 
18 #include "qgsmeshlayer3drenderer.h"
19 #include "qgsmesh3dsymbol.h"
20 #include "qgsmesh3dsymbol_p.h"
21 
22 #include "qgsmeshlayer.h"
23 #include "qgsxmlutils.h"
24 #include "qgsmesh3dentity_p.h"
25 
27  : Qgs3DRendererAbstractMetadata( QStringLiteral( "mesh" ) )
28 {
29 }
30 
32 {
34  r->readXml( elem, context );
35  return r;
36 }
37 
38 // ---------
39 
40 
42  : mSymbol( s )
43 {
44 }
45 
47 {
48  QgsMeshLayer3DRenderer *r = new QgsMeshLayer3DRenderer( mSymbol ? ( QgsMesh3DSymbol * )mSymbol->clone() : nullptr );
49  r->mLayerRef = mLayerRef;
50  return r;
51 }
52 
54 {
55  mLayerRef = QgsMapLayerRef( layer );
56 }
57 
59 {
60  return qobject_cast<QgsMeshLayer *>( mLayerRef.layer );
61 }
62 
64 {
65  mSymbol.reset( symbol );
66 }
67 
69 {
70  return mSymbol.get();
71 }
72 
73 Qt3DCore::QEntity *QgsMeshLayer3DRenderer::createEntity( const Qgs3DMapSettings &map ) const
74 {
75  QgsMeshLayer *meshLayer = layer();
76 
77  if ( !meshLayer || !meshLayer->dataProvider() )
78  return nullptr;
79 
80  if ( meshLayer->dataProvider()->contains( QgsMesh::ElementType::Edge ) ||
81  !mSymbol->isEnabled() )
82  {
83  // 3D not implemented for 1D meshes
84  return nullptr;
85  }
86 
87  Qt3DCore::QEntity *entity = nullptr;
88 
89 
90  QgsCoordinateTransform coordTrans( meshLayer->crs(), map.crs(), map.transformContext() );
91  meshLayer->updateTriangularMesh( coordTrans );
92  const QgsTriangularMesh triangularMesh = *meshLayer->triangularMesh();
93  QgsMeshDataset3dEntity *meshEntity = new QgsMeshDataset3dEntity( map, triangularMesh, meshLayer, mSymbol.get() );
94  meshEntity->build();
95  entity = meshEntity;
96 
97  return entity;
98 }
99 
100 void QgsMeshLayer3DRenderer::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
101 {
102  QDomDocument doc = elem.ownerDocument();
103 
104  elem.setAttribute( QStringLiteral( "layer" ), mLayerRef.layerId );
105 
106  QDomElement elemSymbol = doc.createElement( QStringLiteral( "symbol" ) );
107  if ( mSymbol )
108  {
109  elemSymbol.setAttribute( QStringLiteral( "type" ), mSymbol->type() );
110  mSymbol->writeXml( elemSymbol, context );
111  }
112  elem.appendChild( elemSymbol );
113 }
114 
115 void QgsMeshLayer3DRenderer::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
116 {
117  mLayerRef = QgsMapLayerRef( elem.attribute( QStringLiteral( "layer" ) ) );
118 
119  QDomElement elemSymbol = elem.firstChildElement( QStringLiteral( "symbol" ) );
121  symbol->readXml( elemSymbol, context );
122  mSymbol.reset( symbol );
123 }
124 
126 {
127  mLayerRef.setLayer( project.mapLayer( mLayerRef.layerId ) );
128 }
QgsMapLayer::crs
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:89
Qgs3DMapSettings::crs
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used in the 3D scene.
Definition: qgs3dmapsettings.h:96
QgsMesh3DSymbol::readXml
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads symbol configuration from the given DOM element.
Definition: qgsmesh3dsymbol.cpp:99
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
QgsAbstract3DRenderer
Base class for all renderers that may to participate in 3D view.
Definition: qgsabstract3drenderer.h:49
qgsmesh3dsymbol.h
QgsMeshLayer3DRenderer::symbol
const QgsMesh3DSymbol * symbol() const
Returns 3D symbol associated with the renderer.
Definition: qgsmeshlayer3drenderer.cpp:68
Qgs3DRendererAbstractMetadata
Base metadata class for 3D renderers.
Definition: qgs3drendererregistry.h:35
QgsMesh3DSymbol
3 3D symbol that draws mesh geometry as planar triangles.
Definition: qgsmesh3dsymbol.h:44
QgsMapLayerRef
_LayerRef< QgsMapLayer > QgsMapLayerRef
Definition: qgsmaplayerref.h:294
QgsMeshDataSourceInterface::contains
bool contains(const QgsMesh::ElementType &type) const
Returns whether the mesh contains at mesh elements of given type.
Definition: qgsmeshdataprovider.cpp:184
QgsProject::mapLayer
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
Definition: qgsproject.cpp:3208
_LayerRef::layer
QPointer< TYPE > layer
Weak pointer to map layer.
Definition: qgsmaplayerref.h:113
Qgs3DMapSettings::transformContext
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
Definition: qgs3dmapsettings.cpp:392
QgsMeshLayer::triangularMesh
QgsTriangularMesh * triangularMesh(double minimumTriangleSize=0) const
Returns triangular mesh (nullptr before rendering or calling to updateMesh).
Definition: qgsmeshlayer.cpp:248
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:95
QgsMeshLayer3DRenderer::writeXml
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Writes renderer's properties to given XML element.
Definition: qgsmeshlayer3drenderer.cpp:100
QgsMeshLayer::updateTriangularMesh
void updateTriangularMesh(const QgsCoordinateTransform &transform=QgsCoordinateTransform())
Gets native mesh and updates (creates if it doesn't exist) the base triangular mesh.
Definition: qgsmeshlayer.cpp:262
_LayerRef::layerId
QString layerId
Original layer ID.
Definition: qgsmaplayerref.h:116
QgsMeshLayer::dataProvider
QgsMeshDataProvider * dataProvider() override
Returns the layer's data provider, it may be nullptr.
Definition: qgsmeshlayer.cpp:156
qgsmeshlayer3drenderer.h
_LayerRef::setLayer
void setLayer(TYPE *l)
Sets the reference to point to a specified layer.
Definition: qgsmaplayerref.h:77
QgsMeshLayer
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:95
QgsMeshLayer3DRendererMetadata::createRenderer
QgsAbstract3DRenderer * createRenderer(QDomElement &elem, const QgsReadWriteContext &context) override
Creates an instance of a 3D renderer based on a DOM element with renderer configuration.
Definition: qgsmeshlayer3drenderer.cpp:31
QgsMeshLayer3DRenderer
3D renderer that renders all mesh triangles of a mesh layer.
Definition: qgsmeshlayer3drenderer.h:60
Qgs3DMapSettings
3 Definition of the world
Definition: qgs3dmapsettings.h:54
qgsmesh3dsymbol_p.h
qgsxmlutils.h
qgsmeshlayer.h
QgsMeshLayer3DRenderer::layer
QgsMeshLayer * layer() const
Returns mesh layer associated with the renderer.
Definition: qgsmeshlayer3drenderer.cpp:58
QgsMeshLayer3DRenderer::setLayer
void setLayer(QgsMeshLayer *layer)
Sets vector layer associated with the renderer.
Definition: qgsmeshlayer3drenderer.cpp:53
QgsMeshLayer3DRendererMetadata::QgsMeshLayer3DRendererMetadata
QgsMeshLayer3DRendererMetadata()
Definition: qgsmeshlayer3drenderer.cpp:26
QgsMeshLayer3DRenderer::readXml
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads renderer's properties from given XML element.
Definition: qgsmeshlayer3drenderer.cpp:115
QgsMeshLayer3DRenderer::setSymbol
void setSymbol(QgsMesh3DSymbol *symbol)
Sets 3D symbol associated with the renderer.
Definition: qgsmeshlayer3drenderer.cpp:63
QgsTriangularMesh
Triangular/Derived Mesh is mesh with vertices in map coordinates.
Definition: qgstriangularmesh.h:50
QgsCoordinateTransform
Class for doing transforms between two map coordinate systems.
Definition: qgscoordinatetransform.h:53
QgsMeshLayer3DRenderer::clone
QgsMeshLayer3DRenderer * clone() const override
Returns a cloned instance.
Definition: qgsmeshlayer3drenderer.cpp:46
QgsMeshLayer3DRenderer::QgsMeshLayer3DRenderer
QgsMeshLayer3DRenderer(QgsMesh3DSymbol *s=nullptr)
Takes ownership of the symbol object.
Definition: qgsmeshlayer3drenderer.cpp:41
QgsMeshLayer3DRenderer::resolveReferences
void resolveReferences(const QgsProject &project) override
Resolves references to other objects - second phase of loading - after readXml()
Definition: qgsmeshlayer3drenderer.cpp:125
QgsMeshLayer3DRenderer::createEntity
Qt3DCore::QEntity * createEntity(const Qgs3DMapSettings &map) const override
Returns a 3D entity that will be used to show renderer's data in 3D scene.
Definition: qgsmeshlayer3drenderer.cpp:73