123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_WITHIN_MULTI_POINT_HPP
- #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_WITHIN_MULTI_POINT_HPP
- #include <algorithm>
- #include <vector>
- #include <boost/range/begin.hpp>
- #include <boost/range/end.hpp>
- #include <boost/range/size.hpp>
- #include <boost/range/value_type.hpp>
- #include <boost/geometry/algorithms/detail/disjoint/box_box.hpp>
- #include <boost/geometry/algorithms/detail/disjoint/point_box.hpp>
- #include <boost/geometry/algorithms/detail/expand_by_epsilon.hpp>
- #include <boost/geometry/algorithms/detail/within/point_in_geometry.hpp>
- #include <boost/geometry/algorithms/envelope.hpp>
- #include <boost/geometry/algorithms/detail/partition.hpp>
- #include <boost/geometry/core/tag.hpp>
- #include <boost/geometry/core/tag_cast.hpp>
- #include <boost/geometry/core/tags.hpp>
- #include <boost/geometry/geometries/box.hpp>
- #include <boost/geometry/index/rtree.hpp>
- #include <boost/geometry/policies/compare.hpp>
- #include <boost/geometry/strategies/covered_by.hpp>
- #include <boost/geometry/strategies/disjoint.hpp>
- #include <boost/geometry/util/condition.hpp>
- #include <boost/geometry/util/type_traits.hpp>
- namespace boost { namespace geometry {
- #ifndef DOXYGEN_NO_DETAIL
- namespace detail { namespace within {
- struct multi_point_point
- {
- template <typename MultiPoint, typename Point, typename Strategy>
- static inline bool apply(MultiPoint const& multi_point,
- Point const& point,
- Strategy const& strategy)
- {
- auto const s = strategy.relate(multi_point, point);
- for (auto it = boost::begin(multi_point); it != boost::end(multi_point); ++it)
- {
- if (! s.apply(*it, point))
- {
- return false;
- }
- }
-
- return true;
- }
- };
- struct multi_point_multi_point
- {
- template <typename MultiPoint1, typename MultiPoint2, typename Strategy>
- static inline bool apply(MultiPoint1 const& multi_point1,
- MultiPoint2 const& multi_point2,
- Strategy const& )
- {
- typedef typename boost::range_value<MultiPoint2>::type point2_type;
- typedef geometry::less<void, -1, Strategy> less_type;
- less_type const less = less_type();
- std::vector<point2_type> points2(boost::begin(multi_point2), boost::end(multi_point2));
- std::sort(points2.begin(), points2.end(), less);
- bool result = false;
- for (auto it = boost::begin(multi_point1); it != boost::end(multi_point1); ++it)
- {
- if (! std::binary_search(points2.begin(), points2.end(), *it, less))
- {
- return false;
- }
- else
- {
- result = true;
- }
- }
- return result;
- }
- };
- template <bool Within>
- struct multi_point_single_geometry
- {
- template <typename MultiPoint, typename LinearOrAreal, typename Strategy>
- static inline bool apply(MultiPoint const& multi_point,
- LinearOrAreal const& linear_or_areal,
- Strategy const& strategy)
- {
-
- typedef typename point_type<LinearOrAreal>::type point2_type;
- typedef model::box<point2_type> box2_type;
-
- box2_type box;
- geometry::envelope(linear_or_areal, box, strategy);
- geometry::detail::expand_by_epsilon(box);
-
-
- bool result = false;
- for (auto it = boost::begin(multi_point); it != boost::end(multi_point); ++it )
- {
- typedef decltype(strategy.covered_by(*it, box)) point_in_box_type;
- int in_val = 0;
-
- if (! point_in_box_type::apply(*it, box)
- || (in_val = point_in_geometry(*it, linear_or_areal, strategy)) < 0)
- {
- result = false;
- break;
- }
-
- if (Within ? in_val > 0 : in_val >= 0)
- {
- result = true;
- }
- }
- return result;
- }
- };
- template <bool Within>
- struct multi_point_multi_geometry
- {
- template <typename MultiPoint, typename LinearOrAreal, typename Strategy>
- static inline bool apply(MultiPoint const& multi_point,
- LinearOrAreal const& linear_or_areal,
- Strategy const& strategy)
- {
- typedef typename point_type<LinearOrAreal>::type point2_type;
- typedef model::box<point2_type> box2_type;
- static const bool is_linear = util::is_linear<LinearOrAreal>::value;
-
-
- std::size_t count2 = boost::size(linear_or_areal);
- typedef std::pair<box2_type, std::size_t> box_pair_type;
- typedef std::vector<box_pair_type> box_pair_vector;
- box_pair_vector boxes(count2);
- for (std::size_t i = 0 ; i < count2 ; ++i)
- {
- geometry::envelope(linear_or_areal, boxes[i].first, strategy);
- geometry::detail::expand_by_epsilon(boxes[i].first);
- boxes[i].second = i;
- }
-
- typedef index::parameters<index::rstar<4>, Strategy> index_parameters_type;
- index::rtree<box_pair_type, index_parameters_type>
- rtree(boxes.begin(), boxes.end(),
- index_parameters_type(index::rstar<4>(), strategy));
-
-
- bool result = false;
- for (auto it = boost::begin(multi_point); it != boost::end(multi_point); ++it)
- {
-
-
- box_pair_vector inters_boxes;
- rtree.query(index::intersects(*it), std::back_inserter(inters_boxes));
- bool found_interior = false;
- bool found_boundary = false;
- int boundaries = 0;
- typedef typename box_pair_vector::const_iterator box_iterator;
- for (box_iterator box_it = inters_boxes.begin() ;
- box_it != inters_boxes.end() ; ++box_it )
- {
- int const in_val = point_in_geometry(*it,
- range::at(linear_or_areal, box_it->second), strategy);
- if (in_val > 0)
- {
- found_interior = true;
- }
- else if (in_val == 0)
- {
- ++boundaries;
- }
-
-
-
-
- if (result && in_val >= 0)
- {
- break;
- }
- }
- if (boundaries > 0)
- {
- if (BOOST_GEOMETRY_CONDITION(is_linear) && boundaries % 2 == 0)
- {
- found_interior = true;
- }
- else
- {
- found_boundary = true;
- }
- }
-
- if (! found_interior && ! found_boundary)
- {
- result = false;
- break;
- }
-
- if (Within ? found_interior : (found_interior || found_boundary))
- {
- result = true;
- }
- }
- return result;
- }
- };
- }}
- #endif
- }}
- #endif
|