34#include <Qt3DCore/QTransform>
35#include <QtConcurrent>
37#include "moc_qgsrulebasedchunkloader_p.cpp"
39using namespace Qt::StringLiterals;
44QgsRuleBasedChunkLoader::QgsRuleBasedChunkLoader(
const QgsRuleBasedChunkLoaderFactory *factory, QgsChunkNode *node )
45 : QgsChunkLoader( node )
47 , mContext( factory->mRenderContext )
52void QgsRuleBasedChunkLoader::start()
54 QgsChunkNode *node = chunk();
61 mContext.setExpressionContext( exprContext );
67 mRootRule.reset( mFactory->mRootRule->clone() );
69 mRootRule->createHandlers( layer, mHandlers );
71 QSet<QString> attributeNames;
72 mRootRule->prepare( mContext, attributeNames, node->box3D(), mHandlers );
85 mFutureWatcher =
new QFutureWatcher<void>(
this );
87 connect( mFutureWatcher, &QFutureWatcher<void>::finished,
this, [
this] {
89 mFactory->mNodesAreLeafs[mNode->tileId().text()] = mNodeIsLeaf;
92 connect( mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsChunkQueueJob::finished );
94 const QFuture<void> future = QtConcurrent::run( [req = std::move( req ),
this] {
95 const QgsEventTracing::ScopedEvent e( u
"3D"_s, u
"RB chunk load"_s );
100 bool featureLimitReached =
false;
106 if ( ++featureCount > mFactory->mMaxFeatures )
108 featureLimitReached =
true;
112 mContext.expressionContext().setFeature( f );
113 mRootRule->registerFeature( f, mContext, mHandlers );
115 if ( !featureLimitReached )
117 QgsDebugMsgLevel( u
"All features fetched for node: %1"_s.arg( mNode->tileId().text() ), 3 );
125 mFutureWatcher->setFuture( future );
128QgsRuleBasedChunkLoader::~QgsRuleBasedChunkLoader()
130 if ( mFutureWatcher && !mFutureWatcher->isFinished() )
132 disconnect( mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsChunkQueueJob::finished );
133 mFutureWatcher->waitForFinished();
136 qDeleteAll( mHandlers );
140void QgsRuleBasedChunkLoader::cancel()
145Qt3DCore::QEntity *QgsRuleBasedChunkLoader::createEntity( Qt3DCore::QEntity *parent )
147 long long featureCount = 0;
148 for (
auto it = mHandlers.constBegin(); it != mHandlers.constEnd(); ++it )
150 featureCount += it.value()->featureCount();
152 if ( featureCount == 0 )
158 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity( parent );
159 float zMin = std::numeric_limits<float>::max();
160 float zMax = std::numeric_limits<float>::lowest();
161 for (
auto it = mHandlers.constBegin(); it != mHandlers.constEnd(); ++it )
163 QgsFeature3DHandler *handler = it.value();
164 handler->finalize( entity, mContext );
165 if ( handler->zMinimum() < zMin )
166 zMin = handler->zMinimum();
167 if ( handler->zMaximum() > zMax )
168 zMax = handler->zMaximum();
172 if ( zMin != std::numeric_limits<float>::max() && zMax != std::numeric_limits<float>::lowest() )
177 mNode->setExactBox3D( box );
178 mNode->updateParentBoundingBoxesRecursively();
189 : mRenderContext( context )
191 , mRootRule( rootRule->clone() )
192 , mMaxFeatures( maxFeatures )
198 QgsDebugError( u
"Vector layers in globe scenes are not supported yet!"_s );
199 setupQuadtree( QgsBox3D( -7e6, -7e6, -7e6, 7e6, 7e6, 7e6 ), -1, 3 );
205 rootBox3D.
grow( 1.0 );
208 setupQuadtree( rootBox3D, rootError );
211QgsRuleBasedChunkLoaderFactory::~QgsRuleBasedChunkLoaderFactory() =
default;
213QgsChunkLoader *QgsRuleBasedChunkLoaderFactory::createChunkLoader( QgsChunkNode *node )
const
215 return new QgsRuleBasedChunkLoader(
this, node );
218bool QgsRuleBasedChunkLoaderFactory::canCreateChildren( QgsChunkNode *node )
220 return mNodesAreLeafs.contains( node->tileId().text() );
223QVector<QgsChunkNode *> QgsRuleBasedChunkLoaderFactory::createChildren( QgsChunkNode *node )
const
225 if ( mNodesAreLeafs.value( node->tileId().text(),
false ) )
228 return QgsQuadtreeChunkLoaderFactory::createChildren( node );
234 : QgsChunkedEntity( map, 3, new QgsRuleBasedChunkLoaderFactory(
Qgs3DRenderContext::fromMapSettings( map ), vl, rootRule, zMin, zMax, tilingSettings.maximumChunkFeatures() ), true )
236 mTransform =
new Qt3DCore::QTransform;
237 if ( applyTerrainOffset() )
241 this->addComponent( mTransform );
247QgsRuleBasedChunkedEntity::~QgsRuleBasedChunkedEntity()
254bool QgsRuleBasedChunkedEntity::applyTerrainOffset()
const
256 QgsRuleBasedChunkLoaderFactory *loaderFactory =
static_cast<QgsRuleBasedChunkLoaderFactory *
>( mChunkLoaderFactory );
261 for (
const auto &rule : rules )
263 if ( rule->symbol() )
265 QString symbolType = rule->symbol()->type();
266 if ( symbolType ==
"line" )
274 else if ( symbolType ==
"point" )
282 else if ( symbolType ==
"polygon" )
292 QgsDebugMsgLevel( u
"QgsRuleBasedChunkedEntityChunkedEntity::applyTerrainOffset, unhandled symbol type %1"_s.arg( symbolType ), 2 );
301void QgsRuleBasedChunkedEntity::onTerrainElevationOffsetChanged()
303 const float previousOffset = mTransform->translation()[1];
304 float newOffset =
static_cast<float>( qobject_cast<Qgs3DMapSettings *>( sender() )->terrainSettings()->elevationOffset() );
305 if ( !applyTerrainOffset() )
310 if ( newOffset != previousOffset )
312 mTransform->setTranslation( QVector3D( 0.0f, 0.0f, newOffset ) );
316QList<QgsRayCastHit> QgsRuleBasedChunkedEntity::rayIntersection(
const QgsRay3D &ray,
const QgsRayCastContext &context )
const
318 return QgsVectorLayerChunkedEntity::rayIntersection( activeNodes(), mTransform->matrix(), ray, context, mMapSettings->origin() );
@ 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.
double elevationOffset() const
Returns the elevation offset of the terrain (used to move the terrain up or down).
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 & 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...
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).
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.
Responsible for defining parameters of the ray casting operations in 3D map canvases.
A rectangle specified with double values.
A child rule for a QgsRuleBased3DRenderer.
const QgsRuleBased3DRenderer::RuleList & children() const
Returns all children rules of this rule.
QList< QgsRuleBased3DRenderer::Rule * > RuleList
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.
#define QgsDebugMsgLevel(str, level)