intersection_points.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Use, modification and distribution is subject to the Boost Software License,
  4. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GEOMETRY_GEOMETRY_POLICIES_RELATE_INTERSECTION_POINTS_HPP
  7. #define BOOST_GEOMETRY_GEOMETRY_POLICIES_RELATE_INTERSECTION_POINTS_HPP
  8. #include <algorithm>
  9. #include <string>
  10. #include <boost/concept_check.hpp>
  11. #include <boost/numeric/conversion/cast.hpp>
  12. #include <boost/geometry/arithmetic/determinant.hpp>
  13. #include <boost/geometry/core/access.hpp>
  14. #include <boost/geometry/strategies/side_info.hpp>
  15. #include <boost/geometry/util/select_calculation_type.hpp>
  16. #include <boost/geometry/util/select_most_precise.hpp>
  17. namespace boost { namespace geometry
  18. {
  19. namespace policies { namespace relate
  20. {
  21. template <typename S1, typename S2, typename ReturnType, typename CalculationType = void>
  22. struct segments_intersection_points
  23. {
  24. typedef ReturnType return_type;
  25. typedef S1 segment_type1;
  26. typedef S2 segment_type2;
  27. typedef typename select_calculation_type
  28. <
  29. S1, S2, CalculationType
  30. >::type coordinate_type;
  31. template <typename R>
  32. static inline return_type segments_intersect(side_info const&,
  33. R const& r,
  34. coordinate_type const& dx1, coordinate_type const& dy1,
  35. coordinate_type const& , coordinate_type const& ,
  36. S1 const& s1, S2 const& )
  37. {
  38. typedef typename geometry::coordinate_type
  39. <
  40. typename return_type::point_type
  41. >::type return_coordinate_type;
  42. coordinate_type const s1x = get<0, 0>(s1);
  43. coordinate_type const s1y = get<0, 1>(s1);
  44. return_type result;
  45. result.count = 1;
  46. set<0>(result.intersections[0],
  47. boost::numeric_cast<return_coordinate_type>(R(s1x) + r * R(dx1)));
  48. set<1>(result.intersections[0],
  49. boost::numeric_cast<return_coordinate_type>(R(s1y) + r * R(dy1)));
  50. return result;
  51. }
  52. static inline return_type collinear_touch(coordinate_type const& x,
  53. coordinate_type const& y, int, int)
  54. {
  55. return_type result;
  56. result.count = 1;
  57. set<0>(result.intersections[0], x);
  58. set<1>(result.intersections[0], y);
  59. return result;
  60. }
  61. template <typename S>
  62. static inline return_type collinear_inside(S const& s, int index1 = 0, int index2 = 1)
  63. {
  64. return_type result;
  65. result.count = 2;
  66. set<0>(result.intersections[index1], get<0, 0>(s));
  67. set<1>(result.intersections[index1], get<0, 1>(s));
  68. set<0>(result.intersections[index2], get<1, 0>(s));
  69. set<1>(result.intersections[index2], get<1, 1>(s));
  70. return result;
  71. }
  72. template <typename S>
  73. static inline return_type collinear_interior_boundary_intersect(S const& s, bool a_in_b,
  74. int, int, bool opposite)
  75. {
  76. int index1 = opposite && ! a_in_b ? 1 : 0;
  77. return collinear_inside(s, index1, 1 - index1);
  78. }
  79. static inline return_type collinear_a_in_b(S1 const& s, bool)
  80. {
  81. return collinear_inside(s);
  82. }
  83. static inline return_type collinear_b_in_a(S2 const& s, bool opposite)
  84. {
  85. int index1 = opposite ? 1 : 0;
  86. return collinear_inside(s, index1, 1 - index1);
  87. }
  88. static inline return_type collinear_overlaps(
  89. coordinate_type const& x1, coordinate_type const& y1,
  90. coordinate_type const& x2, coordinate_type const& y2,
  91. int, int, bool)
  92. {
  93. return_type result;
  94. result.count = 2;
  95. set<0>(result.intersections[0], x1);
  96. set<1>(result.intersections[0], y1);
  97. set<0>(result.intersections[1], x2);
  98. set<1>(result.intersections[1], y2);
  99. return result;
  100. }
  101. static inline return_type segment_equal(S1 const& s, bool)
  102. {
  103. return_type result;
  104. result.count = 2;
  105. // TODO: order of IP's
  106. set<0>(result.intersections[0], get<0, 0>(s));
  107. set<1>(result.intersections[0], get<0, 1>(s));
  108. set<0>(result.intersections[1], get<1, 0>(s));
  109. set<1>(result.intersections[1], get<1, 1>(s));
  110. return result;
  111. }
  112. static inline return_type disjoint()
  113. {
  114. return return_type();
  115. }
  116. static inline return_type error(std::string const&)
  117. {
  118. return return_type();
  119. }
  120. static inline return_type collinear_disjoint()
  121. {
  122. return return_type();
  123. }
  124. static inline return_type degenerate(S1 const& s, bool)
  125. {
  126. return_type result;
  127. result.count = 1;
  128. set<0>(result.intersections[0], get<0, 0>(s));
  129. set<1>(result.intersections[0], get<0, 1>(s));
  130. return result;
  131. }
  132. };
  133. }} // namespace policies::relate
  134. }} // namespace boost::geometry
  135. #endif // BOOST_GEOMETRY_GEOMETRY_POLICIES_RELATE_INTERSECTION_POINTS_HPP