QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgspointcloudrenderer.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgspointcloudrenderer.h
3  --------------------
4  begin : October 2020
5  copyright : (C) 2020 by Peter Petrik
6  email : zilolv at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSPOINTCLOUDRENDERER_H
19 #define QGSPOINTCLOUDRENDERER_H
20 
21 #include "qgsrendercontext.h"
22 
23 #include "qgis_core.h"
24 #include "qgis_sip.h"
25 #include "qgsvector3d.h"
26 #include "qgspointcloudattribute.h"
27 
28 class QgsPointCloudBlock;
29 class QgsLayerTreeLayer;
31 class QgsPointCloudLayer;
32 
41 class CORE_EXPORT QgsPointCloudRenderContext
42 {
43  public:
44 
57  QgsPointCloudRenderContext( QgsRenderContext &context, const QgsVector3D &scale, const QgsVector3D &offset,
58  double zValueScale, double zValueFixedOffset, QgsFeedback *feedback = nullptr );
59 
62 
64  QgsPointCloudRenderContext &operator=( const QgsPointCloudRenderContext & ) = delete;
65 
69  QgsRenderContext &renderContext() { return mRenderContext; }
70 
75  const QgsRenderContext &renderContext() const { return mRenderContext; } SIP_SKIP
76 
80  QgsVector3D scale() const { return mScale; }
81 
86  void setScale( const QgsVector3D &scale ) { mScale = scale; }
87 
91  QgsVector3D offset() const { return mOffset; }
92 
97  void setOffset( const QgsVector3D &offset ) { mOffset = offset; }
98 
102  long pointsRendered() const;
103 
110  void incrementPointsRendered( long count );
111 
117  QgsPointCloudAttributeCollection attributes() const { return mAttributes; }
118 
124  void setAttributes( const QgsPointCloudAttributeCollection &attributes );
125 
129  int pointRecordSize() const { return mPointRecordSize; }
130 
137  int xOffset() const { return mXOffset; }
138 
145  int yOffset() const { return mYOffset; }
146 
153  int zOffset() const { return mZOffset; }
154 
160  double zValueScale() const { return mZValueScale; }
161 
167  double zValueFixedOffset() const { return mZValueFixedOffset; }
168 
174  QgsFeedback *feedback() const { return mFeedback; }
175 
176 #ifndef SIP_RUN
177 
182  template <typename T>
183  static void getAttribute( const char *data, std::size_t offset, QgsPointCloudAttribute::DataType type, T &value )
184  {
185  switch ( type )
186  {
189  value = *( data + offset );
190  return;
191 
193  value = *reinterpret_cast< const quint32 * >( data + offset );
194  return;
196  value = *reinterpret_cast< const qint32 * >( data + offset );
197  return;
198 
200  value = *reinterpret_cast< const quint64 * >( data + offset );
201  return;
203  value = *reinterpret_cast< const qint64 * >( data + offset );
204  return;
205 
207  value = *reinterpret_cast< const short * >( data + offset );
208  return;
209 
211  value = *reinterpret_cast< const unsigned short * >( data + offset );
212  return;
213 
215  value = *reinterpret_cast< const float * >( data + offset );
216  return;
217 
219  value = *reinterpret_cast< const double * >( data + offset );
220  return;
221  }
222  }
223 #endif
224 
225  private:
226 #ifdef SIP_RUN
228 #endif
229 
230  QgsRenderContext &mRenderContext;
231  QgsVector3D mScale;
232  QgsVector3D mOffset;
233  long mPointsRendered = 0;
235  int mPointRecordSize = 0;
236  int mXOffset = 0;
237  int mYOffset = 0;
238  int mZOffset = 0;
239  double mZValueScale = 1.0;
240  double mZValueFixedOffset = 0;
241 
242  QgsFeedback *mFeedback = nullptr;
243 };
244 
245 #ifndef SIP_RUN
246 
257 {
258  public:
259 
261 
265  virtual QSet< QString > usedAttributes() const = 0;
266 
272  virtual bool prepareBlock( const QgsPointCloudBlock *block ) = 0;
273 
281  virtual QColor pointColor( const QgsPointCloudBlock *block, int i, double z ) = 0;
282 
283 };
284 
285 #endif
286 
287 
296 class CORE_EXPORT QgsPointCloudRenderer
297 {
298 
299 #ifdef SIP_RUN
301 
302  const QString type = sipCpp->type();
303 
304  if ( type == QLatin1String( "rgb" ) )
305  sipType = sipType_QgsPointCloudRgbRenderer;
306  else if ( type == QLatin1String( "ramp" ) )
307  sipType = sipType_QgsPointCloudAttributeByRampRenderer;
308  else if ( type == QLatin1String( "classified" ) )
309  sipType = sipType_QgsPointCloudClassifiedRenderer;
310  else if ( type == QLatin1String( "extent" ) )
311  sipType = sipType_QgsPointCloudExtentRenderer;
312  else
313  sipType = 0;
314  SIP_END
315 #endif
316 
317  public:
318 
322  QgsPointCloudRenderer() = default;
323 
324  virtual ~QgsPointCloudRenderer() = default;
325 
329  virtual QString type() const = 0;
330 
335  virtual QgsPointCloudRenderer *clone() const = 0 SIP_FACTORY;
336 
338  QgsPointCloudRenderer( const QgsPointCloudRenderer &other ) = delete;
339 
341  QgsPointCloudRenderer &operator=( const QgsPointCloudRenderer &other ) = delete;
342 
346  virtual void renderBlock( const QgsPointCloudBlock *block, QgsPointCloudRenderContext &context ) = 0;
347 
357  QVector<QVariantMap> identify( QgsPointCloudLayer *layer, const QgsRenderContext &context, const QgsGeometry &geometry, double toleranceForPointIdentification = 0 ) SIP_SKIP;
358 
364  virtual bool willRenderPoint( const QMap<QString, QVariant> &pointAttributes )
365  {
366  Q_UNUSED( pointAttributes );
367  return true;
368  }
369 
377  static QgsPointCloudRenderer *load( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
378 
383  virtual QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) const = 0;
384 
392  virtual QSet< QString > usedAttributes( const QgsPointCloudRenderContext &context ) const;
393 
400  virtual std::unique_ptr< QgsPreparedPointCloudRendererData > prepare() SIP_SKIP;
401 
411  virtual void startRender( QgsPointCloudRenderContext &context );
412 
423  virtual void stopRender( QgsPointCloudRenderContext &context );
424 
430  virtual bool legendItemChecked( const QString &key );
431 
437  virtual void checkLegendItem( const QString &key, bool state = true );
438 
445  void setPointSize( double size ) { mPointSize = size; }
446 
456  double pointSize() const { return mPointSize; }
457 
465  void setPointSizeUnit( const QgsUnitTypes::RenderUnit units ) { mPointSizeUnit = units; }
466 
473  QgsUnitTypes::RenderUnit pointSizeUnit() const { return mPointSizeUnit; }
474 
481  void setPointSizeMapUnitScale( const QgsMapUnitScale &scale ) { mPointSizeMapUnitScale = scale; }
482 
489  const QgsMapUnitScale &pointSizeMapUnitScale() const { return mPointSizeMapUnitScale; }
490 
497  Qgis::PointCloudDrawOrder drawOrder2d() const;
498 
505  void setDrawOrder2d( Qgis::PointCloudDrawOrder order );
506 
512  Qgis::PointCloudSymbol pointSymbol() const;
513 
519  void setPointSymbol( Qgis::PointCloudSymbol symbol );
520 
531  double maximumScreenError() const;
532 
543  void setMaximumScreenError( double error );
544 
551  QgsUnitTypes::RenderUnit maximumScreenErrorUnit() const;
552 
559  void setMaximumScreenErrorUnit( QgsUnitTypes::RenderUnit unit );
560 
564  virtual QList<QgsLayerTreeModelLegendNode *> createLegendNodes( QgsLayerTreeLayer *nodeLayer ) SIP_FACTORY;
565 
569  virtual QStringList legendRuleKeys() const;
570 
571  protected:
572 
576  static void pointXY( QgsPointCloudRenderContext &context, const char *ptr, int i, double &x, double &y )
577  {
578  // be wary when copying this code!! In the renderer we explicitly request x/y/z as qint32 values, but in other
579  // situations these may be floats or doubles!
580  const qint32 ix = *reinterpret_cast< const qint32 * >( ptr + i * context.pointRecordSize() + context.xOffset() );
581  const qint32 iy = *reinterpret_cast< const qint32 * >( ptr + i * context.pointRecordSize() + context.yOffset() );
582  x = context.offset().x() + context.scale().x() * ix;
583  y = context.offset().y() + context.scale().y() * iy;
584  }
585 
589  static double pointZ( QgsPointCloudRenderContext &context, const char *ptr, int i )
590  {
591  // be wary when copying this code!! In the renderer we explicitly request x/y/z as qint32 values, but in other
592  // situations these may be floats or doubles!
593  const qint32 iz = *reinterpret_cast<const qint32 * >( ptr + i * context.pointRecordSize() + context.zOffset() );
594  return ( context.offset().z() + context.scale().z() * iz ) * context.zValueScale() + context.zValueFixedOffset();
595  }
596 
600  void drawPoint( double x, double y, const QColor &color, QgsPointCloudRenderContext &context ) const
601  {
602  const QPointF originalXY( x, y );
603  context.renderContext().mapToPixel().transformInPlace( x, y );
604  QPainter *painter = context.renderContext().painter();
605  switch ( mPointSymbol )
606  {
608  painter->fillRect( QRectF( x - mPainterPenWidth * 0.5,
609  y - mPainterPenWidth * 0.5,
610  mPainterPenWidth, mPainterPenWidth ), color );
611  break;
612 
614  painter->setBrush( QBrush( color ) );
615  painter->setPen( Qt::NoPen );
616  painter->drawEllipse( QRectF( x - mPainterPenWidth * 0.5,
617  y - mPainterPenWidth * 0.5,
618  mPainterPenWidth, mPainterPenWidth ) );
619  break;
620  };
621  }
622 
626  void copyCommonProperties( QgsPointCloudRenderer *destination ) const;
627 
634  void restoreCommonProperties( const QDomElement &element, const QgsReadWriteContext &context );
635 
642  void saveCommonProperties( QDomElement &element, const QgsReadWriteContext &context ) const;
643 
644  private:
645 #ifdef SIP_RUN
647 #endif
648 
649 #ifdef QGISDEBUG
650  QThread *mThread = nullptr;
652 #endif
653 
654  double mMaximumScreenError = 0.3;
656 
657  double mPointSize = 1;
659  QgsMapUnitScale mPointSizeMapUnitScale;
660 
662  int mPainterPenWidth = 1;
664 };
665 
666 #endif // QGSPOINTCLOUDRENDERER_H
QgsRenderContext::mapToPixel
const QgsMapToPixel & mapToPixel() const
Returns the context's map to pixel transform, which transforms between map coordinates and device coo...
Definition: qgsrendercontext.h:258
QgsUnitTypes::RenderUnit
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:167
QgsPointCloudAttribute::DataType
DataType
Systems of unit measurement.
Definition: qgspointcloudattribute.h:44
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsVector3D::y
double y() const
Returns Y coordinate.
Definition: qgsvector3d.h:64
QgsPointCloudLayer
Represents a map layer supporting display of point clouds.
Definition: qgspointcloudlayer.h:45
QgsVector3D
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
Definition: qgsvector3d.h:31
QgsPreparedPointCloudRendererData
Base class for 2d point cloud renderer prepared data containers.
Definition: qgspointcloudrenderer.h:256
QgsPointCloudRenderContext::setScale
void setScale(const QgsVector3D &scale)
Sets the scale of the layer's int32 coordinates compared to CRS coords.
Definition: qgspointcloudrenderer.h:86
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
qgspointcloudattribute.h
QgsUnitTypes::RenderMillimeters
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:169
QgsPointCloudAttribute::UInt32
@ UInt32
Unsigned int32 4 bytes.
Definition: qgspointcloudattribute.h:51
QgsPointCloudBlock
Base class for storing raw data from point cloud nodes.
Definition: qgspointcloudblock.h:38
QgsPointCloudAttribute::Double
@ Double
Double 8 bytes.
Definition: qgspointcloudattribute.h:55
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsPointCloudRenderContext::getAttribute
static void getAttribute(const char *data, std::size_t offset, QgsPointCloudAttribute::DataType type, T &value)
Retrieves the attribute value from data at the specified offset, where type indicates the original da...
Definition: qgspointcloudrenderer.h:183
QgsPointCloudAttribute::Short
@ Short
Short int 2 bytes.
Definition: qgspointcloudattribute.h:48
QgsPointCloudRenderContext::xOffset
int xOffset() const
Returns the offset for the x value in a point record.
Definition: qgspointcloudrenderer.h:137
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:186
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsPointCloudAttribute::UInt64
@ UInt64
Unsigned int64 8 bytes.
Definition: qgspointcloudattribute.h:53
QgsPointCloudAttribute::Int32
@ Int32
Int32 4 bytes.
Definition: qgspointcloudattribute.h:50
QgsPointCloudRenderer::drawPoint
void drawPoint(double x, double y, const QColor &color, QgsPointCloudRenderContext &context) const
Draws a point using a color at the specified x and y (in map coordinates).
Definition: qgspointcloudrenderer.h:600
QgsPointCloudRenderContext
Encapsulates the render context for a 2D point cloud rendering operation.
Definition: qgspointcloudrenderer.h:41
QgsPointCloudAttribute::UShort
@ UShort
Unsigned short int 2 bytes.
Definition: qgspointcloudattribute.h:49
QgsVector3D::z
double z() const
Returns Z coordinate.
Definition: qgsvector3d.h:66
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
QgsPointCloudRenderContext::zValueFixedOffset
double zValueFixedOffset() const
Returns any constant offset which must be applied to z values taken from the point cloud index.
Definition: qgspointcloudrenderer.h:167
QgsLayerTreeLayer
Layer tree node points to a map layer.
Definition: qgslayertreelayer.h:43
QgsPointCloudAttributeCollection
Collection of point cloud attributes.
Definition: qgspointcloudattribute.h:141
qgis_sip.h
Qgis::PointCloudSymbol::Square
@ Square
Renders points as squares.
QgsPointCloudRenderContext::yOffset
int yOffset() const
Returns the offset for the y value in a point record.
Definition: qgspointcloudrenderer.h:145
Qgis::PointCloudSymbol
PointCloudSymbol
Rendering symbols for point cloud points.
Definition: qgis.h:1888
qgsrendercontext.h
QgsPointCloudRenderContext::offset
QgsVector3D offset() const
Returns the offset of the layer's int32 coordinates compared to CRS coords.
Definition: qgspointcloudrenderer.h:91
QgsPointCloudRenderContext::zOffset
int zOffset() const
Returns the offset for the y value in a point record.
Definition: qgspointcloudrenderer.h:153
QgsPointCloudRenderer::setPointSizeUnit
void setPointSizeUnit(const QgsUnitTypes::RenderUnit units)
Sets the units used for the point size.
Definition: qgspointcloudrenderer.h:465
Qgis::PointCloudDrawOrder
PointCloudDrawOrder
Pointcloud rendering order for 2d views.
Definition: qgis.h:1900
QgsPointCloudAttribute::Char
@ Char
Char 1 byte.
Definition: qgspointcloudattribute.h:46
QgsPointCloudRenderer::setPointSizeMapUnitScale
void setPointSizeMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale used for the point size.
Definition: qgspointcloudrenderer.h:481
QgsMapUnitScale
Struct for storing maximum and minimum scales for measurements in map units.
Definition: qgsmapunitscale.h:36
QgsPointCloudRenderer::pointXY
static void pointXY(QgsPointCloudRenderContext &context, const char *ptr, int i, double &x, double &y)
Retrieves the x and y coordinate for the point at index i.
Definition: qgspointcloudrenderer.h:576
QgsPointCloudRenderContext::attributes
QgsPointCloudAttributeCollection attributes() const
Returns the attributes associated with the rendered block.
Definition: qgspointcloudrenderer.h:117
QgsPointCloudRenderContext::feedback
QgsFeedback * feedback() const
Returns the feedback object used to cancel rendering.
Definition: qgspointcloudrenderer.h:174
Qgis::PointCloudSymbol::Circle
@ Circle
Renders points as circles.
QgsPointCloudRenderer::willRenderPoint
virtual bool willRenderPoint(const QMap< QString, QVariant > &pointAttributes)
Checks whether the point holding pointAttributes attributes will be rendered By default if not overri...
Definition: qgspointcloudrenderer.h:364
QgsPointCloudRenderer::pointSize
double pointSize() const
Returns the point size.
Definition: qgspointcloudrenderer.h:456
QgsPointCloudAttribute::Int64
@ Int64
Int64 8 bytes.
Definition: qgspointcloudattribute.h:52
QgsPointCloudRenderContext::renderContext
const QgsRenderContext & renderContext() const
Returns a reference to the context's render context.
Definition: qgspointcloudrenderer.h:75
QgsPointCloudRenderContext::zValueScale
double zValueScale() const
Returns any constant scaling factor which must be applied to z values taken from the point cloud inde...
Definition: qgspointcloudrenderer.h:160
QgsMapToPixel::transformInPlace
void transformInPlace(double &x, double &y) const
Transforms device coordinates to map coordinates.
Definition: qgsmaptopixel.h:128
qgsvector3d.h
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
Qgis::PointCloudDrawOrder::Default
@ Default
Draw points in the order they are stored.
QgsPointCloudRenderContext::scale
QgsVector3D scale() const
Returns the scale of the layer's int32 coordinates compared to CRS coords.
Definition: qgspointcloudrenderer.h:80
QgsPointCloudRenderContext::pointRecordSize
int pointRecordSize() const
Returns the size of a single point record.
Definition: qgspointcloudrenderer.h:129
QgsPointCloudRenderer
Abstract base class for 2d point cloud renderers.
Definition: qgspointcloudrenderer.h:296
QgsPointCloudRenderContext::setOffset
void setOffset(const QgsVector3D &offset)
Sets the offset of the layer's int32 coordinates compared to CRS coords.
Definition: qgspointcloudrenderer.h:97
QgsPointCloudRenderer::pointSizeUnit
QgsUnitTypes::RenderUnit pointSizeUnit() const
Returns the units used for the point size.
Definition: qgspointcloudrenderer.h:473
QgsRenderContext::painter
QPainter * painter()
Returns the destination QPainter for the render operation.
Definition: qgsrendercontext.h:112
QgsPointCloudAttribute::UChar
@ UChar
Unsigned char 1 byte.
Definition: qgspointcloudattribute.h:47
QgsPointCloudRenderer::pointSizeMapUnitScale
const QgsMapUnitScale & pointSizeMapUnitScale() const
Returns the map unit scale used for the point size.
Definition: qgspointcloudrenderer.h:489
QgsPointCloudRenderContext::renderContext
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
Definition: qgspointcloudrenderer.h:69
QgsPointCloudRenderer::pointZ
static double pointZ(QgsPointCloudRenderContext &context, const char *ptr, int i)
Retrieves the z value for the point at index i.
Definition: qgspointcloudrenderer.h:589
QgsVector3D::x
double x() const
Returns X coordinate.
Definition: qgsvector3d.h:62
SIP_END
#define SIP_END
Definition: qgis_sip.h:203
QgsPointCloudAttribute::Float
@ Float
Float 4 bytes.
Definition: qgspointcloudattribute.h:54
QgsLayerTreeModelLegendNode
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
Definition: qgslayertreemodellegendnode.h:49