segment_intersection_sph.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // Boost.Geometry
  2. // Unit Test
  3. // Copyright (c) 2016-2018, Oracle and/or its affiliates.
  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. #define BOOST_GEOMETRY_TEST_GEO_INTERSECTION_TEST_SIMILAR
  9. #include "segment_intersection_sph.hpp"
  10. #include <boost/geometry/strategies/spherical/intersection.hpp>
  11. template <typename S, typename P>
  12. void test_spherical_strategy(std::string const& s1_wkt, std::string const& s2_wkt,
  13. char m, std::size_t expected_count,
  14. std::string const& ip0_wkt = "", std::string const& ip1_wkt = "")
  15. {
  16. bg::strategy::intersection::spherical_segments<> strategy;
  17. test_strategy<S, S, P>(s1_wkt, s2_wkt, strategy, m, expected_count, ip0_wkt, ip1_wkt);
  18. }
  19. template <typename T>
  20. void test_spherical()
  21. {
  22. typedef bg::model::point<double, 2, bg::cs::spherical_equatorial<bg::degree> > point_t;
  23. typedef bg::model::segment<point_t> segment_t;
  24. // crossing X
  25. test_spherical_strategy<segment_t, point_t>(
  26. "SEGMENT(-45 -45, 45 45)", "SEGMENT(-45 45, 45 -45)", 'i', 1, "POINT(0 0)");
  27. test_spherical_strategy<segment_t, point_t>(
  28. "SEGMENT(-45 -45, 45 45)", "SEGMENT(45 -45, -45 45)", 'i', 1, "POINT(0 0)");
  29. test_spherical_strategy<segment_t, point_t>(
  30. "SEGMENT(45 45, -45 -45)", "SEGMENT(-45 45, 45 -45)", 'i', 1, "POINT(0 0)");
  31. test_spherical_strategy<segment_t, point_t>(
  32. "SEGMENT(45 45, -45 -45)", "SEGMENT(45 -45, -45 45)", 'i', 1, "POINT(0 0)");
  33. // crossing X
  34. test_spherical_strategy<segment_t, point_t>(
  35. "SEGMENT(-1 -1, 1 1)", "SEGMENT(-1 1, 1 -1)", 'i', 1, "POINT(0 0)");
  36. test_spherical_strategy<segment_t, point_t>(
  37. "SEGMENT(-1 -1, 1 1)", "SEGMENT(1 -1, -1 1)", 'i', 1, "POINT(0 0)");
  38. test_spherical_strategy<segment_t, point_t>(
  39. "SEGMENT(1 1, -1 -1)", "SEGMENT(-1 1, 1 -1)", 'i', 1, "POINT(0 0)");
  40. test_spherical_strategy<segment_t, point_t>(
  41. "SEGMENT(1 1, -1 -1)", "SEGMENT(1 -1, -1 1)", 'i', 1, "POINT(0 0)");
  42. // equal
  43. // //
  44. test_spherical_strategy<segment_t, point_t>(
  45. "SEGMENT(-45 -45, 45 45)", "SEGMENT(-45 -45, 45 45)", 'e', 2, "POINT(-45 -45)", "POINT(45 45)");
  46. // //
  47. test_spherical_strategy<segment_t, point_t>(
  48. "SEGMENT(-45 -45, 45 45)", "SEGMENT(45 45, -45 -45)", 'e', 2, "POINT(-45 -45)", "POINT(45 45)");
  49. // starting outside s1
  50. // /
  51. // |
  52. test_spherical_strategy<segment_t, point_t>(
  53. "SEGMENT(-1 -1, 1 1)", "SEGMENT(-2 -2, -1 -1)", 'a', 1, "POINT(-1 -1)");
  54. // /
  55. // /|
  56. test_spherical_strategy<segment_t, point_t>(
  57. "SEGMENT(-1 -1, 1 1)", "SEGMENT(-2 -2, 0 0)", 'm', 1, "POINT(0 0)");
  58. // /|
  59. // / |
  60. test_spherical_strategy<segment_t, point_t>(
  61. "SEGMENT(-1 -1, 1 1)", "SEGMENT(-2 -2, 1 1)", 't', 1, "POINT(1 1)");
  62. // |/
  63. // /|
  64. test_spherical_strategy<segment_t, point_t>(
  65. "SEGMENT(-1 -1, 1 1)", "SEGMENT(-2 -2, 2 2)", 'i', 1, "POINT(0 0)");
  66. // ------
  67. // ------
  68. test_spherical_strategy<segment_t, point_t>(
  69. "SEGMENT(-1 0, 1 0)", "SEGMENT(-2 0, -1 0)", 'a', 1, "POINT(-1 0)");
  70. // ------
  71. // ------
  72. test_spherical_strategy<segment_t, point_t>(
  73. "SEGMENT(-1 0, 1 0)", "SEGMENT(-2 0, 0 0)", 'c', 2, "POINT(-1 0)", "POINT(0 0)");
  74. // ------
  75. // ---------
  76. test_spherical_strategy<segment_t, point_t>(
  77. "SEGMENT(-1 0, 1 0)", "SEGMENT(-2 0, 1 0)", 'c', 2, "POINT(-1 0)", "POINT(1 0)");
  78. // ------
  79. // ------------
  80. test_spherical_strategy<segment_t, point_t>(
  81. "SEGMENT(-1 0, 1 0)", "SEGMENT(-2 0, 2 0)", 'c', 2, "POINT(-1 0)", "POINT(1 0)");
  82. // starting at s1
  83. // /
  84. // //
  85. test_spherical_strategy<segment_t, point_t>(
  86. "SEGMENT(-1 -1, 1 1)", "SEGMENT(-1 -1, 0 0)", 'c', 2, "POINT(-1 -1)", "POINT(0 0)");
  87. // //
  88. // //
  89. test_spherical_strategy<segment_t, point_t>(
  90. "SEGMENT(-1 -1, 1 1)", "SEGMENT(-1 -1, 1 1)", 'e', 2, "POINT(-1 -1)", "POINT(1 1)");
  91. // | /
  92. // |/
  93. test_spherical_strategy<segment_t, point_t>(
  94. "SEGMENT(-1 -1, 1 1)", "SEGMENT(-1 -1, 2 2)", 'f', 1, "POINT(-1 -1)");
  95. // ------
  96. // ---
  97. test_spherical_strategy<segment_t, point_t>(
  98. "SEGMENT(-1 0, 1 0)", "SEGMENT(-1 0, 0 0)", 'c', 2, "POINT(-1 0)", "POINT(0 0)");
  99. // ------
  100. // ------
  101. test_spherical_strategy<segment_t, point_t>(
  102. "SEGMENT(-1 0, 1 0)", "SEGMENT(-1 0, 1 0)", 'e', 2, "POINT(-1 0)", "POINT(1 0)");
  103. // ------
  104. // ---------
  105. test_spherical_strategy<segment_t, point_t>(
  106. "SEGMENT(-1 0, 1 0)", "SEGMENT(-1 0, 2 0)", 'c', 2, "POINT(-1 0)", "POINT(1 0)");
  107. // starting inside
  108. // //
  109. // /
  110. test_spherical_strategy<segment_t, point_t>(
  111. "SEGMENT(-1 -1, 1 1)", "SEGMENT(0 0, 1 1)", 'c', 2, "POINT(0 0)", "POINT(1 1)");
  112. // |/
  113. // /
  114. test_spherical_strategy<segment_t, point_t>(
  115. "SEGMENT(-1 -1, 1 1)", "SEGMENT(0 0, 2 2)", 's', 1, "POINT(0 0)");
  116. // ------
  117. // ---
  118. test_spherical_strategy<segment_t, point_t>(
  119. "SEGMENT(-1 0, 1 0)", "SEGMENT(0 0, 1 0)", 'c', 2, "POINT(0 0)", "POINT(1 0)");
  120. // ------
  121. // ------
  122. test_spherical_strategy<segment_t, point_t>(
  123. "SEGMENT(-1 0, 1 0)", "SEGMENT(0 0, 2 0)", 'c', 2, "POINT(0 0)", "POINT(1 0)");
  124. // starting at p2
  125. // |
  126. // /
  127. test_spherical_strategy<segment_t, point_t>(
  128. "SEGMENT(-1 -1, 1 1)", "SEGMENT(1 1, 2 2)", 'a', 1, "POINT(1 1)");
  129. // ------
  130. // ---
  131. test_spherical_strategy<segment_t, point_t>(
  132. "SEGMENT(-1 0, 1 0)", "SEGMENT(1 0, 2 0)", 'a', 1, "POINT(1 0)");
  133. // disjoint, crossing
  134. // /
  135. // |
  136. test_spherical_strategy<segment_t, point_t>(
  137. "SEGMENT(-1 -1, 1 1)", "SEGMENT(-3 -3, -2 -2)", 'd', 0);
  138. // |
  139. // /
  140. test_spherical_strategy<segment_t, point_t>(
  141. "SEGMENT(-1 -1, 1 1)", "SEGMENT(2 2, 3 3)", 'd', 0);
  142. // disjoint, collinear
  143. // ------
  144. // ------
  145. test_spherical_strategy<segment_t, point_t>(
  146. "SEGMENT(-1 0, 1 0)", "SEGMENT(-3 0, -2 0)", 'd', 0);
  147. // ------
  148. // ------
  149. test_spherical_strategy<segment_t, point_t>(
  150. "SEGMENT(-1 0, 1 0)", "SEGMENT(2 0, 3 0)", 'd', 0);
  151. // degenerated
  152. // /
  153. // *
  154. test_spherical_strategy<segment_t, point_t>(
  155. "SEGMENT(-1 -1, 1 1)", "SEGMENT(-2 -2, -2 -2)", 'd', 0);
  156. // /
  157. // *
  158. test_spherical_strategy<segment_t, point_t>(
  159. "SEGMENT(-1 -1, 1 1)", "SEGMENT(-1 -1, -1 -1)", '0', 1, "POINT(-1 -1)");
  160. // /
  161. // *
  162. // /
  163. test_spherical_strategy<segment_t, point_t>(
  164. "SEGMENT(-1 -1, 1 1)", "SEGMENT(0 0, 0 0)", '0', 1, "POINT(0 0)");
  165. // *
  166. // /
  167. test_spherical_strategy<segment_t, point_t>(
  168. "SEGMENT(-1 -1, 1 1)", "SEGMENT(1 1, 1 1)", '0', 1, "POINT(1 1)");
  169. // *
  170. // /
  171. test_spherical_strategy<segment_t, point_t>(
  172. "SEGMENT(-1 -1, 1 1)", "SEGMENT(2 2, 2 2)", 'd', 0);
  173. // similar to above, collinear
  174. // * ------
  175. test_spherical_strategy<segment_t, point_t>(
  176. "SEGMENT(-1 0, 1 0)", "SEGMENT(-2 0, -2 0)", 'd', 0);
  177. // *------
  178. test_spherical_strategy<segment_t, point_t>(
  179. "SEGMENT(-1 0, 1 0)", "SEGMENT(-1 0, -1 0)", '0', 1, "POINT(-1 0)");
  180. // ---*---
  181. test_spherical_strategy<segment_t, point_t>(
  182. "SEGMENT(-1 0, 1 0)", "SEGMENT(0 0, 0 0)", '0', 1, "POINT(0 0)");
  183. // ------*
  184. test_spherical_strategy<segment_t, point_t>(
  185. "SEGMENT(-1 0, 1 0)", "SEGMENT(1 0, 1 0)", '0', 1, "POINT(1 0)");
  186. // ------ *
  187. test_spherical_strategy<segment_t, point_t>(
  188. "SEGMENT(-1 0, 1 0)", "SEGMENT(2 0, 2 0)", 'd', 0);
  189. // Northern hemisphere
  190. // --- ------
  191. test_spherical_strategy<segment_t, point_t>(
  192. "SEGMENT(-1 50, 1 50)", "SEGMENT(-3 50, -2 50)", 'd', 0);
  193. // ------
  194. // ---
  195. test_spherical_strategy<segment_t, point_t>(
  196. "SEGMENT(-1 50, 1 50)", "SEGMENT(-2 50, -1 50)", 'a', 1, "POINT(-1 50)");
  197. // \/
  198. // /\ (avoid multi-line comment)
  199. test_spherical_strategy<segment_t, point_t>(
  200. "SEGMENT(-1 50, 1 50)", "SEGMENT(-2 50, 0 50)", 'i', 1, "POINT(-0.5 50.0032229484023)");
  201. // ________
  202. // / _____\ (avoid multi-line comment)
  203. test_spherical_strategy<segment_t, point_t>(
  204. "SEGMENT(-1 50, 1 50)", "SEGMENT(-2 50, 1 50)", 't', 1, "POINT(1 50)");
  205. // _________
  206. // / _____ \ (avoid multi-line comment)
  207. test_spherical_strategy<segment_t, point_t>(
  208. "SEGMENT(-1 50, 1 50)", "SEGMENT(-2 50, 2 50)", 'd', 0);
  209. // ______
  210. // /___ \ (avoid multi-line comment)
  211. test_spherical_strategy<segment_t, point_t>(
  212. "SEGMENT(-1 50, 1 50)", "SEGMENT(-1 50, 0 50)", 'f', 1, "POINT(-1 50)");
  213. // ------
  214. // ------
  215. test_spherical_strategy<segment_t, point_t>(
  216. "SEGMENT(-1 50, 1 50)", "SEGMENT(-1 50, 1 50)", 'e', 2, "POINT(-1 50)", "POINT(1 50)");
  217. // ________
  218. // /_____ \ (avoid multi-line comment)
  219. test_spherical_strategy<segment_t, point_t>(
  220. "SEGMENT(-1 50, 1 50)", "SEGMENT(-1 50, 2 50)", 'f', 1, "POINT(-1 50)");
  221. // ______
  222. // / ___\ (avoid multi-line comment)
  223. test_spherical_strategy<segment_t, point_t>(
  224. "SEGMENT(-1 50, 1 50)", "SEGMENT(0 50, 1 50)", 't', 1, "POINT(1 50)");
  225. // \/
  226. // /\ (avoid multi-line comment)
  227. test_spherical_strategy<segment_t, point_t>(
  228. "SEGMENT(-1 50, 1 50)", "SEGMENT(0 50, 2 50)", 'i', 1, "POINT(0.5 50.0032229484023)");
  229. // ------
  230. // ---
  231. test_spherical_strategy<segment_t, point_t>(
  232. "SEGMENT(-1 50, 1 50)", "SEGMENT(1 50, 2 50)", 'a', 1, "POINT(1 50)");
  233. // ------ ---
  234. test_spherical_strategy<segment_t, point_t>(
  235. "SEGMENT(-1 50, 1 50)", "SEGMENT(2 50, 3 50)", 'd', 0);
  236. // ___|
  237. test_spherical_strategy<segment_t, point_t>(
  238. "SEGMENT(0 0, 1 0)", "SEGMENT(1 0, 1 1)", 'a', 1, "POINT(1 0)");
  239. // ___|
  240. test_spherical_strategy<segment_t, point_t>(
  241. "SEGMENT(1 0, 1 1)", "SEGMENT(0 0, 1 0)", 'a', 1, "POINT(1 0)");
  242. // |/
  243. // /|
  244. test_spherical_strategy<segment_t, point_t>(
  245. "SEGMENT(10 -1, 20 1)", "SEGMENT(12.5 -1, 12.5 1)", 'i', 1, "POINT(12.5 -0.50051443471392)");
  246. // |/
  247. // /|
  248. test_spherical_strategy<segment_t, point_t>(
  249. "SEGMENT(10 -1, 20 1)", "SEGMENT(17.5 -1, 17.5 1)", 'i', 1, "POINT(17.5 0.50051443471392)");
  250. }
  251. template <typename T>
  252. void test_spherical_radian()
  253. {
  254. typedef bg::model::point<T, 2, bg::cs::spherical_equatorial<bg::radian> > point_t;
  255. typedef bg::model::segment<point_t> segment_t;
  256. bg::strategy::intersection::spherical_segments<> strategy;
  257. // https://github.com/boostorg/geometry/issues/470
  258. point_t p0(0.00001, 0.00001);
  259. point_t p1(0.00001, 0.00005);
  260. point_t p2(0.00005, 0.00005);
  261. segment_t s1(p0, p1);
  262. segment_t s2(p1, p2);
  263. test_strategy_one(s1, s1, strategy, 'e', 2, p0, p1);
  264. test_strategy_one(s2, s2, strategy, 'e', 2, p1, p2);
  265. }
  266. int test_main(int, char* [])
  267. {
  268. //test_spherical<float>();
  269. test_spherical<double>();
  270. test_spherical_radian<double>();
  271. return 0;
  272. }