41#include <Qt3DCore/QTransform>
42#include <Qt3DRender/QGeometryRenderer>
43#include <QtConcurrentRun>
45#include "moc_qgsvectorlayerchunkloader_p.cpp"
47using namespace Qt::StringLiterals;
52QgsVectorLayerChunkLoader::QgsVectorLayerChunkLoader(
const QgsVectorLayerChunkLoaderFactory *factory, QgsChunkNode *node )
53 : QgsChunkLoader( node )
55 , mRenderContext( factory->mRenderContext )
60void QgsVectorLayerChunkLoader::start()
62 QgsChunkNode *node = chunk();
65 mLayerName = mFactory->mLayer->
name();
70 QgsDebugError( u
"Unknown 3D symbol type for vector layer: "_s + mFactory->mSymbol->type() );
73 mHandler.reset( handler );
78 mRenderContext.setExpressionContext( exprContext );
80 QSet<QString> attributeNames;
81 if ( !mHandler->prepare( mRenderContext, attributeNames, node->box3D() ) )
100 mFutureWatcher =
new QFutureWatcher<void>(
this );
102 connect( mFutureWatcher, &QFutureWatcher<void>::finished,
this, [
this] {
104 mFactory->mNodesAreLeafs[mNode->tileId().text()] = mNodeIsLeaf;
107 connect( mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsChunkQueueJob::finished );
109 const QFuture<void> future = QtConcurrent::run( [req = std::move( req ),
this] {
110 const QgsEventTracing::ScopedEvent e( u
"3D"_s, u
"VL chunk load"_s );
114 int featureCount = 0;
115 bool featureLimitReached =
false;
121 if ( ++featureCount > mFactory->mMaxFeatures )
123 featureLimitReached =
true;
127 mRenderContext.expressionContext().setFeature( f );
128 mHandler->processFeature( f, mRenderContext );
131 if ( !featureLimitReached )
133 QgsDebugMsgLevel( u
"All features fetched for node: %1"_s.arg( mNode->tileId().text() ), 3 );
141 mFutureWatcher->setFuture( future );
144QgsVectorLayerChunkLoader::~QgsVectorLayerChunkLoader()
146 if ( mFutureWatcher && !mFutureWatcher->isFinished() )
148 disconnect( mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsChunkQueueJob::finished );
149 mFutureWatcher->waitForFinished();
153void QgsVectorLayerChunkLoader::cancel()
158Qt3DCore::QEntity *QgsVectorLayerChunkLoader::createEntity( Qt3DCore::QEntity *parent )
160 if ( mHandler->featureCount() == 0 )
165 mNode->updateParentBoundingBoxesRecursively();
169 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity( parent );
170 entity->setObjectName( mLayerName +
"_" + mNode->tileId().text() );
171 mHandler->finalize( entity, mRenderContext );
174 if ( mHandler->zMinimum() != std::numeric_limits<float>::max() && mHandler->zMaximum() != std::numeric_limits<float>::lowest() )
179 mNode->setExactBox3D( box );
180 mNode->updateParentBoundingBoxesRecursively();
191 : mRenderContext( context )
193 , mSymbol( symbol->clone() )
194 , mMaxFeatures( maxFeatures )
200 QgsDebugError( u
"Vector layers in globe scenes are not supported yet!"_s );
201 setupQuadtree( QgsBox3D( -7e6, -7e6, -7e6, 7e6, 7e6, 7e6 ), -1, 3 );
207 rootBox3D.
grow( 1.0 );
210 setupQuadtree( rootBox3D, rootError );
213QgsChunkLoader *QgsVectorLayerChunkLoaderFactory::createChunkLoader( QgsChunkNode *node )
const
215 return new QgsVectorLayerChunkLoader(
this, node );
218bool QgsVectorLayerChunkLoaderFactory::canCreateChildren( QgsChunkNode *node )
220 return mNodesAreLeafs.contains( node->tileId().text() );
223QVector<QgsChunkNode *> QgsVectorLayerChunkLoaderFactory::createChildren( QgsChunkNode *node )
const
225 if ( mNodesAreLeafs.value( node->tileId().text(),
false ) )
228 return QgsQuadtreeChunkLoaderFactory::createChildren( node );
236 : QgsChunkedEntity( map, 3, new QgsVectorLayerChunkLoaderFactory(
Qgs3DRenderContext::fromMapSettings( map ), vl, symbol, zMin, zMax, tilingSettings.maximumChunkFeatures() ), true )
238 mTransform =
new Qt3DCore::QTransform;
239 if ( applyTerrainOffset() )
243 this->addComponent( mTransform );
250QgsVectorLayerChunkedEntity::~QgsVectorLayerChunkedEntity()
257bool QgsVectorLayerChunkedEntity::applyTerrainOffset()
const
259 QgsVectorLayerChunkLoaderFactory *loaderFactory =
static_cast<QgsVectorLayerChunkLoaderFactory *
>( mChunkLoaderFactory );
262 QString symbolType = loaderFactory->mSymbol.get()->type();
263 if ( symbolType ==
"line" )
271 else if ( symbolType ==
"point" )
279 else if ( symbolType ==
"polygon" )
289 QgsDebugMsgLevel( u
"QgsVectorLayerChunkedEntity::applyTerrainOffset, unhandled symbol type %1"_s.arg( symbolType ), 2 );
296void QgsVectorLayerChunkedEntity::onTerrainElevationOffsetChanged()
298 QgsDebugMsgLevel( u
"QgsVectorLayerChunkedEntity::onTerrainElevationOffsetChanged"_s, 2 );
299 float newOffset =
static_cast<float>( qobject_cast<Qgs3DMapSettings *>( sender() )->terrainSettings()->elevationOffset() );
300 if ( !applyTerrainOffset() )
304 mTransform->setTranslation( QVector3D( 0.0f, 0.0f, newOffset ) );
307QList<QgsRayCastHit> QgsVectorLayerChunkedEntity::rayIntersection(
const QgsRay3D &ray,
const QgsRayCastContext &context )
const
309 return QgsVectorLayerChunkedEntity::rayIntersection( activeNodes(), mTransform->matrix(), ray, context, mMapSettings->origin() );
312QList<QgsRayCastHit> QgsVectorLayerChunkedEntity::rayIntersection(
const QList<QgsChunkNode *> &activeNodes,
const QMatrix4x4 &transformMatrix,
const QgsRay3D &ray,
const QgsRayCastContext &context,
const QgsVector3D &origin )
320 int ignoredGeometries = 0;
322 QList<QgsRayCastHit> result;
325 QVector3D intersectionPoint;
328 for ( QgsChunkNode *node : activeNodes )
341 const QList<Qt3DRender::QGeometryRenderer *> rendLst = node->entity()->findChildren<Qt3DRender::QGeometryRenderer *>();
342 for (
const auto &rend : rendLst )
344 auto *geom = rend->geometry();
354 QVector3D nodeIntPoint;
355 int triangleIndex = -1;
360 const QMatrix4x4 nodeTransformMatrix = node->entity()->findChild<QgsGeoTransform *>()->matrix();
361 const QMatrix4x4 fullTransformMatrix = transformMatrix * nodeTransformMatrix;
367 float dist = ( ray.
origin() - nodeIntPoint ).length();
368 if ( minDist < 0 || dist < minDist )
371 intersectionPoint = nodeIntPoint;
384 result.append( hit );
386 QgsDebugMsgLevel( u
"Active Nodes: %1, checked nodes: %2, hits found: %3, incompatible geometries: %4"_s.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...
@ Geocentric
Geocentric CRS.
const QgsAbstractTerrainSettings * terrainSettings() const
Returns the terrain settings.
void terrainSettingsChanged()
Emitted when the terrain settings are changed.
Rendering context for preparation of 3D entities.
QgsCoordinateReferenceSystem crs() const
Returns the coordinate reference system used in the 3D scene.
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 QgsVector3D worldToMapCoordinates(const QgsVector3D &worldCoords, const QgsVector3D &origin)
Converts 3D world coordinates to map coordinates (applies offset).
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.
Abstract base class for 3D symbols that are used by VectorLayer3DRenderer objects.
double elevationOffset() const
Returns the elevation offset of the terrain (used to move the terrain up or down).
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.
void grow(double delta)
Grows the box in place by the specified amount in all dimensions.
Qgis::CrsType type() const
Returns the type of the CRS.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
void appendScopes(const QList< QgsExpressionContextScope * > &scopes)
Appends a list of scopes to the end of the context.
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.
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...
3D symbol that draws linestring geometries as planar polygons (created from lines using a buffer with...
Qgis::AltitudeClamping altitudeClamping() const
Returns method that determines altitude (whether to clamp to feature to terrain).
QgsCoordinateReferenceSystem crs3D
3D symbol that draws point geometries as 3D objects using one of the predefined shapes.
Qgis::AltitudeClamping altitudeClamping() const
Returns method that determines altitude (whether to clamp to feature to terrain).
3D symbol that draws polygon geometries as planar polygons, optionally extruded (with added walls).
Qgis::AltitudeClamping altitudeClamping() const
Returns method that determines altitude (whether to clamp to feature to terrain).
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.
A rectangle specified with double values.
Qt3DRender::QGeometry subclass that represents polygons tessellated into 3D geometry.
QgsFeatureId triangleIndexToFeatureId(uint triangleIndex) const
Returns ID of the feature to which given triangle index belongs (used for picking).
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
Defines configuration of how a vector layer gets tiled for 3D rendering.
static double tileGeometryErrorRatio()
This is the ratio of tile's largest size to geometry error and is used when setting the root tile's e...
bool showBoundingBoxes() const
Returns whether to display bounding boxes of entity's tiles (for debugging).
static double maximumLeafExtent()
This is the maximum width or height a tile can have and still be considered a leaf node.
Partial snapshot of vector layer's state (only the members necessary for access to features).
Represents a vector layer which manages a vector based dataset.
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.
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)