From c481b064518d1b9b4225c8b6b25e45c6a2e229a6 Mon Sep 17 00:00:00 2001 From: curt Date: Wed, 11 Oct 2000 23:04:09 +0000 Subject: [PATCH] Added route.[ch]xx which maintains a list of waypoints (i.e. a route) Added an elevation field to each waypoint. This can be used by the calling program however it wishes. --- simgear/route/Makefile.am | 3 +- simgear/route/route.cxx | 35 +++++++++++ simgear/route/route.hxx | 125 +++++++++++++++++++++++++++++++++++++ simgear/route/waypoint.cxx | 17 ++--- simgear/route/waypoint.hxx | 12 +++- simgear/route/waytest.cxx | 29 ++++----- 6 files changed, 196 insertions(+), 25 deletions(-) create mode 100644 simgear/route/route.cxx create mode 100644 simgear/route/route.hxx diff --git a/simgear/route/Makefile.am b/simgear/route/Makefile.am index 2f2b30b7..540a31df 100644 --- a/simgear/route/Makefile.am +++ b/simgear/route/Makefile.am @@ -2,9 +2,10 @@ includedir = @includedir@/route lib_LIBRARIES = libsgroute.a -include_HEADERS = waypoint.hxx +include_HEADERS = route.hxx waypoint.hxx libsgroute_a_SOURCES = \ + route.cxx \ waypoint.cxx INCLUDES += -I$(top_srcdir) diff --git a/simgear/route/route.cxx b/simgear/route/route.cxx new file mode 100644 index 00000000..6052e0d7 --- /dev/null +++ b/simgear/route/route.cxx @@ -0,0 +1,35 @@ +// route.cxx -- Class to manage a list of waypoints (route) +// +// Written by Curtis Olson, started October 2000. +// +// Copyright (C) 2000 Curtis L. Olson - curt@hfrl.umn.edu +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// 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. +// +// $Id$ + + +#include "route.hxx" + + +// constructor +SGRoute::SGRoute() { + route.clear(); +} + + +// destructor +SGRoute::~SGRoute() { +} diff --git a/simgear/route/route.hxx b/simgear/route/route.hxx new file mode 100644 index 00000000..7ba46010 --- /dev/null +++ b/simgear/route/route.hxx @@ -0,0 +1,125 @@ +// route.hxx -- Class to manage a list of waypoints (route) +// +// Written by Curtis Olson, started October 2000. +// +// Copyright (C) 2000 Curtis L. Olson - curt@hfrl.umn.edu +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// 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. +// +// $Id$ + + +#ifndef _ROUTE_HXX +#define _ROUTE_HXX + + +#ifndef __cplusplus +# error This library requires C++ +#endif + + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include + +#include STL_STRING +#include + +FG_USING_STD(string); +FG_USING_STD(vector); + +#include "waypoint.hxx" + + +class SGRoute { + +private: + + typedef vector < SGWayPoint > route_list; + route_list route; + int current_wp; + +public: + + SGRoute(); + ~SGRoute(); + + // clear the entire route + inline void clear() { + route.clear(); + current_wp = 0; + } + + // add a waypoint + inline void add_waypoint( const SGWayPoint &wp ) { + route.push_back( wp ); + } + + // get the number of waypoints + inline int size() const { return route.size(); } + + // get the front waypoint + inline SGWayPoint get_first() const { + if ( route.size() ) { + return route[0]; + } else { + return SGWayPoint( 0.0, 0.0, 0.0, SGWayPoint::WGS84, "invalid" ); + } + } + + // get the current waypoint + inline SGWayPoint get_current() const { + if ( current_wp < (int)route.size() ) { + return route[current_wp]; + } else { + return SGWayPoint( 0.0, 0.0, 0.0, SGWayPoint::WGS84, "invalid" ); + } + } + + // set the current waypoint + inline void set_current( int n ) { + if ( n >= 0 && n < (int)route.size() ) { + current_wp = n; + } + } + + // increment the current waypoint + inline void increment_current() { + if ( current_wp < (int)route.size() - 1 ) { + ++current_wp; + } + } + + // get the nth waypoint + inline SGWayPoint get_waypoint( const int n ) const { + if ( n < (int)route.size() ) { + return route[n]; + } else { + return SGWayPoint( 0.0, 0.0, 0.0, SGWayPoint::WGS84, "invalid" ); + } + } + + // delete the front waypoint + inline void delete_first() { + if ( route.size() ) { + route.erase( route.begin() ); + } + } +}; + + +#endif // _ROUTE_HXX diff --git a/simgear/route/waypoint.cxx b/simgear/route/waypoint.cxx index ec490744..50807ea9 100644 --- a/simgear/route/waypoint.cxx +++ b/simgear/route/waypoint.cxx @@ -28,17 +28,18 @@ // Constructor -SGWayPoint::SGWayPoint( const double lon, const double lat, +SGWayPoint::SGWayPoint( const double lon, const double lat, const double alt, const modetype m, const string s ) { target_lon = lon; target_lat = lat; + target_alt = alt; mode = m; id = s; } SGWayPoint::SGWayPoint() { - SGWayPoint( 0.0, 0.0, WGS84, "" ); + SGWayPoint( 0.0, 0.0, 0.0, WGS84, "" ); } @@ -48,14 +49,16 @@ SGWayPoint::~SGWayPoint() { // Calculate course and distances. For WGS84 and SPHERICAL -// coordinates lat, lon, and course are in degrees and distance in -// meters. For CARTESIAN coordinates x = lon, y = lat. Course is in -// degrees and distance is in what ever units x and y are in. -void SGWayPoint::CourseAndDistance( const double cur_lon, const double cur_lat, +// coordinates lat, lon, and course are in degrees, alt and distance +// are in meters. For CARTESIAN coordinates x = lon, y = lat. Course +// is in degrees and distance is in what ever units x and y are in. +void SGWayPoint::CourseAndDistance( const double cur_lon, + const double cur_lat, + const double cur_alt, double *course, double *distance ) { if ( mode == WGS84 ) { double reverse; - geo_inverse_wgs_84( 0.0, cur_lat, cur_lon, target_lat, target_lon, + geo_inverse_wgs_84( cur_alt, cur_lat, cur_lon, target_lat, target_lon, course, &reverse, distance ); } else if ( mode == SPHERICAL ) { Point3D current( cur_lon * DEG_TO_RAD, cur_lat * DEG_TO_RAD, 0.0 ); diff --git a/simgear/route/waypoint.hxx b/simgear/route/waypoint.hxx index 46730878..913a6b6b 100644 --- a/simgear/route/waypoint.hxx +++ b/simgear/route/waypoint.hxx @@ -57,24 +57,30 @@ private: double target_lon; double target_lat; + double target_alt; string id; public: SGWayPoint(); - SGWayPoint( const double lon, const double lat, + SGWayPoint( const double lon, const double lat, const double alt, const modetype m = WGS84, const string s = "" ); ~SGWayPoint(); - // Calculate course and distances. Lat, lon, and azimuth are in - // degrees. distance in meters + // Calculate course and distances. For WGS84 and SPHERICAL + // coordinates lat, lon, and course are in degrees, alt and + // distance are in meters. For CARTESIAN coordinates x = lon, y = + // lat. Course is in degrees and distance is in what ever units x + // and y are in. void CourseAndDistance( const double cur_lon, const double cur_lat, + const double cur_alt, double *course, double *distance ); inline modetype get_mode() const { return mode; } inline double get_target_lon() const { return target_lon; } inline double get_target_lat() const { return target_lat; } + inline double get_target_alt() const { return target_alt; } inline string get_id() const { return id; } }; diff --git a/simgear/route/waytest.cxx b/simgear/route/waytest.cxx index ddcc4eaa..ec748697 100644 --- a/simgear/route/waytest.cxx +++ b/simgear/route/waytest.cxx @@ -2,35 +2,36 @@ #include "waypoint.hxx" int main() { - SGWayPoint a1(-93.216923, 44.880547, SGWayPoint::WGS84, "KMSP"); - SGWayPoint a2(-93.216923, 44.880547, SGWayPoint::SPHERICAL, "KMSP"); + SGWayPoint a1(-93.216923, 44.880547, 0.0, SGWayPoint::WGS84, "KMSP"); + SGWayPoint a2(-93.216923, 44.880547, 0.0, SGWayPoint::SPHERICAL, "KMSP"); // KMSN (Madison) double cur_lon = -89.336939; double cur_lat = 43.139541; + double cur_alt = 0.0; double course, distance; - a1.CourseAndDistance( cur_lon, cur_lat, &course, &distance ); + a1.CourseAndDistance( cur_lon, cur_lat, cur_alt, &course, &distance ); cout << "Course to " << a1.get_id() << " is " << course << endl; cout << "Distance to " << a1.get_id() << " is " << distance * METER_TO_NM << endl; - a2.CourseAndDistance( cur_lon, cur_lat, &course, &distance ); + a2.CourseAndDistance( cur_lon, cur_lat, cur_alt, &course, &distance ); cout << "Course to " << a2.get_id() << " is " << course << endl; cout << "Distance to " << a2.get_id() << " is " << distance * METER_TO_NM << endl; cout << endl; - SGWayPoint b1(-88.237037, 43.041038, SGWayPoint::WGS84, "KUES"); - SGWayPoint b2(-88.237037, 43.041038, SGWayPoint::SPHERICAL, "KUES"); + SGWayPoint b1(-88.237037, 43.041038, 0.0, SGWayPoint::WGS84, "KUES"); + SGWayPoint b2(-88.237037, 43.041038, 0.0, SGWayPoint::SPHERICAL, "KUES"); - b1.CourseAndDistance( cur_lon, cur_lat, &course, &distance ); + b1.CourseAndDistance( cur_lon, cur_lat, cur_alt, &course, &distance ); cout << "Course to " << b1.get_id() << " is " << course << endl; cout << "Distance to " << b1.get_id() << " is " << distance * METER_TO_NM << endl; - b2.CourseAndDistance( cur_lon, cur_lat, &course, &distance ); + b2.CourseAndDistance( cur_lon, cur_lat, cur_alt, &course, &distance ); cout << "Course to " << b2.get_id() << " is " << course << endl; cout << "Distance to " << b2.get_id() << " is " << distance * METER_TO_NM << endl; @@ -39,18 +40,18 @@ int main() { cur_lon = 10; cur_lat = 10; - SGWayPoint c1(-20, 10, SGWayPoint::CARTESIAN, "Due East"); - c1.CourseAndDistance( cur_lon, cur_lat, &course, &distance ); + SGWayPoint c1(-20, 10, 0, SGWayPoint::CARTESIAN, "Due East"); + c1.CourseAndDistance( cur_lon, cur_lat, cur_alt, &course, &distance ); cout << "Course to " << c1.get_id() << " is " << course << endl; cout << "Distance to " << c1.get_id() << " is " << distance << endl; - SGWayPoint c2(20, 20, SGWayPoint::CARTESIAN, "Due SW"); - c2.CourseAndDistance( cur_lon, cur_lat, &course, &distance ); + SGWayPoint c2(20, 20, 0, SGWayPoint::CARTESIAN, "Due SW"); + c2.CourseAndDistance( cur_lon, cur_lat, cur_alt, &course, &distance ); cout << "Course to " << c2.get_id() << " is " << course << endl; cout << "Distance to " << c2.get_id() << " is " << distance << endl; - SGWayPoint c3(20, 0, SGWayPoint::CARTESIAN, "Due NW"); - c3.CourseAndDistance( cur_lon, cur_lat, &course, &distance ); + SGWayPoint c3(20, 0, 0, SGWayPoint::CARTESIAN, "Due NW"); + c3.CourseAndDistance( cur_lon, cur_lat, cur_alt, &course, &distance ); cout << "Course to " << c3.get_id() << " is " << course << endl; cout << "Distance to " << c3.get_id() << " is " << distance << endl; -- 2.39.5