17#include "qgsvirtualpointcloudprovider.h"
25QgsVirtualPointCloudEntity::QgsVirtualPointCloudEntity(
30 float maximumScreenSpaceError,
31 bool showBoundingBoxes,
35 : Qgs3DMapSceneEntity( map, nullptr )
37 , mCoordinateTransform( coordinateTransform )
38 , mZValueScale( zValueScale )
39 , mZValueOffset( zValueOffset )
40 , mPointBudget( pointBudget )
41 , mMaximumScreenSpaceError( maximumScreenSpaceError )
42 , mShowBoundingBoxes( showBoundingBoxes )
44 mSymbol.reset( symbol );
45 mBboxesEntity =
new QgsChunkBoundsEntity(
this );
47 const QVector<QgsPointCloudSubIndex> subIndexes = provider()->subIndexes();
48 for (
int i = 0; i < subIndexes.size(); ++i )
50 const QgsPointCloudSubIndex &si = subIndexes.at( i );
55 createChunkedEntityForSubIndex( i );
59 connect(
this, &QgsVirtualPointCloudEntity::subIndexNeedsLoading, provider(), &QgsVirtualPointCloudProvider::loadSubIndex, Qt::QueuedConnection );
60 connect( provider(), &QgsVirtualPointCloudProvider::subIndexLoaded,
this, &QgsVirtualPointCloudEntity::createChunkedEntityForSubIndex );
64QList<QgsChunkedEntity *> QgsVirtualPointCloudEntity::chunkedEntities()
const
66 return mChunkedEntitiesMap.values();
69QgsVirtualPointCloudProvider *QgsVirtualPointCloudEntity::provider()
const
71 return qobject_cast<QgsVirtualPointCloudProvider *>( mLayer->dataProvider() );
74QgsAABB QgsVirtualPointCloudEntity::boundingBox(
int i )
const
76 return mBboxes.at( i );
79void QgsVirtualPointCloudEntity::createChunkedEntityForSubIndex(
int i )
81 const QVector<QgsPointCloudSubIndex> subIndexes = provider()->subIndexes();
82 const QgsPointCloudSubIndex &si = subIndexes.at( i );
85 if ( !si.index() || mBboxes.at( i ).isEmpty() )
88 QgsPointCloudLayerChunkedEntity *newChunkedEntity =
new QgsPointCloudLayerChunkedEntity(
93 mMaximumScreenSpaceError,
99 mChunkedEntitiesMap.insert( i, newChunkedEntity );
100 newChunkedEntity->setParent(
this );
101 connect( newChunkedEntity, &QgsChunkedEntity::pendingJobsCountChanged,
this, &Qgs3DMapSceneEntity::pendingJobsCountChanged );
102 emit newEntityCreated( newChunkedEntity );
105void QgsVirtualPointCloudEntity::handleSceneUpdate(
const SceneContext &sceneContext )
107 const QVector<QgsPointCloudSubIndex> subIndexes = provider()->subIndexes();
108 for (
int i = 0; i < subIndexes.size(); ++i )
110 const QgsAABB &bbox = mBboxes.at( i );
116 constexpr int SPAN = 256;
117 const float epsilon = std::min( bbox.
xExtent(), bbox.
yExtent() ) / SPAN;
120 constexpr float THRESHOLD = .2;
124 const bool displayAsBbox = sceneContext.cameraPos.isNull() || sse < THRESHOLD;
125 if ( !displayAsBbox && !subIndexes.at( i ).index() )
126 emit subIndexNeedsLoading( i );
128 setRenderSubIndexAsBbox( i, displayAsBbox );
129 if ( !displayAsBbox && mChunkedEntitiesMap.contains( i ) )
130 mChunkedEntitiesMap[i]->handleSceneUpdate( sceneContext );
135QgsRange<float> QgsVirtualPointCloudEntity::getNearFarPlaneRange(
const QMatrix4x4 &viewMatrix )
const
140 for ( QgsChunkedEntity *entity : mChunkedEntitiesMap )
142 if ( entity->isEnabled() )
144 const QgsRange<float> range = entity->getNearFarPlaneRange( viewMatrix );
145 ffar = std::max( range.
upper(), ffar );
146 fnear = std::min( range.
lower(), fnear );
151 if ( fnear == 1e9 && ffar == 0 )
153 for (
const QgsAABB &bbox : mBboxes )
158 fnear = std::min( fnear, bboxfnear );
159 ffar = std::max( ffar, bboxffar );
166int QgsVirtualPointCloudEntity::pendingJobsCount()
const
169 for ( QgsChunkedEntity *entity : mChunkedEntitiesMap )
171 if ( entity->isEnabled() )
172 jobs += entity->pendingJobsCount();
177bool QgsVirtualPointCloudEntity::needsUpdate()
const
179 for ( QgsChunkedEntity *entity : mChunkedEntitiesMap )
181 if ( entity->isEnabled() && entity->needsUpdate() )
187void QgsVirtualPointCloudEntity::updateBboxEntity()
189 QList<QgsAABB> bboxes;
190 const QVector<QgsPointCloudSubIndex> subIndexes = provider()->subIndexes();
191 for (
int i = 0; i < subIndexes.size(); ++i )
193 if ( mChunkedEntitiesMap.contains( i ) && mChunkedEntitiesMap[i]->isEnabled() )
196 if ( mBboxes.at( i ).isEmpty() )
199 bboxes << mBboxes.at( i );
202 mBboxesEntity->setBoxes( bboxes );
205void QgsVirtualPointCloudEntity::setRenderSubIndexAsBbox(
int i,
bool asBbox )
207 if ( !mChunkedEntitiesMap.contains( i ) )
210 mChunkedEntitiesMap[i]->setEnabled( !asBbox );
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.
float xExtent() const
Returns box width in X axis.
bool isEmpty() const
Returns true if xExtent(), yExtent() and zExtent() are all zero, false otherwise.
float distanceFromPoint(float x, float y, float z) const
Returns shortest distance from the box to a point.
QgsCoordinateReferenceSystem crs
Represents a map layer supporting display of point clouds.
A template based class for storing ranges (lower to upper values).
T lower() const
Returns the lower bound of the range.
T upper() const
Returns the upper bound of the range.
A rectangle specified with double values.
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.