QGIS API Documentation 3.35.0-Master (ffb31addd7f)
Loading...
Searching...
No Matches
qgspointcloudrgbrenderer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspointcloudrgbrenderer.h
3 --------------------
4 begin : October 2020
5 copyright : (C) 2020 by Nyall Dawson
6 email : nyall dot dawson 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
19#include "qgspointcloudblock.h"
21
26
28{
29 return QStringLiteral( "rgb" );
30}
31
33{
34 std::unique_ptr< QgsPointCloudRgbRenderer > res = std::make_unique< QgsPointCloudRgbRenderer >();
35 res->mRedAttribute = mRedAttribute;
36 res->mGreenAttribute = mGreenAttribute;
37 res->mBlueAttribute = mBlueAttribute;
38
39 if ( mRedContrastEnhancement )
40 {
41 res->setRedContrastEnhancement( new QgsContrastEnhancement( *mRedContrastEnhancement ) );
42 }
43 if ( mGreenContrastEnhancement )
44 {
45 res->setGreenContrastEnhancement( new QgsContrastEnhancement( *mGreenContrastEnhancement ) );
46 }
47 if ( mBlueContrastEnhancement )
48 {
49 res->setBlueContrastEnhancement( new QgsContrastEnhancement( *mBlueContrastEnhancement ) );
50 }
51
52 copyCommonProperties( res.get() );
53
54 return res.release();
55}
56
58{
59 QgsRectangle visibleExtent = context.renderContext().extent();
60 if ( renderAsTriangles() )
61 {
62 // we need to include also points slightly outside of the visible extent,
63 // otherwise the triangulation may be missing triangles near the edges and corners
64 visibleExtent.grow( std::max( visibleExtent.width(), visibleExtent.height() ) * 0.05 );
65 }
66
67 const char *ptr = block->data();
68 const int count = block->pointCount();
69 const QgsPointCloudAttributeCollection request = block->attributes();
70
71 const std::size_t recordSize = request.pointRecordSize();
72 int redOffset = 0;
73 const QgsPointCloudAttribute *attribute = request.find( mRedAttribute, redOffset );
74 if ( !attribute )
75 return;
76 const QgsPointCloudAttribute::DataType redType = attribute->type();
77
78 int greenOffset = 0;
79 attribute = request.find( mGreenAttribute, greenOffset );
80 if ( !attribute )
81 return;
82 const QgsPointCloudAttribute::DataType greenType = attribute->type();
83
84 int blueOffset = 0;
85 attribute = request.find( mBlueAttribute, blueOffset );
86 if ( !attribute )
87 return;
88 const QgsPointCloudAttribute::DataType blueType = attribute->type();
89
90 const bool useRedContrastEnhancement = mRedContrastEnhancement && mRedContrastEnhancement->contrastEnhancementAlgorithm() != QgsContrastEnhancement::NoEnhancement;
91 const bool useBlueContrastEnhancement = mBlueContrastEnhancement && mBlueContrastEnhancement->contrastEnhancementAlgorithm() != QgsContrastEnhancement::NoEnhancement;
92 const bool useGreenContrastEnhancement = mGreenContrastEnhancement && mGreenContrastEnhancement->contrastEnhancementAlgorithm() != QgsContrastEnhancement::NoEnhancement;
93
94 const bool renderElevation = context.renderContext().elevationMap();
95 const QgsDoubleRange zRange = context.renderContext().zRange();
96 const bool considerZ = !zRange.isInfinite() || renderElevation;
97
98 int rendered = 0;
99 double x = 0;
100 double y = 0;
101 double z = 0;
103 const bool reproject = ct.isValid();
104 for ( int i = 0; i < count; ++i )
105 {
106 if ( context.renderContext().renderingStopped() )
107 {
108 break;
109 }
110
111 if ( considerZ )
112 {
113 // z value filtering is cheapest, if we're doing it...
114 z = pointZ( context, ptr, i );
115 if ( !zRange.contains( z ) )
116 continue;
117 }
118
119 pointXY( context, ptr, i, x, y );
120 if ( visibleExtent.contains( x, y ) )
121 {
122 if ( reproject )
123 {
124 try
125 {
126 ct.transformInPlace( x, y, z );
127 }
128 catch ( QgsCsException & )
129 {
130 continue;
131 }
132 }
133
134 int red = 0;
135 context.getAttribute( ptr, i * recordSize + redOffset, redType, red );
136 int green = 0;
137 context.getAttribute( ptr, i * recordSize + greenOffset, greenType, green );
138 int blue = 0;
139 context.getAttribute( ptr, i * recordSize + blueOffset, blueType, blue );
140
141 //skip if red, green or blue not in displayable range
142 if ( ( useRedContrastEnhancement && !mRedContrastEnhancement->isValueInDisplayableRange( red ) )
143 || ( useGreenContrastEnhancement && !mGreenContrastEnhancement->isValueInDisplayableRange( green ) )
144 || ( useBlueContrastEnhancement && !mBlueContrastEnhancement->isValueInDisplayableRange( blue ) ) )
145 {
146 continue;
147 }
148
149 //stretch color values
150 if ( useRedContrastEnhancement )
151 {
152 red = mRedContrastEnhancement->enhanceContrast( red );
153 }
154 if ( useGreenContrastEnhancement )
155 {
156 green = mGreenContrastEnhancement->enhanceContrast( green );
157 }
158 if ( useBlueContrastEnhancement )
159 {
160 blue = mBlueContrastEnhancement->enhanceContrast( blue );
161 }
162
163 red = std::max( 0, std::min( 255, red ) );
164 green = std::max( 0, std::min( 255, green ) );
165 blue = std::max( 0, std::min( 255, blue ) );
166
167 if ( renderAsTriangles() )
168 {
169 addPointToTriangulation( x, y, z, QColor( red, green, blue ), context );
170 }
171 else
172 {
173 drawPoint( x, y, QColor( red, green, blue ), context );
174 if ( renderElevation )
175 drawPointToElevationMap( x, y, z, context );
176 }
177 rendered++;
178 }
179 }
180 context.incrementPointsRendered( rendered );
181}
182
183
185{
186 std::unique_ptr< QgsPointCloudRgbRenderer > r = std::make_unique< QgsPointCloudRgbRenderer >();
187
188 r->setRedAttribute( element.attribute( QStringLiteral( "red" ), QStringLiteral( "Red" ) ) );
189 r->setGreenAttribute( element.attribute( QStringLiteral( "green" ), QStringLiteral( "Green" ) ) );
190 r->setBlueAttribute( element.attribute( QStringLiteral( "blue" ), QStringLiteral( "Blue" ) ) );
191
192 r->restoreCommonProperties( element, context );
193
194 //contrast enhancements
196 const QDomElement redContrastElem = element.firstChildElement( QStringLiteral( "redContrastEnhancement" ) );
197 if ( !redContrastElem.isNull() )
198 {
200 redContrastEnhancement->readXml( redContrastElem );
201 r->setRedContrastEnhancement( redContrastEnhancement );
202 }
203
205 const QDomElement greenContrastElem = element.firstChildElement( QStringLiteral( "greenContrastEnhancement" ) );
206 if ( !greenContrastElem.isNull() )
207 {
209 greenContrastEnhancement->readXml( greenContrastElem );
210 r->setGreenContrastEnhancement( greenContrastEnhancement );
211 }
212
214 const QDomElement blueContrastElem = element.firstChildElement( QStringLiteral( "blueContrastEnhancement" ) );
215 if ( !blueContrastElem.isNull() )
216 {
218 blueContrastEnhancement->readXml( blueContrastElem );
219 r->setBlueContrastEnhancement( blueContrastEnhancement );
220 }
221
222 return r.release();
223}
224
225QDomElement QgsPointCloudRgbRenderer::save( QDomDocument &doc, const QgsReadWriteContext &context ) const
226{
227 QDomElement rendererElem = doc.createElement( QStringLiteral( "renderer" ) );
228
229 rendererElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "rgb" ) );
230
231 rendererElem.setAttribute( QStringLiteral( "red" ), mRedAttribute );
232 rendererElem.setAttribute( QStringLiteral( "green" ), mGreenAttribute );
233 rendererElem.setAttribute( QStringLiteral( "blue" ), mBlueAttribute );
234
235 saveCommonProperties( rendererElem, context );
236
237 //contrast enhancement
238 if ( mRedContrastEnhancement )
239 {
240 QDomElement redContrastElem = doc.createElement( QStringLiteral( "redContrastEnhancement" ) );
241 mRedContrastEnhancement->writeXml( doc, redContrastElem );
242 rendererElem.appendChild( redContrastElem );
243 }
244 if ( mGreenContrastEnhancement )
245 {
246 QDomElement greenContrastElem = doc.createElement( QStringLiteral( "greenContrastEnhancement" ) );
247 mGreenContrastEnhancement->writeXml( doc, greenContrastElem );
248 rendererElem.appendChild( greenContrastElem );
249 }
250 if ( mBlueContrastEnhancement )
251 {
252 QDomElement blueContrastElem = doc.createElement( QStringLiteral( "blueContrastEnhancement" ) );
253 mBlueContrastEnhancement->writeXml( doc, blueContrastElem );
254 rendererElem.appendChild( blueContrastElem );
255 }
256
257 return rendererElem;
258}
259
261{
262 QSet<QString> res;
263 res << mRedAttribute << mGreenAttribute << mBlueAttribute;
264 return res;
265}
266
267std::unique_ptr<QgsPreparedPointCloudRendererData> QgsPointCloudRgbRenderer::prepare()
268{
269 std::unique_ptr< QgsPointCloudRgbRendererPreparedData > data = std::make_unique< QgsPointCloudRgbRendererPreparedData >();
270 data->redAttribute = mRedAttribute;
271 if ( mRedContrastEnhancement )
272 data->redContrastEnhancement.reset( new QgsContrastEnhancement( *mRedContrastEnhancement ) );
273 data->greenAttribute = mGreenAttribute;
274 if ( mGreenContrastEnhancement )
275 data->greenContrastEnhancement.reset( new QgsContrastEnhancement( *mGreenContrastEnhancement ) );
276 data->blueAttribute = mBlueAttribute;
277 if ( mBlueContrastEnhancement )
278 data->blueContrastEnhancement.reset( new QgsContrastEnhancement( *mBlueContrastEnhancement ) );
279
280 data->useRedContrastEnhancement = mRedContrastEnhancement && mRedContrastEnhancement->contrastEnhancementAlgorithm() != QgsContrastEnhancement::NoEnhancement;
281 data->useBlueContrastEnhancement = mBlueContrastEnhancement && mBlueContrastEnhancement->contrastEnhancementAlgorithm() != QgsContrastEnhancement::NoEnhancement;
282 data->useGreenContrastEnhancement = mGreenContrastEnhancement && mGreenContrastEnhancement->contrastEnhancementAlgorithm() != QgsContrastEnhancement::NoEnhancement;
283
284 return data;
285}
286
291
293{
294 const QgsPointCloudAttributeCollection request = block->attributes();
295 redOffset = 0;
296 const QgsPointCloudAttribute *attribute = request.find( redAttribute, redOffset );
297 if ( !attribute )
298 return false;
299 redType = attribute->type();
300
301 greenOffset = 0;
302 attribute = request.find( greenAttribute, greenOffset );
303 if ( !attribute )
304 return false;
305 greenType = attribute->type();
306
307 blueOffset = 0;
308 attribute = request.find( blueAttribute, blueOffset );
309 if ( !attribute )
310 return false;
311 blueType = attribute->type();
312 return true;
313}
314
316{
317 const char *ptr = block->data();
318 const int pointRecordSize = block->pointRecordSize();
319
320 int red = 0;
321 QgsPointCloudRenderContext::getAttribute( ptr, i * pointRecordSize + redOffset, redType, red );
322 int green = 0;
323 QgsPointCloudRenderContext::getAttribute( ptr, i * pointRecordSize + greenOffset, greenType, green );
324 int blue = 0;
325 QgsPointCloudRenderContext::getAttribute( ptr, i * pointRecordSize + blueOffset, blueType, blue );
326
327 //skip if red, green or blue not in displayable range
328 if ( ( useRedContrastEnhancement && !redContrastEnhancement->isValueInDisplayableRange( red ) )
329 || ( useGreenContrastEnhancement && !greenContrastEnhancement->isValueInDisplayableRange( green ) )
330 || ( useBlueContrastEnhancement && !blueContrastEnhancement->isValueInDisplayableRange( blue ) ) )
331 {
332 return QColor();
333 }
334
335 //stretch color values
337 {
338 red = redContrastEnhancement->enhanceContrast( red );
339 }
341 {
342 green = greenContrastEnhancement->enhanceContrast( green );
343 }
345 {
346 blue = blueContrastEnhancement->enhanceContrast( blue );
347 }
348
349 red = std::max( 0, std::min( 255, red ) );
350 green = std::max( 0, std::min( 255, green ) );
351 blue = std::max( 0, std::min( 255, blue ) );
352
353 return QColor( red, green, blue );
354}
355
357{
358 return mRedAttribute;
359}
360
361void QgsPointCloudRgbRenderer::setRedAttribute( const QString &redAttribute )
362{
363 mRedAttribute = redAttribute;
364}
365
367{
368 return mGreenAttribute;
369}
370
371void QgsPointCloudRgbRenderer::setGreenAttribute( const QString &greenAttribute )
372{
373 mGreenAttribute = greenAttribute;
374}
375
377{
378 return mBlueAttribute;
379}
380
381void QgsPointCloudRgbRenderer::setBlueAttribute( const QString &blueAttribute )
382{
383 mBlueAttribute = blueAttribute;
384}
385
387{
388 return mRedContrastEnhancement.get();
389}
390
392{
393 mRedContrastEnhancement.reset( enhancement );
394}
395
397{
398 return mGreenContrastEnhancement.get();
399}
400
402{
403 mGreenContrastEnhancement.reset( enhancement );
404}
405
407{
408 return mBlueContrastEnhancement.get();
409}
410
412{
413 mBlueContrastEnhancement.reset( enhancement );
414}
@ UnknownDataType
Unknown or unspecified type.
Manipulates raster or point cloud pixel values so that they enhanceContrast or clip into a specified ...
@ NoEnhancement
Default color scaling algorithm, no scaling is applied.
void readXml(const QDomElement &elem)
Class for doing transforms between two map coordinate systems.
void transformInPlace(double &x, double &y, double &z, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Transforms an array of x, y and z double coordinates in place, from the source CRS to the destination...
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
Custom exception class for Coordinate Reference System related exceptions.
QgsRange which stores a range of double values.
Definition qgsrange.h:203
bool isInfinite() const
Returns true if the range consists of all possible values.
Definition qgsrange.h:247
Collection of point cloud attributes.
int pointRecordSize() const
Returns total size of record.
const QgsPointCloudAttribute * find(const QString &attributeName, int &offset) const
Finds the attribute with the name.
Attribute for point cloud data pair of name and size in bytes.
DataType
Systems of unit measurement.
DataType type() const
Returns the data type.
Base class for storing raw data from point cloud nodes.
const char * data() const
Returns raw pointer to data.
QgsPointCloudAttributeCollection attributes() const
Returns the attributes that are stored in the data block, along with their size.
int pointCount() const
Returns number of points that are stored in the block.
int pointRecordSize() const
Returns the total size of each individual point record.
Encapsulates the render context for a 2D point cloud rendering operation.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
void incrementPointsRendered(long count)
Increments the count of points rendered by the specified amount.
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...
Abstract base class for 2d point cloud renderers.
bool renderAsTriangles() const
Returns whether points are triangulated to render solid surface.
void drawPointToElevationMap(double x, double y, double z, QgsPointCloudRenderContext &context) const
Draws a point at the elevation z using at the specified x and y (in map coordinates) on the elevation...
void saveCommonProperties(QDomElement &element, const QgsReadWriteContext &context) const
Saves common renderer properties (such as point size and screen error) to the specified DOM element.
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 copyCommonProperties(QgsPointCloudRenderer *destination) const
Copies common point cloud properties (such as point size and screen error) to the destination rendere...
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).
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.
std::unique_ptr< QgsContrastEnhancement > redContrastEnhancement
QSet< QString > usedAttributes() const override
Returns the set of attributes used by the prepared point cloud renderer.
std::unique_ptr< QgsContrastEnhancement > greenContrastEnhancement
bool prepareBlock(const QgsPointCloudBlock *block) override
Prepares the renderer for using the specified block.
QColor pointColor(const QgsPointCloudBlock *block, int i, double z) override
An optimised method of retrieving the color of a point from a point cloud block.
std::unique_ptr< QgsContrastEnhancement > blueContrastEnhancement
QgsPointCloudAttribute::DataType blueType
QgsPointCloudAttribute::DataType redType
QgsPointCloudAttribute::DataType greenType
std::unique_ptr< QgsPreparedPointCloudRendererData > prepare() override
Returns prepared data container for bulk point color retrieval.
void setRedContrastEnhancement(QgsContrastEnhancement *enhancement)
Sets the contrast enhancement to use for the red channel.
QString redAttribute() const
Returns the attribute to use for the red channel.
static QgsPointCloudRenderer * create(QDomElement &element, const QgsReadWriteContext &context)
Creates an RGB renderer from an XML element.
QString greenAttribute() const
Returns the attribute to use for the green channel.
QString type() const override
Returns the identifier of the renderer type.
QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context) const override
Saves the renderer configuration to an XML element.
void setBlueContrastEnhancement(QgsContrastEnhancement *enhancement)
Sets the contrast enhancement to use for the blue channel.
void renderBlock(const QgsPointCloudBlock *block, QgsPointCloudRenderContext &context) override
Renders a block of point cloud data using the specified render context.
const QgsContrastEnhancement * greenContrastEnhancement() const
Returns the contrast enhancement to use for the green channel.
QString blueAttribute() const
Returns the attribute to use for the blue channel.
QgsPointCloudRgbRenderer()
Constructor for QgsPointCloudRgbRenderer.
void setGreenContrastEnhancement(QgsContrastEnhancement *enhancement)
Sets the contrast enhancement to use for the green channel.
void setBlueAttribute(const QString &attribute)
Sets the attribute to use for the blue channel.
const QgsContrastEnhancement * blueContrastEnhancement() const
Returns the contrast enhancement to use for the blue channel.
void setGreenAttribute(const QString &attribute)
Sets the attribute to use for the green channel.
void setRedAttribute(const QString &attribute)
Sets the attribute to use for the red channel.
QgsPointCloudRenderer * clone() const override
Create a deep copy of this renderer.
const QgsContrastEnhancement * redContrastEnhancement() const
Returns the contrast enhancement to use for the red channel.
QSet< QString > usedAttributes(const QgsPointCloudRenderContext &context) const override
Returns a list of attributes required by this renderer.
bool contains(const QgsRange< T > &other) const
Returns true if this range contains another range.
Definition qgsrange.h:108
The class is used as a container of context for various read/write operations on other objects.
A rectangle specified with double values.
bool contains(const QgsRectangle &rect) const
Returns true when rectangle contains other rectangle.
double width() const
Returns the width of the rectangle.
void grow(double delta)
Grows the rectangle in place by the specified amount.
double height() const
Returns the height of the rectangle.
QgsElevationMap * elevationMap() const
Returns the destination elevation map for the render operation.
const QgsRectangle & extent() const
When rendering a map layer, calling this method returns the "clipping" extent for the layer (in the l...
QgsDoubleRange zRange() const
Returns the range of z-values which should be rendered.
bool renderingStopped() const
Returns true if the rendering operation has been stopped and any ongoing rendering should be canceled...
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.