]> git.mxchange.org Git - simgear.git/blob - simgear/route/route.hxx
Doxygen.
[simgear.git] / simgear / route / route.hxx
1 /**
2  * \file route.hxx
3  * Provides a class to manage a list of waypoints (i.e. a route).
4  */
5
6 // Written by Curtis Olson, started October 2000.
7 //
8 // Copyright (C) 2000  Curtis L. Olson  - curt@hfrl.umn.edu
9 //
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 //
24 // $Id$
25
26
27 #ifndef _ROUTE_HXX
28 #define _ROUTE_HXX
29
30
31 #ifndef __cplusplus                                                          
32 # error This library requires C++
33 #endif                                   
34
35
36 #ifdef HAVE_CONFIG_H
37 #  include <config.h>
38 #endif
39
40 #include <simgear/compiler.h>
41
42 #include STL_STRING
43 #include <vector>
44
45 SG_USING_STD(string);
46 SG_USING_STD(vector);
47
48 #include <simgear/route/waypoint.hxx>
49
50 /**
51  * A class to manage a list of waypoints (i.e. a route).
52  */
53
54 class SGRoute {
55
56 private:
57
58     typedef vector < SGWayPoint > route_list;
59     route_list route;
60     int current_wp;
61
62 public:
63
64     /** Constructor */
65     SGRoute();
66
67     /** Destructor */
68     ~SGRoute();
69
70     /** Clear the entire route */
71     inline void clear() {
72         route.clear();
73         current_wp = 0;
74     }
75
76     /**
77      * Add a waypoint.
78      * @param wp a waypoint
79      */
80     inline void add_waypoint( const SGWayPoint &wp ) {
81         route.push_back( wp );
82
83         int size = route.size();
84         if ( size > 1 ) {
85             SGWayPoint next_to_last = route[ size - 2 ];
86             double tmpd, tmpc;
87             wp.CourseAndDistance( next_to_last, &tmpc, &tmpd );
88             route[size - 1].set_distance( tmpd );
89         }
90     }
91
92     /**
93      * Get the number of waypoints (i.e. route length )
94      * @return route length
95      */
96     inline int size() const { return route.size(); }
97
98     /**
99      * Get the front waypoint.
100      * @return the first waypoint.
101      */
102     inline SGWayPoint get_first() const {
103         if ( route.size() ) {
104             return route[0];
105         } else {
106             return SGWayPoint( 0.0, 0.0, 0.0, SGWayPoint::WGS84, "invalid" );
107         }
108     }
109
110     /**
111      * Get the current waypoint
112      * @return the current waypoint
113      */
114     inline SGWayPoint get_current() const {
115         if ( current_wp < (int)route.size() ) {
116             return route[current_wp];
117         } else {
118             return SGWayPoint( 0.0, 0.0, 0.0, SGWayPoint::WGS84, "invalid" );
119         }
120     }
121
122     /**
123      * Set the current waypoint
124      * @param number of waypoint to make current.
125      */
126     inline void set_current( int n ) {
127         if ( n >= 0 && n < (int)route.size() ) {
128             current_wp = n;
129         }
130     }
131
132     /** Increment the current waypoint pointer. */
133     inline void increment_current() {
134         if ( current_wp < (int)route.size() - 1 ) {
135             ++current_wp;
136         }
137     }
138
139     /**
140      * Get the nth waypoint
141      * @param n waypoint number
142      * @return the nth waypoint
143      */
144     inline SGWayPoint get_waypoint( const int n ) const {
145         if ( n < (int)route.size() ) {
146             return route[n];
147         } else {
148             return SGWayPoint( 0.0, 0.0, 0.0, SGWayPoint::WGS84, "invalid" );
149         }
150     }
151
152     /** Delete the front waypoint */
153     inline void delete_first() {
154         if ( route.size() ) {
155             route.erase( route.begin() );
156         }
157     }
158
159     /**
160      * Calculate perpendicular distance from the current route segment
161      * This routine assumes all points are laying on a flat plane and
162      * ignores the altitude (or Z) dimension.  For most accurate
163      * results, use with CARTESIAN way points.
164      */
165     double distance_off_route( double x, double y ) const;
166 };
167
168
169 #endif // _ROUTE_HXX