QGIS API Documentation 3.37.0-Master (08041ddf763)
Loading...
Searching...
No Matches
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"
27
32class QgsElevationMap;
33
43{
44 public:
45
58 QgsPointCloudRenderContext( QgsRenderContext &context, const QgsVector3D &scale, const QgsVector3D &offset,
59 double zValueScale, double zValueFixedOffset, QgsFeedback *feedback = nullptr );
60
63
66
70 QgsRenderContext &renderContext() { return mRenderContext; }
71
76 const QgsRenderContext &renderContext() const { return mRenderContext; } SIP_SKIP
77
81 QgsVector3D scale() const { return mScale; }
82
87 void setScale( const QgsVector3D &scale ) { mScale = scale; }
88
92 QgsVector3D offset() const { return mOffset; }
93
98 void setOffset( const QgsVector3D &offset ) { mOffset = offset; }
99
103 long pointsRendered() const;
104
111 void incrementPointsRendered( long count );
112
118 QgsPointCloudAttributeCollection attributes() const { return mAttributes; }
119
125 void setAttributes( const QgsPointCloudAttributeCollection &attributes );
126
130 int pointRecordSize() const { return mPointRecordSize; }
131
138 int xOffset() const { return mXOffset; }
139
146 int yOffset() const { return mYOffset; }
147
154 int zOffset() const { return mZOffset; }
155
161 double zValueScale() const { return mZValueScale; }
162
168 double zValueFixedOffset() const { return mZValueFixedOffset; }
169
175 QgsFeedback *feedback() const { return mFeedback; }
176
177#ifndef SIP_RUN
178
183 template <typename T>
184 static void getAttribute( const char *data, std::size_t offset, QgsPointCloudAttribute::DataType type, T &value )
185 {
186 switch ( type )
187 {
189 value = *reinterpret_cast< const unsigned char * >( data + offset );
190 return;
192 value = *( data + offset );
193 return;
194
196 value = *reinterpret_cast< const quint32 * >( data + offset );
197 return;
199 value = *reinterpret_cast< const qint32 * >( data + offset );
200 return;
201
203 value = *reinterpret_cast< const quint64 * >( data + offset );
204 return;
206 value = *reinterpret_cast< const qint64 * >( data + offset );
207 return;
208
210 value = *reinterpret_cast< const short * >( data + offset );
211 return;
212
214 value = *reinterpret_cast< const unsigned short * >( data + offset );
215 return;
216
218 value = *reinterpret_cast< const float * >( data + offset );
219 return;
220
222 value = *reinterpret_cast< const double * >( data + offset );
223 return;
224 }
225 }
226#endif
227
228#ifndef SIP_RUN // this is only meant for low-level rendering in C++ code
229
237 {
238 std::vector<double> points;
239 std::vector<QRgb> colors;
240 std::vector<float> elevations;
241 };
242
247 TriangulationData &triangulationData() { return mTriangulationData; }
248#endif
249
250 private:
251#ifdef SIP_RUN
253#endif
254
255 QgsRenderContext &mRenderContext;
256 QgsVector3D mScale;
257 QgsVector3D mOffset;
258 long mPointsRendered = 0;
260 int mPointRecordSize = 0;
261 int mXOffset = 0;
262 int mYOffset = 0;
263 int mZOffset = 0;
264 double mZValueScale = 1.0;
265 double mZValueFixedOffset = 0;
266
267 QgsFeedback *mFeedback = nullptr;
268
269 TriangulationData mTriangulationData;
270};
271
272#ifndef SIP_RUN
273
284{
285 public:
286
288
292 virtual QSet< QString > usedAttributes() const = 0;
293
299 virtual bool prepareBlock( const QgsPointCloudBlock *block ) = 0;
300
308 virtual QColor pointColor( const QgsPointCloudBlock *block, int i, double z ) = 0;
309
310};
311
312#endif
313
314
323class CORE_EXPORT QgsPointCloudRenderer
324{
325
326#ifdef SIP_RUN
328
329 const QString type = sipCpp->type();
330
331 if ( type == QLatin1String( "rgb" ) )
332 sipType = sipType_QgsPointCloudRgbRenderer;
333 else if ( type == QLatin1String( "ramp" ) )
334 sipType = sipType_QgsPointCloudAttributeByRampRenderer;
335 else if ( type == QLatin1String( "classified" ) )
336 sipType = sipType_QgsPointCloudClassifiedRenderer;
337 else if ( type == QLatin1String( "extent" ) )
338 sipType = sipType_QgsPointCloudExtentRenderer;
339 else
340 sipType = 0;
341 SIP_END
342#endif
343
344 public:
345
350
351 virtual ~QgsPointCloudRenderer() = default;
352
356 virtual QString type() const = 0;
357
363
366
369
373 virtual void renderBlock( const QgsPointCloudBlock *block, QgsPointCloudRenderContext &context ) = 0;
374
384 QVector<QVariantMap> identify( QgsPointCloudLayer *layer, const QgsRenderContext &context, const QgsGeometry &geometry, double toleranceForPointIdentification = 0 ) SIP_SKIP;
385
391 virtual bool willRenderPoint( const QMap<QString, QVariant> &pointAttributes )
392 {
393 Q_UNUSED( pointAttributes );
394 return true;
395 }
396
404 static QgsPointCloudRenderer *load( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
405
410 virtual QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) const = 0;
411
419 virtual QSet< QString > usedAttributes( const QgsPointCloudRenderContext &context ) const;
420
427 virtual std::unique_ptr< QgsPreparedPointCloudRendererData > prepare() SIP_SKIP;
428
438 virtual void startRender( QgsPointCloudRenderContext &context );
439
450 virtual void stopRender( QgsPointCloudRenderContext &context );
451
457 virtual bool legendItemChecked( const QString &key );
458
464 virtual void checkLegendItem( const QString &key, bool state = true );
465
472 void setPointSize( double size ) { mPointSize = size; }
473
483 double pointSize() const { return mPointSize; }
484
492 void setPointSizeUnit( const Qgis::RenderUnit units ) { mPointSizeUnit = units; }
493
500 Qgis::RenderUnit pointSizeUnit() const { return mPointSizeUnit; }
501
508 void setPointSizeMapUnitScale( const QgsMapUnitScale &scale ) { mPointSizeMapUnitScale = scale; }
509
516 const QgsMapUnitScale &pointSizeMapUnitScale() const { return mPointSizeMapUnitScale; }
517
524 Qgis::PointCloudDrawOrder drawOrder2d() const;
525
532 void setDrawOrder2d( Qgis::PointCloudDrawOrder order );
533
539 Qgis::PointCloudSymbol pointSymbol() const;
540
546 void setPointSymbol( Qgis::PointCloudSymbol symbol );
547
558 double maximumScreenError() const;
559
570 void setMaximumScreenError( double error );
571
578 Qgis::RenderUnit maximumScreenErrorUnit() const;
579
586 void setMaximumScreenErrorUnit( Qgis::RenderUnit unit );
587
593 bool renderAsTriangles() const { return mRenderAsTriangles; }
594
600 void setRenderAsTriangles( bool asTriangles ) { mRenderAsTriangles = asTriangles; }
601
612 bool horizontalTriangleFilter() const { return mHorizontalTriangleFilter; }
613
624 void setHorizontalTriangleFilter( bool enabled ) { mHorizontalTriangleFilter = enabled; }
625
637 double horizontalTriangleFilterThreshold() const { return mHorizontalTriangleFilterThreshold; }
638
650 void setHorizontalTriangleFilterThreshold( double threshold ) { mHorizontalTriangleFilterThreshold = threshold; }
651
661 Qgis::RenderUnit horizontalTriangleFilterUnit() const { return mHorizontalTriangleFilterUnit; }
662
672 void setHorizontalTriangleFilterUnit( Qgis::RenderUnit unit ) { mHorizontalTriangleFilterUnit = unit; }
673
677 virtual QList<QgsLayerTreeModelLegendNode *> createLegendNodes( QgsLayerTreeLayer *nodeLayer ) SIP_FACTORY;
678
682 virtual QStringList legendRuleKeys() const;
683
684 protected:
685
689 static void pointXY( QgsPointCloudRenderContext &context, const char *ptr, int i, double &x, double &y )
690 {
691 // be wary when copying this code!! In the renderer we explicitly request x/y/z as qint32 values, but in other
692 // situations these may be floats or doubles!
693 const qint32 ix = *reinterpret_cast< const qint32 * >( ptr + i * context.pointRecordSize() + context.xOffset() );
694 const qint32 iy = *reinterpret_cast< const qint32 * >( ptr + i * context.pointRecordSize() + context.yOffset() );
695 x = context.offset().x() + context.scale().x() * ix;
696 y = context.offset().y() + context.scale().y() * iy;
697 }
698
702 static double pointZ( QgsPointCloudRenderContext &context, const char *ptr, int i )
703 {
704 // be wary when copying this code!! In the renderer we explicitly request x/y/z as qint32 values, but in other
705 // situations these may be floats or doubles!
706 const qint32 iz = *reinterpret_cast<const qint32 * >( ptr + i * context.pointRecordSize() + context.zOffset() );
707 return ( context.offset().z() + context.scale().z() * iz ) * context.zValueScale() + context.zValueFixedOffset();
708 }
709
713 void drawPoint( double x, double y, const QColor &color, QgsPointCloudRenderContext &context ) const
714 {
715 drawPoint( x, y, color, mDefaultPainterPenWidth, context );
716 }
717
723 void drawPoint( double x, double y, const QColor &color, int width, QgsPointCloudRenderContext &context ) const
724 {
725 const QPointF originalXY( x, y );
726 context.renderContext().mapToPixel().transformInPlace( x, y );
727 QPainter *painter = context.renderContext().painter();
728 switch ( mPointSymbol )
729 {
731 painter->fillRect( QRectF( x - width * 0.5,
732 y - width * 0.5,
733 width, width ), color );
734 break;
735
737 painter->setBrush( QBrush( color ) );
738 painter->setPen( Qt::NoPen );
739 painter->drawEllipse( QRectF( x - width * 0.5,
740 y - width * 0.5,
741 width, width ) );
742 break;
743 };
744 }
745
746#ifndef SIP_RUN // intentionally left out from SIP to avoid API breaks in future when we move elevation post-processing elsewhere
747
752 void drawPointToElevationMap( double x, double y, double z, QgsPointCloudRenderContext &context ) const;
753
758 void drawPointToElevationMap( double x, double y, double z, int width, QgsPointCloudRenderContext &context ) const;
759#endif
760
765 void addPointToTriangulation( double x, double y, double z, const QColor &color, QgsPointCloudRenderContext &context )
766 {
767 QgsPointXY p = context.renderContext().mapToPixel().transform( x, y ) * context.renderContext().devicePixelRatio();
769 triangulation.points.push_back( p.x() );
770 triangulation.points.push_back( p.y() );
771 triangulation.colors.push_back( color.rgb() );
772 if ( context.renderContext().elevationMap() )
773 triangulation.elevations.push_back( static_cast<float>( z ) );
774 }
775
779 void copyCommonProperties( QgsPointCloudRenderer *destination ) const;
780
787 void restoreCommonProperties( const QDomElement &element, const QgsReadWriteContext &context );
788
795 void saveCommonProperties( QDomElement &element, const QgsReadWriteContext &context ) const;
796
797 private:
798#ifdef SIP_RUN
800#endif
801
802#ifdef QGISDEBUG
804 QThread *mThread = nullptr;
805#endif
806
807 double mMaximumScreenError = 0.3;
808 Qgis::RenderUnit mMaximumScreenErrorUnit = Qgis::RenderUnit::Millimeters;
809
810 double mPointSize = 1;
812 QgsMapUnitScale mPointSizeMapUnitScale;
813
815 int mDefaultPainterPenWidth = 1;
817
818 bool mRenderAsTriangles = false;
819 bool mHorizontalTriangleFilter = false;
820 double mHorizontalTriangleFilterThreshold = 5.0;
821 Qgis::RenderUnit mHorizontalTriangleFilterUnit = Qgis::RenderUnit::Millimeters;
822};
823
824#endif // QGSPOINTCLOUDRENDERER_H
PointCloudSymbol
Rendering symbols for point cloud points.
Definition qgis.h:3654
@ Circle
Renders points as circles.
@ Square
Renders points as squares.
PointCloudDrawOrder
Pointcloud rendering order for 2d views.
Definition qgis.h:3666
@ Default
Draw points in the order they are stored.
RenderUnit
Rendering size units.
Definition qgis.h:4489
@ Millimeters
Millimeters.
Stores digital elevation model in a raster image which may get updated as a part of map layer renderi...
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
A geometry is the spatial representation of a feature.
Layer tree node points to a map layer.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
QgsPointXY transform(const QgsPointXY &p) const
Transforms a point p from map (world) coordinates to device coordinates.
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.
@ UChar
Unsigned char 1 byte.
@ UInt32
Unsigned int32 4 bytes.
@ UInt64
Unsigned int64 8 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.
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.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
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...
void setOffset(const QgsVector3D &offset)
Sets the offset of the layer's int32 coordinates compared to CRS coords.
void setScale(const QgsVector3D &scale)
Sets the scale 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.
const QgsRenderContext & renderContext() const
Returns a reference to the context's render context.
QgsPointCloudRenderContext & operator=(const QgsPointCloudRenderContext &)=delete
QgsPointCloudRenderContext cannot be copied.
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.
TriangulationData & triangulationData()
Returns reference to the triangulation data structure (only used when rendering as triangles is enabl...
int zOffset() const
Returns the offset for the y value in a point record.
QgsFeedback * feedback() const
Returns the feedback object used to cancel rendering.
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.
bool renderAsTriangles() const
Returns whether points are triangulated to render solid surface.
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.
const QgsMapUnitScale & pointSizeMapUnitScale() const
Returns the map unit scale 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.
bool horizontalTriangleFilter() const
Returns whether large triangles will get rendered.
virtual QString type() const =0
Returns the identifier of the renderer type.
void setHorizontalTriangleFilterThreshold(double threshold)
Sets threshold for filtering of triangles.
void setHorizontalTriangleFilterUnit(Qgis::RenderUnit unit)
Sets units of the threshold for filtering of triangles.
QgsPointCloudRenderer(const QgsPointCloudRenderer &other)=delete
QgsPointCloudRenderer cannot be copied – use clone() instead.
virtual QgsPointCloudRenderer * clone() const =0
Create a deep copy of this renderer.
void addPointToTriangulation(double x, double y, double z, const QColor &color, QgsPointCloudRenderContext &context)
Adds a point to the list of points to be triangulated (only used when renderAsTriangles() is enabled)
void setPointSizeUnit(const Qgis::RenderUnit units)
Sets the units used for the point size.
Qgis::RenderUnit horizontalTriangleFilterUnit() const
Returns units of the threshold for filtering of triangles.
void setRenderAsTriangles(bool asTriangles)
Sets whether points are triangulated to render solid surface.
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).
void drawPoint(double x, double y, const QColor &color, int width, QgsPointCloudRenderContext &context) const
Draws a point using a color and painter width at the specified x and y (in map coordinates).
Qgis::RenderUnit pointSizeUnit() const
Returns the units used for the point size.
double horizontalTriangleFilterThreshold() const
Returns threshold for filtering of triangles.
void setHorizontalTriangleFilter(bool enabled)
Sets whether large triangles will get rendered.
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.
A class to represent a 2D point.
Definition qgspointxy.h:60
double y
Definition qgspointxy.h:64
double x
Definition qgspointxy.h:63
Base class for 2d point cloud renderer prepared data containers.
virtual QColor pointColor(const QgsPointCloudBlock *block, int i, double z)=0
An optimised method of retrieving the color of a point from a point cloud block.
virtual bool prepareBlock(const QgsPointCloudBlock *block)=0
Prepares the renderer for using the specified block.
virtual QSet< QString > usedAttributes() const =0
Returns the set of attributes used by the prepared point cloud renderer.
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.
QgsElevationMap * elevationMap() const
Returns the destination elevation map for the render operation.
float devicePixelRatio() const
Returns the device pixel ratio.
const QgsMapToPixel & mapToPixel() const
Returns the context's map to pixel transform, which transforms between map coordinates and device coo...
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
Definition qgsvector3d.h:31
double y() const
Returns Y coordinate.
Definition qgsvector3d.h:50
double z() const
Returns Z coordinate.
Definition qgsvector3d.h:52
double x() const
Returns X coordinate.
Definition qgsvector3d.h:48
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:191
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_FACTORY
Definition qgis_sip.h:76
#define SIP_END
Definition qgis_sip.h:208
Helper data structure used when rendering points as triangulated surface.
std::vector< QRgb > colors
RGB color for each point.
std::vector< float > elevations
Z value for each point (only used when global map shading is enabled)
std::vector< double > points
X,Y for each point - kept in this structure so that we can use it without further conversions in Dela...