intersection_points.hpp 6.2 KB

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