QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgschunknode_p.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgschunknode_p.cpp
3 --------------------------------------
4 Date : July 2017
5 Copyright : (C) 2017 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
16#include "qgschunknode_p.h"
17
18#include "qgschunkedentity_p.h" // for ChunkLoader destructor
19#include "qgschunklist_p.h"
20#include "qgschunkloader_p.h"
21#include <Qt3DCore/QEntity>
22
24
25QgsChunkNode::QgsChunkNode( const QgsChunkNodeId &nodeId, const QgsAABB &bbox, float error, QgsChunkNode *parent )
26 : mBbox( bbox )
27 , mError( error )
28 , mNodeId( nodeId )
29 , mParent( parent )
30 , mState( Skeleton )
31 , mLoaderQueueEntry( nullptr )
32 , mReplacementQueueEntry( nullptr )
33 , mLoader( nullptr )
34 , mEntity( nullptr )
35 , mUpdaterFactory( nullptr )
36 , mUpdater( nullptr )
37{
38}
39
40QgsChunkNode::~QgsChunkNode()
41{
42 Q_ASSERT( mState == Skeleton );
43 Q_ASSERT( !mLoaderQueueEntry );
44 Q_ASSERT( !mReplacementQueueEntry );
45 Q_ASSERT( !mLoader ); // should be deleted when removed from loader queue
46 Q_ASSERT( !mEntity ); // should be deleted when removed from replacement queue
47 Q_ASSERT( !mUpdater );
48 Q_ASSERT( !mUpdaterFactory );
49
50 qDeleteAll( mChildren );
51}
52
53bool QgsChunkNode::allChildChunksResident( QTime currentTime ) const
54{
55 Q_ASSERT( mChildrenPopulated );
56 for ( int i = 0; i < childCount(); ++i )
57 {
58 if ( mChildren[i]->mHasData && !mChildren[i]->mEntity )
59 return false; // no there yet
60 Q_UNUSED( currentTime ) // seems we do not need this extra time (it just brings extra problems)
61 //if (children[i]->entityCreatedTime.msecsTo(currentTime) < 100)
62 // return false; // allow some time for upload of stuff within Qt3D (TODO: better way to check it is ready?)
63 }
64 return true;
65}
66
67void QgsChunkNode::populateChildren( const QVector<QgsChunkNode *> &children )
68{
69 Q_ASSERT( !mChildrenPopulated );
70 mChildrenPopulated = true;
71 mChildren = children;
72}
73
74int QgsChunkNode::level() const
75{
76 int lvl = 0;
77 QgsChunkNode *p = mParent;
78 while ( p )
79 {
80 ++lvl;
81 p = p->mParent;
82 }
83 return lvl;
84}
85
86QList<QgsChunkNode *> QgsChunkNode::descendants()
87{
88 QList<QgsChunkNode *> lst;
89 lst << this;
90
91 for ( int i = 0; i < childCount(); ++i )
92 {
93 lst << mChildren[i]->descendants();
94 }
95
96 return lst;
97}
98
99void QgsChunkNode::setQueuedForLoad( QgsChunkListEntry *entry )
100{
101 Q_ASSERT( mState == Skeleton );
102 Q_ASSERT( !mLoaderQueueEntry );
103 Q_ASSERT( !mLoader );
104
105 mState = QgsChunkNode::QueuedForLoad;
106 mLoaderQueueEntry = entry;
107}
108
109void QgsChunkNode::cancelQueuedForLoad()
110{
111 Q_ASSERT( mState == QueuedForLoad );
112 Q_ASSERT( mLoaderQueueEntry );
113
114 delete mLoaderQueueEntry;
115 mLoaderQueueEntry = nullptr;
116
117 mState = QgsChunkNode::Skeleton;
118}
119
120void QgsChunkNode::setLoading( QgsChunkLoader *chunkLoader )
121{
122 Q_ASSERT( mState == QueuedForLoad );
123 Q_ASSERT( !mLoader );
124 Q_ASSERT( mLoaderQueueEntry );
125
126 mState = Loading;
127 mLoader = chunkLoader;
128 mLoaderQueueEntry = nullptr;
129}
130
131void QgsChunkNode::cancelLoading()
132{
133 Q_ASSERT( mState == QgsChunkNode::Loading );
134 Q_ASSERT( mLoader );
135 Q_ASSERT( !mLoaderQueueEntry );
136 Q_ASSERT( !mEntity );
137 Q_ASSERT( !mReplacementQueueEntry );
138
139 mLoader = nullptr; // not owned by chunk node
140
141 mState = QgsChunkNode::Skeleton;
142}
143
144void QgsChunkNode::setLoaded( Qt3DCore::QEntity *newEntity )
145{
146 Q_ASSERT( mState == QgsChunkNode::Loading );
147 Q_ASSERT( mLoader );
148 Q_ASSERT( !mLoaderQueueEntry );
149 Q_ASSERT( !mReplacementQueueEntry );
150
151 mEntity = newEntity;
152 mEntityCreatedTime = QTime::currentTime();
153
154 mLoader = nullptr; // not owned by chunk node
155
156 mState = QgsChunkNode::Loaded;
157 mReplacementQueueEntry = new QgsChunkListEntry( this );
158}
159
160void QgsChunkNode::unloadChunk()
161{
162 Q_ASSERT( mState == QgsChunkNode::Loaded );
163 Q_ASSERT( mEntity );
164 Q_ASSERT( mReplacementQueueEntry );
165
166 delete mEntity;
167 mEntity = nullptr;
168
169 delete mReplacementQueueEntry;
170 mReplacementQueueEntry = nullptr;
171 mState = QgsChunkNode::Skeleton;
172}
173
174void QgsChunkNode::setQueuedForUpdate( QgsChunkListEntry *entry, QgsChunkQueueJobFactory *updateJobFactory )
175{
176 Q_ASSERT( mState == QgsChunkNode::Loaded );
177 Q_ASSERT( mEntity );
178 Q_ASSERT( mReplacementQueueEntry );
179 Q_ASSERT( !mLoaderQueueEntry );
180 Q_ASSERT( !mUpdater );
181 Q_ASSERT( !mUpdaterFactory );
182
183 mState = QueuedForUpdate;
184 mLoaderQueueEntry = entry;
185 mUpdaterFactory = updateJobFactory;
186}
187
188void QgsChunkNode::cancelQueuedForUpdate()
189{
190 Q_ASSERT( mState == QueuedForUpdate );
191 Q_ASSERT( mEntity );
192 Q_ASSERT( mLoaderQueueEntry );
193 Q_ASSERT( mUpdaterFactory );
194 Q_ASSERT( !mUpdater );
195
196 mState = Loaded;
197 mUpdaterFactory = nullptr; // not owned by the node
198
199 delete mLoaderQueueEntry;
200 mLoaderQueueEntry = nullptr;
201}
202
203void QgsChunkNode::setUpdating()
204{
205 Q_ASSERT( mState == QgsChunkNode::QueuedForUpdate );
206 Q_ASSERT( mEntity );
207 Q_ASSERT( mReplacementQueueEntry );
208 Q_ASSERT( mLoaderQueueEntry );
209 Q_ASSERT( !mUpdater );
210 Q_ASSERT( mUpdaterFactory );
211
212 mState = Updating;
213 mUpdater = mUpdaterFactory->createJob( this );
214 mUpdaterFactory = nullptr; // not owned by the node
215 mLoaderQueueEntry = nullptr;
216}
217
218void QgsChunkNode::cancelUpdating()
219{
220 Q_ASSERT( mState == QgsChunkNode::Updating );
221 Q_ASSERT( mUpdater );
222 Q_ASSERT( !mLoaderQueueEntry );
223
224 mUpdater = nullptr; // not owned by chunk node
225
226 mState = Loaded;
227}
228
229void QgsChunkNode::setUpdated()
230{
231 Q_ASSERT( mState == QgsChunkNode::Updating );
232 Q_ASSERT( mUpdater );
233 Q_ASSERT( !mLoaderQueueEntry );
234 Q_ASSERT( mReplacementQueueEntry );
235
236 mUpdater = nullptr; // not owned by chunk node
237
238 mState = QgsChunkNode::Loaded;
239}
240
241void QgsChunkNode::setExactBbox( const QgsAABB &box )
242{
243 mBbox = box;
244
245 // TODO: propagate better estimate to children?
246}
247
248void QgsChunkNode::updateParentBoundingBoxesRecursively() const
249{
250 QgsChunkNode *currentNode = parent();
251 while ( currentNode )
252 {
253 QgsChunkNode *const *currentNodeChildren = currentNode->children();
254 float xMin = std::numeric_limits< float >::max();
255 float xMax = -std::numeric_limits< float >::max();
256 float yMin = std::numeric_limits< float >::max();
257 float yMax = -std::numeric_limits< float >::max();
258 float zMin = std::numeric_limits< float >::max();
259 float zMax = -std::numeric_limits< float >::max();
260
261 for ( int i = 0; i < currentNode->childCount(); ++i )
262 {
263 const QgsAABB childBBox = currentNodeChildren[i]->bbox();
264
265 // Nodes without data have an empty bbox and should be skipped
266 if ( childBBox.isEmpty() )
267 continue;
268
269 if ( childBBox.xMin < xMin )
270 xMin = childBBox.xMin;
271 if ( childBBox.yMin < yMin )
272 yMin = childBBox.yMin;
273 if ( childBBox.zMin < zMin )
274 zMin = childBBox.zMin;
275 if ( childBBox.xMax > xMax )
276 xMax = childBBox.xMax;
277 if ( childBBox.yMax > yMax )
278 yMax = childBBox.yMax;
279 if ( childBBox.zMax > zMax )
280 zMax = childBBox.zMax;
281 }
282
283 // QgsAABB is normalized in its constructor, so that min values are always smaller than max.
284 // If all child bboxes were empty, we can end up with min > max, so let's have an empty bbox instead.
285 const QgsAABB currentNodeBbox = xMin > xMax || yMin > yMax || zMin > zMax ? QgsAABB() : QgsAABB( xMin, yMin, zMin, xMax, yMax, zMax );
286
287 currentNode->setExactBbox( currentNodeBbox );
288 currentNode = currentNode->parent();
289 }
290}
291
3
Definition: qgsaabb.h:33
float yMax
Definition: qgsaabb.h:90
float xMax
Definition: qgsaabb.h:89
float xMin
Definition: qgsaabb.h:86
float zMax
Definition: qgsaabb.h:91
float yMin
Definition: qgsaabb.h:87
bool isEmpty() const
Returns true if xExtent(), yExtent() and zExtent() are all zero, false otherwise.
Definition: qgsaabb.h:81
float zMin
Definition: qgsaabb.h:88