LocalCartesian.cpp
Go to the documentation of this file.
00001 /**
00002  * \file LocalCartesian.cpp
00003  * \brief Implementation for GeographicLib::LocalCartesian class
00004  *
00005  * Copyright (c) Charles Karney (2008) <charles@karney.com>
00006  * and licensed under the LGPL.
00007  **********************************************************************/
00008 
00009 #include "GeographicLib/LocalCartesian.hpp"
00010 #include "GeographicLib/Constants.hpp"
00011 #include <cmath>
00012 #include <stdexcept>
00013 
00014 namespace {
00015   char RCSID[] = "$Id: LocalCartesian.cpp 6568 2009-03-01 17:58:41Z ckarney $";
00016   char RCSID_H[] = LOCALCARTESIAN_HPP;
00017 }
00018 
00019 namespace GeographicLib {
00020 
00021   using namespace std;
00022 
00023   void LocalCartesian::Reset(double lat0, double lon0, double h0) throw() {
00024     _lat0 = lat0;
00025     _lon0 = lon0 >= 180 ? lon0 - 360 : lon0 < -180 ? lon0 + 360 : lon0;
00026     _h0 = h0;
00027     _earth.Forward(_lat0, _lon0, _h0, _x0, _y0, _z0);
00028     double
00029       phi = lat0 * Constants::degree(),
00030       sphi = sin(phi),
00031       cphi = cos(phi),
00032       lam = lon0 * Constants::degree(),
00033       slam = sin(lam),
00034       clam = cos(lam);
00035     // Local x axis in geocentric coords
00036     _rxx = -slam; _rxy = clam; _rxz = 0;
00037     // Local y axis in geocentric coords
00038     _ryx = -clam * sphi; _ryy = -slam * sphi; _ryz = cphi;
00039     // Local z axis in geocentric coords
00040     _rzx = clam * cphi; _rzy = slam * cphi; _rzz = sphi;
00041   }
00042 
00043   void LocalCartesian::Forward(double lat, double lon, double h,
00044                                double& x, double& y, double& z) const throw() {
00045     double xc, yc, zc;
00046     _earth.Forward(lat, lon, h, xc, yc, zc);
00047     xc -= _x0; yc -= _y0; zc -= _z0;
00048     x = _rxx * xc + _rxy * yc + _rxz * zc;
00049     y = _ryx * xc + _ryy * yc + _ryz * zc;
00050     z = _rzx * xc + _rzy * yc + _rzz * zc;
00051   }
00052 
00053   void LocalCartesian::Reverse(double x, double y, double z,
00054                                double& lat, double& lon, double& h)
00055     const throw() {
00056     double
00057       xc = _x0 + _rxx * x + _ryx * y + _rzx * z,
00058       yc = _y0 + _rxy * x + _ryy * y + _rzy * z,
00059       zc = _z0 + _rxz * x + _ryz * y + _rzz * z;
00060     _earth.Reverse(xc, yc, zc, lat, lon, h);
00061   }
00062 
00063 } // namespace GeographicLib