1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
5 // This file was modified by Oracle on 2013, 2014.
6 // Modifications copyright (c) 2013-2014 Oracle and/or its affiliates.
8 // Use, modification and distribution is subject to the Boost Software License,
9 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
10 // http://www.boost.org/LICENSE_1_0.txt)
12 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
14 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_FOLLOW_HELPERS_HPP
15 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_FOLLOW_HELPERS_HPP
17 #include <boost/geometry/core/assert.hpp>
19 #include <boost/geometry/util/condition.hpp>
20 #include <boost/geometry/util/range.hpp>
21 //#include <boost/geometry/algorithms/detail/sub_range.hpp>
23 namespace boost { namespace geometry
26 #ifndef DOXYGEN_NO_DETAIL
27 namespace detail { namespace relate {
29 // NOTE: This iterates through single geometries for which turns were not generated.
30 // It doesn't mean that the geometry is disjoint, only that no turns were detected.
32 template <std::size_t OpId,
34 typename Tag = typename geometry::tag<Geometry>::type,
35 bool IsMulti = boost::is_base_of<multi_tag, Tag>::value
37 struct for_each_disjoint_geometry_if
38 : public not_implemented<Tag>
41 template <std::size_t OpId, typename Geometry, typename Tag>
42 struct for_each_disjoint_geometry_if<OpId, Geometry, Tag, false>
44 template <typename TurnIt, typename Pred>
45 static inline bool apply(TurnIt first, TurnIt last,
46 Geometry const& geometry,
56 template <std::size_t OpId, typename Geometry, typename Tag>
57 struct for_each_disjoint_geometry_if<OpId, Geometry, Tag, true>
59 template <typename TurnIt, typename Pred>
60 static inline bool apply(TurnIt first, TurnIt last,
61 Geometry const& geometry,
65 return for_turns(first, last, geometry, pred);
67 return for_empty(geometry, pred);
70 template <typename Pred>
71 static inline bool for_empty(Geometry const& geometry,
74 typedef typename boost::range_iterator<Geometry const>::type iterator;
77 // check predicate for each contained geometry without generated turn
78 for ( iterator it = boost::begin(geometry) ;
79 it != boost::end(geometry) ; ++it )
81 bool cont = pred(*it);
86 return !boost::empty(geometry);
89 template <typename TurnIt, typename Pred>
90 static inline bool for_turns(TurnIt first, TurnIt last,
91 Geometry const& geometry,
94 BOOST_GEOMETRY_ASSERT(first != last);
96 const std::size_t count = boost::size(geometry);
97 boost::ignore_unused_variable_warning(count);
100 // gather info about turns generated for contained geometries
101 std::vector<bool> detected_intersections(count, false);
102 for ( TurnIt it = first ; it != last ; ++it )
104 signed_size_type multi_index = it->operations[OpId].seg_id.multi_index;
105 BOOST_GEOMETRY_ASSERT(multi_index >= 0);
106 std::size_t const index = static_cast<std::size_t>(multi_index);
107 BOOST_GEOMETRY_ASSERT(index < count);
108 detected_intersections[index] = true;
114 // check predicate for each contained geometry without generated turn
115 for ( std::vector<bool>::iterator it = detected_intersections.begin() ;
116 it != detected_intersections.end() ; ++it )
118 // if there were no intersections for this multi_index
122 std::size_t const index = std::size_t(std::distance(detected_intersections.begin(), it));
123 bool cont = pred(range::at(geometry, index));
133 // WARNING! This class stores pointers!
134 // Passing a reference to local variable will result in undefined behavior!
135 template <typename Point>
139 point_info() : sid_ptr(NULL), pt_ptr(NULL) {}
140 point_info(Point const& pt, segment_identifier const& sid)
141 : sid_ptr(boost::addressof(sid))
142 , pt_ptr(boost::addressof(pt))
144 segment_identifier const& seg_id() const
146 BOOST_GEOMETRY_ASSERT(sid_ptr);
149 Point const& point() const
151 BOOST_GEOMETRY_ASSERT(pt_ptr);
155 //friend bool operator==(point_identifier const& l, point_identifier const& r)
157 // return l.seg_id() == r.seg_id()
158 // && detail::equals::equals_point_point(l.point(), r.point());
162 const segment_identifier * sid_ptr;
163 const Point * pt_ptr;
166 // WARNING! This class stores pointers!
167 // Passing a reference to local variable will result in undefined behavior!
171 same_single(segment_identifier const& sid)
172 : sid_ptr(boost::addressof(sid))
175 bool operator()(segment_identifier const& sid) const
177 return sid.multi_index == sid_ptr->multi_index;
180 template <typename Point>
181 bool operator()(point_info<Point> const& pid) const
183 return operator()(pid.seg_id());
187 const segment_identifier * sid_ptr;
193 same_ring(segment_identifier const& sid)
194 : sid_ptr(boost::addressof(sid))
197 bool operator()(segment_identifier const& sid) const
199 return sid.multi_index == sid_ptr->multi_index
200 && sid.ring_index == sid_ptr->ring_index;
204 const segment_identifier * sid_ptr;
207 // WARNING! This class stores pointers!
208 // Passing a reference to local variable will result in undefined behavior!
209 template <typename SameRange = same_single>
210 class segment_watcher
217 bool update(segment_identifier const& seg_id)
219 bool result = m_seg_id_ptr == 0 || !SameRange(*m_seg_id_ptr)(seg_id);
220 m_seg_id_ptr = boost::addressof(seg_id);
225 const segment_identifier * m_seg_id_ptr;
228 // WARNING! This class stores pointers!
229 // Passing a reference to local variable will result in undefined behavior!
230 template <typename TurnInfo, std::size_t OpId>
233 static const std::size_t op_id = OpId;
234 static const std::size_t other_op_id = (OpId + 1) % 2;
236 typedef typename TurnInfo::point_type point_type;
237 typedef detail::relate::point_info<point_type> point_info;
241 : m_exit_operation(overlay::operation_none)
242 , m_exit_turn_ptr(NULL)
245 void enter(TurnInfo const& turn)
247 m_other_entry_points.push_back(
248 point_info(turn.point, turn.operations[other_op_id].seg_id) );
251 // TODO: exit_per_geometry parameter looks not very safe
252 // wrong value may be easily passed
254 void exit(TurnInfo const& turn, bool exit_per_geometry = true)
256 //segment_identifier const& seg_id = turn.operations[op_id].seg_id;
257 segment_identifier const& other_id = turn.operations[other_op_id].seg_id;
258 overlay::operation_type exit_op = turn.operations[op_id].operation;
260 typedef typename std::vector<point_info>::iterator point_iterator;
261 // search for the entry point in the same range of other geometry
262 point_iterator entry_it = std::find_if(m_other_entry_points.begin(),
263 m_other_entry_points.end(),
264 same_single(other_id));
266 // this end point has corresponding entry point
267 if ( entry_it != m_other_entry_points.end() )
269 // erase the corresponding entry point
270 m_other_entry_points.erase(entry_it);
272 if ( exit_per_geometry || m_other_entry_points.empty() )
274 // here we know that we possibly left LS
275 // we must still check if we didn't get back on the same point
276 m_exit_operation = exit_op;
277 m_exit_turn_ptr = boost::addressof(turn);
282 bool is_outside() const
284 // if we didn't entered anything in the past, we're outside
285 return m_other_entry_points.empty();
288 bool is_outside(TurnInfo const& turn) const
290 return m_other_entry_points.empty()
291 || std::find_if(m_other_entry_points.begin(),
292 m_other_entry_points.end(),
294 turn.operations[other_op_id].seg_id))
295 == m_other_entry_points.end();
298 overlay::operation_type get_exit_operation() const
300 return m_exit_operation;
303 point_type const& get_exit_point() const
305 BOOST_GEOMETRY_ASSERT(m_exit_operation != overlay::operation_none);
306 BOOST_GEOMETRY_ASSERT(m_exit_turn_ptr);
307 return m_exit_turn_ptr->point;
310 TurnInfo const& get_exit_turn() const
312 BOOST_GEOMETRY_ASSERT(m_exit_operation != overlay::operation_none);
313 BOOST_GEOMETRY_ASSERT(m_exit_turn_ptr);
314 return *m_exit_turn_ptr;
317 void reset_detected_exit()
319 m_exit_operation = overlay::operation_none;
324 m_exit_operation = overlay::operation_none;
325 m_other_entry_points.clear();
329 overlay::operation_type m_exit_operation;
330 const TurnInfo * m_exit_turn_ptr;
331 std::vector<point_info> m_other_entry_points; // TODO: use map here or sorted vector?
334 template <std::size_t OpId, typename Turn>
335 inline bool turn_on_the_same_ip(Turn const& prev_turn, Turn const& curr_turn)
337 segment_identifier const& prev_seg_id = prev_turn.operations[OpId].seg_id;
338 segment_identifier const& curr_seg_id = curr_turn.operations[OpId].seg_id;
340 if ( prev_seg_id.multi_index != curr_seg_id.multi_index
341 || prev_seg_id.ring_index != curr_seg_id.ring_index )
346 // TODO: will this work if between segments there will be some number of degenerated ones?
348 if ( prev_seg_id.segment_index != curr_seg_id.segment_index
349 && ( ! curr_turn.operations[OpId].fraction.is_zero()
350 || prev_seg_id.segment_index + 1 != curr_seg_id.segment_index ) )
355 return detail::equals::equals_point_point(prev_turn.point, curr_turn.point);
358 template <boundary_query BoundaryQuery,
360 typename BoundaryChecker>
361 static inline bool is_endpoint_on_boundary(Point const& pt,
362 BoundaryChecker & boundary_checker)
364 return boundary_checker.template is_endpoint_boundary<BoundaryQuery>(pt);
367 template <boundary_query BoundaryQuery,
368 typename IntersectionPoint,
369 typename OperationInfo,
370 typename BoundaryChecker>
371 static inline bool is_ip_on_boundary(IntersectionPoint const& ip,
372 OperationInfo const& operation_info,
373 BoundaryChecker & boundary_checker,
374 segment_identifier const& seg_id)
376 boost::ignore_unused_variable_warning(seg_id);
380 // IP on the last point of the linestring
381 if ( BOOST_GEOMETRY_CONDITION(BoundaryQuery == boundary_back || BoundaryQuery == boundary_any)
382 && operation_info.position == overlay::position_back )
384 // check if this point is a boundary
385 res = boundary_checker.template is_endpoint_boundary<boundary_back>(ip);
387 // IP on the last point of the linestring
388 else if ( BOOST_GEOMETRY_CONDITION(BoundaryQuery == boundary_front || BoundaryQuery == boundary_any)
389 && operation_info.position == overlay::position_front )
391 // check if this point is a boundary
392 res = boundary_checker.template is_endpoint_boundary<boundary_front>(ip);
399 }} // namespace detail::relate
400 #endif // DOXYGEN_NO_DETAIL
402 }} // namespace boost::geometry
404 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_FOLLOW_HELPERS_HPP