QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 );
59 
62 
65 
69  QgsRenderContext &renderContext() { return mRenderContext; }
70 
75  const QgsRenderContext &renderContext() const { return mRenderContext; } SIP_SKIP
76 
80  QgsVector3D scale() const { return mScale; }
81 
85  QgsVector3D offset() const { return mOffset; }
86 
90  long pointsRendered() const;
91 
98  void incrementPointsRendered( long count );
99 
105  QgsPointCloudAttributeCollection attributes() const { return mAttributes; }
106 
112  void setAttributes( const QgsPointCloudAttributeCollection &attributes );
113 
117  int pointRecordSize() const { return mPointRecordSize; }
118 
125  int xOffset() const { return mXOffset; }
126 
133  int yOffset() const { return mYOffset; }
134 
141  int zOffset() const { return mZOffset; }
142 
148  double zValueScale() const { return mZValueScale; }
149 
155  double zValueFixedOffset() const { return mZValueFixedOffset; }
156 
157 #ifndef SIP_RUN
158 
163  template <typename T>
164  void getAttribute( const char *data, std::size_t offset, QgsPointCloudAttribute::DataType type, T &value ) const
165  {
166  switch ( type )
167  {
169  value = *( data + offset );
170  return;
171 
173  value = *reinterpret_cast< const qint32 * >( data + offset );
174  return;
175 
177  value = *reinterpret_cast< const short * >( data + offset );
178  return;
179 
181  value = *reinterpret_cast< const unsigned short * >( data + offset );
182  return;
183 
185  value = *reinterpret_cast< const float * >( data + offset );
186  return;
187 
189  value = *reinterpret_cast< const double * >( data + offset );
190  return;
191  }
192  }
193 #endif
194 
195  private:
196 #ifdef SIP_RUN
198 #endif
199 
200  QgsRenderContext &mRenderContext;
201  QgsVector3D mScale;
202  QgsVector3D mOffset;
203  long mPointsRendered = 0;
205  int mPointRecordSize = 0;
206  int mXOffset = 0;
207  int mYOffset = 0;
208  int mZOffset = 0;
209  double mZValueScale = 1.0;
210  double mZValueFixedOffset = 0;
211 };
212 
213 
222 class CORE_EXPORT QgsPointCloudRenderer
223 {
224 
225 #ifdef SIP_RUN
227 
228  const QString type = sipCpp->type();
229 
230  if ( type == QLatin1String( "rgb" ) )
231  sipType = sipType_QgsPointCloudRgbRenderer;
232  else if ( type == QLatin1String( "ramp" ) )
233  sipType = sipType_QgsPointCloudAttributeByRampRenderer;
234  else if ( type == QLatin1String( "classified" ) )
235  sipType = sipType_QgsPointCloudClassifiedRenderer;
236  else if ( type == QLatin1String( "extent" ) )
237  sipType = sipType_QgsPointCloudExtentRenderer;
238  else
239  sipType = 0;
240  SIP_END
241 #endif
242 
243  public:
244 
249  {
252  };
253 
258 
259  virtual ~QgsPointCloudRenderer() = default;
260 
264  virtual QString type() const = 0;
265 
271 
274 
277 
281  virtual void renderBlock( const QgsPointCloudBlock *block, QgsPointCloudRenderContext &context ) = 0;
282 
292  QVector<QVariantMap> identify( QgsPointCloudLayer *layer, const QgsRenderContext &context, const QgsGeometry &geometry, double toleranceForPointIdentification = 0 ) SIP_SKIP;
293 
299  virtual bool willRenderPoint( const QMap<QString, QVariant> &pointAttributes )
300  {
301  Q_UNUSED( pointAttributes );
302  return true;
303  }
304 
312  static QgsPointCloudRenderer *load( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
313 
318  virtual QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) const = 0;
319 
327  virtual QSet< QString > usedAttributes( const QgsPointCloudRenderContext &context ) const;
328 
338  virtual void startRender( QgsPointCloudRenderContext &context );
339 
350  virtual void stopRender( QgsPointCloudRenderContext &context );
351 
357  virtual bool legendItemChecked( const QString &key );
358 
364  virtual void checkLegendItem( const QString &key, bool state = true );
365 
372  void setPointSize( double size ) { mPointSize = size; }
373 
383  double pointSize() const { return mPointSize; }
384 
392  void setPointSizeUnit( const QgsUnitTypes::RenderUnit units ) { mPointSizeUnit = units; }
393 
400  QgsUnitTypes::RenderUnit pointSizeUnit() const { return mPointSizeUnit; }
401 
408  void setPointSizeMapUnitScale( const QgsMapUnitScale &scale ) { mPointSizeMapUnitScale = scale; }
409 
416  const QgsMapUnitScale &pointSizeMapUnitScale() const { return mPointSizeMapUnitScale; }
417 
423  PointSymbol pointSymbol() const;
424 
430  void setPointSymbol( PointSymbol symbol );
431 
442  double maximumScreenError() const;
443 
454  void setMaximumScreenError( double error );
455 
462  QgsUnitTypes::RenderUnit maximumScreenErrorUnit() const;
463 
470  void setMaximumScreenErrorUnit( QgsUnitTypes::RenderUnit unit );
471 
475  virtual QList<QgsLayerTreeModelLegendNode *> createLegendNodes( QgsLayerTreeLayer *nodeLayer ) SIP_FACTORY;
476 
480  virtual QStringList legendRuleKeys() const;
481 
482  protected:
483 
487  static void pointXY( QgsPointCloudRenderContext &context, const char *ptr, int i, double &x, double &y )
488  {
489  // be wary when copying this code!! In the renderer we explicitly request x/y/z as qint32 values, but in other
490  // situations these may be floats or doubles!
491  const qint32 ix = *reinterpret_cast< const qint32 * >( ptr + i * context.pointRecordSize() + context.xOffset() );
492  const qint32 iy = *reinterpret_cast< const qint32 * >( ptr + i * context.pointRecordSize() + context.yOffset() );
493  x = context.offset().x() + context.scale().x() * ix;
494  y = context.offset().y() + context.scale().y() * iy;
495  }
496 
500  static double pointZ( QgsPointCloudRenderContext &context, const char *ptr, int i )
501  {
502  // be wary when copying this code!! In the renderer we explicitly request x/y/z as qint32 values, but in other
503  // situations these may be floats or doubles!
504  const qint32 iz = *reinterpret_cast<const qint32 * >( ptr + i * context.pointRecordSize() + context.zOffset() );
505  return ( context.offset().z() + context.scale().z() * iz ) * context.zValueScale() + context.zValueFixedOffset();
506  }
507 
511  void drawPoint( double x, double y, const QColor &color, QgsPointCloudRenderContext &context ) const
512  {
513  QPointF originalXY( x, y );
514  context.renderContext().mapToPixel().transformInPlace( x, y );
515  QPainter *painter = context.renderContext().painter();
516  switch ( mPointSymbol )
517  {
518  case Square:
519  painter->fillRect( QRectF( x - mPainterPenWidth * 0.5,
520  y - mPainterPenWidth * 0.5,
521  mPainterPenWidth, mPainterPenWidth ), color );
522  break;
523 
524  case Circle:
525  painter->setBrush( QBrush( color ) );
526  painter->setPen( Qt::NoPen );
527  painter->drawEllipse( QRectF( x - mPainterPenWidth * 0.5,
528  y - mPainterPenWidth * 0.5,
529  mPainterPenWidth, mPainterPenWidth ) );
530  break;
531  };
532  }
533 
537  void copyCommonProperties( QgsPointCloudRenderer *destination ) const;
538 
545  void restoreCommonProperties( const QDomElement &element, const QgsReadWriteContext &context );
546 
553  void saveCommonProperties( QDomElement &element, const QgsReadWriteContext &context ) const;
554 
555  private:
556 #ifdef SIP_RUN
558 #endif
559 
560 #ifdef QGISDEBUG
562  QThread *mThread = nullptr;
563 #endif
564 
565  double mMaximumScreenError = 0.3;
567 
568  double mPointSize = 1;
570  QgsMapUnitScale mPointSizeMapUnitScale;
571 
572  PointSymbol mPointSymbol = Square;
573  int mPainterPenWidth = 1;
574 };
575 
576 #endif // QGSPOINTCLOUDRENDERER_H
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
Layer tree node points to a map layer.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
void transformInPlace(double &x, double &y) const
Transforms device coordinates to map coordinates.
Struct for storing maximum and minimum scales for measurements in map units.
Collection of point cloud attributes.
DataType
Systems of unit measurement.
@ UShort
Unsigned short int 2 bytes.
Base class for storing raw data from point cloud nodes.
Represents a map layer supporting display of point clouds.
Encapsulates the render context for a 2D point cloud rendering operation.
void getAttribute(const char *data, std::size_t offset, QgsPointCloudAttribute::DataType type, T &value) const
Retrieves the attribute value from data at the specified offset, where type indicates the original da...
const QgsRenderContext & renderContext() const
Returns a reference to the context's render context.
int yOffset() const
Returns the offset for the y value in a point record.
double zValueFixedOffset() const
Returns any constant offset which must be applied to z values taken from the point cloud index.
QgsVector3D offset() const
Returns the offset of the layer's int32 coordinates compared to CRS coords.
int pointRecordSize() const
Returns the size of a single point record.
int xOffset() const
Returns the offset for the x value in a point record.
QgsPointCloudRenderContext(const QgsPointCloudRenderContext &rh)=delete
QgsPointCloudRenderContext cannot be copied.
double zValueScale() const
Returns any constant scaling factor which must be applied to z values taken from the point cloud inde...
QgsVector3D scale() const
Returns the scale of the layer's int32 coordinates compared to CRS coords.
QgsPointCloudAttributeCollection attributes() const
Returns the attributes associated with the rendered block.
int zOffset() const
Returns the offset for the y value in a point record.
QgsPointCloudRenderContext & operator=(const QgsPointCloudRenderContext &)=delete
QgsPointCloudRenderContext cannot be copied.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
Abstract base class for 2d point cloud renderers.
virtual QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context) const =0
Saves the renderer configuration to an XML element.
const QgsMapUnitScale & pointSizeMapUnitScale() const
Returns the map unit scale used for the point size.
virtual void renderBlock(const QgsPointCloudBlock *block, QgsPointCloudRenderContext &context)=0
Renders a block of point cloud data using the specified render context.
QgsPointCloudRenderer & operator=(const QgsPointCloudRenderer &other)=delete
QgsPointCloudRenderer cannot be copied – use clone() instead.
void setPointSizeUnit(const QgsUnitTypes::RenderUnit units)
Sets the units used for the point size.
void setPointSizeMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale used for the point size.
QgsPointCloudRenderer()=default
Constructor for QgsPointCloudRenderer.
virtual QgsPointCloudRenderer * clone() const =0
Create a deep copy of this renderer.
void setPointSize(double size)
Sets the point size.
PointSymbol
Rendering symbols for points.
@ Square
Renders points as squares.
@ Circle
Renders points as circles.
virtual QString type() const =0
Returns the identifier of the renderer type.
QgsPointCloudRenderer(const QgsPointCloudRenderer &other)=delete
QgsPointCloudRenderer cannot be copied – use clone() instead.
QgsUnitTypes::RenderUnit pointSizeUnit() const
Returns the units used for the point size.
virtual ~QgsPointCloudRenderer()=default
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).
double pointSize() const
Returns the point size.
static double pointZ(QgsPointCloudRenderContext &context, const char *ptr, int i)
Retrieves the z value for the point at index i.
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.
The class is used as a container of context for various read/write operations on other objects.
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
const QgsMapToPixel & mapToPixel() const
Returns the context's map to pixel transform, which transforms between map coordinates and device coo...
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:167
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:168
double y() const
Returns Y coordinate.
Definition: qgsvector3d.h:51
double z() const
Returns Z coordinate.
Definition: qgsvector3d.h:53
double x() const
Returns X coordinate.
Definition: qgsvector3d.h:49
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_END
Definition: qgis_sip.h:194