QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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"
25 
27  : mMaterial( qgis::make_unique< QgsPhongMaterialSettings >() )
28 {
29 
30 }
31 
33 
35 {
36  std::unique_ptr< QgsPolygon3DSymbol > result = qgis::make_unique< QgsPolygon3DSymbol >();
37  result->mAltClamping = mAltClamping;
38  result->mAltBinding = mAltBinding;
39  result->mHeight = mHeight;
40  result->mExtrusionHeight = mExtrusionHeight;
41  result->mMaterial.reset( mMaterial->clone() );
42  result->mCullingMode = mCullingMode;
43  result->mInvertNormals = mInvertNormals;
44  result->mAddBackFaces = mAddBackFaces;
45  result->mRenderedFacade = mRenderedFacade;
46  result->mEdgesEnabled = mEdgesEnabled;
47  result->mEdgeWidth = mEdgeWidth;
48  result->mEdgeColor = mEdgeColor;
49  copyBaseSettings( result.get() );
50  return result.release();
51 }
52 
53 void QgsPolygon3DSymbol::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
54 {
55  Q_UNUSED( context )
56 
57  QDomDocument doc = elem.ownerDocument();
58 
59  QDomElement elemDataProperties = doc.createElement( QStringLiteral( "data" ) );
60  elemDataProperties.setAttribute( QStringLiteral( "alt-clamping" ), Qgs3DUtils::altClampingToString( mAltClamping ) );
61  elemDataProperties.setAttribute( QStringLiteral( "alt-binding" ), Qgs3DUtils::altBindingToString( mAltBinding ) );
62  elemDataProperties.setAttribute( QStringLiteral( "height" ), mHeight );
63  elemDataProperties.setAttribute( QStringLiteral( "extrusion-height" ), mExtrusionHeight );
64  elemDataProperties.setAttribute( QStringLiteral( "culling-mode" ), Qgs3DUtils::cullingModeToString( mCullingMode ) );
65  elemDataProperties.setAttribute( QStringLiteral( "invert-normals" ), mInvertNormals ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
66  elemDataProperties.setAttribute( QStringLiteral( "add-back-faces" ), mAddBackFaces ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
67  elemDataProperties.setAttribute( QStringLiteral( "rendered-facade" ), mRenderedFacade );
68  elem.appendChild( elemDataProperties );
69 
70  elem.setAttribute( QStringLiteral( "material_type" ), mMaterial->type() );
71  QDomElement elemMaterial = doc.createElement( QStringLiteral( "material" ) );
72  mMaterial->writeXml( elemMaterial, context );
73  elem.appendChild( elemMaterial );
74 
75  QDomElement elemDDP = doc.createElement( QStringLiteral( "data-defined-properties" ) );
76  mDataDefinedProperties.writeXml( elemDDP, propertyDefinitions() );
77  elem.appendChild( elemDDP );
78 
79  QDomElement elemEdges = doc.createElement( QStringLiteral( "edges" ) );
80  elemEdges.setAttribute( QStringLiteral( "enabled" ), mEdgesEnabled ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
81  elemEdges.setAttribute( QStringLiteral( "width" ), mEdgeWidth );
82  elemEdges.setAttribute( QStringLiteral( "color" ), QgsSymbolLayerUtils::encodeColor( mEdgeColor ) );
83  elem.appendChild( elemEdges );
84 }
85 
86 void QgsPolygon3DSymbol::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
87 {
88  Q_UNUSED( context )
89 
90  QDomElement elemDataProperties = elem.firstChildElement( QStringLiteral( "data" ) );
91  mAltClamping = Qgs3DUtils::altClampingFromString( elemDataProperties.attribute( QStringLiteral( "alt-clamping" ) ) );
92  mAltBinding = Qgs3DUtils::altBindingFromString( elemDataProperties.attribute( QStringLiteral( "alt-binding" ) ) );
93  mHeight = elemDataProperties.attribute( QStringLiteral( "height" ) ).toFloat();
94  mExtrusionHeight = elemDataProperties.attribute( QStringLiteral( "extrusion-height" ) ).toFloat();
95  mCullingMode = Qgs3DUtils::cullingModeFromString( elemDataProperties.attribute( QStringLiteral( "culling-mode" ) ) );
96  mInvertNormals = elemDataProperties.attribute( QStringLiteral( "invert-normals" ) ).toInt();
97  mAddBackFaces = elemDataProperties.attribute( QStringLiteral( "add-back-faces" ) ).toInt();
98  mRenderedFacade = elemDataProperties.attribute( QStringLiteral( "rendered-facade" ), "3" ).toInt();
99 
100  const QDomElement elemMaterial = elem.firstChildElement( QStringLiteral( "material" ) );
101  const QString materialType = elem.attribute( QStringLiteral( "material_type" ), QStringLiteral( "phong" ) );
102  mMaterial.reset( Qgs3D::materialRegistry()->createMaterialSettings( materialType ) );
103  if ( !mMaterial )
104  mMaterial.reset( Qgs3D::materialRegistry()->createMaterialSettings( QStringLiteral( "phong" ) ) );
105  mMaterial->readXml( elemMaterial, context );
106 
107  QDomElement elemDDP = elem.firstChildElement( QStringLiteral( "data-defined-properties" ) );
108  if ( !elemDDP.isNull() )
109  mDataDefinedProperties.readXml( elemDDP, propertyDefinitions() );
110 
111  QDomElement elemEdges = elem.firstChildElement( QStringLiteral( "edges" ) );
112  if ( !elemEdges.isNull() )
113  {
114  mEdgesEnabled = elemEdges.attribute( QStringLiteral( "enabled" ) ).toInt();
115  mEdgeWidth = elemEdges.attribute( QStringLiteral( "width" ) ).toFloat();
116  mEdgeColor = QgsSymbolLayerUtils::decodeColor( elemEdges.attribute( QStringLiteral( "color" ) ) );
117  }
118 }
119 
120 QList<QgsWkbTypes::GeometryType> QgsPolygon3DSymbol::compatibleGeometryTypes() const
121 {
122  return QList< QgsWkbTypes::GeometryType >() << QgsWkbTypes::PolygonGeometry;
123 }
124 
126 {
127  return new QgsPolygon3DSymbol();
128 }
129 
131 {
132  return mMaterial.get();
133 }
134 
136 {
137  if ( material == mMaterial.get() )
138  return;
139 
140  mMaterial.reset( material );
141 }
142 
143 bool QgsPolygon3DSymbol::exportGeometries( Qgs3DSceneExporter *exporter, Qt3DCore::QEntity *entity, const QString &objectNamePrefix ) const
144 {
145  QList<Qt3DRender::QGeometryRenderer *> renderers = entity->findChildren<Qt3DRender::QGeometryRenderer *>();
146  for ( Qt3DRender::QGeometryRenderer *r : renderers )
147  {
148  Qgs3DExportObject *object = exporter->processGeometryRenderer( r, objectNamePrefix );
149  if ( object == nullptr ) continue;
150  exporter->processEntityMaterial( entity, object );
151  exporter->mObjects.push_back( object );
152  }
153  return renderers.size() != 0;
154 }
QgsSymbolLayerUtils::encodeColor
static QString encodeColor(const QColor &color)
Definition: qgssymbollayerutils.cpp:52
qgs3d.h
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
Qgs3D::materialRegistry
static QgsMaterialRegistry * materialRegistry()
Returns the material registry, used for managing 3D materials.
Definition: qgs3d.cpp:85
qgsmaterialregistry.h
QgsPolygon3DSymbol::material
QgsAbstractMaterialSettings * material() const
Returns material used for shading of the symbol.
Definition: qgspolygon3dsymbol.cpp:130
qgs3dsceneexporter.h
Qgs3DExportObject
The Qgs3DExportObject class Manages the data of each object of the scene (positions,...
Definition: qgs3dexportobject.h:42
qgssymbollayerutils.h
Qgs3DUtils::altBindingFromString
static Qgs3DTypes::AltitudeBinding altBindingFromString(const QString &str)
Converts a string to a value from AltitudeBinding enum.
Definition: qgs3dutils.cpp:218
QgsSymbolLayerUtils::decodeColor
static QColor decodeColor(const QString &str)
Definition: qgssymbollayerutils.cpp:57
QgsWkbTypes::PolygonGeometry
@ PolygonGeometry
Definition: qgswkbtypes.h:144
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:143
QgsPhongMaterialSettings
3 Basic shading material used for rendering based on the Phong shading model with three color compone...
Definition: qgsphongmaterialsettings.h:37
QgsAbstract3DSymbol
3 Abstract base class for 3D symbols that are used by VectorLayer3DRenderer objects.
Definition: qgsabstract3dsymbol.h:46
Qgs3DSceneExporter
The Qgs3DSceneExporter class Entity that handles the exporting of 3D scene.
Definition: qgs3dsceneexporter.h:57
QgsAbstractMaterialSettings
3 Abstract base class for material settings.
Definition: qgsabstractmaterialsettings.h:105
qgs3dutils.h
QgsPolygon3DSymbol::clone
QgsAbstract3DSymbol * clone() const override SIP_FACTORY
Definition: qgspolygon3dsymbol.cpp:34
Qgs3DUtils::altBindingToString
static QString altBindingToString(Qgs3DTypes::AltitudeBinding altBind)
Converts a value from AltitudeBinding enum to a string.
Definition: qgs3dutils.cpp:207
QgsPolygon3DSymbol::~QgsPolygon3DSymbol
~QgsPolygon3DSymbol() override
Qgs3DUtils::altClampingToString
static QString altClampingToString(Qgs3DTypes::AltitudeClamping altClamp)
Converts a value from AltitudeClamping enum to a string.
Definition: qgs3dutils.cpp:184
QgsPolygon3DSymbol::setMaterial
void setMaterial(QgsAbstractMaterialSettings *material SIP_TRANSFER)
Sets the material settings used for shading of the symbol.
Definition: qgspolygon3dsymbol.cpp:135
Qgs3DUtils::cullingModeFromString
static Qgs3DTypes::CullingMode cullingModeFromString(const QString &str)
Converts a string to a value from CullingMode enum.
Definition: qgs3dutils.cpp:238
QgsPolygon3DSymbol::readXml
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Definition: qgspolygon3dsymbol.cpp:86
qgspolygon3dsymbol.h
Qgs3DUtils::altClampingFromString
static Qgs3DTypes::AltitudeClamping altClampingFromString(const QString &str)
Converts a string to a value from AltitudeClamping enum.
Definition: qgs3dutils.cpp:196
QgsPolygon3DSymbol::compatibleGeometryTypes
QList< QgsWkbTypes::GeometryType > compatibleGeometryTypes() const override
Definition: qgspolygon3dsymbol.cpp:120
QgsPolygon3DSymbol::create
static QgsAbstract3DSymbol * create() SIP_FACTORY
Creates a new QgsPolygon3DSymbol.
Definition: qgspolygon3dsymbol.cpp:125
QgsPolygon3DSymbol::QgsPolygon3DSymbol
QgsPolygon3DSymbol()
Constructor for QgsPolygon3DSymbol.
Definition: qgspolygon3dsymbol.cpp:26
Qgs3DUtils::cullingModeToString
static QString cullingModeToString(Qgs3DTypes::CullingMode mode)
Converts a value from CullingMode enum to a string.
Definition: qgs3dutils.cpp:226
QgsPolygon3DSymbol::writeXml
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Definition: qgspolygon3dsymbol.cpp:53