QGIS API Documentation  3.20.0-Odense (decaadbb31)
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( std::make_unique< QgsPhongMaterialSettings >() )
25 {
26 
27 }
28 
30 
32 {
33  std::unique_ptr< QgsLine3DSymbol > result = std::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 }
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 QString altBindingToString(Qgs3DTypes::AltitudeBinding altBind)
Converts a value from AltitudeBinding enum to a string.
Definition: qgs3dutils.cpp:210
static QString altClampingToString(Qgs3DTypes::AltitudeClamping altClamp)
Converts a value from AltitudeClamping enum to a string.
Definition: qgs3dutils.cpp:187
static Qgs3DTypes::AltitudeClamping altClampingFromString(const QString &str)
Converts a string to a value from AltitudeClamping enum.
Definition: qgs3dutils.cpp:199
static Qgs3DTypes::AltitudeBinding altBindingFromString(const QString &str)
Converts a string to a value from AltitudeBinding enum.
Definition: qgs3dutils.cpp:221
static QgsMaterialRegistry * materialRegistry()
Returns the material registry, used for managing 3D materials.
Definition: qgs3d.cpp:90
QList< QgsWkbTypes::GeometryType > compatibleGeometryTypes() const override
bool renderAsSimpleLines() const
Returns whether the renderer will render data with simple lines (otherwise it uses buffer)
QgsLine3DSymbol()
Constructor for QgsLine3DSymbol.
QgsAbstract3DSymbol * clone() const override SIP_FACTORY
void setMaterial(QgsAbstractMaterialSettings *material SIP_TRANSFER)
Sets the material settings used for shading of the symbol.
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
QgsAbstractMaterialSettings * material() const
Returns material used for shading of the symbol.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
~QgsLine3DSymbol() override
bool exportGeometries(Qgs3DSceneExporter *exporter, Qt3DCore::QEntity *entity, const QString &objectNamePrefix) const override SIP_SKIP
Exports the geometries contained within the hierarchy of entity.
static QgsAbstract3DSymbol * create() SIP_FACTORY
Creates a new QgsLine3DSymbol.
The class is used as a container of context for various read/write operations on other objects.