123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587 |
- #ifndef BOOST_GEOMETRY_ALGORITHMS_TOUCHES_HPP
- #define BOOST_GEOMETRY_ALGORITHMS_TOUCHES_HPP
- #include <deque>
- #include <boost/variant/apply_visitor.hpp>
- #include <boost/variant/static_visitor.hpp>
- #include <boost/variant/variant_fwd.hpp>
- #include <boost/geometry/geometries/concepts/check.hpp>
- #include <boost/geometry/algorithms/detail/for_each_range.hpp>
- #include <boost/geometry/algorithms/detail/overlay/overlay.hpp>
- #include <boost/geometry/algorithms/detail/overlay/self_turn_points.hpp>
- #include <boost/geometry/algorithms/disjoint.hpp>
- #include <boost/geometry/algorithms/intersects.hpp>
- #include <boost/geometry/algorithms/num_geometries.hpp>
- #include <boost/geometry/algorithms/detail/sub_range.hpp>
- #include <boost/geometry/policies/robustness/no_rescale_policy.hpp>
- #include <boost/geometry/algorithms/relate.hpp>
- #include <boost/geometry/algorithms/detail/relate/relate_impl.hpp>
- namespace boost { namespace geometry
- {
- #ifndef DOXYGEN_NO_DETAIL
- namespace detail { namespace touches
- {
- template
- <
- std::size_t Dimension,
- std::size_t DimensionCount
- >
- struct box_box_loop
- {
- template <typename Box1, typename Box2>
- static inline bool apply(Box1 const& b1, Box2 const& b2, bool & touch)
- {
- typedef typename coordinate_type<Box1>::type coordinate_type1;
- typedef typename coordinate_type<Box2>::type coordinate_type2;
- coordinate_type1 const& min1 = get<min_corner, Dimension>(b1);
- coordinate_type1 const& max1 = get<max_corner, Dimension>(b1);
- coordinate_type2 const& min2 = get<min_corner, Dimension>(b2);
- coordinate_type2 const& max2 = get<max_corner, Dimension>(b2);
-
-
- if (max1 < min2 || max2 < min1)
- {
- return false;
- }
- if (max1 == min2 || max2 == min1)
- {
- touch = true;
- }
-
- return box_box_loop
- <
- Dimension + 1,
- DimensionCount
- >::apply(b1, b2, touch);
- }
- };
- template
- <
- std::size_t DimensionCount
- >
- struct box_box_loop<DimensionCount, DimensionCount>
- {
- template <typename Box1, typename Box2>
- static inline bool apply(Box1 const& , Box2 const&, bool &)
- {
- return true;
- }
- };
- struct box_box
- {
- template <typename Box1, typename Box2>
- static inline bool apply(Box1 const& b1, Box2 const& b2)
- {
- BOOST_STATIC_ASSERT((boost::is_same
- <
- typename geometry::coordinate_system<Box1>::type,
- typename geometry::coordinate_system<Box2>::type
- >::value
- ));
- assert_dimension_equal<Box1, Box2>();
- bool touches = false;
- bool ok = box_box_loop
- <
- 0,
- dimension<Box1>::type::value
- >::apply(b1, b2, touches);
- return ok && touches;
- }
- };
- struct areal_interrupt_policy
- {
- static bool const enabled = true;
- bool found_touch;
- bool found_not_touch;
-
- static bool const has_intersections = false;
- inline bool result()
- {
- return found_touch && !found_not_touch;
- }
- inline areal_interrupt_policy()
- : found_touch(false), found_not_touch(false)
- {}
- template <typename Range>
- inline bool apply(Range const& range)
- {
-
- if ( found_not_touch )
- return true;
- typedef typename boost::range_iterator<Range const>::type iterator;
- for ( iterator it = boost::begin(range) ; it != boost::end(range) ; ++it )
- {
- if ( it->has(overlay::operation_intersection) )
- {
- found_not_touch = true;
- return true;
- }
- switch(it->method)
- {
- case overlay::method_crosses:
- found_not_touch = true;
- return true;
- case overlay::method_equal:
-
-
- found_not_touch = true;
- return true;
- case overlay::method_touch:
- case overlay::method_touch_interior:
- case overlay::method_collinear:
- if ( ok_for_touch(*it) )
- {
- found_touch = true;
- }
- else
- {
- found_not_touch = true;
- return true;
- }
- break;
- case overlay::method_none :
- case overlay::method_disjoint :
- case overlay::method_error :
- break;
- }
- }
- return false;
- }
- template <typename Turn>
- inline bool ok_for_touch(Turn const& turn)
- {
- return turn.both(overlay::operation_union)
- || turn.both(overlay::operation_blocked)
- || turn.combination(overlay::operation_union, overlay::operation_blocked)
- ;
- }
- };
- template<typename Geometry>
- struct check_each_ring_for_within
- {
- bool has_within;
- Geometry const& m_geometry;
- inline check_each_ring_for_within(Geometry const& g)
- : has_within(false)
- , m_geometry(g)
- {}
- template <typename Range>
- inline void apply(Range const& range)
- {
- typename geometry::point_type<Range>::type p;
- geometry::point_on_border(p, range);
- if ( !has_within && geometry::within(p, m_geometry) )
- {
- has_within = true;
- }
- }
- };
- template <typename FirstGeometry, typename SecondGeometry>
- inline bool rings_containing(FirstGeometry const& geometry1,
- SecondGeometry const& geometry2)
- {
- check_each_ring_for_within<FirstGeometry> checker(geometry1);
- geometry::detail::for_each_range(geometry2, checker);
- return checker.has_within;
- }
- template <typename Geometry1, typename Geometry2>
- struct areal_areal
- {
- static inline
- bool apply(Geometry1 const& geometry1, Geometry2 const& geometry2)
- {
- typedef detail::no_rescale_policy rescale_policy_type;
- typedef typename geometry::point_type<Geometry1>::type point_type;
- typedef detail::overlay::turn_info
- <
- point_type,
- typename segment_ratio_type<point_type, rescale_policy_type>::type
- > turn_info;
- std::deque<turn_info> turns;
- detail::touches::areal_interrupt_policy policy;
- rescale_policy_type robust_policy;
- boost::geometry::get_turns
- <
- detail::overlay::do_reverse<geometry::point_order<Geometry1>::value>::value,
- detail::overlay::do_reverse<geometry::point_order<Geometry2>::value>::value,
- detail::overlay::assign_null_policy
- >(geometry1, geometry2, robust_policy, turns, policy);
- return policy.result()
- && ! geometry::detail::touches::rings_containing(geometry1, geometry2)
- && ! geometry::detail::touches::rings_containing(geometry2, geometry1);
- }
- };
- struct use_point_in_geometry
- {
- template <typename Point, typename Geometry>
- static inline bool apply(Point const& point, Geometry const& geometry)
- {
- return detail::within::point_in_geometry(point, geometry) == 0;
- }
- };
- }}
- #endif
- #ifndef DOXYGEN_NO_DISPATCH
- namespace dispatch {
- template
- <
- typename Geometry1, typename Geometry2,
- typename Tag1 = typename tag<Geometry1>::type,
- typename Tag2 = typename tag<Geometry2>::type,
- typename CastedTag1 = typename tag_cast<Tag1, pointlike_tag, linear_tag, areal_tag>::type,
- typename CastedTag2 = typename tag_cast<Tag2, pointlike_tag, linear_tag, areal_tag>::type,
- bool Reverse = reverse_dispatch<Geometry1, Geometry2>::type::value
- >
- struct touches
- : not_implemented<Tag1, Tag2>
- {};
- template
- <
- typename Geometry1, typename Geometry2,
- typename Tag1, typename Tag2,
- typename CastedTag1, typename CastedTag2
- >
- struct touches<Geometry1, Geometry2, Tag1, Tag2, CastedTag1, CastedTag2, true>
- : touches<Geometry2, Geometry1, Tag2, Tag1, CastedTag2, CastedTag1, false>
- {
- static inline bool apply(Geometry1 const& g1, Geometry2 const& g2)
- {
- return touches<Geometry2, Geometry1>::apply(g2, g1);
- }
- };
- template <typename Geometry1, typename Geometry2, typename Tag1, typename Tag2>
- struct touches<Geometry1, Geometry2, Tag1, Tag2, pointlike_tag, pointlike_tag, false>
- {
- static inline bool apply(Geometry1 const& , Geometry2 const& )
- {
- return false;
- }
- };
- template <typename Point, typename Geometry, typename Tag2, typename CastedTag2>
- struct touches<Point, Geometry, point_tag, Tag2, pointlike_tag, CastedTag2, false>
- : detail::touches::use_point_in_geometry
- {};
- template <typename Box1, typename Box2, typename CastedTag1, typename CastedTag2>
- struct touches<Box1, Box2, box_tag, box_tag, CastedTag1, CastedTag2, false>
- : detail::touches::box_box
- {};
- template <typename Box1, typename Box2>
- struct touches<Box1, Box2, box_tag, box_tag, areal_tag, areal_tag, false>
- : detail::touches::box_box
- {};
- template <typename Linear1, typename Linear2, typename Tag1, typename Tag2>
- struct touches<Linear1, Linear2, Tag1, Tag2, linear_tag, linear_tag, false>
- : detail::relate::relate_impl
- <
- detail::de9im::static_mask_touches_type,
- Linear1,
- Linear2
- >
- {};
- template <typename Linear, typename Areal, typename Tag1, typename Tag2>
- struct touches<Linear, Areal, Tag1, Tag2, linear_tag, areal_tag, false>
- : detail::relate::relate_impl
- <
- detail::de9im::static_mask_touches_type,
- Linear,
- Areal
- >
- {};
- template <typename Linear, typename Areal, typename Tag1, typename Tag2>
- struct touches<Linear, Areal, Tag1, Tag2, linear_tag, areal_tag, true>
- : detail::relate::relate_impl
- <
- detail::de9im::static_mask_touches_type,
- Areal,
- Linear
- >
- {};
- template <typename Areal1, typename Areal2, typename Tag1, typename Tag2>
- struct touches<Areal1, Areal2, Tag1, Tag2, areal_tag, areal_tag, false>
- : detail::relate::relate_impl
- <
- detail::de9im::static_mask_touches_type,
- Areal1,
- Areal2
- >
- {};
- template <typename Areal1, typename Areal2>
- struct touches<Areal1, Areal2, ring_tag, ring_tag, areal_tag, areal_tag, false>
- : detail::touches::areal_areal<Areal1, Areal2>
- {};
- }
- #endif
- namespace resolve_variant {
- template <typename Geometry1, typename Geometry2>
- struct touches
- {
- static bool apply(Geometry1 const& geometry1, Geometry2 const& geometry2)
- {
- concept::check<Geometry1 const>();
- concept::check<Geometry2 const>();
- return dispatch::touches<Geometry1, Geometry2>
- ::apply(geometry1, geometry2);
- }
- };
- template <BOOST_VARIANT_ENUM_PARAMS(typename T), typename Geometry2>
- struct touches<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Geometry2>
- {
- struct visitor: boost::static_visitor<bool>
- {
- Geometry2 const& m_geometry2;
- visitor(Geometry2 const& geometry2): m_geometry2(geometry2) {}
- template <typename Geometry1>
- bool operator()(Geometry1 const& geometry1) const
- {
- return touches<Geometry1, Geometry2>::apply(geometry1, m_geometry2);
- }
- };
- static inline bool
- apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry1,
- Geometry2 const& geometry2)
- {
- return boost::apply_visitor(visitor(geometry2), geometry1);
- }
- };
- template <typename Geometry1, BOOST_VARIANT_ENUM_PARAMS(typename T)>
- struct touches<Geometry1, boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
- {
- struct visitor: boost::static_visitor<bool>
- {
- Geometry1 const& m_geometry1;
- visitor(Geometry1 const& geometry1): m_geometry1(geometry1) {}
- template <typename Geometry2>
- bool operator()(Geometry2 const& geometry2) const
- {
- return touches<Geometry1, Geometry2>::apply(m_geometry1, geometry2);
- }
- };
- static inline bool
- apply(Geometry1 const& geometry1,
- boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry2)
- {
- return boost::apply_visitor(visitor(geometry1), geometry2);
- }
- };
- template <BOOST_VARIANT_ENUM_PARAMS(typename T1),
- BOOST_VARIANT_ENUM_PARAMS(typename T2)>
- struct touches<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T1)>,
- boost::variant<BOOST_VARIANT_ENUM_PARAMS(T2)> >
- {
- struct visitor: boost::static_visitor<bool>
- {
- template <typename Geometry1, typename Geometry2>
- bool operator()(Geometry1 const& geometry1,
- Geometry2 const& geometry2) const
- {
- return touches<Geometry1, Geometry2>::apply(geometry1, geometry2);
- }
- };
- static inline bool
- apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T1)> const& geometry1,
- boost::variant<BOOST_VARIANT_ENUM_PARAMS(T2)> const& geometry2)
- {
- return boost::apply_visitor(visitor(), geometry1, geometry2);
- }
- };
- template <typename Geometry>
- struct self_touches
- {
- static bool apply(Geometry const& geometry)
- {
- concept::check<Geometry const>();
- typedef detail::no_rescale_policy rescale_policy_type;
- typedef typename geometry::point_type<Geometry>::type point_type;
- typedef detail::overlay::turn_info
- <
- point_type,
- typename segment_ratio_type<point_type, rescale_policy_type>::type
- > turn_info;
- typedef detail::overlay::get_turn_info
- <
- detail::overlay::assign_null_policy
- > policy_type;
- std::deque<turn_info> turns;
- detail::touches::areal_interrupt_policy policy;
- rescale_policy_type robust_policy;
- detail::self_get_turn_points::get_turns
- <
- policy_type
- >::apply(geometry, robust_policy, turns, policy);
- return policy.result();
- }
- };
- template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
- struct self_touches<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
- {
- struct visitor: boost::static_visitor<bool>
- {
- template <typename Geometry>
- bool operator()(Geometry const& geometry) const
- {
- return self_touches<Geometry>::apply(geometry);
- }
- };
- static inline bool
- apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry)
- {
- return boost::apply_visitor(visitor(), geometry);
- }
- };
- }
- template <typename Geometry>
- inline bool touches(Geometry const& geometry)
- {
- return resolve_variant::self_touches<Geometry>::apply(geometry);
- }
- template <typename Geometry1, typename Geometry2>
- inline bool touches(Geometry1 const& geometry1, Geometry2 const& geometry2)
- {
- return resolve_variant::touches<Geometry1, Geometry2>::apply(geometry1, geometry2);
- }
- }}
- #endif
|