1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2
3 // Copyright (c) 2011-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2017 Adam Wulkiewicz, Lodz, Poland.
5
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
9
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)
13
14 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_HAS_SELF_INTERSECTIONS_HPP
15 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_HAS_SELF_INTERSECTIONS_HPP
16
17 #include <deque>
18
19 #include <boost/range.hpp>
20 #include <boost/throw_exception.hpp>
21
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>
26
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>
31
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>
35 #endif
36
37
38 namespace boost { namespace geometry
39 {
40
41
42 #if ! defined(BOOST_GEOMETRY_OVERLAY_NO_THROW)
43
44 /*!
45 \brief Overlay Invalid Input Exception
46 \ingroup overlay
47 \details The overlay_invalid_input_exception is thrown at invalid input
48 */
49 class overlay_invalid_input_exception : public geometry::exception
50 {
51 public:
52
overlay_invalid_input_exception()53 inline overlay_invalid_input_exception() {}
54
what() const55 virtual char const* what() const throw()
56 {
57 return "Boost.Geometry Overlay invalid input exception";
58 }
59 };
60
61 #endif
62
63
64 #ifndef DOXYGEN_NO_DETAIL
65 namespace detail { namespace overlay
66 {
67
68
69 template <typename Geometry, typename Strategy, typename RobustPolicy>
has_self_intersections(Geometry const & geometry,Strategy const & strategy,RobustPolicy const & robust_policy,bool throw_on_self_intersection=true)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)
74 {
75 typedef typename point_type<Geometry>::type point_type;
76 typedef turn_info
77 <
78 point_type,
79 typename segment_ratio_type<point_type, RobustPolicy>::type
80 > turn_info;
81 std::deque<turn_info> turns;
82 detail::disjoint::disjoint_interrupt_policy policy;
83
84 detail::self_get_turn_points::self_turns<false, detail::overlay::assign_null_policy>(geometry, strategy, robust_policy, turns, policy);
85
86 #ifdef BOOST_GEOMETRY_DEBUG_HAS_SELF_INTERSECTIONS
87 bool first = true;
88 #endif
89 for(typename std::deque<turn_info>::const_iterator it = boost::begin(turns);
90 it != boost::end(turns); ++it)
91 {
92 turn_info const& info = *it;
93 bool const both_union_turn =
94 info.operations[0].operation == detail::overlay::operation_union
95 && info.operations[1].operation == detail::overlay::operation_union;
96 bool const both_intersection_turn =
97 info.operations[0].operation == detail::overlay::operation_intersection
98 && info.operations[1].operation == detail::overlay::operation_intersection;
99
100 bool const valid = (both_union_turn || both_intersection_turn)
101 && (info.method == detail::overlay::method_touch
102 || info.method == detail::overlay::method_touch_interior);
103
104 if (! valid)
105 {
106 #ifdef BOOST_GEOMETRY_DEBUG_HAS_SELF_INTERSECTIONS
107 if (first)
108 {
109 std::cout << "turn points: " << std::endl;
110 first = false;
111 }
112 std::cout << method_char(info.method);
113 for (int i = 0; i < 2; i++)
114 {
115 std::cout << " " << operation_char(info.operations[i].operation);
116 std::cout << " " << info.operations[i].seg_id;
117 }
118 std::cout << " " << geometry::dsv(info.point) << std::endl;
119 #endif
120
121 #if ! defined(BOOST_GEOMETRY_OVERLAY_NO_THROW)
122 if (throw_on_self_intersection)
123 {
124 BOOST_THROW_EXCEPTION(overlay_invalid_input_exception());
125 }
126 #endif
127 return true;
128 }
129
130 }
131 return false;
132 }
133
134 // For backward compatibility
135 template <typename Geometry>
has_self_intersections(Geometry const & geometry,bool throw_on_self_intersection=true)136 inline bool has_self_intersections(Geometry const& geometry,
137 bool throw_on_self_intersection = true)
138 {
139 typedef typename geometry::point_type<Geometry>::type point_type;
140 typedef typename geometry::rescale_policy_type<point_type>::type
141 rescale_policy_type;
142
143 typename strategy::intersection::services::default_strategy
144 <
145 typename cs_tag<Geometry>::type
146 >::type strategy;
147
148 rescale_policy_type robust_policy
149 = geometry::get_rescale_policy<rescale_policy_type>(geometry);
150
151 return has_self_intersections(geometry, strategy, robust_policy,
152 throw_on_self_intersection);
153 }
154
155
156 }} // namespace detail::overlay
157 #endif // DOXYGEN_NO_DETAIL
158
159
160 }} // namespace boost::geometry
161
162
163 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_HAS_SELF_INTERSECTIONS_HPP
164
165