33#include <QtConcurrent>
34#include <Qt3DCore/QTransform>
39QgsVectorLayerChunkLoader::QgsVectorLayerChunkLoader(
const QgsVectorLayerChunkLoaderFactory *factory, QgsChunkNode *node )
40 : QgsChunkLoader( node )
42 , mRenderContext( factory->mRenderContext )
45 if ( node->level() < mFactory->mLeafLevel )
47 QTimer::singleShot( 0,
this, &QgsVectorLayerChunkLoader::finished );
52 mLayerName = mFactory->mLayer->
name();
57 QgsDebugError( QStringLiteral(
"Unknown 3D symbol type for vector layer: " ) + mFactory->mSymbol->type() );
60 mHandler.reset( handler );
63 exprContext.setFields( layer->
fields() );
64 mRenderContext.setExpressionContext( exprContext );
66 QSet<QString> attributeNames;
67 if ( !mHandler->prepare( mRenderContext, attributeNames ) )
69 QgsDebugError( QStringLiteral(
"Failed to prepare 3D feature handler!" ) );
87 mFutureWatcher =
new QFutureWatcher<void>(
this );
88 connect( mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsChunkQueueJob::finished );
90 const QFuture<void> future = QtConcurrent::run( [req,
this]
92 const QgsEventTracing::ScopedEvent e( QStringLiteral(
"3D" ), QStringLiteral(
"VL chunk load" ) );
100 mRenderContext.expressionContext().setFeature( f );
101 mHandler->processFeature( f, mRenderContext );
106 mFutureWatcher->setFuture( future );
109QgsVectorLayerChunkLoader::~QgsVectorLayerChunkLoader()
111 if ( mFutureWatcher && !mFutureWatcher->isFinished() )
113 disconnect( mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsChunkQueueJob::finished );
114 mFutureWatcher->waitForFinished();
118void QgsVectorLayerChunkLoader::cancel()
123Qt3DCore::QEntity *QgsVectorLayerChunkLoader::createEntity( Qt3DCore::QEntity *parent )
125 if ( mNode->level() < mFactory->mLeafLevel )
127 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity( parent );
128 entity->setObjectName( mLayerName +
"_CONTAINER_" + mNode->tileId().text() );
132 if ( mHandler->featureCount() == 0 )
136 mNode->setExactBbox(
QgsAABB() );
137 mNode->updateParentBoundingBoxesRecursively();
141 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity( parent );
142 entity->setObjectName( mLayerName +
"_" + mNode->tileId().text() );
143 mHandler->finalize( entity, mRenderContext );
146 if ( mHandler->zMinimum() != std::numeric_limits<float>::max() && mHandler->zMaximum() != std::numeric_limits<float>::lowest() )
149 box.
yMin = mHandler->zMinimum();
150 box.
yMax = mHandler->zMaximum();
151 mNode->setExactBbox( box );
152 mNode->updateParentBoundingBoxesRecursively();
163 : mRenderContext( context )
165 , mSymbol( symbol->clone() )
166 , mLeafLevel( leafLevel )
170 rootBbox.
xMin -= 1.0;
171 rootBbox.
xMax += 1.0;
172 rootBbox.
yMin -= 1.0;
173 rootBbox.
yMax += 1.0;
174 rootBbox.
zMin -= 1.0;
175 rootBbox.
zMax += 1.0;
176 setupQuadtree( rootBbox, -1, leafLevel );
179QgsChunkLoader *QgsVectorLayerChunkLoaderFactory::createChunkLoader( QgsChunkNode *node )
const
181 return new QgsVectorLayerChunkLoader(
this, node );
189 : QgsChunkedEntity( map,
191 new QgsVectorLayerChunkLoaderFactory(
Qgs3DRenderContext::fromMapSettings( map ), vl, symbol, tilingSettings.zoomLevelsCount() - 1, zMin, zMax ), true )
193 mTransform =
new Qt3DCore::QTransform;
194 if ( applyTerrainOffset() )
198 this->addComponent( mTransform );
205QgsVectorLayerChunkedEntity::~QgsVectorLayerChunkedEntity()
212bool QgsVectorLayerChunkedEntity::applyTerrainOffset()
const
214 QgsVectorLayerChunkLoaderFactory *loaderFactory =
static_cast<QgsVectorLayerChunkLoaderFactory *
>( mChunkLoaderFactory );
217 QString symbolType = loaderFactory->mSymbol.get()->type();
218 if ( symbolType ==
"line" )
226 else if ( symbolType ==
"point" )
234 else if ( symbolType ==
"polygon" )
244 QgsDebugMsgLevel( QStringLiteral(
"QgsVectorLayerChunkedEntity::applyTerrainOffset, unhandled symbol type %1" ).arg( symbolType ), 2 );
251void QgsVectorLayerChunkedEntity::onTerrainElevationOffsetChanged(
float newOffset )
253 QgsDebugMsgLevel( QStringLiteral(
"QgsVectorLayerChunkedEntity::onTerrainElevationOffsetChanged" ), 2 );
254 if ( !applyTerrainOffset() )
258 mTransform->setTranslation( QVector3D( 0.0f, newOffset, 0.0f ) );
261QVector<QgsRayCastingUtils::RayHit> QgsVectorLayerChunkedEntity::rayIntersection(
const QgsRayCastingUtils::Ray3D &ray,
const QgsRayCastingUtils::RayCastContext &context )
const
263 return QgsVectorLayerChunkedEntity::rayIntersection( activeNodes(), mTransform->matrix(), ray, context );
266QVector<QgsRayCastingUtils::RayHit> QgsVectorLayerChunkedEntity::rayIntersection(
const QList<QgsChunkNode *> &activeNodes,
const QMatrix4x4 &transformMatrix,
const QgsRayCastingUtils::Ray3D &ray,
const QgsRayCastingUtils::RayCastContext &context )
274 int ignoredGeometries = 0;
276 QVector<QgsRayCastingUtils::RayHit> result;
279 QVector3D intersectionPoint;
282 for ( QgsChunkNode *node : activeNodes )
287 if ( node->entity() &&
288 ( minDist < 0 || node->bbox().distanceFromPoint( ray.origin() ) < minDist ) &&
289 QgsRayCastingUtils::rayBoxIntersection( ray, node->bbox() ) )
294 const QList<Qt3DRender::QGeometryRenderer *> rendLst = node->entity()->findChildren<Qt3DRender::QGeometryRenderer *>();
295 for (
const auto &rend : rendLst )
297 auto *geom = rend->geometry();
307 QVector3D nodeIntPoint;
308 int triangleIndex = -1;
310 if ( QgsRayCastingUtils::rayMeshIntersection( rend, ray, transformMatrix, nodeIntPoint, triangleIndex ) )
315 float dist = ( ray.origin() - nodeIntPoint ).length();
316 if ( minDist < 0 || dist < minDist )
319 intersectionPoint = nodeIntPoint;
329 result.append( hit );
331 QgsDebugMsgLevel( QStringLiteral(
"Active Nodes: %1, checked nodes: %2, hits found: %3, incompatible geometries: %4" ).arg( nodesAll ).arg( nodeUsed ).arg( hits ).arg( ignoredGeometries ), 2 );
@ Absolute
Elevation is taken directly from feature and is independent of terrain height (final elevation = feat...
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.
QgsVector3D origin() const
Returns coordinates in map CRS at which 3D scene has origin (0,0,0)
QgsRectangle extent() const
Returns the 3D scene's 2D extent in the 3D scene's CRS.
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)
Fetch next feature and stores in f, returns true on success.
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setCoordinateTransform(const QgsCoordinateTransform &transform)
Sets the coordinate transform which will be used to transform the feature's geometries.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
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...
Qgis::AltitudeClamping altitudeClamping() const
Returns method that determines altitude (whether to clamp to feature to terrain)
QgsCoordinateReferenceSystem crs3D
Qgis::AltitudeClamping altitudeClamping() const
Returns method that determines altitude (whether to clamp to feature to terrain)
Qgis::AltitudeClamping altitudeClamping() const
Returns method that determines altitude (whether to clamp to feature to terrain)
A rectangle specified with double values.
QgsFeatureId triangleIndexToFeatureId(uint triangleIndex) const
Returns ID of the feature to which given triangle index belongs (used for picking).
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.
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)
Helper struct to store ray casting parameters.
Helper struct to store ray casting results.