QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
20 #include "qgs3dmapsettings.h"
21 #include "qgschunknode_p.h"
22 #include "qgsterrainentity_p.h"
23 #include "qgsterraintileentity_p.h"
24 
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;
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  createTextureComponent( entity );
56 
57  // create transform
58 
59  Qt3DCore::QTransform *transform = nullptr;
60  transform = new Qt3DCore::QTransform();
61  entity->addComponent( transform );
62 
63  // set up transform according to the extent covered by the quad geometry
64  QgsAABB bbox = mNode->bbox();
65  double side = bbox.xMax - bbox.xMin;
66  double half = side / 2;
67 
68  transform->setScale( side );
69  transform->setTranslation( QVector3D( bbox.xMin + half, 0, bbox.zMin + half ) );
70 
71  entity->setEnabled( false );
72  entity->setParent( parent );
73  return entity;
74 }
75 
77 
78 // ---------------
79 
80 QgsChunkLoader *QgsFlatTerrainGenerator::createChunkLoader( QgsChunkNode *node ) const
81 {
82  return new FlatTerrainChunkLoader( mTerrain, node );
83 }
84 
86 {
88  cloned->mCrs = mCrs;
89  cloned->mExtent = mExtent;
90  cloned->updateTilingScheme();
91  return cloned;
92 }
93 
95 {
97 }
98 
100 {
101  return mTerrainTilingScheme.tileToExtent( 0, 0, 0 );
102 }
103 
104 void QgsFlatTerrainGenerator::rootChunkHeightRange( float &hMin, float &hMax ) const
105 {
106  hMin = 0;
107  hMax = 0;
108 }
109 
110 void QgsFlatTerrainGenerator::writeXml( QDomElement &elem ) const
111 {
112  QgsRectangle r = mExtent;
113  QDomElement elemExtent = elem.ownerDocument().createElement( QStringLiteral( "extent" ) );
114  elemExtent.setAttribute( QStringLiteral( "xmin" ), QString::number( r.xMinimum() ) );
115  elemExtent.setAttribute( QStringLiteral( "xmax" ), QString::number( r.xMaximum() ) );
116  elemExtent.setAttribute( QStringLiteral( "ymin" ), QString::number( r.yMinimum() ) );
117  elemExtent.setAttribute( QStringLiteral( "ymax" ), QString::number( r.yMaximum() ) );
118 
119  // crs is not read/written - it should be the same as destination crs of the map
120 }
121 
122 void QgsFlatTerrainGenerator::readXml( const QDomElement &elem )
123 {
124  QDomElement elemExtent = elem.firstChildElement( QStringLiteral( "extent" ) );
125  double xmin = elemExtent.attribute( QStringLiteral( "xmin" ) ).toDouble();
126  double xmax = elemExtent.attribute( QStringLiteral( "xmax" ) ).toDouble();
127  double ymin = elemExtent.attribute( QStringLiteral( "ymin" ) ).toDouble();
128  double ymax = elemExtent.attribute( QStringLiteral( "ymax" ) ).toDouble();
129 
130  setExtent( QgsRectangle( xmin, ymin, xmax, ymax ) );
131 
132  // crs is not read/written - it should be the same as destination crs of the map
133 }
134 
136 {
137  mCrs = crs;
138  updateTilingScheme();
139 }
140 
142 {
143  mExtent = extent;
144  updateTilingScheme();
145 }
146 
147 void QgsFlatTerrainGenerator::updateTilingScheme()
148 {
149  if ( mExtent.isNull() )
150  {
152  }
153  else
154  {
155  // the real extent will be a square where the given extent fully fits
156  mTerrainTilingScheme = QgsTilingScheme( mExtent, mCrs );
157  }
158 }
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.
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:171
void readXml(const QDomElement &elem) override
Read terrain generator&#39;s configuration from XML.
float zMin
Definition: qgsaabb.h:77
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
QgsChunkLoader * createChunkLoader(QgsChunkNode *node) const override SIP_FACTORY
QgsCoordinateReferenceSystem crs() const
Returns CRS of the terrain.
QgsRectangle tileToExtent(int x, int y, int z) const
Returns map coordinates of the extent of a tile.
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
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).
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets CRS of the terrain.
3 The class encapsulates tiling scheme (just like with WMTS / TMS / XYZ layers).
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:166
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