boat
C++23 geospatial library
Loading...
Searching...
No Matches
algorithm.hpp
Go to the documentation of this file.
1// Andrew Naplavkov
2
3#ifndef BOAT_GEOMETRY_ALGORITHM_HPP
4#define BOAT_GEOMETRY_ALGORITHM_HPP
5
6#include <boat/detail/numbers.hpp>
8#include <boost/geometry.hpp>
9
10namespace boat::geometry {
11
12template <box T>
13multi_point auto
14box_interpolate(T const& mbr, size_t edge_points, size_t area_points)
15{
16 auto ret = typename d2_t<T>::multi_point{};
17 for (auto edge : boost::geometry::box_view{mbr} | std::views::pairwise) {
18 auto a = std::get<0>(edge), b = std::get<1>(edge);
19 for (auto i : std::views::iota(0uz, edge_points)) {
20 auto t = double(i) / edge_points;
21 ret.emplace_back(
22 std::lerp(a.x(), b.x(), t), std::lerp(a.y(), b.y(), t));
23 }
24 }
25 auto a = mbr.min_corner(), b = mbr.max_corner();
26 for (auto i : std::views::iota(0uz, area_points))
27 ret.emplace_back(
28 std::lerp(a.x(), b.x(), frac(i * numbers::inv_phi)),
29 std::lerp(a.y(), b.y(), (i + .5) / area_points));
30 return ret;
31}
32
33inline auto buffer(double distance, size_t num_points)
34{
35 return [=]<single T>(T const& geom) -> polygon auto {
36 namespace sb = boost::geometry::strategy::buffer;
37 using point_circle = std::conditional_t<
38 std::same_as<
39 boost::geometry::cs_tag_t<T>,
40 boost::geometry::geographic_tag>,
41 sb::geographic_point_circle<>,
42 sb::point_circle>;
43 auto out = typename d2_t<T>::multi_polygon{};
44 boost::geometry::buffer(
45 geom,
46 out,
47 sb::distance_symmetric{distance},
48 sb::side_straight{},
49 sb::join_round{num_points},
50 sb::end_round{num_points},
51 point_circle{num_points});
52 return std::move(out.at(0));
53 };
54}
55
57constexpr auto meter = overloaded{
58 [] { return numbers::radian / numbers::earth::mean_radius; },
59 [](this auto&& self, double lat) {
60 auto den = std::cos(lat * numbers::degree);
61 return den ? self() / den : 0.;
62 }};
63
64auto visitor(auto... fn)
65{
66 return overloaded{
67 fn...,
68 [](this auto&& self, multi auto&& v) -> void {
69 std::ranges::for_each(v, self);
70 },
71 [](this auto&& self, dynamic auto&& v) -> void {
72 std::visit(self, v);
73 }};
74}
75
76void for_each_point(tagged auto const& geom, auto fn)
77{
78 visitor([&](single auto&& v) { boost::geometry::for_each_point(v, fn); })(
79 geom);
80}
81
84{
85 auto [y, reflected] = reflect(p.y(), -90., 90.);
86 return {boat::wrap(p.x() + 180 * reflected, -180., 180.), y};
87}
88
90add_meters(geographic::point const& p, double eastward, double northward)
91{
93 p.x() + eastward * meter(p.y()), p.y() + northward * meter()});
94}
95
96} // namespace boat::geometry
97
98#endif // BOAT_GEOMETRY_ALGORITHM_HPP
Definition vocabulary.hpp:34
Definition vocabulary.hpp:43
Definition vocabulary.hpp:40
Definition vocabulary.hpp:49
Definition vocabulary.hpp:58
Definition vocabulary.hpp:28
Definition algorithm.hpp:10
void for_each_point(tagged auto const &geom, auto fn)
Definition algorithm.hpp:76
geographic::point wrap(geographic::point const &p)
Wraps at the poles and antimeridian.
Definition algorithm.hpp:83
multi_point auto box_interpolate(T const &mbr, size_t edge_points, size_t area_points)
Definition algorithm.hpp:14
geographic::point add_meters(geographic::point const &p, double eastward, double northward)
Definition algorithm.hpp:90
auto buffer(double distance, size_t num_points)
Definition algorithm.hpp:33
auto visitor(auto... fn)
Definition algorithm.hpp:64
constexpr auto meter
Approximate degrees per meter of latitude, or longitude at a given latitude.
Definition algorithm.hpp:57
model::d2::point_xy< double, cs::geographic< boost::geometry::degree > > point
Definition vocabulary.hpp:65