QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
23 
25 {
26  return new QgsPoint3DSymbol( *this );
27 }
28 
30 {
31  setBillboardSymbol( static_cast<QgsMarkerSymbol *>( QgsSymbol::defaultSymbol( QgsWkbTypes::PointGeometry ) ) );
32 }
33 
35  mAltClamping( other.altitudeClamping() )
36  , mMaterial( other.material() )
37  , mShape( other.shape() )
38  , mShapeProperties( other.shapeProperties() )
39  , mTransform( other.transform() )
40  , mBillboardSymbol( other.billboardSymbol() ? other.billboardSymbol()->clone() : nullptr )
41 {
43 }
44 
45 void QgsPoint3DSymbol::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
46 {
47  QDomDocument doc = elem.ownerDocument();
48 
49  QDomElement elemDataProperties = doc.createElement( QStringLiteral( "data" ) );
50  elemDataProperties.setAttribute( QStringLiteral( "alt-clamping" ), Qgs3DUtils::altClampingToString( mAltClamping ) );
51  elem.appendChild( elemDataProperties );
52 
53  QDomElement elemMaterial = doc.createElement( QStringLiteral( "material" ) );
54  mMaterial.writeXml( elemMaterial );
55  elem.appendChild( elemMaterial );
56 
57  elem.setAttribute( QStringLiteral( "shape" ), shapeToString( mShape ) );
58 
59  QVariantMap shapePropertiesCopy( mShapeProperties );
60  shapePropertiesCopy[QStringLiteral( "model" )] = QVariant( context.pathResolver().writePath( shapePropertiesCopy[QStringLiteral( "model" )].toString() ) );
61 
62  QDomElement elemShapeProperties = doc.createElement( QStringLiteral( "shape-properties" ) );
63  elemShapeProperties.appendChild( QgsXmlUtils::writeVariant( shapePropertiesCopy, doc ) );
64  elem.appendChild( elemShapeProperties );
65 
66  QDomElement elemTransform = doc.createElement( QStringLiteral( "transform" ) );
67  elemTransform.setAttribute( QStringLiteral( "matrix" ), Qgs3DUtils::matrix4x4toString( mTransform ) );
68  elem.appendChild( elemTransform );
69 
70  if ( billboardSymbol() )
71  {
72  QDomElement symbolElem = QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "symbol" ), billboardSymbol(), doc, context );
73 
74  elem.appendChild( symbolElem );
75  }
76 }
77 
78 void QgsPoint3DSymbol::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
79 {
80  QDomElement elemDataProperties = elem.firstChildElement( QStringLiteral( "data" ) );
81  mAltClamping = Qgs3DUtils::altClampingFromString( elemDataProperties.attribute( QStringLiteral( "alt-clamping" ) ) );
82 
83  QDomElement elemMaterial = elem.firstChildElement( QStringLiteral( "material" ) );
84  mMaterial.readXml( elemMaterial );
85 
86  mShape = shapeFromString( elem.attribute( QStringLiteral( "shape" ) ) );
87 
88  QDomElement elemShapeProperties = elem.firstChildElement( QStringLiteral( "shape-properties" ) );
89  mShapeProperties = QgsXmlUtils::readVariant( elemShapeProperties.firstChildElement() ).toMap();
90  mShapeProperties[QStringLiteral( "model" )] = QVariant( context.pathResolver().readPath( mShapeProperties[QStringLiteral( "model" )].toString() ) );
91 
92  QDomElement elemTransform = elem.firstChildElement( QStringLiteral( "transform" ) );
93  mTransform = Qgs3DUtils::stringToMatrix4x4( elemTransform.attribute( QStringLiteral( "matrix" ) ) );
94 
95  QDomElement symbolElem = elem.firstChildElement( QStringLiteral( "symbol" ) );
96 
97  setBillboardSymbol( QgsSymbolLayerUtils::loadSymbol< QgsMarkerSymbol >( symbolElem, context ) );
98 }
99 
101 {
102  if ( shape == QStringLiteral( "sphere" ) )
103  return Sphere;
104  else if ( shape == QStringLiteral( "cone" ) )
105  return Cone;
106  else if ( shape == QStringLiteral( "cube" ) )
107  return Cube;
108  else if ( shape == QStringLiteral( "torus" ) )
109  return Torus;
110  else if ( shape == QStringLiteral( "plane" ) )
111  return Plane;
112  else if ( shape == QStringLiteral( "extruded-text" ) )
113  return ExtrudedText;
114  else if ( shape == QStringLiteral( "model" ) )
115  return Model;
116  else if ( shape == QStringLiteral( "billboard" ) )
117  return Billboard;
118  else // "cylinder" (default)
119  return Cylinder;
120 }
121 
123 {
124  switch ( shape )
125  {
126  case Cylinder: return QStringLiteral( "cylinder" );
127  case Sphere: return QStringLiteral( "sphere" );
128  case Cone: return QStringLiteral( "cone" );
129  case Cube: return QStringLiteral( "cube" );
130  case Torus: return QStringLiteral( "torus" );
131  case Plane: return QStringLiteral( "plane" );
132  case ExtrudedText: return QStringLiteral( "extruded-text" );
133  case Model: return QStringLiteral( "model" );
134  case Billboard: return QStringLiteral( "billboard" );
135  default: Q_ASSERT( false ); return QString();
136  }
137 }
138 
140 {
141 
142  float *md = transform().data();
143 
144  QMatrix4x4 billboardTransformMatrix;
145  billboardTransformMatrix.translate( QVector3D( 0, md[13], 0 ) );
146 
147  return billboardTransformMatrix;
148 
149 }
The class is used as a container of context for various read/write operations on other objects...
static QString altClampingToString(Qgs3DTypes::AltitudeClamping altClamp)
Converts a value from AltitudeClamping enum to a string.
Definition: qgs3dutils.cpp:184
QString readPath(const QString &filename) const
Turn filename read from the project file to an absolute path.
Qgs3DTypes::AltitudeClamping altitudeClamping() const
Returns method that determines altitude (whether to clamp to feature to terrain)
Shape shape() const
Returns 3D shape for points.
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the symbol layer&#39;s property collection, used for data defined overrides...
static Qgs3DTypes::AltitudeClamping altClampingFromString(const QString &str)
Converts a string to a value from AltitudeClamping enum.
Definition: qgs3dutils.cpp:196
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
QVariantMap shapeProperties() const
Returns a key-value dictionary of point shape properties.
Supported in Qt 5.9+.
3 Abstract base class for 3D symbols that are used by VectorLayer3DRenderer objects.
QMatrix4x4 transform() const
Returns transform for individual objects represented by the symbol.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
static QgsSymbol * defaultSymbol(QgsWkbTypes::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
Definition: qgssymbol.cpp:297
3 3D symbol that draws point geometries as 3D objects using one of the predefined shapes...
QgsPhongMaterialSettings material() const
Returns material used for shading of the symbol.
void readXml(const QDomElement &elem)
Reads settings from a DOM element.
QMatrix4x4 billboardTransform() const
Returns transform for billboards.
QgsMarkerSymbol * billboardSymbol() const
Returns a symbol for billboard.
QgsAbstract3DSymbol * clone() const override
Returns a new instance of the symbol with the same settings.
Shape
3D shape types supported by the symbol
static Shape shapeFromString(const QString &shape)
Returns shape enum value from a string.
void writeXml(QDomElement &elem) const
Writes settings to a DOM element.
void setBillboardSymbol(QgsMarkerSymbol *symbol)
Set symbol for billboard and the ownership is transferred.
const QgsPathResolver & pathResolver() const
Returns path resolver for conversion between relative and absolute paths.
void setDataDefinedProperties(const QgsPropertyCollection &collection)
Sets the symbol layer&#39;s property collection, used for data defined overrides.
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Writes symbol configuration to the given DOM element.
static QMatrix4x4 stringToMatrix4x4(const QString &str)
Convert a string to a 4x4 transform matrix.
Definition: qgs3dutils.cpp:336
QString writePath(const QString &filename) const
Prepare a filename to save it to the project file.
static QString shapeToString(Shape shape)
Returns string from a shape enum value.
static QString matrix4x4toString(const QMatrix4x4 &m)
Converts a 4x4 transform matrix to a string.
Definition: qgs3dutils.cpp:326
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
QgsPoint3DSymbol()
Constructor for QgsPoint3DSymbol with default QgsMarkerSymbol as the billboardSymbol.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads symbol configuration from the given DOM element.