QGIS API Documentation 3.38.0-Grenoble (exported)
Loading...
Searching...
No Matches
qgsrulebasedchunkloader_p.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrulebasedchunkloader_p.cpp
3 --------------------------------------
4 Date : November 2019
5 Copyright : (C) 2019 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
18
19#include "qgs3dutils.h"
20#include "qgsline3dsymbol.h"
21#include "qgspoint3dsymbol.h"
22#include "qgspolygon3dsymbol.h"
24#include "qgschunknode_p.h"
25#include "qgseventtracing.h"
26
27#include "qgsvectorlayer.h"
29
32
33#include <QtConcurrent>
34#include <Qt3DCore/QTransform>
35
37
38
39QgsRuleBasedChunkLoader::QgsRuleBasedChunkLoader( const QgsRuleBasedChunkLoaderFactory *factory, QgsChunkNode *node )
40 : QgsChunkLoader( node )
41 , mFactory( factory )
42 , mContext( factory->mMap )
43 , mSource( new QgsVectorLayerFeatureSource( factory->mLayer ) )
44{
45 if ( node->level() < mFactory->mLeafLevel )
46 {
47 QTimer::singleShot( 0, this, &QgsRuleBasedChunkLoader::finished );
48 return;
49 }
50
51 QgsVectorLayer *layer = mFactory->mLayer;
52 const Qgs3DMapSettings &map = mFactory->mMap;
53
55 exprContext.setFields( layer->fields() );
56 mContext.setExpressionContext( exprContext );
57
58 // factory is shared among multiple loaders which may be run at the same time
59 // so we need a local copy of our rule tree that does not intefere with others
60 // (e.g. it happened that filter expressions with invalid syntax would cause
61 // nasty crashes when trying to simultaneously record evaluation error)
62 mRootRule.reset( mFactory->mRootRule->clone() );
63
64 mRootRule->createHandlers( layer, mHandlers );
65
66 QSet<QString> attributeNames;
67 mRootRule->prepare( mContext, attributeNames, mHandlers );
68
69 // build the feature request
71 req.setDestinationCrs( map.crs(), map.transformContext() );
72 req.setSubsetOfAttributes( attributeNames, layer->fields() );
73
74 // only a subset of data to be queried
75 const QgsRectangle rect = Qgs3DUtils::worldToMapExtent( node->bbox(), map.origin() );
76 req.setFilterRect( rect );
77
78 //
79 // this will be run in a background thread
80 //
81 mFutureWatcher = new QFutureWatcher<void>( this );
82 connect( mFutureWatcher, &QFutureWatcher<void>::finished, this, &QgsChunkQueueJob::finished );
83
84 const QFuture<void> future = QtConcurrent::run( [req, this]
85 {
86 const QgsEventTracing::ScopedEvent e( QStringLiteral( "3D" ), QStringLiteral( "RB chunk load" ) );
87
88 QgsFeature f;
89 QgsFeatureIterator fi = mSource->getFeatures( req );
90 while ( fi.nextFeature( f ) )
91 {
92 if ( mCanceled )
93 break;
94 mContext.expressionContext().setFeature( f );
95 mRootRule->registerFeature( f, mContext, mHandlers );
96 }
97 } );
98
99 // emit finished() as soon as the handler is populated with features
100 mFutureWatcher->setFuture( future );
101}
102
103QgsRuleBasedChunkLoader::~QgsRuleBasedChunkLoader()
104{
105 if ( mFutureWatcher && !mFutureWatcher->isFinished() )
106 {
107 disconnect( mFutureWatcher, &QFutureWatcher<void>::finished, this, &QgsChunkQueueJob::finished );
108 mFutureWatcher->waitForFinished();
109 }
110
111 qDeleteAll( mHandlers );
112 mHandlers.clear();
113}
114
115void QgsRuleBasedChunkLoader::cancel()
116{
117 mCanceled = true;
118}
119
120Qt3DCore::QEntity *QgsRuleBasedChunkLoader::createEntity( Qt3DCore::QEntity *parent )
121{
122 if ( mNode->level() < mFactory->mLeafLevel )
123 {
124 return new Qt3DCore::QEntity( parent ); // dummy entity
125 }
126
127 long long featureCount = 0;
128 for ( auto it = mHandlers.constBegin(); it != mHandlers.constEnd(); ++it )
129 {
130 featureCount += it.value()->featureCount();
131 }
132 if ( featureCount == 0 )
133 {
134 // an empty node, so we return no entity. This tags the node as having no data and effectively removes it.
135 return nullptr;
136 }
137
138 Qt3DCore::QEntity *entity = new Qt3DCore::QEntity( parent );
139 float zMin = std::numeric_limits<float>::max();
140 float zMax = std::numeric_limits<float>::lowest();
141 for ( auto it = mHandlers.constBegin(); it != mHandlers.constEnd(); ++it )
142 {
143 QgsFeature3DHandler *handler = it.value();
144 handler->finalize( entity, mContext );
145 if ( handler->zMinimum() < zMin )
146 zMin = handler->zMinimum();
147 if ( handler->zMaximum() > zMax )
148 zMax = handler->zMaximum();
149 }
150
151 // fix the vertical range of the node from the estimated vertical range to the true range
152 if ( zMin != std::numeric_limits<float>::max() && zMax != std::numeric_limits<float>::lowest() )
153 {
154 QgsAABB box = mNode->bbox();
155 box.yMin = zMin;
156 box.yMax = zMax;
157 mNode->setExactBbox( box );
158 mNode->updateParentBoundingBoxesRecursively();
159 }
160
161 return entity;
162}
163
164
166
167
168QgsRuleBasedChunkLoaderFactory::QgsRuleBasedChunkLoaderFactory( const Qgs3DMapSettings &map, QgsVectorLayer *vl, QgsRuleBased3DRenderer::Rule *rootRule, int leafLevel, double zMin, double zMax )
169 : mMap( map )
170 , mLayer( vl )
171 , mRootRule( rootRule->clone() )
172 , mLeafLevel( leafLevel )
173{
174 const QgsAABB rootBbox = Qgs3DUtils::mapToWorldExtent( map.extent(), zMin, zMax, map.origin() );
175 setupQuadtree( rootBbox, -1, leafLevel ); // negative root error means that the node does not contain anything
176}
177
178QgsRuleBasedChunkLoaderFactory::~QgsRuleBasedChunkLoaderFactory() = default;
179
180QgsChunkLoader *QgsRuleBasedChunkLoaderFactory::createChunkLoader( QgsChunkNode *node ) const
181{
182 return new QgsRuleBasedChunkLoader( this, node );
183}
184
185
187
188QgsRuleBasedChunkedEntity::QgsRuleBasedChunkedEntity( QgsVectorLayer *vl, double zMin, double zMax, const QgsVectorLayer3DTilingSettings &tilingSettings, QgsRuleBased3DRenderer::Rule *rootRule, const Qgs3DMapSettings &map )
189 : QgsChunkedEntity( -1, // max. allowed screen error (negative tau means that we need to go until leaves are reached)
190 new QgsRuleBasedChunkLoaderFactory( map, vl, rootRule, tilingSettings.zoomLevelsCount() - 1, zMin, zMax ), true )
191{
192 mTransform = new Qt3DCore::QTransform;
193 if ( applyTerrainOffset() )
194 {
195 mTransform->setTranslation( QVector3D( 0.0f, map.terrainElevationOffset(), 0.0f ) );
196 }
197 this->addComponent( mTransform );
198 connect( &map, &Qgs3DMapSettings::terrainElevationOffsetChanged, this, &QgsRuleBasedChunkedEntity::onTerrainElevationOffsetChanged );
199
200 setShowBoundingBoxes( tilingSettings.showBoundingBoxes() );
201}
202
203QgsRuleBasedChunkedEntity::~QgsRuleBasedChunkedEntity()
204{
205 // cancel / wait for jobs
206 cancelActiveJobs();
207}
208
209// if the AltitudeClamping is `Absolute`, do not apply the offset
210bool QgsRuleBasedChunkedEntity::applyTerrainOffset() const
211{
212 QgsRuleBasedChunkLoaderFactory *loaderFactory = static_cast<QgsRuleBasedChunkLoaderFactory *>( mChunkLoaderFactory );
213 if ( loaderFactory )
214 {
215 QgsRuleBased3DRenderer::Rule *rule = loaderFactory->mRootRule.get();
216 if ( rule->symbol() )
217 {
218 QString symbolType = rule->symbol()->type();
219 if ( symbolType == "line" )
220 {
221 QgsLine3DSymbol *lineSymbol = static_cast<QgsLine3DSymbol *>( rule->symbol() );
222 if ( lineSymbol && lineSymbol->altitudeClamping() == Qgis::AltitudeClamping::Absolute )
223 {
224 return false;
225 }
226 }
227 else if ( symbolType == "point" )
228 {
229 QgsPoint3DSymbol *pointSymbol = static_cast<QgsPoint3DSymbol *>( rule->symbol() );
230 if ( pointSymbol && pointSymbol->altitudeClamping() == Qgis::AltitudeClamping::Absolute )
231 {
232 return false;
233 }
234 }
235 else if ( symbolType == "polygon" )
236 {
237 QgsPolygon3DSymbol *polygonSymbol = static_cast<QgsPolygon3DSymbol *>( rule->symbol() );
238 if ( polygonSymbol && polygonSymbol->altitudeClamping() == Qgis::AltitudeClamping::Absolute )
239 {
240 return false;
241 }
242 }
243 else
244 {
245 QgsDebugMsgLevel( QStringLiteral( "QgsRuleBasedChunkedEntityChunkedEntity::applyTerrainOffset, unhandled symbol type %1" ).arg( symbolType ), 2 );
246 }
247 }
248 }
249
250 return true;
251}
252
253void QgsRuleBasedChunkedEntity::onTerrainElevationOffsetChanged( float newOffset )
254{
255 float previousOffset = mTransform->translation()[1];
256 if ( !applyTerrainOffset() )
257 {
258 newOffset = 0.0;
259 }
260
261 if ( newOffset != previousOffset )
262 {
263 mTransform->setTranslation( QVector3D( 0.0f, newOffset, 0.0f ) );
264 }
265}
266
267QVector<QgsRayCastingUtils::RayHit> QgsRuleBasedChunkedEntity::rayIntersection( const QgsRayCastingUtils::Ray3D &ray, const QgsRayCastingUtils::RayCastContext &context ) const
268{
269 return QgsVectorLayerChunkedEntity::rayIntersection( activeNodes(), mTransform->matrix(), ray, context );
270}
@ Absolute
Elevation is taken directly from feature and is independent of terrain height (final elevation = feat...
QgsRectangle extent() const
Returns the 3D scene's 2D extent in the 3D scene'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)
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.
float yMax
Definition qgsaabb.h:90
float yMin
Definition qgsaabb.h:87
virtual QString type() const =0
Returns identifier of symbol type. Each 3D symbol implementation should return a different type.
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 & 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...
Definition qgsfeature.h:58
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)
Qgis::AltitudeClamping altitudeClamping() const
Returns method that determines altitude (whether to clamp to feature to terrain)
A rectangle specified with double values.
QgsAbstract3DSymbol * symbol() const
Returns the labeling settings.
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.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
Helper struct to store ray casting parameters.