8 #include "SphericalScanlineTextureMapper.h"
15 #include "GeoPainter.h"
16 #include "GeoDataPolygon.h"
17 #include "MarbleDebug.h"
18 #include "Quaternion.h"
19 #include "ScanlineTextureMapperContext.h"
20 #include "StackedTileLoader.h"
21 #include "StackedTile.h"
22 #include "TextureColorizer.h"
24 #include "MathHelper.h"
29 class SphericalScanlineTextureMapper::RenderJob :
public QRunnable
38 const int m_tileLevel;
39 QImage *
const m_canvasImage;
47 : m_tileLoader( tileLoader ),
48 m_tileLevel( tileLevel ),
49 m_canvasImage( canvasImage ),
50 m_viewport( viewport ),
51 m_mapQuality( mapQuality ),
57 SphericalScanlineTextureMapper::SphericalScanlineTextureMapper(
StackedTileLoader *tileLoader )
58 : TextureMapperInterface()
59 , m_tileLoader( tileLoader )
65 void SphericalScanlineTextureMapper::mapTexture(
GeoPainter *painter,
68 const QRect &dirtyRect,
69 TextureColorizer *texColorizer )
71 if ( m_canvasImage.size() != viewport->size() || m_radius != viewport->radius() ) {
72 const QImage::Format optimalFormat = ScanlineTextureMapperContext::optimalCanvasImageFormat( viewport );
74 if ( m_canvasImage.size() != viewport->size() || m_canvasImage.format() != optimalFormat ) {
75 m_canvasImage =
QImage( viewport->size(), optimalFormat );
78 if ( !viewport->mapCoversViewport() ) {
79 m_canvasImage.fill( 0 );
82 m_radius = viewport->radius();
83 m_repaintNeeded =
true;
86 if ( m_repaintNeeded ) {
87 mapTexture( viewport, tileZoomLevel, painter->
mapQuality() );
90 texColorizer->colorize( &m_canvasImage, viewport, painter->
mapQuality() );
93 m_repaintNeeded =
false;
96 const int radius = viewport->radius();
98 QRect rect( viewport->width() / 2 - radius, viewport->height() / 2 - radius,
99 2 * radius, 2 * radius);
100 rect = rect.intersected( dirtyRect );
101 painter->
drawImage( rect, m_canvasImage, rect );
104 void SphericalScanlineTextureMapper::mapTexture(
const ViewportParams *viewport,
int tileZoomLevel,
MapQuality mapQuality )
107 m_tileLoader->resetTilehash();
111 const int imageHeight = m_canvasImage.height();
112 const qint64 radius = viewport->radius();
115 const int skip = ( mapQuality ==
LowQuality ) ? 1
117 const int yTop = ( imageHeight / 2 - radius >= 0 ) ? imageHeight / 2 - radius
119 const int yBottom = ( yTop == 0 ) ? imageHeight - skip
120 : yTop + radius + radius - skip;
122 const int numThreads = m_threadPool.maxThreadCount();
123 const int yStep = qCeil(qreal( yBottom - yTop ) / qreal(numThreads));
124 for (
int i = 0; i < numThreads; ++i ) {
125 const int yStart = yTop + i * yStep;
126 const int yEnd = qMin(yBottom, yTop + (i + 1) * yStep);
127 QRunnable *
const job =
new RenderJob( m_tileLoader, tileZoomLevel, &m_canvasImage, viewport, mapQuality, yStart, yEnd );
128 m_threadPool.start( job );
131 m_threadPool.waitForDone();
133 m_tileLoader->cleanupTilehash();
136 void SphericalScanlineTextureMapper::RenderJob::run()
138 const int imageHeight = m_canvasImage->height();
139 const int imageWidth = m_canvasImage->width();
140 const qint64 radius = m_viewport->radius();
141 const qreal inverseRadius = 1.0 / (qreal)(radius);
143 const bool interlaced = ( m_mapQuality ==
LowQuality );
144 const bool highQuality = ( m_mapQuality ==
HighQuality
146 const bool printQuality = ( m_mapQuality ==
PrintQuality );
149 const int n = ScanlineTextureMapperContext::interpolationStep( m_viewport, m_mapQuality );
152 Quaternion northPole = Quaternion::fromSpherical( 0.0, M_PI * 0.5 );
153 northPole.rotateAroundAxis( m_viewport->planetAxis().inverse() );
154 const int northPoleX = imageWidth / 2 + (int)( radius * northPole.v[Q_X] );
155 const int northPoleY = imageHeight / 2 - (int)( radius * northPole.v[Q_Y] );
158 matrix planetAxisMatrix;
159 m_viewport->planetAxis().toMatrix( planetAxisMatrix );
163 ScanlineTextureMapperContext context( m_tileLoader, m_tileLevel );
168 for (
int y = m_yTop; y < m_yBottom ; ++y ) {
171 const qreal qy = inverseRadius * (qreal)( imageHeight / 2 - y );
172 const qreal qr = 1.0 - qy * qy;
175 const int rx = (int)sqrt( (qreal)( radius * radius
176 - ( ( y - imageHeight / 2 )
177 * ( y - imageHeight / 2 ) ) ) );
191 const int xLeft = ( imageWidth / 2 - rx > 0 ) ? imageWidth / 2 - rx
193 const int xRight = ( imageWidth / 2 - rx > 0 ) ? xLeft + rx + rx
196 QRgb * scanLine = (QRgb*)( m_canvasImage->scanLine( y ) ) + xLeft;
198 const int xIpLeft = ( imageWidth / 2 - rx > 0 ) ? n * (
int)( xLeft / n + 1 )
200 const int xIpRight = ( imageWidth / 2 - rx > 0 ) ? n * (
int)( xRight / n - 1 )
201 : n * (
int)( xRight / n - 1 ) + 1;
204 bool crossingPoleArea =
false;
205 if ( northPole.v[Q_Z] > 0
206 && northPoleY - ( n * 0.75 ) <= y
207 && northPoleY + ( n * 0.75 ) >= y )
209 crossingPoleArea =
true;
214 for (
int x = xLeft; x < xRight; ++x ) {
217 const int leftInterval = xIpLeft + ncount * n;
219 bool interpolate =
false;
220 if ( x >= xIpLeft && x <= xIpRight ) {
224 if ( crossingPoleArea
225 && northPoleX >= leftInterval + n
226 && northPoleX < leftInterval + 2 * n
227 && x < leftInterval + 3 * n )
233 interpolate = !printQuality;
242 const qreal qx = (qreal)( x - imageWidth / 2 ) * inverseRadius;
243 const qreal qr2z = qr - qx * qx;
244 const qreal qz = ( qr2z > 0.0 ) ? sqrt( qr2z ) : 0.0;
248 Quaternion qpos( 0.0, qx, qy, qz );
249 qpos.rotateAroundAxis( planetAxisMatrix );
251 qpos.getSpherical( lon, lat );
258 context.pixelValueApproxF( lon, lat, scanLine, n );
260 context.pixelValueApprox( lon, lat, scanLine, n );
262 scanLine += ( n - 1 );
272 if ( x < imageWidth ) {
274 context.pixelValueF( lon, lat, scanLine );
276 context.pixelValue( lon, lat, scanLine );
283 if ( interlaced && y + 1 < m_yBottom ) {
285 const int pixelByteSize = m_canvasImage->bytesPerLine() / imageWidth;
287 memcpy( m_canvasImage->scanLine( y + 1 ) + xLeft * pixelByteSize,
288 m_canvasImage->scanLine( y ) + xLeft * pixelByteSize,
289 ( xRight - xLeft ) * pixelByteSize );