34#include <Qt3DRender/QGeometryRenderer>
35#include <QtConcurrentRun>
37#include "moc_qgstiledscenechunkloader_p.cpp"
39using namespace Qt::StringLiterals;
43size_t qHash(
const QgsChunkNodeId &n )
58 return bounds.
width() > 1e5 || bounds.
height() > 1e5 || bounds.
depth() > 1e5;
63QgsTiledSceneChunkLoader::QgsTiledSceneChunkLoader( QgsChunkNode *node,
const QgsTiledSceneIndex &index,
const QgsTiledSceneChunkLoaderFactory &factory,
double zValueScale,
double zValueOffset )
64 : QgsChunkLoader( node )
66 , mZValueScale( zValueScale )
67 , mZValueOffset( zValueOffset )
72void QgsTiledSceneChunkLoader::start()
74 QgsChunkNode *node = chunk();
76 mFutureWatcher =
new QFutureWatcher<void>(
this );
77 connect( mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsChunkQueueJob::finished );
81 const QgsChunkNodeId tileId = node->tileId();
82 const QgsVector3D chunkOrigin = node->box3D().center();
84 const QFuture<void> future = QtConcurrent::run( [
this, tileId, boundsTransform, chunkOrigin, isGlobe] {
90 if ( !isGlobe && hasLargeBounds( tile, boundsTransform ) )
93 QString uri = tile.
resources().value( u
"content"_s ).toString();
101 uri = tile.
baseUrl().resolved( uri ).toString();
102 QByteArray content = mFactory.mIndex.retrieveContent( uri );
103 if ( content.isEmpty() )
110 QgsGltf3DUtils::EntityTransform entityTransform;
112 entityTransform.chunkOriginTargetCrs = chunkOrigin;
113 entityTransform.ecefToTargetCrs = &mFactory.mBoundsTransform;
114 entityTransform.zValueScale = mZValueScale;
115 entityTransform.zValueOffset = mZValueOffset;
118 const QString &format = tile.
metadata().value( u
"contentFormat"_s ).value<QString>();
120 if ( format ==
"quantizedmesh"_L1 )
125 qmTile.removeDegenerateTriangles();
126 tinygltf::Model model = qmTile.toGltf(
true, 100 );
127 mEntity = QgsGltf3DUtils::parsedGltfToEntity( model, entityTransform, uri, &errors );
131 errors.append( u
"Failed to parse tile from '%1'"_s.arg( uri ) );
134 else if ( format ==
"cesiumtiles"_L1 )
137 if ( tileContent.
gltf.isEmpty() )
139 entityTransform.tileTransform.translate( tileContent.
rtcCenter );
140 mEntity = QgsGltf3DUtils::gltfToEntity( tileContent.
gltf, entityTransform, uri, &errors );
142 else if ( format ==
"draco"_L1 )
144 QgsGltfUtils::I3SNodeContext i3sContext;
145 i3sContext.initFromTile( tile, mFactory.mLayerCrs, mFactory.mBoundsTransform.sourceCrs(), mFactory.mRenderContext.transformContext() );
147 QString dracoLoadError;
148 tinygltf::Model model;
149 if ( !QgsGltfUtils::loadDracoModel( content, i3sContext, model, &dracoLoadError ) )
151 errors.append( dracoLoadError );
155 mEntity = QgsGltf3DUtils::parsedGltfToEntity( model, entityTransform, QString(), &errors );
161 if ( !errors.isEmpty() )
168 QgsGeoTransform *transform =
new QgsGeoTransform;
169 transform->setGeoTranslation( chunkOrigin );
170 mEntity->addComponent( transform );
177 mFutureWatcher->setFuture( future );
180QgsTiledSceneChunkLoader::~QgsTiledSceneChunkLoader()
182 if ( !mFutureWatcher->isFinished() )
184 disconnect( mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsChunkQueueJob::finished );
185 mFutureWatcher->waitForFinished();
189Qt3DCore::QEntity *QgsTiledSceneChunkLoader::createEntity( Qt3DCore::QEntity *parent )
192 mEntity->setParent( parent );
198QgsTiledSceneChunkLoaderFactory::QgsTiledSceneChunkLoaderFactory(
206 : mRenderContext( context )
208 , mZValueScale( zValueScale )
209 , mZValueOffset( zValueOffset )
210 , mLayerCrs( layerCrs )
215QgsChunkLoader *QgsTiledSceneChunkLoaderFactory::createChunkLoader( QgsChunkNode *node )
const
217 return new QgsTiledSceneChunkLoader( node, mIndex, *
this, mZValueScale, mZValueOffset );
220QgsChunkNode *QgsTiledSceneChunkLoaderFactory::nodeForTile(
const QgsTiledSceneTile &t,
const QgsChunkNodeId &nodeId, QgsChunkNode *parent )
const
222 QgsChunkNode *node =
nullptr;
226 QgsVector3D v0( mRenderContext.extent().xMinimum(), mRenderContext.extent().yMinimum(), -100 );
227 QgsVector3D v1( mRenderContext.extent().xMaximum(), mRenderContext.extent().yMaximum(), +100 );
230 node =
new QgsChunkNode( nodeId, box3D, err, parent );
237 node =
new QgsChunkNode( nodeId, box, t.
geometricError(), parent );
245QgsChunkNode *QgsTiledSceneChunkLoaderFactory::createRootNode()
const
248 return nodeForTile( t, QgsChunkNodeId( t.
id() ),
nullptr );
252QVector<QgsChunkNode *> QgsTiledSceneChunkLoaderFactory::createChildren( QgsChunkNode *node )
const
254 QVector<QgsChunkNode *> children;
255 const long long indexTileId = node->tileId().uniqueId;
260 const QVector<long long> childIds = mIndex.childTileIds( indexTileId );
261 for (
long long childId : childIds )
263 const QgsChunkNodeId chId( childId );
270 if ( t.
metadata()[
"contentFormat"] == u
"cesiumtiles"_s
272 && hasLargeBounds( t, mBoundsTransform ) )
278 const QgsPointXY c = mRenderContext.extent().center();
281 const double *half = obb.
halfAxes();
284 half[0], half[3], half[6], 0,
285 half[1], half[4], half[7], 0,
286 half[2], half[5], half[8], 0,
289 QVector3D aaa = rot.inverted().map( ecef2.
toVector3D() );
290 if ( aaa.x() > 1 || aaa.y() > 1 || aaa.z() > 1 || aaa.x() < -1 || aaa.y() < -1 || aaa.z() < -1 )
299 QgsChunkNode *nChild = nodeForTile( t, chId, node );
300 children.append( nChild );
305bool QgsTiledSceneChunkLoaderFactory::canCreateChildren( QgsChunkNode *node )
307 long long nodeId = node->tileId().uniqueId;
308 if ( mFutureHierarchyFetches.contains( nodeId ) || mPendingHierarchyFetches.contains( nodeId ) )
313 mFutureHierarchyFetches.insert( nodeId );
321 const QVector<long long> childIds = mIndex.childTileIds( nodeId );
322 for (
long long childId : childIds )
324 if ( mFutureHierarchyFetches.contains( childId ) || mPendingHierarchyFetches.contains( childId ) )
329 mFutureHierarchyFetches.insert( childId );
336void QgsTiledSceneChunkLoaderFactory::fetchHierarchyForNode(
long long nodeId, QgsChunkNode *origNode )
338 Q_ASSERT( !mPendingHierarchyFetches.contains( nodeId ) );
339 mFutureHierarchyFetches.remove( nodeId );
340 mPendingHierarchyFetches.insert( nodeId );
342 QFutureWatcher<void> *futureWatcher =
new QFutureWatcher<void>(
this );
343 connect( futureWatcher, &QFutureWatcher<void>::finished,
this, [
this, origNode, nodeId, futureWatcher] {
344 mPendingHierarchyFetches.remove( nodeId );
345 emit childrenPrepared( origNode );
346 futureWatcher->deleteLater();
348 futureWatcher->setFuture( QtConcurrent::run( [
this, nodeId] {
349 mIndex.fetchHierarchy( nodeId );
353void QgsTiledSceneChunkLoaderFactory::prepareChildren( QgsChunkNode *node )
355 long long nodeId = node->tileId().uniqueId;
356 if ( mFutureHierarchyFetches.contains( nodeId ) )
358 fetchHierarchyForNode( nodeId, node );
366 const QVector<long long> childIds = mIndex.childTileIds( nodeId );
367 for (
long long childId : childIds )
369 if ( mFutureHierarchyFetches.contains( childId ) )
371 fetchHierarchyForNode( childId, node );
379QgsTiledSceneLayerChunkedEntity::QgsTiledSceneLayerChunkedEntity(
384 double maximumScreenError,
385 bool showBoundingBoxes,
389 : QgsChunkedEntity( map, maximumScreenError, new QgsTiledSceneChunkLoaderFactory(
Qgs3DRenderContext::fromMapSettings( map ), index, tileCrs, layerCrs, zValueScale, zValueOffset ), true )
392 setShowBoundingBoxes( showBoundingBoxes );
395QgsTiledSceneLayerChunkedEntity::~QgsTiledSceneLayerChunkedEntity()
401int QgsTiledSceneLayerChunkedEntity::pendingJobsCount()
const
403 return QgsChunkedEntity::pendingJobsCount() +
static_cast<QgsTiledSceneChunkLoaderFactory *
>( mChunkLoaderFactory )->mPendingHierarchyFetches.count();
406QList<QgsRayCastHit> QgsTiledSceneLayerChunkedEntity::rayIntersection(
const QgsRay3D &ray,
const QgsRayCastContext &context )
const
416 QList<QgsRayCastHit> result;
418 QVector3D intersectionPoint;
419 QgsChunkNode *minNode =
nullptr;
420 int minTriangleIndex = -1;
422 const QList<QgsChunkNode *> active = activeNodes();
423 for ( QgsChunkNode *node : active )
436 const QList<Qt3DRender::QGeometryRenderer *> rendLst = node->entity()->findChildren<Qt3DRender::QGeometryRenderer *>();
437 for ( Qt3DRender::QGeometryRenderer *rend : rendLst )
439 QVector3D nodeIntPoint;
440 int triangleIndex = -1;
441 QgsGeoTransform *nodeGeoTransform = node->entity()->findChild<QgsGeoTransform *>();
442 Q_ASSERT( nodeGeoTransform );
449 float dist = ( ray.
origin() - nodeIntPoint ).length();
450 if ( minDist < 0 || dist < minDist )
454 minTriangleIndex = triangleIndex;
455 intersectionPoint = nodeIntPoint;
467 vm[u
"node_id"_s] = tile.
id();
469 vm[u
"node_content"_s] = tile.
resources().value( u
"content"_s );
470 vm[u
"triangle_index"_s] = minTriangleIndex;
474 hit.
setMapCoordinates( mMapSettings->worldToMapCoordinates( intersectionPoint ) );
476 result.append( hit );
479 QgsDebugMsgLevel( u
"Active Nodes: %1, checked nodes: %2, hits found: %3"_s.arg( nodesAll ).arg( nodeUsed ).arg( hits ), 2 );
@ Geocentric
Geocentric CRS.
@ NeedFetching
Tile has children, but they are not yet available and must be fetched.
@ Reverse
Reverse/inverse transform (from destination to source).
Rendering context for preparation of 3D entities.
QgsCoordinateReferenceSystem crs() const
Returns the coordinate reference system used in the 3D scene.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
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.
float distanceFromPoint(float x, float y, float z) const
Returns shortest distance from the box to a point.
static QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
A 3-dimensional box composed of x, y, z coordinates.
void setZMinimum(double z)
Sets the minimum z value.
double depth() const
Returns the depth of the box.
void setZMaximum(double z)
Sets the maximum z value.
double zMaximum() const
Returns the maximum z value.
double width() const
Returns the width of the box.
double zMinimum() const
Returns the minimum z value.
double height() const
Returns the height of the box.
static TileContents extractGltfFromTileContent(const QByteArray &tileContent)
Parses tile content.
Represents a coordinate reference system (CRS).
Qgis::DistanceUnit mapUnits
A simple 4x4 matrix implementation useful for transformation in 3D space.
Represents a oriented (rotated) box in 3 dimensions.
const double * halfAxes() const
Returns the half axes matrix;.
bool isNull() const
Returns true if the box is a null box.
QgsVector3D center() const
Returns the vector to the center of the box.
Exception thrown on failure to parse Quantized Mesh tile (malformed data).
A representation of a ray in 3D.
QVector3D origin() const
Returns the origin of the ray.
Responsible for defining parameters of the ray casting operations in 3D map canvases.
float maximumDistance() const
The maximum distance from ray origin to look for hits when casting a ray.
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.
QgsOrientedBox3D box() const
Returns the volume's oriented box.
QgsBox3D bounds(const QgsCoordinateTransform &transform=QgsCoordinateTransform(), Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Returns the axis aligned bounding box of the volume.
An index for tiled scene data providers.
Represents an individual tile from a tiled scene data source.
Qgis::TileRefinementProcess refinementProcess() const
Returns the tile's refinement process.
QVariantMap resources() const
Returns the resources attached to the tile.
const QgsTiledSceneBoundingVolume & boundingVolume() const
Returns the bounding volume for the tile.
QVariantMap metadata() const
Returns additional metadata attached to the tile.
long long id() const
Returns the tile's unique ID.
const QgsMatrix4x4 * transform() const
Returns the tile's transform.
double geometricError() const
Returns the tile's geometric error, which is the error, in meters, of the tile's simplified represent...
QUrl baseUrl() const
Returns the tile's base URL.
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
QVector3D toVector3D() const
Converts the current object to QVector3D.
bool rayBoxIntersection(const QgsRay3D &ray, const QgsAABB &nodeBbox)
Tests whether an axis aligned box is intersected by a ray.
bool rayMeshIntersection(Qt3DRender::QGeometryRenderer *geometryRenderer, const QgsRay3D &r, float maxDist, const QMatrix4x4 &worldTransform, QVector3D &intPt, int &triangleIndex)
Tests whether a triangular mesh is intersected by a ray.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
uint qHash(const QVariant &variant)
Hash for QVariant.
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)
Encapsulates the contents of a 3D tile.
QgsVector3D rtcCenter
Center position of relative-to-center coordinates (when used).
QByteArray gltf
GLTF binary content.