QGIS API Documentation  3.20.0-Odense (decaadbb31)
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( std::make_unique< QgsPhongMaterialSettings >() )
28 {
29 
30 }
31 
33 
35 {
36  std::unique_ptr< QgsPolygon3DSymbol > result = std::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 }
Manages the data of each object of the scene (positions, normals, texture coordinates ....
Entity that handles the exporting of 3D scene.
static QString altBindingToString(Qgs3DTypes::AltitudeBinding altBind)
Converts a value from AltitudeBinding enum to a string.
Definition: qgs3dutils.cpp:210
static Qgs3DTypes::CullingMode cullingModeFromString(const QString &str)
Converts a string to a value from CullingMode enum.
Definition: qgs3dutils.cpp:241
static QString altClampingToString(Qgs3DTypes::AltitudeClamping altClamp)
Converts a value from AltitudeClamping enum to a string.
Definition: qgs3dutils.cpp:187
static QString cullingModeToString(Qgs3DTypes::CullingMode mode)
Converts a value from CullingMode enum to a string.
Definition: qgs3dutils.cpp:229
static Qgs3DTypes::AltitudeClamping altClampingFromString(const QString &str)
Converts a string to a value from AltitudeClamping enum.
Definition: qgs3dutils.cpp:199
static Qgs3DTypes::AltitudeBinding altBindingFromString(const QString &str)
Converts a string to a value from AltitudeBinding enum.
Definition: qgs3dutils.cpp:221
static QgsMaterialRegistry * materialRegistry()
Returns the material registry, used for managing 3D materials.
Definition: qgs3d.cpp:90
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
static QgsAbstract3DSymbol * create() SIP_FACTORY
Creates a new QgsPolygon3DSymbol.
QgsPolygon3DSymbol()
Constructor for QgsPolygon3DSymbol.
QgsAbstract3DSymbol * clone() const override SIP_FACTORY
~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 setMaterial(QgsAbstractMaterialSettings *material SIP_TRANSFER)
Sets the material settings used for shading of the symbol.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
QgsAbstractMaterialSettings * material() const
Returns material used for shading of the symbol.
QList< QgsWkbTypes::GeometryType > compatibleGeometryTypes() const override
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)