QGIS API Documentation 3.41.0-Master (cea29feecf2)
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#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 : mMaterialSettings( std::make_unique<QgsPhongMaterialSettings>() )
27{
28}
29
31
33{
34 std::unique_ptr<QgsLine3DSymbol> result = std::make_unique<QgsLine3DSymbol>();
35 result->mAltClamping = mAltClamping;
36 result->mAltBinding = mAltBinding;
37 result->mWidth = mWidth;
38 result->mOffset = mOffset;
39 result->mExtrusionHeight = mExtrusionHeight;
40 result->mRenderAsSimpleLines = mRenderAsSimpleLines;
41 result->mMaterialSettings.reset( mMaterialSettings->clone() );
42 copyBaseSettings( result.get() );
43 return result.release();
44}
45
46void QgsLine3DSymbol::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
47{
48 Q_UNUSED( context )
49
50 QDomDocument doc = elem.ownerDocument();
51
52 QDomElement elemDataProperties = doc.createElement( QStringLiteral( "data" ) );
53 elemDataProperties.setAttribute( QStringLiteral( "alt-clamping" ), Qgs3DUtils::altClampingToString( mAltClamping ) );
54 elemDataProperties.setAttribute( QStringLiteral( "alt-binding" ), Qgs3DUtils::altBindingToString( mAltBinding ) );
55 elemDataProperties.setAttribute( QStringLiteral( "offset" ), mOffset );
56 elemDataProperties.setAttribute( QStringLiteral( "extrusion-height" ), mExtrusionHeight );
57 elemDataProperties.setAttribute( QStringLiteral( "simple-lines" ), mRenderAsSimpleLines ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
58 elemDataProperties.setAttribute( QStringLiteral( "width" ), mWidth );
59 elem.appendChild( elemDataProperties );
60
61 elem.setAttribute( QStringLiteral( "material_type" ), mMaterialSettings->type() );
62 QDomElement elemMaterial = doc.createElement( QStringLiteral( "material" ) );
63 mMaterialSettings->writeXml( elemMaterial, context );
64 elem.appendChild( elemMaterial );
65}
66
67void QgsLine3DSymbol::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
68{
69 Q_UNUSED( context )
70
71 const QDomElement elemDataProperties = elem.firstChildElement( QStringLiteral( "data" ) );
72 mAltClamping = Qgs3DUtils::altClampingFromString( elemDataProperties.attribute( QStringLiteral( "alt-clamping" ) ) );
73 mAltBinding = Qgs3DUtils::altBindingFromString( elemDataProperties.attribute( QStringLiteral( "alt-binding" ) ) );
74 mOffset = elemDataProperties.attribute( QStringLiteral( "offset" ) ).toFloat();
75 mExtrusionHeight = elemDataProperties.attribute( QStringLiteral( "extrusion-height" ) ).toFloat();
76 mWidth = elemDataProperties.attribute( QStringLiteral( "width" ) ).toFloat();
77 mRenderAsSimpleLines = elemDataProperties.attribute( QStringLiteral( "simple-lines" ), QStringLiteral( "0" ) ).toInt();
78
79 const QDomElement elemMaterial = elem.firstChildElement( QStringLiteral( "material" ) );
80 const QString materialType = elem.attribute( QStringLiteral( "material_type" ), QStringLiteral( "phong" ) );
81 mMaterialSettings.reset( Qgs3D::materialRegistry()->createMaterialSettings( materialType ) );
82 if ( !mMaterialSettings )
83 mMaterialSettings.reset( Qgs3D::materialRegistry()->createMaterialSettings( QStringLiteral( "phong" ) ) );
84 mMaterialSettings->readXml( elemMaterial, context );
85}
86
88{
89 return mMaterialSettings.get();
90}
91
93{
94 if ( materialSettings == mMaterialSettings.get() )
95 return;
96
97 mMaterialSettings.reset( materialSettings );
98}
99
100QList<Qgis::GeometryType> QgsLine3DSymbol::compatibleGeometryTypes() const
101{
102 return QList<Qgis::GeometryType>() << Qgis::GeometryType::Line;
103}
104
106{
107 const QgsVectorLayerElevationProperties *props = qgis::down_cast<const QgsVectorLayerElevationProperties *>( const_cast<QgsVectorLayer *>( layer )->elevationProperties() );
108
109 mAltClamping = props->clamping();
110 mAltBinding = props->binding();
111 mExtrusionHeight = props->extrusionEnabled() ? static_cast<float>( props->extrusionHeight() ) : 0.0f;
112 mOffset = static_cast<float>( props->zOffset() );
113}
114
119
120bool QgsLine3DSymbol::exportGeometries( Qgs3DSceneExporter *exporter, Qt3DCore::QEntity *entity, const QString &objectNamePrefix ) const
121{
122 if ( renderAsSimpleLines() )
123 {
124 const QVector<Qgs3DExportObject *> objs = exporter->processLines( entity, objectNamePrefix );
125 exporter->mObjects << objs;
126 return objs.size() != 0;
127 }
128 else
129 {
130 const QList<Qt3DRender::QGeometryRenderer *> renderers = entity->findChildren<Qt3DRender::QGeometryRenderer *>();
131 for ( Qt3DRender::QGeometryRenderer *r : renderers )
132 {
133 Qgs3DExportObject *object = exporter->processGeometryRenderer( r, objectNamePrefix );
134 if ( !object )
135 continue;
136 object->setupMaterial( materialSettings() );
137 exporter->mObjects.push_back( object );
138 }
139 return renderers.size() != 0;
140 }
141}
Manages the data of each object of the scene (positions, normals, texture coordinates ....
void setupMaterial(QgsAbstractMaterialSettings *material)
Sets the material parameters (diffuse color, shininess...) from phong material.
Entity that handles the exporting of 3D scene.
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
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...
The class is used as a container of context for various read/write operations on other 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 data sets.