Skip to content

Commit 1215062

Browse files
committed
Port the used bits from CheapRuler, drop dependencies
1 parent 152a52b commit 1215062

36 files changed

+90
-4086
lines changed

CMakeLists.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,6 @@ include_directories(SYSTEM ${RAPIDJSON_INCLUDE_DIR})
432432
set(MICROTAR_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third_party/microtar/src")
433433
include_directories(SYSTEM ${MICROTAR_INCLUDE_DIR})
434434

435-
set(MBXGEOM_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third_party/geometry.hpp-0.9.2/include")
436-
include_directories(SYSTEM ${MBXGEOM_INCLUDE_DIR})
437-
set(CHEAPRULER_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third_party/cheap-ruler-cpp-2778eb8/include")
438-
include_directories(SYSTEM ${CHEAPRULER_INCLUDE_DIR})
439-
440435
add_library(MICROTAR OBJECT "${CMAKE_CURRENT_SOURCE_DIR}/third_party/microtar/src/microtar.c")
441436
set_property(TARGET MICROTAR PROPERTY POSITION_INDEPENDENT_CODE ON)
442437
target_no_warning(MICROTAR unused-variable)

include/util/cheap_ruler.hpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#pragma once
2+
3+
#include <cassert>
4+
#include <cmath>
5+
#include <cstdint>
6+
#include <limits>
7+
#include <tuple>
8+
#include <utility>
9+
10+
namespace mapbox {
11+
12+
namespace geometry {
13+
template <typename T>
14+
struct point
15+
{
16+
using coordinate_type = T;
17+
18+
constexpr point()
19+
: x(), y()
20+
{}
21+
constexpr point(T x_, T y_)
22+
: x(x_), y(y_)
23+
{}
24+
25+
T x;
26+
T y;
27+
};
28+
}
29+
30+
namespace cheap_ruler {
31+
32+
using point = geometry::point<double>;
33+
34+
class CheapRuler {
35+
36+
// Values that define WGS84 ellipsoid model of the Earth
37+
static constexpr double RE = 6378.137; // equatorial radius
38+
static constexpr double FE = 1.0 / 298.257223563; // flattening
39+
40+
static constexpr double E2 = FE * (2 - FE);
41+
static constexpr double RAD = M_PI / 180.0;
42+
43+
public:
44+
explicit CheapRuler(double latitude) {
45+
// Curvature formulas from https://en.wikipedia.org/wiki/Earth_radius#Meridional
46+
double mul = RAD * RE * 1000;
47+
double coslat = std::cos(latitude * RAD);
48+
double w2 = 1 / (1 - E2 * (1 - coslat * coslat));
49+
double w = std::sqrt(w2);
50+
51+
// multipliers for converting longitude and latitude degrees into distance
52+
kx = mul * w * coslat; // based on normal radius of curvature
53+
ky = mul * w * w2 * (1 - E2); // based on meridonal radius of curvature
54+
}
55+
56+
double squareDistance(point a, point b) const {
57+
auto dx = longDiff(a.x, b.x) * kx;
58+
auto dy = (a.y - b.y) * ky;
59+
return dx * dx + dy * dy;
60+
}
61+
62+
//
63+
// Given two points of the form [x = longitude, y = latitude], returns the distance.
64+
//
65+
double distance(point a, point b) const {
66+
return std::sqrt(squareDistance(a, b));
67+
}
68+
69+
//
70+
// Returns the bearing between two points in angles.
71+
//
72+
double bearing(point a, point b) const {
73+
auto dx = longDiff(b.x, a.x) * kx;
74+
auto dy = (b.y - a.y) * ky;
75+
76+
return std::atan2(dx, dy) / RAD;
77+
}
78+
79+
private:
80+
double ky;
81+
double kx;
82+
static double longDiff(double a, double b) {
83+
return std::remainder(a - b, 360);
84+
}
85+
};
86+
87+
} // namespace cheap_ruler
88+
} // namespace mapbox

src/util/coordinate_calculation.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#include "util/coordinate_calculation.hpp"
2+
#include "util/cheap_ruler.hpp"
23
#include "util/coordinate.hpp"
34
#include "util/trigonometry_table.hpp"
45
#include "util/web_mercator.hpp"
56

67
#include <boost/assert.hpp>
78

8-
#include <mapbox/cheap_ruler.hpp>
9-
109
#include <algorithm>
1110
#include <iterator>
1211
#include <limits>
@@ -32,7 +31,7 @@ class CheapRulerContainer
3231
for (int n = 0; n < number_of_rulers; n++)
3332
{
3433
cheap_ruler_cache[n] = mapbox::cheap_ruler::CheapRuler(
35-
step * (n + 0.5) / COORDINATE_PRECISION, mapbox::cheap_ruler::CheapRuler::Meters);
34+
step * (n + 0.5) / COORDINATE_PRECISION);
3635
}
3736
};
3837

third_party/cheap-ruler-cpp-2778eb8/.clang-format

Lines changed: 0 additions & 18 deletions
This file was deleted.

third_party/cheap-ruler-cpp-2778eb8/.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

third_party/cheap-ruler-cpp-2778eb8/.travis.yml

Lines changed: 0 additions & 25 deletions
This file was deleted.

third_party/cheap-ruler-cpp-2778eb8/CMakeLists.txt

Lines changed: 0 additions & 27 deletions
This file was deleted.

third_party/cheap-ruler-cpp-2778eb8/LICENSE

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)