QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgsvirtualpointcloudentity_p.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvirtualpointcloudentity_p.cpp
3 --------------------------------------
4 Date : April 2023
5 Copyright : (C) 2023 by Stefanos Natsis
6 Email : uclaros 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_qgsvirtualpointcloudentity_p.cpp"
18#include "qgsvirtualpointcloudprovider.h"
21#include "qgs3dutils.h"
22
24
25
26QgsVirtualPointCloudEntity::QgsVirtualPointCloudEntity(
28 QgsPointCloudLayer *layer,
29 const QgsCoordinateTransform &coordinateTransform,
31 float maximumScreenSpaceError,
32 bool showBoundingBoxes,
33 double zValueScale,
34 double zValueOffset,
35 int pointBudget )
36 : Qgs3DMapSceneEntity( map, nullptr )
37 , mLayer( layer )
38 , mCoordinateTransform( coordinateTransform )
39 , mZValueScale( zValueScale )
40 , mZValueOffset( zValueOffset )
41 , mPointBudget( pointBudget )
42 , mMaximumScreenSpaceError( maximumScreenSpaceError )
43 , mShowBoundingBoxes( showBoundingBoxes )
44{
45 mSymbol.reset( symbol );
46 mBboxesEntity = new QgsChunkBoundsEntity( this );
47 const QgsRectangle mapExtent = Qgs3DUtils::tryReprojectExtent2D( map->extent(), map->crs(), layer->crs(), map->transformContext() );
48 const QVector<QgsPointCloudSubIndex> subIndexes = provider()->subIndexes();
49 for ( int i = 0; i < subIndexes.size(); ++i )
50 {
51 const QgsPointCloudSubIndex &si = subIndexes.at( i );
52 const QgsRectangle intersection = si.extent().intersect( mapExtent );
53
54 mBboxes << Qgs3DUtils::mapToWorldExtent( intersection, si.zRange().lower(), si.zRange().upper(), map->origin() );
55
56 createChunkedEntityForSubIndex( i );
57 }
58
59 updateBboxEntity();
60 connect( this, &QgsVirtualPointCloudEntity::subIndexNeedsLoading, provider(), &QgsVirtualPointCloudProvider::loadSubIndex, Qt::QueuedConnection );
61 connect( provider(), &QgsVirtualPointCloudProvider::subIndexLoaded, this, &QgsVirtualPointCloudEntity::createChunkedEntityForSubIndex );
62
63}
64
65QList<QgsChunkedEntity *> QgsVirtualPointCloudEntity::chunkedEntities() const
66{
67 return mChunkedEntitiesMap.values();
68}
69
70QgsVirtualPointCloudProvider *QgsVirtualPointCloudEntity::provider() const
71{
72 return qobject_cast<QgsVirtualPointCloudProvider *>( mLayer->dataProvider() );
73}
74
75QgsAABB QgsVirtualPointCloudEntity::boundingBox( int i ) const
76{
77 return mBboxes.at( i );
78}
79
80void QgsVirtualPointCloudEntity::createChunkedEntityForSubIndex( int i )
81{
82 const QVector<QgsPointCloudSubIndex> subIndexes = provider()->subIndexes();
83 const QgsPointCloudSubIndex &si = subIndexes.at( i );
84
85 // Skip if Index is not yet loaded or is outside the map extents
86 if ( !si.index() || mBboxes.at( i ).isEmpty() )
87 return;
88
89 QgsPointCloudLayerChunkedEntity *newChunkedEntity = new QgsPointCloudLayerChunkedEntity(
90 mapSettings(),
91 si.index(),
92 mCoordinateTransform,
93 static_cast< QgsPointCloud3DSymbol * >( mSymbol->clone() ),
94 mMaximumScreenSpaceError,
95 mShowBoundingBoxes,
96 mZValueScale,
97 mZValueOffset,
98 mPointBudget );
99
100 mChunkedEntitiesMap.insert( i, newChunkedEntity );
101 newChunkedEntity->setParent( this );
102 connect( newChunkedEntity, &QgsChunkedEntity::pendingJobsCountChanged, this, &Qgs3DMapSceneEntity::pendingJobsCountChanged );
103 emit newEntityCreated( newChunkedEntity );
104}
105
106void QgsVirtualPointCloudEntity::handleSceneUpdate( const SceneContext &sceneContext )
107{
108 const QVector<QgsPointCloudSubIndex> subIndexes = provider()->subIndexes();
109 for ( int i = 0; i < subIndexes.size(); ++i )
110 {
111 const QgsAABB &bbox = mBboxes.at( i );
112
113 if ( bbox.isEmpty() )
114 continue;
115
116 // magic number 256 is the common span value for a COPC root node
117 constexpr int SPAN = 256;
118 const float epsilon = std::min( bbox.xExtent(), bbox.yExtent() ) / SPAN;
119 const float distance = bbox.distanceFromPoint( sceneContext.cameraPos );
120 const float sse = Qgs3DUtils::screenSpaceError( epsilon, distance, sceneContext.screenSizePx, sceneContext.cameraFov );
121 constexpr float THRESHOLD = .2;
122
123 // always display as bbox for the initial temporary camera pos (0, 0, 0)
124 // then once the camera changes we display as bbox depending on screen space error
125 const bool displayAsBbox = sceneContext.cameraPos.isNull() || sse < THRESHOLD;
126 if ( !displayAsBbox && !subIndexes.at( i ).index() )
127 emit subIndexNeedsLoading( i );
128
129 setRenderSubIndexAsBbox( i, displayAsBbox );
130 if ( !displayAsBbox && mChunkedEntitiesMap.contains( i ) )
131 mChunkedEntitiesMap[i]->handleSceneUpdate( sceneContext );
132 }
133 updateBboxEntity();
134}
135
136QgsRange<float> QgsVirtualPointCloudEntity::getNearFarPlaneRange( const QMatrix4x4 &viewMatrix ) const
137{
138 float fnear = 1e9;
139 float ffar = 0;
140
141 for ( QgsChunkedEntity *entity : mChunkedEntitiesMap )
142 {
143 if ( entity->isEnabled() )
144 {
145 const QgsRange<float> range = entity->getNearFarPlaneRange( viewMatrix );
146 ffar = std::max( range.upper(), ffar );
147 fnear = std::min( range.lower(), fnear );
148 }
149 }
150
151 // if there were no chunked entities available, we will iterate the bboxes as a fallback instead
152 if ( fnear == 1e9 && ffar == 0 )
153 {
154 for ( const QgsAABB &bbox : mBboxes )
155 {
156 float bboxfnear;
157 float bboxffar;
158 Qgs3DUtils::computeBoundingBoxNearFarPlanes( bbox, viewMatrix, bboxfnear, bboxffar );
159 fnear = std::min( fnear, bboxfnear );
160 ffar = std::max( ffar, bboxffar );
161 }
162 }
163
164 return QgsRange<float>( fnear, ffar );
165}
166
167int QgsVirtualPointCloudEntity::pendingJobsCount() const
168{
169 int jobs = 0;
170 for ( QgsChunkedEntity *entity : mChunkedEntitiesMap )
171 {
172 if ( entity->isEnabled() )
173 jobs += entity->pendingJobsCount();
174 }
175 return jobs;
176}
177
178bool QgsVirtualPointCloudEntity::needsUpdate() const
179{
180 for ( QgsChunkedEntity *entity : mChunkedEntitiesMap )
181 {
182 if ( entity->isEnabled() && entity->needsUpdate() )
183 return true;
184 }
185 return false;
186}
187
188void QgsVirtualPointCloudEntity::updateBboxEntity()
189{
190 QList<QgsAABB> bboxes;
191 const QVector<QgsPointCloudSubIndex> subIndexes = provider()->subIndexes();
192 for ( int i = 0; i < subIndexes.size(); ++i )
193 {
194 if ( mChunkedEntitiesMap.contains( i ) && mChunkedEntitiesMap[i]->isEnabled() )
195 continue;
196
197 if ( mBboxes.at( i ).isEmpty() )
198 continue;
199
200 bboxes << mBboxes.at( i );
201 }
202
203 mBboxesEntity->setBoxes( bboxes );
204}
205
206void QgsVirtualPointCloudEntity::setRenderSubIndexAsBbox( int i, bool asBbox )
207{
208 if ( !mChunkedEntitiesMap.contains( i ) )
209 return;
210
211 mChunkedEntitiesMap[i]->setEnabled( !asBbox );
212}
QgsRectangle extent() const
Returns the 3D scene's 2D extent in the 3D scene's CRS.
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 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 float screenSpaceError(float epsilon, float distance, int screenSize, float fov)
This routine approximately calculates how an error (epsilon) of an object in world coordinates at giv...
static QgsRectangle tryReprojectExtent2D(const QgsRectangle &extent, const QgsCoordinateReferenceSystem &crs1, const QgsCoordinateReferenceSystem &crs2, const QgsCoordinateTransformContext &context)
Reprojects extent from crs1 to crs2 coordinate reference system with context context.
static void computeBoundingBoxNearFarPlanes(const QgsAABB &bbox, const QMatrix4x4 &viewMatrix, float &fnear, float &ffar)
This routine computes nearPlane farPlane from the closest and farthest corners point of bounding box ...
float yExtent() const
Returns box width in Y axis.
Definition qgsaabb.h:44
float xExtent() const
Returns box width in X axis.
Definition qgsaabb.h:42
bool isEmpty() const
Returns true if xExtent(), yExtent() and zExtent() are all zero, false otherwise.
Definition qgsaabb.h:81
float distanceFromPoint(float x, float y, float z) const
Returns shortest distance from the box to a point.
Definition qgsaabb.cpp:50
Class for doing transforms between two map coordinate systems.
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:83
Represents a map layer supporting display of point clouds.
A template based class for storing ranges (lower to upper values).
Definition qgsrange.h:46
T lower() const
Returns the lower bound of the range.
Definition qgsrange.h:78
T upper() const
Returns the upper bound of the range.
Definition qgsrange.h:85
A rectangle specified with double values.
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.