18#include <QElapsedTimer>
42 , mLayerAttributes( layer->attributes() )
60 mZOffset = elevationProps->zOffset();
61 mZScale = elevationProps->zScale();
77 bool applyEdl = mRenderer && mRenderer->eyeDomeLightingEnabled();
79 if ( QImage *painterImage =
dynamic_cast<QImage *
>( painter->device() ) )
88 if ( !mClippingRegions.empty() )
90 bool needsPainterClipPath =
false;
92 if ( needsPainterClipPath )
96 if ( mRenderer->type() == QLatin1String(
"extent" ) )
99 mRenderer->startRender( context );
101 mRenderer->stopRender( context );
118 mBlockRenderUpdates =
true;
119 mElapsedTimer.start();
122 mRenderer->startRender( context );
134 QSet< QString > rendererAttributes = mRenderer->usedAttributes( context );
137 for (
const QString &attribute : std::as_const( rendererAttributes ) )
139 if ( mAttributes.
indexOf( attribute ) >= 0 )
142 const int layerIndex = mLayerAttributes.
indexOf( attribute );
143 if ( layerIndex < 0 )
145 QgsMessageLog::logMessage( QObject::tr(
"Required attribute %1 not found in layer" ).arg( attribute ), QObject::tr(
"Point Cloud" ) );
149 mAttributes.
push_back( mLayerAttributes.
at( layerIndex ) );
175 QgsDebugMsg( QStringLiteral(
"Could not transform node extent to map CRS" ) );
176 rootNodeExtentMapCoords = rootNodeExtentLayerCoords;
181 rootNodeExtentMapCoords = rootNodeExtentLayerCoords;
184 const double rootErrorInMapCoordinates = rootNodeExtentMapCoords.
width() / pc->
span();
187 if ( ( rootErrorInMapCoordinates < 0.0 ) || ( mapUnitsPerPixel < 0.0 ) || ( maximumError < 0.0 ) )
189 QgsDebugMsg( QStringLiteral(
"invalid screen error" ) );
193 double rootErrorPixels = rootErrorInMapCoordinates / mapUnitsPerPixel;
194 const QVector<IndexedPointCloudNode> nodes = traverseTree( pc, context.
renderContext(), pc->
root(), maximumError, rootErrorPixels );
201 bool canceled =
false;
203 switch ( mRenderer->drawOrder2d() )
208 nodesDrawn += renderNodesSorted( nodes, pc, context, request, canceled, mRenderer->drawOrder2d() );
215 case QgsPointCloudIndex::AccessType::Local:
217 nodesDrawn += renderNodesSync( nodes, pc, context, request, canceled );
220 case QgsPointCloudIndex::AccessType::Remote:
222 nodesDrawn += renderNodesAsync( nodes, pc, context, request, canceled );
230 QgsDebugMsgLevel( QStringLiteral(
"totals: %1 nodes | %2 points | %3ms" ).arg( nodesDrawn )
232 .arg( t.elapsed() ), 2 );
237 mRenderer->stopRender( context );
241 if ( QImage *drawnImage =
dynamic_cast<QImage *
>( painter->device() ) )
243 double strength = mRenderer->eyeDomeLightingStrength();
245 mRenderer->eyeDomeLightingDistance(), mRenderer->eyeDomeLightingDistanceUnit() );
246 int distance =
static_cast<int>( std::round( distanceDouble ) );
266 std::unique_ptr<QgsPointCloudBlock> block( pc->
nodeData( n, request ) );
279 mRenderer->renderBlock( block.get(), context );
305 QVector<QgsPointCloudBlockRequest *> blockRequests;
310 for (
int i = 0; i < nodes.size(); ++i )
315 blockRequests.append( blockRequest );
317 [
this, &canceled, &nodesDrawn, &loop, &blockRequests, &context, nStr, blockRequest ]()
319 blockRequests.removeOne( blockRequest );
322 if ( blockRequests.isEmpty() )
325 std::unique_ptr<QgsPointCloudBlock> block( blockRequest->
block() );
327 blockRequest->deleteLater();
337 QgsDebugMsg( QStringLiteral(
"Unable to load node %1, error: %2" ).arg( nStr, blockRequest->
errorStr() ) );
348 mRenderer->renderBlock( block.get(), context );
373 delete blockRequest->
block();
374 blockRequest->deleteLater();
391 QByteArray allByteArrays;
393 QVector<QPair<int, double>> allPairs;
403 std::unique_ptr<QgsPointCloudBlock> block( pc->
nodeData( n, request ) );
411 if ( blockCount == 0 )
413 blockScale = block->scale();
414 blockOffset = block->offset();
419 offsetDifference = blockOffset - block->offset();
422 const char *ptr = block->data();
430 for (
int i = 0; i < block->pointCount(); ++i )
432 allByteArrays.append( ptr + i * recordSize, recordSize );
435 if ( offsetDifference.
x() != 0 )
437 qint32 ix = *
reinterpret_cast< const qint32 *
>( ptr + i * recordSize + context.
xOffset() );
438 ix -= std::lround( offsetDifference.
x() / context.
scale().
x() );
439 const char *xPtr =
reinterpret_cast< const char *
>( &ix );
440 allByteArrays.replace( pointCount * recordSize + context.
xOffset(), 4, QByteArray( xPtr, 4 ) );
442 if ( offsetDifference.
y() != 0 )
444 qint32 iy = *
reinterpret_cast< const qint32 *
>( ptr + i * recordSize + context.
yOffset() );
445 iy -= std::lround( offsetDifference.
y() / context.
scale().
y() );
446 const char *yPtr =
reinterpret_cast< const char *
>( &iy );
447 allByteArrays.replace( pointCount * recordSize + context.
yOffset(), 4, QByteArray( yPtr, 4 ) );
450 qint32 iz = *
reinterpret_cast< const qint32 *
>( ptr + i * recordSize + context.
zOffset() );
451 if ( offsetDifference.
z() != 0 )
453 iz -= std::lround( offsetDifference.
z() / context.
scale().
z() );
454 const char *zPtr =
reinterpret_cast< const char *
>( &iz );
455 allByteArrays.replace( pointCount * recordSize + context.
zOffset(), 4, QByteArray( zPtr, 4 ) );
457 allPairs.append( qMakePair( pointCount,
double( iz ) + block->offset().z() ) );
464 if ( pointCount == 0 )
470 std::sort( allPairs.begin(), allPairs.end(), []( QPair<int, double> a, QPair<int, double> b ) { return a.second < b.second; } );
473 std::sort( allPairs.begin(), allPairs.end(), []( QPair<int, double> a, QPair<int, double> b ) { return a.second > b.second; } );
480 QByteArray sortedByteArray;
481 sortedByteArray.reserve( allPairs.size() );
482 for ( QPair<int, double> pair : allPairs )
483 sortedByteArray.append( allByteArrays.mid( pair.first * recordSize, recordSize ) );
494 context.
setScale( bigBlock->scale() );
498 mRenderer->renderBlock( bigBlock.get(), context );
510 return mRenderer ? mRenderer->type() != QLatin1String(
"extent" ) :
false;
515 mRenderTimeHint = time;
518QVector<IndexedPointCloudNode> QgsPointCloudLayerRenderer::traverseTree(
const QgsPointCloudIndex *pc,
521 double maxErrorPixels,
522 double nodeErrorPixels )
524 QVector<IndexedPointCloudNode> nodes;
543 double childrenErrorPixels = nodeErrorPixels / 2.0;
544 if ( childrenErrorPixels < maxErrorPixels )
547 const QList<IndexedPointCloudNode> children = pc->
nodeChildren( n );
550 nodes += traverseTree( pc, context, nn, maxErrorPixels, childrenErrorPixels );
Represents a indexed point cloud node in octree.
QString toString() const
Encode node to string.
PointCloudDrawOrder
Pointcloud rendering order for 2d views.
@ BottomToTop
Draw points with larger Z values last.
@ Default
Draw points in the order they are stored.
@ TopToBottom
Draw points with larger Z values first.
Custom exception class for Coordinate Reference System related exceptions.
QgsRange which stores a range of double values.
bool isInfinite() const
Returns true if the range consists of all possible values.
Stores digital elevation model in a raster image which may get updated as a part of map layer renderi...
void applyEyeDomeLighting(QImage &img, int distance, float strength, float rendererScale)
Applies eye dome lighting effect to the given image.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
void canceled()
Internal routines can connect to this signal if they use event loop.
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
static QList< QgsMapClippingRegion > collectClippingRegionsForLayer(const QgsRenderContext &context, const QgsMapLayer *layer)
Collects the list of map clipping regions from a context which apply to a map layer.
static QPainterPath calculatePainterClipRegion(const QList< QgsMapClippingRegion > ®ions, const QgsRenderContext &context, QgsMapLayerType layerType, bool &shouldClip)
Returns a QPainterPath representing the intersection of clipping regions from context which should be...
Base class for utility classes that encapsulate information necessary for rendering of map layers.
bool mReadyToCompose
The flag must be set to false in renderer's constructor if wants to use the smarter map redraws funct...
static constexpr int MAX_TIME_TO_USE_CACHED_PREVIEW_IMAGE
Maximum time (in ms) to allow display of a previously cached preview image while rendering layers,...
QgsRenderContext * renderContext()
Returns the render context associated with the renderer.
double mapUnitsPerPixel() const
Returns the current map units per pixel.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Collection of point cloud attributes.
void push_back(const QgsPointCloudAttribute &attribute)
Adds extra attribute.
const QgsPointCloudAttribute & at(int index) const
Returns the attribute at the specified index.
QVector< QgsPointCloudAttribute > attributes() const
Returns all attributes.
int indexOf(const QString &name) const
Returns the index of the attribute with the specified name.
Attribute for point cloud data pair of name and size in bytes.
Base class for handling loading QgsPointCloudBlock asynchronously.
QString errorStr()
Returns the error message string of the request.
QgsPointCloudBlock * block()
Returns the requested block.
void finished()
Emitted when the request processing has finished.
Base class for storing raw data from point cloud nodes.
Represents packaged data bounds.
virtual QgsPointCloudIndex * index() const
Returns the point cloud index associated with the provider.
virtual QgsGeometry polygonBounds() const
Returns the polygon bounds of the layer.
A renderer for 2d visualisation of point clouds which shows the dataset's extents using a fill symbol...
Represents a indexed point clouds data in octree.
int span() const
Returns the number of points in one direction in a single node.
virtual qint64 nodePointCount(const IndexedPointCloudNode &n) const
Returns the number of points of a given node n.
QgsRectangle nodeMapExtent(const IndexedPointCloudNode &node) const
Returns the extent of a node in map coordinates.
virtual QgsPointCloudBlockRequest * asyncNodeData(const IndexedPointCloudNode &n, const QgsPointCloudRequest &request)=0
Returns a handle responsible for loading a node data block.
virtual QList< IndexedPointCloudNode > nodeChildren(const IndexedPointCloudNode &n) const
Returns all children of node.
QgsVector3D offset() const
Returns offset.
QgsVector3D scale() const
Returns scale.
virtual AccessType accessType() const =0
Returns the access type of the data If the access type is Remote, data will be fetched from an HTTP s...
virtual bool isValid() const =0
Returns whether index is loaded and valid.
virtual QgsPointCloudBlock * nodeData(const IndexedPointCloudNode &n, const QgsPointCloudRequest &request)=0
Returns node data block.
IndexedPointCloudNode root()
Returns root node of the index.
QgsDoubleRange nodeZRange(const IndexedPointCloudNode &node) const
Returns the z range of a node.
Point cloud layer specific subclass of QgsMapLayerElevationProperties.
~QgsPointCloudLayerRenderer()
bool forceRasterRender() const override
Returns true if the renderer must be rendered to a raster paint device (e.g.
QgsPointCloudLayerRenderer(QgsPointCloudLayer *layer, QgsRenderContext &context)
Ctor.
void setLayerRenderingTimeHint(int time) override
Sets approximate render time (in ms) for the layer to render.
bool render() override
Do the rendering (based on data stored in the class).
Represents a map layer supporting display of point clouds.
QgsMapLayerElevationProperties * elevationProperties() override
Returns the layer's elevation properties.
QgsPointCloudRenderer * renderer()
Returns the 2D renderer for the point cloud.
QgsPointCloudDataProvider * dataProvider() override
Returns the layer's data provider, it may be nullptr.
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.
QgsVector3D offset() const
Returns the offset of the layer's int32 coordinates compared to CRS coords.
void setElevationMap(QgsElevationMap *elevationMap)
Sets elevation map that will be used to record elevation of rendered points.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
long pointsRendered() const
Returns the total number of points rendered.
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.
QgsElevationMap * elevationMap()
Returns elevation map.
QgsVector3D scale() const
Returns the scale of the layer's int32 coordinates compared to CRS coords.
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.
void setAttributes(const QgsPointCloudAttributeCollection &attributes)
Sets the attributes associated with the rendered block.
virtual QgsPointCloudRenderer * clone() const =0
Create a deep copy of this renderer.
Point cloud data request.
void setAttributes(const QgsPointCloudAttributeCollection &attributes)
Set attributes filter in the request.
bool overlaps(const QgsRange< T > &other) const
Returns true if this range overlaps another range.
T lower() const
Returns the lower bound of the range.
T upper() const
Returns the upper bound of the range.
A rectangle specified with double values.
bool intersects(const QgsRectangle &rect) const SIP_HOLDGIL
Returns true when rectangle intersects with other rectangle.
double width() const SIP_HOLDGIL
Returns the width of the rectangle.
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
double rendererScale() const
Returns the renderer map scale.
void setPainterFlagsUsingContext(QPainter *painter=nullptr) const
Sets relevant flags on a destination painter, using the flags and settings currently defined for the ...
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
const QgsRectangle & extent() const
When rendering a map layer, calling this method returns the "clipping" extent for the layer (in the l...
const QgsMapToPixel & mapToPixel() const
Returns the context's map to pixel transform, which transforms between map coordinates and device coo...
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.
Scoped object for saving and restoring a QPainter object's state.
double y() const
Returns Y coordinate.
double z() const
Returns Z coordinate.
double x() const
Returns X coordinate.
@ VectorTileLayer
Vector tile layer. Added in QGIS 3.14.
#define QgsDebugMsgLevel(str, level)