gctl/lib/geometry/refellipsoid.cpp
2025-03-05 15:42:47 +08:00

98 lines
3.9 KiB
C++

/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2023 Yi Zhang (yizhang-geo@zju.edu.cn)
*
* GCTL is distributed under a dual licensing scheme. You can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 2
* of the License, or (at your option) any later version. You should have
* received a copy of the GNU Lesser General Public License along with this
* program. If not, see <http://www.gnu.org/licenses/>.
*
* If the terms and conditions of the LGPL v.2. would prevent you from using
* the GCTL, please consider the option to obtain a commercial license for a
* fee. These licenses are offered by the GCTL's original author. As a rule,
* licenses are provided "as-is", unlimited in time for a one time fee. Please
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
* to include some description of your company and the realm of its activities.
* Also add information on how to contact you by electronic and paper mail.
******************************************************/
#include "refellipsoid.h"
gctl::refellipsoid::refellipsoid(/* args */){}
gctl::refellipsoid::refellipsoid(refellipsoid_type_e refellipsoid)
{
set(refellipsoid);
}
gctl::refellipsoid::refellipsoid(double R, double r, bool flat)
{
set(R, r, flat);
}
gctl::refellipsoid::~refellipsoid(){}
void gctl::refellipsoid::set(refellipsoid_type_e refellipsoid)
{
if (refellipsoid == Earth) {r_ = R_ = GCTL_Earth_Radius;}
else if (refellipsoid == MagEarth) {r_ = R_ = GCTL_Earth_RefRadius;}
else if (refellipsoid == Moon) {r_ = R_ = GCTL_Moon_Radius;}
else if (refellipsoid == Mars) {r_ = R_ = GCTL_Mars_Radius;}
else if (refellipsoid == WGS84) {r_ = GCTL_WGS84_PoleRadius; R_ = GCTL_WGS84_EquatorRadius;}
else if (refellipsoid == Ardalan2010) {r_ = GCTL_Mars_Ardalan2010_PoleRadius; R_ = GCTL_Mars_Ardalan2010_EquatorRadius;}
else throw std::invalid_argument("Invalid reference system type for gctl::refellipsoid::set(...)");
eps_ = sqrt(1.0 - (r_*r_)/(R_*R_));
epssq_ = eps_*eps_;
return;
}
void gctl::refellipsoid::set(double R, double r_or_flat, bool is_flat)
{
R_ = R;
if (is_flat) r_ = R_*(1.0 - 1.0/r_or_flat);
else r_ = r_or_flat;
eps_ = sqrt(1.0 - (r_*r_)/(R_*R_));
epssq_ = eps_*eps_;
return;
}
double gctl::refellipsoid::radius_at(double lati)
{
return ellipse_radius_2d(R_, r_, lati*M_PI/180.0, 0.0);
}
void gctl::refellipsoid::geodetic2spherical(double geodetic_lati,
double geodetic_hei, double& sph_lati, double& sph_rad)
{
double CosLat, SinLat, rc, xp, zp;
/*
** Convert geodetic coordinates, (defined by the WGS-84
** reference ellipsoid), to Earth Centered Earth Fixed Cartesian
** coordinates, and then to spherical coordinates.
*/
CosLat = cosd(geodetic_lati);
SinLat = sind(geodetic_lati);
// compute the local rRdius of curvature on the WGS-84 reference ellipsoid
rc = R_ / sqrt(1.0 - epssq_ * SinLat * SinLat);
// compute ECEF Cartesian coordinates of specified point (for longitude=0)
xp = (rc + geodetic_hei) * CosLat;
zp = (rc*(1.0 - epssq_) + geodetic_hei) * SinLat;
// compute spherical rRdius and angle lambda and phi of specified point
sph_rad = sqrt(xp * xp + zp * zp);
sph_lati = deg(asin(zp/sph_rad));
return;
}