QGIS API Documentation 4.1.0-Master (26185ffb827)
Loading...
Searching...
No Matches
qgsterrainentity.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsterrainentity.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 "qgsterrainentity.h"
17
18#include <memory>
19
20#include "qgs3dmapsettings.h"
21#include "qgs3dutils.h"
22#include "qgsaabb.h"
24#include "qgschunknode.h"
27#include "qgseventtracing.h"
29#include "qgsraycastingutils.h"
30#include "qgsterraingenerator.h"
34
35#include <QString>
36#include <Qt3DCore/QTransform>
37#include <Qt3DRender/QGeometryRenderer>
38
39#include "moc_qgsterrainentity.cpp"
40
41using namespace Qt::StringLiterals;
42
44
46class TerrainMapUpdateJobFactory : public QgsChunkQueueJobFactory
47{
48 public:
49 TerrainMapUpdateJobFactory( QgsTerrainTextureGenerator *textureGenerator )
50 : mTextureGenerator( textureGenerator )
51 {}
52
53 QgsChunkQueueJob *createJob( QgsChunkNode *chunk ) override { return new TerrainMapUpdateJob( mTextureGenerator, chunk ); }
54
55 private:
56 QgsTerrainTextureGenerator *mTextureGenerator = nullptr;
57};
58
59
60// -----------
61
62
63QgsTerrainEntity::QgsTerrainEntity( Qgs3DMapSettings *map, Qt3DCore::QNode *parent )
64 : QgsChunkedEntity( map, map->terrainSettings()->maximumScreenError(), map->terrainGenerator(), false, std::numeric_limits<int>::max(), parent )
65{
66 map->terrainGenerator()->setTerrain( this );
67 mIsValid = map->terrainGenerator()->isValid();
68
69 mLayerWatcher = make_qobject_unique<QgsLayerStyleWatcher>( map );
70 connect( mLayerWatcher.get(), &QgsLayerStyleWatcher::styleChanged, this, &QgsTerrainEntity::invalidateMapImages );
71
72 connect( map, &Qgs3DMapSettings::showTerrainBoundingBoxesChanged, this, &QgsTerrainEntity::onShowBoundingBoxesChanged );
73 connect( map, &Qgs3DMapSettings::showTerrainTilesInfoChanged, this, &QgsTerrainEntity::invalidateMapImages );
74 connect( map, &Qgs3DMapSettings::showLabelsChanged, this, &QgsTerrainEntity::invalidateMapImages );
75 connect( map, &Qgs3DMapSettings::backgroundColorChanged, this, &QgsTerrainEntity::invalidateMapImages );
76 connect( map, &Qgs3DMapSettings::terrainMapThemeChanged, this, &QgsTerrainEntity::invalidateMapImages );
77 connect( map, &Qgs3DMapSettings::terrainSettingsChanged, this, &QgsTerrainEntity::onTerrainElevationOffsetChanged );
78
79 mTextureGenerator = new QgsTerrainTextureGenerator( *map );
80
81 mUpdateJobFactory = std::make_unique<TerrainMapUpdateJobFactory>( mTextureGenerator );
82
83 mTerrainTransform = new Qt3DCore::QTransform;
84 mTerrainTransform->setScale( 1.0f );
85 mTerrainTransform->setTranslation( QVector3D( 0.0f, 0.0f, map->terrainSettings()->elevationOffset() ) );
86 addComponent( mTerrainTransform );
87}
88
89QgsTerrainEntity::~QgsTerrainEntity()
90{
91 // cancel / wait for jobs
92 cancelActiveJobs();
93
94 delete mTextureGenerator;
95}
96
97QList<QgsRayCastHit> QgsTerrainEntity::rayIntersection( const QgsRay3D &ray, const QgsRayCastContext &context ) const
98{
99 Q_UNUSED( context )
100 QList<QgsRayCastHit> result;
101
102 float minDist = -1;
103 QgsVector3D intersectionPointMapCoords;
104 switch ( mMapSettings->terrainGenerator()->type() )
105 {
107 {
108 if ( ray.direction().z() == 0 )
109 break; // the ray is parallel to the flat terrain
110
111 const float dist = static_cast<float>( mMapSettings->terrainSettings()->elevationOffset() - ray.origin().z() - mMapSettings->origin().z() ) / ray.direction().z();
112 const QVector3D terrainPlanePoint = ray.origin() + ray.direction() * dist;
113 const QgsVector3D mapCoords = Qgs3DUtils::worldToMapCoordinates( terrainPlanePoint, mMapSettings->origin() );
114 if ( mMapSettings->extent().contains( mapCoords.x(), mapCoords.y() ) )
115 {
116 minDist = dist;
117 intersectionPointMapCoords = mapCoords;
118 }
119 break;
120 }
122 {
123 const QList<QgsChunkNode *> activeNodes = this->activeNodes();
124 QVector3D nearestIntersectionPoint;
125 for ( QgsChunkNode *node : activeNodes )
126 {
127 QgsAABB nodeBbox = Qgs3DUtils::mapToWorldExtent( node->box3D(), mMapSettings->origin() );
128
129 if ( node->entity() && ( minDist < 0 || nodeBbox.distanceFromPoint( ray.origin() ) < minDist ) && QgsRayCastingUtils::rayBoxIntersection( ray, nodeBbox ) )
130 {
131 Qt3DRender::QGeometryRenderer *rend = node->entity()->findChild<Qt3DRender::QGeometryRenderer *>();
132 auto *geom = rend->geometry();
133 Qt3DCore::QTransform *tr = node->entity()->findChild<Qt3DCore::QTransform *>();
134 QVector3D nodeIntPoint;
135 DemTerrainTileGeometry *demGeom = static_cast<DemTerrainTileGeometry *>( geom );
136 if ( demGeom->rayIntersection( ray, context, tr->matrix(), nodeIntPoint ) )
137 {
138 const float dist = ( ray.origin() - nodeIntPoint ).length();
139 if ( minDist < 0 || dist < minDist )
140 {
141 minDist = dist;
142 nearestIntersectionPoint = nodeIntPoint;
143 }
144 }
145 }
146 }
147 if ( minDist >= 0 )
148 intersectionPointMapCoords = Qgs3DUtils::worldToMapCoordinates( nearestIntersectionPoint, mMapSettings->origin() );
149 break;
150 }
154 // not supported
155 break;
156 }
157 if ( minDist >= 0 )
158 {
159 QgsRayCastHit hit;
160 hit.setDistance( minDist );
161 hit.setMapCoordinates( intersectionPointMapCoords );
162 result.append( hit );
163 }
164 return result;
165}
166
167void QgsTerrainEntity::onShowBoundingBoxesChanged()
168{
169 setShowBoundingBoxes( mMapSettings->debugFlags().testFlag( Qgis::Map3DDebugFlag::ShowTerrainBoundingBoxes ) );
170}
171
172void QgsTerrainEntity::invalidateMapImages()
173{
174 QgsEventTracing::addEvent( QgsEventTracing::Instant, u"3D"_s, u"Invalidate textures"_s );
175
176 // handle active nodes
177
178 updateNodes( mActiveNodes, mUpdateJobFactory.get() );
179
180 // handle inactive nodes afterwards
181
182 QList<QgsChunkNode *> inactiveNodes;
183 const QList<QgsChunkNode *> descendants = mRootNode->descendants();
184 for ( QgsChunkNode *node : descendants )
185 {
186 if ( !node->entity() )
187 continue;
188 if ( mActiveNodes.contains( node ) )
189 continue;
190 inactiveNodes << node;
191 }
192
193 updateNodes( inactiveNodes, mUpdateJobFactory.get() );
194
195 setNeedsUpdate( true );
196}
197
198void QgsTerrainEntity::onTerrainElevationOffsetChanged()
199{
200 float newOffset = qobject_cast<Qgs3DMapSettings *>( sender() )->terrainSettings()->elevationOffset();
201 mTerrainTransform->setTranslation( QVector3D( 0.0f, 0.0f, newOffset ) );
202}
203
204float QgsTerrainEntity::terrainElevationOffset() const
205{
206 return mMapSettings->terrainSettings()->elevationOffset();
207}
208
209
210// -----------
211
212
213TerrainMapUpdateJob::TerrainMapUpdateJob( QgsTerrainTextureGenerator *textureGenerator, QgsChunkNode *node )
214 : QgsChunkQueueJob( node )
215 , mTextureGenerator( textureGenerator )
216{}
217
218void TerrainMapUpdateJob::start()
219{
220 QgsChunkNode *node = chunk();
221
222 QgsTerrainTileEntity *entity = qobject_cast<QgsTerrainTileEntity *>( node->entity() );
223 connect( mTextureGenerator, &QgsTerrainTextureGenerator::tileReady, this, &TerrainMapUpdateJob::onTileReady );
224 mJobId = mTextureGenerator->render( entity->textureImage()->imageExtent(), node->tileId(), entity->textureImage()->imageDebugText() );
225}
226
227void TerrainMapUpdateJob::cancel()
228{
229 if ( mJobId != -1 )
230 mTextureGenerator->cancelJob( mJobId );
231}
232
233
234void TerrainMapUpdateJob::onTileReady( int jobId, const QImage &image )
235{
236 if ( mJobId == jobId )
237 {
238 QgsTerrainTileEntity *entity = qobject_cast<QgsTerrainTileEntity *>( mNode->entity() );
239 entity->textureImage()->setImage( image );
240 mJobId = -1;
241 emit finished();
242 }
243}
244
@ ShowTerrainBoundingBoxes
Displays bounding boxes of terrain tiles.
Definition qgis.h:4320
Definition of the world.
void backgroundColorChanged()
Emitted when the background color has changed.
void showTerrainBoundingBoxesChanged()
Emitted when the flag whether terrain's bounding boxes are shown has changed.
const QgsAbstractTerrainSettings * terrainSettings() const
Returns the terrain settings.
void terrainMapThemeChanged()
Emitted when terrain's map theme has changed.
QgsTerrainGenerator * terrainGenerator() const
Returns the terrain generator.
void terrainSettingsChanged()
Emitted when the terrain settings are changed.
void showLabelsChanged()
Emitted when the flag whether labels are displayed on terrain tiles has changed.
void showTerrainTilesInfoChanged()
Emitted when the flag whether terrain's tile info is shown has changed.
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 QgsVector3D worldToMapCoordinates(const QgsVector3D &worldCoords, const QgsVector3D &origin)
Converts 3D world coordinates to map coordinates (applies offset).
Axis-aligned bounding box - in world coords.
Definition qgsaabb.h:33
float distanceFromPoint(float x, float y, float z) const
Returns shortest distance from the box to a point.
Definition qgsaabb.cpp:50
double elevationOffset() const
Returns the elevation offset of the terrain (used to move the terrain up or down).
A representation of a ray in 3D.
Definition qgsray3d.h:31
QVector3D origin() const
Returns the origin of the ray.
Definition qgsray3d.h:43
QVector3D direction() const
Returns the direction of the ray see setDirection().
Definition qgsray3d.h:49
Responsible for defining parameters of the ray casting operations in 3D map canvases.
Contains details about the ray intersecting entities when ray casting in a 3D map canvas.
void setMapCoordinates(const QgsVector3D &point)
Sets the hit point position in 3d map coordinates.
void setDistance(double distance)
Sets the hit's distance from the ray's origin.
@ QuantizedMesh
Terrain is built from quantized mesh tiles.
@ Dem
Terrain is built from raster layer with digital elevation model.
@ Online
Terrain is built from downloaded tiles with digital elevation model.
@ Mesh
Terrain is built from mesh layer with z value on vertices.
@ Flat
The whole terrain is flat area.
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
Definition qgsvector3d.h:33
double y() const
Returns Y coordinate.
Definition qgsvector3d.h:60
double x() const
Returns X coordinate.
Definition qgsvector3d.h:58
bool rayBoxIntersection(const QgsRay3D &ray, const QgsAABB &nodeBbox)
Tests whether an axis aligned box is intersected by a ray.
constexpr QObjectUniquePtr< Tp > make_qobject_unique(Args &&...args)
Create an object owned by a QObjectUniquePtr.