123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_HAS_SELF_INTERSECTIONS_HPP
- #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_HAS_SELF_INTERSECTIONS_HPP
- #include <deque>
- #include <boost/range/begin.hpp>
- #include <boost/range/end.hpp>
- #include <boost/throw_exception.hpp>
- #include <boost/geometry/core/point_type.hpp>
- #include <boost/geometry/algorithms/detail/overlay/turn_info.hpp>
- #include <boost/geometry/algorithms/detail/overlay/get_turns.hpp>
- #include <boost/geometry/algorithms/detail/overlay/self_turn_points.hpp>
- #include <boost/geometry/policies/disjoint_interrupt_policy.hpp>
- #include <boost/geometry/policies/robustness/robust_point_type.hpp>
- #include <boost/geometry/policies/robustness/segment_ratio_type.hpp>
- #include <boost/geometry/policies/robustness/get_rescale_policy.hpp>
- #ifdef BOOST_GEOMETRY_DEBUG_HAS_SELF_INTERSECTIONS
- # include <boost/geometry/algorithms/detail/overlay/debug_turn_info.hpp>
- # include <boost/geometry/io/dsv/write.hpp>
- #endif
- namespace boost { namespace geometry
- {
- #if ! defined(BOOST_GEOMETRY_OVERLAY_NO_THROW)
- class overlay_invalid_input_exception : public geometry::exception
- {
- public:
- inline overlay_invalid_input_exception() {}
- char const* what() const noexcept override
- {
- return "Boost.Geometry Overlay invalid input exception";
- }
- };
- #endif
- #ifndef DOXYGEN_NO_DETAIL
- namespace detail { namespace overlay
- {
- template <typename Geometry, typename Strategy, typename RobustPolicy>
- inline bool has_self_intersections(Geometry const& geometry,
- Strategy const& strategy,
- RobustPolicy const& robust_policy,
- bool throw_on_self_intersection = true)
- {
- typedef typename point_type<Geometry>::type point_type;
- typedef turn_info
- <
- point_type,
- typename segment_ratio_type<point_type, RobustPolicy>::type
- > turn_info;
- std::deque<turn_info> turns;
- detail::disjoint::disjoint_interrupt_policy policy;
- detail::self_get_turn_points::self_turns
- <
- false,
- detail::overlay::assign_null_policy
- >(geometry, strategy, robust_policy, turns, policy, 0, false);
- #ifdef BOOST_GEOMETRY_DEBUG_HAS_SELF_INTERSECTIONS
- bool first = true;
- #endif
- for (auto const& info : turns)
- {
- bool const both_union_turn =
- info.operations[0].operation == detail::overlay::operation_union
- && info.operations[1].operation == detail::overlay::operation_union;
- bool const both_intersection_turn =
- info.operations[0].operation == detail::overlay::operation_intersection
- && info.operations[1].operation == detail::overlay::operation_intersection;
- bool const valid = (both_union_turn || both_intersection_turn)
- && (info.method == detail::overlay::method_touch
- || info.method == detail::overlay::method_touch_interior);
- if (! valid)
- {
- #ifdef BOOST_GEOMETRY_DEBUG_HAS_SELF_INTERSECTIONS
- if (first)
- {
- std::cout << "turn points: " << std::endl;
- first = false;
- }
- std::cout << method_char(info.method);
- for (int i = 0; i < 2; i++)
- {
- std::cout << " " << operation_char(info.operations[i].operation);
- std::cout << " " << info.operations[i].seg_id;
- }
- std::cout << " " << geometry::dsv(info.point) << std::endl;
- #endif
- #if ! defined(BOOST_GEOMETRY_OVERLAY_NO_THROW)
- if (throw_on_self_intersection)
- {
- BOOST_THROW_EXCEPTION(overlay_invalid_input_exception());
- }
- #endif
- return true;
- }
- }
- return false;
- }
- }}
- #endif
- }}
- #endif
|