]> git.mxchange.org Git - simgear.git/blob - simgear/route/route.hxx
add optional position argument to SGRoute::add_waypoint(). Default is -1,
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 #include <simgear/compiler.h>
37
38 #include STL_STRING
39 #include <vector>
40
41 SG_USING_STD(string);
42 SG_USING_STD(vector);
43
44 #include <simgear/route/waypoint.hxx>
45
46 /**
47  * A class to manage a list of waypoints (i.e. a route).
48  */
49
50 class SGRoute {
51
52 private:
53
54     typedef vector < SGWayPoint > route_list;
55     route_list route;
56     int current_wp;
57
58 public:
59
60     /** Constructor */
61     SGRoute();
62
63     /** Destructor */
64     ~SGRoute();
65
66     /** Clear the entire route */
67     inline void clear() {
68         route.clear();
69         current_wp = 0;
70     }
71
72     /**
73      * Add waypoint (default), or insert waypoint at position n.
74      * @param wp a waypoint
75      */
76     void add_waypoint( const SGWayPoint &wp, int n = -1 ) {
77         if ( n < 0 || n >= (int)route.size() )
78             route.push_back( wp );
79         else
80             route.insert( route.begin() + n, 1, wp );
81
82         int size = route.size();
83         if ( size > 1 ) {
84             SGWayPoint next_to_last = route[ size - 2 ];
85             double tmpd, tmpc;
86             wp.CourseAndDistance( next_to_last, &tmpc, &tmpd );
87             route[size - 1].set_distance( tmpd );
88         }
89     }
90
91     /**
92      * Get the number of waypoints (i.e. route length )
93      * @return route length
94      */
95     inline int size() const { return route.size(); }
96
97     /**
98      * Get the front waypoint.
99      * @return the first waypoint.
100      */
101     inline SGWayPoint get_first() const {
102         if ( route.size() ) {
103             return route[0];
104         } else {
105             return SGWayPoint( 0.0, 0.0, 0.0, SGWayPoint::WGS84, "invalid" );
106         }
107     }
108
109     /**
110      * Get the current waypoint
111      * @return the current waypoint
112      */
113     inline SGWayPoint get_current() const {
114         if ( current_wp < (int)route.size() ) {
115             return route[current_wp];
116         } else {
117             return SGWayPoint( 0.0, 0.0, 0.0, SGWayPoint::WGS84, "invalid" );
118         }
119     }
120
121     /**
122      * Set the current waypoint
123      * @param number of waypoint to make current.
124      */
125     inline void set_current( int n ) {
126         if ( n >= 0 && n < (int)route.size() ) {
127             current_wp = n;
128         }
129     }
130
131     /** Increment the current waypoint pointer. */
132     inline void increment_current() {
133         if ( current_wp < (int)route.size() - 1 ) {
134             ++current_wp;
135         }
136     }
137
138     /**
139      * Get the nth waypoint
140      * @param n waypoint number
141      * @return the nth waypoint
142      */
143     inline SGWayPoint get_waypoint( const int n ) const {
144         if ( n < (int)route.size() ) {
145             return route[n];
146         } else {
147             return SGWayPoint( 0.0, 0.0, 0.0, SGWayPoint::WGS84, "invalid" );
148         }
149     }
150
151     /** Delete the front waypoint */
152     inline void delete_first() { delete_waypoint(0); }
153
154     /** Delete waypoint waypoint with index n  (last one if n < 0) */
155     void delete_waypoint( int n = 0 ) {
156         if ( !route.size() )
157             return;
158         if ( n < 0 || n >= (int)route.size() )
159             n = route.size() - 1;
160
161         route.erase( route.begin() + n );
162     }
163
164     /**
165      * Calculate perpendicular distance from the current route segment
166      * This routine assumes all points are laying on a flat plane and
167      * ignores the altitude (or Z) dimension.  For most accurate
168      * results, use with CARTESIAN way points.
169      */
170     double distance_off_route( double x, double y ) const;
171 };
172
173
174 #endif // _ROUTE_HXX