has_self_intersections.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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, 2019.
  5. // Modifications copyright (c) 2017, 2019 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.hpp>
  14. #include <boost/throw_exception.hpp>
  15. #include <boost/geometry/core/point_type.hpp>
  16. #include <boost/geometry/algorithms/detail/overlay/turn_info.hpp>
  17. #include <boost/geometry/algorithms/detail/overlay/get_turns.hpp>
  18. #include <boost/geometry/algorithms/detail/overlay/self_turn_points.hpp>
  19. #include <boost/geometry/policies/disjoint_interrupt_policy.hpp>
  20. #include <boost/geometry/policies/robustness/robust_point_type.hpp>
  21. #include <boost/geometry/policies/robustness/segment_ratio_type.hpp>
  22. #include <boost/geometry/policies/robustness/get_rescale_policy.hpp>
  23. #ifdef BOOST_GEOMETRY_DEBUG_HAS_SELF_INTERSECTIONS
  24. # include <boost/geometry/algorithms/detail/overlay/debug_turn_info.hpp>
  25. # include <boost/geometry/io/dsv/write.hpp>
  26. #endif
  27. namespace boost { namespace geometry
  28. {
  29. #if ! defined(BOOST_GEOMETRY_OVERLAY_NO_THROW)
  30. /*!
  31. \brief Overlay Invalid Input Exception
  32. \ingroup overlay
  33. \details The overlay_invalid_input_exception is thrown at invalid input
  34. */
  35. class overlay_invalid_input_exception : public geometry::exception
  36. {
  37. public:
  38. inline overlay_invalid_input_exception() {}
  39. virtual char const* what() const throw()
  40. {
  41. return "Boost.Geometry Overlay invalid input exception";
  42. }
  43. };
  44. #endif
  45. #ifndef DOXYGEN_NO_DETAIL
  46. namespace detail { namespace overlay
  47. {
  48. template <typename Geometry, typename Strategy, typename RobustPolicy>
  49. inline bool has_self_intersections(Geometry const& geometry,
  50. Strategy const& strategy,
  51. RobustPolicy const& robust_policy,
  52. bool throw_on_self_intersection = true)
  53. {
  54. typedef typename point_type<Geometry>::type point_type;
  55. typedef turn_info
  56. <
  57. point_type,
  58. typename segment_ratio_type<point_type, RobustPolicy>::type
  59. > turn_info;
  60. std::deque<turn_info> turns;
  61. detail::disjoint::disjoint_interrupt_policy policy;
  62. detail::self_get_turn_points::self_turns
  63. <
  64. false,
  65. detail::overlay::assign_null_policy
  66. >(geometry, strategy, robust_policy, turns, policy, 0, false);
  67. #ifdef BOOST_GEOMETRY_DEBUG_HAS_SELF_INTERSECTIONS
  68. bool first = true;
  69. #endif
  70. for(typename std::deque<turn_info>::const_iterator it = boost::begin(turns);
  71. it != boost::end(turns); ++it)
  72. {
  73. turn_info const& info = *it;
  74. bool const both_union_turn =
  75. info.operations[0].operation == detail::overlay::operation_union
  76. && info.operations[1].operation == detail::overlay::operation_union;
  77. bool const both_intersection_turn =
  78. info.operations[0].operation == detail::overlay::operation_intersection
  79. && info.operations[1].operation == detail::overlay::operation_intersection;
  80. bool const valid = (both_union_turn || both_intersection_turn)
  81. && (info.method == detail::overlay::method_touch
  82. || info.method == detail::overlay::method_touch_interior);
  83. if (! valid)
  84. {
  85. #ifdef BOOST_GEOMETRY_DEBUG_HAS_SELF_INTERSECTIONS
  86. if (first)
  87. {
  88. std::cout << "turn points: " << std::endl;
  89. first = false;
  90. }
  91. std::cout << method_char(info.method);
  92. for (int i = 0; i < 2; i++)
  93. {
  94. std::cout << " " << operation_char(info.operations[i].operation);
  95. std::cout << " " << info.operations[i].seg_id;
  96. }
  97. std::cout << " " << geometry::dsv(info.point) << std::endl;
  98. #endif
  99. #if ! defined(BOOST_GEOMETRY_OVERLAY_NO_THROW)
  100. if (throw_on_self_intersection)
  101. {
  102. BOOST_THROW_EXCEPTION(overlay_invalid_input_exception());
  103. }
  104. #endif
  105. return true;
  106. }
  107. }
  108. return false;
  109. }
  110. // For backward compatibility
  111. template <typename Geometry>
  112. inline bool has_self_intersections(Geometry const& geometry,
  113. bool throw_on_self_intersection = true)
  114. {
  115. typedef typename geometry::point_type<Geometry>::type point_type;
  116. typedef typename geometry::rescale_policy_type<point_type>::type
  117. rescale_policy_type;
  118. typename strategy::intersection::services::default_strategy
  119. <
  120. typename cs_tag<Geometry>::type
  121. >::type strategy;
  122. rescale_policy_type robust_policy
  123. = geometry::get_rescale_policy<rescale_policy_type>(geometry, strategy);
  124. return has_self_intersections(geometry, strategy, robust_policy,
  125. throw_on_self_intersection);
  126. }
  127. }} // namespace detail::overlay
  128. #endif // DOXYGEN_NO_DETAIL
  129. }} // namespace boost::geometry
  130. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_HAS_SELF_INTERSECTIONS_HPP