• 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
  • routing
RouteSegment.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 2011 Dennis Nienhüser <earthwings@gentoo.org>
9 //
10 
11 #include "RouteSegment.h"
12 
13 #include "MarbleMath.h"
14 
15 namespace Marble
16 {
17 
18 RouteSegment::RouteSegment() :
19  m_valid( false ),
20  m_distance( 0.0 ),
21  m_travelTime( 0 ),
22  m_nextRouteSegment( 0 )
23 {
24  // nothing to do
25 }
26 
27 qreal RouteSegment::distance() const
28 {
29  return m_distance;
30 }
31 
32 const Maneuver & RouteSegment::maneuver() const
33 {
34  return m_maneuver;
35 }
36 
37 void RouteSegment::setManeuver( const Maneuver &maneuver )
38 {
39  m_maneuver = maneuver;
40  m_valid = true;
41 }
42 
43 const GeoDataLineString & RouteSegment::path() const
44 {
45  return m_path;
46 }
47 
48 void RouteSegment::setPath( const GeoDataLineString &path )
49 {
50  m_path = path;
51  m_distance = m_path.length( EARTH_RADIUS );
52  m_bounds = m_path.latLonAltBox();
53  m_valid = true;
54 }
55 
56 int RouteSegment::travelTime() const
57 {
58  return m_travelTime;
59 }
60 
61 void RouteSegment::setTravelTime( int seconds )
62 {
63  m_travelTime = seconds;
64  m_valid = true;
65 }
66 
67 GeoDataLatLonBox RouteSegment::bounds() const
68 {
69  return m_bounds;
70 }
71 
72 const RouteSegment & RouteSegment::nextRouteSegment() const
73 {
74  if ( m_nextRouteSegment ) {
75  return *m_nextRouteSegment;
76  }
77 
78  static RouteSegment invalid;
79  return invalid;
80 }
81 
82 void RouteSegment::setNextRouteSegment( const RouteSegment* segment )
83 {
84  m_nextRouteSegment = segment;
85  if ( segment ) {
86  m_valid = true;
87  }
88 }
89 
90 bool RouteSegment::isValid() const
91 {
92  return m_valid;
93 }
94 
95 qreal RouteSegment::distancePointToLine(const GeoDataCoordinates &p, const GeoDataCoordinates &a, const GeoDataCoordinates &b)
96 {
97  qreal const y0 = p.latitude();
98  qreal const x0 = p.longitude();
99  qreal const y1 = a.latitude();
100  qreal const x1 = a.longitude();
101  qreal const y2 = b.latitude();
102  qreal const x2 = b.longitude();
103  qreal const y01 = x0 - x1;
104  qreal const x01 = y0 - y1;
105  qreal const y10 = x1 - x0;
106  qreal const x10 = y1 - y0;
107  qreal const y21 = x2 - x1;
108  qreal const x21 = y2 - y1;
109  qreal const len =(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
110  qreal const t = (x01*x21 + y01*y21) / len;
111  if ( t<0.0 ) {
112  return EARTH_RADIUS * distanceSphere(p, a);
113  } else if ( t > 1.0 ) {
114  return EARTH_RADIUS * distanceSphere(p, b);
115  } else {
116  qreal const nom = qAbs( x21 * y10 - x10 * y21 );
117  qreal const den = sqrt( x21 * x21 + y21 * y21 );
118  return EARTH_RADIUS * nom / den;
119  }
120 }
121 
122 GeoDataCoordinates RouteSegment::projected(const GeoDataCoordinates &p, const GeoDataCoordinates &a, const GeoDataCoordinates &b)
123 {
124  qreal const y0 = p.latitude();
125  qreal const x0 = p.longitude();
126  qreal const y1 = a.latitude();
127  qreal const x1 = a.longitude();
128  qreal const y2 = b.latitude();
129  qreal const x2 = b.longitude();
130  qreal const y01 = x0 - x1;
131  qreal const x01 = y0 - y1;
132  qreal const y21 = x2 - x1;
133  qreal const x21 = y2 - y1;
134  qreal const len =(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
135  qreal const t = (x01*x21 + y01*y21) / len;
136  if ( t<0.0 ) {
137  return a;
138  } else if ( t > 1.0 ) {
139  return b;
140  } else {
141  // a + t (b - a);
142  qreal const lon = x1 + t * ( x2 - x1 );
143  qreal const lat = y1 + t * ( y2 - y1 );
144  return GeoDataCoordinates( lon, lat );
145  }
146 
147 }
148 
149 qreal RouteSegment::distanceTo( const GeoDataCoordinates &point, GeoDataCoordinates &closest, GeoDataCoordinates &interpolated ) const
150 {
151  Q_ASSERT( !m_path.isEmpty() );
152 
153  if ( m_path.size() == 1 ) {
154  closest = m_path.first();
155  return EARTH_RADIUS * distanceSphere( m_path.first(), point );
156  }
157 
158  qreal minDistance = -1.0;
159  int minIndex = 0;
160  for ( int i=1; i<m_path.size(); ++i ) {
161  qreal const distance = distancePointToLine( point, m_path[i-1], m_path[i] );
162  if ( minDistance < 0.0 || distance < minDistance ) {
163  minDistance = distance;
164  minIndex = i;
165  }
166  }
167 
168  closest = m_path[minIndex];
169  if ( minIndex == 0 ) {
170  interpolated = closest;
171  } else {
172  interpolated = projected( point, m_path[minIndex-1], m_path[minIndex] );
173  }
174  return minDistance;
175 }
176 
177 qreal RouteSegment::minimalDistanceTo( const GeoDataCoordinates &point ) const
178 {
179  if ( bounds().contains( point) ) {
180  return 0.0;
181  }
182 
183  qreal north(0.0), east(0.0), south(0.0), west(0.0);
184  bounds().boundaries( north, south, east, west );
185  GeoDataCoordinates const northWest( west, north );
186  GeoDataCoordinates const northEast( east, north );
187  GeoDataCoordinates const southhWest( west, south );
188  GeoDataCoordinates const southEast( east, south );
189 
190  qreal distNorth = distancePointToLine( point, northWest, northEast );
191  qreal distEast = distancePointToLine( point, northEast, southEast );
192  qreal distSouth = distancePointToLine( point, southhWest, southEast );
193  qreal distWest = distancePointToLine( point, northWest, southhWest );
194  return qMin( qMin( distNorth, distEast ), qMin( distWest, distSouth ) );
195 }
196 
197 bool RouteSegment::operator ==(const RouteSegment &other) const
198 {
199  return m_valid == other.m_valid &&
200  m_distance == other.m_distance &&
201  m_maneuver == other.m_maneuver &&
202  m_travelTime == other.m_travelTime &&
203  m_bounds == other.m_bounds &&
204  m_nextRouteSegment == other.m_nextRouteSegment;
205 }
206 
207 bool RouteSegment::operator !=(const RouteSegment &other) const
208 {
209  return !(other == *this);
210 }
211 
212 }
Marble::RouteSegment::travelTime
int travelTime() const
Definition: RouteSegment.cpp:56
Marble::GeoDataLineString::length
virtual qreal length(qreal planetRadius, int offset=0) const
Returns the length of LineString across a sphere starting from a coordinate in LineString This method...
Definition: GeoDataLineString.cpp:594
Marble::GeoDataCoordinates
A 3d point representation.
Definition: GeoDataCoordinates.h:52
Marble::RouteSegment::setPath
void setPath(const GeoDataLineString &path)
Definition: RouteSegment.cpp:48
Marble::GeoDataLatLonBox::boundaries
void boundaries(qreal &north, qreal &south, qreal &east, qreal &west, GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
Definition: GeoDataLatLonBox.cpp:198
Marble::RouteSegment::path
const GeoDataLineString & path() const
Definition: RouteSegment.cpp:43
MarbleMath.h
Marble::GeoDataLineString::size
int size() const
Returns the number of nodes in a LineString.
Definition: GeoDataLineString.cpp:138
Marble::RouteSegment::operator!=
bool operator!=(const RouteSegment &other) const
Definition: RouteSegment.cpp:207
Marble::RouteSegment::isValid
bool isValid() const
Definition: RouteSegment.cpp:90
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::GeoDataCoordinates::latitude
qreal latitude(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
retrieves the latitude of the GeoDataCoordinates object use the unit parameter to switch between Radi...
Definition: GeoDataCoordinates.cpp:751
Marble::RouteSegment::setTravelTime
void setTravelTime(int seconds)
Definition: RouteSegment.cpp:61
Marble::RouteSegment::bounds
GeoDataLatLonBox bounds() const
Definition: RouteSegment.cpp:67
Marble::Maneuver
Definition: Maneuver.h:22
Marble::EARTH_RADIUS
const qreal EARTH_RADIUS
Definition: MarbleGlobal.h:257
Marble::RouteSegment::RouteSegment
RouteSegment()
Definition: RouteSegment.cpp:18
Marble::GeoDataLineString::first
GeoDataCoordinates & first()
Returns a reference to the first node in the LineString. This method detaches the returned coordinate...
Definition: GeoDataLineString.cpp:177
Marble::GeoDataLineString
A LineString that allows to store a contiguous set of line segments.
Definition: GeoDataLineString.h:75
Marble::RouteSegment::nextRouteSegment
const RouteSegment & nextRouteSegment() const
Definition: RouteSegment.cpp:72
Marble::GeoDataCoordinates::longitude
qreal longitude(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
retrieves the longitude of the GeoDataCoordinates object use the unit parameter to switch between Rad...
Definition: GeoDataCoordinates.cpp:739
RouteSegment.h
Marble::RouteSegment::setNextRouteSegment
void setNextRouteSegment(const RouteSegment *segment)
Definition: RouteSegment.cpp:82
Marble::RouteSegment::operator==
bool operator==(const RouteSegment &other) const
Definition: RouteSegment.cpp:197
Marble::GeoDataLineString::isEmpty
bool isEmpty() const
Returns whether the LineString has no nodes at all.
Definition: GeoDataLineString.cpp:133
Marble::RouteSegment::distanceTo
qreal distanceTo(const GeoDataCoordinates &point, GeoDataCoordinates &closest, GeoDataCoordinates &interpolated) const
Definition: RouteSegment.cpp:149
Marble::RouteSegment::setManeuver
void setManeuver(const Maneuver &maneuver)
Definition: RouteSegment.cpp:37
Marble::RouteSegment::minimalDistanceTo
qreal minimalDistanceTo(const GeoDataCoordinates &point) const
Definition: RouteSegment.cpp:177
Marble::RouteSegment
Definition: RouteSegment.h:23
Marble::RouteSegment::distance
qreal distance() const
Definition: RouteSegment.cpp:27
Marble::RouteSegment::maneuver
const Maneuver & maneuver() const
Definition: RouteSegment.cpp:32
Marble::GeoDataLineString::latLonAltBox
virtual const GeoDataLatLonAltBox & latLonAltBox() const
Returns the smallest latLonAltBox that contains the LineString.
Definition: GeoDataLineString.cpp:580
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:41 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