QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  : mMaterial( 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->mMaterial.reset( mMaterial->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 
55 void 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" ), mMaterial->type() );
73  QDomElement elemMaterial = doc.createElement( QStringLiteral( "material" ) );
74  mMaterial->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 
88 void 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  mMaterial.reset( Qgs3D::materialRegistry()->createMaterialSettings( materialType ) );
105  if ( !mMaterial )
106  mMaterial.reset( Qgs3D::materialRegistry()->createMaterialSettings( QStringLiteral( "phong" ) ) );
107  mMaterial->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 
122 QList<QgsWkbTypes::GeometryType> QgsPolygon3DSymbol::compatibleGeometryTypes() const
123 {
124  return QList< QgsWkbTypes::GeometryType >() << QgsWkbTypes::PolygonGeometry;
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 mMaterial.get();
161 }
162 
164 {
165  if ( material == mMaterial.get() )
166  return;
167 
168  mMaterial.reset( material );
169 }
170 
171 bool QgsPolygon3DSymbol::exportGeometries( Qgs3DSceneExporter *exporter, Qt3DCore::QEntity *entity, const QString &objectNamePrefix ) const
172 {
173  const QList<Qt3DRender::QGeometryRenderer *> renderers = entity->findChildren<Qt3DRender::QGeometryRenderer *>();
174  for ( Qt3DRender::QGeometryRenderer *r : renderers )
175  {
176  Qgs3DExportObject *object = exporter->processGeometryRenderer( r, objectNamePrefix );
177  if ( object == nullptr ) continue;
178  exporter->processEntityMaterial( entity, object );
179  exporter->mObjects.push_back( object );
180  }
181  return renderers.size() != 0;
182 }
QgsMapLayerElevationProperties::ZOffset
@ ZOffset
Definition: qgsmaplayerelevationproperties.h:85
QgsSymbolLayerUtils::encodeColor
static QString encodeColor(const QColor &color)
Definition: qgssymbollayerutils.cpp:64
qgs3d.h
QgsProperty
A store for object properties.
Definition: qgsproperty.h:230
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
Qgs3D::materialRegistry
static QgsMaterialRegistry * materialRegistry()
Returns the material registry, used for managing 3D materials.
Definition: qgs3d.cpp:90
qgsmaterialregistry.h
QgsPolygon3DSymbol::material
QgsAbstractMaterialSettings * material() const
Returns material used for shading of the symbol.
Definition: qgspolygon3dsymbol.cpp:158
qgs3dsceneexporter.h
Qgs3DExportObject
Manages the data of each object of the scene (positions, normals, texture coordinates ....
Definition: qgs3dexportobject.h:40
qgssymbollayerutils.h
QgsMapLayerElevationProperties::ExtrusionHeight
@ ExtrusionHeight
Z offset.
Definition: qgsmaplayerelevationproperties.h:86
QgsVectorLayerElevationProperties::extrusionHeight
double extrusionHeight() const
Returns the feature extrusion height.
Definition: qgsvectorlayerelevationproperties.h:133
QgsSymbolLayerUtils::decodeColor
static QColor decodeColor(const QString &str)
Definition: qgssymbollayerutils.cpp:69
QgsVectorLayerElevationProperties::clamping
Qgis::AltitudeClamping clamping() const
Returns the altitude clamping method, which dictates how feature heights are interpreted with respect...
Definition: qgsvectorlayerelevationproperties.h:67
QgsWkbTypes::PolygonGeometry
@ PolygonGeometry
Definition: qgswkbtypes.h:144
Qgs3DUtils::altBindingFromString
static Qgis::AltitudeBinding altBindingFromString(const QString &str)
Converts a string to a value from AltitudeBinding enum.
Definition: qgs3dutils.cpp:279
QgsPropertyCollection::property
QgsProperty property(int key) const override
Returns a matching property from the collection, if one exists.
Definition: qgspropertycollection.cpp:214
QgsPolygon3DSymbol::exportGeometries
bool exportGeometries(Qgs3DSceneExporter *exporter, Qt3DCore::QEntity *entity, const QString &objectNamePrefix) const override SIP_SKIP
Exports the geometries contained within the hierarchy of entity.
Definition: qgspolygon3dsymbol.cpp:171
QgsPhongMaterialSettings
Basic shading material used for rendering based on the Phong shading model with three color component...
Definition: qgsphongmaterialsettings.h:44
QgsAbstract3DSymbol
Abstract base class for 3D symbols that are used by VectorLayer3DRenderer objects.
Definition: qgsabstract3dsymbol.h:46
Qgs3DSceneExporter
Entity that handles the exporting of 3D scene.
Definition: qgs3dsceneexporter.h:55
QgsAbstractMaterialSettings
Abstract base class for material settings.
Definition: qgsabstractmaterialsettings.h:114
QgsVectorLayerElevationProperties::extrusionEnabled
bool extrusionEnabled() const
Returns true if extrusion is enabled.
Definition: qgsvectorlayerelevationproperties.h:115
qgs3dutils.h
QgsPolygon3DSymbol::clone
QgsAbstract3DSymbol * clone() const override SIP_FACTORY
Definition: qgspolygon3dsymbol.cpp:36
Qgs3DUtils::altClampingFromString
static Qgis::AltitudeClamping altClampingFromString(const QString &str)
Converts a string to a value from AltitudeClamping enum.
Definition: qgs3dutils.cpp:255
QgsVectorLayerElevationProperties
Vector layer specific subclass of QgsMapLayerElevationProperties.
Definition: qgsvectorlayerelevationproperties.h:38
QgsPolygon3DSymbol::~QgsPolygon3DSymbol
~QgsPolygon3DSymbol() override
Qgs3DUtils::altBindingToString
static QString altBindingToString(Qgis::AltitudeBinding altBind)
Converts a value from AltitudeBinding enum to a string.
Definition: qgs3dutils.cpp:266
Qgs3DUtils::altClampingToString
static QString altClampingToString(Qgis::AltitudeClamping altClamp)
Converts a value from AltitudeClamping enum to a string.
Definition: qgs3dutils.cpp:240
QgsPolygon3DSymbol::setMaterial
void setMaterial(QgsAbstractMaterialSettings *material SIP_TRANSFER)
Sets the material settings used for shading of the symbol.
Definition: qgspolygon3dsymbol.cpp:163
Qgs3DUtils::cullingModeFromString
static Qgs3DTypes::CullingMode cullingModeFromString(const QString &str)
Converts a string to a value from CullingMode enum.
Definition: qgs3dutils.cpp:303
qgsvectorlayer.h
QgsPolygon3DSymbol::readXml
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Definition: qgspolygon3dsymbol.cpp:88
qgspolygon3dsymbol.h
qgsvectorlayerelevationproperties.h
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsPolygon3DSymbol::setDefaultPropertiesFromLayer
void setDefaultPropertiesFromLayer(const QgsVectorLayer *layer) override
Definition: qgspolygon3dsymbol.cpp:127
QgsPolygon3DSymbol::compatibleGeometryTypes
QList< QgsWkbTypes::GeometryType > compatibleGeometryTypes() const override
Definition: qgspolygon3dsymbol.cpp:122
QgsPolygon3DSymbol::create
static QgsAbstract3DSymbol * create() SIP_FACTORY
Creates a new QgsPolygon3DSymbol.
Definition: qgspolygon3dsymbol.cpp:153
QgsPolygon3DSymbol::QgsPolygon3DSymbol
QgsPolygon3DSymbol()
Constructor for QgsPolygon3DSymbol.
Definition: qgspolygon3dsymbol.cpp:28
QgsMapLayerElevationProperties::zOffset
double zOffset() const
Returns the z offset, which is a fixed offset amount which should be added to z values from the layer...
Definition: qgsmaplayerelevationproperties.h:180
QgsVectorLayerElevationProperties::binding
Qgis::AltitudeBinding binding() const
Returns the altitude binding method, which determines how altitude is bound to individual vertices in...
Definition: qgsvectorlayerelevationproperties.h:84
Qgs3DUtils::cullingModeToString
static QString cullingModeToString(Qgs3DTypes::CullingMode mode)
Converts a value from CullingMode enum to a string.
Definition: qgs3dutils.cpp:287
QgsPropertyCollection::isActive
bool isActive(int key) const override
Returns true if the collection contains an active property with the specified key.
Definition: qgspropertycollection.cpp:268
QgsPolygon3DSymbol::writeXml
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Definition: qgspolygon3dsymbol.cpp:55
QgsMapLayerElevationProperties::dataDefinedProperties
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the object's property collection, used for data defined overrides.
Definition: qgsmaplayerelevationproperties.h:223