QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsline3dsymbol.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsline3dsymbol.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 "qgsline3dsymbol.h"
17 #include "qgs3dutils.h"
18 #include "qgs3d.h"
19 #include "qgsmaterialregistry.h"
20 #include "qgs3dexportobject.h"
21 #include "qgs3dsceneexporter.h"
22 #include "qgsvectorlayer.h"
24 
26  : mMaterial( std::make_unique< QgsPhongMaterialSettings >() )
27 {
28 
29 }
30 
32 
34 {
35  std::unique_ptr< QgsLine3DSymbol > result = std::make_unique< QgsLine3DSymbol >();
36  result->mAltClamping = mAltClamping;
37  result->mAltBinding = mAltBinding;
38  result->mWidth = mWidth;
39  result->mHeight = mHeight;
40  result->mExtrusionHeight = mExtrusionHeight;
41  result->mRenderAsSimpleLines = mRenderAsSimpleLines;
42  result->mMaterial.reset( mMaterial->clone() );
43  copyBaseSettings( result.get() );
44  return result.release();
45 }
46 
47 void QgsLine3DSymbol::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
48 {
49  Q_UNUSED( context )
50 
51  QDomDocument doc = elem.ownerDocument();
52 
53  QDomElement elemDataProperties = doc.createElement( QStringLiteral( "data" ) );
54  elemDataProperties.setAttribute( QStringLiteral( "alt-clamping" ), Qgs3DUtils::altClampingToString( mAltClamping ) );
55  elemDataProperties.setAttribute( QStringLiteral( "alt-binding" ), Qgs3DUtils::altBindingToString( mAltBinding ) );
56  elemDataProperties.setAttribute( QStringLiteral( "height" ), mHeight );
57  elemDataProperties.setAttribute( QStringLiteral( "extrusion-height" ), mExtrusionHeight );
58  elemDataProperties.setAttribute( QStringLiteral( "simple-lines" ), mRenderAsSimpleLines ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
59  elemDataProperties.setAttribute( QStringLiteral( "width" ), mWidth );
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 
68 void QgsLine3DSymbol::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
69 {
70  Q_UNUSED( context )
71 
72  const QDomElement elemDataProperties = elem.firstChildElement( QStringLiteral( "data" ) );
73  mAltClamping = Qgs3DUtils::altClampingFromString( elemDataProperties.attribute( QStringLiteral( "alt-clamping" ) ) );
74  mAltBinding = Qgs3DUtils::altBindingFromString( elemDataProperties.attribute( QStringLiteral( "alt-binding" ) ) );
75  mHeight = elemDataProperties.attribute( QStringLiteral( "height" ) ).toFloat();
76  mExtrusionHeight = elemDataProperties.attribute( QStringLiteral( "extrusion-height" ) ).toFloat();
77  mWidth = elemDataProperties.attribute( QStringLiteral( "width" ) ).toFloat();
78  mRenderAsSimpleLines = elemDataProperties.attribute( QStringLiteral( "simple-lines" ), QStringLiteral( "0" ) ).toInt();
79 
80  const QDomElement elemMaterial = elem.firstChildElement( QStringLiteral( "material" ) );
81  const QString materialType = elem.attribute( QStringLiteral( "material_type" ), QStringLiteral( "phong" ) );
82  mMaterial.reset( Qgs3D::materialRegistry()->createMaterialSettings( materialType ) );
83  if ( !mMaterial )
84  mMaterial.reset( Qgs3D::materialRegistry()->createMaterialSettings( QStringLiteral( "phong" ) ) );
85  mMaterial->readXml( elemMaterial, context );
86 }
87 
89 {
90  return mMaterial.get();
91 }
92 
94 {
95  if ( material == mMaterial.get() )
96  return;
97 
98  mMaterial.reset( material );
99 }
100 
101 QList<QgsWkbTypes::GeometryType> QgsLine3DSymbol::compatibleGeometryTypes() const
102 {
103  return QList< QgsWkbTypes::GeometryType >() << QgsWkbTypes::LineGeometry;
104 }
105 
107 {
108  const QgsVectorLayerElevationProperties *props = qgis::down_cast< const QgsVectorLayerElevationProperties * >( const_cast< QgsVectorLayer *>( layer )->elevationProperties() );
109 
110  mAltClamping = props->clamping();
111  mAltBinding = props->binding();
112  mExtrusionHeight = props->extrusionEnabled() ? static_cast< float>( props->extrusionHeight() ) : 0.0f;
113  mHeight = static_cast< float >( props->zOffset() );
114 }
115 
117 {
118  return new QgsLine3DSymbol();
119 }
120 
121 bool QgsLine3DSymbol::exportGeometries( Qgs3DSceneExporter *exporter, Qt3DCore::QEntity *entity, const QString &objectNamePrefix ) const
122 {
123  if ( renderAsSimpleLines() )
124  {
125  const QVector<Qgs3DExportObject *> objs = exporter->processLines( entity, objectNamePrefix );
126  exporter->mObjects << objs;
127  return objs.size() != 0;
128  }
129  else
130  {
131  const QList<Qt3DRender::QGeometryRenderer *> renderers = entity->findChildren<Qt3DRender::QGeometryRenderer *>();
132  for ( Qt3DRender::QGeometryRenderer *r : renderers )
133  {
134  Qgs3DExportObject *object = exporter->processGeometryRenderer( r, objectNamePrefix );
135  if ( object == nullptr ) continue;
136  object->setupMaterial( material() );
137  exporter->mObjects.push_back( object );
138  }
139  return renderers.size() != 0;
140  }
141 }
qgs3dexportobject.h
QgsLine3DSymbol::compatibleGeometryTypes
QList< QgsWkbTypes::GeometryType > compatibleGeometryTypes() const override
Definition: qgsline3dsymbol.cpp:101
Qgs3DExportObject::setupMaterial
void setupMaterial(QgsAbstractMaterialSettings *material)
Sets the material parameters (diffuse color, shininess...) from phong material.
Definition: qgs3dexportobject.cpp:77
qgs3d.h
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsLine3DSymbol::~QgsLine3DSymbol
~QgsLine3DSymbol() override
qgsline3dsymbol.h
Qgs3D::materialRegistry
static QgsMaterialRegistry * materialRegistry()
Returns the material registry, used for managing 3D materials.
Definition: qgs3d.cpp:90
qgsmaterialregistry.h
qgs3dsceneexporter.h
Qgs3DExportObject
Manages the data of each object of the scene (positions, normals, texture coordinates ....
Definition: qgs3dexportobject.h:40
QgsLine3DSymbol::setDefaultPropertiesFromLayer
void setDefaultPropertiesFromLayer(const QgsVectorLayer *layer) override
Definition: qgsline3dsymbol.cpp:106
QgsVectorLayerElevationProperties::extrusionHeight
double extrusionHeight() const
Returns the feature extrusion height.
Definition: qgsvectorlayerelevationproperties.h:133
QgsLine3DSymbol::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: qgsline3dsymbol.cpp:121
QgsVectorLayerElevationProperties::clamping
Qgis::AltitudeClamping clamping() const
Returns the altitude clamping method, which dictates how feature heights are interpreted with respect...
Definition: qgsvectorlayerelevationproperties.h:67
QgsLine3DSymbol::QgsLine3DSymbol
QgsLine3DSymbol()
Constructor for QgsLine3DSymbol.
Definition: qgsline3dsymbol.cpp:25
Qgs3DUtils::altBindingFromString
static Qgis::AltitudeBinding altBindingFromString(const QString &str)
Converts a string to a value from AltitudeBinding enum.
Definition: qgs3dutils.cpp:279
QgsLine3DSymbol::renderAsSimpleLines
bool renderAsSimpleLines() const
Returns whether the renderer will render data with simple lines (otherwise it uses buffer)
Definition: qgsline3dsymbol.h:83
QgsPhongMaterialSettings
Basic shading material used for rendering based on the Phong shading model with three color component...
Definition: qgsphongmaterialsettings.h:44
QgsAbstract3DSymbol
Abstract base class for 3D symbols that are used by VectorLayer3DRenderer objects.
Definition: qgsabstract3dsymbol.h:46
Qgs3DSceneExporter
Entity that handles the exporting of 3D scene.
Definition: qgs3dsceneexporter.h:55
QgsLine3DSymbol::readXml
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Definition: qgsline3dsymbol.cpp:68
QgsLine3DSymbol::clone
QgsAbstract3DSymbol * clone() const override SIP_FACTORY
Definition: qgsline3dsymbol.cpp:33
QgsAbstractMaterialSettings
Abstract base class for material settings.
Definition: qgsabstractmaterialsettings.h:114
QgsVectorLayerElevationProperties::extrusionEnabled
bool extrusionEnabled() const
Returns true if extrusion is enabled.
Definition: qgsvectorlayerelevationproperties.h:115
qgs3dutils.h
Qgs3DUtils::altClampingFromString
static Qgis::AltitudeClamping altClampingFromString(const QString &str)
Converts a string to a value from AltitudeClamping enum.
Definition: qgs3dutils.cpp:255
QgsLine3DSymbol::writeXml
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Definition: qgsline3dsymbol.cpp:47
QgsVectorLayerElevationProperties
Vector layer specific subclass of QgsMapLayerElevationProperties.
Definition: qgsvectorlayerelevationproperties.h:38
Qgs3DUtils::altBindingToString
static QString altBindingToString(Qgis::AltitudeBinding altBind)
Converts a value from AltitudeBinding enum to a string.
Definition: qgs3dutils.cpp:266
Qgs3DUtils::altClampingToString
static QString altClampingToString(Qgis::AltitudeClamping altClamp)
Converts a value from AltitudeClamping enum to a string.
Definition: qgs3dutils.cpp:240
qgsvectorlayer.h
QgsLine3DSymbol::material
QgsAbstractMaterialSettings * material() const
Returns material used for shading of the symbol.
Definition: qgsline3dsymbol.cpp:88
QgsWkbTypes::LineGeometry
@ LineGeometry
Definition: qgswkbtypes.h:143
QgsLine3DSymbol::create
static QgsAbstract3DSymbol * create() SIP_FACTORY
Creates a new QgsLine3DSymbol.
Definition: qgsline3dsymbol.cpp:116
qgsvectorlayerelevationproperties.h
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsMapLayerElevationProperties::zOffset
double zOffset() const
Returns the z offset, which is a fixed offset amount which should be added to z values from the layer...
Definition: qgsmaplayerelevationproperties.h:180
QgsVectorLayerElevationProperties::binding
Qgis::AltitudeBinding binding() const
Returns the altitude binding method, which determines how altitude is bound to individual vertices in...
Definition: qgsvectorlayerelevationproperties.h:84
QgsLine3DSymbol::setMaterial
void setMaterial(QgsAbstractMaterialSettings *material SIP_TRANSFER)
Sets the material settings used for shading of the symbol.
Definition: qgsline3dsymbol.cpp:93