]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Boost.Geometry (aka GGL, Generic Geometry Library) |
2 | // Unit Test | |
3 | ||
4 | // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands. | |
5 | // Copyright (c) 2008-2015 Bruno Lalande, Paris, France. | |
6 | // Copyright (c) 2009-2015 Mateusz Loskot, London, UK. | |
7 | ||
8 | // This file was modified by Oracle on 2014, 2015. | |
9 | // Modifications copyright (c) 2014-2015 Oracle and/or its affiliates. | |
10 | ||
11 | // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle | |
12 | ||
13 | // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library | |
14 | // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands. | |
15 | ||
16 | // Use, modification and distribution is subject to the Boost Software License, | |
17 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | |
18 | // http://www.boost.org/LICENSE_1_0.txt) | |
19 | ||
20 | #include <algorithms/test_centroid.hpp> | |
21 | ||
22 | #include <boost/geometry/geometries/geometries.hpp> | |
23 | #include <boost/geometry/geometries/point_xy.hpp> | |
24 | #include <boost/geometry/geometries/adapted/c_array.hpp> | |
25 | #include <boost/geometry/geometries/adapted/boost_tuple.hpp> | |
26 | ||
27 | #include <test_geometries/all_custom_polygon.hpp> | |
28 | ||
29 | BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian) | |
30 | BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian) | |
31 | ||
32 | template <typename Polygon> | |
33 | void test_polygon() | |
34 | { | |
35 | test_centroid<Polygon>( | |
36 | "POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2" | |
37 | ",3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3))", | |
38 | 4.06923363095238, 1.65055803571429); | |
39 | ||
40 | // with holes | |
41 | test_centroid<Polygon>( | |
42 | "POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2" | |
43 | ",3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3)" | |
44 | ",(4 2,4.2 1.4,4.8 1.9,4.4 2.2,4 2))", | |
45 | 4.0466264962959677, 1.6348996057331333); | |
46 | ||
47 | test_centroid<Polygon>("POLYGON((0 0,0 10,10 10,10 0,0 0))", 5.0, 5.0); | |
48 | test_centroid<Polygon>("POLYGON((-10 0,0 0,0 -10,-10 -10,-10 0))", -5.0, -5.0); | |
49 | ||
50 | // invalid, self-intersecting polygon (area = 0) | |
51 | test_centroid<Polygon>("POLYGON((1 1,4 -2,4 2,10 0,1 0,10 1,1 1))", 1.0, 1.0); | |
52 | // invalid, degenerated | |
53 | test_centroid<Polygon>("POLYGON((1 1,1 1,1 1,1 1))", 1.0, 1.0); | |
54 | test_centroid<Polygon>("POLYGON((1 1))", 1.0, 1.0); | |
55 | ||
56 | // should (1.5 1) be returned? | |
57 | // if yes, then all other Polygons degenerated to Linestrings should be handled | |
58 | test_centroid<Polygon>("POLYGON((1 1,2 1,1 1,1 1))", 1.0, 1.0); | |
59 | ||
60 | // reported 2015.04.24 | |
61 | // input INT, result FP | |
62 | test_centroid | |
63 | < | |
64 | bg::model::polygon<bg::model::d2::point_xy<int> >, | |
65 | typename bg::point_type<Polygon>::type, | |
66 | typename bg::coordinate_type<Polygon>::type | |
67 | >("POLYGON((1 1, 1 2, 2 2, 2 1, 1 1))", 1.5, 1.5); | |
68 | } | |
69 | ||
70 | ||
71 | template <typename P> | |
72 | void test_2d() | |
73 | { | |
74 | test_centroid<bg::model::linestring<P> >("LINESTRING(1 1, 2 2, 3 3)", 2.0, 2.0); | |
75 | test_centroid<bg::model::linestring<P> >("LINESTRING(0 0,0 4, 4 4)", 1.0, 3.0); | |
76 | test_centroid<bg::model::linestring<P> >("LINESTRING(0 0,3 3,0 6,3 9,0 12)", 1.5, 6.0); | |
77 | ||
78 | test_centroid<bg::model::linestring<P> >("LINESTRING(1 1,10 1,1 0,10 0,4 -2,1 1)", | |
79 | 5.41385255923004, 0.13507358481085); | |
80 | ||
81 | // degenerated linestring (length = 0) | |
82 | test_centroid<bg::model::linestring<P> >("LINESTRING(1 1, 1 1)", 1.0, 1.0); | |
83 | test_centroid<bg::model::linestring<P> >("LINESTRING(1 1)", 1.0, 1.0); | |
84 | ||
85 | { | |
86 | bg::model::linestring<P> ls; | |
87 | // LINESTRING(1 -1,1e308 -1e308,0.0001 0.000) | |
88 | bg::append(ls, P(1, -1)); | |
89 | typedef typename bg::coordinate_type<P>::type coord_type; | |
90 | //double m = 1.0e308; | |
91 | coord_type m = (std::numeric_limits<coord_type>::max)(); | |
92 | bg::append(ls, P(coord_type(m), coord_type(-m))); | |
93 | bg::append(ls, P(coord_type(0.0001), coord_type(0.000))); | |
94 | if (BOOST_GEOMETRY_CONDITION((boost::is_same<typename bg::coordinate_type<P>::type, double>::value))) | |
95 | { | |
96 | // for doubles the INF is detected and the calculation stopped | |
97 | // currently for Geometries for which the centroid can't be calculated | |
98 | // the first Point is returned | |
99 | test_centroid<bg::model::linestring<P> >(ls, 1.0, -1.0); | |
100 | } | |
101 | else | |
102 | { | |
103 | // for floats internally the double is used to store intermediate results | |
104 | // this type is capable to store MAX_FLT and "correctly" calculate the centroid | |
105 | // test_centroid<bg::model::linestring<P> >(ls, m/3, -m/3); | |
106 | // the result is around (1.7e38 -1.7e38) | |
107 | } | |
108 | } | |
109 | ||
110 | test_centroid<bg::model::segment<P> >("LINESTRING(1 1, 3 3)", 2.0, 2.0); | |
111 | ||
112 | test_centroid<bg::model::ring<P> >( | |
113 | "POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2" | |
114 | ",3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3))", | |
115 | 4.06923363095238, 1.65055803571429); | |
116 | ||
117 | test_polygon<bg::model::polygon<P> >(); | |
118 | test_polygon<all_custom_polygon<P> >(); | |
119 | ||
120 | // ccw | |
121 | test_centroid<bg::model::ring<P, false> >( | |
122 | "POLYGON((2 1.3,2.9 0.7,4.9 0.8,5.4 1.2,5.3 2.6,4.1 3,3.4 2" | |
123 | ",3.7 1.6,3.4 1.2,2.8 1.8,2.4 1.7,2 1.3))", | |
124 | 4.06923363095238, 1.65055803571429); | |
125 | ||
126 | // open / closed | |
127 | test_centroid<bg::model::ring<P, true, true> >( | |
128 | "POLYGON((1 1,2 2,3 1,2 0,1 1))", 2.0, 1.0); | |
129 | test_centroid<bg::model::ring<P, true, false> >( | |
130 | "POLYGON((1 1,2 2,3 1,2 0))", 2.0, 1.0); | |
131 | ||
132 | test_centroid<bg::model::box<P> >("POLYGON((1 2,3 4))", 2, 3); | |
133 | test_centroid<P>("POINT(3 3)", 3, 3); | |
134 | ||
135 | // INT -> FP | |
136 | test_centroid | |
137 | < | |
138 | bg::model::ring<bg::model::d2::point_xy<int> >, | |
139 | P, typename bg::coordinate_type<P>::type | |
140 | >("POLYGON((1 1, 1 2, 2 2, 2 1, 1 1))", 1.5, 1.5); | |
141 | test_centroid | |
142 | < | |
143 | bg::model::linestring<bg::model::d2::point_xy<int> >, | |
144 | P, typename bg::coordinate_type<P>::type | |
145 | >("LINESTRING(1 1, 2 2)", 1.5, 1.5); | |
146 | test_centroid | |
147 | < | |
148 | bg::model::box<bg::model::d2::point_xy<int> >, | |
149 | P, typename bg::coordinate_type<P>::type | |
150 | >("BOX(1 1, 2 2)", 1.5, 1.5); | |
151 | } | |
152 | ||
153 | ||
154 | template <typename P> | |
155 | void test_3d() | |
156 | { | |
157 | test_centroid<bg::model::linestring<P> >("LINESTRING(1 2 3,4 5 -6,7 -8 9,-10 11 12,13 -14 -15, 16 17 18)", | |
158 | 5.6748865168734692, 0.31974938587214002, 1.9915270387763671); | |
159 | test_centroid<bg::model::box<P> >("POLYGON((1 2 3,5 6 7))", 3, 4, 5); | |
160 | test_centroid<bg::model::segment<P> >("LINESTRING(1 1 1,3 3 3)", 2, 2, 2); | |
161 | test_centroid<P>("POINT(1 2 3)", 1, 2, 3); | |
162 | } | |
163 | ||
164 | ||
165 | template <typename P> | |
166 | void test_5d() | |
167 | { | |
168 | test_centroid<bg::model::linestring<P> >("LINESTRING(1 2 3 4 95,4 5 -6 24 40,7 -8 9 -5 -7,-10 11 12 -5 5,13 -14 -15 4 3, 16 17 18 5 12)", | |
169 | 4.9202312983547678, 0.69590937869808345, 1.2632138719797417, 6.0468332057401986, 23.082402715244868); | |
170 | } | |
171 | ||
172 | template <typename P> | |
173 | void test_exceptions() | |
174 | { | |
175 | test_centroid_exception<bg::model::linestring<P> >(); | |
176 | test_centroid_exception<bg::model::polygon<P> >(); | |
177 | test_centroid_exception<bg::model::ring<P> >(); | |
178 | ||
179 | // Empty exterior ring | |
180 | test_centroid_exception<bg::model::polygon<P> >( | |
181 | "POLYGON((), ())"); | |
182 | test_centroid_exception<bg::model::polygon<P> >( | |
183 | "POLYGON((), (0 0, 1 0, 1 1, 0 1, 0 0))"); | |
184 | } | |
185 | ||
186 | template <typename P> | |
187 | void test_empty() | |
188 | { | |
189 | // Empty interior ring | |
190 | test_centroid<bg::model::polygon<P> >( | |
191 | "POLYGON((0 0, 1 0, 1 1, 0 1, 0 0), ())", | |
192 | 0.5, 0.5); | |
193 | } | |
194 | ||
195 | void test_large_integers() | |
196 | { | |
197 | typedef bg::model::point<int, 2, bg::cs::cartesian> int_point_type; | |
198 | typedef bg::model::point<double, 2, bg::cs::cartesian> double_point_type; | |
199 | ||
200 | bg::model::polygon<int_point_type> int_poly; | |
201 | bg::model::polygon<double_point_type> double_poly; | |
202 | ||
203 | std::string const polygon_li = "POLYGON((1872000 528000,1872000 192000,1536119 192000,1536000 528000,1200000 528000,1200000 863880,1536000 863880,1872000 863880,1872000 528000))"; | |
204 | bg::read_wkt(polygon_li, int_poly); | |
205 | bg::read_wkt(polygon_li, double_poly); | |
206 | ||
207 | int_point_type int_centroid; | |
208 | double_point_type double_centroid; | |
209 | ||
210 | bg::centroid(int_poly, int_centroid); | |
211 | bg::centroid(double_poly, double_centroid); | |
212 | ||
213 | int_point_type double_centroid_as_int; | |
214 | bg::assign_zero(double_centroid_as_int); | |
215 | bg::assign(int_centroid, double_centroid_as_int); | |
216 | ||
217 | BOOST_CHECK_EQUAL(bg::get<0>(int_centroid), bg::get<0>(double_centroid_as_int)); | |
218 | BOOST_CHECK_EQUAL(bg::get<1>(int_centroid), bg::get<1>(double_centroid_as_int)); | |
219 | } | |
220 | ||
221 | //#include <to_svg.hpp> | |
222 | ||
223 | void test_large_doubles() | |
224 | { | |
225 | typedef bg::model::point<double, 2, bg::cs::cartesian> point; | |
226 | point pt_far, pt_near; | |
227 | bg::model::polygon<point> poly_far, poly_near; | |
228 | ||
229 | // related to ticket #10643 | |
230 | bg::read_wkt("POLYGON((1074699.93 703064.65, 1074703.90 703064.58, 1074704.53 703061.40, 1074702.10 703054.62, 1074699.93 703064.65))", poly_far); | |
231 | bg::read_wkt("POLYGON((699.93 64.65, 703.90 64.58, 704.53 61.40, 702.10 54.62, 699.93 64.65))", poly_near); | |
232 | ||
233 | bg::centroid(poly_far, pt_far); | |
234 | bg::centroid(poly_near, pt_near); | |
235 | ||
236 | BOOST_CHECK(bg::within(pt_far, poly_far)); | |
237 | BOOST_CHECK(bg::within(pt_near, poly_near)); | |
238 | ||
239 | point pt_near_moved; | |
240 | bg::set<0>(pt_near_moved, bg::get<0>(pt_near) + 1074000.0); | |
241 | bg::set<1>(pt_near_moved, bg::get<1>(pt_near) + 703000.0); | |
242 | ||
243 | //geom_to_svg(poly_far, pt_far, "far.svg"); | |
244 | //geom_to_svg(poly_near, pt_near, "near.svg"); | |
245 | ||
246 | double d = bg::distance(pt_far, pt_near_moved); | |
247 | BOOST_CHECK(d < 0.1); | |
248 | } | |
249 | ||
250 | int test_main(int, char* []) | |
251 | { | |
252 | test_2d<bg::model::d2::point_xy<double> >(); | |
253 | test_2d<boost::tuple<float, float> >(); | |
254 | test_2d<bg::model::d2::point_xy<float> >(); | |
255 | ||
256 | test_3d<boost::tuple<double, double, double> >(); | |
257 | ||
258 | test_5d<boost::tuple<double, double, double, double, double> >(); | |
259 | ||
7c673cae FG |
260 | #ifndef NDEBUG |
261 | // The test currently fails in release mode. TODO: fix this | |
262 | test_large_integers(); | |
263 | #endif | |
264 | ||
265 | test_large_doubles(); | |
266 | ||
267 | test_exceptions<bg::model::d2::point_xy<double> >(); | |
268 | test_empty<bg::model::d2::point_xy<double> >(); | |
269 | ||
270 | return 0; | |
271 | } |