QGIS API Documentation 3.39.0-Master (d85f3c2a281)
Loading...
Searching...
No Matches
qgstiledscenechunkloader_p.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstiledscenechunkloader_p.cpp
3 --------------------------------------
4 Date : July 2023
5 Copyright : (C) 2023 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include "qgs3dmapsettings.h"
19#include "qgsapplication.h"
20#include "qgscesiumutils.h"
22#include "qgsgltf3dutils.h"
26#include "qgstiledscenetile.h"
27
28#include <QtConcurrentRun>
29
30
32
33size_t qHash( const QgsChunkNodeId &n )
34{
35 return n.uniqueId;
36}
37
38static bool hasLargeBounds( const QgsTiledSceneTile &t, const QgsCoordinateTransform &boundsTransform )
39{
40 if ( t.geometricError() > 1e6 )
41 return true;
42
43 if ( t.boundingVolume().box().isNull() )
44 return true;
45
46 Q_ASSERT( boundsTransform.destinationCrs().mapUnits() == Qgis::DistanceUnit::Meters );
47 QgsBox3D bounds = t.boundingVolume().bounds( boundsTransform );
48 return bounds.width() > 1e5 || bounds.height() > 1e5 || bounds.depth() > 1e5;
49}
50
52
53QgsTiledSceneChunkLoader::QgsTiledSceneChunkLoader( QgsChunkNode *node, const QgsTiledSceneIndex &index, const QgsTiledSceneChunkLoaderFactory &factory, double zValueScale, double zValueOffset )
54 : QgsChunkLoader( node )
55 , mFactory( factory )
56 , mIndex( index )
57{
58 mFutureWatcher = new QFutureWatcher<void>( this );
59 connect( mFutureWatcher, &QFutureWatcher<void>::finished, this, &QgsChunkQueueJob::finished );
60
61 const QgsCoordinateTransform &boundsTransform = factory.mBoundsTransform;
62
63 const QgsChunkNodeId tileId = node->tileId();
64 const QFuture<void> future = QtConcurrent::run( [this, tileId, zValueScale, zValueOffset, boundsTransform]
65 {
66 const QgsTiledSceneTile tile = mIndex.getTile( tileId.uniqueId );
67
68 // we do not load tiles that are too big - at least for the time being
69 // the problem is that their 3D bounding boxes with ECEF coordinates are huge
70 // and we are unable to turn them into planar bounding boxes
71 if ( hasLargeBounds( tile, boundsTransform ) )
72 return;
73
74 QString uri = tile.resources().value( QStringLiteral( "content" ) ).toString();
75 if ( uri.isEmpty() )
76 {
77 // nothing to show for this tile
78 // TODO: can we skip loading it at all?
79 return;
80 }
81
82 uri = tile.baseUrl().resolved( uri ).toString();
83 QByteArray content = mFactory.mIndex.retrieveContent( uri );
84 if ( content.isEmpty() )
85 {
86 // the request probably failed
87 // TODO: how can we report it?
88 return;
89 }
90
91 QgsGltf3DUtils::EntityTransform entityTransform;
92 entityTransform.tileTransform = ( tile.transform() ? *tile.transform() : QgsMatrix4x4() );
93 entityTransform.sceneOriginTargetCrs = mFactory.mRenderContext.origin();
94 entityTransform.ecefToTargetCrs = &mFactory.mBoundsTransform;
95 entityTransform.zValueScale = zValueScale;
96 entityTransform.zValueOffset = zValueOffset;
97 entityTransform.gltfUpAxis = static_cast< Qgis::Axis >( tile.metadata().value( QStringLiteral( "gltfUpAxis" ), static_cast< int >( Qgis::Axis::Y ) ).toInt() );
98
99 const QString &format = tile.metadata().value( QStringLiteral( "contentFormat" ) ).value<QString>();
100 QStringList errors;
101 if ( format == QLatin1String( "quantizedmesh" ) )
102 {
103 try
104 {
105 QgsQuantizedMeshTile qmTile( content );
106 qmTile.removeDegenerateTriangles();
107 tinygltf::Model model = qmTile.toGltf( true, 100 );
108 mEntity = QgsGltf3DUtils::parsedGltfToEntity( model, entityTransform, uri, &errors );
109 }
111 {
112 errors.append( QStringLiteral( "Failed to parse tile from '%1'" ).arg( uri ) );
113 }
114 }
115 else if ( format == "cesiumtiles" )
116 {
118 if ( tileContent.gltf.isEmpty() )
119 return;
120 entityTransform.tileTransform.translate( tileContent.rtcCenter );
121 mEntity = QgsGltf3DUtils::gltfToEntity( tileContent.gltf, entityTransform, uri, &errors );
122 }
123 else return; // unsupported tile content type
124
125 // TODO: report errors somewhere?
126 if ( !errors.isEmpty() )
127 {
128 QgsDebugError( "gltf load errors: " + errors.join( '\n' ) );
129 }
130
131 if ( mEntity )
132 mEntity->moveToThread( QgsApplication::instance()->thread() );
133 } );
134
135 // emit finished() as soon as the handler is populated with features
136 mFutureWatcher->setFuture( future );
137}
138
139QgsTiledSceneChunkLoader::~QgsTiledSceneChunkLoader()
140{
141 if ( !mFutureWatcher->isFinished() )
142 {
143 disconnect( mFutureWatcher, &QFutureWatcher<void>::finished, this, &QgsChunkQueueJob::finished );
144 mFutureWatcher->waitForFinished();
145 }
146}
147
148Qt3DCore::QEntity *QgsTiledSceneChunkLoader::createEntity( Qt3DCore::QEntity *parent )
149{
150 if ( mEntity )
151 mEntity->setParent( parent );
152 return mEntity;
153}
154
156
157QgsTiledSceneChunkLoaderFactory::QgsTiledSceneChunkLoaderFactory( const Qgs3DRenderContext &context, const QgsTiledSceneIndex &index, QgsCoordinateReferenceSystem tileCrs, double zValueScale, double zValueOffset )
158 : mRenderContext( context )
159 , mIndex( index )
160 , mZValueScale( zValueScale )
161 , mZValueOffset( zValueOffset )
162{
163 mBoundsTransform = QgsCoordinateTransform( tileCrs, context.crs(), context.transformContext() );
164}
165
166QgsChunkLoader *QgsTiledSceneChunkLoaderFactory::createChunkLoader( QgsChunkNode *node ) const
167{
168 return new QgsTiledSceneChunkLoader( node, mIndex, *this, mZValueScale, mZValueOffset );
169}
170
171// converts box from map coordinates to world coords (also flips [X,Y] to [X,-Z])
172static QgsAABB aabbConvert( const QgsBox3D &b0, const QgsVector3D &sceneOriginTargetCrs )
173{
174 const QgsBox3D b = b0 - sceneOriginTargetCrs;
175 return QgsAABB( b.xMinimum(), b.zMinimum(), -b.yMaximum(), b.xMaximum(), b.zMaximum(), -b.yMinimum() );
176}
177
178QgsChunkNode *QgsTiledSceneChunkLoaderFactory::nodeForTile( const QgsTiledSceneTile &t, const QgsChunkNodeId &nodeId, QgsChunkNode *parent ) const
179{
180 QgsChunkNode *node = nullptr;
181 if ( hasLargeBounds( t, mBoundsTransform ) )
182 {
183 // use the full extent of the scene
184 QgsVector3D v0 = mRenderContext.mapToWorldCoordinates( QgsVector3D( mRenderContext.extent().xMinimum(), mRenderContext.extent().yMinimum(), -100 ) );
185 QgsVector3D v1 = mRenderContext.mapToWorldCoordinates( QgsVector3D( mRenderContext.extent().xMaximum(), mRenderContext.extent().yMaximum(), +100 ) );
186 QgsAABB aabb( v0.x(), v0.y(), v0.z(), v1.x(), v1.y(), v1.z() );
187 float err = std::min( 1e6, t.geometricError() );
188 node = new QgsChunkNode( nodeId, aabb, err, parent );
189 }
190 else
191 {
192 QgsBox3D box = t.boundingVolume().bounds( mBoundsTransform );
193 box.setZMinimum( box.zMinimum() * mZValueScale + mZValueOffset );
194 box.setZMaximum( box.zMaximum() * mZValueScale + mZValueOffset );
195 const QgsAABB aabb = aabbConvert( box, mRenderContext.origin() );
196 node = new QgsChunkNode( nodeId, aabb, t.geometricError(), parent );
197 }
198
199 node->setRefinementProcess( t.refinementProcess() );
200 return node;
201}
202
203
204QgsChunkNode *QgsTiledSceneChunkLoaderFactory::createRootNode() const
205{
206 const QgsTiledSceneTile t = mIndex.rootTile();
207 return nodeForTile( t, QgsChunkNodeId( t.id() ), nullptr );
208}
209
210
211QVector<QgsChunkNode *> QgsTiledSceneChunkLoaderFactory::createChildren( QgsChunkNode *node ) const
212{
213 QVector<QgsChunkNode *> children;
214 const long long indexTileId = node->tileId().uniqueId;
215
216 // fetching of hierarchy is handled by canCreateChildren() + prepareChildren()
217 Q_ASSERT( mIndex.childAvailability( indexTileId ) != Qgis::TileChildrenAvailability::NeedFetching );
218
219 const QVector< long long > childIds = mIndex.childTileIds( indexTileId );
220 for ( long long childId : childIds )
221 {
222 const QgsChunkNodeId chId( childId );
223 QgsTiledSceneTile t = mIndex.getTile( childId );
224
225 // fetching of hierarchy is handled by canCreateChildren() + prepareChildren()
226 Q_ASSERT( mIndex.childAvailability( childId ) != Qgis::TileChildrenAvailability::NeedFetching );
227
228 QgsChunkNode *nChild = nodeForTile( t, chId, node );
229 children.append( nChild );
230 }
231 return children;
232}
233
234bool QgsTiledSceneChunkLoaderFactory::canCreateChildren( QgsChunkNode *node )
235{
236 long long nodeId = node->tileId().uniqueId;
237 if ( mFutureHierarchyFetches.contains( nodeId ) || mPendingHierarchyFetches.contains( nodeId ) )
238 return false;
239
240 if ( mIndex.childAvailability( nodeId ) == Qgis::TileChildrenAvailability::NeedFetching )
241 {
242 mFutureHierarchyFetches.insert( nodeId );
243 return false;
244 }
245
246 // we need to make sure that if a child tile's content references another tileset JSON,
247 // we fetch its hierarchy before a chunk node is created for such child tile - otherwise we
248 // end up trying to load tileset JSON file instead of the actual content
249
250 const QVector< long long > childIds = mIndex.childTileIds( nodeId );
251 for ( long long childId : childIds )
252 {
253 if ( mFutureHierarchyFetches.contains( childId ) || mPendingHierarchyFetches.contains( childId ) )
254 return false;
255
256 if ( mIndex.childAvailability( childId ) == Qgis::TileChildrenAvailability::NeedFetching )
257 {
258 mFutureHierarchyFetches.insert( childId );
259 return false;
260 }
261 }
262 return true;
263}
264
265void QgsTiledSceneChunkLoaderFactory::fetchHierarchyForNode( long long nodeId, QgsChunkNode *origNode )
266{
267 Q_ASSERT( !mPendingHierarchyFetches.contains( nodeId ) );
268 mFutureHierarchyFetches.remove( nodeId );
269 mPendingHierarchyFetches.insert( nodeId );
270
271 QFutureWatcher<void> *futureWatcher = new QFutureWatcher<void>( this );
272 connect( futureWatcher, &QFutureWatcher<void>::finished, this, [this, origNode, nodeId, futureWatcher]
273 {
274 mPendingHierarchyFetches.remove( nodeId );
275 emit childrenPrepared( origNode );
276 futureWatcher->deleteLater();
277 } );
278 futureWatcher->setFuture( QtConcurrent::run( [this, nodeId]
279 {
280 mIndex.fetchHierarchy( nodeId );
281 } ) );
282}
283
284void QgsTiledSceneChunkLoaderFactory::prepareChildren( QgsChunkNode *node )
285{
286 long long nodeId = node->tileId().uniqueId;
287 if ( mFutureHierarchyFetches.contains( nodeId ) )
288 {
289 fetchHierarchyForNode( nodeId, node );
290 return;
291 }
292
293 // we need to make sure that if a child tile's content references another tileset JSON,
294 // we fetch its hierarchy before a chunk node is created for such child tile - otherwise we
295 // end up trying to load tileset JSON file instead of the actual content
296
297 const QVector< long long > childIds = mIndex.childTileIds( nodeId );
298 for ( long long childId : childIds )
299 {
300 if ( mFutureHierarchyFetches.contains( childId ) )
301 {
302 fetchHierarchyForNode( childId, node );
303 }
304 }
305}
306
307
309
310QgsTiledSceneLayerChunkedEntity::QgsTiledSceneLayerChunkedEntity( Qgs3DMapSettings *map, const QgsTiledSceneIndex &index, QgsCoordinateReferenceSystem tileCrs, double maximumScreenError, bool showBoundingBoxes, double zValueScale, double zValueOffset )
311 : QgsChunkedEntity( map, maximumScreenError, new QgsTiledSceneChunkLoaderFactory( Qgs3DRenderContext::fromMapSettings( map ), index, tileCrs, zValueScale, zValueOffset ), true )
312 , mIndex( index )
313{
314 setShowBoundingBoxes( showBoundingBoxes );
315}
316
317QgsTiledSceneLayerChunkedEntity::~QgsTiledSceneLayerChunkedEntity()
318{
319 // cancel / wait for jobs
320 cancelActiveJobs();
321}
322
323int QgsTiledSceneLayerChunkedEntity::pendingJobsCount() const
324{
325 return QgsChunkedEntity::pendingJobsCount() + static_cast<QgsTiledSceneChunkLoaderFactory *>( mChunkLoaderFactory )->mPendingHierarchyFetches.count();
326}
327
328QVector<QgsRayCastingUtils::RayHit> QgsTiledSceneLayerChunkedEntity::rayIntersection( const QgsRayCastingUtils::Ray3D &ray, const QgsRayCastingUtils::RayCastContext &context ) const
329{
330 Q_UNUSED( context );
331 QgsDebugMsgLevel( QStringLiteral( "Ray cast on tiled scene layer" ), 2 );
332#ifdef QGISDEBUG
333 int nodeUsed = 0;
334 int nodesAll = 0;
335 int hits = 0;
336#endif
337
338 QVector<QgsRayCastingUtils::RayHit> result;
339 float minDist = -1;
340 QVector3D intersectionPoint;
341 QgsChunkNode *minNode = nullptr;
342 int minTriangleIndex = -1;
343
344 const QList<QgsChunkNode *> active = activeNodes();
345 for ( QgsChunkNode *node : active )
346 {
347#ifdef QGISDEBUG
348 nodesAll++;
349#endif
350 if ( node->entity() &&
351 ( minDist < 0 || node->bbox().distanceFromPoint( ray.origin() ) < minDist ) &&
352 QgsRayCastingUtils::rayBoxIntersection( ray, node->bbox() ) )
353 {
354#ifdef QGISDEBUG
355 nodeUsed++;
356#endif
357 const QList<Qt3DRender::QGeometryRenderer *> rendLst = node->entity()->findChildren<Qt3DRender::QGeometryRenderer *>();
358 for ( Qt3DRender::QGeometryRenderer *rend : rendLst )
359 {
360 QVector3D nodeIntPoint;
361 int triangleIndex = -1;
362 bool success = QgsRayCastingUtils::rayMeshIntersection( rend, ray, QMatrix4x4(), nodeIntPoint, triangleIndex );
363 if ( success )
364 {
365#ifdef QGISDEBUG
366 hits++;
367#endif
368 float dist = ( ray.origin() - nodeIntPoint ).length();
369 if ( minDist < 0 || dist < minDist )
370 {
371 minDist = dist;
372 minNode = node;
373 minTriangleIndex = triangleIndex;
374 intersectionPoint = nodeIntPoint;
375 }
376 }
377 }
378 }
379 }
380
381 if ( minDist >= 0 )
382 {
383 QVariantMap vm;
384 QgsTiledSceneTile tile = mIndex.getTile( minNode->tileId().uniqueId );
385 // at this point this is mostly for debugging - we may want to change/rename what's returned here
386 vm["node_id"] = tile.id();
387 vm["node_error"] = tile.geometricError();
388 vm["node_content"] = tile.resources().value( QStringLiteral( "content" ) );
389 vm["triangle_index"] = minTriangleIndex;
390 QgsRayCastingUtils::RayHit hit( minDist, intersectionPoint, FID_NULL, vm );
391 result.append( hit );
392 }
393
394 QgsDebugMsgLevel( QStringLiteral( "Active Nodes: %1, checked nodes: %2, hits found: %3" ).arg( nodesAll ).arg( nodeUsed ).arg( hits ), 2 );
395 return result;
396}
397
@ NeedFetching
Tile has children, but they are not yet available and must be fetched.
Axis
Cartesian axes.
Definition qgis.h:2283
@ Y
Y-axis.
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 QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:43
double yMaximum() const
Returns the maximum y value.
Definition qgsbox3d.h:213
void setZMinimum(double z)
Sets the minimum z value.
Definition qgsbox3d.cpp:76
double depth() const
Returns the depth of the box.
Definition qgsbox3d.h:274
void setZMaximum(double z)
Sets the maximum z value.
Definition qgsbox3d.cpp:81
double xMinimum() const
Returns the minimum x value.
Definition qgsbox3d.h:178
double zMaximum() const
Returns the maximum z value.
Definition qgsbox3d.h:241
double xMaximum() const
Returns the maximum x value.
Definition qgsbox3d.h:185
double width() const
Returns the width of the box.
Definition qgsbox3d.h:260
double zMinimum() const
Returns the minimum z value.
Definition qgsbox3d.h:234
double yMinimum() const
Returns the minimum y value.
Definition qgsbox3d.h:206
double height() const
Returns the height of the box.
Definition qgsbox3d.h:267
static TileContents extractGltfFromTileContent(const QByteArray &tileContent)
Parses tile content.
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system, which the transform will transform coordinates t...
A simple 4x4 matrix implementation useful for transformation in 3D space.
bool isNull() const
Returns true if the box is a null box.
Exception thrown on failure to parse Quantized Mesh tile (malformed data)
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.
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
Definition qgsvector3d.h:31
double y() const
Returns Y coordinate.
Definition qgsvector3d.h:50
double z() const
Returns Z coordinate.
Definition qgsvector3d.h:52
double x() const
Returns X coordinate.
Definition qgsvector3d.h:48
uint qHash(const QVariant &variant)
Hash for QVariant.
Definition qgis.cpp:198
#define FID_NULL
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
#define QgsDebugError(str)
Definition qgslogger.h:38
Encapsulates the contents of a 3D tile.
QgsVector3D rtcCenter
Center position of relative-to-center coordinates (when used)
QByteArray gltf
GLTF binary content.
Helper struct to store ray casting parameters.
Helper struct to store ray casting results.