Skip to content

Commit 60e7aba

Browse files
committed
Make box_t type available to boost as box type
This will be useful later when we index geometries.
1 parent f4a93e2 commit 60e7aba

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

src/geom-boost-adaptor.hpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
*/
1212

1313
#include "geom.hpp"
14+
#include "geom-box.hpp"
1415

1516
#include <boost/geometry.hpp>
17+
#include <boost/geometry/geometries/register/box.hpp>
1618
#include <boost/geometry/geometries/register/linestring.hpp>
1719
#include <boost/geometry/geometries/register/multi_linestring.hpp>
1820
#include <boost/geometry/geometries/register/multi_point.hpp>
@@ -78,6 +80,48 @@ struct interior_rings<::geom::polygon_t>
7880
static auto const &get(::geom::polygon_t const &p) { return p.inners(); }
7981
};
8082

83+
BOOST_GEOMETRY_DETAIL_SPECIALIZE_BOX_TRAITS(::geom::box_t, ::geom::point_t)
84+
85+
template <>
86+
struct indexed_access<::geom::box_t, min_corner, 0>
87+
{
88+
static inline double get(::geom::box_t const &b) { return b.min_x(); }
89+
static inline void set(::geom::box_t &b, double value)
90+
{
91+
b.set_min_x(value);
92+
}
93+
};
94+
95+
template <>
96+
struct indexed_access<::geom::box_t, min_corner, 1>
97+
{
98+
static inline double get(::geom::box_t const &b) { return b.min_y(); }
99+
static inline void set(::geom::box_t &b, double value)
100+
{
101+
b.set_min_y(value);
102+
}
103+
};
104+
105+
template <>
106+
struct indexed_access<::geom::box_t, max_corner, 0>
107+
{
108+
static inline double get(::geom::box_t const &b) { return b.max_x(); }
109+
static inline void set(::geom::box_t &b, double value)
110+
{
111+
b.set_max_x(value);
112+
}
113+
};
114+
115+
template <>
116+
struct indexed_access<::geom::box_t, max_corner, 1>
117+
{
118+
static inline double get(::geom::box_t const &b) { return b.max_y(); }
119+
static inline void set(::geom::box_t &b, double value)
120+
{
121+
b.set_max_y(value);
122+
}
123+
};
124+
81125
} // namespace boost::geometry::traits
82126

83127
#endif // OSM2PGSQL_GEOM_BOOST_ADAPTOR_HPP

src/geom-box.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ class box_t
4545
constexpr double max_x() const noexcept { return m_max_x; }
4646
constexpr double max_y() const noexcept { return m_max_y; }
4747

48+
void set_min_x(double value) noexcept { m_min_x = value; }
49+
void set_min_y(double value) noexcept { m_min_y = value; }
50+
void set_max_x(double value) noexcept { m_max_x = value; }
51+
void set_max_y(double value) noexcept { m_max_y = value; }
52+
4853
constexpr double width() const noexcept { return m_max_x - m_min_x; }
4954
constexpr double height() const noexcept { return m_max_y - m_min_y; }
5055

tests/test-geom-box.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,50 @@
99

1010
#include <catch.hpp>
1111

12+
#include "geom-boost-adaptor.hpp"
1213
#include "geom-box.hpp"
1314

15+
TEST_CASE("box_t getter/setter", "[NoDB]")
16+
{
17+
geom::box_t box{1.0, 2.0, 3.0, 4.0};
18+
19+
REQUIRE(box.min_x() == Approx(1.0));
20+
REQUIRE(box.max_x() == Approx(3.0));
21+
REQUIRE(box.min_y() == Approx(2.0));
22+
REQUIRE(box.max_y() == Approx(4.0));
23+
24+
box.set_min_x(1.5);
25+
box.set_min_y(2.5);
26+
box.set_max_x(3.5);
27+
box.set_max_y(4.5);
28+
29+
REQUIRE(box.min_x() == Approx(1.5));
30+
REQUIRE(box.max_x() == Approx(3.5));
31+
REQUIRE(box.min_y() == Approx(2.5));
32+
REQUIRE(box.max_y() == Approx(4.5));
33+
}
34+
35+
TEST_CASE("box_t getter/setter through boost", "[NoDB]")
36+
{
37+
geom::box_t box{1.0, 2.0, 3.0, 4.0};
38+
39+
namespace bg = ::boost::geometry;
40+
REQUIRE(bg::get<bg::min_corner, 0>(box) == Approx(1.0));
41+
REQUIRE(bg::get<bg::min_corner, 1>(box) == Approx(2.0));
42+
REQUIRE(bg::get<bg::max_corner, 0>(box) == Approx(3.0));
43+
REQUIRE(bg::get<bg::max_corner, 1>(box) == Approx(4.0));
44+
45+
bg::set<bg::min_corner, 0>(box, 1.5);
46+
bg::set<bg::min_corner, 1>(box, 2.5);
47+
bg::set<bg::max_corner, 0>(box, 3.5);
48+
bg::set<bg::max_corner, 1>(box, 4.5);
49+
50+
REQUIRE(box.min_x() == Approx(1.5));
51+
REQUIRE(box.min_y() == Approx(2.5));
52+
REQUIRE(box.max_x() == Approx(3.5));
53+
REQUIRE(box.max_y() == Approx(4.5));
54+
}
55+
1456
TEST_CASE("Extend box_t with points", "[NoDB]")
1557
{
1658
geom::box_t box;

0 commit comments

Comments
 (0)