QGIS API Documentation 3.39.0-Master (d85f3c2a281)
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->mRenderContext )
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
54 exprContext.setFields( layer->fields() );
55 mContext.setExpressionContext( exprContext );
56
57 // factory is shared among multiple loaders which may be run at the same time
58 // so we need a local copy of our rule tree that does not intefere with others
59 // (e.g. it happened that filter expressions with invalid syntax would cause
60 // nasty crashes when trying to simultaneously record evaluation error)
61 mRootRule.reset( mFactory->mRootRule->clone() );
62
63 mRootRule->createHandlers( layer, mHandlers );
64
65 QSet<QString> attributeNames;
66 mRootRule->prepare( mContext, attributeNames, mHandlers );
67
68 // build the feature request
70 req.setDestinationCrs( mContext.crs(), mContext.transformContext() );
71 req.setSubsetOfAttributes( attributeNames, layer->fields() );
72
73 // only a subset of data to be queried
74 const QgsRectangle rect = Qgs3DUtils::worldToMapExtent( node->bbox(), mContext.origin() );
75 req.setFilterRect( rect );
76
77 //
78 // this will be run in a background thread
79 //
80 mFutureWatcher = new QFutureWatcher<void>( this );
81 connect( mFutureWatcher, &QFutureWatcher<void>::finished, this, &QgsChunkQueueJob::finished );
82
83 const QFuture<void> future = QtConcurrent::run( [req, this]
84 {
85 const QgsEventTracing::ScopedEvent e( QStringLiteral( "3D" ), QStringLiteral( "RB chunk load" ) );
86
87 QgsFeature f;
88 QgsFeatureIterator fi = mSource->getFeatures( req );
89 while ( fi.nextFeature( f ) )
90 {
91 if ( mCanceled )
92 break;
93 mContext.expressionContext().setFeature( f );
94 mRootRule->registerFeature( f, mContext, mHandlers );
95 }
96 } );
97
98 // emit finished() as soon as the handler is populated with features
99 mFutureWatcher->setFuture( future );
100}
101
102QgsRuleBasedChunkLoader::~QgsRuleBasedChunkLoader()
103{
104 if ( mFutureWatcher && !mFutureWatcher->isFinished() )
105 {
106 disconnect( mFutureWatcher, &QFutureWatcher<void>::finished, this, &QgsChunkQueueJob::finished );
107 mFutureWatcher->waitForFinished();
108 }
109
110 qDeleteAll( mHandlers );
111 mHandlers.clear();
112}
113
114void QgsRuleBasedChunkLoader::cancel()
115{
116 mCanceled = true;
117}
118
119Qt3DCore::QEntity *QgsRuleBasedChunkLoader::createEntity( Qt3DCore::QEntity *parent )
120{
121 if ( mNode->level() < mFactory->mLeafLevel )
122 {
123 return new Qt3DCore::QEntity( parent ); // dummy entity
124 }
125
126 long long featureCount = 0;
127 for ( auto it = mHandlers.constBegin(); it != mHandlers.constEnd(); ++it )
128 {
129 featureCount += it.value()->featureCount();
130 }
131 if ( featureCount == 0 )
132 {
133 // an empty node, so we return no entity. This tags the node as having no data and effectively removes it.
134 return nullptr;
135 }
136
137 Qt3DCore::QEntity *entity = new Qt3DCore::QEntity( parent );
138 float zMin = std::numeric_limits<float>::max();
139 float zMax = std::numeric_limits<float>::lowest();
140 for ( auto it = mHandlers.constBegin(); it != mHandlers.constEnd(); ++it )
141 {
142 QgsFeature3DHandler *handler = it.value();
143 handler->finalize( entity, mContext );
144 if ( handler->zMinimum() < zMin )
145 zMin = handler->zMinimum();
146 if ( handler->zMaximum() > zMax )
147 zMax = handler->zMaximum();
148 }
149
150 // fix the vertical range of the node from the estimated vertical range to the true range
151 if ( zMin != std::numeric_limits<float>::max() && zMax != std::numeric_limits<float>::lowest() )
152 {
153 QgsAABB box = mNode->bbox();
154 box.yMin = zMin;
155 box.yMax = zMax;
156 mNode->setExactBbox( box );
157 mNode->updateParentBoundingBoxesRecursively();
158 }
159
160 return entity;
161}
162
163
165
166
167QgsRuleBasedChunkLoaderFactory::QgsRuleBasedChunkLoaderFactory( const Qgs3DRenderContext &context, QgsVectorLayer *vl, QgsRuleBased3DRenderer::Rule *rootRule, int leafLevel, double zMin, double zMax )
168 : mRenderContext( context )
169 , mLayer( vl )
170 , mRootRule( rootRule->clone() )
171 , mLeafLevel( leafLevel )
172{
173 const QgsAABB rootBbox = Qgs3DUtils::mapToWorldExtent( context.extent(), zMin, zMax, context.origin() );
174 setupQuadtree( rootBbox, -1, leafLevel ); // negative root error means that the node does not contain anything
175}
176
177QgsRuleBasedChunkLoaderFactory::~QgsRuleBasedChunkLoaderFactory() = default;
178
179QgsChunkLoader *QgsRuleBasedChunkLoaderFactory::createChunkLoader( QgsChunkNode *node ) const
180{
181 return new QgsRuleBasedChunkLoader( this, node );
182}
183
184
186
187QgsRuleBasedChunkedEntity::QgsRuleBasedChunkedEntity( Qgs3DMapSettings *map, QgsVectorLayer *vl, double zMin, double zMax, const QgsVectorLayer3DTilingSettings &tilingSettings, QgsRuleBased3DRenderer::Rule *rootRule )
188 : QgsChunkedEntity( map,
189 -1, // max. allowed screen error (negative tau means that we need to go until leaves are reached)
190 new QgsRuleBasedChunkLoaderFactory( Qgs3DRenderContext::fromMapSettings( 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...
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.
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.