centroid_multi.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. // Copyright (c) 2017 Adam Wulkiewicz, Lodz, Poland.
  7. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  8. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  9. // Use, modification and distribution is subject to the Boost Software License,
  10. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #include <algorithms/test_centroid.hpp>
  13. #include <boost/core/ignore_unused.hpp>
  14. #include <boost/geometry/core/point_order.hpp>
  15. #include <boost/geometry/strategies/cartesian/centroid_average.hpp>
  16. #include <boost/geometry/geometries/geometries.hpp>
  17. #include <boost/geometry/geometries/point_xy.hpp>
  18. // #define REPORT_RESULTS
  19. template <typename P>
  20. void test_2d(bool is_integer = false)
  21. {
  22. typedef typename bg::coordinate_type<P>::type ct;
  23. boost::ignore_unused<ct>();
  24. #ifdef REPORT_RESULTS
  25. std::cout << std::endl << "type: " << typeid(ct).name() << " size: " << sizeof(ct) << std::endl;
  26. #endif
  27. test_centroid<bg::model::multi_point<P> >(
  28. "MULTIPOINT((1 1),(3 3))",
  29. 2.0, 2.0);
  30. test_centroid<bg::model::multi_point<P> >(
  31. "MULTIPOINT((-1 -1),(-3 -3))",
  32. -2.0, -2.0);
  33. if (! is_integer)
  34. {
  35. // Only working for floating point:
  36. test_centroid<bg::model::multi_point<P> >(
  37. "MULTIPOINT((1 1),(2 3),(5 0))",
  38. 2.666666666666667, 1.33333);
  39. test_centroid<bg::model::multi_linestring<bg::model::linestring<P> > >(
  40. "MULTILINESTRING((0 0,0 2),(1 0,1 2))",
  41. 0.5, 1.0);
  42. // degenerated
  43. test_centroid<bg::model::multi_linestring<bg::model::linestring<P> > >(
  44. "MULTILINESTRING((1 1,1 1))",
  45. 1.0, 1.0);
  46. test_centroid<bg::model::multi_polygon<bg::model::polygon<P> > >(
  47. "MULTIPOLYGON(((1 1,1 3,3 3,3 1,1 1)),((4 1,4 3,8 3,8 1,4 1)))",
  48. 4.666666666666667, 2.0);
  49. test_centroid<bg::model::multi_polygon<bg::model::polygon<P> > >(
  50. "MULTIPOLYGON(((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2"
  51. ",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)),"
  52. "((10 10,10 12,12 12,12 10,10 10)))",
  53. 7.338463104108615, 6.0606722055552407);
  54. // degenerated
  55. test_centroid<bg::model::multi_polygon<bg::model::polygon<P> > >(
  56. "MULTIPOLYGON(((1 1,1 1,1 1,1 1,1 1)))",
  57. 1.0, 1.0);
  58. test_centroid<bg::model::multi_polygon<bg::model::polygon<P> > >(
  59. "MULTIPOLYGON(((1 1)))",
  60. 1.0, 1.0);
  61. }
  62. // Test using real-world polygon with large (Y) coordinates
  63. // (coordinates can be used for integer and floating point point-types)
  64. // Note that this will fail (overflow) if centroid calculation uses float
  65. test_centroid<bg::model::multi_polygon<bg::model::polygon<P> > >(
  66. "MULTIPOLYGON(((426062 4527794,426123 4527731"
  67. ",426113 4527700,426113 4527693,426115 4527671"
  68. ",426133 4527584,426135 4527569,426124 4527558"
  69. ",426103 4527547,426072 4527538,426003 4527535"
  70. ",425972 4527532,425950 4527531,425918 4527528"
  71. ",425894 4527517,425876 4527504,425870 4527484"
  72. ",425858 4527442,425842 4527414,425816 4527397"
  73. ",425752 4527384,425692 4527369,425658 4527349"
  74. ",425624 4527307,425605 4527260,425598 4527213"
  75. ",425595 4527167,425582 4527125,425548 4527064"
  76. ",425535 4527027,425537 4526990,425534 4526943"
  77. ",425525 4526904,425500 4526856,425461 4526811"
  78. ",425450 4526798,425381 4526823,425362 4526830"
  79. ",425329 4526848,425298 4526883,425291 4526897"
  80. ",425268 4526923,425243 4526945,425209 4526971"
  81. ",425172 4526990,425118 4527028,425104 4527044"
  82. ",425042 4527090,424980 4527126,424925 4527147"
  83. ",424881 4527148,424821 4527147,424698 4527125"
  84. ",424610 4527121,424566 4527126,424468 4527139"
  85. ",424426 4527141,424410 4527142,424333 4527130"
  86. ",424261 4527110,424179 4527073,424024 4527012"
  87. ",423947 4526987,423902 4526973,423858 4526961"
  88. ",423842 4526951,423816 4526935,423799 4526910"
  89. ",423776 4526905,423765 4526911,423739 4526927"
  90. ",423692 4526946,423636 4526976,423608 4527008"
  91. ",423570 4527016,423537 4527011,423505 4526996"
  92. ",423480 4526994,423457 4527012,423434 4527021"
  93. ",423367 4527008,423263 4526998,423210 4526993"
  94. ",423157 4526996,423110 4526994,423071 4526984"
  95. ",423048 4526984,423032 4526994,423254 4527613"
  96. ",423889 4528156,424585 4528050,425479 4527974"
  97. ",425795 4527867,426062 4527794)))",
  98. 424530.6059719588, 4527519.619367547);
  99. }
  100. template <typename P>
  101. void test_exceptions()
  102. {
  103. using namespace bg::model;
  104. typedef multi_polygon<polygon<P> > multi_polygon;
  105. typedef multi_linestring<linestring<P> > multi_linestring;
  106. // Empty multi-polygon
  107. test_centroid_exception<multi_polygon>("MULTIPOLYGON()");
  108. test_centroid_exception<multi_polygon>("MULTIPOLYGON(())");
  109. test_centroid_exception<multi_polygon>("MULTIPOLYGON((), ())");
  110. test_centroid_exception<multi_polygon>("MULTIPOLYGON((()), ())");
  111. test_centroid_exception<multi_polygon>("MULTIPOLYGON(((), ()))");
  112. // Empty multi-linestring
  113. test_centroid_exception<multi_linestring>("MULTILINESTRING()");
  114. test_centroid_exception<multi_linestring>("MULTILINESTRING(())");
  115. test_centroid_exception<multi_linestring>("MULTILINESTRING((), ())");
  116. }
  117. template <typename P>
  118. void test_empty()
  119. {
  120. using namespace bg::model;
  121. typedef multi_polygon<polygon<P> > multi_polygon;
  122. typedef multi_linestring<linestring<P> > multi_linestring;
  123. // Multi-linestring with empty linestring
  124. test_centroid<multi_linestring>(
  125. "MULTILINESTRING((), (0 0))",
  126. 0.0, 0.0);
  127. test_centroid<multi_linestring>(
  128. "MULTILINESTRING((0 0, 1 0), ())",
  129. 0.5, 0.0);
  130. // Multi-polygon with empty polygon
  131. test_centroid<multi_polygon>(
  132. "MULTIPOLYGON((()), ((0 0)))",
  133. 0.0, 0.0);
  134. test_centroid<multi_polygon>(
  135. "MULTIPOLYGON(((0 0, 1 0, 1 1, 0 1, 0 0)), (()))",
  136. 0.5, 0.5);
  137. // Multi-polygon with empty interior ring
  138. test_centroid<multi_polygon>(
  139. "MULTIPOLYGON(((0 0, 1 0, 1 1, 0 1, 0 0), ()))",
  140. 0.5, 0.5);
  141. test_centroid<multi_polygon>(
  142. "MULTIPOLYGON((()), ((0 0, 1 0, 1 1, 0 1, 0 0), ()))",
  143. 0.5, 0.5);
  144. }
  145. int test_main(int, char* [])
  146. {
  147. test_2d<bg::model::d2::point_xy<float> >();
  148. test_2d<bg::model::d2::point_xy<double> >();
  149. test_2d<bg::model::d2::point_xy<long int> >(true);
  150. //test_2d<bg::model::d2::point_xy<long long> >(true);
  151. //test_2d<bg::model::d2::point_xy<long double> >();
  152. #ifdef HAVE_TTMATH
  153. test_2d<bg::model::d2::point_xy<ttmath_big> >();
  154. #endif
  155. test_exceptions<bg::model::d2::point_xy<double> >();
  156. test_empty<bg::model::d2::point_xy<double> >();
  157. return 0;
  158. }