• 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
  • examples
  • cpp
  • squad-interpolation
squad-interpolation.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 2014 Dennis Nienhüser <earthwings@gentoo.org>
9 //
10 
11 #include "squad-interpolation.h"
12 
13 #include <MarbleWidget.h>
14 #include <MarbleGlobal.h>
15 #include <GeoDataDocument.h>
16 #include <GeoDataPlacemark.h>
17 #include <GeoDataLineString.h>
18 #include <GeoDataTreeModel.h>
19 #include <MarblePlacemarkModel.h>
20 #include <GeoDataTypes.h>
21 #include <MarbleMath.h>
22 #include <ViewportParams.h>
23 
24 #include <QApplication>
25 #include <QTimer>
26 
27 namespace Marble
28 {
29 
30 MyPaintLayer::MyPaintLayer ( MarbleWidget *widget ) :
31  m_widget ( widget ),
32  m_fraction ( 0.0 ),
33  m_delta( 0.02 ),
34  m_index ( 0 )
35 {
36  GeoDataCoordinates::Unit const degree = GeoDataCoordinates::Degree;
37  m_cities << GeoDataCoordinates( 7.64573, 45.04981, 0.0, degree ); // Torino
38  m_cities << GeoDataCoordinates( 8.33439, 49.01673, 0.0, degree ); // Karlsruhe
39  m_cities << GeoDataCoordinates( 14.41637, 50.09329, 0.0, degree ); // Praha
40  m_cities << GeoDataCoordinates( 15.97254, 45.80268, 0.0, degree ); // Zagred
41  addInterpolatedPoint();
42 }
43 
44 QStringList MyPaintLayer::renderPosition() const
45 {
46  return QStringList() << "USER_TOOLS";
47 }
48 
49 bool MyPaintLayer::render ( GeoPainter *painter, ViewportParams *viewport, const QString &, GeoSceneLayer * )
50 {
51  if ( m_index < 20 ) {
52  // Gray dotted line connects all current cities
53  QPen grayPen = Marble::Oxygen::aluminumGray4;
54  grayPen.setWidth ( 3 );
55  grayPen.setStyle ( Qt::DotLine );
56  painter->setPen ( grayPen );
57  painter->drawPolyline ( m_cities );
58  }
59 
60  // Blue circle around each city
61  painter->setBrush ( QBrush ( QColor ( Marble::Oxygen::skyBlue4 ) ) );
62  painter->setPen ( QColor ( Marble::Oxygen::aluminumGray4 ) );
63  for ( int i = 0; i < m_cities.size(); ++i ) {
64  painter->drawEllipse ( m_cities[i], 32, 32 );
65  }
66 
67  if (m_index < 10) {
68  // Show how squad interpolation works internally
69  Q_ASSERT( m_cities.size() == 4 );
70  painter->setBrush ( QBrush ( QColor ( Marble::Oxygen::grapeViolet4 ) ) );
71  painter->setPen ( QColor ( Marble::Oxygen::aluminumGray4 ) );
72  GeoDataCoordinates a2 = basePoint( m_cities[0], m_cities[1], m_cities[2] );
73  painter->drawEllipse ( a2, 8, 8 );
74  qreal x, y;
75  if ( viewport->screenCoordinates ( a2, x, y ) ) {
76  painter->drawText( x+5, y, "A" );
77  }
78  GeoDataCoordinates b1 = basePoint( m_cities[1], m_cities[2], m_cities[3] );
79  painter->drawEllipse ( b1, 8, 8 );
80  if ( viewport->screenCoordinates ( b1, x, y ) ) {
81  painter->drawText( x+5, y, "B" );
82  }
83 
84  QPen grapePen = Marble::Oxygen::grapeViolet4;
85  grapePen.setWidth ( 2 );
86  painter->setPen ( grapePen );
87  GeoDataLineString string;
88  string << m_cities[0] << a2 << b1 << m_cities[3];
89  painter->drawPolyline ( string );
90 
91  GeoDataCoordinates i1 = m_cities[1].interpolate( m_cities[2], m_fraction-m_delta );
92  GeoDataCoordinates i2 = a2.interpolate( b1, m_fraction-m_delta );
93  QPen raspberryPen = Marble::Oxygen::burgundyPurple4;
94  raspberryPen.setWidth ( 2 );
95  painter->setPen ( raspberryPen );
96  GeoDataLineString inter;
97  inter << i1 << i2;
98  painter->drawPolyline ( inter );
99  }
100 
101  // Green linestring shows interpolation path
102  QPen greenPen = Marble::Oxygen::forestGreen4;
103  greenPen.setWidth ( 3 );
104  painter->setPen ( greenPen );
105  painter->drawPolyline ( m_interpolated, "Squad\nInterpolation", LineEnd );
106 
107  // Increasing city indices with some transparency effect for readability
108  QFont font = painter->font();
109  font.setBold( true );
110  painter->setFont( font );
111  QColor blue = QColor ( Marble::Oxygen::skyBlue4 );
112  blue.setAlpha( 150 );
113  painter->setBrush ( QBrush ( blue ) );
114  int const h = painter->fontMetrics().height();
115  for ( int i = 0; i < m_cities.size(); ++i ) {
116  qreal x, y;
117  QString const text = QString::number ( m_index + i );
118  int const w = painter->fontMetrics().width( text );
119  painter->setPen ( Qt::NoPen );
120  painter->drawEllipse ( m_cities[i], 1.5*w, 1.5*h );
121  painter->setPen ( QColor ( Marble::Oxygen::aluminumGray4 ) );
122  if ( viewport->screenCoordinates ( m_cities[i], x, y ) ) {
123  painter->drawText ( x-w/2, y+h/3, text );
124  }
125  }
126 
127  return true;
128 }
129 
130 GeoDataLatLonBox MyPaintLayer::center() const
131 {
132  GeoDataLinearRing ring;
133  foreach( const GeoDataCoordinates &city, m_cities ) {
134  ring << city;
135  }
136  return ring.latLonAltBox();
137 }
138 
139 void MyPaintLayer::addRandomCity ( double minDistance, double maxDistance )
140 {
141  minDistance *= KM2METER;
142  maxDistance *= KM2METER;
143  GeoDataTreeModel* tree = m_widget->model()->treeModel();
144  if ( !tree || tree->rowCount() < 6 || m_cities.isEmpty() ) {
145  return;
146  }
147 
148  // Traverse Marble's internal city database and add a random one
149  // which is in the requested distance range to the last one
150  for ( int i = 0; i < tree->rowCount(); ++i ) {
151  QVariant const data = tree->data ( tree->index ( i, 0 ), MarblePlacemarkModel::ObjectPointerRole );
152  GeoDataObject *object = qvariant_cast<GeoDataObject*> ( data );
153  Q_ASSERT ( object );
154  if ( object->nodeType() == GeoDataTypes::GeoDataDocumentType ) {
155  GeoDataDocument* document = static_cast<GeoDataDocument*> ( object );
156  if ( document->name() == "cityplacemarks" ) {
157  QVector<GeoDataPlacemark*> placemarks = document->placemarkList();
158  for ( int i = qrand() % placemarks.size(); i < placemarks.size(); ++i ) {
159  double const distance = EARTH_RADIUS * distanceSphere ( m_cities.last(), placemarks[i]->coordinate() );
160  if ( distance >= minDistance && distance <= maxDistance ) {
161  m_cities << placemarks[i]->coordinate();
162  return;
163  }
164  }
165  }
166  }
167  }
168 
169  addRandomCity();
170 }
171 
172 GeoDataCoordinates MyPaintLayer::basePoint( const GeoDataCoordinates &c1, const GeoDataCoordinates &c2, const GeoDataCoordinates &c3 )
173 {
174  Quaternion const a = (c2.quaternion().inverse() * c3.quaternion()).log();
175  Quaternion const b = (c2.quaternion().inverse() * c1.quaternion()).log();
176  Quaternion const c = c2.quaternion() * ((a+b)*-0.25).exp();
177  qreal lon, lat;
178  c.getSpherical( lon, lat );
179  return GeoDataCoordinates( lon, lat );
180 }
181 
182 void MyPaintLayer::addInterpolatedPoint()
183 {
184  while ( m_interpolated.size() > 2.0/m_delta ) {
185  m_interpolated.remove ( 0 );
186  }
187 
188  m_delta = m_index < 20 ? 0.01 : 0.04;
189  Q_ASSERT ( m_cities.size() == 4 );
190  // Interpolate for the current city
191  m_interpolated << m_cities[1].interpolate ( m_cities[0], m_cities[2], m_cities[3], m_fraction );
192  m_fraction += m_delta;
193 
194  // If current city is done, move one forward
195  if ( m_fraction > 1.0 ) {
196  m_fraction = 0.0;
197  m_cities.remove ( 0 );
198  addRandomCity();
199  ++m_index;
200  }
201 
202  // Repaint map, recenter if out of view
203  bool hidden;
204  qreal x; qreal y;
205  if ( m_widget->viewport()->screenCoordinates ( m_interpolated.last(), x, y, hidden ) ) {
206  m_widget->update();
207  } else {
208  m_widget->centerOn ( center() );
209  }
210 
211  int const timeout = qBound( 0, 150 - 50 * m_index, 150 );
212  QTimer::singleShot ( timeout, this, SLOT ( addInterpolatedPoint() ) );
213 }
214 
215 }
216 
217 int main ( int argc, char** argv )
218 {
219  using namespace Marble;
220  QApplication app ( argc, argv );
221  MarbleWidget *mapWidget = new MarbleWidget;
222  mapWidget->setWindowTitle( "Marble - Squad Interpolation" );
223 
224  // Create and register our paint layer
225  MyPaintLayer* layer = new MyPaintLayer ( mapWidget );
226  mapWidget->addLayer ( layer );
227  mapWidget->centerOn ( layer->center() );
228 
229  // Finish widget creation.
230  mapWidget->setMapThemeId( "earth/plain/plain.dgml" );
231  mapWidget->setShowCities( false );
232  mapWidget->setShowCrosshairs( false );
233  mapWidget->setShowOtherPlaces( false );
234  mapWidget->setShowPlaces( false );
235  mapWidget->setShowTerrain( false );
236  mapWidget->show();
237 
238  return app.exec();
239 }
240 
241 #include "squad-interpolation.moc"
Marble::GeoDataCoordinates::Unit
Unit
enum used constructor to specify the units used
Definition: GeoDataCoordinates.h:64
GeoDataDocument.h
Marble::GeoDataCoordinates
A 3d point representation.
Definition: GeoDataCoordinates.h:52
Marble::GeoDataLineString::last
GeoDataCoordinates & last()
Returns a reference to the last node in the LineString. This method detaches the returned coordinate ...
Definition: GeoDataLineString.cpp:169
QPen::setStyle
void setStyle(Qt::PenStyle style)
Marble::GeoDataTreeModel
The representation of GeoData in a model This class represents all available data given by kml-data f...
Definition: GeoDataTreeModel.h:32
Marble::GeoDataLinearRing
A LinearRing that allows to store a closed, contiguous set of line segments.
Definition: GeoDataLinearRing.h:68
MarbleMath.h
Marble::MarbleModel::treeModel
GeoDataTreeModel * treeModel()
Return the list of Placemarks as a QAbstractItemModel *.
Definition: MarbleModel.cpp:477
Marble::GeoPainter
A painter that allows to draw geometric primitives on the map.
Definition: GeoPainter.h:98
QPainter::font
const QFont & font() const
Marble::KM2METER
const qreal KM2METER
Definition: MarbleGlobal.h:223
Marble::GeoDataLineString::size
int size() const
Returns the number of nodes in a LineString.
Definition: GeoDataLineString.cpp:138
QFont
QApplication
Marble::GeoDataTypes::GeoDataDocumentType
const char * GeoDataDocumentType
Definition: GeoDataTypes.cpp:38
QColor::setAlpha
void setAlpha(int alpha)
Marble::GeoPainter::drawPolyline
void drawPolyline(const GeoDataLineString &lineString, const QString &labelText=QString(), LabelPositionFlags labelPositionFlags=LineCenter)
Draws a given line string (a "polyline").
Definition: GeoPainter.cpp:474
QBrush
Marble::distanceSphere
qreal distanceSphere(qreal lon1, qreal lat1, qreal lon2, qreal lat2)
This method calculates the shortest distance between two points on a sphere.
Definition: MarbleMath.h:52
Marble::GeoDataTreeModel::data
QVariant data(const QModelIndex &index, int role) const
Definition: GeoDataTreeModel.cpp:230
squad-interpolation.h
Marble::GeoPainter::drawEllipse
void drawEllipse(const GeoDataCoordinates &centerPosition, qreal width, qreal height, bool isGeoProjected=false)
Draws an ellipse at the given position. The ellipse is placed with its center located at the given ce...
Definition: GeoPainter.cpp:289
main
int main(int argc, char **argv)
Definition: squad-interpolation.cpp:217
Marble::MarblePlacemarkModel::ObjectPointerRole
The pointer to a specific object.
Definition: MarblePlacemarkModel.h:62
QWidget::update
void update()
Marble::GeoDataCoordinates::Degree
Definition: GeoDataCoordinates.h:66
Marble::MyPaintLayer::MyPaintLayer
MyPaintLayer(MarbleWidget *widget)
Definition: examples/cpp/custom-layers/main.cpp:49
Marble::MarbleWidget
A widget class that displays a view of the earth.
Definition: MarbleWidget.h:104
QFont::setBold
void setBold(bool enable)
QPainter::setFont
void setFont(const QFont &font)
QString::number
QString number(int n, int base)
Marble::GeoSceneLayer
Layer of a GeoScene document.
Definition: GeoSceneLayer.h:43
Marble::EARTH_RADIUS
const qreal EARTH_RADIUS
Definition: MarbleGlobal.h:257
Marble::Oxygen::aluminumGray4
QColor const aluminumGray4
Definition: MarbleColors.h:92
Marble::MyPaintLayer::renderPosition
QStringList renderPosition() const
Preferred level in the layer stack for the rendering.
Definition: examples/cpp/custom-layers/main.cpp:54
QPainter::setPen
void setPen(const QColor &color)
Marble::LineEnd
Definition: MarbleGlobal.h:114
Marble::MarbleWidget::model
MarbleModel * model()
Return the model that this view shows.
Definition: MarbleWidget.cpp:289
GeoDataLineString.h
QPainter::setBrush
void setBrush(const QBrush &brush)
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::Oxygen::skyBlue4
QColor const skyBlue4
Definition: MarbleColors.h:56
QString
MarblePlacemarkModel.h
QColor
MarbleGlobal.h
GeoDataPlacemark.h
GeoDataTreeModel.h
Marble::GeoDataLineString
A LineString that allows to store a contiguous set of line segments.
Definition: GeoDataLineString.h:75
QStringList
Marble::ViewportParams
A public class that controls what is visible in the viewport of a Marble map.
Definition: ViewportParams.h:44
Marble::MarbleWidget::viewport
ViewportParams * viewport()
Definition: MarbleWidget.cpp:299
Marble::MyPaintLayer
Definition: squad-interpolation.h:28
Marble::MarbleWidget::centerOn
void centerOn(const qreal lon, const qreal lat, bool animated=false)
Center the view on a geographical point.
Definition: MarbleWidget.cpp:549
Marble::MyPaintLayer::render
bool render(GeoPainter *painter, ViewportParams *viewport, const QString &renderPos="NONE", GeoSceneLayer *layer=0)
Renders the content provided by the layer on the viewport.
Definition: examples/cpp/custom-layers/main.cpp:91
QFontMetrics::width
int width(const QString &text, int len) const
ViewportParams.h
This file contains the headers for ViewportParams.
Marble::GeoDataLineString::remove
void remove(int i)
Removes the node at the given position and destroys it.
Definition: GeoDataLineString.cpp:635
Marble::GeoPainter::drawText
void drawText(const GeoDataCoordinates &position, const QString &text)
Draws the given text at a given geographic position. The text is drawn starting at the given position...
Definition: GeoPainter.cpp:264
QApplication::exec
int exec()
QVector
Marble::GeoDataLineString::isEmpty
bool isEmpty() const
Returns whether the LineString has no nodes at all.
Definition: GeoDataLineString.cpp:133
QPen::setWidth
void setWidth(int width)
Marble::Oxygen::burgundyPurple4
QColor const burgundyPurple4
Definition: MarbleColors.h:44
QPainter::fontMetrics
QFontMetrics fontMetrics() const
QWidget::setWindowTitle
void setWindowTitle(const QString &)
QFontMetrics::height
int height() const
Marble::Oxygen::forestGreen4
QColor const forestGreen4
Definition: MarbleColors.h:74
Marble::GeoDataTreeModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Definition: GeoDataTreeModel.cpp:424
QPen
MarbleWidget.h
This file contains the headers for MarbleWidget.
Marble::GeoDataTreeModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Return the number of Items in the Model.
Definition: GeoDataTreeModel.cpp:142
GeoDataTypes.h
QVector::size
int size() const
Marble::GeoDataLineString::latLonAltBox
virtual const GeoDataLatLonAltBox & latLonAltBox() const
Returns the smallest latLonAltBox that contains the LineString.
Definition: GeoDataLineString.cpp:580
Marble::GeoDataCoordinates::interpolate
GeoDataCoordinates interpolate(const GeoDataCoordinates &target, double t) const
slerp (spherical linear) interpolation between this coordinate and the given target coordinate ...
Definition: GeoDataCoordinates.cpp:1241
Marble::Oxygen::grapeViolet4
QColor const grapeViolet4
Definition: MarbleColors.h:50
Marble::MyPaintLayer::center
GeoDataLatLonBox center() const
Definition: squad-interpolation.cpp:130
Marble::GeoDataLatLonBox
A class that defines a 2D bounding box for geographic data.
Definition: GeoDataLatLonBox.h:51
QTimer::singleShot
singleShot
QVariant
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