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

marble

  • sources
  • kde-4.14
  • kdeedu
  • marble
  • src
  • lib
  • marble
ViewportParams.cpp
Go to the documentation of this file.
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2007 Inge Wallin <ingwa@kde.org>
9 // Copyright 2008 Jens-Michael Hoffmann <jensmh@gmx.de>
10 // Copyright 2010-2013 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
11 //
12 
13 
14 #include "ViewportParams.h"
15 
16 #include <QRect>
17 
18 #include <QPainterPath>
19 #include <QPainterPathStroker>
20 #include <QRegion>
21 
22 #include "MarbleDebug.h"
23 #include "SphericalProjection.h"
24 #include "EquirectProjection.h"
25 #include "MercatorProjection.h"
26 
27 
28 namespace Marble
29 {
30 
31 class ViewportParamsPrivate
32 {
33 public:
34  ViewportParamsPrivate( Projection projection,
35  qreal centerLongitude, qreal centerLatitude,
36  int radius,
37  const QSize &size );
38 
39  static const AbstractProjection *abstractProjection( Projection projection );
40 
41  // These two go together. m_currentProjection points to one of
42  // the static Projection classes at the bottom.
43  Projection m_projection;
44  const AbstractProjection *m_currentProjection;
45 
46  // Parameters that determine the painting
47  qreal m_centerLongitude;
48  qreal m_centerLatitude;
49  Quaternion m_planetAxis; // Position, coded in a quaternion
50  matrix m_planetAxisMatrix;
51  int m_radius; // Zoom level (pixels / globe radius)
52  qreal m_angularResolution;
53 
54  QSize m_size; // width, height
55 
56 
57  bool m_dirtyBox;
58  GeoDataLatLonAltBox m_viewLatLonAltBox;
59 
60  static const SphericalProjection s_sphericalProjection;
61  static const EquirectProjection s_equirectProjection;
62  static const MercatorProjection s_mercatorProjection;
63 
64  GeoDataCoordinates m_focusPoint;
65 };
66 
67 const SphericalProjection ViewportParamsPrivate::s_sphericalProjection;
68 const EquirectProjection ViewportParamsPrivate::s_equirectProjection;
69 const MercatorProjection ViewportParamsPrivate::s_mercatorProjection;
70 
71 ViewportParamsPrivate::ViewportParamsPrivate( Projection projection,
72  qreal centerLongitude, qreal centerLatitude,
73  int radius,
74  const QSize &size )
75  : m_projection( projection ),
76  m_currentProjection( abstractProjection( projection ) ),
77  m_centerLongitude( centerLongitude ),
78  m_centerLatitude( centerLatitude ),
79  m_planetAxis(),
80  m_planetAxisMatrix(),
81  m_radius( radius ),
82  m_angularResolution( 4 / fabs( (qreal)( m_radius ) ) ),
83  m_size( size ),
84  m_dirtyBox( true ),
85  m_viewLatLonAltBox()
86 {
87 }
88 
89 const AbstractProjection *ViewportParamsPrivate::abstractProjection(Projection projection)
90 {
91  switch ( projection ) {
92  case Spherical:
93  return &s_sphericalProjection;
94  case Equirectangular:
95  return &s_equirectProjection;
96  case Mercator:
97  return &s_mercatorProjection;
98  }
99 
100  return 0;
101 }
102 
103 
104 ViewportParams::ViewportParams()
105  : d( new ViewportParamsPrivate( Spherical, 0, 0, 2000, QSize( 100, 100 ) ) )
106 {
107  centerOn( d->m_centerLongitude, d->m_centerLatitude );
108 }
109 
110 ViewportParams::ViewportParams( Projection projection,
111  qreal centerLongitude, qreal centerLatitude,
112  int radius,
113  const QSize &size )
114  : d( new ViewportParamsPrivate( projection, centerLongitude, centerLatitude, radius, size ) )
115 {
116  centerOn( d->m_centerLongitude, d->m_centerLatitude );
117 }
118 
119 ViewportParams::~ViewportParams()
120 {
121  delete d;
122 }
123 
124 
125 // ================================================================
126 // Getters and setters
127 
128 
129 Projection ViewportParams::projection() const
130 {
131  return d->m_projection;
132 }
133 
134 const AbstractProjection *ViewportParams::currentProjection() const
135 {
136  return d->m_currentProjection;
137 }
138 
139 void ViewportParams::setProjection(Projection newProjection)
140 {
141  d->m_projection = newProjection;
142  d->m_currentProjection = ViewportParamsPrivate::abstractProjection( newProjection );
143 
144  // We now need to reset the planetAxis to make sure
145  // that it's a valid axis orientation!
146  // So this line is important (although it might look odd) ! :
147  centerOn( d->m_centerLongitude, d->m_centerLatitude );
148 }
149 
150 int ViewportParams::polarity() const
151 {
152  // For mercator this just gives the extreme latitudes
153  // instead of the actual poles but it works fine as well:
154  GeoDataCoordinates northPole( 0.0, +currentProjection()->maxLat() );
155  GeoDataCoordinates southPole( 0.0, -currentProjection()->maxLat() );
156 
157  bool globeHidesN, globeHidesS;
158  qreal x;
159  qreal yN, yS;
160 
161  currentProjection()->screenCoordinates( northPole, this,
162  x, yN, globeHidesN );
163  currentProjection()->screenCoordinates( southPole, this,
164  x, yS, globeHidesS );
165 
166  int polarity = 0;
167 
168  // case of the flat map:
169  if ( !globeHidesN && !globeHidesS ) {
170  if ( yN < yS ) {
171  polarity = +1;
172  }
173  if ( yS < yN ) {
174  polarity = -1;
175  }
176  }
177  else {
178  if ( !globeHidesN && yN < height() / 2 ) {
179  polarity = +1;
180  }
181  if ( !globeHidesN && yN > height() / 2 ) {
182  polarity = -1;
183  }
184  if ( !globeHidesS && yS > height() / 2 ) {
185  polarity = +1;
186  }
187  if ( !globeHidesS && yS < height() / 2 ) {
188  polarity = -1;
189  }
190  }
191 
192  return polarity;
193 }
194 
195 int ViewportParams::radius() const
196 {
197  return d->m_radius;
198 }
199 
200 void ViewportParams::setRadius(int newRadius)
201 {
202  if ( newRadius > 0 ) {
203  d->m_dirtyBox = true;
204 
205  d->m_radius = newRadius;
206  d->m_angularResolution = 4 / fabs( (qreal)(d->m_radius) );
207  }
208 }
209 
210 void ViewportParams::centerOn( qreal lon, qreal lat )
211 {
212  if ( !d->m_currentProjection->traversablePoles() ) {
213  if ( lat > d->m_currentProjection->maxLat() )
214  lat = d->m_currentProjection->maxLat();
215 
216  if ( lat < d->m_currentProjection->minLat() )
217  lat = d->m_currentProjection->minLat();
218  } else {
219  while ( lat > M_PI )
220  lat -= 2 * M_PI;
221  while ( lat < -M_PI )
222  lat += 2 * M_PI;
223  }
224 
225  while ( lon > M_PI )
226  lon -= 2 * M_PI;
227  while ( lon < -M_PI )
228  lon += 2 * M_PI;
229 
230  d->m_centerLongitude = lon;
231  d->m_centerLatitude = lat;
232 
233  d->m_planetAxis = Quaternion::fromEuler( -lat, lon, 0.0 );
234  d->m_planetAxis.normalize();
235 
236  d->m_dirtyBox = true;
237  d->m_planetAxis.inverse().toMatrix( d->m_planetAxisMatrix );
238 }
239 
240 Quaternion ViewportParams::planetAxis() const
241 {
242  return d->m_planetAxis;
243 }
244 
245 const matrix &ViewportParams::planetAxisMatrix() const
246 {
247  return d->m_planetAxisMatrix;
248 }
249 
250 int ViewportParams::width() const
251 {
252  return d->m_size.width();
253 }
254 
255 int ViewportParams::height() const
256 {
257  return d->m_size.height();
258 }
259 
260 QSize ViewportParams::size() const
261 {
262  return d->m_size;
263 }
264 
265 
266 void ViewportParams::setWidth(int newWidth)
267 {
268  setSize( QSize( newWidth, height() ) );
269 }
270 
271 void ViewportParams::setHeight(int newHeight)
272 {
273  setSize( QSize( width(), newHeight ) );
274 }
275 
276 void ViewportParams::setSize(QSize newSize)
277 {
278  if ( newSize == d->m_size )
279  return;
280 
281  d->m_dirtyBox = true;
282 
283  d->m_size = newSize;
284 }
285 
286 // ================================================================
287 // Other functions
288 
289 qreal ViewportParams::centerLongitude() const
290 {
291  return d->m_centerLongitude;
292 }
293 
294 qreal ViewportParams::centerLatitude() const
295 {
296  return d->m_centerLatitude;
297 }
298 
299 void ViewportParams::centerCoordinates( qreal &centerLon, qreal &centerLat ) const
300 {
301  centerLon = d->m_centerLongitude;
302  centerLat = d->m_centerLatitude;
303 }
304 
305 const GeoDataLatLonAltBox& ViewportParams::viewLatLonAltBox() const
306 {
307  if (d->m_dirtyBox) {
308  d->m_viewLatLonAltBox = d->m_currentProjection->latLonAltBox( QRect( QPoint( 0, 0 ),
309  d->m_size ),
310  this );
311  d->m_dirtyBox = false;
312  }
313 
314  return d->m_viewLatLonAltBox;
315 }
316 
317 GeoDataLatLonAltBox ViewportParams::latLonAltBox( const QRect &screenRect ) const
318 {
319  return d->m_currentProjection->latLonAltBox( screenRect, this );
320 }
321 
322 qreal ViewportParams::angularResolution() const
323 {
324  // We essentially divide the diameter by 180 deg and
325  // take half of the result as a guess for the angle per pixel resolution.
326  // d->m_angularResolution = 0.25 * M_PI / fabs( (qreal)(d->m_radius);
327  return d->m_angularResolution;
328 }
329 
330 bool ViewportParams::resolves ( const GeoDataLatLonBox &latLonBox ) const
331 {
332  return latLonBox.width() + latLonBox.height() > 2.0 * angularResolution();
333 }
334 
335 
336 bool ViewportParams::resolves ( const GeoDataLatLonAltBox &latLonAltBox ) const
337 {
338  return latLonAltBox.width() + latLonAltBox.height() > 2.0 * angularResolution()
339  || latLonAltBox.maxAltitude() - latLonAltBox.minAltitude() > 10000;
340 
341 }
342 
343 bool ViewportParams::resolves ( const GeoDataCoordinates &coord1,
344  const GeoDataCoordinates &coord2 ) const
345 {
346  qreal lon1, lat1;
347  coord1.geoCoordinates( lon1, lat1 );
348 
349  qreal lon2, lat2;
350  coord2.geoCoordinates( lon2, lat2 );
351 
352  // We take the manhattan length as an approximation for the distance
353  return ( fabs( lon2 - lon1 ) + fabs( lat2 - lat1 ) < angularResolution() );
354 }
355 
356 
357 bool ViewportParams::screenCoordinates( const qreal lon, const qreal lat,
358  qreal &x, qreal &y ) const
359 {
360  return d->m_currentProjection->screenCoordinates( lon, lat, this, x, y );
361 }
362 
363 bool ViewportParams::screenCoordinates( const GeoDataCoordinates &geopoint,
364  qreal &x, qreal &y,
365  bool &globeHidesPoint ) const
366 {
367  return d->m_currentProjection->screenCoordinates( geopoint, this, x, y, globeHidesPoint );
368 }
369 
370 bool ViewportParams::screenCoordinates( const GeoDataCoordinates &geopoint,
371  qreal &x, qreal &y ) const
372 {
373  return d->m_currentProjection->screenCoordinates( geopoint, this, x, y );
374 }
375 
376 bool ViewportParams::screenCoordinates( const GeoDataCoordinates &coordinates,
377  qreal *x, qreal &y, int &pointRepeatNum,
378  const QSizeF& size,
379  bool &globeHidesPoint ) const
380 {
381  return d->m_currentProjection->screenCoordinates( coordinates, this, x, y, pointRepeatNum, size, globeHidesPoint );
382 }
383 
384 
385 bool ViewportParams::screenCoordinates( const GeoDataLineString &lineString,
386  QVector<QPolygonF*> &polygons ) const
387 {
388  return d->m_currentProjection->screenCoordinates( lineString, this, polygons );
389 }
390 
391 bool ViewportParams::geoCoordinates( const int x, const int y,
392  qreal &lon, qreal &lat,
393  GeoDataCoordinates::Unit unit ) const
394 {
395  return d->m_currentProjection->geoCoordinates( x, y, this, lon, lat, unit );
396 }
397 
398 bool ViewportParams::mapCoversViewport() const
399 {
400  return d->m_currentProjection->mapCoversViewport( this );
401 }
402 
403 QPainterPath ViewportParams::mapShape() const
404 {
405  return d->m_currentProjection->mapShape( this );
406 }
407 
408 QRegion ViewportParams::mapRegion() const
409 {
410  return d->m_currentProjection->mapRegion( this );
411 }
412 
413 GeoDataCoordinates ViewportParams::focusPoint() const
414 {
415  if (d->m_focusPoint.isValid()) {
416  return d->m_focusPoint;
417  }
418  else {
419  const qreal lon = d->m_centerLongitude;
420  const qreal lat = d->m_centerLatitude;
421 
422  return GeoDataCoordinates(lon, lat, 0.0, GeoDataCoordinates::Radian);
423  }
424 
425 }
426 
427 void ViewportParams::setFocusPoint(const GeoDataCoordinates &focusPoint)
428 {
429  d->m_focusPoint = focusPoint;
430 }
431 
432 void ViewportParams::resetFocusPoint()
433 {
434  d->m_focusPoint = GeoDataCoordinates();
435 }
436 
437 }
Marble::GeoDataCoordinates::Unit
Unit
enum used constructor to specify the units used
Definition: GeoDataCoordinates.h:64
Marble::GeoDataLatLonBox::height
qreal height(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
Get the height of the latitude interval.
Definition: GeoDataLatLonBox.cpp:255
Marble::ViewportParams::latLonAltBox
GeoDataLatLonAltBox latLonAltBox(const QRect &screenRect) const
Definition: ViewportParams.cpp:317
Marble::matrix
xmmfloat matrix[3]
Definition: Quaternion.h:38
Marble::GeoDataCoordinates
A 3d point representation.
Definition: GeoDataCoordinates.h:52
Marble::GeoDataCoordinates::Radian
Definition: GeoDataCoordinates.h:65
Marble::GeoDataLatLonBox::width
qreal width(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
Get the width of the longitude interval.
Definition: GeoDataLatLonBox.cpp:236
Marble::ViewportParams::size
QSize size() const
Definition: ViewportParams.cpp:260
Marble::ViewportParams::setProjection
void setProjection(Projection newProjection)
Definition: ViewportParams.cpp:139
Marble::ViewportParams::geoCoordinates
bool geoCoordinates(const int x, const int y, qreal &lon, qreal &lat, GeoDataCoordinates::Unit unit=GeoDataCoordinates::Degree) const
Get the earth coordinates corresponding to a pixel in the map.
Definition: ViewportParams.cpp:391
MercatorProjection.h
This file contains the headers for MercatorProjection.
Marble::ViewportParams::angularResolution
qreal angularResolution() const
Definition: ViewportParams.cpp:322
Marble::ViewportParams::viewLatLonAltBox
const GeoDataLatLonAltBox & viewLatLonAltBox() const
Definition: ViewportParams.cpp:305
Marble::ViewportParams::setRadius
void setRadius(int radius)
Change the radius of the planet.
Definition: ViewportParams.cpp:200
Marble::ViewportParams::projection
Projection projection() const
Definition: ViewportParams.cpp:129
QPoint
SphericalProjection.h
This file contains the headers for SphericalProjection.
MarbleDebug.h
Marble::ViewportParams::height
int height() const
Definition: ViewportParams.cpp:255
Marble::ViewportParams::ViewportParams
ViewportParams()
Definition: ViewportParams.cpp:104
Marble::Equirectangular
Flat projection ("plate carree")
Definition: MarbleGlobal.h:46
QRect
Marble::ViewportParams::setFocusPoint
void setFocusPoint(const GeoDataCoordinates &focusPoint)
Change the point of focus, overridding any previously set focus point.
Definition: ViewportParams.cpp:427
Marble::ViewportParams::setWidth
void setWidth(int newWidth)
Definition: ViewportParams.cpp:266
Marble::GeoDataCoordinates::geoCoordinates
void geoCoordinates(qreal &lon, qreal &lat, GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
use this function to get the longitude and latitude with one call - use the unit parameter to switch ...
Definition: GeoDataCoordinates.cpp:715
Marble::ViewportParams::width
int width() const
Definition: ViewportParams.cpp:250
Marble::Mercator
Mercator projection.
Definition: MarbleGlobal.h:47
Marble::ViewportParams::focusPoint
GeoDataCoordinates focusPoint() const
Definition: ViewportParams.cpp:413
Marble::ViewportParams::mapCoversViewport
bool mapCoversViewport() const
Definition: ViewportParams.cpp:398
Marble::radius
static qreal radius(qreal zoom)
Definition: thumbnailer.cpp:99
EquirectProjection.h
This file contains the headers for EquirectProjection.
Marble::ViewportParams::screenCoordinates
bool screenCoordinates(const qreal lon, const qreal lat, qreal &x, qreal &y) const
Get the screen coordinates corresponding to geographical coordinates in the map.
Definition: ViewportParams.cpp:357
Marble::ViewportParams::centerOn
void centerOn(qreal lon, qreal lat)
Definition: ViewportParams.cpp:210
Marble::ViewportParams::currentProjection
const AbstractProjection * currentProjection() const
Definition: ViewportParams.cpp:134
Marble::GeoDataLatLonAltBox::maxAltitude
qreal maxAltitude() const
Get the upper altitude boundary of the bounding box.
Definition: GeoDataLatLonAltBox.cpp:136
Marble::ViewportParams::polarity
int polarity() const
Definition: ViewportParams.cpp:150
Marble::ViewportParams::planetAxisMatrix
const matrix & planetAxisMatrix() const
Definition: ViewportParams.cpp:245
Marble::ViewportParams::mapRegion
QRegion mapRegion() const
Definition: ViewportParams.cpp:408
Marble::GeoDataLineString
A LineString that allows to store a contiguous set of line segments.
Definition: GeoDataLineString.h:75
Marble::ViewportParams::resetFocusPoint
void resetFocusPoint()
Invalidate any focus point set with setFocusPoint.
Definition: ViewportParams.cpp:432
Marble::AbstractProjection::screenCoordinates
bool screenCoordinates(const qreal lon, const qreal lat, const ViewportParams *viewport, qreal &x, qreal &y) const
Get the screen coordinates corresponding to geographical coordinates in the map.
Definition: AbstractProjection.cpp:118
Marble::ViewportParams::~ViewportParams
~ViewportParams()
Definition: ViewportParams.cpp:119
QSize
ViewportParams.h
This file contains the headers for ViewportParams.
Marble::ViewportParams::mapShape
QPainterPath mapShape() const
Definition: ViewportParams.cpp:403
Marble::ViewportParams::centerLatitude
qreal centerLatitude() const
Definition: ViewportParams.cpp:294
QPainterPath
QVector
QSizeF
Marble::ViewportParams::radius
int radius() const
Definition: ViewportParams.cpp:195
Marble::ViewportParams::centerLongitude
qreal centerLongitude() const
Definition: ViewportParams.cpp:289
Marble::Quaternion
Definition: Quaternion.h:41
Marble::AbstractProjection
A base class for all projections in Marble.
Definition: AbstractProjection.h:49
Marble::ViewportParams::resolves
bool resolves(const GeoDataLatLonBox &latLonBox) const
Definition: ViewportParams.cpp:330
Marble::ViewportParams::planetAxis
Quaternion planetAxis() const
Definition: ViewportParams.cpp:240
Marble::Projection
Projection
This enum is used to choose the projection shown in the view.
Definition: MarbleGlobal.h:44
M_PI
#define M_PI
Definition: GeoDataCoordinates.h:26
Marble::Spherical
Spherical projection.
Definition: MarbleGlobal.h:45
Marble::ViewportParams::setHeight
void setHeight(int newHeight)
Definition: ViewportParams.cpp:271
Marble::GeoDataLatLonAltBox
A class that defines a 3D bounding box for geographic data.
Definition: GeoDataLatLonAltBox.h:49
Marble::GeoDataLatLonAltBox::minAltitude
qreal minAltitude() const
Get the lower altitude boundary of the bounding box.
Definition: GeoDataLatLonAltBox.cpp:126
Marble::Quaternion::fromEuler
static Quaternion fromEuler(qreal pitch, qreal yaw, qreal roll)
Definition: Quaternion.cpp:105
Marble::ViewportParams::setSize
void setSize(QSize newSize)
Definition: ViewportParams.cpp:276
QRegion
Marble::GeoDataLatLonBox
A class that defines a 2D bounding box for geographic data.
Definition: GeoDataLatLonBox.h:51
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:42 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
  • 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