X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Froute%2Froute.cxx;h=d0da1804035ba40b4a6038be5feaa9e7748288e1;hb=7bdb530440d1dadc991f305edb1b70ec85f27451;hp=6052e0d7612d5e6b692b4522971a37d0dd48170f;hpb=c481b064518d1b9b4225c8b6b25e45c6a2e229a6;p=simgear.git diff --git a/simgear/route/route.cxx b/simgear/route/route.cxx index 6052e0d7..d0da1804 100644 --- a/simgear/route/route.cxx +++ b/simgear/route/route.cxx @@ -16,10 +16,13 @@ // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software -// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // // $Id$ +#ifdef HAVE_CONFIG_H +# include +#endif #include "route.hxx" @@ -33,3 +36,60 @@ SGRoute::SGRoute() { // destructor SGRoute::~SGRoute() { } + +/** Update the length of the leg ending at waypoint index */ +void SGRoute::update_distance_and_track(int index) +{ + SGWayPoint& curr = route[ index ]; + double course, dist; + + if ( index == 0 ) { + dist = 0; + course = 0.0; + } else { + const SGWayPoint& prev = route[index - 1]; + curr.CourseAndDistance( prev, &course, &dist ); + } + + curr.set_distance(dist); + curr.set_track(course); +} + +/** + * Add waypoint (default), or insert waypoint at position n. + * @param wp a waypoint + */ +void SGRoute::add_waypoint( const SGWayPoint &wp, int n ) { + int size = route.size(); + if ( n < 0 || n >= size ) { + n = size; + route.push_back( wp ); + } else { + route.insert( route.begin() + n, 1, wp ); + // update distance of next leg if not at end of route + update_distance_and_track( n + 1 ); + } + update_distance_and_track( n ); +} + +/** Delete waypoint with index n (last one if n < 0) */ +void SGRoute::delete_waypoint( int n ) { + int size = route.size(); + if ( size == 0 ) + return; + if ( n < 0 || n >= size ) + n = size - 1; + + route.erase( route.begin() + n ); + // update distance of next leg if not at end of route + if ( n < size - 1 ) + update_distance_and_track( n ); +} + +double SGRoute::total_distance() const { + double total = 0.0; + for (unsigned int i=0; i