1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2011-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2017 Adam Wulkiewicz, Lodz, Poland.
6 // This file was modified by Oracle on 2017.
7 // Modifications copyright (c) 2017 Oracle and/or its affiliates.
8 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
10 // Use, modification and distribution is subject to the Boost Software License,
11 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
12 // http://www.boost.org/LICENSE_1_0.txt)
14 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_HAS_SELF_INTERSECTIONS_HPP
15 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_HAS_SELF_INTERSECTIONS_HPP
19 #include <boost/range.hpp>
20 #include <boost/throw_exception.hpp>
22 #include <boost/geometry/core/point_type.hpp>
23 #include <boost/geometry/algorithms/detail/overlay/turn_info.hpp>
24 #include <boost/geometry/algorithms/detail/overlay/get_turns.hpp>
25 #include <boost/geometry/algorithms/detail/overlay/self_turn_points.hpp>
27 #include <boost/geometry/policies/disjoint_interrupt_policy.hpp>
28 #include <boost/geometry/policies/robustness/robust_point_type.hpp>
29 #include <boost/geometry/policies/robustness/segment_ratio_type.hpp>
30 #include <boost/geometry/policies/robustness/get_rescale_policy.hpp>
32 #ifdef BOOST_GEOMETRY_DEBUG_HAS_SELF_INTERSECTIONS
33 # include <boost/geometry/algorithms/detail/overlay/debug_turn_info.hpp>
34 # include <boost/geometry/io/dsv/write.hpp>
38 namespace boost { namespace geometry
42 #if ! defined(BOOST_GEOMETRY_OVERLAY_NO_THROW)
45 \brief Overlay Invalid Input Exception
47 \details The overlay_invalid_input_exception is thrown at invalid input
49 class overlay_invalid_input_exception : public geometry::exception
53 inline overlay_invalid_input_exception() {}
55 virtual char const* what() const throw()
57 return "Boost.Geometry Overlay invalid input exception";
64 #ifndef DOXYGEN_NO_DETAIL
65 namespace detail { namespace overlay
69 template <typename Geometry, typename Strategy, typename RobustPolicy>
70 inline bool has_self_intersections(Geometry const& geometry,
71 Strategy const& strategy,
72 RobustPolicy const& robust_policy,
73 bool throw_on_self_intersection = true)
75 typedef typename point_type<Geometry>::type point_type;
79 typename segment_ratio_type<point_type, RobustPolicy>::type
81 std::deque<turn_info> turns;
82 detail::disjoint::disjoint_interrupt_policy policy;
84 detail::self_get_turn_points::self_turns
87 detail::overlay::assign_null_policy
88 >(geometry, strategy, robust_policy, turns, policy, 0, false);
90 #ifdef BOOST_GEOMETRY_DEBUG_HAS_SELF_INTERSECTIONS
93 for(typename std::deque<turn_info>::const_iterator it = boost::begin(turns);
94 it != boost::end(turns); ++it)
96 turn_info const& info = *it;
97 bool const both_union_turn =
98 info.operations[0].operation == detail::overlay::operation_union
99 && info.operations[1].operation == detail::overlay::operation_union;
100 bool const both_intersection_turn =
101 info.operations[0].operation == detail::overlay::operation_intersection
102 && info.operations[1].operation == detail::overlay::operation_intersection;
104 bool const valid = (both_union_turn || both_intersection_turn)
105 && (info.method == detail::overlay::method_touch
106 || info.method == detail::overlay::method_touch_interior);
110 #ifdef BOOST_GEOMETRY_DEBUG_HAS_SELF_INTERSECTIONS
113 std::cout << "turn points: " << std::endl;
116 std::cout << method_char(info.method);
117 for (int i = 0; i < 2; i++)
119 std::cout << " " << operation_char(info.operations[i].operation);
120 std::cout << " " << info.operations[i].seg_id;
122 std::cout << " " << geometry::dsv(info.point) << std::endl;
125 #if ! defined(BOOST_GEOMETRY_OVERLAY_NO_THROW)
126 if (throw_on_self_intersection)
128 BOOST_THROW_EXCEPTION(overlay_invalid_input_exception());
138 // For backward compatibility
139 template <typename Geometry>
140 inline bool has_self_intersections(Geometry const& geometry,
141 bool throw_on_self_intersection = true)
143 typedef typename geometry::point_type<Geometry>::type point_type;
144 typedef typename geometry::rescale_policy_type<point_type>::type
147 typename strategy::intersection::services::default_strategy
149 typename cs_tag<Geometry>::type
152 rescale_policy_type robust_policy
153 = geometry::get_rescale_policy<rescale_policy_type>(geometry);
155 return has_self_intersections(geometry, strategy, robust_policy,
156 throw_on_self_intersection);
160 }} // namespace detail::overlay
161 #endif // DOXYGEN_NO_DETAIL
164 }} // namespace boost::geometry
167 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_HAS_SELF_INTERSECTIONS_HPP