QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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"
21#include "qgschunknode_p.h"
23#include "qgseventtracing.h"
24
25#include "qgsvectorlayer.h"
27
30
31#include <QtConcurrent>
32#include <Qt3DCore/QTransform>
33
35
36
37QgsRuleBasedChunkLoader::QgsRuleBasedChunkLoader( const QgsRuleBasedChunkLoaderFactory *factory, QgsChunkNode *node )
38 : QgsChunkLoader( node )
39 , mFactory( factory )
40 , mContext( factory->mMap )
41 , mSource( new QgsVectorLayerFeatureSource( factory->mLayer ) )
42{
43 if ( node->level() < mFactory->mLeafLevel )
44 {
45 QTimer::singleShot( 0, this, &QgsRuleBasedChunkLoader::finished );
46 return;
47 }
48
49 QgsVectorLayer *layer = mFactory->mLayer;
50 const Qgs3DMapSettings &map = mFactory->mMap;
51
53 exprContext.setFields( layer->fields() );
54 mContext.setExpressionContext( exprContext );
55
56 // factory is shared among multiple loaders which may be run at the same time
57 // so we need a local copy of our rule tree that does not intefere with others
58 // (e.g. it happened that filter expressions with invalid syntax would cause
59 // nasty crashes when trying to simultaneously record evaluation error)
60 mRootRule.reset( mFactory->mRootRule->clone() );
61
62 mRootRule->createHandlers( layer, mHandlers );
63
64 QSet<QString> attributeNames;
65 mRootRule->prepare( mContext, attributeNames, mHandlers );
66
67 // build the feature request
69 req.setDestinationCrs( map.crs(), map.transformContext() );
70 req.setSubsetOfAttributes( attributeNames, layer->fields() );
71
72 // only a subset of data to be queried
73 const QgsRectangle rect = Qgs3DUtils::worldToMapExtent( node->bbox(), map.origin() );
74 req.setFilterRect( rect );
75
76 //
77 // this will be run in a background thread
78 //
79 mFutureWatcher = new QFutureWatcher<void>( this );
80 connect( mFutureWatcher, &QFutureWatcher<void>::finished, this, &QgsChunkQueueJob::finished );
81
82 const QFuture<void> future = QtConcurrent::run( [req, this]
83 {
84 const QgsEventTracing::ScopedEvent e( QStringLiteral( "3D" ), QStringLiteral( "RB chunk load" ) );
85
86 QgsFeature f;
87 QgsFeatureIterator fi = mSource->getFeatures( req );
88 while ( fi.nextFeature( f ) )
89 {
90 if ( mCanceled )
91 break;
92 mContext.expressionContext().setFeature( f );
93 mRootRule->registerFeature( f, mContext, mHandlers );
94 }
95 } );
96
97 // emit finished() as soon as the handler is populated with features
98 mFutureWatcher->setFuture( future );
99}
100
101QgsRuleBasedChunkLoader::~QgsRuleBasedChunkLoader()
102{
103 if ( mFutureWatcher && !mFutureWatcher->isFinished() )
104 {
105 disconnect( mFutureWatcher, &QFutureWatcher<void>::finished, this, &QgsChunkQueueJob::finished );
106 mFutureWatcher->waitForFinished();
107 }
108
109 qDeleteAll( mHandlers );
110 mHandlers.clear();
111}
112
113void QgsRuleBasedChunkLoader::cancel()
114{
115 mCanceled = true;
116}
117
118Qt3DCore::QEntity *QgsRuleBasedChunkLoader::createEntity( Qt3DCore::QEntity *parent )
119{
120 if ( mNode->level() < mFactory->mLeafLevel )
121 {
122 return new Qt3DCore::QEntity( parent ); // dummy entity
123 }
124
125 long long featureCount = 0;
126 for ( auto it = mHandlers.constBegin(); it != mHandlers.constEnd(); ++it )
127 {
128 featureCount += it.value()->featureCount();
129 }
130 if ( featureCount == 0 )
131 {
132 // an empty node, so we return no entity. This tags the node as having no data and effectively removes it.
133 return nullptr;
134 }
135
136 Qt3DCore::QEntity *entity = new Qt3DCore::QEntity( parent );
137 float zMin = std::numeric_limits<float>::max();
138 float zMax = std::numeric_limits<float>::lowest();
139 for ( auto it = mHandlers.constBegin(); it != mHandlers.constEnd(); ++it )
140 {
141 QgsFeature3DHandler *handler = it.value();
142 handler->finalize( entity, mContext );
143 if ( handler->zMinimum() < zMin )
144 zMin = handler->zMinimum();
145 if ( handler->zMaximum() > zMax )
146 zMax = handler->zMaximum();
147 }
148
149 // fix the vertical range of the node from the estimated vertical range to the true range
150 if ( zMin != std::numeric_limits<float>::max() && zMax != std::numeric_limits<float>::lowest() )
151 {
152 QgsAABB box = mNode->bbox();
153 box.yMin = zMin;
154 box.yMax = zMax;
155 mNode->setExactBbox( box );
156 mNode->updateParentBoundingBoxesRecursively();
157 }
158
159 return entity;
160}
161
162
164
165
166QgsRuleBasedChunkLoaderFactory::QgsRuleBasedChunkLoaderFactory( const Qgs3DMapSettings &map, QgsVectorLayer *vl, QgsRuleBased3DRenderer::Rule *rootRule, int leafLevel, double zMin, double zMax )
167 : mMap( map )
168 , mLayer( vl )
169 , mRootRule( rootRule->clone() )
170 , mLeafLevel( leafLevel )
171{
172 const QgsAABB rootBbox = Qgs3DUtils::mapToWorldExtent( map.extent(), zMin, zMax, map.origin() );
173 setupQuadtree( rootBbox, -1, leafLevel ); // negative root error means that the node does not contain anything
174}
175
176QgsRuleBasedChunkLoaderFactory::~QgsRuleBasedChunkLoaderFactory() = default;
177
178QgsChunkLoader *QgsRuleBasedChunkLoaderFactory::createChunkLoader( QgsChunkNode *node ) const
179{
180 return new QgsRuleBasedChunkLoader( this, node );
181}
182
183
185
186QgsRuleBasedChunkedEntity::QgsRuleBasedChunkedEntity( QgsVectorLayer *vl, double zMin, double zMax, const QgsVectorLayer3DTilingSettings &tilingSettings, QgsRuleBased3DRenderer::Rule *rootRule, const Qgs3DMapSettings &map )
187 : QgsChunkedEntity( -1, // max. allowed screen error (negative tau means that we need to go until leaves are reached)
188 new QgsRuleBasedChunkLoaderFactory( map, vl, rootRule, tilingSettings.zoomLevelsCount() - 1, zMin, zMax ), true )
189{
190 mTransform = new Qt3DCore::QTransform;
191 mTransform->setTranslation( QVector3D( 0.0f, map.terrainElevationOffset(), 0.0f ) );
192 this->addComponent( mTransform );
193 connect( &map, &Qgs3DMapSettings::terrainElevationOffsetChanged, this, &QgsRuleBasedChunkedEntity::onTerrainElevationOffsetChanged );
194
195 setShowBoundingBoxes( tilingSettings.showBoundingBoxes() );
196}
197
198QgsRuleBasedChunkedEntity::~QgsRuleBasedChunkedEntity()
199{
200 // cancel / wait for jobs
201 cancelActiveJobs();
202}
203
204void QgsRuleBasedChunkedEntity::onTerrainElevationOffsetChanged( float newOffset )
205{
206 mTransform->setTranslation( QVector3D( 0.0f, newOffset, 0.0f ) );
207}
208
209QVector<QgsRayCastingUtils::RayHit> QgsRuleBasedChunkedEntity::rayIntersection( const QgsRayCastingUtils::Ray3D &ray, const QgsRayCastingUtils::RayCastContext &context ) const
210{
211 return QgsVectorLayerChunkedEntity::rayIntersection( activeNodes(), mTransform->matrix(), ray, context );
212}
QgsRectangle extent() const
Returns the 3D scene's 2D extent in project'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:88
float yMin
Definition qgsaabb.h:85
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)
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:56
A rectangle specified with double values.
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.
QgsFields fields() const FINAL
Returns the list of fields of this layer.