QGIS API Documentation 3.99.0-Master (a8f284845db)
Loading...
Searching...
No Matches
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
18#include "qgis.h"
19#include "qgs3d.h"
20#include "qgs3dexportobject.h"
21#include "qgs3dsceneexporter.h"
22#include "qgs3dutils.h"
23#include "qgsmaterialregistry.h"
24#include "qgsvectorlayer.h"
26
27#include <QString>
28
29using namespace Qt::StringLiterals;
30
32 : mMaterialSettings( std::make_unique<QgsPhongMaterialSettings>() )
33{
34}
35
37
39{
40 auto result = std::make_unique<QgsLine3DSymbol>();
41 result->mAltClamping = mAltClamping;
42 result->mAltBinding = mAltBinding;
43 result->mWidth = mWidth;
44 result->mOffset = mOffset;
45 result->mExtrusionHeight = mExtrusionHeight;
46 result->mRenderAsSimpleLines = mRenderAsSimpleLines;
47 result->mMaterialSettings.reset( mMaterialSettings->clone() );
48 copyBaseSettings( result.get() );
49 return result.release();
50}
51
52void QgsLine3DSymbol::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
53{
54 Q_UNUSED( context )
55
56 QDomDocument doc = elem.ownerDocument();
57
58 QDomElement elemDataProperties = doc.createElement( u"data"_s );
59 elemDataProperties.setAttribute( u"alt-clamping"_s, Qgs3DUtils::altClampingToString( mAltClamping ) );
60 elemDataProperties.setAttribute( u"alt-binding"_s, Qgs3DUtils::altBindingToString( mAltBinding ) );
61 elemDataProperties.setAttribute( u"offset"_s, mOffset );
62 elemDataProperties.setAttribute( u"extrusion-height"_s, mExtrusionHeight );
63 elemDataProperties.setAttribute( u"simple-lines"_s, mRenderAsSimpleLines ? u"1"_s : u"0"_s );
64 elemDataProperties.setAttribute( u"width"_s, mWidth );
65 elem.appendChild( elemDataProperties );
66
67 elem.setAttribute( u"material_type"_s, mMaterialSettings->type() );
68 QDomElement elemMaterial = doc.createElement( u"material"_s );
69 mMaterialSettings->writeXml( elemMaterial, context );
70 elem.appendChild( elemMaterial );
71}
72
73void QgsLine3DSymbol::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
74{
75 Q_UNUSED( context )
76
77 const QDomElement elemDataProperties = elem.firstChildElement( u"data"_s );
78 mAltClamping = Qgs3DUtils::altClampingFromString( elemDataProperties.attribute( u"alt-clamping"_s ) );
79 mAltBinding = Qgs3DUtils::altBindingFromString( elemDataProperties.attribute( u"alt-binding"_s ) );
80 mOffset = elemDataProperties.attribute( u"offset"_s ).toFloat();
81 mExtrusionHeight = elemDataProperties.attribute( u"extrusion-height"_s ).toFloat();
82 mWidth = elemDataProperties.attribute( u"width"_s ).toFloat();
83 mRenderAsSimpleLines = elemDataProperties.attribute( u"simple-lines"_s, u"0"_s ).toInt();
84
85 const QDomElement elemMaterial = elem.firstChildElement( u"material"_s );
86 const QString materialType = elem.attribute( u"material_type"_s, u"phong"_s );
87 mMaterialSettings.reset( Qgs3D::materialRegistry()->createMaterialSettings( materialType ) );
88 if ( !mMaterialSettings )
89 mMaterialSettings.reset( Qgs3D::materialRegistry()->createMaterialSettings( u"phong"_s ) );
90 mMaterialSettings->readXml( elemMaterial, context );
91}
92
94{
95 return mMaterialSettings.get();
96}
97
99{
100 if ( materialSettings == mMaterialSettings.get() )
101 return;
102
103 mMaterialSettings.reset( materialSettings );
104}
105
106QList<Qgis::GeometryType> QgsLine3DSymbol::compatibleGeometryTypes() const
107{
108 return QList<Qgis::GeometryType>() << Qgis::GeometryType::Line;
109}
110
112{
113 const QgsVectorLayerElevationProperties *props = qgis::down_cast<const QgsVectorLayerElevationProperties *>( const_cast<QgsVectorLayer *>( layer )->elevationProperties() );
114
115 mAltClamping = props->clamping();
116 mAltBinding = props->binding();
117 mExtrusionHeight = props->extrusionEnabled() ? static_cast<float>( props->extrusionHeight() ) : 0.0f;
118 mOffset = static_cast<float>( props->zOffset() );
119}
120
125
126bool QgsLine3DSymbol::exportGeometries( Qgs3DSceneExporter *exporter, Qt3DCore::QEntity *entity, const QString &objectNamePrefix ) const
127{
128 if ( renderAsSimpleLines() )
129 {
130 const QVector<Qgs3DExportObject *> objs = exporter->processLines( entity, objectNamePrefix );
131 exporter->mObjects << objs;
132 return objs.size() != 0;
133 }
134 else
135 {
136 const QList<Qt3DRender::QGeometryRenderer *> renderers = entity->findChildren<Qt3DRender::QGeometryRenderer *>();
137 for ( Qt3DRender::QGeometryRenderer *r : renderers )
138 {
139 Qgs3DExportObject *object = exporter->processGeometryRenderer( r, objectNamePrefix );
140 if ( !object )
141 continue;
142 object->setupMaterial( materialSettings() );
143 exporter->mObjects.push_back( object );
144 }
145 return renderers.size() != 0;
146 }
147}
@ Line
Lines.
Definition qgis.h:378
Manages the data of each object of the scene (positions, normals, texture coordinates ....
void setupMaterial(QgsAbstractMaterialSettings *material)
Sets the material parameters (diffuse color, shininess...) to be exported in the ....
Entity that handles the exporting of 3D scenes.
static Qgis::AltitudeClamping altClampingFromString(const QString &str)
Converts a string to a value from AltitudeClamping enum.
static Qgis::AltitudeBinding altBindingFromString(const QString &str)
Converts a string to a value from AltitudeBinding enum.
static QString altClampingToString(Qgis::AltitudeClamping altClamp)
Converts a value from AltitudeClamping enum to a string.
static QString altBindingToString(Qgis::AltitudeBinding altBind)
Converts a value from AltitudeBinding enum to a string.
static QgsMaterialRegistry * materialRegistry()
Returns the material registry, used for managing 3D materials.
Definition qgs3d.cpp:88
Abstract base class for 3D symbols that are used by VectorLayer3DRenderer objects.
Abstract base class for material settings.
bool renderAsSimpleLines() const
Returns whether the renderer will render data with simple lines (otherwise it uses buffer).
void setDefaultPropertiesFromLayer(const QgsVectorLayer *layer) override
QgsAbstract3DSymbol * clone() const override SIP_FACTORY
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
~QgsLine3DSymbol() override
QList< Qgis::GeometryType > compatibleGeometryTypes() const 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 setMaterialSettings(QgsAbstractMaterialSettings *materialSettings SIP_TRANSFER)
Sets the material settings used for shading of the symbol.
static QgsAbstract3DSymbol * create() SIP_FACTORY
Creates a new QgsLine3DSymbol.
QgsAbstractMaterialSettings * materialSettings() const
Returns material settings used for shading of the symbol.
double zOffset() const
Returns the z offset, which is a fixed offset amount which should be added to z values from the layer...
Basic shading material used for rendering based on the Phong shading model with three color component...
A container for the context for various read/write operations on objects.
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...
Qgis::AltitudeBinding binding() const
Returns the altitude binding method, which determines how altitude is bound to individual vertices in...
bool extrusionEnabled() const
Returns true if extrusion is enabled.
Represents a vector layer which manages a vector based dataset.