has_self_intersections.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2011-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2017 Adam Wulkiewicz, Lodz, Poland.
  4. // This file was modified by Oracle on 2017-2020.
  5. // Modifications copyright (c) 2017-2020 Oracle and/or its affiliates.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_HAS_SELF_INTERSECTIONS_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_HAS_SELF_INTERSECTIONS_HPP
  12. #include <deque>
  13. #include <boost/range/begin.hpp>
  14. #include <boost/range/end.hpp>
  15. #include <boost/throw_exception.hpp>
  16. #include <boost/geometry/core/point_type.hpp>
  17. #include <boost/geometry/algorithms/detail/overlay/turn_info.hpp>
  18. #include <boost/geometry/algorithms/detail/overlay/get_turns.hpp>
  19. #include <boost/geometry/algorithms/detail/overlay/self_turn_points.hpp>
  20. #include <boost/geometry/policies/disjoint_interrupt_policy.hpp>
  21. #include <boost/geometry/policies/robustness/robust_point_type.hpp>
  22. #include <boost/geometry/policies/robustness/segment_ratio_type.hpp>
  23. #include <boost/geometry/policies/robustness/get_rescale_policy.hpp>
  24. #ifdef BOOST_GEOMETRY_DEBUG_HAS_SELF_INTERSECTIONS
  25. # include <boost/geometry/algorithms/detail/overlay/debug_turn_info.hpp>
  26. # include <boost/geometry/io/dsv/write.hpp>
  27. #endif
  28. namespace boost { namespace geometry
  29. {
  30. #if ! defined(BOOST_GEOMETRY_OVERLAY_NO_THROW)
  31. /*!
  32. \brief Overlay Invalid Input Exception
  33. \ingroup overlay
  34. \details The overlay_invalid_input_exception is thrown at invalid input
  35. */
  36. class overlay_invalid_input_exception : public geometry::exception
  37. {
  38. public:
  39. inline overlay_invalid_input_exception() {}
  40. char const* what() const noexcept override
  41. {
  42. return "Boost.Geometry Overlay invalid input exception";
  43. }
  44. };
  45. #endif
  46. #ifndef DOXYGEN_NO_DETAIL
  47. namespace detail { namespace overlay
  48. {
  49. template <typename Geometry, typename Strategy, typename RobustPolicy>
  50. inline bool has_self_intersections(Geometry const& geometry,
  51. Strategy const& strategy,
  52. RobustPolicy const& robust_policy,
  53. bool throw_on_self_intersection = true)
  54. {
  55. typedef typename point_type<Geometry>::type point_type;
  56. typedef turn_info
  57. <
  58. point_type,
  59. typename segment_ratio_type<point_type, RobustPolicy>::type
  60. > turn_info;
  61. std::deque<turn_info> turns;
  62. detail::disjoint::disjoint_interrupt_policy policy;
  63. detail::self_get_turn_points::self_turns
  64. <
  65. false,
  66. detail::overlay::assign_null_policy
  67. >(geometry, strategy, robust_policy, turns, policy, 0, false);
  68. #ifdef BOOST_GEOMETRY_DEBUG_HAS_SELF_INTERSECTIONS
  69. bool first = true;
  70. #endif
  71. for (auto const& info : turns)
  72. {
  73. bool const both_union_turn =
  74. info.operations[0].operation == detail::overlay::operation_union
  75. && info.operations[1].operation == detail::overlay::operation_union;
  76. bool const both_intersection_turn =
  77. info.operations[0].operation == detail::overlay::operation_intersection
  78. && info.operations[1].operation == detail::overlay::operation_intersection;
  79. bool const valid = (both_union_turn || both_intersection_turn)
  80. && (info.method == detail::overlay::method_touch
  81. || info.method == detail::overlay::method_touch_interior);
  82. if (! valid)
  83. {
  84. #ifdef BOOST_GEOMETRY_DEBUG_HAS_SELF_INTERSECTIONS
  85. if (first)
  86. {
  87. std::cout << "turn points: " << std::endl;
  88. first = false;
  89. }
  90. std::cout << method_char(info.method);
  91. for (int i = 0; i < 2; i++)
  92. {
  93. std::cout << " " << operation_char(info.operations[i].operation);
  94. std::cout << " " << info.operations[i].seg_id;
  95. }
  96. std::cout << " " << geometry::dsv(info.point) << std::endl;
  97. #endif
  98. #if ! defined(BOOST_GEOMETRY_OVERLAY_NO_THROW)
  99. if (throw_on_self_intersection)
  100. {
  101. BOOST_THROW_EXCEPTION(overlay_invalid_input_exception());
  102. }
  103. #endif
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. }} // namespace detail::overlay
  110. #endif // DOXYGEN_NO_DETAIL
  111. }} // namespace boost::geometry
  112. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_HAS_SELF_INTERSECTIONS_HPP