QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgspolygon3dsymbol.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspolygon3dsymbol.cpp
3 --------------------------------------
4 Date : July 2017
5 Copyright : (C) 2017 by Martin Dobias
6 Email : wonder dot sk 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 "qgspolygon3dsymbol.h"
17
18#include <Qt3DCore/QEntity>
19
20#include "qgs3dutils.h"
21#include "qgs3d.h"
22#include "qgscolorutils.h"
23#include "qgsmaterialregistry.h"
24#include "qgs3dsceneexporter.h"
26#include "qgsvectorlayer.h"
28
30 : mMaterialSettings( std::make_unique<QgsPhongMaterialSettings>() )
31{
32}
33
35
37{
38 std::unique_ptr<QgsPolygon3DSymbol> result = std::make_unique<QgsPolygon3DSymbol>();
39 result->mAltClamping = mAltClamping;
40 result->mAltBinding = mAltBinding;
41 result->mOffset = mOffset;
42 result->mExtrusionHeight = mExtrusionHeight;
43 result->mMaterialSettings.reset( mMaterialSettings->clone() );
44 result->mCullingMode = mCullingMode;
45 result->mInvertNormals = mInvertNormals;
46 result->mAddBackFaces = mAddBackFaces;
47 result->mRenderedFacade = mRenderedFacade;
48 result->mEdgesEnabled = mEdgesEnabled;
49 result->mEdgeWidth = mEdgeWidth;
50 result->mEdgeColor = mEdgeColor;
51 copyBaseSettings( result.get() );
52 return result.release();
53}
54
55void QgsPolygon3DSymbol::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
56{
57 Q_UNUSED( context )
58
59 QDomDocument doc = elem.ownerDocument();
60
61 QDomElement elemDataProperties = doc.createElement( QStringLiteral( "data" ) );
62 elemDataProperties.setAttribute( QStringLiteral( "alt-clamping" ), Qgs3DUtils::altClampingToString( mAltClamping ) );
63 elemDataProperties.setAttribute( QStringLiteral( "alt-binding" ), Qgs3DUtils::altBindingToString( mAltBinding ) );
64 elemDataProperties.setAttribute( QStringLiteral( "offset" ), mOffset );
65 elemDataProperties.setAttribute( QStringLiteral( "extrusion-height" ), mExtrusionHeight );
66 elemDataProperties.setAttribute( QStringLiteral( "culling-mode" ), Qgs3DUtils::cullingModeToString( mCullingMode ) );
67 elemDataProperties.setAttribute( QStringLiteral( "invert-normals" ), mInvertNormals ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
68 elemDataProperties.setAttribute( QStringLiteral( "add-back-faces" ), mAddBackFaces ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
69 elemDataProperties.setAttribute( QStringLiteral( "rendered-facade" ), mRenderedFacade );
70 elem.appendChild( elemDataProperties );
71
72 elem.setAttribute( QStringLiteral( "material_type" ), mMaterialSettings->type() );
73 QDomElement elemMaterial = doc.createElement( QStringLiteral( "material" ) );
74 mMaterialSettings->writeXml( elemMaterial, context );
75 elem.appendChild( elemMaterial );
76
77 QDomElement elemDDP = doc.createElement( QStringLiteral( "data-defined-properties" ) );
78 mDataDefinedProperties.writeXml( elemDDP, propertyDefinitions() );
79 elem.appendChild( elemDDP );
80
81 QDomElement elemEdges = doc.createElement( QStringLiteral( "edges" ) );
82 elemEdges.setAttribute( QStringLiteral( "enabled" ), mEdgesEnabled ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
83 elemEdges.setAttribute( QStringLiteral( "width" ), mEdgeWidth );
84 elemEdges.setAttribute( QStringLiteral( "color" ), QgsColorUtils::colorToString( mEdgeColor ) );
85 elem.appendChild( elemEdges );
86}
87
88void QgsPolygon3DSymbol::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
89{
90 Q_UNUSED( context )
91
92 const QDomElement elemDataProperties = elem.firstChildElement( QStringLiteral( "data" ) );
93 mAltClamping = Qgs3DUtils::altClampingFromString( elemDataProperties.attribute( QStringLiteral( "alt-clamping" ) ) );
94 mAltBinding = Qgs3DUtils::altBindingFromString( elemDataProperties.attribute( QStringLiteral( "alt-binding" ) ) );
95 mOffset = elemDataProperties.attribute( QStringLiteral( "offset" ) ).toFloat();
96 mExtrusionHeight = elemDataProperties.attribute( QStringLiteral( "extrusion-height" ) ).toFloat();
97 mCullingMode = Qgs3DUtils::cullingModeFromString( elemDataProperties.attribute( QStringLiteral( "culling-mode" ) ) );
98 mInvertNormals = elemDataProperties.attribute( QStringLiteral( "invert-normals" ) ).toInt();
99 mAddBackFaces = elemDataProperties.attribute( QStringLiteral( "add-back-faces" ) ).toInt();
100 mRenderedFacade = elemDataProperties.attribute( QStringLiteral( "rendered-facade" ), "3" ).toInt();
101
102 const QDomElement elemMaterial = elem.firstChildElement( QStringLiteral( "material" ) );
103 const QString materialType = elem.attribute( QStringLiteral( "material_type" ), QStringLiteral( "phong" ) );
104 mMaterialSettings.reset( Qgs3D::materialRegistry()->createMaterialSettings( materialType ) );
105 if ( !mMaterialSettings )
106 mMaterialSettings.reset( Qgs3D::materialRegistry()->createMaterialSettings( QStringLiteral( "phong" ) ) );
107 mMaterialSettings->readXml( elemMaterial, context );
108
109 const QDomElement elemDDP = elem.firstChildElement( QStringLiteral( "data-defined-properties" ) );
110 if ( !elemDDP.isNull() )
111 mDataDefinedProperties.readXml( elemDDP, propertyDefinitions() );
112
113 const QDomElement elemEdges = elem.firstChildElement( QStringLiteral( "edges" ) );
114 if ( !elemEdges.isNull() )
115 {
116 mEdgesEnabled = elemEdges.attribute( QStringLiteral( "enabled" ) ).toInt();
117 mEdgeWidth = elemEdges.attribute( QStringLiteral( "width" ) ).toFloat();
118 mEdgeColor = QgsColorUtils::colorFromString( elemEdges.attribute( QStringLiteral( "color" ) ) );
119 }
120}
121
122QList<Qgis::GeometryType> QgsPolygon3DSymbol::compatibleGeometryTypes() const
123{
124 return QList<Qgis::GeometryType>() << Qgis::GeometryType::Polygon;
125}
126
128{
129 const QgsVectorLayerElevationProperties *props = qgis::down_cast<const QgsVectorLayerElevationProperties *>( const_cast<QgsVectorLayer *>( layer )->elevationProperties() );
130
131 mAltClamping = props->clamping();
132 mAltBinding = props->binding();
133 mExtrusionHeight = props->extrusionEnabled() ? static_cast<float>( props->extrusionHeight() ) : 0.0f;
135 {
137 }
138 else
139 {
140 mDataDefinedProperties.setProperty( QgsAbstract3DSymbol::Property::ExtrusionHeight, QgsProperty() );
141 }
143 {
145 }
146 else
147 {
148 mDataDefinedProperties.setProperty( QgsAbstract3DSymbol::Property::Height, QgsProperty() );
149 }
150 mOffset = static_cast<float>( props->zOffset() );
151}
152
157
159{
160 return mMaterialSettings.get();
161}
162
164{
165 if ( materialSettings == mMaterialSettings.get() )
166 return;
167
168 mMaterialSettings.reset( materialSettings );
169}
170
171bool QgsPolygon3DSymbol::exportGeometries( Qgs3DSceneExporter *exporter, Qt3DCore::QEntity *entity, const QString &objectNamePrefix ) const
172{
173 QList<Qt3DCore::QEntity *> subEntities = entity->findChildren<Qt3DCore::QEntity *>( QString(), Qt::FindDirectChildrenOnly );
174 // sort geometries by their name in order to always export them in the same way:
175 std::sort( subEntities.begin(), subEntities.end(), []( const Qt3DCore::QEntity *a, const Qt3DCore::QEntity *b ) {
176 return a->objectName() < b->objectName();
177 } );
178
179 if ( subEntities.isEmpty() )
180 {
181 const QList<Qt3DRender::QGeometryRenderer *> renderers = entity->findChildren<Qt3DRender::QGeometryRenderer *>();
182 const int startSize = exporter->mObjects.size();
183 for ( Qt3DRender::QGeometryRenderer *renderer : renderers )
184 {
185 Qgs3DExportObject *object = exporter->processGeometryRenderer( renderer, objectNamePrefix );
186 if ( object )
187 {
188 exporter->processEntityMaterial( entity, object );
189 exporter->mObjects.push_back( object );
190 }
191 }
192 return exporter->mObjects.size() > startSize;
193 }
194 else
195 {
196 bool out = false;
197 QString prefix;
198 for ( Qt3DCore::QEntity *e : subEntities )
199 {
200 if ( e->objectName().isEmpty() )
201 prefix = objectNamePrefix;
202 else
203 prefix = e->objectName() + "_";
204
205 out |= exportGeometries( exporter, e, prefix );
206 }
207 return out;
208 }
209}
@ Polygon
Polygons.
Manages the data of each object of the scene (positions, normals, texture coordinates ....
Entity that handles the exporting of 3D scene.
static Qgs3DTypes::CullingMode cullingModeFromString(const QString &str)
Converts a string to a value from CullingMode enum.
static Qgis::AltitudeClamping altClampingFromString(const QString &str)
Converts a string to a value from AltitudeClamping enum.
static Qgis::AltitudeBinding altBindingFromString(const QString &str)
Converts a string to a value from AltitudeBinding enum.
static QString cullingModeToString(Qgs3DTypes::CullingMode mode)
Converts a value from CullingMode enum to a string.
static QString altClampingToString(Qgis::AltitudeClamping altClamp)
Converts a value from AltitudeClamping enum to a string.
static QString altBindingToString(Qgis::AltitudeBinding altBind)
Converts a value from AltitudeBinding enum to a string.
static QgsMaterialRegistry * materialRegistry()
Returns the material registry, used for managing 3D materials.
Definition qgs3d.cpp:88
@ ExtrusionHeight
Extrusion height (zero means no extrusion)
@ Height
Height (altitude)
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the object's property collection, used for data defined overrides.
double zOffset() const
Returns the z offset, which is a fixed offset amount which should be added to z values from the layer...
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
static QgsAbstract3DSymbol * create() SIP_FACTORY
Creates a new QgsPolygon3DSymbol.
QgsAbstractMaterialSettings * materialSettings() const
Returns material settings used for shading of the symbol.
QgsAbstract3DSymbol * clone() const override SIP_FACTORY
void setMaterialSettings(QgsAbstractMaterialSettings *materialSettings SIP_TRANSFER)
Sets the material settings used for shading of the symbol.
~QgsPolygon3DSymbol() override
bool exportGeometries(Qgs3DSceneExporter *exporter, Qt3DCore::QEntity *entity, const QString &objectNamePrefix) const override SIP_SKIP
Exports the geometries contained within the hierarchy of entity.
void setDefaultPropertiesFromLayer(const QgsVectorLayer *layer) override
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
QList< Qgis::GeometryType > compatibleGeometryTypes() const override
bool isActive(int key) const final
Returns true if the collection contains an active property with the specified key.
QgsProperty property(int key) const final
Returns a matching property from the collection, if one exists.
A store for object properties.
The class is used as a container of context for various read/write operations on other objects.
Vector layer specific subclass of QgsMapLayerElevationProperties.
double extrusionHeight() const
Returns the feature extrusion height.
Qgis::AltitudeClamping clamping() const
Returns the altitude clamping method, which dictates how feature heights are interpreted with respect...
Qgis::AltitudeBinding binding() const
Returns the altitude binding method, which determines how altitude is bound to individual vertices in...
bool extrusionEnabled() const
Returns true if extrusion is enabled.
Represents a vector layer which manages a vector based data sets.