• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, 2019.
7 // Modifications copyright (c) 2017, 2019 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
85         <
86             false,
87             detail::overlay::assign_null_policy
88         >(geometry, strategy, robust_policy, turns, policy, 0, false);
89 
90 #ifdef BOOST_GEOMETRY_DEBUG_HAS_SELF_INTERSECTIONS
91     bool first = true;
92 #endif
93     for(typename std::deque<turn_info>::const_iterator it = boost::begin(turns);
94         it != boost::end(turns); ++it)
95     {
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;
103 
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);
107 
108         if (! valid)
109         {
110 #ifdef BOOST_GEOMETRY_DEBUG_HAS_SELF_INTERSECTIONS
111             if (first)
112             {
113                 std::cout << "turn points: " << std::endl;
114                 first = false;
115             }
116             std::cout << method_char(info.method);
117             for (int i = 0; i < 2; i++)
118             {
119                 std::cout << " " << operation_char(info.operations[i].operation);
120                 std::cout << " " << info.operations[i].seg_id;
121             }
122             std::cout << " " << geometry::dsv(info.point) << std::endl;
123 #endif
124 
125 #if ! defined(BOOST_GEOMETRY_OVERLAY_NO_THROW)
126             if (throw_on_self_intersection)
127             {
128                 BOOST_THROW_EXCEPTION(overlay_invalid_input_exception());
129             }
130 #endif
131             return true;
132         }
133 
134     }
135     return false;
136 }
137 
138 // For backward compatibility
139 template <typename Geometry>
has_self_intersections(Geometry const & geometry,bool throw_on_self_intersection=true)140 inline bool has_self_intersections(Geometry const& geometry,
141                     bool throw_on_self_intersection = true)
142 {
143     typedef typename geometry::point_type<Geometry>::type point_type;
144     typedef typename geometry::rescale_policy_type<point_type>::type
145         rescale_policy_type;
146 
147     typename strategy::intersection::services::default_strategy
148         <
149             typename cs_tag<Geometry>::type
150         >::type strategy;
151 
152     rescale_policy_type robust_policy
153         = geometry::get_rescale_policy<rescale_policy_type>(geometry, strategy);
154 
155     return has_self_intersections(geometry, strategy, robust_policy,
156                                   throw_on_self_intersection);
157 }
158 
159 
160 }} // namespace detail::overlay
161 #endif // DOXYGEN_NO_DETAIL
162 
163 
164 }} // namespace boost::geometry
165 
166 
167 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_HAS_SELF_INTERSECTIONS_HPP
168 
169