QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsflatterraingenerator.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsflatterraingenerator.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 
17 
18 #include <Qt3DRender/QGeometryRenderer>
19 #include <Qt3DCore/QTransform>
20 
21 #include "qgs3dmapsettings.h"
22 #include "qgschunknode_p.h"
23 #include "qgsterrainentity_p.h"
24 #include "qgsterraintileentity_p.h"
26 
27 
28 //---------------
29 
30 
31 FlatTerrainChunkLoader::FlatTerrainChunkLoader( QgsTerrainEntity *terrain, QgsChunkNode *node )
32  : QgsTerrainTileLoader( terrain, node )
33 {
34  loadTexture();
35 }
36 
37 
38 Qt3DCore::QEntity *FlatTerrainChunkLoader::createEntity( Qt3DCore::QEntity *parent )
39 {
40  QgsTerrainTileEntity *entity = new QgsTerrainTileEntity( mNode->tileId() );
41 
42  // make geometry renderer
43 
44  // simple quad geometry shared by all tiles
45  // QPlaneGeometry by default is 1x1 with mesh resolution QSize(2,2), centered at 0
46  // TODO: the geometry could be shared inside Terrain instance (within terrain-generator specific data?)
47  mTileGeometry = new Qt3DExtras::QPlaneGeometry;
48 
49  Qt3DRender::QGeometryRenderer *mesh = new Qt3DRender::QGeometryRenderer;
50  mesh->setGeometry( mTileGeometry ); // takes ownership if the component has no parent
51  entity->addComponent( mesh ); // takes ownership if the component has no parent
52 
53  // create material
54 
55  const Qgs3DMapSettings &map = terrain()->map3D();
56  createTextureComponent( entity, map.isTerrainShadingEnabled(), map.terrainShadingMaterial(), !map.layers().empty() );
57 
58  // create transform
59 
60  Qt3DCore::QTransform *transform = nullptr;
61  transform = new Qt3DCore::QTransform();
62  entity->addComponent( transform );
63 
64  // set up transform according to the extent covered by the quad geometry
65  const QgsAABB bbox = mNode->bbox();
66  const double side = bbox.xMax - bbox.xMin;
67  const double half = side / 2;
68 
69  transform->setScale( side );
70  transform->setTranslation( QVector3D( bbox.xMin + half, 0, bbox.zMin + half ) );
71 
72  entity->setEnabled( false );
73  entity->setParent( parent );
74  return entity;
75 }
76 
78 
79 // ---------------
80 
81 QgsChunkLoader *QgsFlatTerrainGenerator::createChunkLoader( QgsChunkNode *node ) const
82 {
83  return new FlatTerrainChunkLoader( mTerrain, node );
84 }
85 
87 {
89  cloned->mCrs = mCrs;
90  cloned->mExtent = mExtent;
91  cloned->updateTilingScheme();
92  return cloned;
93 }
94 
96 {
98 }
99 
101 {
102  return mTerrainTilingScheme.tileToExtent( 0, 0, 0 );
103 }
104 
105 void QgsFlatTerrainGenerator::rootChunkHeightRange( float &hMin, float &hMax ) const
106 {
107  hMin = 0;
108  hMax = 0;
109 }
110 
111 void QgsFlatTerrainGenerator::writeXml( QDomElement &elem ) const
112 {
113  const QgsRectangle r = mExtent;
114  QDomElement elemExtent = elem.ownerDocument().createElement( QStringLiteral( "extent" ) );
115  elemExtent.setAttribute( QStringLiteral( "xmin" ), QString::number( r.xMinimum() ) );
116  elemExtent.setAttribute( QStringLiteral( "xmax" ), QString::number( r.xMaximum() ) );
117  elemExtent.setAttribute( QStringLiteral( "ymin" ), QString::number( r.yMinimum() ) );
118  elemExtent.setAttribute( QStringLiteral( "ymax" ), QString::number( r.yMaximum() ) );
119  elem.appendChild( elemExtent );
120 
121  // crs is not read/written - it should be the same as destination crs of the map
122 }
123 
124 void QgsFlatTerrainGenerator::readXml( const QDomElement &elem )
125 {
126  const QDomElement elemExtent = elem.firstChildElement( QStringLiteral( "extent" ) );
127  const double xmin = elemExtent.attribute( QStringLiteral( "xmin" ) ).toDouble();
128  const double xmax = elemExtent.attribute( QStringLiteral( "xmax" ) ).toDouble();
129  const double ymin = elemExtent.attribute( QStringLiteral( "ymin" ) ).toDouble();
130  const double ymax = elemExtent.attribute( QStringLiteral( "ymax" ) ).toDouble();
131 
132  setExtent( QgsRectangle( xmin, ymin, xmax, ymax ) );
133 
134  // crs is not read/written - it should be the same as destination crs of the map
135 }
136 
138 {
139  mCrs = crs;
140  updateTilingScheme();
141 }
142 
144 {
145  if ( mExtent == extent )
146  return;
147 
148  mExtent = extent;
149  updateTilingScheme();
150 
151  emit extentChanged();
152 }
153 
154 void QgsFlatTerrainGenerator::updateTilingScheme()
155 {
156  if ( mExtent.isNull() )
157  {
159  }
160  else
161  {
162  // the real extent will be a square where the given extent fully fits
163  mTerrainTilingScheme = QgsTilingScheme( mExtent, mCrs );
164  }
165 }
QgsFlatTerrainGenerator::QgsFlatTerrainGenerator
QgsFlatTerrainGenerator()=default
Creates flat terrain generator object.
Qgs3DMapSettings::terrainShadingMaterial
QgsPhongMaterialSettings terrainShadingMaterial() const
Returns terrain shading material.
Definition: qgs3dmapsettings.h:309
QgsFlatTerrainGenerator::setExtent
void setExtent(const QgsRectangle &extent) override
sets the extent of the terrain in terrain's CRS
Definition: qgsflatterraingenerator.cpp:143
QgsTerrainGenerator::extentChanged
void extentChanged()
Emitted when the terrain extent has changed.
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
QgsFlatTerrainGenerator::crs
QgsCoordinateReferenceSystem crs() const
Returns CRS of the terrain.
Definition: qgsflatterraingenerator.h:73
QgsRectangle::yMinimum
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:198
qgschunknode_p.h
QgsFlatTerrainGenerator::rootChunkHeightRange
void rootChunkHeightRange(float &hMin, float &hMax) const override
Returns height range of the root chunk in world coordinates.
Definition: qgsflatterraingenerator.cpp:105
qgsterraintileentity_p.h
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsRectangle::xMaximum
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:183
QgsTerrainGenerator::mTerrain
QgsTerrainEntity * mTerrain
Definition: qgsterraingenerator.h:122
QgsTerrainGenerator::mTerrainTilingScheme
QgsTilingScheme mTerrainTilingScheme
Tiling scheme of the terrain.
Definition: qgsterraingenerator.h:121
QgsFlatTerrainGenerator::readXml
void readXml(const QDomElement &elem) override
Read terrain generator's configuration from XML.
Definition: qgsflatterraingenerator.cpp:124
QgsTerrainGenerator
Base class for generators of terrain.
Definition: qgsterraingenerator.h:49
QgsFlatTerrainGenerator::clone
QgsTerrainGenerator * clone() const override SIP_FACTORY
Makes a copy of the current instance.
Definition: qgsflatterraingenerator.cpp:86
QgsTerrainGenerator::Flat
@ Flat
The whole terrain is flat area.
Definition: qgsterraingenerator.h:57
QgsAABB::xMin
float xMin
Definition: qgsaabb.h:81
QgsAABB
Axis-aligned bounding box - in world coords.
Definition: qgsaabb.h:33
QgsTerrainGenerator::Type
Type
Enumeration of the available terrain generators.
Definition: qgsterraingenerator.h:55
Qgs3DMapSettings
Definition of the world.
Definition: qgs3dmapsettings.h:57
QgsRectangle::xMinimum
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:188
QgsFlatTerrainGenerator
Terrain generator that creates a simple square flat area.
Definition: qgsflatterraingenerator.h:53
QgsTilingScheme::tileToExtent
QgsRectangle tileToExtent(int x, int y, int z) const
Returns map coordinates of the extent of a tile.
Definition: qgstilingscheme.cpp:43
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
QgsFlatTerrainGenerator::createChunkLoader
QgsChunkLoader * createChunkLoader(QgsChunkNode *node) const override SIP_FACTORY
Definition: qgsflatterraingenerator.cpp:81
qgsterrainentity_p.h
qgs3dmapsettings.h
QgsFlatTerrainGenerator::type
Type type() const override
What texture generator implementation is this.
Definition: qgsflatterraingenerator.cpp:95
QgsRectangle::yMaximum
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:193
QgsFlatTerrainGenerator::setCrs
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets CRS of the terrain.
Definition: qgsflatterraingenerator.cpp:137
Qgs3DMapSettings::layers
QList< QgsMapLayer * > layers() const
Returns the list of 3D map layers to be rendered in the scene.
Definition: qgs3dmapsettings.cpp:562
QgsTilingScheme
The class encapsulates tiling scheme (just like with WMTS / TMS / XYZ layers).
Definition: qgstilingscheme.h:37
QgsFlatTerrainGenerator::extent
QgsRectangle extent() const override
extent of the terrain in terrain's CRS
Definition: qgsflatterraingenerator.cpp:100
QgsFlatTerrainGenerator::writeXml
void writeXml(QDomElement &elem) const override
Write terrain generator's configuration to XML.
Definition: qgsflatterraingenerator.cpp:111
QgsAABB::zMin
float zMin
Definition: qgsaabb.h:83
QgsAABB::xMax
float xMax
Definition: qgsaabb.h:84
qgsflatterraingenerator.h
QgsRectangle::isNull
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
Definition: qgsrectangle.h:479
Qgs3DMapSettings::isTerrainShadingEnabled
bool isTerrainShadingEnabled() const
Returns whether terrain shading is enabled.
Definition: qgs3dmapsettings.h:295