infinite_line_functions.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2018-2019 Barend Gehrels, Amsterdam, the Netherlands.
  4. // This file was modified by Oracle on 2019.
  5. // Modifications copyright (c) 2019, 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 <geometry_test_common.hpp>
  11. #include <boost/geometry/arithmetic/infinite_line_functions.hpp>
  12. #include <boost/geometry/geometries/infinite_line.hpp>
  13. #include <boost/geometry/algorithms/detail/make/make.hpp>
  14. #include <boost/geometry/geometries/point.hpp>
  15. #include <boost/geometry/io/wkt/wkt.hpp>
  16. namespace
  17. {
  18. // Boost.Test does not support BOOST_CHECK_CLOSE for integral types
  19. template <typename T>
  20. bool is_small(T const& value)
  21. {
  22. static long double const epsilon = 1.0e-5;
  23. return bg::math::abs(value) < epsilon;
  24. }
  25. }
  26. template <typename T, typename C>
  27. void verify_point_on_line(bg::model::infinite_line<T> const& line,
  28. C const& x, C const& y)
  29. {
  30. BOOST_CHECK_MESSAGE(is_small(line.a * x + line.b * y + line.c),
  31. "Point is not located on the line");
  32. }
  33. template <typename T>
  34. void test_side_value()
  35. {
  36. typedef bg::model::infinite_line<T> line_type;
  37. // Horizontal line going right
  38. line_type line = bg::detail::make::make_infinite_line<T>(0, 0, 10, 0);
  39. // Point above (= on left side)
  40. T d = bg::arithmetic::side_value(line, 5, 5);
  41. BOOST_CHECK_MESSAGE(d > 0, "point not on left side");
  42. // Point below (= on right side)
  43. d = bg::arithmetic::side_value(line, 5, -5);
  44. BOOST_CHECK_MESSAGE(d < 0, "point not on right side");
  45. // Diagonal not through origin, from right (down) to left (up)
  46. line = bg::detail::make::make_infinite_line<T>(5, 2, -7, 10);
  47. d = bg::arithmetic::side_value(line, 5, 2);
  48. BOOST_CHECK_MESSAGE(is_small(d), "point not on line");
  49. d = bg::arithmetic::side_value(line, -7, 10);
  50. BOOST_CHECK_MESSAGE(is_small(d), "point not on line");
  51. // vector is (-12, 8), move (-3,2) on the line from (5,2)
  52. d = bg::arithmetic::side_value(line, 2, 4);
  53. BOOST_CHECK_MESSAGE(is_small(d), "point not on line");
  54. // Go perpendicular (2,3) from (2,4) up, so right of the line (negative)
  55. d = bg::arithmetic::side_value(line, 4, 7);
  56. BOOST_CHECK_MESSAGE(d < 0, "point not on right side");
  57. // Go perpendicular (2,3) from (2,4) down, so left of the line (positive)
  58. d = bg::arithmetic::side_value(line, 0, 1);
  59. BOOST_CHECK_MESSAGE(d > 0, "point not on left side");
  60. }
  61. template <typename T>
  62. void test_get_intersection()
  63. {
  64. typedef bg::model::infinite_line<T> line_type;
  65. // Diagonal lines (first is same as in distance measure,
  66. // second is perpendicular and used there for distance measures)
  67. line_type p = bg::detail::make::make_infinite_line<T>(5, 2, -7, 10);
  68. line_type q = bg::detail::make::make_infinite_line<T>(4, 7, 0, 1);
  69. typedef bg::model::point<T, 2, bg::cs::cartesian> point_type;
  70. point_type ip;
  71. BOOST_CHECK(bg::arithmetic::intersection_point(p, q, ip));
  72. BOOST_CHECK_MESSAGE(is_small(bg::get<0>(ip) - 2), "x-coordinate wrong");
  73. BOOST_CHECK_MESSAGE(is_small(bg::get<1>(ip) - 4), "y-coordinate wrong");
  74. verify_point_on_line(p, bg::get<0>(ip), bg::get<1>(ip));
  75. verify_point_on_line(q, bg::get<0>(ip), bg::get<1>(ip));
  76. }
  77. template <typename T>
  78. void test_same_direction()
  79. {
  80. bg::model::infinite_line<T> p, q;
  81. // Exactly opposite, diagonal
  82. p = bg::detail::make::make_infinite_line<T>(2, 1, 12, 11);
  83. q = bg::detail::make::make_infinite_line<T>(12, 11, 2, 1);
  84. BOOST_CHECK(! bg::arithmetic::similar_direction(p, q));
  85. // Exactly opposite, horizontal
  86. p = bg::detail::make::make_infinite_line<T>(0, 0, 10, 0);
  87. q = bg::detail::make::make_infinite_line<T>(10, 0, 0, 0);
  88. BOOST_CHECK(! bg::arithmetic::similar_direction(p, q));
  89. // Exactly opposite, vertical
  90. p = bg::detail::make::make_infinite_line<T>(0, 0, 0, 10);
  91. q = bg::detail::make::make_infinite_line<T>(0, 10, 0, 0);
  92. BOOST_CHECK(! bg::arithmetic::similar_direction(p, q));
  93. // Exactly equal, diagonal
  94. p = bg::detail::make::make_infinite_line<T>(0, 0, 10, 10);
  95. q = bg::detail::make::make_infinite_line<T>(0, 0, 10, 10);
  96. BOOST_CHECK(bg::arithmetic::similar_direction(p, q));
  97. // Exactly equal, horizontal
  98. p = bg::detail::make::make_infinite_line<T>(0, 0, 10, 0);
  99. q = bg::detail::make::make_infinite_line<T>(0, 0, 10, 0);
  100. BOOST_CHECK(bg::arithmetic::similar_direction(p, q));
  101. // Exactly equal, vertical
  102. p = bg::detail::make::make_infinite_line<T>(0, 0, 0, 10);
  103. q = bg::detail::make::make_infinite_line<T>(0, 0, 0, 10);
  104. BOOST_CHECK(bg::arithmetic::similar_direction(p, q));
  105. // Coming together, diagonal
  106. p = bg::detail::make::make_infinite_line<T>(0, 0, 10, 10);
  107. q = bg::detail::make::make_infinite_line<T>(20, 20, 10, 10);
  108. BOOST_CHECK(! bg::arithmetic::similar_direction(p, q));
  109. // Leaving from common point, diagonal
  110. p = bg::detail::make::make_infinite_line<T>(10, 10, 0, 0);
  111. q = bg::detail::make::make_infinite_line<T>(0, 0, 10, 10);
  112. BOOST_CHECK(! bg::arithmetic::similar_direction(p, q));
  113. // Continuing each other, diagonal
  114. p = bg::detail::make::make_infinite_line<T>(0, 0, 10, 10);
  115. q = bg::detail::make::make_infinite_line<T>(10, 10, 20, 20);
  116. BOOST_CHECK(bg::arithmetic::similar_direction(p, q));
  117. // (Nearly) perpendicular
  118. p = bg::detail::make::make_infinite_line<T>(0, 0, 10, 10);
  119. q = bg::detail::make::make_infinite_line<T>(0, 0, -10, 10);
  120. BOOST_CHECK(! bg::arithmetic::similar_direction(p, q));
  121. // 45 deg
  122. p = bg::detail::make::make_infinite_line<T>(0, 0, 10, 10);
  123. q = bg::detail::make::make_infinite_line<T>(0, 0, 0, 10);
  124. BOOST_CHECK(bg::arithmetic::similar_direction(p, q));
  125. // a bit more than 45 deg
  126. p = bg::detail::make::make_infinite_line<T>(0, 0, 10, 10);
  127. q = bg::detail::make::make_infinite_line<T>(0, 0, -1, 10);
  128. BOOST_CHECK(! bg::arithmetic::similar_direction(p, q));
  129. // 135 deg
  130. p = bg::detail::make::make_infinite_line<T>(0, 0, 10, 10);
  131. q = bg::detail::make::make_infinite_line<T>(0, 0, -10, 0);
  132. BOOST_CHECK(! bg::arithmetic::similar_direction(p, q));
  133. }
  134. template <typename T>
  135. void test_degenerate()
  136. {
  137. typedef bg::model::infinite_line<T> line_type;
  138. line_type line = bg::detail::make::make_infinite_line<T>(0, 0, 10, 0);
  139. BOOST_CHECK(! bg::arithmetic::is_degenerate(line));
  140. line = bg::detail::make::make_infinite_line<T>(0, 0, 0, 10);
  141. BOOST_CHECK(! bg::arithmetic::is_degenerate(line));
  142. line = bg::detail::make::make_infinite_line<T>(0, 0, 10, 10);
  143. BOOST_CHECK(! bg::arithmetic::is_degenerate(line));
  144. line = bg::detail::make::make_infinite_line<T>(0, 0, 0, 0);
  145. BOOST_CHECK(bg::arithmetic::is_degenerate(line));
  146. }
  147. template <typename T>
  148. void test_all()
  149. {
  150. test_side_value<T>();
  151. test_get_intersection<T>();
  152. test_same_direction<T>();
  153. test_degenerate<T>();
  154. }
  155. int test_main(int, char* [])
  156. {
  157. test_all<double>();
  158. test_all<long double>();
  159. test_all<float>();
  160. test_all<int>();
  161. return 0;
  162. }