centroid.cpp 10.0 KB

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