21 #include "qgsmeshterraingenerator.h"
26 #include <QDomDocument>
27 #include <QDomElement>
35 , mOrigin( other.mOrigin )
37 , mBackgroundColor( other.mBackgroundColor )
38 , mSelectionColor( other.mSelectionColor )
39 , mTerrainVerticalScale( other.mTerrainVerticalScale )
40 , mTerrainGenerator( other.mTerrainGenerator ? other.mTerrainGenerator->clone() : nullptr )
41 , mMapTileResolution( other.mMapTileResolution )
42 , mMaxTerrainScreenError( other.mMaxTerrainScreenError )
43 , mMaxTerrainGroundError( other.mMaxTerrainGroundError )
44 , mTerrainShadingEnabled( other.mTerrainShadingEnabled )
45 , mTerrainShadingMaterial( other.mTerrainShadingMaterial )
46 , mTerrainMapTheme( other.mTerrainMapTheme )
47 , mShowTerrainBoundingBoxes( other.mShowTerrainBoundingBoxes )
48 , mShowTerrainTileInfo( other.mShowTerrainTileInfo )
49 , mShowCameraViewCenter( other.mShowCameraViewCenter )
50 , mShowLabels( other.mShowLabels )
51 , mPointLights( other.mPointLights )
52 , mFieldOfView( other.mFieldOfView )
53 , mLayers( other.mLayers )
55 , mSkyboxEnabled( other.mSkyboxEnabled )
56 , mSkyboxFileBase( other.mSkyboxFileBase )
57 , mSkyboxFileExtension( other.mSkyboxFileExtension )
58 , mTransformContext( other.mTransformContext )
59 , mPathResolver( other.mPathResolver )
60 , mMapThemes( other.mMapThemes )
64 mRenderers << renderer->
clone();
70 qDeleteAll( mRenderers );
75 QDomElement elemOrigin = elem.firstChildElement( QStringLiteral(
"origin" ) );
77 elemOrigin.attribute( QStringLiteral(
"x" ) ).toDouble(),
78 elemOrigin.attribute( QStringLiteral(
"y" ) ).toDouble(),
79 elemOrigin.attribute( QStringLiteral(
"z" ) ).toDouble() );
81 QDomElement elemCamera = elem.firstChildElement( QStringLiteral(
"camera" ) );
82 if ( !elemCamera.isNull() )
84 mFieldOfView = elemCamera.attribute( QStringLiteral(
"field-of-view" ), QStringLiteral(
"45" ) ).toFloat();
87 QDomElement elemColor = elem.firstChildElement( QStringLiteral(
"color" ) );
88 if ( !elemColor.isNull() )
94 QDomElement elemCrs = elem.firstChildElement( QStringLiteral(
"crs" ) );
97 QDomElement elemTerrain = elem.firstChildElement( QStringLiteral(
"terrain" ) );
98 mTerrainVerticalScale = elemTerrain.attribute( QStringLiteral(
"exaggeration" ), QStringLiteral(
"1" ) ).toFloat();
99 mMapTileResolution = elemTerrain.attribute( QStringLiteral(
"texture-size" ), QStringLiteral(
"512" ) ).toInt();
100 mMaxTerrainScreenError = elemTerrain.attribute( QStringLiteral(
"max-terrain-error" ), QStringLiteral(
"3" ) ).toFloat();
101 mMaxTerrainGroundError = elemTerrain.attribute( QStringLiteral(
"max-ground-error" ), QStringLiteral(
"1" ) ).toFloat();
102 mTerrainShadingEnabled = elemTerrain.attribute( QStringLiteral(
"shading-enabled" ), QStringLiteral(
"0" ) ).toInt();
103 QDomElement elemTerrainShadingMaterial = elemTerrain.firstChildElement( QStringLiteral(
"shading-material" ) );
104 if ( !elemTerrainShadingMaterial.isNull() )
105 mTerrainShadingMaterial.
readXml( elemTerrainShadingMaterial );
106 mTerrainMapTheme = elemTerrain.attribute( QStringLiteral(
"map-theme" ) );
107 mShowLabels = elemTerrain.attribute( QStringLiteral(
"show-labels" ), QStringLiteral(
"0" ) ).toInt();
109 mPointLights.clear();
110 QDomElement elemPointLights = elem.firstChildElement( QStringLiteral(
"point-lights" ) );
111 if ( !elemPointLights.isNull() )
113 QDomElement elemPointLight = elemPointLights.firstChildElement( QStringLiteral(
"point-light" ) );
114 while ( !elemPointLight.isNull() )
117 pointLight.
readXml( elemPointLight );
118 mPointLights << pointLight;
119 elemPointLight = elemPointLight.nextSiblingElement( QStringLiteral(
"point-light" ) );
127 mPointLights << defaultLight;
130 QDomElement elemMapLayers = elemTerrain.firstChildElement( QStringLiteral(
"layers" ) );
131 QDomElement elemMapLayer = elemMapLayers.firstChildElement( QStringLiteral(
"layer" ) );
132 QList<QgsMapLayerRef> mapLayers;
133 while ( !elemMapLayer.isNull() )
135 mapLayers <<
QgsMapLayerRef( elemMapLayer.attribute( QStringLiteral(
"id" ) ) );
136 elemMapLayer = elemMapLayer.nextSiblingElement( QStringLiteral(
"layer" ) );
140 QDomElement elemTerrainGenerator = elemTerrain.firstChildElement( QStringLiteral(
"generator" ) );
141 QString terrainGenType = elemTerrainGenerator.attribute( QStringLiteral(
"type" ) );
142 if ( terrainGenType == QLatin1String(
"dem" ) )
145 demTerrainGenerator->
setCrs( mCrs, mTransformContext );
146 mTerrainGenerator.reset( demTerrainGenerator );
148 else if ( terrainGenType == QLatin1String(
"online" ) )
151 onlineTerrainGenerator->
setCrs( mCrs, mTransformContext );
152 mTerrainGenerator.reset( onlineTerrainGenerator );
154 else if ( terrainGenType == QLatin1String(
"mesh" ) )
156 QgsMeshTerrainGenerator *meshTerrainGenerator =
new QgsMeshTerrainGenerator;
157 meshTerrainGenerator->setCrs( mCrs, mTransformContext );
158 mTerrainGenerator.reset( meshTerrainGenerator );
164 mTerrainGenerator.reset( flatGen );
166 mTerrainGenerator->readXml( elemTerrainGenerator );
168 qDeleteAll( mRenderers );
171 QDomElement elemRenderers = elem.firstChildElement( QStringLiteral(
"renderers" ) );
172 QDomElement elemRenderer = elemRenderers.firstChildElement( QStringLiteral(
"renderer" ) );
173 while ( !elemRenderer.isNull() )
176 QString type = elemRenderer.attribute( QStringLiteral(
"type" ) );
177 if ( type == QLatin1String(
"vector" ) )
181 else if ( type == QLatin1String(
"mesh" ) )
188 renderer->
readXml( elemRenderer, context );
189 mRenderers.append( renderer );
191 elemRenderer = elemRenderer.nextSiblingElement( QStringLiteral(
"renderer" ) );
194 QDomElement elemSkybox = elem.firstChildElement( QStringLiteral(
"skybox" ) );
195 mSkyboxEnabled = elemSkybox.attribute( QStringLiteral(
"enabled" ), QStringLiteral(
"0" ) ).toInt();
196 mSkyboxFileBase = elemSkybox.attribute( QStringLiteral(
"file-base" ) );
197 mSkyboxFileExtension = elemSkybox.attribute( QStringLiteral(
"file-ext" ) );
199 QDomElement elemDebug = elem.firstChildElement( QStringLiteral(
"debug" ) );
200 mShowTerrainBoundingBoxes = elemDebug.attribute( QStringLiteral(
"bounding-boxes" ), QStringLiteral(
"0" ) ).toInt();
201 mShowTerrainTileInfo = elemDebug.attribute( QStringLiteral(
"terrain-tile-info" ), QStringLiteral(
"0" ) ).toInt();
202 mShowCameraViewCenter = elemDebug.attribute( QStringLiteral(
"camera-view-center" ), QStringLiteral(
"0" ) ).toInt();
204 QDomElement elemTemporalRange = elem.firstChildElement( QStringLiteral(
"temporal-range" ) );
205 QDateTime start = QDateTime::fromString( elemTemporalRange.attribute( QStringLiteral(
"start" ) ), Qt::ISODate );
206 QDateTime end = QDateTime::fromString( elemTemporalRange.attribute( QStringLiteral(
"end" ) ), Qt::ISODate );
212 QDomElement elem = doc.createElement( QStringLiteral(
"qgis3d" ) );
214 QDomElement elemOrigin = doc.createElement( QStringLiteral(
"origin" ) );
215 elemOrigin.setAttribute( QStringLiteral(
"x" ), QString::number( mOrigin.
x() ) );
216 elemOrigin.setAttribute( QStringLiteral(
"y" ), QString::number( mOrigin.
y() ) );
217 elemOrigin.setAttribute( QStringLiteral(
"z" ), QString::number( mOrigin.
z() ) );
218 elem.appendChild( elemOrigin );
220 QDomElement elemCamera = doc.createElement( QStringLiteral(
"camera" ) );
221 elemCamera.setAttribute( QStringLiteral(
"field-of-view" ), mFieldOfView );
222 elem.appendChild( elemCamera );
224 QDomElement elemColor = doc.createElement( QStringLiteral(
"color" ) );
227 elem.appendChild( elemColor );
229 QDomElement elemCrs = doc.createElement( QStringLiteral(
"crs" ) );
231 elem.appendChild( elemCrs );
233 QDomElement elemTerrain = doc.createElement( QStringLiteral(
"terrain" ) );
234 elemTerrain.setAttribute( QStringLiteral(
"exaggeration" ), QString::number( mTerrainVerticalScale ) );
235 elemTerrain.setAttribute( QStringLiteral(
"texture-size" ), mMapTileResolution );
236 elemTerrain.setAttribute( QStringLiteral(
"max-terrain-error" ), QString::number( mMaxTerrainScreenError ) );
237 elemTerrain.setAttribute( QStringLiteral(
"max-ground-error" ), QString::number( mMaxTerrainGroundError ) );
238 elemTerrain.setAttribute( QStringLiteral(
"shading-enabled" ), mTerrainShadingEnabled ? 1 : 0 );
239 QDomElement elemTerrainShadingMaterial = doc.createElement( QStringLiteral(
"shading-material" ) );
240 mTerrainShadingMaterial.
writeXml( elemTerrainShadingMaterial );
241 elemTerrain.appendChild( elemTerrainShadingMaterial );
242 elemTerrain.setAttribute( QStringLiteral(
"map-theme" ), mTerrainMapTheme );
243 elemTerrain.setAttribute( QStringLiteral(
"show-labels" ), mShowLabels ? 1 : 0 );
245 QDomElement elemPointLights = doc.createElement( QStringLiteral(
"point-lights" ) );
248 QDomElement elemPointLight = pointLight.writeXml( doc );
249 elemPointLights.appendChild( elemPointLight );
251 elem.appendChild( elemPointLights );
253 QDomElement elemMapLayers = doc.createElement( QStringLiteral(
"layers" ) );
256 QDomElement elemMapLayer = doc.createElement( QStringLiteral(
"layer" ) );
257 elemMapLayer.setAttribute( QStringLiteral(
"id" ), layerRef.
layerId );
258 elemMapLayers.appendChild( elemMapLayer );
260 elemTerrain.appendChild( elemMapLayers );
261 QDomElement elemTerrainGenerator = doc.createElement( QStringLiteral(
"generator" ) );
263 mTerrainGenerator->writeXml( elemTerrainGenerator );
264 elemTerrain.appendChild( elemTerrainGenerator );
265 elem.appendChild( elemTerrain );
267 QDomElement elemRenderers = doc.createElement( QStringLiteral(
"renderers" ) );
270 QDomElement elemRenderer = doc.createElement( QStringLiteral(
"renderer" ) );
271 elemRenderer.setAttribute( QStringLiteral(
"type" ), renderer->
type() );
272 renderer->
writeXml( elemRenderer, context );
273 elemRenderers.appendChild( elemRenderer );
275 elem.appendChild( elemRenderers );
277 QDomElement elemSkybox = doc.createElement( QStringLiteral(
"skybox" ) );
278 elemSkybox.setAttribute( QStringLiteral(
"enabled" ), mSkyboxEnabled ? 1 : 0 );
280 elemSkybox.setAttribute( QStringLiteral(
"file-base" ), mSkyboxFileBase );
281 elemSkybox.setAttribute( QStringLiteral(
"file-ext" ), mSkyboxFileExtension );
282 elem.appendChild( elemSkybox );
284 QDomElement elemDebug = doc.createElement( QStringLiteral(
"debug" ) );
285 elemDebug.setAttribute( QStringLiteral(
"bounding-boxes" ), mShowTerrainBoundingBoxes ? 1 : 0 );
286 elemDebug.setAttribute( QStringLiteral(
"terrain-tile-info" ), mShowTerrainTileInfo ? 1 : 0 );
287 elemDebug.setAttribute( QStringLiteral(
"camera-view-center" ), mShowCameraViewCenter ? 1 : 0 );
288 elem.appendChild( elemDebug );
290 QDomElement elemTemporalRange = doc.createElement( QStringLiteral(
"temporal-range" ) );
291 elemTemporalRange.setAttribute( QStringLiteral(
"start" ),
temporalRange().begin().toString( Qt::ISODate ) );
292 elemTemporalRange.setAttribute( QStringLiteral(
"end" ),
temporalRange().end().toString( Qt::ISODate ) );
299 for (
int i = 0; i < mLayers.count(); ++i )
305 mTerrainGenerator->resolveReferences( project );
307 for (
int i = 0; i < mRenderers.count(); ++i )
331 return mTransformContext;
336 mTransformContext = context;
341 if ( color == mBackgroundColor )
344 mBackgroundColor = color;
350 return mBackgroundColor;
355 if ( color == mSelectionColor )
358 mSelectionColor = color;
364 return mSelectionColor;
369 if ( zScale == mTerrainVerticalScale )
372 mTerrainVerticalScale = zScale;
378 return mTerrainVerticalScale;
383 QList<QgsMapLayerRef> lst;
384 lst.reserve(
layers.count() );
390 if ( mLayers == lst )
399 QList<QgsMapLayer *> lst;
400 lst.reserve( mLayers.count() );
403 if ( layerRef.
layer )
404 lst.append( layerRef.
layer );
411 if ( mMapTileResolution == res )
414 mMapTileResolution = res;
420 return mMapTileResolution;
425 if ( mMaxTerrainScreenError == error )
428 mMaxTerrainScreenError = error;
434 return mMaxTerrainScreenError;
439 if ( mMaxTerrainGroundError == error )
442 mMaxTerrainGroundError = error;
448 return mMaxTerrainGroundError;
453 mTerrainGenerator.reset( gen );
459 if ( mTerrainShadingEnabled == enabled )
462 mTerrainShadingEnabled = enabled;
468 if ( mTerrainShadingMaterial == material )
471 mTerrainShadingMaterial = material;
477 if ( mTerrainMapTheme == theme )
480 mTerrainMapTheme = theme;
486 qDeleteAll( mRenderers );
495 if ( mShowTerrainBoundingBoxes == enabled )
498 mShowTerrainBoundingBoxes = enabled;
504 if ( mShowTerrainTileInfo == enabled )
507 mShowTerrainTileInfo = enabled;
513 if ( mShowCameraViewCenter == enabled )
516 mShowCameraViewCenter = enabled;
522 if ( mShowLabels == enabled )
525 mShowLabels = enabled;
549 mSkyboxEnabled = enabled;
550 mSkyboxFileBase = fileBase;
551 mSkyboxFileExtension = fileExtension;