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