QGIS API Documentation 3.99.0-Master (d270888f95f)
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 "qgs3d.h"
19#include "qgs3dexportobject.h"
20#include "qgs3dsceneexporter.h"
21#include "qgs3dutils.h"
22#include "qgsmaterialregistry.h"
23#include "qgsvectorlayer.h"
25
26#include <QString>
27
28using namespace Qt::StringLiterals;
29
31 : mMaterialSettings( std::make_unique<QgsPhongMaterialSettings>() )
32{
33}
34
36
38{
39 auto result = std::make_unique<QgsLine3DSymbol>();
40 result->mAltClamping = mAltClamping;
41 result->mAltBinding = mAltBinding;
42 result->mWidth = mWidth;
43 result->mOffset = mOffset;
44 result->mExtrusionHeight = mExtrusionHeight;
45 result->mRenderAsSimpleLines = mRenderAsSimpleLines;
46 result->mMaterialSettings.reset( mMaterialSettings->clone() );
47 copyBaseSettings( result.get() );
48 return result.release();
49}
50
51void QgsLine3DSymbol::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
52{
53 Q_UNUSED( context )
54
55 QDomDocument doc = elem.ownerDocument();
56
57 QDomElement elemDataProperties = doc.createElement( u"data"_s );
58 elemDataProperties.setAttribute( u"alt-clamping"_s, Qgs3DUtils::altClampingToString( mAltClamping ) );
59 elemDataProperties.setAttribute( u"alt-binding"_s, Qgs3DUtils::altBindingToString( mAltBinding ) );
60 elemDataProperties.setAttribute( u"offset"_s, mOffset );
61 elemDataProperties.setAttribute( u"extrusion-height"_s, mExtrusionHeight );
62 elemDataProperties.setAttribute( u"simple-lines"_s, mRenderAsSimpleLines ? u"1"_s : u"0"_s );
63 elemDataProperties.setAttribute( u"width"_s, mWidth );
64 elem.appendChild( elemDataProperties );
65
66 elem.setAttribute( u"material_type"_s, mMaterialSettings->type() );
67 QDomElement elemMaterial = doc.createElement( u"material"_s );
68 mMaterialSettings->writeXml( elemMaterial, context );
69 elem.appendChild( elemMaterial );
70}
71
72void QgsLine3DSymbol::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
73{
74 Q_UNUSED( context )
75
76 const QDomElement elemDataProperties = elem.firstChildElement( u"data"_s );
77 mAltClamping = Qgs3DUtils::altClampingFromString( elemDataProperties.attribute( u"alt-clamping"_s ) );
78 mAltBinding = Qgs3DUtils::altBindingFromString( elemDataProperties.attribute( u"alt-binding"_s ) );
79 mOffset = elemDataProperties.attribute( u"offset"_s ).toFloat();
80 mExtrusionHeight = elemDataProperties.attribute( u"extrusion-height"_s ).toFloat();
81 mWidth = elemDataProperties.attribute( u"width"_s ).toFloat();
82 mRenderAsSimpleLines = elemDataProperties.attribute( u"simple-lines"_s, u"0"_s ).toInt();
83
84 const QDomElement elemMaterial = elem.firstChildElement( u"material"_s );
85 const QString materialType = elem.attribute( u"material_type"_s, u"phong"_s );
86 mMaterialSettings.reset( Qgs3D::materialRegistry()->createMaterialSettings( materialType ) );
87 if ( !mMaterialSettings )
88 mMaterialSettings.reset( Qgs3D::materialRegistry()->createMaterialSettings( u"phong"_s ) );
89 mMaterialSettings->readXml( elemMaterial, context );
90}
91
93{
94 return mMaterialSettings.get();
95}
96
98{
99 if ( materialSettings == mMaterialSettings.get() )
100 return;
101
102 mMaterialSettings.reset( materialSettings );
103}
104
105QList<Qgis::GeometryType> QgsLine3DSymbol::compatibleGeometryTypes() const
106{
107 return QList<Qgis::GeometryType>() << Qgis::GeometryType::Line;
108}
109
111{
112 const QgsVectorLayerElevationProperties *props = qgis::down_cast<const QgsVectorLayerElevationProperties *>( const_cast<QgsVectorLayer *>( layer )->elevationProperties() );
113
114 mAltClamping = props->clamping();
115 mAltBinding = props->binding();
116 mExtrusionHeight = props->extrusionEnabled() ? static_cast<float>( props->extrusionHeight() ) : 0.0f;
117 mOffset = static_cast<float>( props->zOffset() );
118}
119
124
125bool QgsLine3DSymbol::exportGeometries( Qgs3DSceneExporter *exporter, Qt3DCore::QEntity *entity, const QString &objectNamePrefix ) const
126{
127 if ( renderAsSimpleLines() )
128 {
129 const QVector<Qgs3DExportObject *> objs = exporter->processLines( entity, objectNamePrefix );
130 exporter->mObjects << objs;
131 return objs.size() != 0;
132 }
133 else
134 {
135 const QList<Qt3DRender::QGeometryRenderer *> renderers = entity->findChildren<Qt3DRender::QGeometryRenderer *>();
136 for ( Qt3DRender::QGeometryRenderer *r : renderers )
137 {
138 Qgs3DExportObject *object = exporter->processGeometryRenderer( r, objectNamePrefix );
139 if ( !object )
140 continue;
141 object->setupMaterial( materialSettings() );
142 exporter->mObjects.push_back( object );
143 }
144 return renderers.size() != 0;
145 }
146}
@ Line
Lines.
Definition qgis.h:367
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:89
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.