QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
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 "qgssymbollayerutils.h"
22#include "qgs3d.h"
23#include "qgsmaterialregistry.h"
24#include "qgs3dsceneexporter.h"
26#include "qgsvectorlayer.h"
27
29 : mMaterialSettings( std::make_unique< QgsPhongMaterialSettings >() )
30{
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->mHeight = mHeight;
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( "height" ), mHeight );
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" ), QgsSymbolLayerUtils::encodeColor( 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 mHeight = elemDataProperties.attribute( QStringLiteral( "height" ) ).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 = QgsSymbolLayerUtils::decodeColor( 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 {
136 mDataDefinedProperties.setProperty( PropertyExtrusionHeight, props->dataDefinedProperties().property( QgsMapLayerElevationProperties::ExtrusionHeight ) );
137 }
138 else
139 {
140 mDataDefinedProperties.setProperty( PropertyExtrusionHeight, QgsProperty() );
141 }
143 {
144 mDataDefinedProperties.setProperty( PropertyHeight, props->dataDefinedProperties().property( QgsMapLayerElevationProperties::ZOffset ) );
145 }
146 else
147 {
148 mDataDefinedProperties.setProperty( PropertyHeight, QgsProperty() );
149 }
150 mHeight = static_cast< float >( props->zOffset() );
151}
152
154{
155 return new QgsPolygon3DSymbol();
156}
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 const QList<Qt3DCore::QEntity *> entities = entity->findChildren<Qt3DCore::QEntity *>( QString(), Qt::FindDirectChildrenOnly );
174 if ( entities.isEmpty() )
175 {
176 const QList<Qt3DRender::QGeometryRenderer *> renderers = entity->findChildren<Qt3DRender::QGeometryRenderer *>();
177 for ( Qt3DRender::QGeometryRenderer *renderer : renderers )
178 {
179 Qgs3DExportObject *object = exporter->processGeometryRenderer( renderer, objectNamePrefix );
180 if ( object == nullptr ) continue;
181 exporter->processEntityMaterial( entity, object );
182 exporter->mObjects.push_back( object );
183 }
184 return renderers.size() != 0;
185 }
186 else
187 {
188 bool out = false;
189 QString prefix;
190 for ( Qt3DCore::QEntity *e : entities )
191 {
192 if ( e->objectName().isEmpty() )
193 prefix = objectNamePrefix;
194 else
195 prefix = e->objectName() + "_";
196
197 out |= exportGeometries( exporter, e, prefix );
198 }
199 return out;
200 }
201}
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.
Definition: qgs3dutils.cpp:317
static Qgis::AltitudeClamping altClampingFromString(const QString &str)
Converts a string to a value from AltitudeClamping enum.
Definition: qgs3dutils.cpp:269
static Qgis::AltitudeBinding altBindingFromString(const QString &str)
Converts a string to a value from AltitudeBinding enum.
Definition: qgs3dutils.cpp:293
static QString cullingModeToString(Qgs3DTypes::CullingMode mode)
Converts a value from CullingMode enum to a string.
Definition: qgs3dutils.cpp:301
static QString altClampingToString(Qgis::AltitudeClamping altClamp)
Converts a value from AltitudeClamping enum to a string.
Definition: qgs3dutils.cpp:254
static QString altBindingToString(Qgis::AltitudeBinding altBind)
Converts a value from AltitudeBinding enum to a string.
Definition: qgs3dutils.cpp:280
static QgsMaterialRegistry * materialRegistry()
Returns the material registry, used for managing 3D materials.
Definition: qgs3d.cpp:90
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.
QgsPolygon3DSymbol()
Constructor for QgsPolygon3DSymbol.
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
QgsProperty property(int key) const override
Returns a matching property from the collection, if one exists.
bool isActive(int key) const override
Returns true if the collection contains an active property with the specified key.
A store for object properties.
Definition: qgsproperty.h:230
The class is used as a container of context for various read/write operations on other objects.
static QColor decodeColor(const QString &str)
static QString encodeColor(const QColor &color)
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.