intersection_points.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2016, 2022.
  4. // Modifications copyright (c) 2016-2022 Oracle and/or its affiliates.
  5. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  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. #ifndef BOOST_GEOMETRY_GEOMETRY_POLICIES_RELATE_INTERSECTION_POINTS_HPP
  11. #define BOOST_GEOMETRY_GEOMETRY_POLICIES_RELATE_INTERSECTION_POINTS_HPP
  12. #include <algorithm>
  13. #include <string>
  14. #include <boost/geometry/algorithms/assign.hpp>
  15. #include <boost/geometry/algorithms/detail/assign_indexed_point.hpp>
  16. #include <boost/geometry/core/access.hpp>
  17. #include <boost/geometry/core/assert.hpp>
  18. #include <boost/geometry/strategies/side_info.hpp>
  19. namespace boost { namespace geometry
  20. {
  21. namespace policies { namespace relate
  22. {
  23. /*!
  24. \brief Policy calculating the intersection points themselves
  25. */
  26. template
  27. <
  28. typename ReturnType
  29. >
  30. struct segments_intersection_points
  31. {
  32. typedef ReturnType return_type;
  33. template
  34. <
  35. typename Segment1,
  36. typename Segment2,
  37. typename SegmentIntersectionInfo
  38. >
  39. static inline return_type segments_crosses(side_info const&,
  40. SegmentIntersectionInfo const& sinfo,
  41. Segment1 const& s1, Segment2 const& s2)
  42. {
  43. return_type result;
  44. result.count = 1;
  45. sinfo.calculate(result.intersections[0], s1, s2);
  46. // Temporary - this should go later
  47. result.fractions[0].assign(sinfo);
  48. return result;
  49. }
  50. template<typename SegmentIntersectionInfo, typename Point>
  51. static inline return_type
  52. segments_share_common_point(side_info const&, SegmentIntersectionInfo const& sinfo,
  53. Point const& p)
  54. {
  55. return_type result;
  56. result.count = 1;
  57. boost::geometry::assign(result.intersections[0], p);
  58. // Temporary - this should go later
  59. result.fractions[0].assign(sinfo);
  60. return result;
  61. }
  62. template <typename Segment1, typename Segment2, typename Ratio>
  63. static inline return_type segments_collinear(
  64. Segment1 const& a, Segment2 const& b, bool /*opposite*/,
  65. int a1_wrt_b, int a2_wrt_b, int b1_wrt_a, int b2_wrt_a,
  66. Ratio const& ra_from_wrt_b, Ratio const& ra_to_wrt_b,
  67. Ratio const& rb_from_wrt_a, Ratio const& rb_to_wrt_a)
  68. {
  69. return_type result;
  70. unsigned int index = 0;
  71. Ratio on_a[2];
  72. // The conditions "index < 2" are necessary for non-robust handling,
  73. // if index would be 2 this indicate an (currently uncatched) error
  74. // IMPORTANT: the order of conditions is different as in direction.hpp
  75. if (a1_wrt_b >= 1 && a1_wrt_b <= 3 // ra_from_wrt_b.on_segment()
  76. && index < 2)
  77. {
  78. // a1--------->a2
  79. // b1----->b2
  80. //
  81. // ra1 (relative to b) is between 0/1:
  82. // -> First point of A is intersection point
  83. detail::assign_point_from_index<0>(a, result.intersections[index]);
  84. result.fractions[index].assign(Ratio::zero(), ra_from_wrt_b);
  85. on_a[index] = Ratio::zero();
  86. index++;
  87. }
  88. if (b1_wrt_a == 2 //rb_from_wrt_a.in_segment()
  89. && index < 2)
  90. {
  91. // We take the first intersection point of B
  92. // a1--------->a2
  93. // b1----->b2
  94. // But only if it is not located on A
  95. // a1--------->a2
  96. // b1----->b2 rb_from_wrt_a == 0/1 -> a already taken
  97. detail::assign_point_from_index<0>(b, result.intersections[index]);
  98. result.fractions[index].assign(rb_from_wrt_a, Ratio::zero());
  99. on_a[index] = rb_from_wrt_a;
  100. index++;
  101. }
  102. if (a2_wrt_b >= 1 && a2_wrt_b <= 3 //ra_to_wrt_b.on_segment()
  103. && index < 2)
  104. {
  105. // Similarly, second IP (here a2)
  106. // a1--------->a2
  107. // b1----->b2
  108. detail::assign_point_from_index<1>(a, result.intersections[index]);
  109. result.fractions[index].assign(Ratio::one(), ra_to_wrt_b);
  110. on_a[index] = Ratio::one();
  111. index++;
  112. }
  113. if (b2_wrt_a == 2 // rb_to_wrt_a.in_segment()
  114. && index < 2)
  115. {
  116. detail::assign_point_from_index<1>(b, result.intersections[index]);
  117. result.fractions[index].assign(rb_to_wrt_a, Ratio::one());
  118. on_a[index] = rb_to_wrt_a;
  119. index++;
  120. }
  121. // TEMPORARY
  122. // If both are from b, and b is reversed w.r.t. a, we swap IP's
  123. // to align them w.r.t. a
  124. // get_turn_info still relies on some order (in some collinear cases)
  125. if (index == 2 && on_a[1] < on_a[0])
  126. {
  127. std::swap(result.fractions[0], result.fractions[1]);
  128. std::swap(result.intersections[0], result.intersections[1]);
  129. }
  130. result.count = index;
  131. return result;
  132. }
  133. static inline return_type disjoint()
  134. {
  135. return return_type();
  136. }
  137. static inline return_type error(std::string const&)
  138. {
  139. return return_type();
  140. }
  141. // Both degenerate
  142. template <typename Segment>
  143. static inline return_type degenerate(Segment const& segment, bool)
  144. {
  145. return_type result;
  146. result.count = 1;
  147. set<0>(result.intersections[0], get<0, 0>(segment));
  148. set<1>(result.intersections[0], get<0, 1>(segment));
  149. return result;
  150. }
  151. // One degenerate
  152. template <typename Segment, typename Ratio>
  153. static inline return_type one_degenerate(Segment const& degenerate_segment,
  154. Ratio const& ratio, bool a_degenerate)
  155. {
  156. return_type result;
  157. result.count = 1;
  158. set<0>(result.intersections[0], get<0, 0>(degenerate_segment));
  159. set<1>(result.intersections[0], get<0, 1>(degenerate_segment));
  160. if (a_degenerate)
  161. {
  162. // IP lies on ratio w.r.t. segment b
  163. result.fractions[0].assign(Ratio::zero(), ratio);
  164. }
  165. else
  166. {
  167. result.fractions[0].assign(ratio, Ratio::zero());
  168. }
  169. return result;
  170. }
  171. };
  172. }} // namespace policies::relate
  173. }} // namespace boost::geometry
  174. #endif // BOOST_GEOMETRY_GEOMETRY_POLICIES_RELATE_INTERSECTION_POINTS_HPP