18#include <QElapsedTimer>
42 , mLayerName( layer->name() )
43 , mLayerAttributes( layer->attributes() )
44 , mSubIndexes( layer && layer->dataProvider() ? layer->dataProvider()->subIndexes() : QVector<QgsPointCloudSubIndex>() )
46 , mEnableProfile( context.flags() &
Qgis::RenderContextFlag::RecordProfile )
57 if ( !mSubIndexes.isEmpty() )
68 mZOffset = elevationProps->zOffset();
69 mZScale = elevationProps->zScale();
78 mPreparationTime = timer.elapsed();
83 std::unique_ptr< QgsScopedRuntimeProfile > profile;
86 profile = std::make_unique< QgsScopedRuntimeProfile >( mLayerName, QStringLiteral(
"rendering" ),
layerId() );
87 if ( mPreparationTime > 0 )
91 std::unique_ptr< QgsScopedRuntimeProfile > preparingProfile;
94 preparingProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Preparing render" ), QStringLiteral(
"rendering" ) );
105 if ( !mClippingRegions.empty() )
107 bool needsPainterClipPath =
false;
109 if ( needsPainterClipPath )
113 if ( mRenderer->type() == QLatin1String(
"extent" ) )
116 mRenderer->startRender( context );
118 mRenderer->stopRender( context );
125 if ( mSubIndexes.isEmpty() &&
136 mBlockRenderUpdates =
true;
137 mElapsedTimer.start();
140 mRenderer->startRender( context );
152 QSet< QString > rendererAttributes = mRenderer->usedAttributes( context );
155 for (
const QString &attribute : std::as_const( rendererAttributes ) )
157 if ( mAttributes.
indexOf( attribute ) >= 0 )
160 const int layerIndex = mLayerAttributes.
indexOf( attribute );
161 if ( layerIndex < 0 )
163 QgsMessageLog::logMessage( QObject::tr(
"Required attribute %1 not found in layer" ).arg( attribute ), QObject::tr(
"Point Cloud" ) );
167 mAttributes.
push_back( mLayerAttributes.
at( layerIndex ) );
177 QgsDebugError( QStringLiteral(
"Transformation of extent failed!" ) );
180 preparingProfile.reset();
181 std::unique_ptr< QgsScopedRuntimeProfile > renderingProfile;
182 if ( mEnableProfile )
184 renderingProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Rendering" ), QStringLiteral(
"rendering" ) );
187 bool canceled =
false;
188 if ( mSubIndexes.isEmpty() )
190 canceled = !renderIndex( pc );
194 mSubIndexExtentRenderer->startRender( context );
195 for (
const auto &si : mSubIndexes )
202 if ( !renderExtent.
intersects( si.extent() ) )
205 if ( !pc || !pc->
isValid() || renderExtent.
width() > si.extent().width() )
209 mSubIndexExtentRenderer->renderExtent( si.polygonBounds(), context );
213 canceled = !renderIndex( pc );
216 mSubIndexExtentRenderer->stopRender( context );
219 mRenderer->stopRender( context );
241 const double maximumError = context.renderContext().convertToPainterUnits( mRenderer->maximumScreenError(), mRenderer->maximumScreenErrorUnit() );
245 if ( !context.renderContext().coordinateTransform().isShortCircuited() )
255 QgsDebugError( QStringLiteral(
"Could not transform node extent to map CRS" ) );
256 rootNodeExtentMapCoords = rootNodeExtentLayerCoords;
261 rootNodeExtentMapCoords = rootNodeExtentLayerCoords;
264 const double rootErrorInMapCoordinates = rootNodeExtentMapCoords.
width() / pc->
span();
266 double mapUnitsPerPixel = context.renderContext().mapToPixel().mapUnitsPerPixel();
267 if ( ( rootErrorInMapCoordinates < 0.0 ) || ( mapUnitsPerPixel < 0.0 ) || ( maximumError < 0.0 ) )
272 double rootErrorPixels = rootErrorInMapCoordinates / mapUnitsPerPixel;
273 const QVector<IndexedPointCloudNode> nodes = traverseTree( pc, context.renderContext(), pc->
root(), maximumError, rootErrorPixels );
280 bool canceled =
false;
282 switch ( mRenderer->drawOrder2d() )
287 nodesDrawn += renderNodesSorted( nodes, pc, context, request, canceled, mRenderer->drawOrder2d() );
296 nodesDrawn += renderNodesSync( nodes, pc, context, request, canceled );
301 nodesDrawn += renderNodesAsync( nodes, pc, context, request, canceled );
309 QgsDebugMsgLevel( QStringLiteral(
"totals: %1 nodes | %2 points | %3ms" ).arg( nodesDrawn )
310 .arg( context.pointsRendered() )
311 .arg( t.elapsed() ), 2 );
330 std::unique_ptr<QgsPointCloudBlock> block( pc->
nodeData( n, request ) );
343 mRenderer->renderBlock( block.get(), context );
369 QVector<QgsPointCloudBlockRequest *> blockRequests;
374 for (
int i = 0; i < nodes.size(); ++i )
379 blockRequests.append( blockRequest );
381 [
this, &canceled, &nodesDrawn, &loop, &blockRequests, &context, nStr, blockRequest ]()
383 blockRequests.removeOne( blockRequest );
386 if ( blockRequests.isEmpty() )
389 std::unique_ptr<QgsPointCloudBlock> block( blockRequest->
takeBlock() );
391 blockRequest->deleteLater();
401 QgsDebugError( QStringLiteral(
"Unable to load node %1, error: %2" ).arg( nStr, blockRequest->
errorStr() ) );
412 mRenderer->renderBlock( block.get(), context );
437 std::unique_ptr<QgsPointCloudBlock> block = blockRequest->
takeBlock();
440 blockRequest->deleteLater();
457 QByteArray allByteArrays;
459 QVector<QPair<int, double>> allPairs;
469 std::unique_ptr<QgsPointCloudBlock> block( pc->
nodeData( n, request ) );
477 if ( blockCount == 0 )
479 blockScale = block->scale();
480 blockOffset = block->offset();
485 offsetDifference = blockOffset - block->offset();
488 const char *ptr = block->data();
496 for (
int i = 0; i < block->pointCount(); ++i )
498 allByteArrays.append( ptr + i * recordSize, recordSize );
501 if ( offsetDifference.
x() != 0 )
503 qint32 ix = *
reinterpret_cast< const qint32 *
>( ptr + i * recordSize + context.
xOffset() );
504 ix -= std::lround( offsetDifference.
x() / context.
scale().
x() );
505 const char *xPtr =
reinterpret_cast< const char *
>( &ix );
506 allByteArrays.replace( pointCount * recordSize + context.
xOffset(), 4, QByteArray( xPtr, 4 ) );
508 if ( offsetDifference.
y() != 0 )
510 qint32 iy = *
reinterpret_cast< const qint32 *
>( ptr + i * recordSize + context.
yOffset() );
511 iy -= std::lround( offsetDifference.
y() / context.
scale().
y() );
512 const char *yPtr =
reinterpret_cast< const char *
>( &iy );
513 allByteArrays.replace( pointCount * recordSize + context.
yOffset(), 4, QByteArray( yPtr, 4 ) );
516 qint32 iz = *
reinterpret_cast< const qint32 *
>( ptr + i * recordSize + context.
zOffset() );
517 if ( offsetDifference.
z() != 0 )
519 iz -= std::lround( offsetDifference.
z() / context.
scale().
z() );
520 const char *zPtr =
reinterpret_cast< const char *
>( &iz );
521 allByteArrays.replace( pointCount * recordSize + context.
zOffset(), 4, QByteArray( zPtr, 4 ) );
523 allPairs.append( qMakePair( pointCount,
double( iz ) + block->offset().z() ) );
530 if ( pointCount == 0 )
536 std::sort( allPairs.begin(), allPairs.end(), []( QPair<int, double> a, QPair<int, double> b ) { return a.second < b.second; } );
539 std::sort( allPairs.begin(), allPairs.end(), []( QPair<int, double> a, QPair<int, double> b ) { return a.second > b.second; } );
546 QByteArray sortedByteArray;
547 sortedByteArray.reserve( allPairs.size() );
548 for ( QPair<int, double> pair : allPairs )
549 sortedByteArray.append( allByteArrays.mid( pair.first * recordSize, recordSize ) );
560 context.
setScale( bigBlock->scale() );
564 mRenderer->renderBlock( bigBlock.get(), context );
576 return mRenderer ? mRenderer->type() != QLatin1String(
"extent" ) :
false;
581 mRenderTimeHint = time;
584QVector<IndexedPointCloudNode> QgsPointCloudLayerRenderer::traverseTree(
const QgsPointCloudIndex *pc,
587 double maxErrorPixels,
588 double nodeErrorPixels )
590 QVector<IndexedPointCloudNode> nodes;
609 double childrenErrorPixels = nodeErrorPixels / 2.0;
610 if ( childrenErrorPixels < maxErrorPixels )
613 const QList<IndexedPointCloudNode> children = pc->
nodeChildren( n );
616 nodes += traverseTree( pc, context, nn, maxErrorPixels, childrenErrorPixels );
Represents a indexed point cloud node in octree.
QString toString() const
Encode node to string.
The Qgis class provides global constants for use throughout the application.
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.
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
@ Reverse
Reverse/inverse transform (from destination to source)
static QgsRuntimeProfiler * profiler()
Returns the application runtime profiler.
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.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
bool isCanceled() const
Tells whether the operation has been canceled already.
void canceled()
Internal routines can connect to this signal if they use event loop.
static QPainterPath calculatePainterClipRegion(const QList< QgsMapClippingRegion > ®ions, const QgsRenderContext &context, Qgis::LayerType layerType, bool &shouldClip)
Returns a QPainterPath representing the intersection of clipping regions from context which should be...
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.
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,...
QString layerId() const
Gets access to the ID of the layer rendered by this class.
QgsRenderContext * renderContext()
Returns the render context associated with the renderer.
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.
void finished()
Emitted when the request processing has finished.
std::unique_ptr< QgsPointCloudBlock > takeBlock()
Returns the requested block.
Base class for storing raw data from point cloud nodes.
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.
@ Remote
Remote means it's loaded through a protocol like HTTP.
@ Local
Local means the source is a local file on the machine.
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.
IndexedPointCloudNode root()
Returns root node of the index.
QgsDoubleRange nodeZRange(const IndexedPointCloudNode &node) const
Returns the z range of a node.
virtual std::unique_ptr< QgsPointCloudBlock > nodeData(const IndexedPointCloudNode &n, const QgsPointCloudRequest &request)=0
Returns node data block.
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.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
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.
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
Returns true when rectangle intersects with other rectangle.
double width() const
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.
void setPainterFlagsUsingContext(QPainter *painter=nullptr) const
Sets relevant flags on a destination painter, using the flags and settings currently defined for the ...
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.
void record(const QString &name, double time, const QString &group="startup", const QString &id=QString())
Manually adds a profile event with the given name and total time (in seconds).
Scoped object for saving and restoring a QPainter object's state.
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
double y() const
Returns Y coordinate.
double z() const
Returns Z coordinate.
double x() const
Returns X coordinate.
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)