]> git.mxchange.org Git - simgear.git/blob - simgear/route/route.cxx
Change SGWaypoint to use SGGeod internally. Remove some unused code, to
[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
24 #include <plib/sg.h>
25
26 #include <simgear/math/vector.hxx>
27
28 #include "route.hxx"
29
30
31 // constructor
32 SGRoute::SGRoute() {
33     route.clear();
34 }
35
36
37 // destructor
38 SGRoute::~SGRoute() {
39 }
40
41 /** Update the length of the leg ending at waypoint index */
42 void SGRoute::update_distance(int index)
43 {
44     SGWayPoint& curr = route[ index ];
45     double course, dist;
46
47     if ( index == 0 ) {
48         dist = 0;
49     } else {
50         const SGWayPoint& prev = route[ index - 1 ];
51         curr.CourseAndDistance( prev, &course, &dist );
52     }
53
54     curr.set_distance( dist );
55 }
56
57 /**
58  * Add waypoint (default), or insert waypoint at position n.
59  * @param wp a waypoint
60  */
61 void SGRoute::add_waypoint( const SGWayPoint &wp, int n ) {
62     int size = route.size();
63     if ( n < 0 || n >= size ) {
64         n = size;
65         route.push_back( wp );
66     } else {
67         route.insert( route.begin() + n, 1, wp );
68         // update distance of next leg if not at end of route
69         update_distance( n + 1 );
70     }
71     update_distance( n );
72 }
73
74 /** Delete waypoint with index n  (last one if n < 0) */
75 void SGRoute::delete_waypoint( int n ) {
76     int size = route.size();
77     if ( size == 0 )
78         return;
79     if ( n < 0 || n >= size )
80         n = size - 1;
81
82     route.erase( route.begin() + n );
83     // update distance of next leg if not at end of route
84     if ( n < size - 1 )
85         update_distance( n );
86 }
87
88 double SGRoute::total_distance() const {
89   double total = 0.0;
90   for (unsigned int i=0; i<route.size(); ++i) {
91     total += route[i].get_distance();
92   }
93   return total;
94 }