00001 /* 00002 Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without 00006 modification, are permitted provided that the following conditions are met: 00007 * Redistributions of source code must retain the above copyright 00008 notice, this list of conditions and the following disclaimer. 00009 * Redistributions in binary form must reproduce the above copyright 00010 notice, this list of conditions and the following disclaimer in the 00011 documentation and/or other materials provided with the distribution. 00012 * Neither the name of the Universite de Sherbrooke nor the 00013 names of its contributors may be used to endorse or promote products 00014 derived from this software without specific prior written permission. 00015 00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00017 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00018 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY 00020 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00021 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00022 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00023 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00024 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00025 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 */ 00027 00028 /* 00029 * The methods in this file were modified from the originals of the MRPT toolkit (see notice below): 00030 * https://github.com/MRPT/mrpt/blob/master/libs/topography/src/conversions.cpp 00031 */ 00032 00033 /* +---------------------------------------------------------------------------+ 00034 | Mobile Robot Programming Toolkit (MRPT) | 00035 | http://www.mrpt.org/ | 00036 | | 00037 | Copyright (c) 2005-2016, Individual contributors, see AUTHORS file | 00038 | See: http://www.mrpt.org/Authors - All rights reserved. | 00039 | Released under BSD License. See details in http://www.mrpt.org/License | 00040 +---------------------------------------------------------------------------+ */ 00041 00042 00043 #include "rtabmap/core/GeodeticCoords.h" 00044 00045 #include <cmath> 00046 #ifndef M_PI 00047 #define M_PI 3.14159265358979323846 00048 #endif 00049 00050 namespace rtabmap { 00051 00052 00053 inline double DEG2RAD(const double x) { return x*M_PI/180.0;} 00054 inline double square(const double & value) {return value*value;} 00055 00056 //*--------------------------------------------------------------- 00057 // geodeticToGeocentric_WGS84 00058 // ---------------------------------------------------------------*/ 00059 cv::Point3d GeodeticCoords::toGeocentric_WGS84() const 00060 { 00061 // -------------------------------------------------------------------- 00062 // See: http://en.wikipedia.org/wiki/Reference_ellipsoid 00063 // Constants are for WGS84 00064 // -------------------------------------------------------------------- 00065 00066 static const double a = 6378137; // Semi-major axis of the Earth (meters) 00067 static const double b = 6356752.3142; // Semi-minor axis: 00068 00069 static const double ae = acos(b/a); // eccentricity: 00070 static const double cos2_ae_earth = square(cos(ae)); // The cos^2 of the angular eccentricity of the Earth: // 0.993305619995739L; 00071 static const double sin2_ae_earth = square(sin(ae)); // The sin^2 of the angular eccentricity of the Earth: // 0.006694380004261L; 00072 00073 const double lon = DEG2RAD( double(this->longitude()) ); 00074 const double lat = DEG2RAD( double(this->latitude()) ); 00075 00076 // The radius of curvature in the prime vertical: 00077 const double N = a / std::sqrt( 1.0 - sin2_ae_earth*square( sin(lat) ) ); 00078 00079 // Generate 3D point: 00080 cv::Point3d out; 00081 out.x = (N+this->altitude())*cos(lat)*cos(lon); 00082 out.y = (N+this->altitude())*cos(lat)*sin(lon); 00083 out.z = (cos2_ae_earth*N+this->altitude())*sin(lat); 00084 00085 return out; 00086 } 00087 00088 00089 /*--------------------------------------------------------------- 00090 geodeticToENU_WGS84 00091 ---------------------------------------------------------------*/ 00092 cv::Point3d GeodeticCoords::toENU_WGS84(const GeodeticCoords &origin) const 00093 { 00094 // -------------------------------------------------------------------- 00095 // Explanation: We compute the earth-centric coordinates first, 00096 // then make a system transformation to local XYZ coordinates 00097 // using a system of three orthogonal vectors as local reference. 00098 // 00099 // See: http://en.wikipedia.org/wiki/Reference_ellipsoid 00100 // (JLBC 21/DEC/2006) (Fixed: JLBC 9/JUL/2008) 00101 // - Oct/2013, Emilio Sanjurjo: Fixed UP vector pointing exactly normal to ellipsoid surface. 00102 // -------------------------------------------------------------------- 00103 // Generate 3D point: 00104 cv::Point3d P_geocentric = this->toGeocentric_WGS84(); 00105 00106 // Generate reference 3D point: 00107 cv::Point3d P_geocentric_ref = origin.toGeocentric_WGS84(); 00108 00109 const double clat = cos(DEG2RAD(origin.latitude())), slat = sin(DEG2RAD(origin.latitude())); 00110 const double clon = cos(DEG2RAD(origin.longitude())), slon = sin(DEG2RAD(origin.longitude())); 00111 00112 // Compute the resulting relative coordinates: 00113 // For using smaller numbers: 00114 P_geocentric -= P_geocentric_ref; 00115 00116 // Optimized calculation: Local transformed coordinates of P_geo(x,y,z) 00117 // after rotation given by the transposed rotation matrix from ENU -> ECEF. 00118 cv::Point3d out; 00119 out.x = -slon*P_geocentric.x + clon*P_geocentric.y; 00120 out.y = -clon*slat*P_geocentric.x -slon*slat*P_geocentric.y + clat*P_geocentric.z; 00121 out.z = clon*clat*P_geocentric.x + slon*clat*P_geocentric.y +slat*P_geocentric.z; 00122 00123 return out; 00124 } 00125 00126 GeodeticCoords::GeodeticCoords() : 00127 latitude_(0.0), 00128 longitude_(0.0), 00129 altitude_(0.0) 00130 { 00131 00132 } 00133 GeodeticCoords::GeodeticCoords(double latitude, double longitude, double altitude) : 00134 latitude_(latitude), 00135 longitude_(longitude), 00136 altitude_(altitude) 00137 { 00138 00139 } 00140 00141 }