]> git.mxchange.org Git - simgear.git/blob - simgear/route/route.cxx
Remove deprecated vector classes - finally!
[simgear.git] / simgear / route / route.cxx
1 // route.cxx -- Class to manage a list of waypoints (route)
2 //
3 // Written by Curtis Olson, started October 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson  - curt@hfrl.umn.edu
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <simgear_config.h>
25 #endif
26
27 #include <plib/sg.h>
28
29 #include "route.hxx"
30
31
32 // constructor
33 SGRoute::SGRoute() {
34     route.clear();
35 }
36
37
38 // destructor
39 SGRoute::~SGRoute() {
40 }
41
42 /** Update the length of the leg ending at waypoint index */
43 void SGRoute::update_distance_and_track(int index)
44 {
45   SGWayPoint& curr = route[ index ];
46   double course, dist;
47
48   if ( index == 0 ) {
49     dist = 0;
50     course = 0.0;
51   } else {
52     const SGWayPoint& prev = route[index - 1];
53     curr.CourseAndDistance( prev, &course, &dist );
54   }
55
56   curr.set_distance(dist);
57   curr.set_track(course);
58 }
59
60 /**
61  * Add waypoint (default), or insert waypoint at position n.
62  * @param wp a waypoint
63  */
64 void SGRoute::add_waypoint( const SGWayPoint &wp, int n ) {
65     int size = route.size();
66     if ( n < 0 || n >= size ) {
67         n = size;
68         route.push_back( wp );
69     } else {
70         route.insert( route.begin() + n, 1, wp );
71         // update distance of next leg if not at end of route
72         update_distance_and_track( n + 1 );
73     }
74     update_distance_and_track( n );
75 }
76
77 /** Delete waypoint with index n  (last one if n < 0) */
78 void SGRoute::delete_waypoint( int n ) {
79     int size = route.size();
80     if ( size == 0 )
81         return;
82     if ( n < 0 || n >= size )
83         n = size - 1;
84
85     route.erase( route.begin() + n );
86     // update distance of next leg if not at end of route
87     if ( n < size - 1 )
88         update_distance_and_track( n );
89 }
90
91 double SGRoute::total_distance() const {
92   double total = 0.0;
93   for (unsigned int i=0; i<route.size(); ++i) {
94     total += route[i].get_distance();
95   }
96   return total;
97 }