1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
5 // Use, modification and distribution is subject to the Boost Software License,
6 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_BACKTRACK_CHECK_SI_HPP
10 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_BACKTRACK_CHECK_SI_HPP
15 #include <boost/range.hpp>
17 #include <boost/geometry/core/access.hpp>
18 #include <boost/geometry/algorithms/detail/overlay/turn_info.hpp>
19 #include <boost/geometry/algorithms/detail/has_self_intersections.hpp>
20 #if defined(BOOST_GEOMETRY_DEBUG_INTERSECTION) || defined(BOOST_GEOMETRY_OVERLAY_REPORT_WKT)
21 # include <boost/geometry/algorithms/detail/overlay/debug_turn_info.hpp>
22 # include <boost/geometry/io/wkt/wkt.hpp>
25 namespace boost { namespace geometry
28 #ifndef DOXYGEN_NO_DETAIL
29 namespace detail { namespace overlay
32 template <typename Turns>
33 inline void clear_visit_info(Turns& turns)
35 typedef typename boost::range_value<Turns>::type tp_type;
37 for (typename boost::range_iterator<Turns>::type
38 it = boost::begin(turns);
39 it != boost::end(turns);
42 for (typename boost::range_iterator
44 typename tp_type::container_type
45 >::type op_it = boost::begin(it->operations);
46 op_it != boost::end(it->operations);
49 op_it->visited.clear();
54 struct backtrack_state
58 inline backtrack_state() : m_good(true) {}
59 inline void reset() { m_good = true; }
60 inline bool good() const { return m_good; }
64 enum traverse_error_type
67 traverse_error_no_next_ip_at_start,
68 traverse_error_no_next_ip,
69 traverse_error_dead_end_at_start,
70 traverse_error_dead_end,
71 traverse_error_visit_again,
72 traverse_error_endless_loop
75 inline std::string traverse_error_string(traverse_error_type error)
79 case traverse_error_none : return "";
80 case traverse_error_no_next_ip_at_start : return "No next IP at start";
81 case traverse_error_no_next_ip : return "No next IP";
82 case traverse_error_dead_end_at_start : return "Dead end at start";
83 case traverse_error_dead_end : return "Dead end";
84 case traverse_error_visit_again : return "Visit again";
85 case traverse_error_endless_loop : return "Endless loop";
97 class backtrack_check_self_intersections
99 struct state : public backtrack_state
107 typedef state state_type;
109 template <typename Operation, typename Rings, typename Ring, typename Turns, typename RobustPolicy, typename Visitor>
110 static inline void apply(std::size_t size_at_start,
111 Rings& rings, Ring& ring,
113 typename boost::range_value<Turns>::type const& turn,
114 Operation& operation,
115 traverse_error_type traverse_error,
116 Geometry1 const& geometry1,
117 Geometry2 const& geometry2,
118 RobustPolicy const& robust_policy,
123 visitor.visit_traverse_reject(turns, turn, operation, traverse_error);
125 state.m_good = false;
127 // Check self-intersections and throw exception if appropriate
128 if (! state.m_checked)
130 state.m_checked = true;
131 has_self_intersections(geometry1, robust_policy);
132 has_self_intersections(geometry2, robust_policy);
135 // Make bad output clean
136 rings.resize(size_at_start);
137 geometry::traits::clear<typename boost::range_value<Rings>::type>::apply(ring);
139 // Reject this as a starting point
140 operation.visited.set_rejected();
142 // And clear all visit info
143 clear_visit_info(turns);
147 #ifdef BOOST_GEOMETRY_OVERLAY_REPORT_WKT
153 class backtrack_debug
156 typedef backtrack_state state_type;
158 template <typename Operation, typename Rings, typename Turns>
159 static inline void apply(std::size_t size_at_start,
160 Rings& rings, typename boost::range_value<Rings>::type& ring,
161 Turns& turns, Operation& operation,
162 std::string const& reason,
163 Geometry1 const& geometry1,
164 Geometry2 const& geometry2,
168 std::cout << " REJECT " << reason << std::endl;
170 state.m_good = false;
172 rings.resize(size_at_start);
174 operation.visited.set_rejected();
175 clear_visit_info(turns);
178 for (int i = 0; i < turns.size(); i++)
180 for (int j = 0; j < 2; j++)
182 if (turns[i].operations[j].visited.rejected())
188 std::cout << "BACKTRACK (" << reason << " )"
189 << " " << c << " of " << turns.size() << " rejected"
192 << geometry::wkt(geometry1) << std::endl
193 << geometry::wkt(geometry2) << std::endl;
196 #endif // BOOST_GEOMETRY_OVERLAY_REPORT_WKT
198 }} // namespace detail::overlay
199 #endif // DOXYGEN_NO_DETAIL
201 }} // namespace boost::geometry
203 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_BACKTRACK_CHECK_SI_HPP