QGIS API Documentation 4.1.0-Master (d6fb7a379fb)
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
20#include <memory>
21
23#include "qgspointcloudblock.h"
24
25#include <QString>
26
27using namespace Qt::StringLiterals;
28
31
33{
34 return u"rgb"_s;
35}
36
38{
39 auto res = std::make_unique< QgsPointCloudRgbRenderer >();
40 res->mRedAttribute = mRedAttribute;
41 res->mGreenAttribute = mGreenAttribute;
42 res->mBlueAttribute = mBlueAttribute;
43
44 if ( mRedContrastEnhancement )
45 {
46 res->setRedContrastEnhancement( new QgsContrastEnhancement( *mRedContrastEnhancement ) );
47 }
48 if ( mGreenContrastEnhancement )
49 {
50 res->setGreenContrastEnhancement( new QgsContrastEnhancement( *mGreenContrastEnhancement ) );
51 }
52 if ( mBlueContrastEnhancement )
53 {
54 res->setBlueContrastEnhancement( new QgsContrastEnhancement( *mBlueContrastEnhancement ) );
55 }
56
57 copyCommonProperties( res.get() );
58
59 return res.release();
60}
61
63{
64 QgsRectangle visibleExtent = context.renderContext().extent();
65 if ( renderAsTriangles() )
66 {
67 // we need to include also points slightly outside of the visible extent,
68 // otherwise the triangulation may be missing triangles near the edges and corners
69 visibleExtent.grow( std::max( visibleExtent.width(), visibleExtent.height() ) * 0.05 );
70 }
71
72 const char *ptr = block->data();
73 const int count = block->pointCount();
74 const QgsPointCloudAttributeCollection request = block->attributes();
75
76 const std::size_t recordSize = request.pointRecordSize();
77 int redOffset = 0;
78 const QgsPointCloudAttribute *attribute = request.find( mRedAttribute, redOffset );
79 if ( !attribute )
80 return;
81 const QgsPointCloudAttribute::DataType redType = attribute->type();
82
83 int greenOffset = 0;
84 attribute = request.find( mGreenAttribute, greenOffset );
85 if ( !attribute )
86 return;
87 const QgsPointCloudAttribute::DataType greenType = attribute->type();
88
89 int blueOffset = 0;
90 attribute = request.find( mBlueAttribute, blueOffset );
91 if ( !attribute )
92 return;
93 const QgsPointCloudAttribute::DataType blueType = attribute->type();
94
95 const bool useRedContrastEnhancement = mRedContrastEnhancement && mRedContrastEnhancement->contrastEnhancementAlgorithm() != QgsContrastEnhancement::NoEnhancement;
96 const bool useBlueContrastEnhancement = mBlueContrastEnhancement && mBlueContrastEnhancement->contrastEnhancementAlgorithm() != QgsContrastEnhancement::NoEnhancement;
97 const bool useGreenContrastEnhancement = mGreenContrastEnhancement && mGreenContrastEnhancement->contrastEnhancementAlgorithm() != QgsContrastEnhancement::NoEnhancement;
98
99 const bool renderElevation = context.renderContext().elevationMap();
100 const QgsDoubleRange zRange = context.renderContext().zRange();
101 const bool considerZ = !zRange.isInfinite() || renderElevation;
102
103 int rendered = 0;
104 double x = 0;
105 double y = 0;
106 double z = 0;
108 const bool reproject = ct.isValid();
109
110 bool dataDefinedPropertiesActive = dataDefinedProperties().isActive( QgsPointCloudRenderer::Property::Color );
111 for ( int i = 0; i < count; ++i )
112 {
113 if ( context.renderContext().renderingStopped() )
114 {
115 break;
116 }
117
118 if ( considerZ )
119 {
120 // z value filtering is cheapest, if we're doing it...
121 z = pointZ( context, ptr, i );
122 if ( !zRange.contains( z ) )
123 continue;
124 }
125
126 pointXY( context, ptr, i, x, y );
127 if ( visibleExtent.contains( x, y ) )
128 {
129 if ( reproject )
130 {
131 try
132 {
133 ct.transformInPlace( x, y, z );
134 }
135 catch ( QgsCsException & )
136 {
137 continue;
138 }
139 }
140
141 int red = 0;
142 context.getAttribute( ptr, i * recordSize + redOffset, redType, red );
143 int green = 0;
144 context.getAttribute( ptr, i * recordSize + greenOffset, greenType, green );
145 int blue = 0;
146 context.getAttribute( ptr, i * recordSize + blueOffset, blueType, blue );
147
148 //skip if red, green or blue not in displayable range
149 if ( ( useRedContrastEnhancement && !mRedContrastEnhancement->isValueInDisplayableRange( red ) )
150 || ( useGreenContrastEnhancement && !mGreenContrastEnhancement->isValueInDisplayableRange( green ) )
151 || ( useBlueContrastEnhancement && !mBlueContrastEnhancement->isValueInDisplayableRange( blue ) ) )
152 {
153 continue;
154 }
155
156 //stretch color values
157 if ( useRedContrastEnhancement )
158 {
159 red = mRedContrastEnhancement->enhanceContrast( red );
160 }
161 if ( useGreenContrastEnhancement )
162 {
163 green = mGreenContrastEnhancement->enhanceContrast( green );
164 }
165 if ( useBlueContrastEnhancement )
166 {
167 blue = mBlueContrastEnhancement->enhanceContrast( blue );
168 }
169
170 red = std::max( 0, std::min( 255, red ) );
171 green = std::max( 0, std::min( 255, green ) );
172 blue = std::max( 0, std::min( 255, blue ) );
173
174 QColor color( red, green, blue );
175 if ( dataDefinedPropertiesActive )
176 color = colorFromExpression( block, i, color, context );
177
178 if ( renderAsTriangles() )
179 {
180 addPointToTriangulation( x, y, z, color, context );
181
182 // We don't want to render any points if we're rendering triangles and there is no preview painter
183 if ( !context.renderContext().previewRenderPainter() )
184 continue;
185 }
186
187 drawPoint( x, y, color, context );
188 if ( renderElevation )
189 drawPointToElevationMap( x, y, z, context );
190
191 rendered++;
192 }
193 }
194 context.incrementPointsRendered( rendered );
195}
196
197
199{
200 auto r = std::make_unique< QgsPointCloudRgbRenderer >();
201
202 r->setRedAttribute( element.attribute( u"red"_s, u"Red"_s ) );
203 r->setGreenAttribute( element.attribute( u"green"_s, u"Green"_s ) );
204 r->setBlueAttribute( element.attribute( u"blue"_s, u"Blue"_s ) );
205
206 r->restoreCommonProperties( element, context );
207
208 //contrast enhancements
210 const QDomElement redContrastElem = element.firstChildElement( u"redContrastEnhancement"_s );
211 if ( !redContrastElem.isNull() )
212 {
214 redContrastEnhancement->readXml( redContrastElem );
215 r->setRedContrastEnhancement( redContrastEnhancement );
216 }
217
219 const QDomElement greenContrastElem = element.firstChildElement( u"greenContrastEnhancement"_s );
220 if ( !greenContrastElem.isNull() )
221 {
223 greenContrastEnhancement->readXml( greenContrastElem );
224 r->setGreenContrastEnhancement( greenContrastEnhancement );
225 }
226
228 const QDomElement blueContrastElem = element.firstChildElement( u"blueContrastEnhancement"_s );
229 if ( !blueContrastElem.isNull() )
230 {
232 blueContrastEnhancement->readXml( blueContrastElem );
233 r->setBlueContrastEnhancement( blueContrastEnhancement );
234 }
235
236 return r.release();
237}
238
239QDomElement QgsPointCloudRgbRenderer::save( QDomDocument &doc, const QgsReadWriteContext &context ) const
240{
241 QDomElement rendererElem = doc.createElement( u"renderer"_s );
242
243 rendererElem.setAttribute( u"type"_s, u"rgb"_s );
244
245 rendererElem.setAttribute( u"red"_s, mRedAttribute );
246 rendererElem.setAttribute( u"green"_s, mGreenAttribute );
247 rendererElem.setAttribute( u"blue"_s, mBlueAttribute );
248
249 saveCommonProperties( rendererElem, context );
250
251 //contrast enhancement
252 if ( mRedContrastEnhancement )
253 {
254 QDomElement redContrastElem = doc.createElement( u"redContrastEnhancement"_s );
255 mRedContrastEnhancement->writeXml( doc, redContrastElem );
256 rendererElem.appendChild( redContrastElem );
257 }
258 if ( mGreenContrastEnhancement )
259 {
260 QDomElement greenContrastElem = doc.createElement( u"greenContrastEnhancement"_s );
261 mGreenContrastEnhancement->writeXml( doc, greenContrastElem );
262 rendererElem.appendChild( greenContrastElem );
263 }
264 if ( mBlueContrastEnhancement )
265 {
266 QDomElement blueContrastElem = doc.createElement( u"blueContrastEnhancement"_s );
267 mBlueContrastEnhancement->writeXml( doc, blueContrastElem );
268 rendererElem.appendChild( blueContrastElem );
269 }
270
271 return rendererElem;
272}
273
275{
276 QSet<QString> res;
277 res << mRedAttribute << mGreenAttribute << mBlueAttribute;
278 return res;
279}
280
281std::unique_ptr<QgsPreparedPointCloudRendererData> QgsPointCloudRgbRenderer::prepare()
282{
283 auto data = std::make_unique< QgsPointCloudRgbRendererPreparedData >();
284 data->redAttribute = mRedAttribute;
285 if ( mRedContrastEnhancement )
286 data->redContrastEnhancement = std::make_unique<QgsContrastEnhancement>( *mRedContrastEnhancement );
287 data->greenAttribute = mGreenAttribute;
288 if ( mGreenContrastEnhancement )
289 data->greenContrastEnhancement = std::make_unique<QgsContrastEnhancement>( *mGreenContrastEnhancement );
290 data->blueAttribute = mBlueAttribute;
291 if ( mBlueContrastEnhancement )
292 data->blueContrastEnhancement = std::make_unique<QgsContrastEnhancement>( *mBlueContrastEnhancement );
293
294 data->useRedContrastEnhancement = mRedContrastEnhancement && mRedContrastEnhancement->contrastEnhancementAlgorithm() != QgsContrastEnhancement::NoEnhancement;
295 data->useBlueContrastEnhancement = mBlueContrastEnhancement && mBlueContrastEnhancement->contrastEnhancementAlgorithm() != QgsContrastEnhancement::NoEnhancement;
296 data->useGreenContrastEnhancement = mGreenContrastEnhancement && mGreenContrastEnhancement->contrastEnhancementAlgorithm() != QgsContrastEnhancement::NoEnhancement;
297
298 return data;
299}
300
305
307{
308 const QgsPointCloudAttributeCollection request = block->attributes();
309 redOffset = 0;
310 const QgsPointCloudAttribute *attribute = request.find( redAttribute, redOffset );
311 if ( !attribute )
312 return false;
313 redType = attribute->type();
314
315 greenOffset = 0;
316 attribute = request.find( greenAttribute, greenOffset );
317 if ( !attribute )
318 return false;
319 greenType = attribute->type();
320
321 blueOffset = 0;
322 attribute = request.find( blueAttribute, blueOffset );
323 if ( !attribute )
324 return false;
325 blueType = attribute->type();
326 return true;
327}
328
330{
331 const char *ptr = block->data();
332 const int pointRecordSize = block->pointRecordSize();
333
334 int red = 0;
335 QgsPointCloudRenderContext::getAttribute( ptr, i * pointRecordSize + redOffset, redType, red );
336 int green = 0;
337 QgsPointCloudRenderContext::getAttribute( ptr, i * pointRecordSize + greenOffset, greenType, green );
338 int blue = 0;
339 QgsPointCloudRenderContext::getAttribute( ptr, i * pointRecordSize + blueOffset, blueType, blue );
340
341 //skip if red, green or blue not in displayable range
342 if ( ( useRedContrastEnhancement && !redContrastEnhancement->isValueInDisplayableRange( red ) )
343 || ( useGreenContrastEnhancement && !greenContrastEnhancement->isValueInDisplayableRange( green ) )
344 || ( useBlueContrastEnhancement && !blueContrastEnhancement->isValueInDisplayableRange( blue ) ) )
345 {
346 return QColor();
347 }
348
349 //stretch color values
351 {
352 red = redContrastEnhancement->enhanceContrast( red );
353 }
355 {
356 green = greenContrastEnhancement->enhanceContrast( green );
357 }
359 {
360 blue = blueContrastEnhancement->enhanceContrast( blue );
361 }
362
363 red = std::max( 0, std::min( 255, red ) );
364 green = std::max( 0, std::min( 255, green ) );
365 blue = std::max( 0, std::min( 255, blue ) );
366
367 return QColor( red, green, blue );
368}
369
371{
372 return mRedAttribute;
373}
374
376{
377 mRedAttribute = redAttribute;
378}
379
381{
382 return mGreenAttribute;
383}
384
386{
387 mGreenAttribute = greenAttribute;
388}
389
391{
392 return mBlueAttribute;
393}
394
396{
397 mBlueAttribute = blueAttribute;
398}
399
401{
402 return mRedContrastEnhancement.get();
403}
404
406{
407 mRedContrastEnhancement.reset( enhancement );
408}
409
411{
412 return mGreenContrastEnhancement.get();
413}
414
416{
417 mGreenContrastEnhancement.reset( enhancement );
418}
419
421{
422 return mBlueContrastEnhancement.get();
423}
424
426{
427 mBlueContrastEnhancement.reset( enhancement );
428}
@ UnknownDataType
Unknown or unspecified type.
Definition qgis.h:394
Handles contrast enhancement and clipping.
@ NoEnhancement
Default color scaling algorithm, no scaling is applied.
Handles coordinate transforms between two 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:217
bool isInfinite() const
Returns true if the range consists of all possible values.
Definition qgsrange.h:266
A 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...
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.
QColor colorFromExpression(const QgsPointCloudBlock *block, int pointIndex, const QColor &rendererColor, QgsPointCloudRenderContext &context)
Computes color from the expression set, uses expression referenced variables and renderer base color.
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).
const QgsPropertyCollection & dataDefinedProperties() const
Returns the renderer's property collection, used for data defined overrides.
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 isActive(int key) const final
Returns true if the collection contains an active property with the specified key.
bool contains(const QgsRange< T > &other) const
Returns true if this range contains another range.
Definition qgsrange.h:138
A container for the context for various read/write operations on objects.
A rectangle specified with double values.
bool contains(const QgsRectangle &rect) const
Returns true when rectangle contains other rectangle.
void grow(double delta)
Grows the rectangle in place by the specified amount.
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...
QPainter * previewRenderPainter()
Returns the const destination QPainter for temporary in-progress preview renders.
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.