40#include <Qt3DCore/QAttribute>
41#include <Qt3DRender/QGraphicsApiFilter>
42#include <Qt3DRender/QShaderProgram>
43#include <Qt3DRender/QTechnique>
44#include <QtConcurrentRun>
46#include "moc_qgspointcloudlayerchunkloader_p.cpp"
48using namespace Qt::StringLiterals;
52QgsPointCloudLayerChunkLoader::QgsPointCloudLayerChunkLoader(
55 : mRenderContext( context )
56 , mCoordinateTransform( coordinateTransform )
57 , mPointCloudIndex( std::move( pc ) )
58 , mZValueScale( zValueScale )
59 , mZValueOffset( zValueOffset )
60 , mPointBudget( pointBudget )
62 mSymbol.reset( symbol );
68 mExtent = mCoordinateTransform.transformBoundingBox( mRenderContext.extent(), Qgis::TransformDirection::Reverse );
78QFuture<QgsChunkLoaderResult> QgsPointCloudLayerChunkLoader::loadChunk( QgsChunkNode *node )
80 const QgsChunkNodeId
id = node->tileId();
86 auto context = std::make_shared<QgsPointCloud3DRenderContext>( mRenderContext, mCoordinateTransform, std::unique_ptr<QgsPointCloud3DSymbol>( symbol ), mZValueScale, mZValueOffset );
91 const QgsChunkNodeId nodeId = node->tileId();
94 Q_ASSERT( pc.
hasNode( pcNode ) );
98 std::shared_ptr<QgsPointCloud3DSymbolHandler> handler;
99 if ( context->symbol()->symbolType() ==
"single-color"_L1 )
100 handler = std::make_shared<QgsSingleColorPointCloud3DSymbolHandler>();
101 else if ( context->symbol()->symbolType() ==
"color-ramp"_L1 )
102 handler = std::make_shared<QgsColorRampPointCloud3DSymbolHandler>();
103 else if ( context->symbol()->symbolType() ==
"rgb"_L1 )
104 handler = std::make_shared<QgsRGBPointCloud3DSymbolHandler>();
105 else if ( context->symbol()->symbolType() ==
"classification"_L1 )
107 handler = std::make_shared<QgsClassificationPointCloud3DSymbolHandler>();
112 const QgsBox3D box3D = node->box3D();
113 return QtConcurrent::run( [pc = std::move( pc ), node, pcNode, box3D, context, handler]( QPromise<QgsChunkLoaderResult> &promise )
mutable {
114 const QgsScopedEvent e( u
"3D"_s, u
"PC chunk load"_s );
116 if ( promise.isCanceled() )
119 promise.addResult( QgsChunkLoaderResult::sEmpty );
123 handler->processNode( pc, pcNode, *context );
125 if ( promise.isCanceled() )
128 promise.addResult( QgsChunkLoaderResult::sEmpty );
132 if ( context->symbol()->renderAsTriangles() )
133 handler->triangulate( pc, pcNode, *context, box3D );
135 promise.addResult( QgsChunkLoaderResult { [node, handler, context, pc]( Qt3DCore::QEntity *parent ) -> Qt3DCore::QEntity * {
137 const QgsChunkNodeId nodeId = node->tileId();
139 Q_ASSERT( pc.
hasNode( pcNode ) );
141 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity( parent );
142 handler->finalize( entity, *context );
148int QgsPointCloudLayerChunkLoader::primitivesCount( QgsChunkNode *node )
const
150 const QgsChunkNodeId
id = node->tileId();
152 Q_ASSERT( mPointCloudIndex.hasNode( n ) );
153 return mPointCloudIndex.getNode( n ).pointCount();
156QFuture<void> QgsPointCloudLayerChunkLoader::fetchHierarchyForNode(
const QgsPointCloudNodeId &nodeId )
158 return QtConcurrent::run( [index = mPointCloudIndex, nodeId] {
162 ( void ) index.hasNode( nodeId );
163 const QVector<QgsPointCloudNodeId> children = nodeId.
childrenNodes();
166 ( void ) index.hasNode( child );
168 const QVector<QgsPointCloudNodeId> grandchildren = child.childrenNodes();
171 ( void ) index.hasNode( grandChild );
186 extentMin3D = extentTransform.
transform( extentMin3D );
187 extentMax3D = extentTransform.
transform( extentMax3D );
191 QgsDebugError( u
"Error transforming node bounds coordinate"_s );
193 return QgsBox3D( extentMin3D.x(), extentMin3D.y(), extentMin3D.z(), extentMax3D.x(), extentMax3D.y(), extentMax3D.z() );
197QgsChunkNode *QgsPointCloudLayerChunkLoader::createRootNode()
const
199 const QgsPointCloudNode pcNode = mPointCloudIndex.getNode( mPointCloudIndex.root() );
201 QgsBox3D rootNodeBox3D = nodeBoundsToBox3D( rootNodeBounds, mCoordinateTransform, mZValueOffset, mZValueScale );
203 const float error = pcNode.
error();
204 QgsChunkNode *node =
new QgsChunkNode( QgsChunkNodeId( 0, 0, 0, 0 ), rootNodeBox3D, error );
209QFuture<QVector<QgsChunkNode *>> QgsPointCloudLayerChunkLoader::createChildren( QgsChunkNode *node )
211 const QgsChunkNodeId
id = node->tileId();
213 if ( mPointCloudIndex.needsHierarchyFetching( n ) )
214 return fetchHierarchyForNode( n ).then(
this, [
this, node]() {
return createChildrenInner( node ); } );
216 return QtFuture::makeReadyValueFuture( createChildrenInner( node ) );
219QVector<QgsChunkNode *> QgsPointCloudLayerChunkLoader::createChildrenInner( QgsChunkNode *node )
221 QVector<QgsChunkNode *> children;
222 const QgsChunkNodeId nodeId = node->tileId();
223 const float childError = node->error() / 2;
226 const QVector<QgsPointCloudNodeId> childrenPcIds = pcId.childrenNodes();
229 const QgsChunkNodeId childId( childPcId.d(), childPcId.x(), childPcId.y(), childPcId.z() );
230 if ( !mPointCloudIndex.hasNode( childPcId ) )
237 QgsBox3D childBox3D = nodeBoundsToBox3D( childBounds, mCoordinateTransform, mZValueOffset, mZValueScale );
239 QgsChunkNode *child =
new QgsChunkNode( childId, childBox3D, childError, node );
249static QgsChunkNode *findChunkNodeFromNodeId( QgsChunkNode *rootNode,
QgsPointCloudNodeId nodeId )
252 QVector<QgsPointCloudNodeId> parentIds;
253 while ( nodeId.
d() > 0 )
260 QgsChunkNode *chunk = rootNode;
261 while ( !parentIds.empty() )
264 QgsChunkNodeId childNodeId( p.
d(), p.
x(), p.
y(), p.
z() );
266 if ( !chunk->hasChildrenPopulated() )
269 QgsChunkNode *chunkChild =
nullptr;
270 QgsChunkNode *
const *children = chunk->children();
271 for (
int i = 0; i < chunk->childCount(); ++i )
273 if ( children[i]->tileId() == childNodeId )
275 chunkChild = children[i];
279 Q_ASSERT( chunkChild );
286QgsPointCloudLayerChunkedEntity::QgsPointCloudLayerChunkedEntity(
289 const int indexPosition,
292 float maximumScreenSpaceError,
293 bool showBoundingBoxes,
298 : QgsChunkedEntity( map, maximumScreenSpaceError, new QgsPointCloudLayerChunkLoader(
Qgs3DRenderContext::fromMapSettings( map ), coordinateTransform, resolveIndex( pcl, indexPosition ), symbol, zValueScale, zValueOffset, pointBudget ), true, pointBudget )
300 , mIndexPosition( indexPosition )
302 setShowBoundingBoxes( showBoundingBoxes );
307 if ( ( pcl->
isVpc() && indexPosition >= 0 ) || ( !pcl->
isVpc() ) )
312 if ( indexPosition != mIndexPosition )
315 QgsChunkNode *node = findChunkNodeFromNodeId( mRootNode.get(), n );
318 updateNodes( QList<QgsChunkNode *>() << node );
325QgsPointCloudLayerChunkedEntity::~QgsPointCloudLayerChunkedEntity()
333 if ( indexPosition >= 0 && pcl->
isVpc() )
334 return pcl->
subIndexes().at( indexPosition ).index();
335 else if ( indexPosition == -1 && !pcl->
isVpc() )
337 else if ( indexPosition < -1 && pcl->isVpc() )
339 const int ovId = -indexPosition - 2;
346void QgsPointCloudLayerChunkedEntity::updateIndex()
348 if ( mLayer->isVpc() )
350 if ( mIndexPosition >= 0 )
351 static_cast<QgsPointCloudLayerChunkLoader *
>( mChunkLoader )->mPointCloudIndex = mLayer->subIndexes().at( mIndexPosition ).index();
354 static_cast<QgsPointCloudLayerChunkLoader *
>( mChunkLoader )->mPointCloudIndex = mLayer->index();
357QList<QgsRayCastHit> QgsPointCloudLayerChunkedEntity::rayIntersection(
const QgsRay3D &ray,
const QgsRayCastContext &context )
const
359 QList<QgsRayCastHit> result;
360 QgsPointCloudLayerChunkLoader *loader =
static_cast<QgsPointCloudLayerChunkLoader *
>( mChunkLoader );
368 const QgsVector3D rayOriginMapCoords = loader->mRenderContext.worldToMapCoordinates( ray.
origin() );
369 const QgsVector3D pointMapCoords = loader->mRenderContext.worldToMapCoordinates( ray.
point( ray.
origin().length() ) );
370 QgsVector3D rayDirectionMapCoords = pointMapCoords - rayOriginMapCoords;
379 const QgsVector3D adjustedRayOrigin =
QgsVector3D( rayOriginMapCoords.x(), rayOriginMapCoords.y(), ( rayOriginMapCoords.z() - loader->mZValueOffset ) / loader->mZValueScale );
380 QgsVector3D adjustedRayDirection =
QgsVector3D( rayDirectionMapCoords.
x(), rayDirectionMapCoords.
y(), rayDirectionMapCoords.
z() / loader->mZValueScale );
389 double minDist = -1.;
390 const QList<QgsChunkNode *> activeNodes = this->activeNodes();
391 for ( QgsChunkNode *node : activeNodes )
393 const QgsChunkNodeId
id = node->tileId();
403 std::unique_ptr<QgsPointCloudBlock> block( index.
nodeData( n, request ) );
410 const char *ptr = block->data();
413 int xOffset = 0, yOffset = 0, zOffset = 0;
417 for (
int i = 0; i < block->pointCount(); ++i )
420 QgsPointCloudAttribute::getPointXYZ( ptr, i, recordSize, xOffset, xType, yOffset, yType, zOffset, zType, blockScale, blockOffset, x, y, z );
425 QgsVector3D vectorToPoint = point - adjustedRayOrigin;
434 const double d1 = projPoint.
distance( adjustedRayOrigin );
435 const double d2 = projPoint.
distance( point );
436 const double angle = std::atan2( d2, d1 );
438 if ( angle > limitAngle )
441 const double dist = rayOriginMapCoords.distance( point );
443 if ( minDist < 0 || dist < minDist )
454 pointAttr[u
"X"_s] = x;
455 pointAttr[u
"Y"_s] = y;
456 pointAttr[u
"Z"_s] = z;
464 result.append( hit );
@ Geocentric
Geocentric CRS.
@ Additive
When tile is refined its content should be used alongside its children simultaneously.
@ Replacement
When tile is refined then its children should be used in place of itself.
Rendering context for preparation of 3D entities.
QgsCoordinateReferenceSystem crs() const
Returns the coordinate reference system used in the 3D scene.
static QgsAABB mapToWorldExtent(const QgsRectangle &extent, double zMin, double zMax, const QgsVector3D &mapOrigin)
Converts map extent to axis aligned bounding box in 3D world coordinates.
Axis-aligned bounding box - in world coords.
A 3-dimensional box composed of x, y, z coordinates.
double yMaximum() const
Returns the maximum y value.
double xMinimum() const
Returns the minimum x value.
double zMaximum() const
Returns the maximum z value.
double xMaximum() const
Returns the maximum x value.
QgsRectangle toRectangle() const
Converts the box to a 2D rectangle.
double zMinimum() const
Returns the minimum z value.
double yMinimum() const
Returns the minimum y value.
3D symbol that draws point cloud geometries as 3D objects using classification of the dataset.
QgsPointCloudCategoryList getFilteredOutCategories() const
Gets the list of categories of the classification that should not be rendered.
Qgis::CrsType type() const
Returns the type of the CRS.
Custom exception class for Coordinate Reference System related exceptions.
void editingStopped()
Emitted when edited changes have been successfully written to the data provider.
void editingStarted()
Emitted when editing on this layer has started.
3D symbol that draws point cloud geometries as 3D objects.
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.
QVector< QgsPointCloudAttribute > attributes() const
Returns all attributes.
DataType
Systems of unit measurement.
static void getPointXYZ(const char *ptr, int i, std::size_t pointRecordSize, int xOffset, QgsPointCloudAttribute::DataType xType, int yOffset, QgsPointCloudAttribute::DataType yType, int zOffset, QgsPointCloudAttribute::DataType zType, const QgsVector3D &indexScale, const QgsVector3D &indexOffset, double &x, double &y, double &z)
Retrieves the x, y, z values for the point at index i.
static QVariantMap getAttributeMap(const char *data, std::size_t recordOffset, const QgsPointCloudAttributeCollection &attributeCollection)
Retrieves all the attributes of a point.
DataType type() const
Returns the data type.
Smart pointer for QgsAbstractPointCloudIndex.
std::unique_ptr< QgsPointCloudBlock > nodeData(QgsPointCloudNodeId n, const QgsPointCloudRequest &request)
Returns node data block.
bool hasNode(QgsPointCloudNodeId id) const
Returns whether the octree contain given node.
QgsPointCloudAttributeCollection attributes() const
Returns all attributes that are stored in the file.
Represents a map layer supporting display of point clouds.
void chunkAttributeValuesChanged(QgsPointCloudNodeId n, const int position)
Emitted when a node gets some attribute values of some points changed.
QgsPointCloudIndex index() const
Returns the point cloud index associated with the layer.
bool isVpc() const
Returns whether the layer has a virtual point cloud data provider or not.
QVector< QgsPointCloudIndex > overviews() const
Returns a list of all overview point cloud indexes associated with the layer (only if the layer has a...
QVector< QgsPointCloudSubIndex > subIndexes() const
Returns point cloud indexes associated with the layer (only if the layer has a virtual point cloud da...
bool supportsEditing() const override
Returns whether the layer supports editing or not.
Represents an indexed point cloud node's position in octree.
QgsPointCloudNodeId parentNode() const
Returns the parent of the node.
QVector< QgsPointCloudNodeId > childrenNodes() const
Returns the node's 8 direct child nodes.
Keeps metadata for an indexed point cloud node.
float error() const
Returns node's error in map units (used to determine in whether the node has enough detail for the cu...
QgsBox3D bounds() const
Returns node's bounding cube in CRS coords.
Point cloud data request.
void setAttributes(const QgsPointCloudAttributeCollection &attributes)
Set attributes filter in the request.
A representation of a ray in 3D.
QVector3D origin() const
Returns the origin of the ray.
QVector3D point(float distance) const
Returns the point along the ray with the specified distance from the ray's origin.
Responsible for defining parameters of the ray casting operations in 3D map canvases.
float angleThreshold() const
Sets an angle threshold in degrees for ray intersections, effectively turning a ray into a cone.
bool singleResult() const
Returns whether to fetch only the closest hit for each layer or entity type.
Contains details about the ray intersecting entities when ray casting in a 3D map canvas.
void setProperties(const QVariantMap &attributes)
Sets the point cloud point attributes, empty map if hit was not on a point cloud point.
void setMapCoordinates(const QgsVector3D &point)
Sets the hit point position in 3d map coordinates.
void setDistance(double distance)
Sets the hit's distance from the ray's origin.
bool intersects(const QgsRectangle &rect) const
Returns true when rectangle intersects with other rectangle.
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
double y() const
Returns Y coordinate.
double z() const
Returns Z coordinate.
double distance(const QgsVector3D &other) const
Returns the distance with the other QgsVector3D.
static double dotProduct(const QgsVector3D &v1, const QgsVector3D &v2)
Returns the dot product of two vectors.
double x() const
Returns X coordinate.
void normalize()
Normalizes the current vector in place.
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored).
bool rayBoxIntersection(const QgsRay3D &ray, const QgsAABB &nodeBbox)
Tests whether an axis aligned box is intersected by a ray.
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)
#define QGIS_CHECK_MAIN_THREAD_ACCESS