QGIS API Documentation 3.41.0-Master (cea29feecf2)
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#include "qgsstyle.h"
28
33class QgsElevationMap;
34
44{
45 public:
46
59 QgsPointCloudRenderContext( QgsRenderContext &context, const QgsVector3D &scale, const QgsVector3D &offset,
60 double zValueScale, double zValueFixedOffset, QgsFeedback *feedback = nullptr );
61
64
68 QgsRenderContext &renderContext() { return mRenderContext; }
69
74 const QgsRenderContext &renderContext() const SIP_SKIP { return mRenderContext; }
75
79 QgsVector3D scale() const { return mScale; }
80
85 void setScale( const QgsVector3D &scale ) { mScale = scale; }
86
90 QgsVector3D offset() const { return mOffset; }
91
96 void setOffset( const QgsVector3D &offset ) { mOffset = offset; }
97
101 long pointsRendered() const;
102
109 void incrementPointsRendered( long count );
110
116 QgsPointCloudAttributeCollection attributes() const { return mAttributes; }
117
123 void setAttributes( const QgsPointCloudAttributeCollection &attributes );
124
128 int pointRecordSize() const { return mPointRecordSize; }
129
136 int xOffset() const { return mXOffset; }
137
144 int yOffset() const { return mYOffset; }
145
152 int zOffset() const { return mZOffset; }
153
159 double zValueScale() const { return mZValueScale; }
160
166 double zValueFixedOffset() const { return mZValueFixedOffset; }
167
173 QgsFeedback *feedback() const { return mFeedback; }
174
175#ifndef SIP_RUN
176
181 template <typename T>
182 static void getAttribute( const char *data, std::size_t offset, QgsPointCloudAttribute::DataType type, T &value )
183 {
184 switch ( type )
185 {
187 value = *reinterpret_cast< const unsigned char * >( data + offset );
188 return;
190 value = *( data + offset );
191 return;
192
194 value = *reinterpret_cast< const quint32 * >( data + offset );
195 return;
197 value = *reinterpret_cast< const qint32 * >( data + offset );
198 return;
199
201 value = *reinterpret_cast< const quint64 * >( data + offset );
202 return;
204 value = *reinterpret_cast< const qint64 * >( data + offset );
205 return;
206
208 value = *reinterpret_cast< const short * >( data + offset );
209 return;
210
212 value = *reinterpret_cast< const unsigned short * >( data + offset );
213 return;
214
216 value = *reinterpret_cast< const float * >( data + offset );
217 return;
218
220 value = *reinterpret_cast< const double * >( data + offset );
221 return;
222 }
223 }
224#endif
225
226#ifndef SIP_RUN // this is only meant for low-level rendering in C++ code
227
235 {
236 std::vector<double> points;
237 std::vector<QRgb> colors;
238 std::vector<float> elevations;
239 };
240
245 TriangulationData &triangulationData() { return mTriangulationData; }
246#endif
247
248 private:
249#ifdef SIP_RUN
251#endif
252
253 QgsRenderContext &mRenderContext;
254 QgsVector3D mScale;
255 QgsVector3D mOffset;
256 long mPointsRendered = 0;
258 int mPointRecordSize = 0;
259 int mXOffset = 0;
260 int mYOffset = 0;
261 int mZOffset = 0;
262 double mZValueScale = 1.0;
263 double mZValueFixedOffset = 0;
264
265 QgsFeedback *mFeedback = nullptr;
266
267 TriangulationData mTriangulationData;
268};
269
270#ifndef SIP_RUN
271
282{
283 public:
284
286
290 virtual QSet< QString > usedAttributes() const = 0;
291
297 virtual bool prepareBlock( const QgsPointCloudBlock *block ) = 0;
298
306 virtual QColor pointColor( const QgsPointCloudBlock *block, int i, double z ) = 0;
307
308};
309
310#endif
311
312
321class CORE_EXPORT QgsPointCloudRenderer
322{
323
324#ifdef SIP_RUN
326
327 const QString type = sipCpp->type();
328
329 if ( type == QLatin1String( "rgb" ) )
330 sipType = sipType_QgsPointCloudRgbRenderer;
331 else if ( type == QLatin1String( "ramp" ) )
332 sipType = sipType_QgsPointCloudAttributeByRampRenderer;
333 else if ( type == QLatin1String( "classified" ) )
334 sipType = sipType_QgsPointCloudClassifiedRenderer;
335 else if ( type == QLatin1String( "extent" ) )
336 sipType = sipType_QgsPointCloudExtentRenderer;
337 else
338 sipType = 0;
339 SIP_END
340#endif
341
342 public:
343
345
346 virtual ~QgsPointCloudRenderer() = default;
347
351 virtual QString type() const = 0;
352
358
361
364
368 virtual void renderBlock( const QgsPointCloudBlock *block, QgsPointCloudRenderContext &context ) = 0;
369
379 QVector<QVariantMap> identify( QgsPointCloudLayer *layer, const QgsRenderContext &context, const QgsGeometry &geometry, double toleranceForPointIdentification = 0 ) SIP_SKIP;
380
386 virtual bool willRenderPoint( const QMap<QString, QVariant> &pointAttributes )
387 {
388 Q_UNUSED( pointAttributes );
389 return true;
390 }
391
399 static QgsPointCloudRenderer *load( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
400
405 virtual QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) const = 0;
406
414 virtual QSet< QString > usedAttributes( const QgsPointCloudRenderContext &context ) const;
415
422 virtual std::unique_ptr< QgsPreparedPointCloudRendererData > prepare() SIP_SKIP;
423
433 virtual void startRender( QgsPointCloudRenderContext &context );
434
445 virtual void stopRender( QgsPointCloudRenderContext &context );
446
452 virtual bool legendItemChecked( const QString &key );
453
459 virtual void checkLegendItem( const QString &key, bool state = true );
460
467 void setPointSize( double size ) { mPointSize = size; }
468
478 double pointSize() const { return mPointSize; }
479
487 void setPointSizeUnit( const Qgis::RenderUnit units ) { mPointSizeUnit = units; }
488
495 Qgis::RenderUnit pointSizeUnit() const { return mPointSizeUnit; }
496
503 void setPointSizeMapUnitScale( const QgsMapUnitScale &scale ) { mPointSizeMapUnitScale = scale; }
504
511 const QgsMapUnitScale &pointSizeMapUnitScale() const { return mPointSizeMapUnitScale; }
512
519 Qgis::PointCloudDrawOrder drawOrder2d() const;
520
527 void setDrawOrder2d( Qgis::PointCloudDrawOrder order );
528
534 Qgis::PointCloudSymbol pointSymbol() const;
535
541 void setPointSymbol( Qgis::PointCloudSymbol symbol );
542
553 double maximumScreenError() const;
554
565 void setMaximumScreenError( double error );
566
573 Qgis::RenderUnit maximumScreenErrorUnit() const;
574
581 void setMaximumScreenErrorUnit( Qgis::RenderUnit unit );
582
588 bool renderAsTriangles() const { return mRenderAsTriangles; }
589
595 void setRenderAsTriangles( bool asTriangles ) { mRenderAsTriangles = asTriangles; }
596
607 bool horizontalTriangleFilter() const { return mHorizontalTriangleFilter; }
608
619 void setHorizontalTriangleFilter( bool enabled ) { mHorizontalTriangleFilter = enabled; }
620
632 double horizontalTriangleFilterThreshold() const { return mHorizontalTriangleFilterThreshold; }
633
645 void setHorizontalTriangleFilterThreshold( double threshold ) { mHorizontalTriangleFilterThreshold = threshold; }
646
656 Qgis::RenderUnit horizontalTriangleFilterUnit() const { return mHorizontalTriangleFilterUnit; }
657
667 void setHorizontalTriangleFilterUnit( Qgis::RenderUnit unit ) { mHorizontalTriangleFilterUnit = unit; }
668
672 virtual QList<QgsLayerTreeModelLegendNode *> createLegendNodes( QgsLayerTreeLayer *nodeLayer ) SIP_FACTORY;
673
677 virtual QStringList legendRuleKeys() const;
678
683 void setShowLabels( const bool show ) { mShowLabels = show; }
684
690 bool showLabels() const { return mShowLabels; }
691
696 void setLabelTextFormat( const QgsTextFormat &textFormat ) { mLabelTextFormat = textFormat; }
697
702 QgsTextFormat labelTextFormat() const { return mLabelTextFormat; }
703
704 protected:
705
709 static void pointXY( QgsPointCloudRenderContext &context, const char *ptr, int i, double &x, double &y )
710 {
711 // be wary when copying this code!! In the renderer we explicitly request x/y/z as qint32 values, but in other
712 // situations these may be floats or doubles!
713 const qint32 ix = *reinterpret_cast< const qint32 * >( ptr + i * context.pointRecordSize() + context.xOffset() );
714 const qint32 iy = *reinterpret_cast< const qint32 * >( ptr + i * context.pointRecordSize() + context.yOffset() );
715 x = context.offset().x() + context.scale().x() * ix;
716 y = context.offset().y() + context.scale().y() * iy;
717 }
718
722 static double pointZ( QgsPointCloudRenderContext &context, const char *ptr, int i )
723 {
724 // be wary when copying this code!! In the renderer we explicitly request x/y/z as qint32 values, but in other
725 // situations these may be floats or doubles!
726 const qint32 iz = *reinterpret_cast<const qint32 * >( ptr + i * context.pointRecordSize() + context.zOffset() );
727 return ( context.offset().z() + context.scale().z() * iz ) * context.zValueScale() + context.zValueFixedOffset();
728 }
729
733 void drawPoint( double x, double y, const QColor &color, QgsPointCloudRenderContext &context ) const
734 {
735 drawPoint( x, y, color, mDefaultPainterPenWidth, context );
736 }
737
743 void drawPoint( double x, double y, const QColor &color, int width, QgsPointCloudRenderContext &context ) const
744 {
745 const QPointF originalXY( x, y );
746 context.renderContext().mapToPixel().transformInPlace( x, y );
747 QPainter *painter = context.renderContext().painter();
748 switch ( mPointSymbol )
749 {
751 painter->fillRect( QRectF( x - width * 0.5,
752 y - width * 0.5,
753 width, width ), color );
754 break;
755
757 painter->setBrush( QBrush( color ) );
758 painter->setPen( Qt::NoPen );
759 painter->drawEllipse( QRectF( x - width * 0.5,
760 y - width * 0.5,
761 width, width ) );
762 break;
763 };
764 }
765
766#ifndef SIP_RUN // intentionally left out from SIP to avoid API breaks in future when we move elevation post-processing elsewhere
767
772 void drawPointToElevationMap( double x, double y, double z, QgsPointCloudRenderContext &context ) const;
773
778 void drawPointToElevationMap( double x, double y, double z, int width, QgsPointCloudRenderContext &context ) const;
779#endif
780
785 void addPointToTriangulation( double x, double y, double z, const QColor &color, QgsPointCloudRenderContext &context )
786 {
787 QgsPointXY p = context.renderContext().mapToPixel().transform( x, y ) * context.renderContext().devicePixelRatio();
789 triangulation.points.push_back( p.x() );
790 triangulation.points.push_back( p.y() );
791 triangulation.colors.push_back( color.rgb() );
792 if ( context.renderContext().elevationMap() )
793 triangulation.elevations.push_back( static_cast<float>( z ) );
794 }
795
799 void copyCommonProperties( QgsPointCloudRenderer *destination ) const;
800
807 void restoreCommonProperties( const QDomElement &element, const QgsReadWriteContext &context );
808
815 void saveCommonProperties( QDomElement &element, const QgsReadWriteContext &context ) const;
816
817 private:
818#ifdef SIP_RUN
820#endif
821
822#ifdef QGISDEBUG
824 QThread *mThread = nullptr;
825#endif
826
827 double mMaximumScreenError = 0.3;
828 Qgis::RenderUnit mMaximumScreenErrorUnit = Qgis::RenderUnit::Millimeters;
829
830 double mPointSize = 1;
832 QgsMapUnitScale mPointSizeMapUnitScale;
833
835 int mDefaultPainterPenWidth = 1;
837
838 bool mRenderAsTriangles = false;
839 bool mHorizontalTriangleFilter = false;
840 double mHorizontalTriangleFilterThreshold = 5.0;
841 Qgis::RenderUnit mHorizontalTriangleFilterUnit = Qgis::RenderUnit::Millimeters;
842
843 bool mShowLabels = false;
844 QgsTextFormat mLabelTextFormat;
845};
846
847#endif // QGSPOINTCLOUDRENDERER_H
PointCloudSymbol
Rendering symbols for point cloud points.
Definition qgis.h:3995
@ Circle
Renders points as circles.
@ Square
Renders points as squares.
PointCloudDrawOrder
Pointcloud rendering order for 2d views.
Definition qgis.h:4007
@ Default
Draw points in the order they are stored.
RenderUnit
Rendering size units.
Definition qgis.h:4892
@ 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 map coordinates to device 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(const QgsPointCloudRenderContext &rh)=delete
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.
QgsTextFormat labelTextFormat() const
Returns the text format renderer is using for rendering labels.
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.
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.
bool showLabels() const
Returns whether the renderer shows file labels inside the extent rectangle.
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 setLabelTextFormat(const QgsTextFormat &textFormat)
Sets the text format renderers should use for rendering labels.
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).
void setShowLabels(const bool show)
Set whether the renderer should also render file labels inside extent.
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...
Container for all settings relating to text rendering.
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...