Marble

ScanlineTextureMapperContext.h
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2007 Andrew Manson <[email protected]>
4 // SPDX-FileCopyrightText: 2011 Bernhard Beschow <[email protected]>
5 //
6 
7 #ifndef MARBLE_SCANLINETEXTUREMAPPERCONTEXT_H
8 #define MARBLE_SCANLINETEXTUREMAPPERCONTEXT_H
9 
10 #include <QSize>
11 #include <QImage>
12 
13 #include "GeoSceneTileDataset.h"
14 #include "MarbleMath.h"
15 #include "MathHelper.h"
16 
17 namespace Marble
18 {
19 
20 class StackedTile;
21 class StackedTileLoader;
22 class ViewportParams;
23 
24 
25 class ScanlineTextureMapperContext
26 {
27 public:
28  ScanlineTextureMapperContext( StackedTileLoader * const tileLoader, int tileLevel );
29 
30  void pixelValueF( const qreal lon, const qreal lat,
31  QRgb* const scanLine );
32  void pixelValue( const qreal lon, const qreal lat,
33  QRgb* const scanLine );
34 
35  void pixelValueApproxF( const qreal lon, const qreal lat,
36  QRgb *scanLine, const int n );
37  void pixelValueApprox( const qreal lon, const qreal lat,
38  QRgb *scanLine, const int n );
39 
40  static int interpolationStep( const ViewportParams *viewport, MapQuality mapQuality );
41 
42  static QImage::Format optimalCanvasImageFormat( const ViewportParams *viewport );
43 
44  int globalWidth() const;
45  int globalHeight() const;
46 
47 private:
48  // method for fast integer calculation
49  void nextTile( int& posx, int& posy );
50 
51  // method for precise interpolation
52  void nextTile( qreal& posx, qreal& posy );
53 
54  // Converts Radian to global texture coordinates
55  // ( with origin in center, measured in pixel)
56  qreal rad2PixelX( const qreal lon ) const;
57  qreal rad2PixelY( const qreal lat ) const;
58 
59  // Checks whether the pixelValueApprox method will make use of more than
60  // one tile
61  bool isOutOfTileRange( const int itLon, const int itLat,
62  const int itStepLon, const int itStepLat,
63  const int n ) const;
64 
65  bool isOutOfTileRangeF( const qreal itLon, const qreal itLat,
66  const qreal itStepLon, const qreal itStepLat,
67  const int n ) const;
68 
69 private:
70  StackedTileLoader *const m_tileLoader;
71  GeoSceneAbstractTileProjection::Type const m_textureProjection;
72  /// size of the tiles of the current texture layer
73  QSize const m_tileSize;
74 
75  int const m_tileLevel;
76  int const m_globalWidth;
77  int const m_globalHeight;
78  qreal const m_normGlobalWidth;
79  qreal const m_normGlobalHeight;
80 
81  const StackedTile *m_tile;
82 
83  // Coordinate transformations:
84 
85  // Position of the tile in global Texture Coordinates
86  // ( with origin in upper left corner, measured in pixel)
87  int m_tilePosX;
88  int m_tilePosY;
89 
90  // Converts global texture coordinates
91  // ( with origin in center, measured in pixel)
92  // to tile coordinates ( measured in pixel )
93  qreal m_toTileCoordinatesLon;
94  qreal m_toTileCoordinatesLat;
95 
96  // Previous coordinates
97  qreal m_prevLat;
98  qreal m_prevLon;
99  qreal m_prevPixelX;
100  qreal m_prevPixelY;
101 };
102 
103 inline int ScanlineTextureMapperContext::globalWidth() const
104 {
105  return m_globalWidth;
106 }
107 
108 inline int ScanlineTextureMapperContext::globalHeight() const
109 {
110  return m_globalHeight;
111 }
112 
113 inline qreal ScanlineTextureMapperContext::rad2PixelX( const qreal lon ) const
114 {
115  return lon * m_normGlobalWidth;
116 }
117 
118 inline qreal ScanlineTextureMapperContext::rad2PixelY( const qreal lat ) const
119 {
120  switch ( m_textureProjection ) {
121  case GeoSceneAbstractTileProjection::Equirectangular:
122  return -lat * m_normGlobalHeight;
123  case GeoSceneAbstractTileProjection::Mercator:
124  if ( fabs( lat ) < 1.4835 ) {
125  // We develop the inverse Gudermannian into a MacLaurin Series:
126  // In spite of the many elements needed to get decent
127  // accuracy this is still faster by far than calculating the
128  // trigonometric expression:
129  // return - asinh( tan( lat ) ) * 0.5 * m_normGlobalHeight;
130 
131  // We are using the Horner Scheme as a polynom representation
132 
133  return - gdInv( lat ) * 0.5 * m_normGlobalHeight;
134  }
135  if ( lat >= +1.4835 )
136  // asinh( tan (1.4835)) => 3.1309587
137  return - 3.1309587 * 0.5 * m_normGlobalHeight;
138  if ( lat <= -1.4835 )
139  // asinh( tan( -1.4835 )) => −3.1309587
140  return 3.1309587 * 0.5 * m_normGlobalHeight;
141  }
142 
143  // Dummy value to avoid a warning.
144  return 0.0;
145 }
146 
147 }
148 
149 #endif
qreal gdInv(qreal x)
This method is a fast Mac Laurin power series approximation of the.
Definition: MarbleMath.h:73
MapQuality
This enum is used to choose the map quality shown in the view.
Definition: MarbleGlobal.h:74
Binds a QML item to a specific geodetic location in screen coordinates.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Sep 27 2023 04:09:07 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.