30#include <QtConcurrent>
31#include <Qt3DCore/QTransform>
36QgsVectorLayerChunkLoader::QgsVectorLayerChunkLoader(
const QgsVectorLayerChunkLoaderFactory *factory, QgsChunkNode *node )
37 : QgsChunkLoader( node )
39 , mContext( factory->mMap )
42 if ( node->level() < mFactory->mLeafLevel )
44 QTimer::singleShot( 0,
this, &QgsVectorLayerChunkLoader::finished );
54 QgsDebugError( QStringLiteral(
"Unknown 3D symbol type for vector layer: " ) + mFactory->mSymbol->type() );
57 mHandler.reset( handler );
60 exprContext.setFields( layer->
fields() );
61 mContext.setExpressionContext( exprContext );
63 QSet<QString> attributeNames;
64 if ( !mHandler->prepare( mContext, attributeNames ) )
66 QgsDebugError( QStringLiteral(
"Failed to prepare 3D feature handler!" ) );
82 mFutureWatcher =
new QFutureWatcher<void>(
this );
83 connect( mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsChunkQueueJob::finished );
85 const QFuture<void> future = QtConcurrent::run( [req,
this]
87 const QgsEventTracing::ScopedEvent e( QStringLiteral(
"3D" ), QStringLiteral(
"VL chunk load" ) );
95 mContext.expressionContext().setFeature( f );
96 mHandler->processFeature( f, mContext );
101 mFutureWatcher->setFuture( future );
104QgsVectorLayerChunkLoader::~QgsVectorLayerChunkLoader()
106 if ( mFutureWatcher && !mFutureWatcher->isFinished() )
108 disconnect( mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsChunkQueueJob::finished );
109 mFutureWatcher->waitForFinished();
113void QgsVectorLayerChunkLoader::cancel()
118Qt3DCore::QEntity *QgsVectorLayerChunkLoader::createEntity( Qt3DCore::QEntity *parent )
120 if ( mNode->level() < mFactory->mLeafLevel )
122 return new Qt3DCore::QEntity( parent );
125 if ( mHandler->featureCount() == 0 )
131 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity( parent );
132 mHandler->finalize( entity, mContext );
135 if ( mHandler->zMinimum() != std::numeric_limits<float>::max() && mHandler->zMaximum() != std::numeric_limits<float>::lowest() )
138 box.
yMin = mHandler->zMinimum();
139 box.
yMax = mHandler->zMaximum();
140 mNode->setExactBbox( box );
141 mNode->updateParentBoundingBoxesRecursively();
154 , mSymbol( symbol->clone() )
155 , mLeafLevel( leafLevel )
159 rootBbox.
xMin -= 1.0;
160 rootBbox.
xMax += 1.0;
161 rootBbox.
yMin -= 1.0;
162 rootBbox.
yMax += 1.0;
163 rootBbox.
zMin -= 1.0;
164 rootBbox.
zMax += 1.0;
165 setupQuadtree( rootBbox, -1, leafLevel );
168QgsChunkLoader *QgsVectorLayerChunkLoaderFactory::createChunkLoader( QgsChunkNode *node )
const
170 return new QgsVectorLayerChunkLoader(
this, node );
178 : QgsChunkedEntity( -1,
179 new QgsVectorLayerChunkLoaderFactory( map, vl, symbol, tilingSettings.zoomLevelsCount() - 1, zMin, zMax ), true )
181 mTransform =
new Qt3DCore::QTransform;
183 this->addComponent( mTransform );
190QgsVectorLayerChunkedEntity::~QgsVectorLayerChunkedEntity()
196void QgsVectorLayerChunkedEntity::onTerrainElevationOffsetChanged(
float newOffset )
198 QgsDebugMsgLevel( QStringLiteral(
"QgsVectorLayerChunkedEntity::onTerrainElevationOffsetChanged" ), 2 );
199 mTransform->setTranslation( QVector3D( 0.0f, newOffset, 0.0f ) );
202QVector<QgsRayCastingUtils::RayHit> QgsVectorLayerChunkedEntity::rayIntersection(
const QgsRayCastingUtils::Ray3D &ray,
const QgsRayCastingUtils::RayCastContext &context )
const
204 return QgsVectorLayerChunkedEntity::rayIntersection( activeNodes(), mTransform->matrix(), ray, context );
207QVector<QgsRayCastingUtils::RayHit> QgsVectorLayerChunkedEntity::rayIntersection(
const QList<QgsChunkNode *> &activeNodes,
const QMatrix4x4 &transformMatrix,
const QgsRayCastingUtils::Ray3D &ray,
const QgsRayCastingUtils::RayCastContext &context )
215 int ignoredGeometries = 0;
217 QVector<QgsRayCastingUtils::RayHit> result;
220 QVector3D intersectionPoint;
223 for ( QgsChunkNode *node : activeNodes )
228 if ( node->entity() &&
229 ( minDist < 0 || node->bbox().distanceFromPoint( ray.origin() ) < minDist ) &&
230 QgsRayCastingUtils::rayBoxIntersection( ray, node->bbox() ) )
235 const QList<Qt3DRender::QGeometryRenderer *> rendLst = node->entity()->findChildren<Qt3DRender::QGeometryRenderer *>();
236 for (
const auto &rend : rendLst )
238 auto *geom = rend->geometry();
248 QVector3D nodeIntPoint;
250 if ( polygonGeom->
rayIntersection( ray, transformMatrix, nodeIntPoint, fid ) )
255 float dist = ( ray.origin() - nodeIntPoint ).length();
256 if ( minDist < 0 || dist < minDist )
259 intersectionPoint = nodeIntPoint;
268 QgsRayCastingUtils::RayHit hit( minDist, intersectionPoint, nearestFid );
269 result.append( hit );
271 QgsDebugMsgLevel( QStringLiteral(
"Active Nodes: %1, checked nodes: %2, hits found: %3, incompatible geometries: %4" ).arg( nodesAll ).arg( nodeUsed ).arg( hits ).arg( ignoredGeometries ), 2 );
QgsRectangle extent() const
Returns the 3D scene's 2D extent in project's CRS.
float terrainElevationOffset() const
Returns the elevation offset of the terrain (used to move the terrain up or down)
void terrainElevationOffsetChanged(float newElevation)
Emitted when the terrain elevation offset is changed.
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used in the 3D scene.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
QgsVector3D origin() const
Returns coordinates in map CRS at which 3D scene has origin (0,0,0)
QgsFeature3DHandler * createHandlerForSymbol(QgsVectorLayer *layer, const QgsAbstract3DSymbol *symbol)
Creates a feature handler for a symbol, for the specified vector layer.
static QgsRectangle worldToMapExtent(const QgsAABB &bbox, const QgsVector3D &mapOrigin)
Converts axis aligned bounding box in 3D world coordinates to extent in map coordinates.
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.
static QgsExpressionContext globalProjectLayerExpressionContext(QgsVectorLayer *layer)
Returns expression context for use in preparation of 3D data of a layer.
static Qgs3DSymbolRegistry * symbol3DRegistry()
Returns registry of available 3D symbols.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QgsFeatureRequest & setDestinationCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets the destination crs for feature's geometries.
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
A rectangle specified with double values.
bool rayIntersection(const QgsRayCastingUtils::Ray3D &ray, const QMatrix4x4 &worldTransform, QVector3D &intersectionPoint, QgsFeatureId &fid)
Tests whether the geometry is intersected by ray.
bool showBoundingBoxes() const
Returns whether to display bounding boxes of entity's tiles (for debugging)
Partial snapshot of vector layer's state (only the members necessary for access to features)
Represents a vector layer which manages a vector based data sets.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)