Skip to content

Commit 4ed05f4

Browse files
committed
Move deg2rad and rad2deg into MathUtil class
1 parent 9f9f95c commit 4ed05f4

7 files changed

Lines changed: 61 additions & 61 deletions

File tree

docs/api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,5 +313,5 @@ Low-level math helpers used internally. Available for direct use if needed.
313313
| `MathUtil::EARTH_RADIUS` | `double` | Mean Earth radius: `6371009.0` m (IUGG) |
314314
| `MathUtil::clamp(x, low, high)` | `double` | Clamps `x` to `[low, high]` |
315315
| `MathUtil::wrap(n, min, max)` | `double` | Wraps `n` into `[min, max)` |
316-
| `deg2rad(degrees)` | `double` | Converts degrees to radians |
317-
| `rad2deg(radians)` | `double` | Converts radians to degrees |
316+
| `MathUtil::deg2rad(degrees)` | `double` | Converts degrees to radians |
317+
| `MathUtil::rad2deg(radians)` | `double` | Converts radians to degrees |

include/CppGeometryLibrary/MathUtil.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,6 @@
2121
#define M_PI 3.14159265358979323846
2222
#endif
2323

24-
inline double deg2rad(double degrees) {
25-
return degrees * M_PI / 180.0;
26-
}
27-
28-
inline double rad2deg(double angle) {
29-
return angle * 180.0 / M_PI;
30-
}
31-
3224
class MathUtil {
3325
public:
3426
/**
@@ -37,6 +29,14 @@ class MathUtil {
3729
*/
3830
static constexpr double EARTH_RADIUS = 6371009.0;
3931

32+
static inline double deg2rad(double degrees) {
33+
return degrees * M_PI / 180.0;
34+
}
35+
36+
static inline double rad2deg(double angle) {
37+
return angle * 180.0 / M_PI;
38+
}
39+
4040
/**
4141
* Restrict x to the range [low, high].
4242
*/

include/CppGeometryLibrary/PolyUtil.hpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ class PolyUtil {
3737
if (size == 0) {
3838
return false;
3939
}
40-
double lat3 = deg2rad(point.lat);
41-
double lng3 = deg2rad(point.lng);
40+
double lat3 = MathUtil::deg2rad(point.lat);
41+
double lng3 = MathUtil::deg2rad(point.lng);
4242
LatLng prev = polygon[size - 1];
43-
double lat1 = deg2rad(prev.lat);
44-
double lng1 = deg2rad(prev.lng);
43+
double lat1 = MathUtil::deg2rad(prev.lat);
44+
double lng1 = MathUtil::deg2rad(prev.lng);
4545

4646
size_t nIntersect = 0;
4747

@@ -52,8 +52,8 @@ class PolyUtil {
5252
return true;
5353
}
5454

55-
double lat2 = deg2rad(val.lat);
56-
double lng2 = deg2rad(val.lng);
55+
double lat2 = MathUtil::deg2rad(val.lat);
56+
double lng2 = MathUtil::deg2rad(val.lng);
5757

5858
// Offset longitudes by -lng1.
5959
if (PolyUtil::intersects(lat1, lat2, MathUtil::wrap(lng2 - lng1, -M_PI, M_PI), lat3, dLng3, geodesic)) {
@@ -115,16 +115,16 @@ class PolyUtil {
115115

116116
double tolerance = toleranceEarth / MathUtil::EARTH_RADIUS;
117117
double havTolerance = MathUtil::hav(tolerance);
118-
double lat3 = deg2rad(point.lat);
119-
double lng3 = deg2rad(point.lng);
118+
double lat3 = MathUtil::deg2rad(point.lat);
119+
double lng3 = MathUtil::deg2rad(point.lng);
120120
LatLng prev = poly[closed ? size - 1 : 0];
121-
double lat1 = deg2rad(prev.lat);
122-
double lng1 = deg2rad(prev.lng);
121+
double lat1 = MathUtil::deg2rad(prev.lat);
122+
double lng1 = MathUtil::deg2rad(prev.lng);
123123

124124
if (geodesic) {
125125
for (auto val : poly) {
126-
double lat2 = deg2rad(val.lat);
127-
double lng2 = deg2rad(val.lng);
126+
double lat2 = MathUtil::deg2rad(val.lat);
127+
double lng2 = MathUtil::deg2rad(val.lng);
128128
if (PolyUtil::isOnSegmentGC(lat1, lng1, lat2, lng2, lat3, lng3, havTolerance)) {
129129
return true;
130130
}
@@ -143,9 +143,9 @@ class PolyUtil {
143143
double y3 = MathUtil::mercator(lat3);
144144
double xTry[3];
145145
for (auto val : poly) {
146-
double lat2 = deg2rad(val.lat);
146+
double lat2 = MathUtil::deg2rad(val.lat);
147147
double y2 = MathUtil::mercator(lat2);
148-
double lng2 = deg2rad(val.lng);
148+
double lng2 = MathUtil::deg2rad(val.lng);
149149
if (std::max(lat1, lat2) >= minAcceptable && std::min(lat1, lat2) <= maxAcceptable) {
150150
// We offset longitudes by -lng1; the implicit x1 is 0.
151151
double x2 = MathUtil::wrap(lng2 - lng1, -M_PI, M_PI);
@@ -188,12 +188,12 @@ class PolyUtil {
188188
if (start == end) {
189189
return SphericalUtil::computeDistanceBetween(end, p);
190190
}
191-
double s0lat = deg2rad(p.lat);
192-
double s0lng = deg2rad(p.lng);
193-
double s1lat = deg2rad(start.lat);
194-
double s1lng = deg2rad(start.lng);
195-
double s2lat = deg2rad(end.lat);
196-
double s2lng = deg2rad(end.lng);
191+
double s0lat = MathUtil::deg2rad(p.lat);
192+
double s0lng = MathUtil::deg2rad(p.lng);
193+
double s1lat = MathUtil::deg2rad(start.lat);
194+
double s1lng = MathUtil::deg2rad(start.lng);
195+
double s2lat = MathUtil::deg2rad(end.lat);
196+
double s2lng = MathUtil::deg2rad(end.lng);
197197
double s2s1lat = s2lat - s1lat;
198198
double s2s1lng = s2lng - s1lng;
199199
double u = ((s0lat - s1lat) * s2s1lat + (s0lng - s1lng) * s2s1lng)

include/CppGeometryLibrary/SphericalUtil.hpp

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ class SphericalUtil {
2929
*/
3030
inline static double computeHeading(const LatLng& from, const LatLng& to) {
3131
// http://williams.best.vwh.net/avform.htm#Crs
32-
double fromLat = deg2rad(from.lat);
33-
double fromLng = deg2rad(from.lng);
34-
double toLat = deg2rad(to.lat);
35-
double toLng = deg2rad(to.lng);
32+
double fromLat = MathUtil::deg2rad(from.lat);
33+
double fromLng = MathUtil::deg2rad(from.lng);
34+
double toLat = MathUtil::deg2rad(to.lat);
35+
double toLng = MathUtil::deg2rad(to.lng);
3636
double dLng = toLng - fromLng;
3737
double heading = atan2(
3838
sin(dLng) * cos(toLat),
3939
cos(fromLat) * sin(toLat) - sin(fromLat) * cos(toLat) * cos(dLng));
4040

41-
return MathUtil::wrap(rad2deg(heading), -180, 180);
41+
return MathUtil::wrap(MathUtil::rad2deg(heading), -180, 180);
4242
}
4343

4444

@@ -52,10 +52,10 @@ class SphericalUtil {
5252
*/
5353
inline static LatLng computeOffset(const LatLng& from, double distance, double heading) {
5454
distance /= MathUtil::EARTH_RADIUS;
55-
heading = deg2rad(heading);
55+
heading = MathUtil::deg2rad(heading);
5656
// http://williams.best.vwh.net/avform.htm#LL
57-
double fromLat = deg2rad(from.lat);
58-
double fromLng = deg2rad(from.lng);
57+
double fromLat = MathUtil::deg2rad(from.lat);
58+
double fromLng = MathUtil::deg2rad(from.lng);
5959
double cosDistance = cos(distance);
6060
double sinDistance = sin(distance);
6161
double sinFromLat = sin(fromLat);
@@ -65,7 +65,7 @@ class SphericalUtil {
6565
sinDistance * cosFromLat * sin(heading),
6666
cosDistance - sinFromLat * sinLat);
6767

68-
return LatLng(rad2deg(asin(MathUtil::clamp(sinLat, -1.0, 1.0))), rad2deg(fromLng + dLng));
68+
return LatLng(MathUtil::rad2deg(asin(MathUtil::clamp(sinLat, -1.0, 1.0))), MathUtil::rad2deg(fromLng + dLng));
6969
}
7070

7171

@@ -81,13 +81,13 @@ class SphericalUtil {
8181
* @param heading The heading in degrees clockwise from north.
8282
*/
8383
inline static std::optional<LatLng> computeOffsetOrigin(const LatLng& to, double distance, double heading) {
84-
heading = deg2rad(heading);
84+
heading = MathUtil::deg2rad(heading);
8585
distance /= MathUtil::EARTH_RADIUS;
8686
// http://lists.maptools.org/pipermail/proj/2008-October/003939.html
8787
double n1 = cos(distance);
8888
double n2 = sin(distance) * cos(heading);
8989
double n3 = sin(distance) * sin(heading);
90-
double n4 = sin(deg2rad(to.lat));
90+
double n4 = sin(MathUtil::deg2rad(to.lat));
9191
// Rewrite n4 = n1*sin(φ) + n2*cos(φ) as r*sin(φ + α) = n4,
9292
// where r = sqrt(n1²+n2²), α = atan2(n2, n1).
9393
// Solving via asin avoids dividing by n1, which causes catastrophic
@@ -103,8 +103,8 @@ class SphericalUtil {
103103
fromLatRadians = M_PI - asin(sinArg) - alpha;
104104
}
105105
if (fromLatRadians < -M_PI / 2 || fromLatRadians > M_PI / 2) return std::nullopt;
106-
double fromLngRadians = deg2rad(to.lng) - atan2(n3, n1 * cos(fromLatRadians) - n2 * sin(fromLatRadians));
107-
return LatLng(rad2deg(fromLatRadians), rad2deg(fromLngRadians));
106+
double fromLngRadians = MathUtil::deg2rad(to.lng) - atan2(n3, n1 * cos(fromLatRadians) - n2 * sin(fromLatRadians));
107+
return LatLng(MathUtil::rad2deg(fromLatRadians), MathUtil::rad2deg(fromLngRadians));
108108
}
109109

110110

@@ -120,10 +120,10 @@ class SphericalUtil {
120120
*/
121121
inline static LatLng interpolate(const LatLng& from, const LatLng& to, double fraction) {
122122
// http://en.wikipedia.org/wiki/Slerp
123-
double fromLat = deg2rad(from.lat);
124-
double fromLng = deg2rad(from.lng);
125-
double toLat = deg2rad(to.lat);
126-
double toLng = deg2rad(to.lng);
123+
double fromLat = MathUtil::deg2rad(from.lat);
124+
double fromLng = MathUtil::deg2rad(from.lng);
125+
double toLat = MathUtil::deg2rad(to.lat);
126+
double toLng = MathUtil::deg2rad(to.lng);
127127
double cosFromLat = cos(fromLat);
128128
double cosToLat = cos(toLat);
129129
// Computes Spherical interpolation coefficients.
@@ -143,15 +143,15 @@ class SphericalUtil {
143143
// Converts interpolated vector back to polar.
144144
double lat = atan2(z, sqrt(x * x + y * y));
145145
double lng = atan2(y, x);
146-
return LatLng(rad2deg(lat), rad2deg(lng));
146+
return LatLng(MathUtil::rad2deg(lat), MathUtil::rad2deg(lng));
147147
}
148148

149149
/**
150150
* Returns the angle between two LatLngs, in radians. This is the same as the distance
151151
* on the unit sphere.
152152
*/
153153
inline static double computeAngleBetween(const LatLng& from, const LatLng& to) {
154-
return SphericalUtil::distanceRadians(deg2rad(from.lat), deg2rad(from.lng), deg2rad(to.lat), deg2rad(to.lng));
154+
return SphericalUtil::distanceRadians(MathUtil::deg2rad(from.lat), MathUtil::deg2rad(from.lng), MathUtil::deg2rad(to.lat), MathUtil::deg2rad(to.lng));
155155
}
156156

157157
/**
@@ -171,11 +171,11 @@ class SphericalUtil {
171171
}
172172
double length = 0;
173173
LatLng prev = path[0];
174-
double prevLat = deg2rad(prev.lat);
175-
double prevLng = deg2rad(prev.lng);
174+
double prevLat = MathUtil::deg2rad(prev.lat);
175+
double prevLng = MathUtil::deg2rad(prev.lng);
176176
for (auto point : path) {
177-
double lat = deg2rad(point.lat);
178-
double lng = deg2rad(point.lng);
177+
double lat = MathUtil::deg2rad(point.lat);
178+
double lng = MathUtil::deg2rad(point.lng);
179179
length += SphericalUtil::distanceRadians(prevLat, prevLng, lat, lng);
180180
prevLat = lat;
181181
prevLng = lng;
@@ -227,13 +227,13 @@ class SphericalUtil {
227227
if (size < 3U) { return 0; }
228228
double total = 0;
229229
LatLng prev = path[size - 1];
230-
double prevTanLat = tan((M_PI / 2 - deg2rad(prev.lat)) / 2);
231-
double prevLng = deg2rad(prev.lng);
230+
double prevTanLat = tan((M_PI / 2 - MathUtil::deg2rad(prev.lat)) / 2);
231+
double prevLng = MathUtil::deg2rad(prev.lng);
232232
// For each edge, accumulate the signed area of the triangle formed by the North Pole
233233
// and that edge ("polar triangle").
234234
for (auto point : path) {
235-
double tanLat = tan((M_PI / 2 - deg2rad(point.lat)) / 2);
236-
double lng = deg2rad(point.lng);
235+
double tanLat = tan((M_PI / 2 - MathUtil::deg2rad(point.lat)) / 2);
236+
double lng = MathUtil::deg2rad(point.lng);
237237
total += SphericalUtil::polarTriangleArea(tanLat, lng, prevTanLat, prevLng);
238238
prevTanLat = tanLat;
239239
prevLng = lng;

tests/MathUtil/mod.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ TEST(MathUtil, mercator_inverseMercator) {
4343
EXPECT_NEAR(MathUtil::inverseMercator(0.0), 0.0, 1e-10);
4444

4545
// Round-trip: inverseMercator(mercator(lat)) == lat
46-
EXPECT_NEAR(MathUtil::inverseMercator(MathUtil::mercator(deg2rad( 45.0))), deg2rad( 45.0), 1e-10);
47-
EXPECT_NEAR(MathUtil::inverseMercator(MathUtil::mercator(deg2rad(-30.0))), deg2rad(-30.0), 1e-10);
46+
EXPECT_NEAR(MathUtil::inverseMercator(MathUtil::mercator(MathUtil::deg2rad( 45.0))), MathUtil::deg2rad( 45.0), 1e-10);
47+
EXPECT_NEAR(MathUtil::inverseMercator(MathUtil::mercator(MathUtil::deg2rad(-30.0))), MathUtil::deg2rad(-30.0), 1e-10);
4848
}
4949

5050
TEST(MathUtil, hav_arcHav) {

tests/SphericalUtil/computeLength.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ TEST(SphericalUtil, computeLength) {
1515

1616
// List with two points
1717
latLngs.push_back(LatLng(0.1, 0.1));
18-
EXPECT_NEAR(SphericalUtil::computeLength(latLngs), deg2rad(0.1) * sqrt(2) * MathUtil::EARTH_RADIUS, 1);
18+
EXPECT_NEAR(SphericalUtil::computeLength(latLngs), MathUtil::deg2rad(0.1) * sqrt(2) * MathUtil::EARTH_RADIUS, 1);
1919

2020
// List with three points
2121
std::vector<LatLng> latLngs2 = { {0, 0}, {90, 0}, {0, 90} };

tests/TestHelpers.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#define EXPECT_NEAR_LatLng(expected, actual) \
1010
do { \
1111
EXPECT_NEAR((expected).lat, (actual).lat, 1e-6); \
12-
const double _cosLat = cos(deg2rad((expected).lat)); \
12+
const double _cosLat = cos(MathUtil::deg2rad((expected).lat)); \
1313
const double _eLng = MathUtil::wrap((expected).lng, -180.0, 180.0); \
1414
const double _aLng = MathUtil::wrap((actual).lng, -180.0, 180.0); \
1515
EXPECT_NEAR(_cosLat * _eLng, _cosLat * _aLng, 1e-6); \

0 commit comments

Comments
 (0)