QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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 <QString>
27
28#include "moc_qgsterraintexturegenerator_p.cpp"
29
30using namespace Qt::StringLiterals;
31
33
34QgsTerrainTextureGenerator::QgsTerrainTextureGenerator( const Qgs3DMapSettings &map )
35 : mMap( map )
36 , mTextureSize( QSize( mMap.terrainSettings()->mapTileResolution(), mMap.terrainSettings()->mapTileResolution() ) )
37{}
38
39int QgsTerrainTextureGenerator::render( const QgsRectangle &extent, QgsChunkNodeId tileId, const QString &debugText )
40{
41 QgsMapSettings mapSettings( baseMapSettings() );
42 mapSettings.setExtent( extent );
43 QSize size = QSize( mTextureSize );
44
45 QgsRectangle clippedExtent = extent;
46 if ( mMap.sceneMode() == Qgis::SceneMode::Local && mMap.terrainGenerator()->type() == QgsTerrainGenerator::Flat )
47 {
48 // The flat terrain generator might have non-square tiles, clipped at the scene's extent.
49 // We need to produce non-square textures for those cases.
50 clippedExtent = extent.intersect( mMap.extent() );
51 mapSettings.setExtent( clippedExtent );
52 }
53 if ( !qgsDoubleNear( clippedExtent.width(), clippedExtent.height() ) )
54 {
55 if ( clippedExtent.height() > clippedExtent.width() )
56 size.setWidth( static_cast< int >( std::round( size.width() * clippedExtent.width() / clippedExtent.height() ) ) );
57 else if ( clippedExtent.height() < clippedExtent.width() )
58 size.setHeight( static_cast< int >( std::round( size.height() * clippedExtent.height() / clippedExtent.width() ) ) );
59 }
60 mapSettings.setOutputSize( size );
61
62 QgsEventTracing::addEvent( QgsEventTracing::AsyncBegin, u"3D"_s, u"Texture"_s, tileId.text() );
63
65 connect( job, &QgsMapRendererJob::finished, this, &QgsTerrainTextureGenerator::onRenderingFinished );
66
67 JobData jobData;
68 jobData.jobId = ++mLastJobId;
69 jobData.tileId = tileId;
70 jobData.job = job;
71 jobData.extent = extent;
72 jobData.debugText = debugText;
73
74 mJobs.insert( job, jobData ); //store job data just before launching the job
75 job->start();
76
77 // QgsDebugMsgLevel( u"added job: %1 .... in queue: %2"_s.arg( jobData.jobId ).arg( jobs.count() ), 2);
78 return jobData.jobId;
79}
80
81void QgsTerrainTextureGenerator::cancelJob( int jobId )
82{
83 for ( const JobData &jd : std::as_const( mJobs ) )
84 {
85 if ( jd.jobId == jobId )
86 {
87 // QgsDebugMsgLevel( u"canceling job %1"_s.arg( jobId ), 2 );
88 disconnect( jd.job, &QgsMapRendererJob::finished, this, &QgsTerrainTextureGenerator::onRenderingFinished );
89 connect( jd.job, &QgsMapRendererJob::finished, jd.job, &QgsMapRendererSequentialJob::deleteLater );
90 jd.job->cancelWithoutBlocking();
91 mJobs.remove( jd.job );
92 return;
93 }
94 }
95 Q_ASSERT( false && "requested job ID does not exist!" );
96}
97
98void QgsTerrainTextureGenerator::waitForFinished()
99{
100 for ( auto it = mJobs.keyBegin(); it != mJobs.keyEnd(); it++ )
101 disconnect( *it, &QgsMapRendererJob::finished, this, &QgsTerrainTextureGenerator::onRenderingFinished );
102 QVector<QgsMapRendererSequentialJob *> toBeDeleted;
103 for ( auto it = mJobs.constBegin(); it != mJobs.constEnd(); it++ )
104 {
105 QgsMapRendererSequentialJob *mapJob = it.key();
106 mapJob->waitForFinished();
107 JobData jobData = it.value();
108 toBeDeleted.push_back( mapJob );
109
110 QImage img = mapJob->renderedImage();
111
112 if ( mMap.showTerrainTilesInfo() )
113 {
114 // extra tile information for debugging
115 QPainter p( &img );
116 p.setPen( Qt::red );
117 p.setBackgroundMode( Qt::OpaqueMode );
118 QFont font = p.font();
119 font.setPixelSize( std::max( 30, mMap.terrainSettings()->mapTileResolution() / 6 ) );
120 p.setFont( font );
121 p.drawRect( 0, 0, img.width() - 1, img.height() - 1 );
122 p.drawText( img.rect(), jobData.debugText, QTextOption( Qt::AlignCenter ) );
123 p.end();
124 }
125
126 // pass QImage further
127 emit tileReady( jobData.jobId, img );
128 }
129
130 for ( QgsMapRendererSequentialJob *mapJob : toBeDeleted )
131 {
132 mJobs.remove( mapJob );
133 mapJob->deleteLater();
134 }
135}
136
137void QgsTerrainTextureGenerator::onRenderingFinished()
138{
139 QgsMapRendererSequentialJob *mapJob = static_cast<QgsMapRendererSequentialJob *>( sender() );
140
141 Q_ASSERT( mJobs.contains( mapJob ) );
142 JobData jobData = mJobs.value( mapJob );
143
144 QImage img = mapJob->renderedImage();
145
146 if ( mMap.showTerrainTilesInfo() )
147 {
148 // extra tile information for debugging
149 QPainter p( &img );
150 p.setPen( Qt::red );
151 p.setBackgroundMode( Qt::OpaqueMode );
152 QFont font = p.font();
153 font.setPixelSize( std::max( 30, mMap.terrainSettings()->mapTileResolution() / 6 ) );
154 p.setFont( font );
155 p.drawRect( 0, 0, img.width() - 1, img.height() - 1 );
156 p.drawText( img.rect(), jobData.debugText, QTextOption( Qt::AlignCenter ) );
157 p.end();
158 }
159
160 mapJob->deleteLater();
161 mJobs.remove( mapJob );
162
163 // QgsDebugMsgLevel( u"finished job %1 ... in queue: %2"_s.arg( jobData.jobId).arg( jobs.count() ), 2 );
164
165 QgsEventTracing::addEvent( QgsEventTracing::AsyncEnd, u"3D"_s, u"Texture"_s, jobData.tileId.text() );
166
167 // pass QImage further
168 emit tileReady( jobData.jobId, img );
169}
170
171QgsMapSettings QgsTerrainTextureGenerator::baseMapSettings()
172{
173 QgsMapSettings mapSettings;
174
175 mapSettings.setOutputSize( mTextureSize );
176 mapSettings.setDestinationCrs( mMap.sceneMode() == Qgis::SceneMode::Globe ? mMap.crs().toGeographicCrs() : mMap.crs() );
177 mapSettings.setBackgroundColor( mMap.backgroundColor() );
178 mapSettings.setFlag( Qgis::MapSettingsFlag::DrawLabeling, mMap.showLabels() );
180 mapSettings.setTransformContext( mMap.transformContext() );
181 mapSettings.setPathResolver( mMap.pathResolver() );
182 mapSettings.setRendererUsage( mMap.rendererUsage() );
183
184 QList<QgsMapLayer *> layers;
185 QgsMapThemeCollection *mapThemes = mMap.mapThemeCollection();
186 QString mapThemeName = mMap.terrainMapTheme();
187 if ( mapThemeName.isEmpty() || !mapThemes || !mapThemes->hasMapTheme( mapThemeName ) )
188 {
189 layers = mMap.layers();
190 }
191 else
192 {
193 layers = mapThemes->mapThemeVisibleLayers( mapThemeName );
194 mapSettings.setLayerStyleOverrides( mapThemes->mapThemeStyleOverrides( mapThemeName ) );
195 }
196 layers.erase( std::remove_if( layers.begin(), layers.end(), []( const QgsMapLayer *layer ) { return layer->renderer3D(); } ), layers.end() );
197 mapSettings.setLayers( layers );
198
199 return mapSettings;
200}
201
@ Globe
Scene is represented as a globe using a geocentric CRS.
Definition qgis.h:4299
@ Local
Local scene based on a projected CRS.
Definition qgis.h:4298
@ DrawLabeling
Enable drawing of labels on top of the map.
Definition qgis.h:2816
@ Render3DMap
Render is for a 3D map.
Definition qgis.h:2826
Definition of the world.
Base class for all map layer types.
Definition qgsmaplayer.h:83
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:6975