// Boost.Geometry (aka GGL, Generic Geometry Library) // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands. // Copyright (c) 2008-2015 Bruno Lalande, Paris, France. // Copyright (c) 2009-2015 Mateusz Loskot, London, UK. // Copyright (c) 2013-2015 Adam Wulkiewicz, Lodz, Poland. // This file was modified by Oracle on 2013, 2014, 2015, 2017. // Modifications copyright (c) 2013-2017, Oracle and/or its affiliates. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands. // Use, modification and distribution is subject to the Boost Software License, // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_GEOMETRY_ALGORITHMS_TOUCHES_HPP #define BOOST_GEOMETRY_ALGORITHMS_TOUCHES_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace geometry { #ifndef DOXYGEN_NO_DETAIL namespace detail { namespace touches { // Box/Box template < std::size_t Dimension, std::size_t DimensionCount > struct box_box_loop { template static inline bool apply(Box1 const& b1, Box2 const& b2, bool & touch) { typedef typename coordinate_type::type coordinate_type1; typedef typename coordinate_type::type coordinate_type2; coordinate_type1 const& min1 = get(b1); coordinate_type1 const& max1 = get(b1); coordinate_type2 const& min2 = get(b2); coordinate_type2 const& max2 = get(b2); // TODO assert or exception? //BOOST_GEOMETRY_ASSERT(min1 <= max1 && min2 <= max2); 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 { template static inline bool apply(Box1 const& , Box2 const&, bool &) { return true; } }; struct box_box { template static inline bool apply(Box1 const& b1, Box2 const& b2, Strategy const& /*strategy*/) { BOOST_STATIC_ASSERT((boost::is_same < typename geometry::coordinate_system::type, typename geometry::coordinate_system::type >::value )); assert_dimension_equal(); bool touches = false; bool ok = box_box_loop < 0, dimension::type::value >::apply(b1, b2, touches); return ok && touches; } }; // Areal/Areal struct areal_interrupt_policy { static bool const enabled = true; bool found_touch; bool found_not_touch; // dummy variable required by self_get_turn_points::get_turns 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 inline bool apply(Range const& range) { // if already rejected (temp workaround?) if ( found_not_touch ) return true; typedef typename boost::range_iterator::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: // Segment spatially equal means: at the right side // the polygon internally overlaps. So return false. 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 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 struct check_each_ring_for_within { bool has_within; Geometry const& m_geometry; PointInRingStrategy const& m_strategy; inline check_each_ring_for_within(Geometry const& g, PointInRingStrategy const& strategy) : has_within(false) , m_geometry(g) , m_strategy(strategy) {} template inline void apply(Range const& range) { typename geometry::point_type::type p; geometry::point_on_border(p, range); if ( !has_within && geometry::within(p, m_geometry, m_strategy) ) { has_within = true; } } }; template inline bool rings_containing(FirstGeometry const& geometry1, SecondGeometry const& geometry2, IntersectionStrategy const& strategy) { // NOTE: This strategy could be defined inside IntersectionStrategy typedef typename IntersectionStrategy::template point_in_geometry_strategy < FirstGeometry, SecondGeometry >::type point_in_ring_strategy_type; point_in_ring_strategy_type point_in_ring_strategy = strategy.template get_point_in_geometry_strategy(); check_each_ring_for_within < FirstGeometry, point_in_ring_strategy_type > checker(geometry1, point_in_ring_strategy); geometry::detail::for_each_range(geometry2, checker); return checker.has_within; } template struct areal_areal { template static inline bool apply(Geometry1 const& geometry1, Geometry2 const& geometry2, IntersectionStrategy const& strategy) { typedef detail::no_rescale_policy rescale_policy_type; typedef typename geometry::point_type::type point_type; typedef detail::overlay::turn_info < point_type, typename segment_ratio_type::type > turn_info; std::deque turns; detail::touches::areal_interrupt_policy policy; rescale_policy_type robust_policy; boost::geometry::get_turns < detail::overlay::do_reverse::value>::value, detail::overlay::do_reverse::value>::value, detail::overlay::assign_null_policy >(geometry1, geometry2, strategy, robust_policy, turns, policy); return policy.result() && ! geometry::detail::touches::rings_containing(geometry1, geometry2, strategy) && ! geometry::detail::touches::rings_containing(geometry2, geometry1, strategy); } }; // P/* struct use_point_in_geometry { template static inline bool apply(Point const& point, Geometry const& geometry, Strategy const& strategy) { return detail::within::point_in_geometry(point, geometry, strategy) == 0; } }; }} #endif // DOXYGEN_NO_DETAIL #ifndef DOXYGEN_NO_DISPATCH namespace dispatch { // TODO: Since CastedTags are used is Reverse needed? template < typename Geometry1, typename Geometry2, typename Tag1 = typename tag::type, typename Tag2 = typename tag::type, typename CastedTag1 = typename tag_cast::type, typename CastedTag2 = typename tag_cast::type, bool Reverse = reverse_dispatch::type::value > struct touches : not_implemented {}; // If reversal is needed, perform it template < typename Geometry1, typename Geometry2, typename Tag1, typename Tag2, typename CastedTag1, typename CastedTag2 > struct touches : touches { template static inline bool apply(Geometry1 const& g1, Geometry2 const& g2, Strategy const& strategy) { return touches::apply(g2, g1, strategy); } }; // P/P template struct touches { template static inline bool apply(Geometry1 const& , Geometry2 const& , Strategy const&) { return false; } }; template struct touches { template static inline bool apply(Geometry1 const&, Geometry2 const&, Strategy const&) { return false; } }; // P/* template struct touches : detail::touches::use_point_in_geometry {}; // TODO: support touches(MPt, Linear/Areal) // Box/Box template struct touches : detail::touches::box_box {}; template struct touches : detail::touches::box_box {}; // L/L template struct touches : detail::relate::relate_impl < detail::de9im::static_mask_touches_type, Linear1, Linear2 > {}; // L/A template struct touches : detail::relate::relate_impl < detail::de9im::static_mask_touches_type, Linear, Areal > {}; // A/L template struct touches : detail::relate::relate_impl < detail::de9im::static_mask_touches_type, Areal, Linear > {}; // A/A template struct touches : detail::relate::relate_impl < detail::de9im::static_mask_touches_type, Areal1, Areal2 > {}; template struct touches : detail::touches::areal_areal {}; } // namespace dispatch #endif // DOXYGEN_NO_DISPATCH namespace resolve_strategy { struct touches { template static inline bool apply(Geometry1 const& geometry1, Geometry2 const& geometry2, Strategy const& strategy) { return dispatch::touches < Geometry1, Geometry2 >::apply(geometry1, geometry2, strategy); } template static inline bool apply(Geometry1 const& geometry1, Geometry2 const& geometry2, default_strategy) { typedef typename strategy::relate::services::default_strategy < Geometry1, Geometry2 >::type strategy_type; return dispatch::touches < Geometry1, Geometry2 >::apply(geometry1, geometry2, strategy_type()); } }; } // namespace resolve_strategy namespace resolve_variant { template struct touches { template static bool apply(Geometry1 const& geometry1, Geometry2 const& geometry2, Strategy const& strategy) { concepts::check(); concepts::check(); return resolve_strategy::touches::apply(geometry1, geometry2, strategy); } }; template struct touches, Geometry2> { template struct visitor: boost::static_visitor { Geometry2 const& m_geometry2; Strategy const& m_strategy; visitor(Geometry2 const& geometry2, Strategy const& strategy) : m_geometry2(geometry2) , m_strategy(strategy) {} template bool operator()(Geometry1 const& geometry1) const { return touches::apply(geometry1, m_geometry2, m_strategy); } }; template static inline bool apply(boost::variant const& geometry1, Geometry2 const& geometry2, Strategy const& strategy) { return boost::apply_visitor(visitor(geometry2, strategy), geometry1); } }; template struct touches > { template struct visitor: boost::static_visitor { Geometry1 const& m_geometry1; Strategy const& m_strategy; visitor(Geometry1 const& geometry1, Strategy const& strategy) : m_geometry1(geometry1) , m_strategy(strategy) {} template bool operator()(Geometry2 const& geometry2) const { return touches::apply(m_geometry1, geometry2, m_strategy); } }; template static inline bool apply(Geometry1 const& geometry1, boost::variant const& geometry2, Strategy const& strategy) { return boost::apply_visitor(visitor(geometry1, strategy), geometry2); } }; template struct touches, boost::variant > { template struct visitor: boost::static_visitor { Strategy const& m_strategy; visitor(Strategy const& strategy) : m_strategy(strategy) {} template bool operator()(Geometry1 const& geometry1, Geometry2 const& geometry2) const { return touches::apply(geometry1, geometry2, m_strategy); } }; template static inline bool apply(boost::variant const& geometry1, boost::variant const& geometry2, Strategy const& strategy) { return boost::apply_visitor(visitor(strategy), geometry1, geometry2); } }; template struct self_touches { static bool apply(Geometry const& geometry) { concepts::check(); typedef typename strategy::relate::services::default_strategy < Geometry, Geometry >::type strategy_type; typedef detail::no_rescale_policy rescale_policy_type; typedef typename geometry::point_type::type point_type; typedef detail::overlay::turn_info < point_type, typename segment_ratio_type::type > turn_info; typedef detail::overlay::get_turn_info < detail::overlay::assign_null_policy > policy_type; std::deque turns; detail::touches::areal_interrupt_policy policy; strategy_type strategy; rescale_policy_type robust_policy; detail::self_get_turn_points::get_turns < policy_type >::apply(geometry, strategy, robust_policy, turns, policy); return policy.result(); } }; template struct self_touches > { struct visitor: boost::static_visitor { template bool operator()(Geometry const& geometry) const { return self_touches::apply(geometry); } }; static inline bool apply(boost::variant const& geometry) { return boost::apply_visitor(visitor(), geometry); } }; } // namespace resolve_variant /*! \brief \brief_check{has at least one touching point (self-tangency)} \note This function can be called for one geometry (self-tangency) and also for two geometries (touch) \ingroup touches \tparam Geometry \tparam_geometry \param geometry \param_geometry \return \return_check{is self-touching} \qbk{distinguish,one geometry} \qbk{[def __one_parameter__]} \qbk{[include reference/algorithms/touches.qbk]} */ template inline bool touches(Geometry const& geometry) { return resolve_variant::self_touches::apply(geometry); } /*! \brief \brief_check2{have at least one touching point (tangent - non overlapping)} \ingroup touches \tparam Geometry1 \tparam_geometry \tparam Geometry2 \tparam_geometry \param geometry1 \param_geometry \param geometry2 \param_geometry \return \return_check2{touch each other} \qbk{distinguish,two geometries} \qbk{[include reference/algorithms/touches.qbk]} */ template inline bool touches(Geometry1 const& geometry1, Geometry2 const& geometry2) { return resolve_variant::touches < Geometry1, Geometry2 >::apply(geometry1, geometry2, default_strategy()); } /*! \brief \brief_check2{have at least one touching point (tangent - non overlapping)} \ingroup touches \tparam Geometry1 \tparam_geometry \tparam Geometry2 \tparam_geometry \tparam Strategy \tparam_strategy{Touches} \param geometry1 \param_geometry \param geometry2 \param_geometry \param strategy \param_strategy{touches} \return \return_check2{touch each other} \qbk{distinguish,with strategy} \qbk{[include reference/algorithms/touches.qbk]} */ template inline bool touches(Geometry1 const& geometry1, Geometry2 const& geometry2, Strategy const& strategy) { return resolve_variant::touches < Geometry1, Geometry2 >::apply(geometry1, geometry2, strategy); } }} // namespace boost::geometry #endif // BOOST_GEOMETRY_ALGORITHMS_TOUCHES_HPP