QGIS API Documentation 4.1.0-Master (01362494303)
Loading...
Searching...
No Matches
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 "qgs3d.h"
19#include "qgs3dexportobject.h"
20#include "qgs3dsceneexporter.h"
21#include "qgs3dutils.h"
22#include "qgsmarkersymbol.h"
23#include "qgsmaterialregistry.h"
24#include "qgsreadwritecontext.h"
25#include "qgssymbollayerutils.h"
26#include "qgsvectorlayer.h"
28#include "qgsxmlutils.h"
29
30#include <QString>
31
32using namespace Qt::StringLiterals;
33
35{
36 return new QgsPoint3DSymbol( *this );
37}
38
43
49
51 : mAltClamping( other.altitudeClamping() )
52 , mMaterialSettings( other.materialSettings() ? other.materialSettings()->clone() : nullptr )
53 , mShape( other.shape() )
54 , mShapeProperties( other.shapeProperties() )
55 , mTransform( other.transform() )
56 , mBillboardSymbol( other.billboardSymbol() ? other.billboardSymbol()->clone() : nullptr )
57{
59}
60
62
63void QgsPoint3DSymbol::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
64{
65 QDomDocument doc = elem.ownerDocument();
66
67 QDomElement elemDataProperties = doc.createElement( u"data"_s );
68 elemDataProperties.setAttribute( u"alt-clamping"_s, Qgs3DUtils::altClampingToString( mAltClamping ) );
69 elem.appendChild( elemDataProperties );
70
71 elem.setAttribute( u"material_type"_s, mMaterialSettings->type() );
72 QDomElement elemMaterial = doc.createElement( u"material"_s );
73 mMaterialSettings->writeXml( elemMaterial, context );
74 elem.appendChild( elemMaterial );
75
76 elem.setAttribute( u"shape"_s, shapeToString( mShape ) );
77
78 QVariantMap shapePropertiesCopy( mShapeProperties );
79 shapePropertiesCopy[u"model"_s] = QVariant( context.pathResolver().writePath( shapePropertiesCopy[u"model"_s].toString() ) );
80
81 QDomElement elemShapeProperties = doc.createElement( u"shape-properties"_s );
82 elemShapeProperties.appendChild( QgsXmlUtils::writeVariant( shapePropertiesCopy, doc ) );
83 elem.appendChild( elemShapeProperties );
84
85 QDomElement elemTransform = doc.createElement( u"transform"_s );
86 elemTransform.setAttribute( u"matrix"_s, Qgs3DUtils::matrix4x4toString( mTransform ) );
87 elem.appendChild( elemTransform );
88
89 QDomElement elemDDP = doc.createElement( u"data-defined-properties"_s );
90 mDataDefinedProperties.writeXml( elemDDP, propertyDefinitions() );
91 elem.appendChild( elemDDP );
92
93 if ( billboardSymbol() )
94 {
95 const QDomElement symbolElem = QgsSymbolLayerUtils::saveSymbol( u"symbol"_s, billboardSymbol(), doc, context );
96
97 elem.appendChild( symbolElem );
98 }
99}
100
101void QgsPoint3DSymbol::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
102{
103 const QDomElement elemDataProperties = elem.firstChildElement( u"data"_s );
104 mAltClamping = Qgs3DUtils::altClampingFromString( elemDataProperties.attribute( u"alt-clamping"_s ) );
105
106 const QDomElement elemMaterial = elem.firstChildElement( u"material"_s );
107 const QString materialType = elem.attribute( u"material_type"_s, u"phong"_s );
108 mMaterialSettings = Qgs3D::materialRegistry()->createMaterialSettings( materialType );
109 if ( !mMaterialSettings )
110 mMaterialSettings = Qgs3D::materialRegistry()->createMaterialSettings( u"phong"_s );
111 mMaterialSettings->readXml( elemMaterial, context );
112
113 mShape = shapeFromString( elem.attribute( u"shape"_s ) );
114
115 const QDomElement elemShapeProperties = elem.firstChildElement( u"shape-properties"_s );
116 mShapeProperties = QgsXmlUtils::readVariant( elemShapeProperties.firstChildElement() ).toMap();
117 mShapeProperties[u"model"_s] = QVariant( context.pathResolver().readPath( mShapeProperties[u"model"_s].toString() ) );
118
119 const QDomElement elemTransform = elem.firstChildElement( u"transform"_s );
120 mTransform = Qgs3DUtils::stringToMatrix4x4( elemTransform.attribute( u"matrix"_s ) );
121
122 const QDomElement elemDDP = elem.firstChildElement( u"data-defined-properties"_s );
123 if ( !elemDDP.isNull() )
124 mDataDefinedProperties.readXml( elemDDP, propertyDefinitions() );
125
126 const QDomElement symbolElem = elem.firstChildElement( u"symbol"_s );
127
129}
130
131QList<Qgis::GeometryType> QgsPoint3DSymbol::compatibleGeometryTypes() const
132{
133 return QList<Qgis::GeometryType>() << Qgis::GeometryType::Point;
134}
135
137{
138 const QgsVectorLayerElevationProperties *props = qgis::down_cast<const QgsVectorLayerElevationProperties *>( const_cast<QgsVectorLayer *>( layer )->elevationProperties() );
139
140 mAltClamping = props->clamping();
141 mTransform.data()[13] = static_cast<float>( props->zOffset() );
142 mShapeProperties[u"length"_s] = props->extrusionEnabled() ? static_cast<float>( props->extrusionHeight() ) : 0.0f;
143}
144
146{
147 if ( shape == "sphere"_L1 )
149 else if ( shape == "cone"_L1 )
151 else if ( shape == "cube"_L1 )
153 else if ( shape == "torus"_L1 )
155 else if ( shape == "plane"_L1 )
157 else if ( shape == "extruded-text"_L1 )
159 else if ( shape == "model"_L1 )
161 else if ( shape == "billboard"_L1 )
163 else // "cylinder" (default)
165}
166
168{
169 switch ( shape )
170 {
172 return u"cylinder"_s;
174 return u"sphere"_s;
176 return u"cone"_s;
178 return u"cube"_s;
180 return u"torus"_s;
182 return u"plane"_s;
184 return u"extruded-text"_s;
186 return u"model"_s;
188 return u"billboard"_s;
189 default:
190 Q_ASSERT( false );
191 return QString();
192 }
193}
194
195QVariant QgsPoint3DSymbol::shapeProperty( const QString &property ) const
196{
197 switch ( mShape )
198 {
200 {
201 if ( property == "length"_L1 )
202 {
203 const float length = mShapeProperties.value( property ).toFloat();
204 if ( length == 0 )
205 return 10;
206 return length;
207 }
208 else if ( property == "radius"_L1 )
209 {
210 const float radius = mShapeProperties.value( property ).toFloat();
211 if ( radius == 0 )
212 return 10;
213 return radius;
214 }
215 break;
216 }
218 {
219 if ( property == "radius"_L1 )
220 {
221 const float radius = mShapeProperties.value( property ).toFloat();
222 if ( radius == 0 )
223 return 10;
224 return radius;
225 }
226 break;
227 }
229 {
230 if ( property == "length"_L1 )
231 {
232 const float length = mShapeProperties.value( property ).toFloat();
233 if ( length == 0 )
234 return 10;
235 return length;
236 }
237 break;
238 }
240 {
241 if ( property == "size"_L1 )
242 {
243 const float size = mShapeProperties.value( property ).toFloat();
244 if ( size == 0 )
245 return 10;
246 return size;
247 }
248 break;
249 }
251 {
252 if ( property == "radius"_L1 )
253 {
254 const float radius = mShapeProperties.value( property ).toFloat();
255 if ( radius == 0 )
256 return 10;
257 return radius;
258 }
259 else if ( property == "minorRadius"_L1 )
260 {
261 const float minorRadius = mShapeProperties.value( property ).toFloat();
262 if ( minorRadius == 0 )
263 return 5;
264 return minorRadius;
265 }
266 break;
267 }
269 {
270 if ( property == "size"_L1 )
271 {
272 const float size = mShapeProperties.value( property ).toFloat();
273 if ( size == 0 )
274 return 10;
275 return size;
276 }
277 break;
278 }
280 {
281 if ( property == "depth"_L1 )
282 {
283 const float depth = mShapeProperties.value( property ).toFloat();
284 if ( depth == 0 )
285 return 1;
286 return depth;
287 }
288 break;
289 }
290
292 {
293 // defaults are "z" up, "y" forward -- this ensures default rendering matches 3.x appearance
294 if ( property == "upAxis"_L1 )
295 {
296 return mShapeProperties.value( u"upAxis"_s, u"z"_s ).toString();
297 }
298 if ( property == "forwardAxis"_L1 )
299 {
300 return mShapeProperties.value( u"forwardAxis"_s, u"y"_s ).toString();
301 }
302 break;
303 }
304
306 break;
307 }
308 return mShapeProperties.value( property );
309}
310
312{
313 return mTransform.data()[14];
314}
315
317{
318 return mMaterialSettings.get();
319}
320
322{
323 if ( materialSettings == mMaterialSettings.get() )
324 return;
325
326 mMaterialSettings.reset( materialSettings );
327}
328
329bool QgsPoint3DSymbol::exportGeometries( Qgs3DSceneExporter *exporter, Qt3DCore::QEntity *entity, const QString &objectNamePrefix ) const
330{
332 {
333 Qt3DRender::QSceneLoader *sceneLoader = entity->findChild<Qt3DRender::QSceneLoader *>();
334 if ( sceneLoader )
335 {
336 const QVector<Qgs3DExportObject *> objects = exporter->processSceneLoaderGeometries( sceneLoader, objectNamePrefix );
337 for ( Qgs3DExportObject *obj : objects )
338 {
339 obj->setSmoothEdges( exporter->smoothEdges() );
340 obj->setupMaterial( materialSettings() );
341 }
342 exporter->mObjects << objects;
343 }
344 else
345 {
346 const QList<Qt3DRender::QMesh *> meshes = entity->findChildren<Qt3DRender::QMesh *>();
347 for ( Qt3DRender::QMesh *mesh : meshes )
348 {
349 Qgs3DExportObject *object = exporter->processGeometryRenderer( mesh, objectNamePrefix );
350 if ( !object )
351 continue;
352 object->setSmoothEdges( exporter->smoothEdges() );
353 object->setupMaterial( materialSettings() );
354 exporter->mObjects << object;
355 }
356 }
357 return true;
358 }
359 else if ( shape() == Qgis::Point3DShape::Billboard )
360 {
361 Qgs3DExportObject *obj = exporter->processPoints( entity, objectNamePrefix );
362 if ( obj )
363 {
364 exporter->mObjects << obj;
365 return true;
366 }
367 }
368 else
369 {
370 const QVector<Qgs3DExportObject *> objects = exporter->processInstancedPointGeometry( entity, objectNamePrefix );
371 for ( Qgs3DExportObject *obj : objects )
372 {
373 obj->setupMaterial( materialSettings() );
374 exporter->mObjects << obj;
375 }
376 return true;
377 }
378 return false;
379}
380
382{
383 return mBillboardSymbol.get();
384}
385
387{
388 mBillboardSymbol.reset( symbol );
389}
Point3DShape
3D point shape types.
Definition qgis.h:4320
@ Plane
Flat plane.
Definition qgis.h:4326
@ Cylinder
Cylinder.
Definition qgis.h:4321
@ Torus
Torus.
Definition qgis.h:4325
@ ExtrudedText
Extruded text.
Definition qgis.h:4327
@ Model
Model.
Definition qgis.h:4328
@ Sphere
Sphere.
Definition qgis.h:4322
@ Billboard
Billboard.
Definition qgis.h:4329
@ Point
Points.
Definition qgis.h:380
Manages the data of each object of the scene (positions, normals, texture coordinates ....
void setSmoothEdges(bool smoothEdges)
Sets whether triangles edges will look smooth.
Entity that handles the exporting of 3D scenes.
bool smoothEdges() const
Returns whether the triangles will look smooth.
static Qgis::AltitudeClamping altClampingFromString(const QString &str)
Converts a string to a value from AltitudeClamping enum.
static QString matrix4x4toString(const QMatrix4x4 &m)
Converts a 4x4 transform matrix to a string.
static QString altClampingToString(Qgis::AltitudeClamping altClamp)
Converts a value from AltitudeClamping enum to a string.
static QMatrix4x4 stringToMatrix4x4(const QString &str)
Convert a string to a 4x4 transform matrix.
static QgsMaterialRegistry * materialRegistry()
Returns the material registry, used for managing 3D materials.
Definition qgs3d.cpp:114
Abstract base class for 3D symbols that are used by VectorLayer3DRenderer objects.
static const QgsPropertiesDefinition & propertyDefinitions()
Returns the symbol layer property definitions.
void setDataDefinedProperties(const QgsPropertyCollection &collection)
Sets the symbol layer's property collection, used for data defined overrides.
QgsPropertyCollection mDataDefinedProperties
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the symbol layer's property collection, used for data defined overrides.
Abstract base class for material settings.
double zOffset() const
Returns the z offset, which is a fixed offset amount which should be added to z values from the layer...
A marker symbol type, for rendering Point and MultiPoint geometries.
std::unique_ptr< QgsAbstractMaterialSettings > createMaterialSettings(const QString &type) const
Creates a new instance of the material settings of the specified type.
QString writePath(const QString &filename) const
Prepare a filename to save it to the project file.
QString readPath(const QString &filename) const
Turn filename read from the project file to an absolute path.
Basic shading material used for rendering based on the Phong shading model with three color component...
static QString shapeToString(Qgis::Point3DShape shape)
Returns string from a shape enum value.
~QgsPoint3DSymbol() override
void setDefaultPropertiesFromLayer(const QgsVectorLayer *layer) override
Sets default properties for the symbol based on a layer's configuration.
QgsAbstractMaterialSettings * materialSettings() const
Returns material settings used for shading of the symbol.
static QgsAbstract3DSymbol * create()
Creates a new QgsPoint3DSymbol.
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Writes symbol configuration to the given DOM element.
QgsPoint3DSymbol()
Constructor for QgsPoint3DSymbol with default QgsMarkerSymbol as the billboardSymbol.
QMatrix4x4 transform() const
Returns transform for individual objects represented by the symbol.
QgsMarkerSymbol * billboardSymbol() const
Returns a symbol for billboard.
Qgis::Point3DShape shape() const
Returns 3D shape for points.
float billboardHeight() const
Returns how much the billboard should be elevated upwards.
QList< Qgis::GeometryType > compatibleGeometryTypes() const override
Returns the list of the vector layer geometry types which are compatible with this symbol.
bool exportGeometries(Qgs3DSceneExporter *exporter, Qt3DCore::QEntity *entity, const QString &objectNamePrefix) const override
Exports the geometries contained within the hierarchy of entity.
void setBillboardSymbol(QgsMarkerSymbol *symbol)
Set symbol for billboard and the ownership is transferred.
QVariantMap shapeProperties() const
Returns a key-value dictionary of point shape properties.
Qgis::AltitudeClamping altitudeClamping() const
Returns method that determines altitude (whether to clamp to feature to terrain).
QgsAbstract3DSymbol * clone() const override
Returns a new instance of the symbol with the same settings.
void setMaterialSettings(QgsAbstractMaterialSettings *materialSettings)
Sets the material settings used for shading of the symbol.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads symbol configuration from the given DOM element.
QVariant shapeProperty(const QString &property) const
Returns the value for a specific shape property.
static Qgis::Point3DShape shapeFromString(const QString &shape)
Returns shape enum value from a string.
A container for the context for various read/write operations on objects.
const QgsPathResolver & pathResolver() const
Returns path resolver for conversion between relative and absolute paths.
static std::unique_ptr< QgsSymbol > loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element.
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
static QgsSymbol * defaultSymbol(Qgis::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
Vector layer specific subclass of QgsMapLayerElevationProperties.
double extrusionHeight() const
Returns the feature extrusion height.
Qgis::AltitudeClamping clamping() const
Returns the altitude clamping method, which dictates how feature heights are interpreted with respect...
bool extrusionEnabled() const
Returns true if extrusion is enabled.
Represents a vector layer which manages a vector based dataset.
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.