QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
24  : mMaterial( qgis::make_unique< QgsPhongMaterialSettings >() )
25 {
26 
27 }
28 
30 
32 {
33  std::unique_ptr< QgsLine3DSymbol > result = qgis::make_unique< QgsLine3DSymbol >();
34  result->mAltClamping = mAltClamping;
35  result->mAltBinding = mAltBinding;
36  result->mWidth = mWidth;
37  result->mHeight = mHeight;
38  result->mExtrusionHeight = mExtrusionHeight;
39  result->mRenderAsSimpleLines = mRenderAsSimpleLines;
40  result->mMaterial.reset( mMaterial->clone() );
41  copyBaseSettings( result.get() );
42  return result.release();
43 }
44 
45 void QgsLine3DSymbol::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
46 {
47  Q_UNUSED( context )
48 
49  QDomDocument doc = elem.ownerDocument();
50 
51  QDomElement elemDataProperties = doc.createElement( QStringLiteral( "data" ) );
52  elemDataProperties.setAttribute( QStringLiteral( "alt-clamping" ), Qgs3DUtils::altClampingToString( mAltClamping ) );
53  elemDataProperties.setAttribute( QStringLiteral( "alt-binding" ), Qgs3DUtils::altBindingToString( mAltBinding ) );
54  elemDataProperties.setAttribute( QStringLiteral( "height" ), mHeight );
55  elemDataProperties.setAttribute( QStringLiteral( "extrusion-height" ), mExtrusionHeight );
56  elemDataProperties.setAttribute( QStringLiteral( "simple-lines" ), mRenderAsSimpleLines ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
57  elemDataProperties.setAttribute( QStringLiteral( "width" ), mWidth );
58  elem.appendChild( elemDataProperties );
59 
60  elem.setAttribute( QStringLiteral( "material_type" ), mMaterial->type() );
61  QDomElement elemMaterial = doc.createElement( QStringLiteral( "material" ) );
62  mMaterial->writeXml( elemMaterial, context );
63  elem.appendChild( elemMaterial );
64 }
65 
66 void QgsLine3DSymbol::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
67 {
68  Q_UNUSED( context )
69 
70  QDomElement elemDataProperties = elem.firstChildElement( QStringLiteral( "data" ) );
71  mAltClamping = Qgs3DUtils::altClampingFromString( elemDataProperties.attribute( QStringLiteral( "alt-clamping" ) ) );
72  mAltBinding = Qgs3DUtils::altBindingFromString( elemDataProperties.attribute( QStringLiteral( "alt-binding" ) ) );
73  mHeight = elemDataProperties.attribute( QStringLiteral( "height" ) ).toFloat();
74  mExtrusionHeight = elemDataProperties.attribute( QStringLiteral( "extrusion-height" ) ).toFloat();
75  mWidth = elemDataProperties.attribute( QStringLiteral( "width" ) ).toFloat();
76  mRenderAsSimpleLines = elemDataProperties.attribute( QStringLiteral( "simple-lines" ), QStringLiteral( "0" ) ).toInt();
77 
78  const QDomElement elemMaterial = elem.firstChildElement( QStringLiteral( "material" ) );
79  const QString materialType = elem.attribute( QStringLiteral( "material_type" ), QStringLiteral( "phong" ) );
80  mMaterial.reset( Qgs3D::materialRegistry()->createMaterialSettings( materialType ) );
81  if ( !mMaterial )
82  mMaterial.reset( Qgs3D::materialRegistry()->createMaterialSettings( QStringLiteral( "phong" ) ) );
83  mMaterial->readXml( elemMaterial, context );
84 }
85 
87 {
88  return mMaterial.get();
89 }
90 
92 {
93  if ( material == mMaterial.get() )
94  return;
95 
96  mMaterial.reset( material );
97 }
98 
99 QList<QgsWkbTypes::GeometryType> QgsLine3DSymbol::compatibleGeometryTypes() const
100 {
101  return QList< QgsWkbTypes::GeometryType >() << QgsWkbTypes::LineGeometry;
102 }
103 
105 {
106  return new QgsLine3DSymbol();
107 }
108 
109 bool QgsLine3DSymbol::exportGeometries( Qgs3DSceneExporter *exporter, Qt3DCore::QEntity *entity, const QString &objectNamePrefix ) const
110 {
111  if ( renderAsSimpleLines() )
112  {
113  QVector<Qgs3DExportObject *> objs = exporter->processLines( entity, objectNamePrefix );
114  exporter->mObjects << objs;
115  return objs.size() != 0;
116  }
117  else
118  {
119  QList<Qt3DRender::QGeometryRenderer *> renderers = entity->findChildren<Qt3DRender::QGeometryRenderer *>();
120  for ( Qt3DRender::QGeometryRenderer *r : renderers )
121  {
122  Qgs3DExportObject *object = exporter->processGeometryRenderer( r, objectNamePrefix );
123  if ( object == nullptr ) continue;
124  object->setupMaterial( material() );
125  exporter->mObjects.push_back( object );
126  }
127  return renderers.size() != 0;
128  }
129 }
qgs3dexportobject.h
QgsLine3DSymbol::compatibleGeometryTypes
QList< QgsWkbTypes::GeometryType > compatibleGeometryTypes() const override
Definition: qgsline3dsymbol.cpp:99
Qgs3DExportObject::setupMaterial
void setupMaterial(QgsAbstractMaterialSettings *material)
Sets the material parameters (diffuse color, shininess...) from phong material.
Definition: qgs3dexportobject.cpp:78
qgs3d.h
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
QgsLine3DSymbol::~QgsLine3DSymbol
~QgsLine3DSymbol() override
qgsline3dsymbol.h
Qgs3D::materialRegistry
static QgsMaterialRegistry * materialRegistry()
Returns the material registry, used for managing 3D materials.
Definition: qgs3d.cpp:85
qgsmaterialregistry.h
qgs3dsceneexporter.h
Qgs3DExportObject
The Qgs3DExportObject class Manages the data of each object of the scene (positions,...
Definition: qgs3dexportobject.h:42
Qgs3DUtils::altBindingFromString
static Qgs3DTypes::AltitudeBinding altBindingFromString(const QString &str)
Converts a string to a value from AltitudeBinding enum.
Definition: qgs3dutils.cpp:218
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:109
QgsLine3DSymbol::QgsLine3DSymbol
QgsLine3DSymbol()
Constructor for QgsLine3DSymbol.
Definition: qgsline3dsymbol.cpp:23
QgsLine3DSymbol::renderAsSimpleLines
bool renderAsSimpleLines() const
Returns whether the renderer will render data with simple lines (otherwise it uses buffer)
Definition: qgsline3dsymbol.h:82
QgsPhongMaterialSettings
3 Basic shading material used for rendering based on the Phong shading model with three color compone...
Definition: qgsphongmaterialsettings.h:37
QgsAbstract3DSymbol
3 Abstract base class for 3D symbols that are used by VectorLayer3DRenderer objects.
Definition: qgsabstract3dsymbol.h:46
Qgs3DSceneExporter
The Qgs3DSceneExporter class Entity that handles the exporting of 3D scene.
Definition: qgs3dsceneexporter.h:57
QgsLine3DSymbol::readXml
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Definition: qgsline3dsymbol.cpp:66
QgsLine3DSymbol::clone
QgsAbstract3DSymbol * clone() const override SIP_FACTORY
Definition: qgsline3dsymbol.cpp:31
QgsAbstractMaterialSettings
3 Abstract base class for material settings.
Definition: qgsabstractmaterialsettings.h:105
qgs3dutils.h
Qgs3DUtils::altBindingToString
static QString altBindingToString(Qgs3DTypes::AltitudeBinding altBind)
Converts a value from AltitudeBinding enum to a string.
Definition: qgs3dutils.cpp:207
QgsLine3DSymbol::writeXml
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Definition: qgsline3dsymbol.cpp:45
Qgs3DUtils::altClampingToString
static QString altClampingToString(Qgs3DTypes::AltitudeClamping altClamp)
Converts a value from AltitudeClamping enum to a string.
Definition: qgs3dutils.cpp:184
QgsLine3DSymbol::material
QgsAbstractMaterialSettings * material() const
Returns material used for shading of the symbol.
Definition: qgsline3dsymbol.cpp:86
Qgs3DUtils::altClampingFromString
static Qgs3DTypes::AltitudeClamping altClampingFromString(const QString &str)
Converts a string to a value from AltitudeClamping enum.
Definition: qgs3dutils.cpp:196
QgsWkbTypes::LineGeometry
@ LineGeometry
Definition: qgswkbtypes.h:143
QgsLine3DSymbol::create
static QgsAbstract3DSymbol * create() SIP_FACTORY
Creates a new QgsLine3DSymbol.
Definition: qgsline3dsymbol.cpp:104
QgsLine3DSymbol::setMaterial
void setMaterial(QgsAbstractMaterialSettings *material SIP_TRANSFER)
Sets the material settings used for shading of the symbol.
Definition: qgsline3dsymbol.cpp:91