intersection_ratios.hpp 3.2 KB

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