touches.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. //
  3. // Copyright (c) 2012-2015 Barend Gehrels, Amsterdam, the Netherlands.
  4. // This file was modified by Oracle on 2013, 2014. 2015.
  5. // Modifications copyright (c) 2013-2015, 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_touches.hpp"
  11. template <typename P>
  12. void test_all()
  13. {
  14. typedef bg::model::multi_point<P> mpoint;
  15. typedef bg::model::ring<P> ring;
  16. typedef bg::model::polygon<P> polygon;
  17. typedef bg::model::linestring<P> linestring;
  18. typedef bg::model::multi_polygon<polygon> mpolygon;
  19. typedef bg::model::multi_linestring<linestring> mlinestring;
  20. // Touching at corner
  21. test_touches<polygon, polygon>
  22. (
  23. "POLYGON((0 0,0 100,100 100,100 0,0 0))",
  24. "POLYGON((100 100,100 200,200 200, 200 100,100 100))",
  25. true
  26. );
  27. // Intersecting at corner
  28. test_touches<polygon, polygon>
  29. (
  30. "POLYGON((0 0,0 100,101 101,100 0,0 0))",
  31. "POLYGON((100 100,100 200,200 200, 200 100,100 100))",
  32. false
  33. );
  34. // Touching at side (interior of a segment)
  35. test_touches<polygon, polygon>
  36. (
  37. "POLYGON((0 0,0 100,100 100,100 0,0 0))",
  38. "POLYGON((200 0,100 50,200 100,200 0))",
  39. true
  40. );
  41. // Touching at side (partly collinear)
  42. test_touches<polygon, polygon>
  43. (
  44. "POLYGON((0 0,0 100,100 100,100 0,0 0))",
  45. "POLYGON((200 20,100 20,100 80,200 80,200 20))",
  46. true
  47. );
  48. // Completely equal
  49. test_touches<polygon, polygon>
  50. (
  51. "POLYGON((0 0,0 100,100 100,100 0,0 0))",
  52. "POLYGON((0 0,0 100,100 100,100 0,0 0))",
  53. false
  54. );
  55. // Spatially equal
  56. test_touches<polygon, polygon>
  57. (
  58. "POLYGON((0 0,0 100,100 100,100 0,0 0))",
  59. "POLYGON((0 0,0 100,100 100,100 80,100 20,100 0,0 0))",
  60. false
  61. );
  62. // Spatially equal (without equal segments)
  63. test_touches<polygon, polygon>
  64. (
  65. "POLYGON((0 0,0 100,100 100,100 0,0 0))",
  66. "POLYGON((0 0,0 50,0 100,50 100,100 100,100 50,100 0,50 0,0 0))",
  67. false
  68. );
  69. // Touching at side (opposite equal)
  70. test_touches<polygon, polygon>
  71. (
  72. "POLYGON((0 0,0 100,100 100,100 0,0 0))",
  73. "POLYGON((200 0,100 0,100 100,200 100,200 0))",
  74. true
  75. );
  76. // Touching at side (opposite equal - but with real "equal" turning point)
  77. test_touches<polygon, polygon>
  78. (
  79. "POLYGON((0 0,0 100,100 100,100 80,100 20,100 0,0 0))",
  80. "POLYGON((200 0,100 0,100 20,100 80,100 100,200 100,200 0))",
  81. true
  82. );
  83. // First partly collinear to side, than overlapping
  84. test_touches<polygon, polygon>
  85. (
  86. "POLYGON((0 0,0 100,100 100,100 0,0 0))",
  87. "POLYGON((200 20,100 20,100 50,50 50,50 80,100 80,200 80,200 20))",
  88. false
  89. );
  90. // Touching interior (= no touch)
  91. test_touches<polygon, polygon>
  92. (
  93. "POLYGON((0 0,0 100,100 100,100 0,0 0))",
  94. "POLYGON((20 20,20 80,100 50,20 20))",
  95. false
  96. );
  97. // Fitting in hole
  98. test_touches<polygon, polygon>
  99. (
  100. "POLYGON((0 0,0 100,100 100,100 0,0 0),(40 40,60 40,60 60,40 60,40 40))",
  101. "POLYGON((40 40,40 60,60 60,60 40,40 40))",
  102. true
  103. );
  104. // mysql 21873343
  105. test_touches<polygon, polygon>
  106. (
  107. "POLYGON((0 0,0 10,10 10,10 0,0 0), (0 8, 8 5, 8 8, 0 8))",
  108. "POLYGON((0 8,-8 5,-8 8,0 8))",
  109. true
  110. );
  111. test_touches<polygon, polygon>
  112. (
  113. "POLYGON((0 0,0 10,10 10,10 0,0 0), (0 6, 6 3, 6 6, 0 6))",
  114. "POLYGON((0 6,-6 3,-6 6,0 6))",
  115. true
  116. );
  117. // Point-Polygon
  118. test_touches<P, ring>("POINT(40 50)", "POLYGON((40 40,40 60,60 60,60 40,40 40))", true);
  119. test_touches<P, polygon>("POINT(40 50)", "POLYGON((40 40,40 60,60 60,60 40,40 40))", true);
  120. test_touches<P, polygon>("POINT(60 60)", "POLYGON((40 40,40 60,60 60,60 40,40 40))", true);
  121. test_touches<P, polygon>("POINT(50 50)", "POLYGON((40 40,40 60,60 60,60 40,40 40))", false);
  122. test_touches<P, polygon>("POINT(30 50)", "POLYGON((40 40,40 60,60 60,60 40,40 40))", false);
  123. // Point-MultiPolygon
  124. test_touches<P, mpolygon>("POINT(40 50)", "MULTIPOLYGON(((40 40,40 60,60 60,60 40,40 40)),((0 0,0 10,10 10,10 0)))", true);
  125. // Point-Linestring
  126. test_touches<P, linestring>("POINT(0 0)", "LINESTRING(0 0, 2 2, 10 2)", true);
  127. test_touches<P, linestring>("POINT(2 2)", "LINESTRING(0 0, 2 2, 10 2)", false);
  128. test_touches<P, linestring>("POINT(1 1)", "LINESTRING(0 0, 2 2, 10 2)", false);
  129. test_touches<P, linestring>("POINT(5 5)", "LINESTRING(0 0, 2 2, 10 2)", false);
  130. // Point-MultiLinestring
  131. test_touches<P, mlinestring>("POINT(0 0)", "MULTILINESTRING((0 0, 2 2, 10 2),(5 5, 6 6))", true);
  132. test_touches<P, mlinestring>("POINT(0 0)", "MULTILINESTRING((0 0, 2 2, 10 2),(0 0, 6 6))", false);
  133. // MultiPoint-Polygon
  134. test_touches<mpoint, ring>("MULTIPOINT(40 50, 30 50)", "POLYGON((40 40,40 60,60 60,60 40,40 40))", true);
  135. test_touches<mpoint, polygon>("MULTIPOINT(40 50, 50 50)", "POLYGON((40 40,40 60,60 60,60 40,40 40))", false);
  136. // Linestring-Linestring
  137. test_touches<linestring, linestring>("LINESTRING(0 0,2 0)", "LINESTRING(0 0,0 2)", true);
  138. test_touches<linestring, linestring>("LINESTRING(0 0,2 0)", "LINESTRING(2 0,2 2)", true);
  139. test_touches<linestring, linestring>("LINESTRING(0 0,2 0)", "LINESTRING(0 2,0 0)", true);
  140. test_touches<linestring, linestring>("LINESTRING(0 0,2 0)", "LINESTRING(2 2,2 0)", true);
  141. test_touches<linestring, linestring>("LINESTRING(2 0,0 0)", "LINESTRING(0 0,0 2)", true);
  142. test_touches<linestring, linestring>("LINESTRING(2 0,0 0)", "LINESTRING(2 0,2 2)", true);
  143. test_touches<linestring, linestring>("LINESTRING(2 0,0 0)", "LINESTRING(0 2,0 0)", true);
  144. test_touches<linestring, linestring>("LINESTRING(2 0,0 0)", "LINESTRING(2 2,2 0)", true);
  145. test_touches<linestring, linestring>("LINESTRING(0 0,2 0)", "LINESTRING(1 0,1 1)", true);
  146. test_touches<linestring, linestring>("LINESTRING(0 0,2 0)", "LINESTRING(1 1,1 0)", true);
  147. test_touches<linestring, linestring>("LINESTRING(2 0,0 0)", "LINESTRING(1 0,1 1)", true);
  148. test_touches<linestring, linestring>("LINESTRING(2 0,0 0)", "LINESTRING(1 1,1 0)", true);
  149. test_touches<linestring, linestring>("LINESTRING(0 0,10 0)", "LINESTRING(0 0,5 5,10 0)", true);
  150. test_touches<linestring, linestring>("LINESTRING(0 0,10 10)", "LINESTRING(0 0,0 5,10 5)", false);
  151. test_touches<linestring, linestring>("LINESTRING(0 5,5 6,10 5)", "LINESTRING(0 7,5 6,10 7)", false);
  152. test_touches<linestring, linestring>("LINESTRING(0 5,5 6,10 5)", "LINESTRING(10 7,5 6,0 7)", false);
  153. test_touches<linestring, linestring>("LINESTRING(10 5,5 6,0 5)", "LINESTRING(0 7,5 6,10 7)", false);
  154. test_touches<linestring, linestring>("LINESTRING(10 5,5 6,0 5)", "LINESTRING(10 7,5 6,0 7)", false);
  155. test_touches<linestring, linestring>("LINESTRING(0 0,1 1,2 2)", "LINESTRING(2 0,2 2,1 2,1 1)", true);
  156. test_touches<linestring, linestring>("LINESTRING(2 2,1 1,0 0)", "LINESTRING(2 0,2 2,1 2,1 1)", true);
  157. test_touches<linestring, linestring>("LINESTRING(0 0,1 1,2 2)", "LINESTRING(1 1,1 2,2 2,2 0)", true);
  158. test_touches<linestring, linestring>("LINESTRING(2 2,1 1,0 0)", "LINESTRING(1 1,1 2,2 2,2 0)", true);
  159. test_touches<linestring, linestring>("LINESTRING(0 0,1 1,0 1)", "LINESTRING(1 1,2 2,1 2,1 1)", false);
  160. test_touches<linestring, mlinestring>("LINESTRING(0 0,1 1,0 1)", "MULTILINESTRING((1 1,2 2),(1 2,1 1))", false);
  161. //Linestring-Polygon
  162. test_touches<linestring, polygon>("LINESTRING(10 0,15 5,10 10,5 15,5 10,0 10,5 15,5 10)", "POLYGON((0 0,0 10,10 10,10 0,0 0))", true);
  163. test_touches<linestring, polygon>("LINESTRING(5 10,5 15,0 10,5 10,5 15,10 10,15 5,10 0)", "POLYGON((0 0,0 10,10 10,10 0,0 0))", true);
  164. test_touches<linestring, polygon>("LINESTRING(5 10,5 15,0 10,5 10,5 15,10 10,5 5,10 0)", "POLYGON((0 0,0 10,10 10,10 0,0 0))", false);
  165. test_touches<linestring, ring>("LINESTRING(0 15,5 5)", "POLYGON((0 0,0 10,10 10,10 0,0 0))", false);
  166. test_touches<linestring, polygon>("LINESTRING(0 15,5 5)", "POLYGON((0 0,0 10,10 10,10 0,0 0))", false);
  167. test_touches<linestring, polygon>("LINESTRING(0 15,5 10,5 5)", "POLYGON((0 0,0 10,10 10,10 0,0 0))", false);
  168. test_touches<linestring, polygon>("LINESTRING(10 15,5 10,0 5)", "POLYGON((0 0,0 10,10 10,10 0,0 0))", false);
  169. test_touches<linestring, polygon>("LINESTRING(0 0,3 3)", "POLYGON((0 0,0 10,10 10,10 0,0 0),(0 0,9 1,9 9,1 9,0 0))", true);
  170. test_touches<linestring, mpolygon>("LINESTRING(-1 -1,3 3)", "MULTIPOLYGON(((0 0,0 10,10 10,10 0,0 0),(0 0,9 1,9 9,1 9,0 0)))", true);
  171. test_touches<mlinestring, mpolygon>("MULTILINESTRING((0 0,11 11))", "MULTIPOLYGON(((0 0,0 10,10 10,10 0,0 0),(0 0,9 1,9 9,1 9,0 0)))", false);
  172. }
  173. int test_main( int , char* [] )
  174. {
  175. test_all<bg::model::d2::point_xy<double> >();
  176. #if defined(HAVE_TTMATH)
  177. test_all<bg::model::d2::point_xy<ttmath_big> >();
  178. #endif
  179. return 0;
  180. }
  181. /*
  182. with viewy as
  183. (
  184. select geometry::STGeomFromText('POLYGON((0 0,0 100,100 100,100 0,0 0))',0) as p
  185. , geometry::STGeomFromText('POLYGON((200 0,100 50,200 100,200 0))',0) as q
  186. )
  187. -- select p from viewy union all select q from viewy
  188. select p.STTouches(q) from viewy
  189. */