QGIS API Documentation 4.3.0-Master (8c22baaae17)
Loading...
Searching...
No Matches
qgschunkloader.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgschunkloader.cpp
3 --------------------------------------
4 Date : October 2020
5 Copyright : (C) 2020 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 "qgschunkloader.h"
17
18#include "qgschunknode.h"
19#include "qgsthreadingutils.h"
20
21#include <QVector>
22
23#include "moc_qgschunkloader.cpp"
24
26
27const QgsChunkLoaderResult QgsChunkLoaderResult::sEmpty = QgsChunkLoaderResult( []( Qt3DCore::QEntity * ) {
29 return nullptr;
30} );
31
32QFuture<QgsChunkLoaderResult> QgsChunkLoader::updateChunk( QgsChunkNode *node )
33{
34 return loadChunk( node );
35}
36
37QgsQuadtreeChunkLoader::QgsQuadtreeChunkLoader() = default;
38
39QgsQuadtreeChunkLoader::~QgsQuadtreeChunkLoader() = default;
40
41void QgsQuadtreeChunkLoader::setupQuadtree( const QgsBox3D &rootBox3D, float rootError, int maxLevel, const QgsBox3D &clippingBox3D )
42{
43 mRootBox3D = rootBox3D;
44 mRootError = rootError;
45 mMaxLevel = maxLevel;
46 mClippingBox3D = clippingBox3D;
47}
48
49QgsChunkNode *QgsQuadtreeChunkLoader::createRootNode() const
50{
51 return new QgsChunkNode( QgsChunkNodeId( 0, 0, 0 ), mRootBox3D, mRootError );
52}
53
54QFuture<QVector<QgsChunkNode *>> QgsQuadtreeChunkLoader::createChildren( QgsChunkNode *node )
55{
56 QVector<QgsChunkNode *> children;
57
58 // If there is a max level set, we should respect that
59 if ( mMaxLevel != -1 && node->level() >= mMaxLevel )
60 return QtFuture::makeReadyValueFuture( children );
61
62 const QgsBox3D box3D = node->box3D();
63
64 // Nodes without extent cannot have children
65 if ( box3D.isNull() )
66 return QtFuture::makeReadyValueFuture( children );
67
68 const QgsChunkNodeId nodeId = node->tileId();
69 const float childError = node->error() / 2;
70 QgsVector3D center = box3D.center();
71
72 // if box is very wide (or very thin) we will only split it in one direction so the children
73 // are not as wide/thin and are closer to squares
74 const bool skipDx = box3D.height() > box3D.width() * 2;
75 const bool skipDy = box3D.width() > box3D.height() * 2;
76 for ( int i = 0; i < 4; ++i )
77 {
78 int dx = i & 1, dy = !!( i & 2 );
79
80 if ( dx && skipDx )
81 continue;
82 if ( dy && skipDy )
83 continue;
84
85 const QgsChunkNodeId childId( nodeId.d + 1, nodeId.x * 2 + dx, nodeId.y * 2 + dy );
86
87 const double chXMin = dx ? center.x() : box3D.xMinimum();
88 const double chXMax = dx || skipDx ? box3D.xMaximum() : center.x();
89 const double chYMin = dy ? center.y() : box3D.yMinimum();
90 const double chYMax = dy || skipDy ? box3D.yMaximum() : center.y();
91 const double chZMin = box3D.zMinimum();
92 const double chZMax = box3D.zMaximum();
93 const QgsBox3D childBox3D( chXMin, chYMin, chZMin, chXMax, chYMax, chZMax );
94
95 // When there's a clipping box defined, only keep intersecting children. Note that a QgsBox3D.isEmpty() == true when is2d() == true
96 if ( mClippingBox3D.isNull() || childBox3D.intersects( mClippingBox3D ) )
97 children << new QgsChunkNode( childId, childBox3D, childError, node );
98 }
99 return QtFuture::makeReadyValueFuture( children );
100}
101
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:45
double yMaximum() const
Returns the maximum y value.
Definition qgsbox3d.h:240
double xMinimum() const
Returns the minimum x value.
Definition qgsbox3d.h:205
double zMaximum() const
Returns the maximum z value.
Definition qgsbox3d.h:268
QgsVector3D center() const
Returns the center of the box as a vector.
Definition qgsbox3d.cpp:124
double xMaximum() const
Returns the maximum x value.
Definition qgsbox3d.h:212
double width() const
Returns the width of the box.
Definition qgsbox3d.h:287
double zMinimum() const
Returns the minimum z value.
Definition qgsbox3d.h:261
double yMinimum() const
Returns the minimum y value.
Definition qgsbox3d.h:233
double height() const
Returns the height of the box.
Definition qgsbox3d.h:294
bool isNull() const
Test if the box is null (holding no spatial information).
Definition qgsbox3d.cpp:311
double y() const
Returns Y coordinate.
Definition qgsvector3d.h:60
double x() const
Returns X coordinate.
Definition qgsvector3d.h:58
#define QGIS_CHECK_MAIN_THREAD_ACCESS