17#include "moc_qgsvectorlayerchunkloader_p.cpp"
34#include <QtConcurrent>
35#include <Qt3DCore/QTransform>
40QgsVectorLayerChunkLoader::QgsVectorLayerChunkLoader(
const QgsVectorLayerChunkLoaderFactory *factory, QgsChunkNode *node )
41 : QgsChunkLoader( node )
43 , mRenderContext( factory->mRenderContext )
46 if ( node->level() < mFactory->mLeafLevel )
48 QTimer::singleShot( 0,
this, &QgsVectorLayerChunkLoader::finished );
53 mLayerName = mFactory->mLayer->
name();
58 QgsDebugError( QStringLiteral(
"Unknown 3D symbol type for vector layer: " ) + mFactory->mSymbol->type() );
61 mHandler.reset( handler );
70 exprContext.setFields( layer->
fields() );
71 mRenderContext.setExpressionContext( exprContext );
73 QSet<QString> attributeNames;
74 if ( !mHandler->prepare( mRenderContext, attributeNames, chunkOrigin ) )
76 QgsDebugError( QStringLiteral(
"Failed to prepare 3D feature handler!" ) );
91 mFutureWatcher =
new QFutureWatcher<void>(
this );
92 connect( mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsChunkQueueJob::finished );
94 const QFuture<void> future = QtConcurrent::run( [req,
this]
96 const QgsEventTracing::ScopedEvent e( QStringLiteral(
"3D" ), QStringLiteral(
"VL chunk load" ) );
104 mRenderContext.expressionContext().setFeature( f );
105 mHandler->processFeature( f, mRenderContext );
110 mFutureWatcher->setFuture( future );
113QgsVectorLayerChunkLoader::~QgsVectorLayerChunkLoader()
115 if ( mFutureWatcher && !mFutureWatcher->isFinished() )
117 disconnect( mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsChunkQueueJob::finished );
118 mFutureWatcher->waitForFinished();
122void QgsVectorLayerChunkLoader::cancel()
127Qt3DCore::QEntity *QgsVectorLayerChunkLoader::createEntity( Qt3DCore::QEntity *parent )
129 if ( mNode->level() < mFactory->mLeafLevel )
131 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity( parent );
132 entity->setObjectName( mLayerName +
"_CONTAINER_" + mNode->tileId().text() );
136 if ( mHandler->featureCount() == 0 )
141 mNode->updateParentBoundingBoxesRecursively();
145 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity( parent );
146 entity->setObjectName( mLayerName +
"_" + mNode->tileId().text() );
147 mHandler->finalize( entity, mRenderContext );
150 if ( mHandler->zMinimum() != std::numeric_limits<float>::max() && mHandler->zMaximum() != std::numeric_limits<float>::lowest() )
155 mNode->setExactBox3D( box );
156 mNode->updateParentBoundingBoxesRecursively();
167 : mRenderContext( context )
169 , mSymbol( symbol->clone() )
170 , mLeafLevel( leafLevel )
174 rootBox3D.grow( 1.0 );
175 setupQuadtree( rootBox3D, -1, leafLevel );
178QgsChunkLoader *QgsVectorLayerChunkLoaderFactory::createChunkLoader( QgsChunkNode *node )
const
180 return new QgsVectorLayerChunkLoader(
this, node );
188 : QgsChunkedEntity( map,
190 new QgsVectorLayerChunkLoaderFactory(
Qgs3DRenderContext::fromMapSettings( map ), vl, symbol, tilingSettings.zoomLevelsCount() - 1, zMin, zMax ), true )
192 mTransform =
new Qt3DCore::QTransform;
193 if ( applyTerrainOffset() )
197 this->addComponent( mTransform );
204QgsVectorLayerChunkedEntity::~QgsVectorLayerChunkedEntity()
211bool QgsVectorLayerChunkedEntity::applyTerrainOffset()
const
213 QgsVectorLayerChunkLoaderFactory *loaderFactory =
static_cast<QgsVectorLayerChunkLoaderFactory *
>( mChunkLoaderFactory );
216 QString symbolType = loaderFactory->mSymbol.get()->type();
217 if ( symbolType ==
"line" )
225 else if ( symbolType ==
"point" )
233 else if ( symbolType ==
"polygon" )
243 QgsDebugMsgLevel( QStringLiteral(
"QgsVectorLayerChunkedEntity::applyTerrainOffset, unhandled symbol type %1" ).arg( symbolType ), 2 );
250void QgsVectorLayerChunkedEntity::onTerrainElevationOffsetChanged(
float newOffset )
252 QgsDebugMsgLevel( QStringLiteral(
"QgsVectorLayerChunkedEntity::onTerrainElevationOffsetChanged" ), 2 );
253 if ( !applyTerrainOffset() )
257 mTransform->setTranslation( QVector3D( 0.0f, newOffset, 0.0f ) );
260QVector<QgsRayCastingUtils::RayHit> QgsVectorLayerChunkedEntity::rayIntersection(
const QgsRayCastingUtils::Ray3D &ray,
const QgsRayCastingUtils::RayCastContext &context )
const
262 return QgsVectorLayerChunkedEntity::rayIntersection( activeNodes(), mTransform->matrix(), ray, context, mMapSettings->origin() );
265QVector<QgsRayCastingUtils::RayHit> QgsVectorLayerChunkedEntity::rayIntersection(
const QList<QgsChunkNode *> &activeNodes,
const QMatrix4x4 &transformMatrix,
const QgsRayCastingUtils::Ray3D &ray,
const QgsRayCastingUtils::RayCastContext &context,
const QgsVector3D &origin )
273 int ignoredGeometries = 0;
275 QVector<QgsRayCastingUtils::RayHit> result;
278 QVector3D intersectionPoint;
281 for ( QgsChunkNode *node : activeNodes )
289 if ( node->entity() &&
291 QgsRayCastingUtils::rayBoxIntersection( ray, nodeBbox ) )
296 const QList<Qt3DRender::QGeometryRenderer *> rendLst = node->entity()->findChildren<Qt3DRender::QGeometryRenderer *>();
297 for (
const auto &rend : rendLst )
299 auto *geom = rend->geometry();
309 QVector3D nodeIntPoint;
310 int triangleIndex = -1;
312 if ( QgsRayCastingUtils::rayMeshIntersection( rend, ray, transformMatrix, nodeIntPoint, triangleIndex ) )
317 float dist = ( ray.origin() - nodeIntPoint ).length();
318 if ( minDist < 0 || dist < minDist )
321 intersectionPoint = nodeIntPoint;
331 result.append( hit );
333 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.
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 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.
float distanceFromPoint(float x, float y, float z) const
Returns shortest distance from the box to a point.
static Qgs3DSymbolRegistry * symbol3DRegistry()
Returns registry of available 3D symbols.
A 3-dimensional box composed of x, y, z coordinates.
void setZMinimum(double z)
Sets the minimum z value.
void setZMaximum(double z)
Sets the maximum z value.
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.
QgsPointXY center() const
Returns the center point of the rectangle.
QgsFeatureId triangleIndexToFeatureId(uint triangleIndex) const
Returns ID of the feature to which given triangle index belongs (used for picking).
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
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.