]> git.mxchange.org Git - simgear.git/blobdiff - simgear/math/polar3d.hxx
Modified Files:
[simgear.git] / simgear / math / polar3d.hxx
index 8e42fdb03216b20249e58acfe9537fe6cb2fb82b..75d1f26cd65e8d4867050a7188ce4e4233d0dc09 100644 (file)
-// polar.hxx -- routines to deal with polar math and transformations
-//
+/**
+ * \file polar3d.hxx
+ * Routines to deal with polar math and transformations.
+ */
+
 // Written by Curtis Olson, started June 1997.
 //
-// Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
+// Copyright (C) 1997  Curtis L. Olson  - http://www.flightgear.org/~curt
 //
-// 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 library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Library 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
+// This library 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.
+// Library 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.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 
 
-#ifndef _POLAR_HXX
-#define _POLAR_HXX
+#ifndef _POLAR3D_HXX
+#define _POLAR3D_HXX
 
 
-#ifndef __cplusplus                                                          
+#ifndef __cplusplus
 # error This library requires C++
-#endif                                   
+#endif
 
 
 #include <simgear/constants.h>
-#include <simgear/point3d.hxx>
-
-
-// Find the Altitude above the Ellipsoid (WGS84) given the Earth
-// Centered Cartesian coordinate vector Distances are specified in
-// meters.
-double fgGeodAltFromCart(const Point3D& cp);
-
-
-// Convert a polar coordinate to a cartesian coordinate.  Lon and Lat
-// must be specified in radians.  The FG convention is for distances
-// to be specified in meters
-inline Point3D fgPolarToCart3d(const Point3D& p) {
-    double tmp = cos( p.lat() ) * p.radius();
-
-    return Point3D( cos( p.lon() ) * tmp,
-                   sin( p.lon() ) * tmp,
-                   sin( p.lat() ) * p.radius() );
+#include <simgear/math/point3d.hxx>
+
+#include "SGMath.hxx"
+
+/** 
+ * Find the Altitude above the Ellipsoid (WGS84) given the Earth
+ * Centered Cartesian coordinate vector Distances are specified in
+ * meters.
+ * @param cp point specified in cartesian coordinates
+ * @return altitude above the (wgs84) earth in meters
+ */
+inline double sgGeodAltFromCart(const Point3D& p)
+{
+  SGGeod geod;
+  SGGeodesy::SGCartToGeod(SGVec3<double>(p.x(), p.y(), p.z()), geod);
+  return geod.getElevationM();
 }
 
 
-// Convert a cartesian coordinate to polar coordinates (lon/lat
-// specified in radians.  Distances are specified in meters.
-inline Point3D fgCartToPolar3d(const Point3D& cp) {
-    return Point3D( atan2( cp.y(), cp.x() ),
-                   FG_PI_2 - 
-                   atan2( sqrt(cp.x()*cp.x() + cp.y()*cp.y()), cp.z() ),
-                   sqrt(cp.x()*cp.x() + cp.y()*cp.y() + cp.z()*cp.z()) );
+/**
+ * Convert a polar coordinate to a cartesian coordinate.  Lon and Lat
+ * must be specified in radians.  The SG convention is for distances
+ * to be specified in meters
+ * @param p point specified in polar coordinates
+ * @return the same point in cartesian coordinates
+ */
+inline Point3D sgPolarToCart3d(const Point3D& p)
+{
+  SGVec3<double> cart;
+  SGGeodesy::SGGeocToCart(SGGeoc::fromRadM(p.lon(), p.lat(), p.radius()), cart);
+  return Point3D::fromSGVec3(cart);
 }
 
 
-// calc new lon/lat given starting lon/lat, and offset radial, and
-// distance.  NOTE: starting point is specifed in radians, distance is
-// specified in meters (and converted internally to radians)
-// ... assumes a spherical world
-inline Point3D calc_lon_lat( const Point3D& orig, double course, double dist ) {
-    Point3D result;
-
-    // lat=asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc))
-    // IF (cos(lat)=0)
-    //   lon=lon1      // endpoint a pole
-    // ELSE
-    //   lon=mod(lon1-asin(sin(tc)*sin(d)/cos(lat))+pi,2*pi)-pi
-    // ENDIF
-
-    // printf("calc_lon_lat()  offset.theta = %.2f offset.dist = %.2f\n",
-    //        offset.theta, offset.dist);
-
-    dist *= METER_TO_NM * NM_TO_RAD;
-    
-    result.sety( asin( sin(orig.y()) * cos(dist) + 
-                      cos(orig.y()) * sin(dist) * cos(course) ) );
-
-    if ( cos(result.y()) < FG_EPSILON ) {
-        result.setx( orig.x() );      // endpoint a pole
-    } else {
-        result.setx( 
-           fmod(orig.x() - asin( sin(course) * sin(dist) / 
-                                 cos(result.y()) ) + FG_PI, FG_2PI) - FG_PI );
-    }
-
-    return result;
+/**
+ * Convert a cartesian coordinate to polar coordinates (lon/lat
+ * specified in radians.  Distances are specified in meters.
+ * @param cp point specified in cartesian coordinates
+ * @return the same point in polar coordinates
+ */
+inline Point3D sgCartToPolar3d(const Point3D& p)
+{
+  SGGeoc geoc;
+  SGGeodesy::SGCartToGeoc(SGVec3<double>(p.x(), p.y(), p.z()), geoc);
+  return Point3D::fromSGGeoc(geoc);
 }
 
 
-#endif // _POLAR_HXX
-
+/**
+ * Calculate new lon/lat given starting lon/lat, and offset radial, and
+ * distance.  NOTE: starting point is specifed in radians, distance is
+ * specified in meters (and converted internally to radians)
+ * ... assumes a spherical world.
+ * @param orig specified in polar coordinates
+ * @param course offset radial
+ * @param dist offset distance
+ * @return destination point in polar coordinates
+ */
+Point3D calc_gc_lon_lat( const Point3D& orig, double course, double dist );
+
+
+/**
+ * Calculate course/dist given two spherical points.
+ * @param start starting point
+ * @param dest ending point
+ * @param course resulting course
+ * @param dist resulting distance
+ */
+void calc_gc_course_dist( const Point3D& start, const Point3D& dest, 
+                                 double *course, double *dist );
+
+#if 0
+/**
+ * Calculate course/dist given two spherical points.
+ * @param start starting point
+ * @param dest ending point
+ * @param course resulting course
+ * @param dist resulting distance
+ */
+void calc_gc_course_dist( const Point3D& start, const Point3D& dest, 
+                                double *course, double *dist );
+#endif // 0
+
+#endif // _POLAR3D_HXX