• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.12
  • kdeedu
  • marble
  • tools
  • mapreproject
NwwMapImage.cpp
Go to the documentation of this file.
1 #include "NwwMapImage.h"
2 
3 #include "InterpolationMethod.h"
4 
5 #include <QDebug>
6 #include <cmath>
7 
8 NwwMapImage::NwwMapImage( QDir const & baseDirectory, int const tileLevel )
9  : m_tileEdgeLengthPixel( 512 ),
10  m_emptyPixel( qRgba( 0, 0, 0, 255 )),
11  m_baseDirectory( baseDirectory ),
12  m_tileLevel( tileLevel ),
13  m_mapWidthTiles( 10 * pow( 2, m_tileLevel )),
14  m_mapHeightTiles( 5 * pow( 2, m_tileLevel )),
15  m_mapWidthPixel( m_mapWidthTiles * m_tileEdgeLengthPixel ),
16  m_mapHeightPixel( m_mapHeightTiles * m_tileEdgeLengthPixel ),
17  m_interpolationMethod(),
18  m_tileCache( DefaultCacheSizeBytes )
19 {
20  if ( !m_baseDirectory.exists() )
21  qFatal( "Base directory '%s' does not exist.", m_baseDirectory.path().toStdString().c_str() );
22 
23  qDebug() << "tileLevel:" << m_tileLevel
24  << "\nmapWidthTiles:" << m_mapWidthTiles
25  << "\nmapHeightTiles:" << m_mapHeightTiles
26  << "\nmapWidthPixel:" << m_mapWidthPixel
27  << "\nmapHeightPixel:" << m_mapHeightPixel;
28 }
29 
30 QRgb NwwMapImage::pixel( double const lonRad, double const latRad )
31 {
32  double const x = lonRadToPixelX( lonRad );
33  double const y = latRadToPixelY( latRad );
34  return m_interpolationMethod->interpolate( x, y );
35 }
36 
37 QRgb NwwMapImage::pixel( int const x, int const y )
38 {
39  int const tileX = x / m_tileEdgeLengthPixel;
40  int const tileY = y / m_tileEdgeLengthPixel;
41 
42  // fast check if tile is missing
43  int const tileKey = tileId( tileX, tileY );
44  if ( m_tileMissing.contains( tileKey ))
45  return m_emptyPixel;
46 
47  QPair<QImage, bool> potentialTile = tile( tileX, tileY );
48  if ( !potentialTile.second )
49  return m_emptyPixel;
50  else
51  return potentialTile.first.pixel( x % m_tileEdgeLengthPixel,
52  m_tileEdgeLengthPixel - y % m_tileEdgeLengthPixel - 1 );
53 }
54 
55 void NwwMapImage::setBaseDirectory( QDir const & baseDirectory )
56 {
57  m_baseDirectory = baseDirectory;
58 }
59 
60 void NwwMapImage::setCacheSizeBytes(const int cacheSizeBytes)
61 {
62  m_tileCache.setMaxCost( cacheSizeBytes );
63 }
64 
65 void NwwMapImage::setInterpolationMethod( InterpolationMethod * const method )
66 {
67  m_interpolationMethod = method;
68  m_interpolationMethod->setMapImage( this );
69 }
70 
71 void NwwMapImage::setTileLevel( int const tileLevel )
72 {
73  m_tileLevel = tileLevel;
74  m_mapWidthTiles = 10 * pow( 2, m_tileLevel );
75  m_mapHeightTiles = 5 * pow( 2, m_tileLevel );
76  m_mapWidthPixel = m_mapWidthTiles * m_tileEdgeLengthPixel;
77  m_mapHeightPixel = m_mapHeightTiles * m_tileEdgeLengthPixel;
78 }
79 
80 inline int NwwMapImage::tileId( int const tileX, int const tileY )
81 {
82  return (tileX << 16) + tileY;
83 }
84 
85 QPair<QImage, bool> NwwMapImage::tile( int const tileX, int const tileY )
86 {
87  int const tileKey = tileId( tileX, tileY );
88 
89  // first check cache
90  QImage * const cachedTile = m_tileCache.object( tileKey );
91  if ( cachedTile )
92  return QPair<QImage, bool>( *cachedTile, true );
93 
94  QString const filename = QString("%1/%2/%2_%3.jpg")
95  .arg( m_baseDirectory.path() )
96  .arg( tileY, 4, 10, QLatin1Char('0'))
97  .arg( tileX, 4, 10, QLatin1Char('0'));
98  QImage tile;
99  bool const loaded = tile.load( filename );
100  if ( !loaded ) {
101  m_tileMissing.insert( tileKey );
102  //qDebug() << "Tile" << filename << "not found";
103  } else {
104  m_tileCache.insert( tileKey, new QImage( tile ), tile.byteCount() );
105  //qDebug() << "Tile" << filename << "loaded and inserted in cache";
106  }
107  return QPair<QImage, bool>( tile, loaded );
108 }
109 
110 inline double NwwMapImage::lonRadToPixelX( double const lonRad ) const
111 {
112  return static_cast<double>( m_mapWidthPixel ) / ( 2.0 * M_PI ) * lonRad
113  + 0.5 * static_cast<double>( m_mapWidthPixel );
114 }
115 
116 inline double NwwMapImage::latRadToPixelY( double const latRad ) const
117 {
118  return static_cast<double>( m_mapHeightPixel ) / M_PI * latRad
119  + 0.5 * static_cast<double>( m_mapHeightPixel );
120 }
121 
122 QRgb NwwMapImage::nearestNeighbor( double const x, double const y )
123 {
124  int const xr = round( x );
125  int const yr = round( y );
126  return pixel( xr, yr );
127 }
128 
129 QRgb NwwMapImage::bilinearInterpolation( double const x, double const y )
130 {
131  int const x1 = x;
132  int const x2 = x1 + 1;
133  int const y1 = y;
134  int const y2 = y1 + 1;
135 
136  QRgb const lowerLeftPixel = pixel( x1, y1 );
137  QRgb const lowerRightPixel = pixel( x2, y1 );
138  QRgb const upperLeftPixel = pixel( x1, y2 );
139  QRgb const upperRightPixel = pixel( x2, y2 );
140 
141  // interpolate horizontically
142  //
143  // x2 - x x2 - x
144  // ------- = ------ = x1 + 1 - x = 1 - fractionX
145  // x2 - x1 1
146  //
147  // x - x1 x - x1
148  // ------- = ------ = fractionX
149  // x2 - x1 1
150 
151  double const fractionX = x - x1;
152  double const lowerMidRed = ( 1.0 - fractionX ) * qRed( lowerLeftPixel ) + fractionX * qRed( lowerRightPixel );
153  double const lowerMidGreen = ( 1.0 - fractionX ) * qGreen( lowerLeftPixel ) + fractionX * qGreen( lowerRightPixel );
154  double const lowerMidBlue = ( 1.0 - fractionX ) * qBlue( lowerLeftPixel ) + fractionX * qBlue( lowerRightPixel );
155  double const lowerMidAlpha = ( 1.0 - fractionX ) * qAlpha( lowerLeftPixel ) + fractionX * qAlpha( lowerRightPixel );
156 
157  double const upperMidRed = ( 1.0 - fractionX ) * qRed( upperLeftPixel ) + fractionX * qRed( upperRightPixel );
158  double const upperMidGreen = ( 1.0 - fractionX ) * qGreen( upperLeftPixel ) + fractionX * qGreen( upperRightPixel );
159  double const upperMidBlue = ( 1.0 - fractionX ) * qBlue( upperLeftPixel ) + fractionX * qBlue( upperRightPixel );
160  double const upperMidAlpha = ( 1.0 - fractionX ) * qAlpha( upperLeftPixel ) + fractionX * qAlpha( upperRightPixel );
161 
162  // interpolate vertically
163  //
164  // y2 - y y2 - y
165  // ------- = ------ = y1 + 1 - y = 1 - fractionY
166  // y2 - y1 1
167  //
168  // y - y1 y - y1
169  // ------- = ------ = fractionY
170  // y2 - y1 1
171 
172  double const fractionY = y - y1;
173  double const red = ( 1.0 - fractionY ) * lowerMidRed + fractionY * upperMidRed;
174  double const green = ( 1.0 - fractionY ) * lowerMidGreen + fractionY * upperMidGreen;
175  double const blue = ( 1.0 - fractionY ) * lowerMidBlue + fractionY * upperMidBlue;
176  double const alpha = ( 1.0 - fractionY ) * lowerMidAlpha + fractionY * upperMidAlpha;
177 
178  return qRgba( round( red ), round( green ), round( blue ), round( alpha ));
179 }
NwwMapImage::NwwMapImage
NwwMapImage(QDir const &baseDirectory, int const tileLevel)
Definition: NwwMapImage.cpp:8
InterpolationMethod.h
InterpolationMethod
Definition: InterpolationMethod.h:8
NwwMapImage::setCacheSizeBytes
void setCacheSizeBytes(int const cacheSizeBytes)
Definition: NwwMapImage.cpp:60
NwwMapImage::setTileLevel
void setTileLevel(int const level)
Definition: NwwMapImage.cpp:71
InterpolationMethod::interpolate
virtual QRgb interpolate(double const x, double const y)=0
NwwMapImage::pixel
virtual QRgb pixel(double const lonRad, double const latRad)
Definition: NwwMapImage.cpp:30
NwwMapImage::setBaseDirectory
void setBaseDirectory(QDir const &baseDirectory)
Definition: NwwMapImage.cpp:55
NwwMapImage::setInterpolationMethod
void setInterpolationMethod(InterpolationMethod *const method)
Definition: NwwMapImage.cpp:65
InterpolationMethod::setMapImage
void setMapImage(ReadOnlyMapImage *const mapImage)
Definition: InterpolationMethod.h:22
M_PI
#define M_PI
Definition: GeoDataCoordinates.h:26
NwwMapImage.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:38:51 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal