intersection_ratios.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2014 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_RATIOS_HPP
  7. #define BOOST_GEOMETRY_GEOMETRY_POLICIES_RELATE_INTERSECTION_RATIOS_HPP
  8. #include <algorithm>
  9. #include <string>
  10. #include <boost/concept_check.hpp>
  11. #include <boost/geometry/algorithms/detail/assign_indexed_point.hpp>
  12. #include <boost/geometry/core/access.hpp>
  13. #include <boost/geometry/strategies/side_info.hpp>
  14. namespace boost { namespace geometry
  15. {
  16. namespace policies { namespace relate
  17. {
  18. /*!
  19. \brief Policy returning segment ratios
  20. \note Template argument FractionType should be a fraction_type<SegmentRatio>
  21. */
  22. template
  23. <
  24. typename FractionType
  25. >
  26. struct segments_intersection_ratios
  27. {
  28. typedef FractionType return_type;
  29. template
  30. <
  31. typename Segment1,
  32. typename Segment2,
  33. typename SegmentIntersectionInfo
  34. >
  35. static inline return_type segments_crosses(side_info const&,
  36. SegmentIntersectionInfo const& sinfo,
  37. Segment1 const& , Segment2 const& )
  38. {
  39. return_type result;
  40. result.assign(sinfo);
  41. return result;
  42. }
  43. template<typename SegmentIntersectionInfo, typename Point>
  44. static inline return_type
  45. segments_share_common_point(side_info const&, SegmentIntersectionInfo const& sinfo,
  46. Point const& p)
  47. {
  48. return_type result;
  49. result.assign(sinfo);
  50. return result;
  51. }
  52. template <typename Segment1, typename Segment2, typename Ratio>
  53. static inline return_type segments_collinear(
  54. Segment1 const& , Segment2 const& ,
  55. Ratio const& ra_from_wrt_b, Ratio const& ra_to_wrt_b,
  56. Ratio const& rb_from_wrt_a, Ratio const& rb_to_wrt_a)
  57. {
  58. // We have only one result, for (potentially) two IP's,
  59. // so we take a first one
  60. return_type result;
  61. if (ra_from_wrt_b.on_segment())
  62. {
  63. result.assign(Ratio::zero(), ra_from_wrt_b);
  64. }
  65. else if (rb_from_wrt_a.in_segment())
  66. {
  67. result.assign(rb_from_wrt_a, Ratio::zero());
  68. }
  69. else if (ra_to_wrt_b.on_segment())
  70. {
  71. result.assign(Ratio::one(), ra_to_wrt_b);
  72. }
  73. else if (rb_to_wrt_a.in_segment())
  74. {
  75. result.assign(rb_to_wrt_a, Ratio::one());
  76. }
  77. return result;
  78. }
  79. static inline return_type disjoint()
  80. {
  81. return return_type();
  82. }
  83. static inline return_type error(std::string const&)
  84. {
  85. return return_type();
  86. }
  87. template <typename Segment>
  88. static inline return_type degenerate(Segment const& segment, bool)
  89. {
  90. return return_type();
  91. }
  92. template <typename Segment, typename Ratio>
  93. static inline return_type one_degenerate(Segment const& ,
  94. Ratio const& ratio, bool a_degenerate)
  95. {
  96. return_type result;
  97. if (a_degenerate)
  98. {
  99. // IP lies on ratio w.r.t. segment b
  100. result.assign(Ratio::zero(), ratio);
  101. }
  102. else
  103. {
  104. result.assign(ratio, Ratio::zero());
  105. }
  106. return result;
  107. }
  108. };
  109. }} // namespace policies::relate
  110. }} // namespace boost::geometry
  111. #endif // BOOST_GEOMETRY_GEOMETRY_POLICIES_RELATE_INTERSECTION_RATIOS_HPP