QGIS API Documentation 3.99.0-Master (21b3aa880ba)
Loading...
Searching...
No Matches
qgsterraintexturegenerator_p.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsterraintexturegenerator_p.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 "qgs3dmapsettings.h"
20#include "qgseventtracing.h"
22#include "qgsmapsettings.h"
24#include "qgsproject.h"
25
26#include "moc_qgsterraintexturegenerator_p.cpp"
27
29
30QgsTerrainTextureGenerator::QgsTerrainTextureGenerator( const Qgs3DMapSettings &map )
31 : mMap( map )
32 , mTextureSize( QSize( mMap.terrainSettings()->mapTileResolution(), mMap.terrainSettings()->mapTileResolution() ) )
33{
34}
35
36int QgsTerrainTextureGenerator::render( const QgsRectangle &extent, QgsChunkNodeId tileId, const QString &debugText )
37{
38 QgsMapSettings mapSettings( baseMapSettings() );
39 mapSettings.setExtent( extent );
40 QSize size = QSize( mTextureSize );
41
42 QgsRectangle clippedExtent = extent;
43 if ( mMap.sceneMode() == Qgis::SceneMode::Local && mMap.terrainGenerator()->type() == QgsTerrainGenerator::Flat )
44 {
45 // The flat terrain generator might have non-square tiles, clipped at the scene's extent.
46 // We need to produce non-square textures for those cases.
47 clippedExtent = extent.intersect( mMap.extent() );
48 mapSettings.setExtent( clippedExtent );
49 }
50 if ( !qgsDoubleNear( clippedExtent.width(), clippedExtent.height() ) )
51 {
52 if ( clippedExtent.height() > clippedExtent.width() )
53 size.setWidth( static_cast< int >( std::round( size.width() * clippedExtent.width() / clippedExtent.height() ) ) );
54 else if ( clippedExtent.height() < clippedExtent.width() )
55 size.setHeight( static_cast< int >( std::round( size.height() * clippedExtent.height() / clippedExtent.width() ) ) );
56 }
57 mapSettings.setOutputSize( size );
58
59 QgsEventTracing::addEvent( QgsEventTracing::AsyncBegin, QStringLiteral( "3D" ), QStringLiteral( "Texture" ), tileId.text() );
60
62 connect( job, &QgsMapRendererJob::finished, this, &QgsTerrainTextureGenerator::onRenderingFinished );
63
64 JobData jobData;
65 jobData.jobId = ++mLastJobId;
66 jobData.tileId = tileId;
67 jobData.job = job;
68 jobData.extent = extent;
69 jobData.debugText = debugText;
70
71 mJobs.insert( job, jobData ); //store job data just before launching the job
72 job->start();
73
74 // QgsDebugMsgLevel( QStringLiteral("added job: %1 .... in queue: %2").arg( jobData.jobId ).arg( jobs.count() ), 2);
75 return jobData.jobId;
76}
77
78void QgsTerrainTextureGenerator::cancelJob( int jobId )
79{
80 for ( const JobData &jd : std::as_const( mJobs ) )
81 {
82 if ( jd.jobId == jobId )
83 {
84 // QgsDebugMsgLevel( QStringLiteral("canceling job %1").arg( jobId ), 2 );
85 disconnect( jd.job, &QgsMapRendererJob::finished, this, &QgsTerrainTextureGenerator::onRenderingFinished );
86 connect( jd.job, &QgsMapRendererJob::finished, jd.job, &QgsMapRendererSequentialJob::deleteLater );
87 jd.job->cancelWithoutBlocking();
88 mJobs.remove( jd.job );
89 return;
90 }
91 }
92 Q_ASSERT( false && "requested job ID does not exist!" );
93}
94
95void QgsTerrainTextureGenerator::waitForFinished()
96{
97 for ( auto it = mJobs.keyBegin(); it != mJobs.keyEnd(); it++ )
98 disconnect( *it, &QgsMapRendererJob::finished, this, &QgsTerrainTextureGenerator::onRenderingFinished );
99 QVector<QgsMapRendererSequentialJob *> toBeDeleted;
100 for ( auto it = mJobs.constBegin(); it != mJobs.constEnd(); it++ )
101 {
102 QgsMapRendererSequentialJob *mapJob = it.key();
103 mapJob->waitForFinished();
104 JobData jobData = it.value();
105 toBeDeleted.push_back( mapJob );
106
107 QImage img = mapJob->renderedImage();
108
109 if ( mMap.showTerrainTilesInfo() )
110 {
111 // extra tile information for debugging
112 QPainter p( &img );
113 p.setPen( Qt::red );
114 p.setBackgroundMode( Qt::OpaqueMode );
115 QFont font = p.font();
116 font.setPixelSize( std::max( 30, mMap.terrainSettings()->mapTileResolution() / 6 ) );
117 p.setFont( font );
118 p.drawRect( 0, 0, img.width() - 1, img.height() - 1 );
119 p.drawText( img.rect(), jobData.debugText, QTextOption( Qt::AlignCenter ) );
120 p.end();
121 }
122
123 // pass QImage further
124 emit tileReady( jobData.jobId, img );
125 }
126
127 for ( QgsMapRendererSequentialJob *mapJob : toBeDeleted )
128 {
129 mJobs.remove( mapJob );
130 mapJob->deleteLater();
131 }
132}
133
134void QgsTerrainTextureGenerator::onRenderingFinished()
135{
136 QgsMapRendererSequentialJob *mapJob = static_cast<QgsMapRendererSequentialJob *>( sender() );
137
138 Q_ASSERT( mJobs.contains( mapJob ) );
139 JobData jobData = mJobs.value( mapJob );
140
141 QImage img = mapJob->renderedImage();
142
143 if ( mMap.showTerrainTilesInfo() )
144 {
145 // extra tile information for debugging
146 QPainter p( &img );
147 p.setPen( Qt::red );
148 p.setBackgroundMode( Qt::OpaqueMode );
149 QFont font = p.font();
150 font.setPixelSize( std::max( 30, mMap.terrainSettings()->mapTileResolution() / 6 ) );
151 p.setFont( font );
152 p.drawRect( 0, 0, img.width() - 1, img.height() - 1 );
153 p.drawText( img.rect(), jobData.debugText, QTextOption( Qt::AlignCenter ) );
154 p.end();
155 }
156
157 mapJob->deleteLater();
158 mJobs.remove( mapJob );
159
160 // QgsDebugMsgLevel( QStringLiteral("finished job %1 ... in queue: %2").arg( jobData.jobId).arg( jobs.count() ), 2 );
161
162 QgsEventTracing::addEvent( QgsEventTracing::AsyncEnd, QStringLiteral( "3D" ), QStringLiteral( "Texture" ), jobData.tileId.text() );
163
164 // pass QImage further
165 emit tileReady( jobData.jobId, img );
166}
167
168QgsMapSettings QgsTerrainTextureGenerator::baseMapSettings()
169{
170 QgsMapSettings mapSettings;
171
172 mapSettings.setOutputSize( mTextureSize );
173 mapSettings.setDestinationCrs( mMap.sceneMode() == Qgis::SceneMode::Globe ? mMap.crs().toGeographicCrs() : mMap.crs() );
174 mapSettings.setBackgroundColor( mMap.backgroundColor() );
175 mapSettings.setFlag( Qgis::MapSettingsFlag::DrawLabeling, mMap.showLabels() );
177 mapSettings.setTransformContext( mMap.transformContext() );
178 mapSettings.setPathResolver( mMap.pathResolver() );
179 mapSettings.setRendererUsage( mMap.rendererUsage() );
180
181 QList<QgsMapLayer *> layers;
182 QgsMapThemeCollection *mapThemes = mMap.mapThemeCollection();
183 QString mapThemeName = mMap.terrainMapTheme();
184 if ( mapThemeName.isEmpty() || !mapThemes || !mapThemes->hasMapTheme( mapThemeName ) )
185 {
186 layers = mMap.layers();
187 }
188 else
189 {
190 layers = mapThemes->mapThemeVisibleLayers( mapThemeName );
191 mapSettings.setLayerStyleOverrides( mapThemes->mapThemeStyleOverrides( mapThemeName ) );
192 }
193 layers.erase( std::remove_if( layers.begin(), layers.end(), []( const QgsMapLayer *layer ) { return layer->renderer3D(); } ), layers.end() );
194 mapSettings.setLayers( layers );
195
196 return mapSettings;
197}
198
@ Globe
Scene is represented as a globe using a geocentric CRS.
Definition qgis.h:4170
@ Local
Local scene based on a projected CRS.
Definition qgis.h:4169
@ DrawLabeling
Enable drawing of labels on top of the map.
Definition qgis.h:2721
@ Render3DMap
Render is for a 3D map.
Definition qgis.h:2730
Definition of the world.
Base class for all map layer types.
Definition qgsmaplayer.h:80
void finished()
emitted when asynchronous rendering is finished (or canceled).
void start()
Start the rendering job and immediately return.
Job implementation that renders everything sequentially in one thread.
QImage renderedImage() override
Gets a preview/resulting image.
void waitForFinished() override
Block until the job has finished.
Contains configuration for rendering maps.
void setLayers(const QList< QgsMapLayer * > &layers)
Sets the list of layers to render in the map.
void setRendererUsage(Qgis::RendererUsage rendererUsage)
Sets the rendering usage.
void setLayerStyleOverrides(const QMap< QString, QString > &overrides)
Sets the map of map layer style overrides (key: layer ID, value: style name) where a different style ...
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context, which stores various information regarding which datum transfo...
void setPathResolver(const QgsPathResolver &resolver)
Sets the path resolver for conversion between relative and absolute paths during rendering operations...
void setOutputSize(QSize size)
Sets the size of the resulting map image, in pixels.
void setBackgroundColor(const QColor &color)
Sets the background color of the map.
void setFlag(Qgis::MapSettingsFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected).
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Sets the destination crs (coordinate reference system) for the map render.
Container class that allows storage of map themes consisting of visible map layers and layer styles.
bool hasMapTheme(const QString &name) const
Returns whether a map theme with a matching name exists.
QList< QgsMapLayer * > mapThemeVisibleLayers(const QString &name) const
Returns the list of layers that are visible for the specified map theme.
QMap< QString, QString > mapThemeStyleOverrides(const QString &name)
Gets layer style overrides (for QgsMapSettings) of the visible layers for given map theme.
A rectangle specified with double values.
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
@ Flat
The whole terrain is flat area.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6607