/Src/Dependencies/Boost/libs/geometry/test/strategies/segment_intersection_collinear.cpp

http://hadesmem.googlecode.com/ · C++ · 252 lines · 161 code · 43 blank · 48 comment · 5 complexity · f74ee11350f373f4b8ce3d05b34d6ea1 MD5 · raw file

  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2011 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2008-2011 Bruno Lalande, Paris, France.
  5. // Copyright (c) 2009-2011 Mateusz Loskot, London, UK.
  6. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  7. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  8. // Use, modification and distribution is subject to the Boost Software License,
  9. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. #include <geometry_test_common.hpp>
  12. #include <boost/geometry/algorithms/assign.hpp>
  13. #include <boost/geometry/strategies/cartesian/cart_intersect.hpp>
  14. #include <boost/geometry/strategies/intersection_result.hpp>
  15. #include <boost/geometry/policies/relate/intersection_points.hpp>
  16. #include <boost/geometry/policies/relate/direction.hpp>
  17. #include <boost/geometry/policies/relate/de9im.hpp>
  18. #include <boost/geometry/policies/relate/tupled.hpp>
  19. #include <boost/geometry/algorithms/intersection.hpp>
  20. #include <boost/geometry/geometries/point.hpp>
  21. #include <boost/geometry/geometries/segment.hpp>
  22. template <typename IntersectionPoints>
  23. static int check(IntersectionPoints const& is,
  24. std::size_t index, double expected_x, double expected_y)
  25. {
  26. if (expected_x != -99 && expected_y != -99 && is.count > index)
  27. {
  28. double x = bg::get<0>(is.intersections[index]);
  29. double y = bg::get<1>(is.intersections[index]);
  30. BOOST_CHECK_CLOSE(x, expected_x, 0.001);
  31. BOOST_CHECK_CLOSE(y, expected_y, 0.001);
  32. return 1;
  33. }
  34. return 0;
  35. }
  36. template <typename P>
  37. static void test_segment_intersection(std::string const& case_id,
  38. int x1, int y1, int x2, int y2,
  39. int x3, int y3, int x4, int y4,
  40. char expected_how, bool expected_opposite,
  41. int expected_arrival1, int expected_arrival2,
  42. int expected_x1, int expected_y1,
  43. int expected_x2 = -99, int expected_y2 = -99)
  44. {
  45. //#ifdef REPORT
  46. std::cout << "Case: " << case_id << std::endl;
  47. //#endif
  48. typedef typename bg::coordinate_type<P>::type coordinate_type;
  49. typedef bg::model::referring_segment<const P> segment_type;
  50. P p1, p2, p3, p4;
  51. bg::assign_values(p1, x1, y1);
  52. bg::assign_values(p2, x2, y2);
  53. bg::assign_values(p3, x3, y3);
  54. bg::assign_values(p4, x4, y4);
  55. segment_type s12(p1,p2);
  56. segment_type s34(p3,p4);
  57. // Get the intersection point (or two points)
  58. bg::segment_intersection_points<P> is
  59. = bg::strategy::intersection::relate_cartesian_segments
  60. <
  61. bg::policies::relate::segments_intersection_points
  62. <
  63. segment_type,
  64. segment_type,
  65. bg::segment_intersection_points<P>
  66. >
  67. >::apply(s12, s34);
  68. // Get just a character for Left/Right/intersects/etc, purpose is more for debugging
  69. bg::policies::relate::direction_type dir
  70. = bg::strategy::intersection::relate_cartesian_segments
  71. <
  72. bg::policies::relate::segments_direction
  73. <
  74. segment_type,
  75. segment_type
  76. >
  77. >::apply(s12, s34);
  78. int expected_count =
  79. check(is, 0, expected_x1, expected_y1)
  80. + check(is, 1, expected_x2, expected_y2);
  81. BOOST_CHECK_EQUAL(is.count, expected_count);
  82. BOOST_CHECK_EQUAL(dir.how, expected_how);
  83. BOOST_CHECK_EQUAL(dir.opposite, expected_opposite);
  84. BOOST_CHECK_EQUAL(dir.arrival[0], expected_arrival1);
  85. BOOST_CHECK_EQUAL(dir.arrival[1], expected_arrival2);
  86. }
  87. template <typename P>
  88. void test_all()
  89. {
  90. // Collinear - non opposite
  91. // a1---------->a2
  92. // b1--->b2
  93. test_segment_intersection<P>("n1",
  94. 2, 0, 6, 0,
  95. 0, 0, 2, 0,
  96. 'a', false, -1, 0,
  97. 2, 0);
  98. // a1---------->a2
  99. // b1--->b2
  100. test_segment_intersection<P>("n2",
  101. 2, 0, 6, 0,
  102. 1, 0, 3, 0,
  103. 'c', false, -1, 1,
  104. 2, 0, 3, 0);
  105. // a1---------->a2
  106. // b1--->b2
  107. test_segment_intersection<P>("n3",
  108. 2, 0, 6, 0,
  109. 2, 0, 4, 0,
  110. 'c', false, -1, 1,
  111. 2, 0, 4, 0);
  112. // a1---------->a2
  113. // b1--->b2
  114. test_segment_intersection<P>("n4",
  115. 2, 0, 6, 0,
  116. 3, 0, 5, 0,
  117. 'c', false, -1, 1,
  118. 3, 0, 5, 0);
  119. // a1---------->a2
  120. // b1--->b2
  121. test_segment_intersection<P>("n5",
  122. 2, 0, 6, 0,
  123. 4, 0, 6, 0,
  124. 'c', false, 0, 0,
  125. 4, 0, 6, 0);
  126. // a1---------->a2
  127. // b1--->b2
  128. test_segment_intersection<P>("n6",
  129. 2, 0, 6, 0,
  130. 5, 0, 7, 0,
  131. 'c', false, 1, -1,
  132. 5, 0, 6, 0);
  133. // a1---------->a2
  134. // b1--->b2
  135. test_segment_intersection<P>("n7",
  136. 2, 0, 6, 0,
  137. 6, 0, 8, 0,
  138. 'a', false, 0, -1,
  139. 6, 0);
  140. // Collinear - opposite
  141. // a1---------->a2
  142. // b2<---b1
  143. test_segment_intersection<P>("o1",
  144. 2, 0, 6, 0,
  145. 2, 0, 0, 0,
  146. 'f', true, -1, -1,
  147. 2, 0);
  148. // a1---------->a2
  149. // b2<---b1
  150. test_segment_intersection<P>("o2",
  151. 2, 0, 6, 0,
  152. 3, 0, 1, 0,
  153. 'c', true, -1, -1,
  154. 2, 0, 3, 0);
  155. // a1---------->a2
  156. // b2<---b1
  157. test_segment_intersection<P>("o3",
  158. 2, 0, 6, 0,
  159. 4, 0, 2, 0,
  160. 'c', true, -1, 0,
  161. 2, 0, 4, 0);
  162. // a1---------->a2
  163. // b2<---b1
  164. test_segment_intersection<P>("o4",
  165. 2, 0, 6, 0,
  166. 5, 0, 3, 0,
  167. 'c', true, -1, 1,
  168. 3, 0, 5, 0);
  169. // a1---------->a2
  170. // b2<---b1
  171. test_segment_intersection<P>("o5",
  172. 2, 0, 6, 0,
  173. 6, 0, 4, 0,
  174. 'c', true, 0, 1,
  175. 4, 0, 6, 0);
  176. // a1---------->a2
  177. // b2<---b1
  178. test_segment_intersection<P>("o6",
  179. 2, 0, 6, 0,
  180. 7, 0, 5, 0,
  181. 'c', true, 1, 1,
  182. 5, 0, 6, 0);
  183. // a1---------->a2
  184. // b2<---b1
  185. test_segment_intersection<P>("o7",
  186. 2, 0, 6, 0,
  187. 8, 0, 6, 0,
  188. 't', true, 0, 0,
  189. 6, 0);
  190. // a1---------->a2
  191. // b1---------->b2
  192. test_segment_intersection<P>("e1",
  193. 2, 0, 6, 0,
  194. 2, 0, 6, 0,
  195. 'e', false, 0, 0,
  196. 2, 0, 6, 0);
  197. // a1---------->a2
  198. // b2<----------b1
  199. test_segment_intersection<P>("e1",
  200. 2, 0, 6, 0,
  201. 6, 0, 2, 0,
  202. 'e', true, 0, 0,
  203. 2, 0, 6, 0);
  204. }
  205. int test_main(int, char* [])
  206. {
  207. test_all<bg::model::point<double, 2, bg::cs::cartesian> >();
  208. return 0;
  209. }