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