intersects.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2013-2015 Adam Wulkiewicz, Lodz, Poland.
  4. // This file was modified by Oracle on 2013, 2015, 2016.
  5. // Modifications copyright (c) 2013-2016, Oracle and/or its affiliates.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #include "test_intersects.hpp"
  11. #include <boost/geometry/geometries/geometries.hpp>
  12. #include <boost/geometry/geometries/point_xy.hpp>
  13. #include <boost/geometry/util/rational.hpp>
  14. template <typename P1, typename P2>
  15. void test_intersects_polygon_polygon()
  16. {
  17. typedef bg::model::polygon<P1, false, false> poly_ccw_o1;
  18. typedef bg::model::polygon<P2, false, false> poly_ccw_o2;
  19. test_geometry<poly_ccw_o1, poly_ccw_o2>("POLYGON((1 1, 3 3, 2 5))", "POLYGON((0 0, 9 0, 9 9, 0 9),(5 5,5 8,8 8,8 5))", true);
  20. test_geometry<poly_ccw_o1, poly_ccw_o2>("POLYGON((6 6, 7 6, 7 7, 6 7))", "POLYGON((0 0, 9 0, 9 9, 0 9),(5 5,5 8,8 8,8 5))", false);
  21. test_geometry<poly_ccw_o1, poly_ccw_o2>("POLYGON((7 7, 9 7, 9 9, 7 9))", "POLYGON((0 0, 9 0, 9 9, 0 9),(5 5,5 8,8 8,8 5))", true);
  22. }
  23. template <typename P1, typename P2>
  24. void test_intersects_linestring_segment()
  25. {
  26. typedef bg::model::linestring<P1> ls;
  27. typedef bg::model::segment<P2> seg;
  28. test_geometry<ls, seg>("LINESTRING(1 1, 3 3, 2 5)", "SEGMENT(2 0, 2 6)", true);
  29. test_geometry<ls, seg>("LINESTRING(1 1, 3 3)", "SEGMENT(1 0, 1 1)", true);
  30. test_geometry<ls, seg>("LINESTRING(1 1, 3 3)", "SEGMENT(2 0, 2 2)", true);
  31. test_geometry<ls, seg>("LINESTRING(1 1, 3 3)", "SEGMENT(3 0, 4 1)", false);
  32. }
  33. template <typename P1, typename P2>
  34. void test_intersects_linestring_linestring()
  35. {
  36. typedef bg::model::linestring<P1> ls1;
  37. typedef bg::model::linestring<P2> ls2;
  38. test_geometry<ls1, ls2>("LINESTRING(0 0,2 0,3 0)", "LINESTRING(0 0,1 1,2 2)", true);
  39. test_geometry<ls1, ls2>("LINESTRING(0 0,2 0,3 0)", "LINESTRING(2 2,1 1,0 0)", true);
  40. test_geometry<ls1, ls2>("LINESTRING(3 0,2 0,0 0)", "LINESTRING(0 0,1 1,2 2)", true);
  41. test_geometry<ls1, ls2>("LINESTRING(3 0,2 0,0 0)", "LINESTRING(2 2,1 1,0 0)", true);
  42. test_geometry<ls1, ls2>("LINESTRING(0 0,2 0,3 0)", "LINESTRING(1 0,4 0,5 0)", true);
  43. test_geometry<ls1, ls2>("LINESTRING(1 0,2 0)", "LINESTRING(1 0,0 0)", true);
  44. }
  45. template <typename P1, typename P2>
  46. void test_intersects_linestring_polygon()
  47. {
  48. typedef bg::model::linestring<P1> ls;
  49. typedef bg::model::multi_linestring<ls> mls;
  50. typedef bg::model::polygon<P2> poly_cw_c;
  51. typedef bg::model::polygon<P2, false> poly_ccw_c;
  52. typedef bg::model::polygon<P2, false, false> poly_ccw_o;
  53. typedef bg::model::multi_polygon<poly_ccw_c> mpoly_ccw_c;
  54. test_geometry<ls, poly_ccw_c>("LINESTRING(1 1,2 2)", "POLYGON((0 0,10 0,10 10,0 10,0 0))", true);
  55. test_geometry<ls, poly_ccw_c>("LINESTRING(1 0,2 2)", "POLYGON((0 0,10 0,10 10,0 10,0 0))", true);
  56. test_geometry<ls, poly_ccw_c>("LINESTRING(11 0,12 12)", "POLYGON((0 0,10 0,10 10,0 10,0 0))", false);
  57. test_geometry<ls, poly_ccw_o>("LINESTRING(1 1, 3 3, 2 5)", "POLYGON((0 0, 9 0, 9 9, 0 9),(5 5,5 8,8 8,8 5))", true);
  58. test_geometry<ls, poly_ccw_o>("LINESTRING(6 6, 7 6, 7 7, 6 7)", "POLYGON((0 0, 9 0, 9 9, 0 9),(5 5,5 8,8 8,8 5))", false);
  59. test_geometry<ls, poly_ccw_o>("LINESTRING(7 7, 9 7, 9 9, 7 9)", "POLYGON((0 0, 9 0, 9 9, 0 9),(5 5,5 8,8 8,8 5))", true);
  60. test_geometry<poly_cw_c, ls>("POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))", "LINESTRING(-2 -2, 12 7)", true);
  61. test_geometry<poly_cw_c, ls>("POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))", "LINESTRING(5 5, 15 4)", true);
  62. test_geometry<poly_cw_c, ls>("POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))", "LINESTRING(7 6, 15 4)", true);
  63. test_geometry<poly_cw_c, ls>("POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))", "LINESTRING(6 2, 12 1)", true);
  64. // MULTI
  65. test_geometry<ls, mpoly_ccw_c>("LINESTRING(1 1,2 2)", "MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)))", true);
  66. test_geometry<mls, mpoly_ccw_c>("MULTILINESTRING((1 1,2 2))", "MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)))", true);
  67. }
  68. template <typename P1, typename P2>
  69. void test_intersects_linestring_ring()
  70. {
  71. typedef bg::model::linestring<P1> ls;
  72. typedef bg::model::multi_linestring<ls> mls;
  73. typedef bg::model::ring<P2, false> ring_ccw_c;
  74. test_geometry<ls, ring_ccw_c>("LINESTRING(1 1,2 2)", "POLYGON((0 0,10 0,10 10,0 10,0 0))", true);
  75. test_geometry<ls, ring_ccw_c>("LINESTRING(1 0,2 2)", "POLYGON((0 0,10 0,10 10,0 10,0 0))", true);
  76. test_geometry<ls, ring_ccw_c>("LINESTRING(11 0,12 12)", "POLYGON((0 0,10 0,10 10,0 10,0 0))", false);
  77. // MULTI
  78. test_geometry<mls, ring_ccw_c>("MULTILINESTRING((1 1,2 2))", "POLYGON((0 0,10 0,10 10,0 10,0 0))", true);
  79. }
  80. template <typename P1, typename P2>
  81. void test_intersects_ring_polygon()
  82. {
  83. typedef bg::model::ring<P1, false, false> ring_ccw_o;
  84. typedef bg::model::polygon<P2, false, false> poly_ccw_o;
  85. test_geometry<ring_ccw_o, poly_ccw_o>("POLYGON((1 1, 3 3, 2 5))", "POLYGON((0 0, 9 0, 9 9, 0 9),(5 5,5 8,8 8,8 5))", true);
  86. test_geometry<ring_ccw_o, poly_ccw_o>("POLYGON((6 6, 7 6, 7 7, 6 7))", "POLYGON((0 0, 9 0, 9 9, 0 9),(5 5,5 8,8 8,8 5))", false);
  87. test_geometry<ring_ccw_o, poly_ccw_o>("POLYGON((7 7, 9 7, 9 9, 7 9))", "POLYGON((0 0, 9 0, 9 9, 0 9),(5 5,5 8,8 8,8 5))", true);
  88. test_geometry<ring_ccw_o, poly_ccw_o>("POLYGON((6 6,7 6,7 7,6 7))", "POLYGON((0 0, 9 0, 9 9, 0 9),(5 5,5 8,8 8,8 5))", false);
  89. }
  90. template <typename P1, typename P2>
  91. void test_intersects_point_linestring()
  92. {
  93. typedef bg::model::linestring<P2> ls;
  94. typedef bg::model::multi_linestring<ls> mls;
  95. test_geometry<P1, ls>("POINT(0 0)", "LINESTRING(0 0,2 2,4 0)", true);
  96. test_geometry<P1, ls>("POINT(1 1)", "LINESTRING(0 0,2 2,4 0)", true);
  97. test_geometry<P1, ls>("POINT(1 0)", "LINESTRING(0 0,2 2,4 0)", false);
  98. // MULTI
  99. test_geometry<P1, mls>("POINT(0 0)", "MULTILINESTRING((0 0,2 2,4 0))", true);
  100. }
  101. template <typename P1, typename P2>
  102. void test_intersects_point_segment()
  103. {
  104. typedef bg::model::segment<P2> seg;
  105. test_geometry<P1, seg>("POINT(0 0)", "LINESTRING(0 0,2 2)", true);
  106. test_geometry<P1, seg>("POINT(1 1)", "LINESTRING(0 0,2 2)", true);
  107. test_geometry<P1, seg>("POINT(1 0)", "LINESTRING(0 0,2 2)", false);
  108. }
  109. template <typename P1, typename P2>
  110. void test_multi_linestring_polygon()
  111. {
  112. typedef bg::model::linestring<P1> ls;
  113. typedef bg::model::multi_linestring<ls> mls;
  114. typedef bg::model::polygon<P2> poly;
  115. test_geometry<mls, poly>("MULTILINESTRING((11 11, 20 20),(5 7, 4 1))",
  116. "POLYGON((0 0,0 10,10 10,10 0,0 0),(2 2,4 2,4 4,2 4,2 2))",
  117. true);
  118. test_geometry<mls, poly>("MULTILINESTRING((10 0, 18 12),(2 2,2 1))",
  119. "POLYGON((5 0,0 -5,-5 0,0 5,5 0))",
  120. true);
  121. }
  122. template <typename P1, typename P2>
  123. void test_multi_polygon_polygon()
  124. {
  125. typedef bg::model::polygon<P1> poly1;
  126. typedef bg::model::multi_polygon<poly1> mpoly;
  127. typedef bg::model::polygon<P2> poly2;
  128. test_geometry<mpoly, poly2>("MULTIPOLYGON(((11 11,11 20,20 20,20 11,11 11)),((5 5,5 6,6 6,6 5,5 5)))",
  129. "POLYGON((0 0,0 10,10 10,10 0,0 0),(2 2,4 2,4 4,2 4,2 2))",
  130. true);
  131. }
  132. template <typename P1, typename P2>
  133. void test_point_polygon()
  134. {
  135. typedef bg::model::ring<P2> ring;
  136. typedef bg::model::polygon<P2> poly;
  137. test_geometry<P1, ring>(
  138. "POINT(0 0)",
  139. "POLYGON((0 0,3 3,3 3,4 1))",
  140. true);
  141. test_geometry<P1, poly>(
  142. "POINT(0 0)",
  143. "POLYGON((0 0,3 3,3 3,4 1))",
  144. true);
  145. test_geometry<ring, P1>(
  146. "POLYGON((0 0,3 3,3 3,4 1))",
  147. "POINT(0 0)",
  148. true);
  149. test_geometry<poly, P1>(
  150. "POLYGON((0 0,3 3,3 3,4 1))",
  151. "POINT(0 0)",
  152. true);
  153. }
  154. template <typename P1, typename P2>
  155. void test_all()
  156. {
  157. test_intersects_point_segment<P1, P2>();
  158. test_intersects_point_linestring<P1, P2>();
  159. test_intersects_polygon_polygon<P1, P2>();
  160. test_intersects_linestring_polygon<P1, P2>();
  161. test_intersects_linestring_ring<P1, P2>();
  162. test_intersects_linestring_segment<P1, P2>();
  163. test_intersects_linestring_linestring<P1, P2>();
  164. test_intersects_ring_polygon<P1, P2>();
  165. test_multi_linestring_polygon<P1, P2>();
  166. test_multi_polygon_polygon<P1, P2>();
  167. test_point_polygon<P1, P2>();
  168. }
  169. template <typename P>
  170. void test_all()
  171. {
  172. test_all<P, P>();
  173. }
  174. int test_main( int , char* [] )
  175. {
  176. test_all<bg::model::d2::point_xy<double> >();
  177. test_all<bg::model::d2::point_xy<double>, bg::model::point<double, 2, bg::cs::cartesian> >();
  178. #if ! defined(BOOST_GEOMETRY_RESCALE_TO_ROBUST)
  179. test_all<bg::model::d2::point_xy<boost::rational<int> > >();
  180. #endif
  181. #if defined(HAVE_TTMATH)
  182. test_all<bg::model::d2::point_xy<ttmath_big> >();
  183. #endif
  184. return 0;
  185. }