QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgspointcloudindex.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspointcloudindex.cpp
3  --------------------
4  begin : October 2020
5  copyright : (C) 2020 by Peter Petrik
6  email : zilolv at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgspointcloudindex.h"
19 #include <QFile>
20 #include <QFileInfo>
21 #include <QDir>
22 #include <QJsonArray>
23 #include <QJsonDocument>
24 #include <QJsonObject>
25 #include <QTime>
26 #include <QtDebug>
27 
28 #include "qgstiledownloadmanager.h"
29 
31  mD( -1 ),
32  mX( 0 ),
33  mY( 0 ),
34  mZ( 0 )
35 {}
36 
37 IndexedPointCloudNode::IndexedPointCloudNode( int _d, int _x, int _y, int _z ):
38  mD( _d ),
39  mX( _x ),
40  mY( _y ),
41  mZ( _z )
42 {}
43 
45 {
46  return IndexedPointCloudNode( mD - 1, mX / 2, mY / 2, mZ / 2 );
47 }
48 
50 {
51  QStringList lst = str.split( '-' );
52  if ( lst.count() != 4 )
53  return IndexedPointCloudNode();
54  return IndexedPointCloudNode( lst[0].toInt(), lst[1].toInt(), lst[2].toInt(), lst[3].toInt() );
55 }
56 
58 {
59  return QStringLiteral( "%1-%2-%3-%4" ).arg( mD ).arg( mX ).arg( mY ).arg( mZ );
60 }
61 
63 {
64  return mD;
65 }
66 
68 {
69  return mX;
70 }
71 
73 {
74  return mY;
75 }
76 
78 {
79  return mZ;
80 }
81 
83 {
84  return id.d() + id.x() + id.y() + id.z();
85 }
86 
88 
89 //
90 // QgsPointCloudDataBounds
91 //
92 
94 
95 QgsPointCloudDataBounds::QgsPointCloudDataBounds( qint32 xmin, qint32 ymin, qint32 zmin, qint32 xmax, qint32 ymax, qint32 zmax )
96  : mXMin( xmin )
97  , mYMin( ymin )
98  , mZMin( zmin )
99  , mXMax( xmax )
100  , mYMax( ymax )
101  , mZMax( zmax )
102 {
103 
104 }
105 
106 qint32 QgsPointCloudDataBounds::xMin() const
107 {
108  return mXMin;
109 }
110 
111 qint32 QgsPointCloudDataBounds::yMin() const
112 {
113  return mYMin;
114 }
115 
116 qint32 QgsPointCloudDataBounds::xMax() const
117 {
118  return mXMax;
119 }
120 
121 qint32 QgsPointCloudDataBounds::yMax() const
122 {
123  return mYMax;
124 }
125 
126 qint32 QgsPointCloudDataBounds::zMin() const
127 {
128  return mZMin;
129 }
130 
131 qint32 QgsPointCloudDataBounds::zMax() const
132 {
133  return mZMax;
134 }
135 
136 QgsRectangle QgsPointCloudDataBounds::mapExtent( const QgsVector3D &offset, const QgsVector3D &scale ) const
137 {
138  return QgsRectangle(
139  mXMin * scale.x() + offset.x(), mYMin * scale.y() + offset.y(),
140  mXMax * scale.x() + offset.x(), mYMax * scale.y() + offset.y()
141  );
142 }
143 
144 QgsDoubleRange QgsPointCloudDataBounds::zRange( const QgsVector3D &offset, const QgsVector3D &scale ) const
145 {
146  return QgsDoubleRange( mZMin * scale.z() + offset.z(), mZMax * scale.z() + offset.z() );
147 }
148 
150 
151 //
152 // QgsPointCloudIndex
153 //
154 
156 
158 
160 {
161  mHierarchyMutex.lock();
162  bool found = mHierarchy.contains( n );
163  mHierarchyMutex.unlock();
164  return found;
165 }
166 
167 QList<IndexedPointCloudNode> QgsPointCloudIndex::nodeChildren( const IndexedPointCloudNode &n ) const
168 {
169  mHierarchyMutex.lock();
170  Q_ASSERT( mHierarchy.contains( n ) );
171  QList<IndexedPointCloudNode> lst;
172  int d = n.d() + 1;
173  int x = n.x() * 2;
174  int y = n.y() * 2;
175  int z = n.z() * 2;
176 
177  for ( int i = 0; i < 8; ++i )
178  {
179  int dx = i & 1, dy = !!( i & 2 ), dz = !!( i & 4 );
180  IndexedPointCloudNode n2( d, x + dx, y + dy, z + dz );
181  if ( mHierarchy.contains( n2 ) )
182  lst.append( n2 );
183  }
184  mHierarchyMutex.unlock();
185  return lst;
186 }
187 
189 {
190  return mAttributes;
191 }
192 
194 {
195  qint32 xMin = -999999999, yMin = -999999999, zMin = -999999999;
196  qint32 xMax = 999999999, yMax = 999999999, zMax = 999999999;
197 
198  int d = mRootBounds.xMax() - mRootBounds.xMin();
199  double dLevel = ( double )d / pow( 2, n.d() );
200 
201  xMin = round( mRootBounds.xMin() + dLevel * n.x() );
202  xMax = round( mRootBounds.xMin() + dLevel * ( n.x() + 1 ) );
203  yMin = round( mRootBounds.yMin() + dLevel * n.y() );
204  yMax = round( mRootBounds.yMin() + dLevel * ( n.y() + 1 ) );
205  zMin = round( mRootBounds.zMin() + dLevel * n.z() );
206  zMax = round( mRootBounds.zMin() + dLevel * ( n.z() + 1 ) );
207 
208  QgsPointCloudDataBounds db( xMin, yMin, zMin, xMax, yMax, zMax );
209  return db;
210 }
211 
213 {
214  return nodeBounds( node ).mapExtent( mOffset, mScale );
215 }
216 
218 {
219  return nodeBounds( node ).zRange( mOffset, mScale );
220 }
221 
223 {
224  double w = nodeMapExtent( n ).width();
225  return w / mSpan;
226 }
227 
229 {
230  return mScale;
231 }
232 
234 {
235  return mOffset;
236 }
237 
239 {
241 }
242 
244 {
245  return mSpan;
246 }
247 
249 {
250  int count = -1;
251  mHierarchyMutex.lock();
252  if ( mHierarchy.contains( n ) )
253  count = mHierarchy.contains( n );
254  mHierarchyMutex.unlock();
255  return count;
256 }
Represents a indexed point cloud node in octree.
int y() const
Returns y.
static IndexedPointCloudNode fromString(const QString &str)
Creates node from string.
int x() const
Returns x.
QString toString() const
Encode node to string.
int d() const
Returns d.
IndexedPointCloudNode parentNode() const
Returns the parent of the node.
int z() const
Returns z.
IndexedPointCloudNode()
Constructs invalid node.
QgsRange which stores a range of double values.
Definition: qgsrange.h:203
Collection of point cloud attributes.
Represents packaged data bounds.
qint32 xMax() const
Returns x max.
qint32 xMin() const
Returns x min.
qint32 yMax() const
Returns y max.
QgsDoubleRange zRange(const QgsVector3D &offset, const QgsVector3D &scale) const
Returns the z range, applying the specified offset and scale.
QgsPointCloudDataBounds()
Constructs invalid bounds.
qint32 zMax() const
Returns z max.
QgsRectangle mapExtent(const QgsVector3D &offset, const QgsVector3D &scale) const
Returns 2D rectangle in map coordinates.
qint32 yMin() const
Returns y min.
qint32 zMin() const
Returns z min.
int span() const
Returns the number of points in one direction in a single node.
double zMax() const
Returns z max.
QgsRectangle nodeMapExtent(const IndexedPointCloudNode &node) const
Returns the extent of a node in map coordinates.
virtual QList< IndexedPointCloudNode > nodeChildren(const IndexedPointCloudNode &n) const
Returns all children of node.
QgsPointCloudIndex()
Constructs index.
double zMin() const
Returns z min.
QgsVector3D offset() const
Returns offset.
QgsVector3D scale() const
Returns scale.
QHash< IndexedPointCloudNode, int > mHierarchy
Data hierarchy.
QgsPointCloudDataBounds mRootBounds
Bounds of the root node's cube (in int32 coordinates)
QgsPointCloudAttributeCollection mAttributes
QgsPointCloudDataBounds nodeBounds(const IndexedPointCloudNode &node) const
Returns bounds of particular node.
virtual bool hasNode(const IndexedPointCloudNode &n) const
Returns whether the octree contain given node.
QgsVector3D mOffset
Offset of our int32 coordinates compared to CRS coords.
QgsDoubleRange nodeZRange(const IndexedPointCloudNode &node) const
Returns the z range of a node.
int nodePointCount(const IndexedPointCloudNode &n)
Returns the number of poiny of indexed point cloud node n.
float nodeError(const IndexedPointCloudNode &n) const
Returns node's error in map units (used to determine in whether the node has enough detail for the cu...
int mSpan
All native attributes stored in the file.
QgsVector3D mScale
Scale of our int32 coordinates compared to CRS coords.
void setAttributes(const QgsPointCloudAttributeCollection &attributes)
Sets native attributes of the data.
QgsPointCloudAttributeCollection attributes() const
Returns all attributes that are stored in the file.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
double width() const SIP_HOLDGIL
Returns the width of the rectangle.
Definition: qgsrectangle.h:223
double y() const
Returns Y coordinate.
Definition: qgsvector3d.h:51
double z() const
Returns Z coordinate.
Definition: qgsvector3d.h:53
double x() const
Returns X coordinate.
Definition: qgsvector3d.h:49
#define str(x)
Definition: qgis.cpp:37
uint qHash(IndexedPointCloudNode id)
Hash function for indexed nodes.