compare_turns.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2014-2015, Oracle and/or its affiliates.
  3. // Licensed under the Boost Software License version 1.0.
  4. // http://www.boost.org/users/license.html
  5. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  6. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_TURNS_COMPARE_TURNS_HPP
  7. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_TURNS_COMPARE_TURNS_HPP
  8. #include <cstddef>
  9. #include <functional>
  10. #include <boost/geometry/util/math.hpp>
  11. #include <boost/geometry/algorithms/detail/overlay/turn_info.hpp>
  12. namespace boost { namespace geometry
  13. {
  14. namespace detail { namespace turns
  15. {
  16. // TURNS SORTING AND SEARCHING
  17. // sort turns by G1 - source_index == 0 by:
  18. // seg_id -> fraction -> other_id -> operation
  19. template
  20. <
  21. typename IdLess = std::less<signed_size_type>,
  22. int N = 0, int U = 1, int I = 2, int B = 3, int C = 4, int O = 0,
  23. std::size_t OpId = 0
  24. >
  25. struct less_seg_fraction_other_op
  26. {
  27. BOOST_STATIC_ASSERT(OpId < 2);
  28. static const std::size_t other_op_id = (OpId + 1) % 2;
  29. template <typename Op>
  30. static inline int order_op(Op const& op)
  31. {
  32. switch (op.operation)
  33. {
  34. case detail::overlay::operation_none : return N;
  35. case detail::overlay::operation_union : return U;
  36. case detail::overlay::operation_intersection : return I;
  37. case detail::overlay::operation_blocked : return B;
  38. case detail::overlay::operation_continue : return C;
  39. case detail::overlay::operation_opposite : return O;
  40. }
  41. return -1;
  42. }
  43. template <typename Op>
  44. static inline bool use_operation(Op const& left, Op const& right)
  45. {
  46. return order_op(left) < order_op(right);
  47. }
  48. template <typename Turn>
  49. static inline bool use_other_id(Turn const& left, Turn const& right)
  50. {
  51. segment_identifier const& left_other_seg_id = left.operations[other_op_id].seg_id;
  52. segment_identifier const& right_other_seg_id = right.operations[other_op_id].seg_id;
  53. if ( left_other_seg_id.multi_index != right_other_seg_id.multi_index )
  54. {
  55. return left_other_seg_id.multi_index < right_other_seg_id.multi_index;
  56. }
  57. if ( left_other_seg_id.ring_index != right_other_seg_id.ring_index )
  58. {
  59. return left_other_seg_id.ring_index != right_other_seg_id.ring_index;
  60. }
  61. if ( left_other_seg_id.segment_index != right_other_seg_id.segment_index )
  62. {
  63. return IdLess()(left_other_seg_id.segment_index,
  64. right_other_seg_id.segment_index);
  65. }
  66. return use_operation(left.operations[OpId], right.operations[OpId]);
  67. }
  68. template <typename Turn>
  69. static inline bool use_fraction(Turn const& left, Turn const& right)
  70. {
  71. return
  72. geometry::math::equals(left.operations[OpId].fraction,
  73. right.operations[OpId].fraction)
  74. ?
  75. use_other_id(left, right)
  76. :
  77. (left.operations[OpId].fraction < right.operations[OpId].fraction)
  78. ;
  79. }
  80. template <typename Turn>
  81. inline bool operator()(Turn const& left, Turn const& right) const
  82. {
  83. segment_identifier const& sl = left.operations[OpId].seg_id;
  84. segment_identifier const& sr = right.operations[OpId].seg_id;
  85. return sl < sr || ( sl == sr && use_fraction(left, right) );
  86. }
  87. };
  88. }} // namespace detail::turns
  89. }} // namespace boost::geometry
  90. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_TURNS_COMPARE_TURNS_HPP