disjoint_seg_box.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // Boost.Geometry
  2. // Copyright (c) 2016-2019 Oracle and/or its affiliates.
  3. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  4. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #include <boost/geometry/geometries/box.hpp>
  9. #include <boost/geometry/geometries/point_xy.hpp>
  10. #include <boost/geometry/geometries/geometries.hpp>
  11. #include <boost/geometry/geometries/adapted/c_array.hpp>
  12. #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
  13. #include <test_common/test_point.hpp>
  14. #include <boost/geometry/formulas/andoyer_inverse.hpp>
  15. #include <boost/geometry/formulas/thomas_inverse.hpp>
  16. #include <boost/geometry/formulas/vincenty_inverse.hpp>
  17. #include <boost/geometry/strategies/strategies.hpp>
  18. #include <boost/geometry/algorithms/disjoint.hpp>
  19. #include <geometry_test_common.hpp>
  20. #include "test_disjoint_seg_box.hpp"
  21. namespace bg = boost::geometry;
  22. //Tests for disjoint(point, box), disjoint(box, box) and disjoint(segment, box)
  23. template <typename P>
  24. void disjoint_tests_1()
  25. {
  26. test_disjoint<bg::model::box<P>, P>("BOX(1 1,3 3)", "POINT(4 4)", true);
  27. test_disjoint<bg::model::box<P>, P>("BOX(1 1,3 3)", "POINT(2 2)", false);
  28. test_disjoint<bg::model::box<P>, P>("BOX(1 1,3 3)", "POINT(3 3)", false);
  29. test_disjoint<bg::model::box<P>, P>("BOX(1 1,3 3)", "POINT(2 3)", false);
  30. test_disjoint<bg::model::box<P>, bg::model::box<P> >("BOX(1 1,3 3)",
  31. "BOX(1 4,5 5)",
  32. true);
  33. test_disjoint<bg::model::box<P>, bg::model::box<P> >("BOX(1 1,3 3)",
  34. "BOX(2 2,4 4)",
  35. false);
  36. test_disjoint<bg::model::box<P>, bg::model::box<P> >("BOX(1 1,3 3)",
  37. "BOX(3 3,4 4)",
  38. false);
  39. test_disjoint<bg::model::box<P>, bg::model::box<P> >("BOX(1 1,3 3)",
  40. "BOX(2 3,4 4)",
  41. false);
  42. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(1 1,3 3)",
  43. "SEGMENT(1 4, 5 5)",
  44. true);
  45. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(1 1,3 3)",
  46. "SEGMENT(3 3, 5 5)",
  47. false);
  48. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(1 1,3 3)",
  49. "SEGMENT(1 1, 4 1)",
  50. false);
  51. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(1 1,3 3)",
  52. "SEGMENT(1 2, 5 5)",
  53. false);
  54. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(1 1,3 3)",
  55. "SEGMENT(1 2, 3 2)",
  56. false);
  57. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(1 1,3 3)",
  58. "SEGMENT(0 0, 4 0)",
  59. true);
  60. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(1 1,3 3)",
  61. "SEGMENT(2 2, 4 4)",
  62. false);
  63. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(1 1,3 3)",
  64. "SEGMENT(4 4, 2 2)",
  65. false);
  66. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(1 1,3 3)",
  67. "SEGMENT(1.5 1.5, 2 2)",
  68. false);
  69. }
  70. template <typename P>
  71. void disjoint_tests_2(bool expected_result)
  72. {
  73. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(1 1,3 3)",
  74. "SEGMENT(1 0.999, 10 0.999)",
  75. expected_result);
  76. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(1 1,3 3)",
  77. "SEGMENT(10 0.999, 1 0.999)",
  78. expected_result);
  79. }
  80. template <typename P>
  81. void disjoint_tests_3(bool expected_result)
  82. {
  83. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(3 4.42, 100 5)",
  84. "SEGMENT(2 2.9, 100 2.9)",
  85. expected_result);
  86. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(3 4.42, 100 5)",
  87. "SEGMENT(100 2.9, 2 2.9)",
  88. expected_result);
  89. }
  90. template <typename P>
  91. void disjoint_tests_4(bool expected_result)
  92. {
  93. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(1 1,3 3)",
  94. "SEGMENT(0 0.99999999, 2 0.99999999)",
  95. expected_result);
  96. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(1 1,3 3)",
  97. "SEGMENT(2 0.99999999, 0 0.99999999)",
  98. expected_result);
  99. }
  100. template <typename P, typename CT>
  101. void disjoint_tests_with_strategy(bool expected_result)
  102. {
  103. bg::strategy::disjoint::segment_box_geographic
  104. <
  105. bg::strategy::andoyer,
  106. bg::srs::spheroid<CT>,
  107. CT
  108. > geographic_andoyer;
  109. bg::strategy::disjoint::segment_box_geographic
  110. <
  111. bg::strategy::thomas,
  112. bg::srs::spheroid<CT>,
  113. CT
  114. > geographic_thomas;
  115. bg::strategy::disjoint::segment_box_geographic
  116. <
  117. bg::strategy::vincenty,
  118. bg::srs::spheroid<CT>,
  119. CT
  120. > geographic_vincenty;
  121. test_disjoint_strategy<bg::model::box<P>, bg::model::segment<P> >
  122. ("BOX(1 1,3 3)", "SEGMENT(1 0.999, 10 0.999)",
  123. expected_result, geographic_andoyer);
  124. test_disjoint_strategy<bg::model::box<P>, bg::model::segment<P> >
  125. ("BOX(1 1,3 3)", "SEGMENT(1 0.999, 10 0.999)",
  126. expected_result, geographic_thomas);
  127. test_disjoint_strategy<bg::model::box<P>, bg::model::segment<P> >
  128. ("BOX(1 1,3 3)", "SEGMENT(1 0.999, 10 0.999)",
  129. expected_result, geographic_vincenty);
  130. }
  131. template <typename P>
  132. void disjoint_tests_sph_geo()
  133. {
  134. //Case A: box intersects without containing the vertex
  135. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(0 6, 120 7)",
  136. "SEGMENT(0 5, 120 5)",
  137. false);
  138. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(0 -6, 120 -7)",
  139. "SEGMENT(0 -5, 120 -5)",
  140. false);
  141. //Case B: box intersects and contains the vertex
  142. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(0 9, 120 10)",
  143. "SEGMENT(0 5, 120 5)",
  144. false);
  145. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(0 -10, 120 -9)",
  146. "SEGMENT(0 -5, 120 -5)",
  147. false);
  148. //Case C: bounding boxes disjoint
  149. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(0 10, 10 20)",
  150. "SEGMENT(0 5, 120 5)",
  151. true);
  152. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(0 -20, 10 -10)",
  153. "SEGMENT(0 -5, 120 -5)",
  154. true);
  155. //Case D: bounding boxes intersect but box segment are disjoint
  156. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(0 9, 0.1 20)",
  157. "SEGMENT(0 5, 120 5)",
  158. true);
  159. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(0 -20, 0.1 -9)",
  160. "SEGMENT(0 -5, 120 -5)",
  161. true);
  162. //Case E: geodesic intersects box but box segment are disjoint
  163. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(121 0, 122 10)",
  164. "SEGMENT(0 5, 120 5)",
  165. true);
  166. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(121 -10, 122 0)",
  167. "SEGMENT(0 -5, 120 -5)",
  168. true);
  169. //Case F: segment crosses box
  170. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(100 0, 110 20)",
  171. "SEGMENT(0 5, 120 5)",
  172. false);
  173. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(100 -20, 110 0)",
  174. "SEGMENT(0 -5, 120 -5)",
  175. false);
  176. //Case G: box contains one segment endpoint
  177. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(110 0, 130 10)",
  178. "SEGMENT(0 5, 120 5)",
  179. false);
  180. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(110 -10, 130 0)",
  181. "SEGMENT(0 -5, 120 -5)",
  182. false);
  183. //Case H: box below segment
  184. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(50 0, 70 6)",
  185. "SEGMENT(0 5, 120 5)",
  186. true);
  187. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(50 -6, 70 0)",
  188. "SEGMENT(0 -5, 120 -5)",
  189. true);
  190. //Case I: box contains segment
  191. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(-10 0, 130 10)",
  192. "SEGMENT(0 5, 120 5)",
  193. false);
  194. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(-10 -10, 130 0)",
  195. "SEGMENT(0 -5, 120 -5)",
  196. false);
  197. //ascending segment
  198. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(0 10, 120 10.1)",
  199. "SEGMENT(0 5, 120 5.1)",
  200. false);
  201. //descending segment
  202. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(0 9.8, 120 10)",
  203. "SEGMENT(0 5, 120 4.9)",
  204. false);
  205. //ascending segment both hemispheres
  206. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(100 5, 120 6)",
  207. "SEGMENT(0 -1, 120 4.9)",
  208. false);
  209. //descending segment both hemispheres
  210. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(0 5, 20 6)",
  211. "SEGMENT(0 4.9, 120 -1)",
  212. false);
  213. //https://github.com/boostorg/geometry/issues/579
  214. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(10 10,20 20)",
  215. "SEGMENT(12 2,12 1)",
  216. true);
  217. test_disjoint<bg::model::box<P>, bg::model::segment<P> >("BOX(10 10,20 20)",
  218. "SEGMENT(12 1,12 2)",
  219. true);
  220. }
  221. template <typename CT>
  222. void test_all()
  223. {
  224. typedef bg::model::d2::point_xy<CT> point;
  225. typedef bg::model::point<CT, 2,
  226. bg::cs::spherical_equatorial<bg::degree> > sph_point;
  227. typedef bg::model::point<CT, 2,
  228. bg::cs::geographic<bg::degree> > geo_point;
  229. disjoint_tests_1<point>();
  230. disjoint_tests_1<sph_point>();
  231. disjoint_tests_1<geo_point>();
  232. disjoint_tests_2<point>(true);
  233. disjoint_tests_2<sph_point>(false);
  234. disjoint_tests_2<geo_point>(false);
  235. //illustrate difference between spherical and geographic computation on same data
  236. disjoint_tests_3<point>(true);
  237. disjoint_tests_3<sph_point>(true);
  238. disjoint_tests_3<geo_point>(false);
  239. disjoint_tests_4<sph_point>(false);
  240. disjoint_tests_4<geo_point>(false);
  241. disjoint_tests_with_strategy<geo_point, CT>(false);
  242. disjoint_tests_sph_geo<sph_point>();
  243. disjoint_tests_sph_geo<geo_point>();
  244. }
  245. int test_main( int , char* [] )
  246. {
  247. test_all<float>();
  248. test_all<double>();
  249. #ifdef HAVE_TTMATH
  250. common_tests<bg::model::d2::point_xy<ttmath_big> >();
  251. common_tests<bg::model::point<ttmath_big, 3, bg::cs::cartesian> >();
  252. #endif
  253. return 0;
  254. }