MLIB
Loading...
Searching...
No Matches
convert.h
Go to the documentation of this file.
1/*
2 Copyright (c) Mircea Neacsu (2014-2024) Licensed under MIT License.
3 This is part of MLIB project. See LICENSE file for full license terms.
4*/
5
6#pragma once
13#include <math.h>
14#include <numbers>
15#include <string>
16
17#ifndef M_PI
18#define M_PI std::numbers::pi
19#endif
20
21// Useful constants
22
24constexpr double D2R = M_PI / 180.;
25
27constexpr double A_WGS84 = 6378137.000;
28
30constexpr double F_WGS84 = 0.003352810664747;
31
33constexpr double F1_WGS84 = 298.257223563;
34
35#define NM2M 1852.
36#define MPS2KNOT (3600. / NM2M)
37#define USFOOT2M (1200. / 3937.)
38
40constexpr double MAS = M_PI / (180 * 3600000.);
41
42
45constexpr double operator"" _ftUS (long double ftus)
46{
47 return ftus * USFOOT2M;
48}
49
50constexpr double operator"" _ftUS (unsigned long long ftus)
51{
52 return ftus * USFOOT2M;
53}
55
58constexpr double operator"" _nmi (long double nmi)
59{
60 return nmi * NM2M;
61}
62
63constexpr double operator"" _nmi (unsigned long long nmi)
64{
65 return nmi * NM2M;
66}
68
69// ================== Degrees conversion functions ==========================
70
71// Decimal degrees---------------------------------------------------------
72
74constexpr double DEG (double dd)
75{
76 return dd * D2R;
77}
78
80constexpr double D2rad (double dd)
81{
82 return dd * D2R;
83}
84
86constexpr double rad2D (double r)
87{
88 return r / D2R;
89}
90
91// Degrees, minutes --------------------------------------------------------
92
94constexpr double DM (double dd, double mm)
95{
96 return (dd + mm / 60.) * D2R;
97}
98
100constexpr double DM2deg (double ddmm)
101{
102 int sign = (ddmm >= 0) ? 1 : -1;
103 if (ddmm < 0)
104 ddmm = -ddmm;
105 int deg = (int)(ddmm / 100.);
106 ddmm -= (double)deg * 100.;
107 return sign * (deg + ddmm / 60.);
108}
109
111constexpr double deg2DM (double dd)
112{
113 int deg = (int)dd;
114 return (dd - deg) * 60. + deg * 100.;
115}
116
118constexpr double rad2DM (double rad)
119{
120 return deg2DM (rad / D2R);
121}
122
124constexpr double DM2rad (double val)
125{
126 return DM2deg (val) * D2R;
127}
128
129// Degrees, minutes, seconds ------------------------------------------------
130
132constexpr double DMS (double dd, double mm, double ss)
133{
134 return (dd + mm / 60. + ss / 3600.) * D2R;
135}
136
138constexpr double DMS2deg (double dms)
139{
140 int sign = (dms >= 0) ? 1 : -1;
141 if (dms < 0)
142 dms = -dms;
143 int deg = (int)(dms / 10000.);
144 dms -= (double)deg * 10000;
145 int min = (int)(dms / 100.);
146 dms -= (double)min * 100.;
147 return sign * (deg + min / 60. + dms / 3600.);
148}
149
151constexpr double DMS2rad (double dms)
152{
153 return DMS2deg (dms) * D2R;
154}
155
156// =================== Angle units literal operators =========================
157
160constexpr double operator"" _deg (long double deg)
161{
162 return deg * D2R;
163}
164
165constexpr double operator"" _deg (unsigned long long deg)
166{
167 return deg * D2R;
168}
170
173constexpr double operator"" _arcmin (long double min)
174{
175 return min/60. * D2R;
176}
177
178constexpr double operator"" _arcmin (unsigned long long min)
179{
180 return min/60. * D2R;
181}
183
186constexpr double operator ""_dm (long double val)
187{
188 return DM2rad (val);
189}
190
191constexpr double operator ""_dm (unsigned long long val)
192{
193 return DM2rad ((double)val);
194}
196
199constexpr double operator ""_dms (long double val)
200{
201 return DMS2rad (val);
202}
203
204constexpr double operator ""_dms (unsigned long long val)
205{
206 return DMS2rad ((double)val);
207}
209
212constexpr double operator"" _arcsec (long double sec)
213{
214 return sec/3600. * D2R;
215}
216
217constexpr double operator"" _arcsec (unsigned long long sec)
218{
219 return sec/3600. * D2R;
220}
222
226constexpr double operator"" _mas (long double mas)
227{
228 return mas * MAS;
229}
230
231constexpr double operator"" _mas (unsigned long long mas)
232{
233 return mas * MAS;
234}
236
237
238namespace mlib {
240double deg_reduce (double value);
241
244{
247 seconds = 2
249
251std::string degtoa (double degrees, deg_fmt format, bool latitude, int precision);
252
254double atodeg (const std::string& str);
256
257} // namespace mlib
constexpr double rad2DM(double rad)
Convert from radians to degrees, minutes (DDMM.mmm)
Definition convert.h:118
constexpr double DM2rad(double val)
Convert degrees, minutes (DDMM.mmm) to radians.
Definition convert.h:124
constexpr double DMS2rad(double dms)
Convert degrees, minutes, seconds (DDMMSS.sss) to radians.
Definition convert.h:151
constexpr double DMS2deg(double dms)
Convert degrees, minutes, seconds (DDMMSS.sss) to decimal degrees.
Definition convert.h:138
constexpr double F_WGS84
Flattening of WGS84 ellipsoid.
Definition convert.h:30
#define USFOOT2M
US Survey foot to meters conversion factor.
Definition convert.h:37
constexpr double MAS
milli-arcsecond
Definition convert.h:40
double atodeg(const std::string &str)
Conversion from string to decimal degrees.
Definition convert.cpp:116
constexpr double DM(double dd, double mm)
Convert degrees, minutes to radians.
Definition convert.h:94
#define NM2M
Nautical mile to meters conversion factor.
Definition convert.h:35
constexpr double DEG(double dd)
Convert decimal degrees to radians.
Definition convert.h:74
constexpr double F1_WGS84
Inverse of flattening for WGS84 ellipsoid.
Definition convert.h:33
constexpr double D2R
Degrees to radians conversion factor.
Definition convert.h:24
constexpr double D2rad(double dd)
Convert decimal degrees to radians.
Definition convert.h:80
constexpr double deg2DM(double dd)
Convert decimal degrees to degrees, minutes (DDMM.mmm)
Definition convert.h:111
constexpr double rad2D(double r)
Convert radians to decimal degrees.
Definition convert.h:86
constexpr double DM2deg(double ddmm)
Convert degrees, minutes (DDMM.mmm) to decimal degrees.
Definition convert.h:100
std::string degtoa(double degrees, deg_fmt format, bool latitude, int precision)
Conversion from degrees to a string.
Definition convert.cpp:62
deg_fmt
Formatting options for degtoa() function.
Definition convert.h:244
double deg_reduce(double value)
Reduces a degrees value to [0,360) interval.
Definition convert.cpp:39
constexpr double A_WGS84
Semimajor axis of WGS84 ellipsoid.
Definition convert.h:27
constexpr double DMS(double dd, double mm, double ss)
Convert degrees, minutes seconds to radians.
Definition convert.h:132
@ minutes
Degrees, minutes format (DD°MM.mmmm')
Definition convert.h:246
@ degrees
Decimal degrees (DD.dddd°)
Definition convert.h:245
@ seconds
Degrees, minutes, seconds format (DD°MM'SS.sss")
Definition convert.h:247