QGIS API Documentation 3.41.0-Master (3440c17df1d)
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
17#include "moc_qgsrulebasedchunkloader_p.cpp"
19
20#include "qgs3dutils.h"
21#include "qgsline3dsymbol.h"
22#include "qgspoint3dsymbol.h"
23#include "qgspolygon3dsymbol.h"
25#include "qgschunknode.h"
26#include "qgseventtracing.h"
27
28#include "qgsvectorlayer.h"
30
33
34#include <QtConcurrent>
35#include <Qt3DCore/QTransform>
36
38
39
40QgsRuleBasedChunkLoader::QgsRuleBasedChunkLoader( const QgsRuleBasedChunkLoaderFactory *factory, QgsChunkNode *node )
41 : QgsChunkLoader( node )
42 , mFactory( factory )
43 , mContext( factory->mRenderContext )
44 , mSource( new QgsVectorLayerFeatureSource( factory->mLayer ) )
45{
46 if ( node->level() < mFactory->mLeafLevel )
47 {
48 QTimer::singleShot( 0, this, &QgsRuleBasedChunkLoader::finished );
49 return;
50 }
51
52 QgsVectorLayer *layer = mFactory->mLayer;
53
54 // only a subset of data to be queried
55 const QgsRectangle rect = node->box3D().toRectangle();
56 // origin for coordinates of the chunk - it is kind of arbitrary, but it should be
57 // picked so that the coordinates are relatively small to avoid numerical precision issues
58 QgsVector3D chunkOrigin( rect.center().x(), rect.center().y(), 0 );
59
61 exprContext.setFields( layer->fields() );
62 mContext.setExpressionContext( exprContext );
63
64 // factory is shared among multiple loaders which may be run at the same time
65 // so we need a local copy of our rule tree that does not intefere with others
66 // (e.g. it happened that filter expressions with invalid syntax would cause
67 // nasty crashes when trying to simultaneously record evaluation error)
68 mRootRule.reset( mFactory->mRootRule->clone() );
69
70 mRootRule->createHandlers( layer, mHandlers );
71
72 QSet<QString> attributeNames;
73 mRootRule->prepare( mContext, attributeNames, chunkOrigin, mHandlers );
74
75 // build the feature request
77 req.setDestinationCrs( mContext.crs(), mContext.transformContext() );
78 req.setSubsetOfAttributes( attributeNames, layer->fields() );
79 req.setFilterRect( rect );
80
81 //
82 // this will be run in a background thread
83 //
84 mFutureWatcher = new QFutureWatcher<void>( this );
85 connect( mFutureWatcher, &QFutureWatcher<void>::finished, this, &QgsChunkQueueJob::finished );
86
87 const QFuture<void> future = QtConcurrent::run( [req, this]
88 {
89 const QgsEventTracing::ScopedEvent e( QStringLiteral( "3D" ), QStringLiteral( "RB chunk load" ) );
90
91 QgsFeature f;
92 QgsFeatureIterator fi = mSource->getFeatures( req );
93 while ( fi.nextFeature( f ) )
94 {
95 if ( mCanceled )
96 break;
97 mContext.expressionContext().setFeature( f );
98 mRootRule->registerFeature( f, mContext, mHandlers );
99 }
100 } );
101
102 // emit finished() as soon as the handler is populated with features
103 mFutureWatcher->setFuture( future );
104}
105
106QgsRuleBasedChunkLoader::~QgsRuleBasedChunkLoader()
107{
108 if ( mFutureWatcher && !mFutureWatcher->isFinished() )
109 {
110 disconnect( mFutureWatcher, &QFutureWatcher<void>::finished, this, &QgsChunkQueueJob::finished );
111 mFutureWatcher->waitForFinished();
112 }
113
114 qDeleteAll( mHandlers );
115 mHandlers.clear();
116}
117
118void QgsRuleBasedChunkLoader::cancel()
119{
120 mCanceled = true;
121}
122
123Qt3DCore::QEntity *QgsRuleBasedChunkLoader::createEntity( Qt3DCore::QEntity *parent )
124{
125 if ( mNode->level() < mFactory->mLeafLevel )
126 {
127 return new Qt3DCore::QEntity( parent ); // dummy entity
128 }
129
130 long long featureCount = 0;
131 for ( auto it = mHandlers.constBegin(); it != mHandlers.constEnd(); ++it )
132 {
133 featureCount += it.value()->featureCount();
134 }
135 if ( featureCount == 0 )
136 {
137 // an empty node, so we return no entity. This tags the node as having no data and effectively removes it.
138 return nullptr;
139 }
140
141 Qt3DCore::QEntity *entity = new Qt3DCore::QEntity( parent );
142 float zMin = std::numeric_limits<float>::max();
143 float zMax = std::numeric_limits<float>::lowest();
144 for ( auto it = mHandlers.constBegin(); it != mHandlers.constEnd(); ++it )
145 {
146 QgsFeature3DHandler *handler = it.value();
147 handler->finalize( entity, mContext );
148 if ( handler->zMinimum() < zMin )
149 zMin = handler->zMinimum();
150 if ( handler->zMaximum() > zMax )
151 zMax = handler->zMaximum();
152 }
153
154 // fix the vertical range of the node from the estimated vertical range to the true range
155 if ( zMin != std::numeric_limits<float>::max() && zMax != std::numeric_limits<float>::lowest() )
156 {
157 QgsBox3D box = mNode->box3D();
158 box.setZMinimum( zMin );
159 box.setZMaximum( zMax );
160 mNode->setExactBox3D( box );
161 mNode->updateParentBoundingBoxesRecursively();
162 }
163
164 return entity;
165}
166
167
169
170
171QgsRuleBasedChunkLoaderFactory::QgsRuleBasedChunkLoaderFactory( const Qgs3DRenderContext &context, QgsVectorLayer *vl, QgsRuleBased3DRenderer::Rule *rootRule, int leafLevel, double zMin, double zMax )
172 : mRenderContext( context )
173 , mLayer( vl )
174 , mRootRule( rootRule->clone() )
175 , mLeafLevel( leafLevel )
176{
177 const QgsBox3D rootBox3D( context.extent(), zMin, zMax );
178 setupQuadtree( rootBox3D, -1, leafLevel ); // negative root error means that the node does not contain anything
179}
180
181QgsRuleBasedChunkLoaderFactory::~QgsRuleBasedChunkLoaderFactory() = default;
182
183QgsChunkLoader *QgsRuleBasedChunkLoaderFactory::createChunkLoader( QgsChunkNode *node ) const
184{
185 return new QgsRuleBasedChunkLoader( this, node );
186}
187
188
190
191QgsRuleBasedChunkedEntity::QgsRuleBasedChunkedEntity( Qgs3DMapSettings *map, QgsVectorLayer *vl, double zMin, double zMax, const QgsVectorLayer3DTilingSettings &tilingSettings, QgsRuleBased3DRenderer::Rule *rootRule )
192 : QgsChunkedEntity( map,
193 -1, // max. allowed screen error (negative tau means that we need to go until leaves are reached)
194 new QgsRuleBasedChunkLoaderFactory( Qgs3DRenderContext::fromMapSettings( map ), vl, rootRule, tilingSettings.zoomLevelsCount() - 1, zMin, zMax ), true )
195{
196 mTransform = new Qt3DCore::QTransform;
197 if ( applyTerrainOffset() )
198 {
199 mTransform->setTranslation( QVector3D( 0.0f, map->terrainElevationOffset(), 0.0f ) );
200 }
201 this->addComponent( mTransform );
202 connect( map, &Qgs3DMapSettings::terrainElevationOffsetChanged, this, &QgsRuleBasedChunkedEntity::onTerrainElevationOffsetChanged );
203
204 setShowBoundingBoxes( tilingSettings.showBoundingBoxes() );
205}
206
207QgsRuleBasedChunkedEntity::~QgsRuleBasedChunkedEntity()
208{
209 // cancel / wait for jobs
210 cancelActiveJobs();
211}
212
213// if the AltitudeClamping is `Absolute`, do not apply the offset
214bool QgsRuleBasedChunkedEntity::applyTerrainOffset() const
215{
216 QgsRuleBasedChunkLoaderFactory *loaderFactory = static_cast<QgsRuleBasedChunkLoaderFactory *>( mChunkLoaderFactory );
217 if ( loaderFactory )
218 {
219 QgsRuleBased3DRenderer::Rule *rule = loaderFactory->mRootRule.get();
220 if ( rule->symbol() )
221 {
222 QString symbolType = rule->symbol()->type();
223 if ( symbolType == "line" )
224 {
225 QgsLine3DSymbol *lineSymbol = static_cast<QgsLine3DSymbol *>( rule->symbol() );
226 if ( lineSymbol && lineSymbol->altitudeClamping() == Qgis::AltitudeClamping::Absolute )
227 {
228 return false;
229 }
230 }
231 else if ( symbolType == "point" )
232 {
233 QgsPoint3DSymbol *pointSymbol = static_cast<QgsPoint3DSymbol *>( rule->symbol() );
234 if ( pointSymbol && pointSymbol->altitudeClamping() == Qgis::AltitudeClamping::Absolute )
235 {
236 return false;
237 }
238 }
239 else if ( symbolType == "polygon" )
240 {
241 QgsPolygon3DSymbol *polygonSymbol = static_cast<QgsPolygon3DSymbol *>( rule->symbol() );
242 if ( polygonSymbol && polygonSymbol->altitudeClamping() == Qgis::AltitudeClamping::Absolute )
243 {
244 return false;
245 }
246 }
247 else
248 {
249 QgsDebugMsgLevel( QStringLiteral( "QgsRuleBasedChunkedEntityChunkedEntity::applyTerrainOffset, unhandled symbol type %1" ).arg( symbolType ), 2 );
250 }
251 }
252 }
253
254 return true;
255}
256
257void QgsRuleBasedChunkedEntity::onTerrainElevationOffsetChanged( float newOffset )
258{
259 float previousOffset = mTransform->translation()[1];
260 if ( !applyTerrainOffset() )
261 {
262 newOffset = 0.0;
263 }
264
265 if ( newOffset != previousOffset )
266 {
267 mTransform->setTranslation( QVector3D( 0.0f, newOffset, 0.0f ) );
268 }
269}
270
271QVector<QgsRayCastingUtils::RayHit> QgsRuleBasedChunkedEntity::rayIntersection( const QgsRayCastingUtils::Ray3D &ray, const QgsRayCastingUtils::RayCastContext &context ) const
272{
273 return QgsVectorLayerChunkedEntity::rayIntersection( activeNodes(), mTransform->matrix(), ray, context, mMapSettings->origin() );
274}
@ 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.
static QgsExpressionContext globalProjectLayerExpressionContext(QgsVectorLayer *layer)
Returns expression context for use in preparation of 3D data of a layer.
virtual QString type() const =0
Returns identifier of symbol type. Each 3D symbol implementation should return a different type.
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:43
void setZMinimum(double z)
Sets the minimum z value.
Definition qgsbox3d.cpp:88
void setZMaximum(double z)
Sets the maximum z value.
Definition qgsbox3d.cpp:93
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)
double y
Definition qgspointxy.h:64
double x
Definition qgspointxy.h:63
Qgis::AltitudeClamping altitudeClamping() const
Returns method that determines altitude (whether to clamp to feature to terrain)
A rectangle specified with double values.
QgsPointXY center
QgsAbstract3DSymbol * symbol() const
Returns the labeling settings.
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
Definition qgsvector3d.h:31
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.