QGIS API Documentation 4.3.0-Master (9dce731a7a1)
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"
32
33#include <QString>
34#include <Qt3DCore/QTransform>
35#include <Qt3DRender/QGeometryRenderer>
36
37#include "moc_qgsterrainentity.cpp"
38
39using namespace Qt::StringLiterals;
40
42
43QgsTerrainEntity::QgsTerrainEntity( Qgs3DMapSettings *map, Qt3DCore::QNode *parent )
44 : QgsChunkedEntity( map, map->terrainSettings()->maximumScreenError(), map->terrainGenerator(), false, std::numeric_limits<int>::max(), parent )
45{
46 map->terrainGenerator()->setTerrain( this );
47 mIsValid = map->terrainGenerator()->isValid();
48
49 mLayerWatcher = make_qobject_unique<QgsLayerStyleWatcher>( map );
50 connect( mLayerWatcher.get(), &QgsLayerStyleWatcher::styleChanged, this, &QgsTerrainEntity::invalidateMapImages );
51
52 connect( map, &Qgs3DMapSettings::showTerrainBoundingBoxesChanged, this, &QgsTerrainEntity::onShowBoundingBoxesChanged );
53 connect( map, &Qgs3DMapSettings::showTerrainTilesInfoChanged, this, &QgsTerrainEntity::invalidateMapImages );
54 connect( map, &Qgs3DMapSettings::showLabelsChanged, this, &QgsTerrainEntity::invalidateMapImages );
55 connect( map, &Qgs3DMapSettings::backgroundColorChanged, this, &QgsTerrainEntity::invalidateMapImages );
56 connect( map, &Qgs3DMapSettings::terrainMapThemeChanged, this, &QgsTerrainEntity::invalidateMapImages );
57 connect( map, &Qgs3DMapSettings::terrainSettingsChanged, this, &QgsTerrainEntity::onTerrainElevationOffsetChanged );
58
59 mTextureGenerator = std::make_unique<QgsTerrainTextureGenerator>( *map );
60
61 mTerrainTransform = new Qt3DCore::QTransform;
62 mTerrainTransform->setScale( 1.0f );
63 mTerrainTransform->setTranslation( QVector3D( 0.0f, 0.0f, map->terrainSettings()->elevationOffset() ) );
64 addComponent( mTerrainTransform );
65}
66
67QgsTerrainEntity::~QgsTerrainEntity()
68{
69 // cancel / wait for jobs
70 cancelActiveJobs();
71}
72
73QList<QgsRayCastHit> QgsTerrainEntity::rayIntersection( const QgsRay3D &ray, const QgsRayCastContext &context ) const
74{
75 Q_UNUSED( context )
76 QList<QgsRayCastHit> result;
77
78 float minDist = -1;
79 QgsVector3D intersectionPointMapCoords;
80 switch ( mMapSettings->terrainGenerator()->type() )
81 {
83 {
84 if ( ray.direction().z() == 0 )
85 break; // the ray is parallel to the flat terrain
86
87 const float dist = static_cast<float>( mMapSettings->terrainSettings()->elevationOffset() - ray.origin().z() - mMapSettings->origin().z() ) / ray.direction().z();
88 const QVector3D terrainPlanePoint = ray.point( dist );
89 const QgsVector3D mapCoords = Qgs3DUtils::worldToMapCoordinates( terrainPlanePoint, mMapSettings->origin() );
90 if ( mMapSettings->extent().contains( mapCoords.x(), mapCoords.y() ) )
91 {
92 minDist = dist;
93 intersectionPointMapCoords = mapCoords;
94 }
95 break;
96 }
98 {
99 const QList<QgsChunkNode *> activeNodes = this->activeNodes();
100 QVector3D nearestIntersectionPoint;
101 for ( QgsChunkNode *node : activeNodes )
102 {
103 QgsAABB nodeBbox = Qgs3DUtils::mapToWorldExtent( node->box3D(), mMapSettings->origin() );
104
105 if ( node->entity() && ( minDist < 0 || nodeBbox.distanceFromPoint( ray.origin() ) < minDist ) && QgsRayCastingUtils::rayBoxIntersection( ray, nodeBbox ) )
106 {
107 Qt3DRender::QGeometryRenderer *rend = node->entity()->findChild<Qt3DRender::QGeometryRenderer *>();
108 auto *geom = rend->geometry();
109 Qt3DCore::QTransform *tr = node->entity()->findChild<Qt3DCore::QTransform *>();
110 QVector3D nodeIntPoint;
111 DemTerrainTileGeometry *demGeom = static_cast<DemTerrainTileGeometry *>( geom );
112 if ( demGeom->rayIntersection( ray, context, tr->matrix(), nodeIntPoint ) )
113 {
114 const float dist = ( ray.origin() - nodeIntPoint ).length();
115 if ( minDist < 0 || dist < minDist )
116 {
117 minDist = dist;
118 nearestIntersectionPoint = nodeIntPoint;
119 }
120 }
121 }
122 }
123 if ( minDist >= 0 )
124 intersectionPointMapCoords = Qgs3DUtils::worldToMapCoordinates( nearestIntersectionPoint, mMapSettings->origin() );
125 break;
126 }
130 // not supported
131 break;
132 }
133 if ( minDist >= 0 )
134 {
135 QgsRayCastHit hit;
136 hit.setDistance( minDist );
137 hit.setMapCoordinates( intersectionPointMapCoords );
138 result.append( hit );
139 }
140 return result;
141}
142
143void QgsTerrainEntity::onShowBoundingBoxesChanged()
144{
145 setShowBoundingBoxes( mMapSettings->debugFlags().testFlag( Qgis::Map3DDebugFlag::ShowTerrainBoundingBoxes ) );
146}
147
148void QgsTerrainEntity::invalidateMapImages()
149{
150 QgsEventTracing::addEvent( QgsEventTracing::Instant, u"3D"_s, u"Invalidate textures"_s );
151
152 // handle active nodes
153
154 updateNodes( mActiveNodes );
155
156 // handle inactive nodes afterwards
157
158 QList<QgsChunkNode *> inactiveNodes;
159 const QList<QgsChunkNode *> descendants = mRootNode->descendants();
160 for ( QgsChunkNode *node : descendants )
161 {
162 if ( !node->entity() )
163 continue;
164 if ( mActiveNodes.contains( node ) )
165 continue;
166 inactiveNodes << node;
167 }
168
169 updateNodes( inactiveNodes );
170
171 setNeedsUpdate( true );
172}
173
174void QgsTerrainEntity::onTerrainElevationOffsetChanged()
175{
176 float newOffset = qobject_cast<Qgs3DMapSettings *>( sender() )->terrainSettings()->elevationOffset();
177 mTerrainTransform->setTranslation( QVector3D( 0.0f, 0.0f, newOffset ) );
178}
179
180float QgsTerrainEntity::terrainElevationOffset() const
181{
182 return mMapSettings->terrainSettings()->elevationOffset();
183}
184
@ ShowTerrainBoundingBoxes
Displays bounding boxes of terrain tiles.
Definition qgis.h:4411
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
QVector3D point(float distance) const
Returns the point along the ray with the specified distance from the ray's origin.
Definition qgsray3d.cpp:62
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.