is_empty.cpp 11 KB

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