QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
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
17
19#include "qgsmesh3dsymbol.h"
20
21#include "qgsmeshlayer.h"
22#include "qgsxmlutils.h"
23#include "qgsmesh3dentity_p.h"
24#include "qgs3drendercontext.h"
25#include "qgs3dmapsettings.h"
26
31
33{
35 r->readXml( elem, context );
36 return r;
37}
38
39// ---------
40
41
46
48{
49 QgsMeshLayer3DRenderer *r = new QgsMeshLayer3DRenderer( mSymbol ? ( QgsMesh3DSymbol * ) mSymbol->clone() : nullptr );
50 r->mLayerRef = mLayerRef;
51 return r;
52}
53
55{
56 mLayerRef = QgsMapLayerRef( layer );
57}
58
60{
61 return qobject_cast<QgsMeshLayer *>( mLayerRef.layer );
62}
63
65{
66 mSymbol.reset( symbol );
67}
68
70{
71 return mSymbol.get();
72}
73
75{
76 QgsMeshLayer *meshLayer = layer();
77
78 if ( !meshLayer || !meshLayer->dataProvider() )
79 return nullptr;
80
81 if ( meshLayer->dataProvider()->contains( QgsMesh::ElementType::Edge ) || !mSymbol->isEnabled() )
82 {
83 // 3D not implemented for 1D meshes
84 return nullptr;
85 }
86
87 if ( mSymbol->verticalDatasetGroupIndex() < 0 )
88 return nullptr;
89
90 Qt3DCore::QEntity *entity = nullptr;
91
92 const QgsCoordinateTransform coordTrans( meshLayer->crs(), map->crs(), map->transformContext() );
93 meshLayer->updateTriangularMesh( coordTrans );
94 const QgsTriangularMesh triangularMesh = *meshLayer->triangularMeshByLodIndex( mSymbol->levelOfDetailIndex() );
95 QgsMeshDataset3DEntity *meshEntity = new QgsMeshDataset3DEntity( Qgs3DRenderContext::fromMapSettings( map ), triangularMesh, meshLayer, mSymbol.get() );
96 meshEntity->build();
97 entity = meshEntity;
98
99 return entity;
100}
101
102void QgsMeshLayer3DRenderer::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
103{
104 QDomDocument doc = elem.ownerDocument();
105
106 elem.setAttribute( QStringLiteral( "layer" ), mLayerRef.layerId );
107
108 QDomElement elemSymbol = doc.createElement( QStringLiteral( "symbol" ) );
109 if ( mSymbol )
110 {
111 elemSymbol.setAttribute( QStringLiteral( "type" ), mSymbol->type() );
112 mSymbol->writeXml( elemSymbol, context );
113 }
114 elem.appendChild( elemSymbol );
115}
116
117void QgsMeshLayer3DRenderer::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
118{
119 mLayerRef = QgsMapLayerRef( elem.attribute( QStringLiteral( "layer" ) ) );
120
121 const QDomElement elemSymbol = elem.firstChildElement( QStringLiteral( "symbol" ) );
123 symbol->readXml( elemSymbol, context );
124 mSymbol.reset( symbol );
125}
126
128{
129 mLayerRef.setLayer( project.mapLayer( mLayerRef.layerId ) );
130}
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used in the 3D scene.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
static Qgs3DRenderContext fromMapSettings(const Qgs3DMapSettings *mapSettings)
Creates an initialized Qgs3DRenderContext instance from given Qgs3DMapSettings.
Base metadata class for 3D renderers.
Base class for all renderers that may to participate in 3D view.
Class for doing transforms between two map coordinate systems.
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:83
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads symbol configuration from the given DOM element.
bool contains(const QgsMesh::ElementType &type) const
Returns whether the mesh contains at mesh elements of given type.
QgsAbstract3DRenderer * createRenderer(QDomElement &elem, const QgsReadWriteContext &context) override
Creates an instance of a 3D renderer based on a DOM element with renderer configuration.
3D renderer that renders all mesh triangles of a mesh layer.
Qt3DCore::QEntity * createEntity(Qgs3DMapSettings *map) const override
Returns a 3D entity that will be used to show renderer's data in 3D scene.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads renderer's properties from given XML element.
void setSymbol(QgsMesh3DSymbol *symbol)
Sets 3D symbol associated with the renderer.
const QgsMesh3DSymbol * symbol() const
Returns 3D symbol associated with the renderer.
QgsMeshLayer3DRenderer(QgsMesh3DSymbol *s=nullptr)
Takes ownership of the symbol object.
void setLayer(QgsMeshLayer *layer)
Sets vector layer associated with the renderer.
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Writes renderer's properties to given XML element.
void resolveReferences(const QgsProject &project) override
Resolves references to other objects - second phase of loading - after readXml()
QgsMeshLayer * layer() const
Returns mesh layer associated with the renderer.
QgsMeshLayer3DRenderer * clone() const override
Returns a cloned instance.
Represents a mesh layer supporting display of data on structured or unstructured meshes.
void updateTriangularMesh(const QgsCoordinateTransform &transform=QgsCoordinateTransform())
Gets native mesh and updates (creates if it doesn't exist) the base triangular mesh.
QgsTriangularMesh * triangularMeshByLodIndex(int lodIndex) const
Returns triangular corresponding to the index of level of details.
QgsMeshDataProvider * dataProvider() override
Returns the layer's data provider, it may be nullptr.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
The class is used as a container of context for various read/write operations on other objects.
Triangular/Derived Mesh is mesh with vertices in map coordinates.
_LayerRef< QgsMapLayer > QgsMapLayerRef
QPointer< TYPE > layer
Weak pointer to map layer.
void setLayer(TYPE *l)
Sets the reference to point to a specified layer.
QString layerId
Original layer ID.