4 // Copyright (c) 2021 Barend Gehrels, Amsterdam, the Netherlands.
6 // Use, modification and distribution is subject to the Boost Software License,
7 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
10 #include <geometry_test_common.hpp>
12 #include <boost/geometry/algorithms/detail/overlay/get_ring.hpp>
14 #include <boost/geometry/algorithms/area.hpp>
15 #include <boost/geometry/algorithms/is_valid.hpp>
17 #include <boost/geometry/strategies/strategies.hpp>
18 #include <boost/geometry/geometries/geometries.hpp>
19 #include <boost/geometry/io/wkt/wkt.hpp>
26 std::string
const simplex
= "POLYGON((0 2,1 2,1 1,0 1,0 2))";
27 std::string
const case_a
= "POLYGON((1 0,0 5,5 2,1 0),(2 1,3 2,1 3,2 1))";
28 std::string
const multi
= "MULTIPOLYGON(((0 0,0 5,5 5,5 0,0 0),(1 1,2 1,2 2,1 2,1 1),(3 3,4 3,4 4,3 4,3 3)),((6 6,6 10,10 10,10 6,6 6),(7 7,8 7,8 8,7 7)))";
31 template <typename Geometry
>
32 void test_get_ring(std::string
const& case_id
, std::string
const& wkt
,
33 bg::ring_identifier
const& ring_id
,
34 std::string
const& expected_wkt
)
36 using tag
= typename
bg::tag
<Geometry
>::type
;
39 bg::read_wkt(wkt
, geometry
);
41 auto const ring
= bg::detail::overlay::get_ring
<tag
>::apply(ring_id
, geometry
);
46 << " valid: " << bg::is_valid(geometry
)
47 << " area: " << bg::area(geometry
)
48 << " area(ring): " << bg::area(ring
)
49 << " wkt(ring): " << bg::wkt(ring
)
53 std::ostringstream out
;
55 std::string
const detected
= out
.str();
56 BOOST_CHECK_MESSAGE(detected
== expected_wkt
, "get_ring: " << case_id
57 << " expected: " << expected_wkt
58 << " detected: " << detected
);
61 template <typename Geometry
>
62 void test_segment_count_on_ring(std::string
const& case_id
, std::string
const& wkt
,
63 bg::ring_identifier
const& ring_id
, bg::signed_size_type expected_count
)
66 bg::read_wkt(wkt
, geometry
);
68 auto const detected_count
= bg::detail::overlay::segment_count_on_ring(geometry
, ring_id
);
70 BOOST_CHECK_MESSAGE(detected_count
== expected_count
,
71 "test_get_ring: " << case_id
72 << " expected: " << expected_count
73 << " detected: " << detected_count
);
76 template <typename Geometry
>
77 void test_segment_distance(std::string
const& case_id
, int line
, std::string
const& wkt
,
78 bg::segment_identifier
const& id1
,
79 bg::segment_identifier
const& id2
,
80 bg::signed_size_type expected_distance
)
83 bg::read_wkt(wkt
, geometry
);
85 auto const detected_distance
= bg::detail::overlay::segment_distance(geometry
, id1
, id2
);
87 BOOST_CHECK_MESSAGE(detected_distance
== expected_distance
,
88 "segment_distance: " << case_id
<< " (" << line
<< ")"
89 << " expected: " << expected_distance
90 << " detected: " << detected_distance
);
93 template <typename Point
, bool Closed
>
96 using ring
= bg::model::ring
<Point
, Closed
>;
97 using polygon
= bg::model::polygon
<Point
, Closed
>;
98 using multi_polygon
= bg::model::multi_polygon
<polygon
>;
100 test_get_ring
<ring
>("ring_simplex", simplex
, {0, -1, -1}, simplex
);
101 test_get_ring
<polygon
>("polygon_simplex", simplex
, {0, -1, -1}, simplex
);
102 test_get_ring
<polygon
>("case_a_outer", case_a
, {0, -1, -1}, "POLYGON((1 0,0 5,5 2,1 0))");
103 test_get_ring
<polygon
>("case_a_0", case_a
, {0, -1, 0}, "POLYGON((2 1,3 2,1 3,2 1))");
104 test_get_ring
<multi_polygon
>("multi_0_outer", multi
, {0, 0, -1}, "POLYGON((0 0,0 5,5 5,5 0,0 0))");
105 test_get_ring
<multi_polygon
>("multi_0_0", multi
, {0, 0, 0}, "POLYGON((1 1,2 1,2 2,1 2,1 1))");
106 test_get_ring
<multi_polygon
>("multi_0_1", multi
, {0, 0, 1}, "POLYGON((3 3,4 3,4 4,3 4,3 3))");
107 test_get_ring
<multi_polygon
>("multi_1_outer", multi
, {0, 1, -1}, "POLYGON((6 6,6 10,10 10,10 6,6 6))");
108 test_get_ring
<multi_polygon
>("multi_1_1", multi
, {0, 1, 0}, "POLYGON((7 7,8 7,8 8,7 7))");
111 template <typename Point
, bool Closed
>
112 void test_segment_count_on_ring()
114 using ring
= bg::model::ring
<Point
, true, Closed
>;
115 using polygon
= bg::model::polygon
<Point
, true, Closed
>;
116 using multi_polygon
= bg::model::multi_polygon
<polygon
>;
118 test_segment_count_on_ring
<ring
>("ring_simplex", simplex
, {0, -1, -1}, 4);
119 test_segment_count_on_ring
<polygon
>("polygon_simplex", simplex
, {0, -1, -1}, 4);
120 test_segment_count_on_ring
<polygon
>("case_a_outer", case_a
, {0, -1, -1}, 3);
121 test_segment_count_on_ring
<polygon
>("case_a_0", case_a
, {0, -1, 0}, 3);
122 test_segment_count_on_ring
<multi_polygon
>("multi_0_outer", multi
, {0, 0, -1}, 4);
123 test_segment_count_on_ring
<multi_polygon
>("multi_0_0", multi
, {0, 0, 0}, 4);
124 test_segment_count_on_ring
<multi_polygon
>("multi_0_1", multi
, {0, 0, 1}, 4);
125 test_segment_count_on_ring
<multi_polygon
>("multi_1_outer", multi
, {0, 1, -1}, 4);
126 test_segment_count_on_ring
<multi_polygon
>("multi_1_1", multi
, {0, 1, 0}, 3);
129 template <typename Point
, bool Closed
>
130 void test_segment_distance()
132 using ring
= bg::model::ring
<Point
, true, Closed
>;
134 std::string
const case_id
= "ring_simplex";
136 test_segment_distance
<ring
>(case_id
, __LINE__
, simplex
, {0, -1, -1, 0}, {0, -1, -1, 0}, 0);
137 test_segment_distance
<ring
>(case_id
, __LINE__
, simplex
, {0, -1, -1, 0}, {0, -1, -1, 1}, 1);
138 test_segment_distance
<ring
>(case_id
, __LINE__
, simplex
, {0, -1, -1, 0}, {0, -1, -1, 2}, 2);
139 test_segment_distance
<ring
>(case_id
, __LINE__
, simplex
, {0, -1, -1, 0}, {0, -1, -1, 3}, 3);
141 test_segment_distance
<ring
>(case_id
, __LINE__
, simplex
, {0, -1, -1, 1}, {0, -1, -1, 0}, 3);
142 test_segment_distance
<ring
>(case_id
, __LINE__
, simplex
, {0, -1, -1, 1}, {0, -1, -1, 1}, 0);
143 test_segment_distance
<ring
>(case_id
, __LINE__
, simplex
, {0, -1, -1, 1}, {0, -1, -1, 2}, 1);
144 test_segment_distance
<ring
>(case_id
, __LINE__
, simplex
, {0, -1, -1, 1}, {0, -1, -1, 3}, 2);
146 test_segment_distance
<ring
>(case_id
, __LINE__
, simplex
, {0, -1, -1, 2}, {0, -1, -1, 0}, 2);
147 test_segment_distance
<ring
>(case_id
, __LINE__
, simplex
, {0, -1, -1, 2}, {0, -1, -1, 1}, 3);
148 test_segment_distance
<ring
>(case_id
, __LINE__
, simplex
, {0, -1, -1, 2}, {0, -1, -1, 2}, 0);
149 test_segment_distance
<ring
>(case_id
, __LINE__
, simplex
, {0, -1, -1, 2}, {0, -1, -1, 3}, 1);
151 test_segment_distance
<ring
>(case_id
, __LINE__
, simplex
, {0, -1, -1, 3}, {0, -1, -1, 0}, 1);
152 test_segment_distance
<ring
>(case_id
, __LINE__
, simplex
, {0, -1, -1, 3}, {0, -1, -1, 1}, 2);
153 test_segment_distance
<ring
>(case_id
, __LINE__
, simplex
, {0, -1, -1, 3}, {0, -1, -1, 2}, 3);
154 test_segment_distance
<ring
>(case_id
, __LINE__
, simplex
, {0, -1, -1, 3}, {0, -1, -1, 3}, 0);
157 int test_main(int, char* [])
159 using point
= bg::model::point
<default_test_type
, 2, bg::cs::cartesian
>;
160 test_get_ring
<point
, true>();
161 test_segment_count_on_ring
<point
, true>();
162 test_segment_count_on_ring
<point
, false>();
163 test_segment_distance
<point
, true>();
164 test_segment_distance
<point
, false>();