segment_to_segment.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2023 Adam Wulkiewicz, Lodz, Poland.
  3. // Copyright (c) 2014-2021, Oracle and/or its affiliates.
  4. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Licensed under the Boost Software License version 1.0.
  7. // http://www.boost.org/users/license.html
  8. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISTANCE_SEGMENT_TO_SEGMENT_HPP
  9. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISTANCE_SEGMENT_TO_SEGMENT_HPP
  10. #include <algorithm>
  11. #include <iterator>
  12. #include <boost/core/addressof.hpp>
  13. #include <boost/geometry/algorithms/assign.hpp>
  14. #include <boost/geometry/algorithms/detail/distance/is_comparable.hpp>
  15. #include <boost/geometry/algorithms/detail/distance/strategy_utils.hpp>
  16. #include <boost/geometry/algorithms/dispatch/distance.hpp>
  17. #include <boost/geometry/algorithms/intersects.hpp>
  18. #include <boost/geometry/core/point_type.hpp>
  19. #include <boost/geometry/core/tags.hpp>
  20. #include <boost/geometry/strategies/distance.hpp>
  21. #include <boost/geometry/strategies/tags.hpp>
  22. #include <boost/geometry/util/constexpr.hpp>
  23. namespace boost { namespace geometry
  24. {
  25. #ifndef DOXYGEN_NO_DETAIL
  26. namespace detail { namespace distance
  27. {
  28. // compute segment-segment distance
  29. template<typename Segment1, typename Segment2, typename Strategies>
  30. class segment_to_segment
  31. {
  32. typedef distance::strategy_t<Segment1, Segment2, Strategies> strategy_type;
  33. public:
  34. typedef distance::return_t<Segment1, Segment2, Strategies> return_type;
  35. static inline return_type apply(Segment1 const& segment1, Segment2 const& segment2,
  36. Strategies const& strategies)
  37. {
  38. if (geometry::intersects(segment1, segment2, strategies))
  39. {
  40. return 0;
  41. }
  42. typename point_type<Segment1>::type p[2];
  43. detail::assign_point_from_index<0>(segment1, p[0]);
  44. detail::assign_point_from_index<1>(segment1, p[1]);
  45. typename point_type<Segment2>::type q[2];
  46. detail::assign_point_from_index<0>(segment2, q[0]);
  47. detail::assign_point_from_index<1>(segment2, q[1]);
  48. strategy_type const strategy = strategies.distance(segment1, segment2);
  49. auto const cstrategy = strategy::distance::services::get_comparable
  50. <
  51. strategy_type
  52. >::apply(strategy);
  53. distance::creturn_t<Segment1, Segment2, Strategies> d[4];
  54. d[0] = cstrategy.apply(q[0], p[0], p[1]);
  55. d[1] = cstrategy.apply(q[1], p[0], p[1]);
  56. d[2] = cstrategy.apply(p[0], q[0], q[1]);
  57. d[3] = cstrategy.apply(p[1], q[0], q[1]);
  58. std::size_t imin = std::distance(boost::addressof(d[0]),
  59. std::min_element(d, d + 4));
  60. if BOOST_GEOMETRY_CONSTEXPR (is_comparable<strategy_type>::value)
  61. {
  62. return d[imin];
  63. }
  64. else // else prevents unreachable code warning
  65. {
  66. switch (imin)
  67. {
  68. case 0:
  69. return strategy.apply(q[0], p[0], p[1]);
  70. case 1:
  71. return strategy.apply(q[1], p[0], p[1]);
  72. case 2:
  73. return strategy.apply(p[0], q[0], q[1]);
  74. default:
  75. return strategy.apply(p[1], q[0], q[1]);
  76. }
  77. }
  78. }
  79. };
  80. }} // namespace detail::distance
  81. #endif // DOXYGEN_NO_DETAIL
  82. #ifndef DOXYGEN_NO_DISPATCH
  83. namespace dispatch
  84. {
  85. // segment-segment
  86. template <typename Segment1, typename Segment2, typename Strategy>
  87. struct distance
  88. <
  89. Segment1, Segment2, Strategy, segment_tag, segment_tag,
  90. strategy_tag_distance_point_segment, false
  91. >
  92. : detail::distance::segment_to_segment<Segment1, Segment2, Strategy>
  93. {};
  94. } // namespace dispatch
  95. #endif // DOXYGEN_NO_DISPATCH
  96. }} // namespace boost::geometry
  97. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISTANCE_SEGMENT_TO_SEGMENT_HPP