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, 2017, 2018.
6 // Modifications copyright (c) 2013-2018, 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_RELATE_POINT_POINT_HPP
15 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_POINT_POINT_HPP
20 #include <boost/range/empty.hpp>
22 #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
23 #include <boost/geometry/algorithms/detail/within/point_in_geometry.hpp>
24 #include <boost/geometry/algorithms/detail/relate/result.hpp>
26 #include <boost/geometry/policies/compare.hpp>
28 namespace boost { namespace geometry
31 #ifndef DOXYGEN_NO_DETAIL
32 namespace detail { namespace relate {
34 template <typename Point1, typename Point2>
37 static const bool interruption_enabled = false;
39 template <typename Result, typename Strategy>
40 static inline void apply(Point1 const& point1, Point2 const& point2,
42 Strategy const& strategy)
44 bool equal = detail::equals::equals_point_point(point1, point2, strategy);
47 relate::set<interior, interior, '0'>(result);
51 relate::set<interior, exterior, '0'>(result);
52 relate::set<exterior, interior, '0'>(result);
55 relate::set<exterior, exterior, result_dimension<Point1>::value>(result);
59 template <typename Point, typename MultiPoint, typename EqPPStrategy>
60 std::pair<bool, bool> point_multipoint_check(Point const& point,
61 MultiPoint const& multi_point,
62 EqPPStrategy const& strategy)
64 bool found_inside = false;
65 bool found_outside = false;
67 // point_in_geometry could be used here but why iterate over MultiPoint twice?
68 // we must search for a point in the exterior because all points in MultiPoint can be equal
70 typedef typename boost::range_iterator<MultiPoint const>::type iterator;
71 iterator it = boost::begin(multi_point);
72 iterator last = boost::end(multi_point);
73 for ( ; it != last ; ++it )
75 bool ii = detail::equals::equals_point_point(point, *it, strategy);
82 if ( found_inside && found_outside )
86 return std::make_pair(found_inside, found_outside);
89 template <typename Point, typename MultiPoint, bool Transpose = false>
90 struct point_multipoint
92 static const bool interruption_enabled = false;
94 template <typename Result, typename Strategy>
95 static inline void apply(Point const& point, MultiPoint const& multi_point,
97 Strategy const& strategy)
99 if ( boost::empty(multi_point) )
101 // TODO: throw on empty input?
102 relate::set<interior, exterior, '0', Transpose>(result);
106 std::pair<bool, bool> rel = point_multipoint_check(point, multi_point, strategy);
108 if ( rel.first ) // some point of MP is equal to P
110 relate::set<interior, interior, '0', Transpose>(result);
112 if ( rel.second ) // a point of MP was found outside P
114 relate::set<exterior, interior, '0', Transpose>(result);
119 relate::set<interior, exterior, '0', Transpose>(result);
120 relate::set<exterior, interior, '0', Transpose>(result);
123 relate::set<exterior, exterior, result_dimension<Point>::value, Transpose>(result);
127 template <typename MultiPoint, typename Point>
128 struct multipoint_point
130 static const bool interruption_enabled = false;
132 template <typename Result, typename Strategy>
133 static inline void apply(MultiPoint const& multi_point, Point const& point,
135 Strategy const& strategy)
137 point_multipoint<Point, MultiPoint, true>::apply(point, multi_point, result, strategy);
141 template <typename MultiPoint1, typename MultiPoint2>
142 struct multipoint_multipoint
144 static const bool interruption_enabled = true;
146 template <typename Result, typename Strategy>
147 static inline void apply(MultiPoint1 const& multi_point1, MultiPoint2 const& multi_point2,
149 Strategy const& /*strategy*/)
151 typedef typename Strategy::cs_tag cs_tag;
154 // TODO: throw on empty input?
155 bool empty1 = boost::empty(multi_point1);
156 bool empty2 = boost::empty(multi_point2);
157 if ( empty1 && empty2 )
163 relate::set<exterior, interior, '0'>(result);
168 relate::set<interior, exterior, '0'>(result);
173 // The geometry containing smaller number of points will be analysed first
174 if ( boost::size(multi_point1) < boost::size(multi_point2) )
176 search_both<false, cs_tag>(multi_point1, multi_point2, result);
180 search_both<true, cs_tag>(multi_point2, multi_point1, result);
183 relate::set<exterior, exterior, result_dimension<MultiPoint1>::value>(result);
186 template <bool Transpose, typename CSTag, typename MPt1, typename MPt2, typename Result>
187 static inline void search_both(MPt1 const& first_sorted_mpt, MPt2 const& first_iterated_mpt,
190 if ( relate::may_update<interior, interior, '0'>(result)
191 || relate::may_update<interior, exterior, '0'>(result)
192 || relate::may_update<exterior, interior, '0'>(result) )
195 bool is_disjoint = search<Transpose, CSTag>(first_sorted_mpt, first_iterated_mpt, result);
197 if ( BOOST_GEOMETRY_CONDITION(is_disjoint || result.interrupt) )
201 if ( relate::may_update<interior, interior, '0'>(result)
202 || relate::may_update<interior, exterior, '0'>(result)
203 || relate::may_update<exterior, interior, '0'>(result) )
206 search<! Transpose, CSTag>(first_iterated_mpt, first_sorted_mpt, result);
210 template <bool Transpose,
212 typename SortedMultiPoint,
213 typename IteratedMultiPoint,
215 static inline bool search(SortedMultiPoint const& sorted_mpt,
216 IteratedMultiPoint const& iterated_mpt,
219 // sort points from the 1 MPt
220 typedef typename geometry::point_type<SortedMultiPoint>::type point_type;
221 typedef geometry::less<void, -1, CSTag> less_type;
223 std::vector<point_type> points(boost::begin(sorted_mpt), boost::end(sorted_mpt));
225 less_type const less = less_type();
226 std::sort(points.begin(), points.end(), less);
228 bool found_inside = false;
229 bool found_outside = false;
231 // for each point in the second MPt
232 typedef typename boost::range_iterator<IteratedMultiPoint const>::type iterator;
233 for ( iterator it = boost::begin(iterated_mpt) ;
234 it != boost::end(iterated_mpt) ; ++it )
237 std::binary_search(points.begin(), points.end(), *it, less);
241 found_outside = true;
243 if ( found_inside && found_outside )
247 if ( found_inside ) // some point of MP2 is equal to some of MP1
249 // TODO: if I/I is set for one MPt, this won't be changed when the other one in analysed
250 // so if e.g. only I/I must be analysed we musn't check the other MPt
252 relate::set<interior, interior, '0', Transpose>(result);
254 if ( found_outside ) // some point of MP2 was found outside of MP1
256 relate::set<exterior, interior, '0', Transpose>(result);
261 relate::set<interior, exterior, '0', Transpose>(result);
262 relate::set<exterior, interior, '0', Transpose>(result);
265 // if no point is intersecting the other MPt then we musn't analyse the reversed case
266 return ! found_inside;
270 }} // namespace detail::relate
271 #endif // DOXYGEN_NO_DETAIL
273 }} // namespace boost::geometry
275 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_POINT_POINT_HPP