1 // route.cxx -- Class to manage a list of waypoints (route)
3 // Written by Curtis Olson, started October 2000.
5 // Copyright (C) 2000 Curtis L. Olson - curt@hfrl.umn.edu
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 # include <simgear_config.h>
29 #include <simgear/math/vector.hxx>
44 /** Update the length of the leg ending at waypoint index */
45 void SGRoute::update_distance_and_track(int index)
47 SGWayPoint& curr = route[ index ];
54 const SGWayPoint& prev = route[index - 1];
55 curr.CourseAndDistance( prev, &course, &dist );
58 curr.set_distance(dist);
59 curr.set_track(course);
63 * Add waypoint (default), or insert waypoint at position n.
64 * @param wp a waypoint
66 void SGRoute::add_waypoint( const SGWayPoint &wp, int n ) {
67 int size = route.size();
68 if ( n < 0 || n >= size ) {
70 route.push_back( wp );
72 route.insert( route.begin() + n, 1, wp );
73 // update distance of next leg if not at end of route
74 update_distance_and_track( n + 1 );
76 update_distance_and_track( n );
79 /** Delete waypoint with index n (last one if n < 0) */
80 void SGRoute::delete_waypoint( int n ) {
81 int size = route.size();
84 if ( n < 0 || n >= size )
87 route.erase( route.begin() + n );
88 // update distance of next leg if not at end of route
90 update_distance_and_track( n );
93 double SGRoute::total_distance() const {
95 for (unsigned int i=0; i<route.size(); ++i) {
96 total += route[i].get_distance();