3 // Copyright (c) 2017, 2019 Oracle and/or its affiliates.
5 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
7 // Use, modification and distribution is subject to the Boost Software License,
8 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_WITHIN_MULTI_POINT_HPP
12 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_WITHIN_MULTI_POINT_HPP
18 #include <boost/range.hpp>
19 #include <boost/type_traits/is_same.hpp>
21 #include <boost/geometry/algorithms/detail/disjoint/box_box.hpp>
22 #include <boost/geometry/algorithms/detail/disjoint/point_box.hpp>
23 #include <boost/geometry/algorithms/detail/expand_by_epsilon.hpp>
24 #include <boost/geometry/algorithms/detail/within/point_in_geometry.hpp>
25 #include <boost/geometry/algorithms/envelope.hpp>
26 #include <boost/geometry/algorithms/detail/partition.hpp>
27 #include <boost/geometry/core/tag.hpp>
28 #include <boost/geometry/core/tag_cast.hpp>
29 #include <boost/geometry/core/tags.hpp>
31 #include <boost/geometry/geometries/box.hpp>
33 #include <boost/geometry/index/rtree.hpp>
35 #include <boost/geometry/policies/compare.hpp>
37 #include <boost/geometry/strategies/covered_by.hpp>
38 #include <boost/geometry/strategies/disjoint.hpp>
41 namespace boost { namespace geometry {
43 #ifndef DOXYGEN_NO_DETAIL
44 namespace detail { namespace within {
46 struct multi_point_point
48 template <typename MultiPoint, typename Point, typename Strategy>
49 static inline bool apply(MultiPoint const& multi_point,
51 Strategy const& strategy)
53 typedef typename boost::range_const_iterator<MultiPoint>::type iterator;
54 for ( iterator it = boost::begin(multi_point) ; it != boost::end(multi_point) ; ++it )
56 if (! strategy.apply(*it, point))
62 // all points of MultiPoint inside Point
67 // NOTE: currently the strategy is ignored, math::equals() is used inside geometry::less<>
68 struct multi_point_multi_point
70 template <typename MultiPoint1, typename MultiPoint2, typename Strategy>
71 static inline bool apply(MultiPoint1 const& multi_point1,
72 MultiPoint2 const& multi_point2,
73 Strategy const& /*strategy*/)
75 typedef typename boost::range_value<MultiPoint2>::type point2_type;
76 typedef typename Strategy::cs_tag cs_tag;
77 typedef geometry::less<void, -1, cs_tag> less_type;
79 less_type const less = less_type();
81 std::vector<point2_type> points2(boost::begin(multi_point2), boost::end(multi_point2));
82 std::sort(points2.begin(), points2.end(), less);
86 typedef typename boost::range_const_iterator<MultiPoint1>::type iterator;
87 for ( iterator it = boost::begin(multi_point1) ; it != boost::end(multi_point1) ; ++it )
89 if (! std::binary_search(points2.begin(), points2.end(), *it, less))
104 // TODO: the complexity could be lesser
105 // the second geometry could be "prepared"/sorted
106 // For Linear geometries partition could be used
107 // For Areal geometries point_in_geometry() would have to call the winding
108 // strategy differently, currently it linearly calls the strategy for each
109 // segment. So the segments would have to be sorted in a way consistent with
110 // the strategy and then the strategy called only for the segments in range.
111 template <bool Within>
112 struct multi_point_single_geometry
114 template <typename MultiPoint, typename LinearOrAreal, typename Strategy>
115 static inline bool apply(MultiPoint const& multi_point,
116 LinearOrAreal const& linear_or_areal,
117 Strategy const& strategy)
119 //typedef typename boost::range_value<MultiPoint>::type point1_type;
120 typedef typename point_type<LinearOrAreal>::type point2_type;
121 typedef model::box<point2_type> box2_type;
123 // Create envelope of geometry
125 geometry::envelope(linear_or_areal, box, strategy.get_envelope_strategy());
126 geometry::detail::expand_by_epsilon(box);
128 typedef typename Strategy::disjoint_point_box_strategy_type point_in_box_type;
130 // Test each Point with envelope and then geometry if needed
131 // If in the exterior, break
134 typedef typename boost::range_const_iterator<MultiPoint>::type iterator;
135 for ( iterator it = boost::begin(multi_point) ; it != boost::end(multi_point) ; ++it )
139 // exterior of box and of geometry
140 if (! point_in_box_type::apply(*it, box)
141 || (in_val = point_in_geometry(*it, linear_or_areal, strategy)) < 0)
147 // interior : interior/boundary
148 if (Within ? in_val > 0 : in_val >= 0)
159 // TODO: same here, probably the complexity could be lesser
160 template <bool Within>
161 struct multi_point_multi_geometry
163 template <typename MultiPoint, typename LinearOrAreal, typename Strategy>
164 static inline bool apply(MultiPoint const& multi_point,
165 LinearOrAreal const& linear_or_areal,
166 Strategy const& strategy)
168 typedef typename point_type<LinearOrAreal>::type point2_type;
169 typedef model::box<point2_type> box2_type;
170 static const bool is_linear = is_same
174 typename tag<LinearOrAreal>::type,
180 typename Strategy::envelope_strategy_type const
181 envelope_strategy = strategy.get_envelope_strategy();
183 // TODO: box pairs could be constructed on the fly, inside the rtree
185 // Prepare range of envelopes and ids
186 std::size_t count2 = boost::size(linear_or_areal);
187 typedef std::pair<box2_type, std::size_t> box_pair_type;
188 typedef std::vector<box_pair_type> box_pair_vector;
189 box_pair_vector boxes(count2);
190 for (std::size_t i = 0 ; i < count2 ; ++i)
192 geometry::envelope(linear_or_areal, boxes[i].first, envelope_strategy);
193 geometry::detail::expand_by_epsilon(boxes[i].first);
198 typedef strategy::index::services::from_strategy
201 > index_strategy_from;
202 typedef index::parameters
204 index::rstar<4>, typename index_strategy_from::type
205 > index_parameters_type;
206 index::rtree<box_pair_type, index_parameters_type>
207 rtree(boxes.begin(), boxes.end(),
208 index_parameters_type(index::rstar<4>(), index_strategy_from::get(strategy)));
210 // For each point find overlapping envelopes and test corresponding single geometries
211 // If a point is in the exterior break
214 typedef typename boost::range_const_iterator<MultiPoint>::type iterator;
215 for ( iterator it = boost::begin(multi_point) ; it != boost::end(multi_point) ; ++it )
217 // TODO: investigate the possibility of using satisfies
218 // TODO: investigate the possibility of using iterative queries (optimization below)
219 box_pair_vector inters_boxes;
220 rtree.query(index::intersects(*it), std::back_inserter(inters_boxes));
222 bool found_interior = false;
223 bool found_boundary = false;
226 typedef typename box_pair_vector::const_iterator box_iterator;
227 for (box_iterator box_it = inters_boxes.begin() ;
228 box_it != inters_boxes.end() ; ++box_it )
230 int const in_val = point_in_geometry(*it,
231 range::at(linear_or_areal, box_it->second), strategy);
235 found_interior = true;
237 else if (in_val == 0)
242 // If the result was set previously (interior or
243 // interior/boundary found) the only thing that needs to be
244 // done for other points is to make sure they're not
245 // overlapping the exterior no need to analyse boundaries.
246 if (result && in_val >= 0)
254 if (is_linear && boundaries % 2 == 0)
256 found_interior = true;
260 found_boundary = true;
265 if (! found_interior && ! found_boundary)
271 // interior : interior/boundary
272 if (Within ? found_interior : (found_interior || found_boundary))
282 }} // namespace detail::within
283 #endif // DOXYGEN_NO_DETAIL
285 }} // namespace boost::geometry
288 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_WITHIN_MULTI_POINT_HPP