num_segments.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2014, Oracle and/or its affiliates.
  4. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  5. // Licensed under the Boost Software License version 1.0.
  6. // http://www.boost.org/users/license.html
  7. #ifndef BOOST_TEST_MODULE
  8. #define BOOST_TEST_MODULE test_num_segments
  9. #endif
  10. #include <iostream>
  11. #include <boost/test/included/unit_test.hpp>
  12. #include <boost/variant/variant.hpp>
  13. #include <boost/geometry/algorithms/num_segments.hpp>
  14. #include <boost/geometry/core/closure.hpp>
  15. #include <boost/geometry/core/tag.hpp>
  16. #include <boost/geometry/core/tags.hpp>
  17. #include <boost/geometry/geometries/geometries.hpp>
  18. #include <boost/geometry/io/wkt/wkt.hpp>
  19. #include <boost/geometry/io/dsv/write.hpp>
  20. namespace bg = boost::geometry;
  21. typedef bg::model::point<double, 2, bg::cs::cartesian> point;
  22. typedef bg::model::linestring<point> linestring;
  23. typedef bg::model::segment<point> segment;
  24. typedef bg::model::box<point> box;
  25. typedef bg::model::ring<point, true, true> ring_cw_closed;
  26. typedef bg::model::ring<point, true, false> ring_cw_open;
  27. typedef bg::model::ring<point, false, true> ring_ccw_closed;
  28. typedef bg::model::ring<point, false, false> ring_ccw_open;
  29. typedef bg::model::polygon<point, true, true> polygon_cw_closed;
  30. typedef bg::model::polygon<point, true, false> polygon_cw_open;
  31. typedef bg::model::polygon<point, false, true> polygon_ccw_closed;
  32. typedef bg::model::polygon<point, false, false> polygon_ccw_open;
  33. typedef bg::model::multi_point<point> multi_point;
  34. typedef bg::model::multi_linestring<linestring> multi_linestring;
  35. typedef bg::model::multi_polygon<polygon_cw_closed> multi_polygon_cw_closed;
  36. typedef bg::model::multi_polygon<polygon_cw_open> multi_polygon_cw_open;
  37. typedef bg::model::multi_polygon<polygon_ccw_closed> multi_polygon_ccw_closed;
  38. typedef bg::model::multi_polygon<polygon_ccw_open> multi_polygon_ccw_open;
  39. template <std::size_t D, typename T = double>
  40. struct box_dD
  41. {
  42. typedef boost::geometry::model::box
  43. <
  44. boost::geometry::model::point<T, D, boost::geometry::cs::cartesian>
  45. > type;
  46. };
  47. template <typename Geometry, typename Tag = typename bg::tag<Geometry>::type>
  48. struct test_num_segments
  49. {
  50. static inline void apply(Geometry const& geometry, std::size_t expected)
  51. {
  52. std::size_t detected = bg::num_segments(geometry);
  53. BOOST_CHECK_MESSAGE( detected == expected,
  54. "Expected: " << expected
  55. << " detected: " << detected
  56. << " wkt: " << bg::wkt(geometry) );
  57. }
  58. static inline void apply(std::string const& wkt, std::size_t expected)
  59. {
  60. Geometry geometry;
  61. bg::read_wkt(wkt, geometry);
  62. apply(geometry, expected);
  63. }
  64. };
  65. template <typename Box>
  66. struct test_num_segments<Box, bg::box_tag>
  67. {
  68. static inline void apply(Box const& box, std::size_t expected)
  69. {
  70. std::size_t detected = bg::num_segments(box);
  71. BOOST_CHECK_MESSAGE( detected == expected,
  72. "Expected: " << expected
  73. << " detected: " << detected
  74. << " dsv: " << bg::dsv(box) );
  75. }
  76. static inline void apply(std::string const& wkt, std::size_t expected)
  77. {
  78. Box box;
  79. bg::read_wkt(wkt, box);
  80. apply(box, expected);
  81. }
  82. };
  83. BOOST_AUTO_TEST_CASE( test_point )
  84. {
  85. test_num_segments<point>::apply("POINT(0 0)", 0);
  86. }
  87. BOOST_AUTO_TEST_CASE( test_segment )
  88. {
  89. test_num_segments<segment>::apply("SEGMENT(0 0,1 1)", 1);
  90. }
  91. BOOST_AUTO_TEST_CASE( test_box )
  92. {
  93. test_num_segments<box>::apply("BOX(0 0,1 1)", 4);
  94. // test higher-dimensional boxes
  95. test_num_segments<box_dD<3>::type>::apply("BOX(0 0 0,1 1 1)", 12);
  96. test_num_segments<box_dD<4>::type>::apply("BOX(0 0 0 0,1 1 1 1)", 32);
  97. test_num_segments<box_dD<5>::type>::apply("BOX(0 0 0 0 0,1 1 1 1 1)", 80);
  98. }
  99. BOOST_AUTO_TEST_CASE( test_linestring )
  100. {
  101. typedef test_num_segments<linestring> tester;
  102. tester::apply("LINESTRING()", 0);
  103. tester::apply("LINESTRING(0 0)", 0);
  104. tester::apply("LINESTRING(0 0,0 0)", 1);
  105. tester::apply("LINESTRING(0 0,0 0,1 1)", 2);
  106. tester::apply("LINESTRING(0 0,0 0,0 0,1 1)", 3);
  107. }
  108. BOOST_AUTO_TEST_CASE( test_multipoint )
  109. {
  110. typedef test_num_segments<multi_point> tester;
  111. tester::apply("MULTIPOINT()", 0);
  112. tester::apply("MULTIPOINT(0 0)", 0);
  113. tester::apply("MULTIPOINT(0 0,0 0)", 0);
  114. tester::apply("MULTIPOINT(0 0,0 0,1 1)", 0);
  115. }
  116. BOOST_AUTO_TEST_CASE( test_multilinestring )
  117. {
  118. typedef test_num_segments<multi_linestring> tester;
  119. tester::apply("MULTILINESTRING()", 0);
  120. tester::apply("MULTILINESTRING((),(0 0))", 0);
  121. tester::apply("MULTILINESTRING((0 0))", 0);
  122. tester::apply("MULTILINESTRING((0 0,1 0))", 1);
  123. tester::apply("MULTILINESTRING((),(),(0 0,1 0))", 1);
  124. tester::apply("MULTILINESTRING((0 0,1 0,0 1),(0 0,1 0,0 1,0 0))", 5);
  125. }
  126. template <typename OpenRing>
  127. void test_open_ring()
  128. {
  129. typedef test_num_segments<OpenRing> tester;
  130. tester::apply("POLYGON(())", 0);
  131. tester::apply("POLYGON((0 0))", 0);
  132. tester::apply("POLYGON((0 0,1 0))", 2);
  133. tester::apply("POLYGON((0 0,1 0,0 1))", 3);
  134. tester::apply("POLYGON((0 0,0 0,1 0,0 1))", 4);
  135. }
  136. template <typename ClosedRing>
  137. void test_closed_ring()
  138. {
  139. typedef test_num_segments<ClosedRing> tester;
  140. tester::apply("POLYGON(())", 0);
  141. tester::apply("POLYGON((0 0))", 0);
  142. tester::apply("POLYGON((0 0,0 0))", 1);
  143. tester::apply("POLYGON((0 0,1 0,0 0))", 2);
  144. tester::apply("POLYGON((0 0,1 0,0 1,0 0))", 3);
  145. tester::apply("POLYGON((0 0,1 0,1 0,0 1,0 0))", 4);
  146. }
  147. BOOST_AUTO_TEST_CASE( test_ring )
  148. {
  149. test_open_ring<ring_ccw_open>();
  150. test_open_ring<ring_cw_open>();
  151. test_closed_ring<ring_ccw_closed>();
  152. test_closed_ring<ring_cw_closed>();
  153. }
  154. template <typename OpenPolygon>
  155. void test_open_polygon()
  156. {
  157. typedef test_num_segments<OpenPolygon> tester;
  158. tester::apply("POLYGON(())", 0);
  159. tester::apply("POLYGON((0 0))", 0);
  160. tester::apply("POLYGON((0 0,10 0),(0 0))", 2);
  161. tester::apply("POLYGON((0 0,10 0),(1 1,2 1))", 4);
  162. tester::apply("POLYGON((0 0,10 0,0 10))", 3);
  163. tester::apply("POLYGON((0 0,10 0,0 10),())", 3);
  164. tester::apply("POLYGON((0 0,10 0,0 10),(1 1))", 3);
  165. tester::apply("POLYGON((0 0,10 0,0 10),(1 1,2 1))", 5);
  166. tester::apply("POLYGON((0 0,10 0,0 10),(1 1,2 1,1 2))", 6);
  167. tester::apply("POLYGON((0 0,10 0,10 10,0 10),(1 1,2 1,1 2))", 7);
  168. tester::apply("POLYGON((0 0,10 0,10 10,0 10),(1 1,2 1,2 2,1 2))", 8);
  169. tester::apply("POLYGON((0 0,10 0,10 10,0 10),(1 1,2 1,2 2,1 2),(5 5,6 5,6 6,5 6))", 12);
  170. }
  171. template <typename ClosedPolygon>
  172. void test_closed_polygon()
  173. {
  174. typedef test_num_segments<ClosedPolygon> tester;
  175. tester::apply("POLYGON(())", 0);
  176. tester::apply("POLYGON((0 0))", 0);
  177. tester::apply("POLYGON((0 0,10 0,0 0),(0 0))", 2);
  178. tester::apply("POLYGON((0 0,10 0,0 0),(1 1,2 1,1 1))", 4);
  179. tester::apply("POLYGON((0 0,10 0,0 10,0 0))", 3);
  180. tester::apply("POLYGON((0 0,10 0,0 10,0 0),())", 3);
  181. tester::apply("POLYGON((0 0,10 0,0 10,0 0),(1 1))", 3);
  182. tester::apply("POLYGON((0 0,10 0,0 10,0 0),(1 1,2 1,1 1))", 5);
  183. tester::apply("POLYGON((0 0,10 0,0 10,0 0),(1 1,2 1,1 2,1 1))", 6);
  184. tester::apply("POLYGON((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,1 2,1 1))", 7);
  185. tester::apply("POLYGON((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,2 2,1 2,1 1))", 8);
  186. tester::apply("POLYGON((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,2 2,1 2,1 1),(5 5,6 5,6 6,5 6,5 5))", 12);
  187. }
  188. BOOST_AUTO_TEST_CASE( test_polygon )
  189. {
  190. test_open_polygon<polygon_ccw_open>();
  191. test_open_polygon<polygon_cw_open>();
  192. test_closed_polygon<polygon_ccw_closed>();
  193. test_closed_polygon<polygon_cw_closed>();
  194. }
  195. template <typename OpenMultiPolygon>
  196. void test_open_multipolygon()
  197. {
  198. typedef test_num_segments<OpenMultiPolygon> tester;
  199. tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10),(1 1,2 1,1 2)))", 7);
  200. tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10),(1 1,2 1,2 2,1 2),(5 5,6 5,6 6,5 6)))", 12);
  201. tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10),(1 1,2 1,1 2)),((100 100,110 100,110 110),(101 101,102 101,102 102)))", 13);
  202. tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10),(1 1,2 1,2 2,1 2),(5 5,6 5,6 6,5 6)),((100 100,110 100,110 110),(101 101,102 101,102 102),(105 105,106 105,106 106,105 106)))", 22);
  203. }
  204. template <typename ClosedMultiPolygon>
  205. void test_closed_multipolygon()
  206. {
  207. typedef test_num_segments<ClosedMultiPolygon> tester;
  208. tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,1 2,1 1)))", 7);
  209. tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,2 2,1 2,1 1),(5 5,6 5,6 6,5 6,5 5)))", 12);
  210. tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,1 2,1 1)),((100 100,110 100,110 110,100 100),(101 101,102 101,102 102,101 101)))", 13);
  211. tester::apply("MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,2 2,1 2,1 1),(5 5,6 5,6 6,5 6,5 5)),((100 100,110 100,110 110,100 100),(101 101,102 101,102 102,101 101),(105 105,106 105,106 106,105 106,105 105)))", 22);
  212. }
  213. BOOST_AUTO_TEST_CASE( test_multipolygon )
  214. {
  215. test_open_multipolygon<multi_polygon_ccw_open>();
  216. test_open_multipolygon<multi_polygon_cw_open>();
  217. test_closed_multipolygon<multi_polygon_ccw_closed>();
  218. test_closed_multipolygon<multi_polygon_cw_closed>();
  219. }
  220. BOOST_AUTO_TEST_CASE( test_variant )
  221. {
  222. typedef boost::variant
  223. <
  224. linestring, polygon_cw_open, polygon_cw_closed
  225. > variant_geometry_type;
  226. typedef test_num_segments<variant_geometry_type> tester;
  227. linestring ls;
  228. bg::read_wkt("LINESTRING(0 0,1 1,2 2)", ls);
  229. polygon_cw_open p_open;
  230. bg::read_wkt("POLYGON((0 0,0 1,1 0))", p_open);
  231. polygon_cw_closed p_closed;
  232. bg::read_wkt("POLYGON((0 0,0 1,1 1,1 0,0 0))", p_closed);
  233. variant_geometry_type variant_geometry;
  234. variant_geometry = ls;
  235. tester::apply(variant_geometry, 2);
  236. variant_geometry = p_open;
  237. tester::apply(variant_geometry, 3);
  238. variant_geometry = p_closed;
  239. tester::apply(variant_geometry, 4);
  240. }