intersection_points.hpp 6.0 KB

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