QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgspoint3dsymbol.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspoint3dsymbol.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 "qgspoint3dsymbol.h"
17 
18 #include "qgs3dutils.h"
19 #include "qgsreadwritecontext.h"
20 #include "qgsxmlutils.h"
21 #include "qgssymbollayerutils.h"
22 #include "qgs3d.h"
23 #include "qgsmaterialregistry.h"
24 #include "qgs3dexportobject.h"
25 #include "qgs3dsceneexporter.h"
26 
28 {
29  return new QgsPoint3DSymbol( *this );
30 }
31 
33 {
34  return new QgsPoint3DSymbol();
35 }
36 
38  : mMaterial( qgis::make_unique< QgsPhongMaterialSettings >() )
39 {
41 }
42 
44  : mAltClamping( other.altitudeClamping() )
45  , mMaterial( other.material() ? other.material()->clone() : nullptr )
46  , mShape( other.shape() )
47  , mShapeProperties( other.shapeProperties() )
48  , mTransform( other.transform() )
49  , mBillboardSymbol( other.billboardSymbol() ? other.billboardSymbol()->clone() : nullptr )
50 {
51  setDataDefinedProperties( other.dataDefinedProperties() );
52 }
53 
54 void QgsPoint3DSymbol::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
55 {
56  QDomDocument doc = elem.ownerDocument();
57 
58  QDomElement elemDataProperties = doc.createElement( QStringLiteral( "data" ) );
59  elemDataProperties.setAttribute( QStringLiteral( "alt-clamping" ), Qgs3DUtils::altClampingToString( mAltClamping ) );
60  elem.appendChild( elemDataProperties );
61 
62  elem.setAttribute( QStringLiteral( "material_type" ), mMaterial->type() );
63  QDomElement elemMaterial = doc.createElement( QStringLiteral( "material" ) );
64  mMaterial->writeXml( elemMaterial, context );
65  elem.appendChild( elemMaterial );
66 
67  elem.setAttribute( QStringLiteral( "shape" ), shapeToString( mShape ) );
68 
69  QVariantMap shapePropertiesCopy( mShapeProperties );
70  shapePropertiesCopy[QStringLiteral( "model" )] = QVariant( context.pathResolver().writePath( shapePropertiesCopy[QStringLiteral( "model" )].toString() ) );
71 
72  QDomElement elemShapeProperties = doc.createElement( QStringLiteral( "shape-properties" ) );
73  elemShapeProperties.appendChild( QgsXmlUtils::writeVariant( shapePropertiesCopy, doc ) );
74  elem.appendChild( elemShapeProperties );
75 
76  QDomElement elemTransform = doc.createElement( QStringLiteral( "transform" ) );
77  elemTransform.setAttribute( QStringLiteral( "matrix" ), Qgs3DUtils::matrix4x4toString( mTransform ) );
78  elem.appendChild( elemTransform );
79 
80  if ( billboardSymbol() )
81  {
82  QDomElement symbolElem = QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "symbol" ), billboardSymbol(), doc, context );
83 
84  elem.appendChild( symbolElem );
85  }
86 }
87 
88 void QgsPoint3DSymbol::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
89 {
90  QDomElement elemDataProperties = elem.firstChildElement( QStringLiteral( "data" ) );
91  mAltClamping = Qgs3DUtils::altClampingFromString( elemDataProperties.attribute( QStringLiteral( "alt-clamping" ) ) );
92 
93  QDomElement elemMaterial = elem.firstChildElement( QStringLiteral( "material" ) );
94  const QString materialType = elem.attribute( QStringLiteral( "material_type" ), QStringLiteral( "phong" ) );
95  mMaterial.reset( Qgs3D::materialRegistry()->createMaterialSettings( materialType ) );
96  if ( !mMaterial )
97  mMaterial.reset( Qgs3D::materialRegistry()->createMaterialSettings( QStringLiteral( "phong" ) ) );
98  mMaterial->readXml( elemMaterial, context );
99 
100  mShape = shapeFromString( elem.attribute( QStringLiteral( "shape" ) ) );
101 
102  QDomElement elemShapeProperties = elem.firstChildElement( QStringLiteral( "shape-properties" ) );
103  mShapeProperties = QgsXmlUtils::readVariant( elemShapeProperties.firstChildElement() ).toMap();
104  mShapeProperties[QStringLiteral( "model" )] = QVariant( context.pathResolver().readPath( mShapeProperties[QStringLiteral( "model" )].toString() ) );
105 
106  QDomElement elemTransform = elem.firstChildElement( QStringLiteral( "transform" ) );
107  mTransform = Qgs3DUtils::stringToMatrix4x4( elemTransform.attribute( QStringLiteral( "matrix" ) ) );
108 
109  QDomElement symbolElem = elem.firstChildElement( QStringLiteral( "symbol" ) );
110 
111  setBillboardSymbol( QgsSymbolLayerUtils::loadSymbol< QgsMarkerSymbol >( symbolElem, context ) );
112 }
113 
114 QList<QgsWkbTypes::GeometryType> QgsPoint3DSymbol::compatibleGeometryTypes() const
115 {
116  return QList< QgsWkbTypes::GeometryType >() << QgsWkbTypes::PointGeometry;
117 }
118 
120 {
121  if ( shape == QStringLiteral( "sphere" ) )
122  return Sphere;
123  else if ( shape == QLatin1String( "cone" ) )
124  return Cone;
125  else if ( shape == QLatin1String( "cube" ) )
126  return Cube;
127  else if ( shape == QLatin1String( "torus" ) )
128  return Torus;
129  else if ( shape == QLatin1String( "plane" ) )
130  return Plane;
131  else if ( shape == QLatin1String( "extruded-text" ) )
132  return ExtrudedText;
133  else if ( shape == QLatin1String( "model" ) )
134  return Model;
135  else if ( shape == QLatin1String( "billboard" ) )
136  return Billboard;
137  else // "cylinder" (default)
138  return Cylinder;
139 }
140 
142 {
143  switch ( shape )
144  {
145  case Cylinder: return QStringLiteral( "cylinder" );
146  case Sphere: return QStringLiteral( "sphere" );
147  case Cone: return QStringLiteral( "cone" );
148  case Cube: return QStringLiteral( "cube" );
149  case Torus: return QStringLiteral( "torus" );
150  case Plane: return QStringLiteral( "plane" );
151  case ExtrudedText: return QStringLiteral( "extruded-text" );
152  case Model: return QStringLiteral( "model" );
153  case Billboard: return QStringLiteral( "billboard" );
154  default: Q_ASSERT( false ); return QString();
155  }
156 }
157 
159 {
160  QMatrix4x4 billboardTransformMatrix;
161  billboardTransformMatrix.translate( QVector3D( 0, mTransform.data()[13], 0 ) );
162 
163  return billboardTransformMatrix;
164 }
165 
167 {
168  return mMaterial.get();
169 }
170 
172 {
173  if ( material == mMaterial.get() )
174  return;
175 
176  mMaterial.reset( material );
177 }
178 
179 bool QgsPoint3DSymbol::exportGeometries( Qgs3DSceneExporter *exporter, Qt3DCore::QEntity *entity, const QString &objectNamePrefix ) const
180 {
181  if ( shape() == QgsPoint3DSymbol::Model )
182  {
183  Qt3DRender::QSceneLoader *sceneLoader = entity->findChild<Qt3DRender::QSceneLoader *>();
184  if ( sceneLoader != nullptr )
185  {
186  QVector<Qgs3DExportObject *> objects = exporter->processSceneLoaderGeometries( sceneLoader, objectNamePrefix );
187  for ( Qgs3DExportObject *obj : objects )
188  {
189  obj->setSmoothEdges( exporter->smoothEdges() );
190  obj->setupMaterial( material() );
191  }
192  exporter->mObjects << objects;
193  }
194  else
195  {
196  QList<Qt3DRender::QMesh *> meshes = entity->findChildren<Qt3DRender::QMesh *>();
197  for ( Qt3DRender::QMesh *mesh : meshes )
198  {
199  Qgs3DExportObject *object = exporter->processGeometryRenderer( mesh, objectNamePrefix );
200  if ( object == nullptr ) continue;
201  object->setSmoothEdges( exporter->smoothEdges() );
202  object->setupMaterial( material() );
203  exporter->mObjects << object;
204  }
205  }
206  return true;
207  }
208  else if ( shape() == QgsPoint3DSymbol::Billboard )
209  {
210  Qgs3DExportObject *obj = exporter->processPoints( entity, objectNamePrefix );
211  if ( obj != nullptr ) exporter->mObjects << obj;
212  if ( obj != nullptr ) return true;
213  }
214  else
215  {
216  QVector<Qgs3DExportObject *> objects = exporter->processInstancedPointGeometry( entity, objectNamePrefix );
217  for ( Qgs3DExportObject *obj : objects )
218  {
219  obj->setupMaterial( material() );
220  exporter->mObjects << obj;
221  }
222  return true;
223  }
224  return false;
225 }
qgs3dexportobject.h
QgsPoint3DSymbol::billboardTransform
QMatrix4x4 billboardTransform() const
Returns transform for billboards.
Definition: qgspoint3dsymbol.cpp:158
qgs3d.h
Qgs3DExportObject::setSmoothEdges
void setSmoothEdges(bool smoothEdges)
Sets whether triangles edges will look smooth.
Definition: qgs3dexportobject.h:75
QgsPoint3DSymbol::Cube
@ Cube
Definition: qgspoint3dsymbol.h:82
QgsPoint3DSymbol::shapeFromString
static Shape shapeFromString(const QString &shape)
Returns shape enum value from a string.
Definition: qgspoint3dsymbol.cpp:119
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
QgsSymbol::defaultSymbol
static QgsSymbol * defaultSymbol(QgsWkbTypes::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
Definition: qgssymbol.cpp:320
Qgs3D::materialRegistry
static QgsMaterialRegistry * materialRegistry()
Returns the material registry, used for managing 3D materials.
Definition: qgs3d.cpp:85
qgsmaterialregistry.h
qgsreadwritecontext.h
QgsPoint3DSymbol::Shape
Shape
3D shape types supported by the symbol
Definition: qgspoint3dsymbol.h:78
qgs3dsceneexporter.h
Qgs3DExportObject
The Qgs3DExportObject class Manages the data of each object of the scene (positions,...
Definition: qgs3dexportobject.h:42
qgssymbollayerutils.h
QgsPoint3DSymbol::setBillboardSymbol
void setBillboardSymbol(QgsMarkerSymbol *symbol)
Set symbol for billboard and the ownership is transferred.
Definition: qgspoint3dsymbol.h:108
QgsPoint3DSymbol::Sphere
@ Sphere
Definition: qgspoint3dsymbol.h:80
QgsPoint3DSymbol::material
QgsAbstractMaterialSettings * material() const
Returns material used for shading of the symbol.
Definition: qgspoint3dsymbol.cpp:166
QgsPoint3DSymbol::writeXml
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Definition: qgspoint3dsymbol.cpp:54
Qgs3DUtils::stringToMatrix4x4
static QMatrix4x4 stringToMatrix4x4(const QString &str)
Convert a string to a 4x4 transform matrix.
Definition: qgs3dutils.cpp:336
QgsPoint3DSymbol::shapeToString
static QString shapeToString(Shape shape)
Returns string from a shape enum value.
Definition: qgspoint3dsymbol.cpp:141
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
QgsPathResolver::writePath
QString writePath(const QString &filename) const
Prepare a filename to save it to the project file.
Definition: qgspathresolver.cpp:192
Qgs3DSceneExporter
The Qgs3DSceneExporter class Entity that handles the exporting of 3D scene.
Definition: qgs3dsceneexporter.h:57
QgsPoint3DSymbol::Cone
@ Cone
Definition: qgspoint3dsymbol.h:81
QgsPoint3DSymbol::create
static QgsAbstract3DSymbol * create() SIP_FACTORY
Creates a new QgsPoint3DSymbol.
Definition: qgspoint3dsymbol.cpp:32
QgsPoint3DSymbol::QgsPoint3DSymbol
QgsPoint3DSymbol()
Constructor for QgsPoint3DSymbol with default QgsMarkerSymbol as the billboardSymbol.
Definition: qgspoint3dsymbol.cpp:37
QgsPoint3DSymbol::readXml
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Definition: qgspoint3dsymbol.cpp:88
QgsAbstractMaterialSettings
3 Abstract base class for material settings.
Definition: qgsabstractmaterialsettings.h:105
QgsXmlUtils::readVariant
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
Definition: qgsxmlutils.cpp:251
qgs3dutils.h
QgsMarkerSymbol
A marker symbol type, for rendering Point and MultiPoint geometries.
Definition: qgssymbol.h:931
Qgs3DUtils::matrix4x4toString
static QString matrix4x4toString(const QMatrix4x4 &m)
Converts a 4x4 transform matrix to a string.
Definition: qgs3dutils.cpp:326
QgsPoint3DSymbol::Model
@ Model
Definition: qgspoint3dsymbol.h:86
QgsPoint3DSymbol::Cylinder
@ Cylinder
Definition: qgspoint3dsymbol.h:79
Qgs3DUtils::altClampingToString
static QString altClampingToString(Qgs3DTypes::AltitudeClamping altClamp)
Converts a value from AltitudeClamping enum to a string.
Definition: qgs3dutils.cpp:184
Qgs3DSceneExporter::smoothEdges
bool smoothEdges() const
Returns whether the triangles will look smooth.
Definition: qgs3dsceneexporter.h:83
QgsPoint3DSymbol::Torus
@ Torus
Definition: qgspoint3dsymbol.h:83
qgsxmlutils.h
QgsPoint3DSymbol::shape
Shape shape() const
Returns 3D shape for points.
Definition: qgspoint3dsymbol.h:96
QgsPoint3DSymbol::Plane
@ Plane
Definition: qgspoint3dsymbol.h:84
QgsPathResolver::readPath
QString readPath(const QString &filename) const
Turn filename read from the project file to an absolute path.
Definition: qgspathresolver.cpp:35
Qgs3DUtils::altClampingFromString
static Qgs3DTypes::AltitudeClamping altClampingFromString(const QString &str)
Converts a string to a value from AltitudeClamping enum.
Definition: qgs3dutils.cpp:196
QgsWkbTypes::PointGeometry
@ PointGeometry
Definition: qgswkbtypes.h:142
qgspoint3dsymbol.h
QgsPoint3DSymbol::ExtrudedText
@ ExtrudedText
Supported in Qt 5.9+.
Definition: qgspoint3dsymbol.h:85
QgsPoint3DSymbol::setMaterial
void setMaterial(QgsAbstractMaterialSettings *material SIP_TRANSFER)
Sets the material settings used for shading of the symbol.
Definition: qgspoint3dsymbol.cpp:171
QgsPoint3DSymbol::compatibleGeometryTypes
QList< QgsWkbTypes::GeometryType > compatibleGeometryTypes() const override
Definition: qgspoint3dsymbol.cpp:114
QgsXmlUtils::writeVariant
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
Definition: qgsxmlutils.cpp:106
QgsPoint3DSymbol::Billboard
@ Billboard
Definition: qgspoint3dsymbol.h:87
QgsSymbolLayerUtils::saveSymbol
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
Definition: qgssymbollayerutils.cpp:1182
QgsPoint3DSymbol::billboardSymbol
QgsMarkerSymbol * billboardSymbol() const
Returns a symbol for billboard.
Definition: qgspoint3dsymbol.h:106
QgsPoint3DSymbol::clone
QgsAbstract3DSymbol * clone() const override SIP_FACTORY
Definition: qgspoint3dsymbol.cpp:27
QgsPoint3DSymbol
3 3D symbol that draws point geometries as 3D objects using one of the predefined shapes.
Definition: qgspoint3dsymbol.h:39
QgsPoint3DSymbol::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: qgspoint3dsymbol.cpp:179
QgsReadWriteContext::pathResolver
const QgsPathResolver & pathResolver() const
Returns path resolver for conversion between relative and absolute paths.
Definition: qgsreadwritecontext.cpp:47