head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.39; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.56; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.54.02; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.21.58; author curt; state Exp; branches; next ; desc @@ 1.4 log @Updated doxygen docs. @ text @
Main Page Class Hierarchy Compound List File List Compound Members File Members

# polar3d.hxx

Go to the documentation of this file.

    00001
    00006 // Written by Curtis Olson, started June 1997.
    00007 //
    00008 // Copyright (C) 1997  Curtis L. Olson  - curt@@infoplane.com
    00009 //
    00010 // This library is free software; you can redistribute it and/or
    00011 // modify it under the terms of the GNU Library General Public
    00012 // License as published by the Free Software Foundation; either
    00013 // version 2 of the License, or (at your option) any later version.
    00014 //
    00015 // This library is distributed in the hope that it will be useful,
    00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00018 // Library General Public License for more details.
    00019 //
    00020 // You should have received a copy of the GNU Library General Public
    00021 // License along with this library; if not, write to the
    00022 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00023 // Boston, MA  02111-1307, USA.
    00024 //
    00025 // $Id: polar3d.hxx,v 1.10 2001/06/12 21:44:03 curt Exp $
    00026
    00027
    00028 #ifndef _POLAR3D_HXX
    00029 #define _POLAR3D_HXX
    00030
    00031
    00032 #ifndef __cplusplus
    00033 # error This library requires C++
    00034 #endif
    00035
    00036
    00037 #include <math.h>
    00038
    00039 #include <simgear/constants.h>
    00040 #include <simgear/math/point3d.hxx>
    00041
    00042
    00050 double sgGeodAltFromCart(const Point3D& cp);
    00051
    00052
    00060 inline Point3D sgPolarToCart3d(const Point3D& p) {
    00061     double tmp = cos( p.lat() ) * p.radius();
    00062
    00063     return Point3D( cos( p.lon() ) * tmp,
    00064                     sin( p.lon() ) * tmp,
    00065                     sin( p.lat() ) * p.radius() );
    00066 }
    00067
    00068
    00075 inline Point3D sgCartToPolar3d(const Point3D& cp) {
    00076     return Point3D( atan2( cp.y(), cp.x() ),
    00077                     SGD_PI_2 -
    00078                     atan2( sqrt(cp.x()*cp.x() + cp.y()*cp.y()), cp.z() ),
    00079                     sqrt(cp.x()*cp.x() + cp.y()*cp.y() + cp.z()*cp.z()) );
    00080 }
    00081
    00082
    00093 inline Point3D calc_gc_lon_lat( const Point3D& orig, double course,
    00094                                 double dist ) {
    00095     Point3D result;
    00096
    00097     // lat=asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc))
    00098     // IF (cos(lat)=0)
    00099     //   lon=lon1      // endpoint a pole
    00100     // ELSE
    00101     //   lon=mod(lon1-asin(sin(tc)*sin(d)/cos(lat))+pi,2*pi)-pi
    00102     // ENDIF
    00103
    00104     // printf("calc_lon_lat()  offset.theta = %.2f offset.dist = %.2f\n",
    00105     //        offset.theta, offset.dist);
    00106
    00107     dist *= SG_METER_TO_NM * SG_NM_TO_RAD;
    00108
    00109     result.sety( asin( sin(orig.y()) * cos(dist) +
    00110                        cos(orig.y()) * sin(dist) * cos(course) ) );
    00111
    00112     if ( cos(result.y()) < SG_EPSILON ) {
    00113         result.setx( orig.x() );      // endpoint a pole
    00114     } else {
    00115         result.setx(
    00116             fmod(orig.x() - asin( sin(course) * sin(dist) /
    00117                                   cos(result.y()) )
    00118                  + SGD_PI, SGD_2PI) - SGD_PI );
    00119     }
    00120
    00121     return result;
    00122 }
    00123
    00124
    00132 inline void calc_gc_course_dist( const Point3D& start, const Point3D& dest,
    00133                                  double *course, double *dist )
    00134 {
    00135     // d = 2*asin(sqrt((sin((lat1-lat2)/2))^2 +
    00136     //            cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))
    00137     double cos_start_y = cos( start.y() );
    00138     volatile double tmp1 = sin( (start.y() - dest.y()) * 0.5 );
    00139     volatile double tmp2 = sin( (start.x() - dest.x()) * 0.5 );
    00140     double d = 2.0 * asin( sqrt( tmp1 * tmp1 +
    00141                                  cos_start_y * cos(dest.y()) * tmp2 * tmp2));
    00142
    00143     *dist = d * SG_RAD_TO_NM * SG_NM_TO_METER;
    00144
    00145     // We obtain the initial course, tc1, (at point 1) from point 1 to
    00146     // point 2 by the following. The formula fails if the initial
    00147     // point is a pole. We can special case this with:
    00148     //
    00149     // IF (cos(lat1) < EPS)   // EPS a small number ~ machine precision
    00150     //   IF (lat1 > 0)
    00151     //     tc1= pi        //  starting from N pole
    00152     //   ELSE
    00153     //     tc1= 0         //  starting from S pole
    00154     //   ENDIF
    00155     // ENDIF
    00156     //
    00157     // For starting points other than the poles:
    00158     //
    00159     // IF sin(lon2-lon1)<0
    00160     //   tc1=acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
    00161     // ELSE
    00162     //   tc1=2*pi-acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
    00163     // ENDIF
    00164
    00165     // if ( cos(start.y()) < SG_EPSILON ) {
    00166     // doing it this way saves a transcendental call
    00167     double sin_start_y = sin( start.y() );
    00168     if ( fabs(1.0-sin_start_y) < SG_EPSILON ) {
    00169         // EPS a small number ~ machine precision
    00170         if ( start.y() > 0 ) {
    00171             *course = SGD_PI;   // starting from N pole
    00172         } else {
    00173             *course = 0;        // starting from S pole
    00174         }
    00175     } else {
    00176         // For starting points other than the poles:
    00177         // double tmp3 = sin(d)*cos_start_y);
    00178         // double tmp4 = sin(dest.y())-sin(start.y())*cos(d);
    00179         // double tmp5 = acos(tmp4/tmp3);
    00180
    00181         // Doing this way gaurentees that the temps are
    00182         // not stored into memory
    00183         double tmp5 = acos( (sin(dest.y()) - sin_start_y * cos(d)) /
    00184                             (sin(d) * cos_start_y) );
    00185
    00186         // if ( sin( dest.x() - start.x() ) < 0 ) {
    00187         // the sin of the negative angle is just the opposite sign
    00188         // of the sin of the angle  so tmp2 will have the opposite
    00189         // sign of sin( dest.x() - start.x() )
    00190         if ( tmp2 >= 0 ) {
    00191             *course = tmp5;
    00192         } else {
    00193             *course = 2 * SGD_PI - tmp5;
    00194         }
    00195     }
    00196 }
    00197
    00198 #if 0
    00199
    00206 inline void calc_gc_course_dist( const Point3D& start, const Point3D& dest,
    00207                                  double *course, double *dist ) {
    00208     // d = 2*asin(sqrt((sin((lat1-lat2)/2))^2 +
    00209     //            cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))
    00210     double tmp1 = sin( (start.y() - dest.y()) / 2 );
    00211     double tmp2 = sin( (start.x() - dest.x()) / 2 );
    00212     double d = 2.0 * asin( sqrt( tmp1 * tmp1 +
    00213                                  cos(start.y()) * cos(dest.y()) * tmp2 * tmp2));
    00214
    00215     // We obtain the initial course, tc1, (at point 1) from point 1 to
    00216     // point 2 by the following. The formula fails if the initial
    00217     // point is a pole. We can special case this with:
    00218     //
    00219     // IF (cos(lat1) < EPS)   // EPS a small number ~ machine precision
    00220     //   IF (lat1 > 0)
    00221     //     tc1= pi        //  starting from N pole
    00222     //   ELSE
    00223     //     tc1= 0         //  starting from S pole
    00224     //   ENDIF
    00225     // ENDIF
    00226     //
    00227     // For starting points other than the poles:
    00228     //
    00229     // IF sin(lon2-lon1)<0
    00230     //   tc1=acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
    00231     // ELSE
    00232     //   tc1=2*pi-acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
    00233     // ENDIF
    00234
    00235     double tc1;
    00236
    00237     if ( cos(start.y()) < SG_EPSILON ) {
    00238         // EPS a small number ~ machine precision
    00239         if ( start.y() > 0 ) {
    00240             tc1 = SGD_PI;        // starting from N pole
    00241         } else {
    00242             tc1 = 0;            // starting from S pole
    00243         }
    00244     }
    00245
    00246     // For starting points other than the poles:
    00247
    00248     double tmp3 = sin(d)*cos(start.y());
    00249     double tmp4 = sin(dest.y())-sin(start.y())*cos(d);
    00250     double tmp5 = acos(tmp4/tmp3);
    00251     if ( sin( dest.x() - start.x() ) < 0 ) {
    00252          tc1 = tmp5;
    00253     } else {
    00254          tc1 = 2 * SGD_PI - tmp5;
    00255     }
    00256
    00257     *course = tc1;
    00258     *dist = d * SG_RAD_TO_NM * SG_NM_TO_METER;
    00259 }
    00260 #endif // 0
    00261
    00262 #endif // _POLAR3D_HXX

@ 1.3 log @Oops, try again. @ text @d224 1 a224 1