QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgspointcloudlayer3drenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspointcloudlayer3drenderer.cpp
3  --------------------------------------
4  Date : October 2020
5  Copyright : (C) 2020 by Peter Petrik
6  Email : zilolv 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 
17 
18 #include "qgs3dutils.h"
19 #include "qgschunkedentity_p.h"
21 
22 #include "qgspointcloudindex.h"
23 #include "qgspointcloudlayer.h"
24 #include "qgsxmlutils.h"
25 #include "qgsapplication.h"
26 #include "qgs3dsymbolregistry.h"
27 #include "qgspointcloud3dsymbol.h"
29 
30 #include "qgis.h"
31 
32 QgsPointCloud3DRenderContext::QgsPointCloud3DRenderContext( const Qgs3DMapSettings &map, const QgsCoordinateTransform &coordinateTransform, std::unique_ptr<QgsPointCloud3DSymbol> symbol, double zValueScale, double zValueFixedOffset )
33  : Qgs3DRenderContext( map )
34  , mSymbol( std::move( symbol ) )
35  , mZValueScale( zValueScale )
36  , mZValueFixedOffset( zValueFixedOffset )
37  , mCoordinateTransform( coordinateTransform )
38 {
39  auto callback = []()->bool
40  {
41  return false;
42  };
43  mIsCanceledCallback = callback;
44 }
45 
47 {
48  mAttributes = attributes;
49 }
50 
52 {
53  mSymbol.reset( symbol );
54 }
55 
57 {
58  mFilteredOutCategories = categories;
59 }
60 
62 {
63  QSet<int> filteredOut;
64  for ( QgsPointCloudCategory category : mFilteredOutCategories )
65  filteredOut.insert( category.value() );
66  return filteredOut;
67 }
68 
70 {
71  mCoordinateTransform = coordinateTransform;
72 }
73 
75  : Qgs3DRendererAbstractMetadata( QStringLiteral( "pointcloud" ) )
76 {
77 }
78 
80 {
82  r->readXml( elem, context );
83  return r;
84 }
85 
86 
87 // ---------
88 
89 
91 {
92 }
93 
95 {
96  mLayerRef = QgsMapLayerRef( layer );
97 }
98 
100 {
101  return qobject_cast<QgsPointCloudLayer *>( mLayerRef.layer );
102 }
103 
105 {
106  return QStringLiteral( "pointcloud" );
107 }
108 
110 {
112  if ( mSymbol )
113  {
114  QgsAbstract3DSymbol *symbolClone = mSymbol->clone();
115  r->setSymbol( dynamic_cast<QgsPointCloud3DSymbol *>( symbolClone ) );
116  }
117  r->setMaximumScreenError( mMaximumScreenError );
118  r->setShowBoundingBoxes( mShowBoundingBoxes );
119  return r;
120 }
121 
122 Qt3DCore::QEntity *QgsPointCloudLayer3DRenderer::createEntity( const Qgs3DMapSettings &map ) const
123 {
124  QgsPointCloudLayer *pcl = layer();
125  if ( !pcl || !pcl->dataProvider() || !pcl->dataProvider()->index() )
126  return nullptr;
127  if ( !mSymbol )
128  return nullptr;
129 
130  QgsCoordinateTransform coordinateTransform( pcl->crs(), map.crs(), map.transformContext() );
131 
132  return new QgsPointCloudLayerChunkedEntity( pcl->dataProvider()->index(), map, coordinateTransform, dynamic_cast<QgsPointCloud3DSymbol *>( mSymbol->clone() ), maximumScreenError(), showBoundingBoxes(),
133  static_cast< const QgsPointCloudLayerElevationProperties * >( pcl->elevationProperties() )->zScale(),
134  static_cast< const QgsPointCloudLayerElevationProperties * >( pcl->elevationProperties() )->zOffset(), mPointBudget );
135 }
136 
138 {
139  mSymbol.reset( symbol );
140 }
141 
142 void QgsPointCloudLayer3DRenderer::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
143 {
144  Q_UNUSED( context )
145 
146  QDomDocument doc = elem.ownerDocument();
147 
148  elem.setAttribute( QStringLiteral( "layer" ), mLayerRef.layerId );
149  elem.setAttribute( QStringLiteral( "max-screen-error" ), maximumScreenError() );
150  elem.setAttribute( QStringLiteral( "show-bounding-boxes" ), showBoundingBoxes() ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
151  elem.setAttribute( QStringLiteral( "point-budget" ), mPointBudget );
152 
153  QDomElement elemSymbol = doc.createElement( QStringLiteral( "symbol" ) );
154  if ( mSymbol )
155  {
156  elemSymbol.setAttribute( QStringLiteral( "type" ), mSymbol->symbolType() );
157  mSymbol->writeXml( elemSymbol, context );
158  }
159  elem.appendChild( elemSymbol );
160 }
161 
162 void QgsPointCloudLayer3DRenderer::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
163 {
164  mLayerRef = QgsMapLayerRef( elem.attribute( QStringLiteral( "layer" ) ) );
165 
166  QDomElement elemSymbol = elem.firstChildElement( QStringLiteral( "symbol" ) );
167 
168  const QString symbolType = elemSymbol.attribute( QStringLiteral( "type" ) );
169  mShowBoundingBoxes = elem.attribute( QStringLiteral( "show-bounding-boxes" ), QStringLiteral( "0" ) ).toInt();
170  mMaximumScreenError = elem.attribute( QStringLiteral( "max-screen-error" ), QStringLiteral( "1.0" ) ).toDouble();
171  mPointBudget = elem.attribute( QStringLiteral( "point-budget" ), QStringLiteral( "1000000" ) ).toInt();
172 
173  if ( symbolType == QLatin1String( "single-color" ) )
174  mSymbol.reset( new QgsSingleColorPointCloud3DSymbol );
175  else if ( symbolType == QLatin1String( "color-ramp" ) )
176  mSymbol.reset( new QgsColorRampPointCloud3DSymbol );
177  else if ( symbolType == QLatin1String( "rgb" ) )
178  mSymbol.reset( new QgsRgbPointCloud3DSymbol );
179  else if ( symbolType == QLatin1String( "classification" ) )
180  mSymbol.reset( new QgsClassificationPointCloud3DSymbol );
181  else
182  mSymbol.reset();
183 
184  if ( mSymbol )
185  mSymbol->readXml( elemSymbol, context );
186 }
187 
189 {
190  mLayerRef.setLayer( project.mapLayer( mLayerRef.layerId ) );
191 }
192 
194 {
195  return mMaximumScreenError;
196 }
197 
199 {
200  mMaximumScreenError = error;
201 }
202 
204 {
205  return mShowBoundingBoxes;
206 }
207 
209 {
210  mShowBoundingBoxes = showBoundingBoxes;
211 }
212 
214 {
215  mPointBudget = budget;
216 }
217 
218 
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used in the 3D scene.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
Base metadata class for 3D renderers.
Base class for all renderers that may to participate in 3D view.
Class for doing transforms between two map coordinate systems.
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:91
QSet< int > getFilteredOutValues() const
Returns a set containing the filtered out values.
void setFilteredOutCategories(const QgsPointCloudCategoryList &categories)
Sets the list of categories of the classification that won't be rendered.
QgsPointCloudAttributeCollection attributes() const
Returns the attributes associated with the rendered block.
void setSymbol(QgsPointCloud3DSymbol *symbol)
Sets the symbol used for rendering the point cloud Takes ownership over the passed symbol.
void setCoordinateTransform(const QgsCoordinateTransform &coordinateTransform)
Sets the coordinate transform used to transform points from layer CRS to the map CRS.
QgsCoordinateTransform coordinateTransform() const
Returns the coordinate transform used to transform points from layer CRS to the map CRS.
QgsPointCloud3DSymbol * symbol() const
Returns the symbol used for rendering the point cloud.
QgsPointCloud3DRenderContext(const Qgs3DMapSettings &map, const QgsCoordinateTransform &coordinateTransform, std::unique_ptr< QgsPointCloud3DSymbol > symbol, double zValueScale, double zValueFixedOffset)
Constructor for QgsPointCloud3DRenderContext.
void setAttributes(const QgsPointCloudAttributeCollection &attributes)
Sets the attributes associated with the rendered block.
Collection of point cloud attributes.
Represents an individual category (class) from a QgsPointCloudClassifiedRenderer.
virtual QgsPointCloudIndex * index() const
Returns the point cloud index associated with the provider.
QgsAbstract3DRenderer * createRenderer(QDomElement &elem, const QgsReadWriteContext &context) override
Creates an instance of a 3D renderer based on a DOM element with renderer configuration.
3D renderer that renders all points from a point cloud layer
void resolveReferences(const QgsProject &project) override
Resolves references to other objects - second phase of loading - after readXml()
void setLayer(QgsPointCloudLayer *layer)
Sets point cloud layer associated with the renderer.
double maximumScreenError() const
Returns the maximum screen error allowed when rendering the point cloud.
void setPointRenderingBudget(int budget)
Sets the maximum number of points to be rendered in the scene.
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Writes renderer's properties to given XML element.
QString type() const override
Returns unique identifier of the renderer class (used to identify subclass)
const QgsPointCloud3DSymbol * symbol() const
Returns 3D symbol associated with the renderer.
Qt3DCore::QEntity * createEntity(const Qgs3DMapSettings &map) const override
Returns a 3D entity that will be used to show renderer's data in 3D scene.
bool showBoundingBoxes() const
Returns whether bounding boxes will be visible when rendering the point cloud.
QgsPointCloudLayer3DRenderer()
Takes ownership of the symbol object.
QgsPointCloudLayer3DRenderer * clone() const override
Returns a cloned instance.
QgsPointCloudLayer * layer() const
Returns point cloud layer associated with the renderer.
void setSymbol(QgsPointCloud3DSymbol *symbol)
Sets the 3D symbol associated with the renderer.
void setMaximumScreenError(double error)
Sets the maximum screen error allowed when rendering the point cloud.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads renderer's properties from given XML element.
void setShowBoundingBoxes(bool showBoundingBoxes)
Sets whether bounding boxes will be visible when rendering the point cloud.
Point cloud layer specific subclass of QgsMapLayerElevationProperties.
double zOffset() const
Returns the z offset, which is a fixed offset amount which should be added to z values from the layer...
double zScale() const
Returns the z scale, which is a scaling factor which should be applied to z values from the layer.
Represents a map layer supporting display of point clouds.
QgsMapLayerElevationProperties * elevationProperties() override
Returns the layer's elevation properties.
QgsPointCloudDataProvider * dataProvider() override
Returns the layer's data provider, it may be nullptr.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:99
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
The class is used as a container of context for various read/write operations on other objects.
_LayerRef< QgsMapLayer > QgsMapLayerRef
QList< QgsPointCloudCategory > QgsPointCloudCategoryList
QPointer< TYPE > layer
Weak pointer to map layer.
void setLayer(TYPE *l)
Sets the reference to point to a specified layer.
QString layerId
Original layer ID.