1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2015 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2015 Mateusz Loskot, London, UK.
6 // Copyright (c) 2013-2015 Adam Wulkiewicz, Lodz, Poland.
8 // This file was modified by Oracle on 2013, 2014, 2015, 2017.
9 // Modifications copyright (c) 2013-2017, Oracle and/or its affiliates.
11 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
13 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
14 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
16 // Use, modification and distribution is subject to the Boost Software License,
17 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt)
20 #ifndef BOOST_GEOMETRY_ALGORITHMS_TOUCHES_HPP
21 #define BOOST_GEOMETRY_ALGORITHMS_TOUCHES_HPP
26 #include <boost/variant/apply_visitor.hpp>
27 #include <boost/variant/static_visitor.hpp>
28 #include <boost/variant/variant_fwd.hpp>
30 #include <boost/geometry/geometries/concepts/check.hpp>
31 #include <boost/geometry/algorithms/detail/for_each_range.hpp>
32 #include <boost/geometry/algorithms/detail/overlay/overlay.hpp>
33 #include <boost/geometry/algorithms/detail/overlay/self_turn_points.hpp>
34 #include <boost/geometry/algorithms/disjoint.hpp>
35 #include <boost/geometry/algorithms/intersects.hpp>
36 #include <boost/geometry/algorithms/num_geometries.hpp>
37 #include <boost/geometry/algorithms/detail/sub_range.hpp>
38 #include <boost/geometry/policies/robustness/no_rescale_policy.hpp>
40 #include <boost/geometry/algorithms/relate.hpp>
41 #include <boost/geometry/algorithms/detail/relate/relate_impl.hpp>
44 namespace boost { namespace geometry
47 #ifndef DOXYGEN_NO_DETAIL
48 namespace detail { namespace touches
55 std::size_t Dimension,
56 std::size_t DimensionCount
60 template <typename Box1, typename Box2>
61 static inline bool apply(Box1 const& b1, Box2 const& b2, bool & touch)
63 typedef typename coordinate_type<Box1>::type coordinate_type1;
64 typedef typename coordinate_type<Box2>::type coordinate_type2;
66 coordinate_type1 const& min1 = get<min_corner, Dimension>(b1);
67 coordinate_type1 const& max1 = get<max_corner, Dimension>(b1);
68 coordinate_type2 const& min2 = get<min_corner, Dimension>(b2);
69 coordinate_type2 const& max2 = get<max_corner, Dimension>(b2);
71 // TODO assert or exception?
72 //BOOST_GEOMETRY_ASSERT(min1 <= max1 && min2 <= max2);
74 if (max1 < min2 || max2 < min1)
79 if (max1 == min2 || max2 == min1)
88 >::apply(b1, b2, touch);
94 std::size_t DimensionCount
96 struct box_box_loop<DimensionCount, DimensionCount>
98 template <typename Box1, typename Box2>
99 static inline bool apply(Box1 const& , Box2 const&, bool &)
107 template <typename Box1, typename Box2, typename Strategy>
108 static inline bool apply(Box1 const& b1, Box2 const& b2, Strategy const& /*strategy*/)
110 BOOST_STATIC_ASSERT((boost::is_same
112 typename geometry::coordinate_system<Box1>::type,
113 typename geometry::coordinate_system<Box2>::type
116 assert_dimension_equal<Box1, Box2>();
118 bool touches = false;
119 bool ok = box_box_loop
122 dimension<Box1>::type::value
123 >::apply(b1, b2, touches);
125 return ok && touches;
131 struct areal_interrupt_policy
133 static bool const enabled = true;
135 bool found_not_touch;
137 // dummy variable required by self_get_turn_points::get_turns
138 static bool const has_intersections = false;
142 return found_touch && !found_not_touch;
145 inline areal_interrupt_policy()
146 : found_touch(false), found_not_touch(false)
149 template <typename Range>
150 inline bool apply(Range const& range)
152 // if already rejected (temp workaround?)
153 if ( found_not_touch )
156 typedef typename boost::range_iterator<Range const>::type iterator;
157 for ( iterator it = boost::begin(range) ; it != boost::end(range) ; ++it )
159 if ( it->has(overlay::operation_intersection) )
161 found_not_touch = true;
167 case overlay::method_crosses:
168 found_not_touch = true;
170 case overlay::method_equal:
171 // Segment spatially equal means: at the right side
172 // the polygon internally overlaps. So return false.
173 found_not_touch = true;
175 case overlay::method_touch:
176 case overlay::method_touch_interior:
177 case overlay::method_collinear:
178 if ( ok_for_touch(*it) )
184 found_not_touch = true;
188 case overlay::method_none :
189 case overlay::method_disjoint :
190 case overlay::method_error :
198 template <typename Turn>
199 inline bool ok_for_touch(Turn const& turn)
201 return turn.both(overlay::operation_union)
202 || turn.both(overlay::operation_blocked)
203 || turn.combination(overlay::operation_union, overlay::operation_blocked)
208 template<typename Geometry, typename PointInRingStrategy>
209 struct check_each_ring_for_within
212 Geometry const& m_geometry;
213 PointInRingStrategy const& m_strategy;
215 inline check_each_ring_for_within(Geometry const& g, PointInRingStrategy const& strategy)
218 , m_strategy(strategy)
221 template <typename Range>
222 inline void apply(Range const& range)
224 typename geometry::point_type<Range>::type p;
225 geometry::point_on_border(p, range);
226 if ( !has_within && geometry::within(p, m_geometry, m_strategy) )
233 template <typename FirstGeometry, typename SecondGeometry, typename IntersectionStrategy>
234 inline bool rings_containing(FirstGeometry const& geometry1,
235 SecondGeometry const& geometry2,
236 IntersectionStrategy const& strategy)
238 // NOTE: This strategy could be defined inside IntersectionStrategy
239 typedef typename IntersectionStrategy::template point_in_geometry_strategy
241 FirstGeometry, SecondGeometry
242 >::type point_in_ring_strategy_type;
244 point_in_ring_strategy_type point_in_ring_strategy
245 = strategy.template get_point_in_geometry_strategy<FirstGeometry, SecondGeometry>();
247 check_each_ring_for_within
249 FirstGeometry, point_in_ring_strategy_type
250 > checker(geometry1, point_in_ring_strategy);
251 geometry::detail::for_each_range(geometry2, checker);
252 return checker.has_within;
255 template <typename Geometry1, typename Geometry2>
258 template <typename IntersectionStrategy>
259 static inline bool apply(Geometry1 const& geometry1,
260 Geometry2 const& geometry2,
261 IntersectionStrategy const& strategy)
263 typedef detail::no_rescale_policy rescale_policy_type;
264 typedef typename geometry::point_type<Geometry1>::type point_type;
265 typedef detail::overlay::turn_info
268 typename segment_ratio_type<point_type, rescale_policy_type>::type
271 std::deque<turn_info> turns;
272 detail::touches::areal_interrupt_policy policy;
273 rescale_policy_type robust_policy;
274 boost::geometry::get_turns
276 detail::overlay::do_reverse<geometry::point_order<Geometry1>::value>::value,
277 detail::overlay::do_reverse<geometry::point_order<Geometry2>::value>::value,
278 detail::overlay::assign_null_policy
279 >(geometry1, geometry2, strategy, robust_policy, turns, policy);
281 return policy.result()
282 && ! geometry::detail::touches::rings_containing(geometry1, geometry2, strategy)
283 && ! geometry::detail::touches::rings_containing(geometry2, geometry1, strategy);
289 struct use_point_in_geometry
291 template <typename Point, typename Geometry, typename Strategy>
292 static inline bool apply(Point const& point, Geometry const& geometry, Strategy const& strategy)
294 return detail::within::point_in_geometry(point, geometry, strategy) == 0;
299 #endif // DOXYGEN_NO_DETAIL
301 #ifndef DOXYGEN_NO_DISPATCH
304 // TODO: Since CastedTags are used is Reverse needed?
310 typename Tag1 = typename tag<Geometry1>::type,
311 typename Tag2 = typename tag<Geometry2>::type,
312 typename CastedTag1 = typename tag_cast<Tag1, pointlike_tag, linear_tag, areal_tag>::type,
313 typename CastedTag2 = typename tag_cast<Tag2, pointlike_tag, linear_tag, areal_tag>::type,
314 bool Reverse = reverse_dispatch<Geometry1, Geometry2>::type::value
317 : not_implemented<Tag1, Tag2>
320 // If reversal is needed, perform it
323 typename Geometry1, typename Geometry2,
324 typename Tag1, typename Tag2,
325 typename CastedTag1, typename CastedTag2
327 struct touches<Geometry1, Geometry2, Tag1, Tag2, CastedTag1, CastedTag2, true>
328 : touches<Geometry2, Geometry1, Tag2, Tag1, CastedTag2, CastedTag1, false>
330 template <typename Strategy>
331 static inline bool apply(Geometry1 const& g1, Geometry2 const& g2, Strategy const& strategy)
333 return touches<Geometry2, Geometry1>::apply(g2, g1, strategy);
339 template <typename Geometry1, typename Geometry2, typename Tag2>
340 struct touches<Geometry1, Geometry2, point_tag, Tag2, pointlike_tag, pointlike_tag, false>
342 template <typename Strategy>
343 static inline bool apply(Geometry1 const& , Geometry2 const& , Strategy const&)
349 template <typename Geometry1, typename Geometry2, typename Tag2>
350 struct touches<Geometry1, Geometry2, multi_point_tag, Tag2, pointlike_tag, pointlike_tag, false>
352 template <typename Strategy>
353 static inline bool apply(Geometry1 const&, Geometry2 const&, Strategy const&)
361 template <typename Point, typename Geometry, typename Tag2, typename CastedTag2>
362 struct touches<Point, Geometry, point_tag, Tag2, pointlike_tag, CastedTag2, false>
363 : detail::touches::use_point_in_geometry
366 // TODO: support touches(MPt, Linear/Areal)
370 template <typename Box1, typename Box2, typename CastedTag1, typename CastedTag2>
371 struct touches<Box1, Box2, box_tag, box_tag, CastedTag1, CastedTag2, false>
372 : detail::touches::box_box
375 template <typename Box1, typename Box2>
376 struct touches<Box1, Box2, box_tag, box_tag, areal_tag, areal_tag, false>
377 : detail::touches::box_box
382 template <typename Linear1, typename Linear2, typename Tag1, typename Tag2>
383 struct touches<Linear1, Linear2, Tag1, Tag2, linear_tag, linear_tag, false>
384 : detail::relate::relate_impl
386 detail::de9im::static_mask_touches_type,
394 template <typename Linear, typename Areal, typename Tag1, typename Tag2>
395 struct touches<Linear, Areal, Tag1, Tag2, linear_tag, areal_tag, false>
396 : detail::relate::relate_impl
398 detail::de9im::static_mask_touches_type,
405 template <typename Linear, typename Areal, typename Tag1, typename Tag2>
406 struct touches<Areal, Linear, Tag1, Tag2, areal_tag, linear_tag, false>
407 : detail::relate::relate_impl
409 detail::de9im::static_mask_touches_type,
417 template <typename Areal1, typename Areal2, typename Tag1, typename Tag2>
418 struct touches<Areal1, Areal2, Tag1, Tag2, areal_tag, areal_tag, false>
419 : detail::relate::relate_impl
421 detail::de9im::static_mask_touches_type,
427 template <typename Areal1, typename Areal2>
428 struct touches<Areal1, Areal2, ring_tag, ring_tag, areal_tag, areal_tag, false>
429 : detail::touches::areal_areal<Areal1, Areal2>
432 } // namespace dispatch
433 #endif // DOXYGEN_NO_DISPATCH
436 namespace resolve_strategy
441 template <typename Geometry1, typename Geometry2, typename Strategy>
442 static inline bool apply(Geometry1 const& geometry1,
443 Geometry2 const& geometry2,
444 Strategy const& strategy)
446 return dispatch::touches
449 >::apply(geometry1, geometry2, strategy);
452 template <typename Geometry1, typename Geometry2>
453 static inline bool apply(Geometry1 const& geometry1,
454 Geometry2 const& geometry2,
457 typedef typename strategy::relate::services::default_strategy
461 >::type strategy_type;
463 return dispatch::touches
466 >::apply(geometry1, geometry2, strategy_type());
470 } // namespace resolve_strategy
473 namespace resolve_variant {
475 template <typename Geometry1, typename Geometry2>
478 template <typename Strategy>
479 static bool apply(Geometry1 const& geometry1, Geometry2 const& geometry2, Strategy const& strategy)
481 concepts::check<Geometry1 const>();
482 concepts::check<Geometry2 const>();
484 return resolve_strategy::touches::apply(geometry1, geometry2, strategy);
488 template <BOOST_VARIANT_ENUM_PARAMS(typename T), typename Geometry2>
489 struct touches<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Geometry2>
491 template <typename Strategy>
492 struct visitor: boost::static_visitor<bool>
494 Geometry2 const& m_geometry2;
495 Strategy const& m_strategy;
497 visitor(Geometry2 const& geometry2, Strategy const& strategy)
498 : m_geometry2(geometry2)
499 , m_strategy(strategy)
502 template <typename Geometry1>
503 bool operator()(Geometry1 const& geometry1) const
505 return touches<Geometry1, Geometry2>::apply(geometry1, m_geometry2, m_strategy);
509 template <typename Strategy>
510 static inline bool apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry1,
511 Geometry2 const& geometry2,
512 Strategy const& strategy)
514 return boost::apply_visitor(visitor<Strategy>(geometry2, strategy), geometry1);
518 template <typename Geometry1, BOOST_VARIANT_ENUM_PARAMS(typename T)>
519 struct touches<Geometry1, boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
521 template <typename Strategy>
522 struct visitor: boost::static_visitor<bool>
524 Geometry1 const& m_geometry1;
525 Strategy const& m_strategy;
527 visitor(Geometry1 const& geometry1, Strategy const& strategy)
528 : m_geometry1(geometry1)
529 , m_strategy(strategy)
532 template <typename Geometry2>
533 bool operator()(Geometry2 const& geometry2) const
535 return touches<Geometry1, Geometry2>::apply(m_geometry1, geometry2, m_strategy);
539 template <typename Strategy>
540 static inline bool apply(Geometry1 const& geometry1,
541 boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry2,
542 Strategy const& strategy)
544 return boost::apply_visitor(visitor<Strategy>(geometry1, strategy), geometry2);
548 template <BOOST_VARIANT_ENUM_PARAMS(typename T1),
549 BOOST_VARIANT_ENUM_PARAMS(typename T2)>
550 struct touches<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T1)>,
551 boost::variant<BOOST_VARIANT_ENUM_PARAMS(T2)> >
553 template <typename Strategy>
554 struct visitor: boost::static_visitor<bool>
556 Strategy const& m_strategy;
558 visitor(Strategy const& strategy)
559 : m_strategy(strategy)
562 template <typename Geometry1, typename Geometry2>
563 bool operator()(Geometry1 const& geometry1,
564 Geometry2 const& geometry2) const
566 return touches<Geometry1, Geometry2>::apply(geometry1, geometry2, m_strategy);
570 template <typename Strategy>
571 static inline bool apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T1)> const& geometry1,
572 boost::variant<BOOST_VARIANT_ENUM_PARAMS(T2)> const& geometry2,
573 Strategy const& strategy)
575 return boost::apply_visitor(visitor<Strategy>(strategy), geometry1, geometry2);
579 template <typename Geometry>
582 static bool apply(Geometry const& geometry)
584 concepts::check<Geometry const>();
586 typedef typename strategy::relate::services::default_strategy
589 >::type strategy_type;
590 typedef detail::no_rescale_policy rescale_policy_type;
591 typedef typename geometry::point_type<Geometry>::type point_type;
592 typedef detail::overlay::turn_info
595 typename segment_ratio_type<point_type, rescale_policy_type>::type
598 typedef detail::overlay::get_turn_info
600 detail::overlay::assign_null_policy
603 std::deque<turn_info> turns;
604 detail::touches::areal_interrupt_policy policy;
605 strategy_type strategy;
606 rescale_policy_type robust_policy;
607 detail::self_get_turn_points::get_turns
610 >::apply(geometry, strategy, robust_policy, turns, policy);
612 return policy.result();
616 template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
617 struct self_touches<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
619 struct visitor: boost::static_visitor<bool>
621 template <typename Geometry>
622 bool operator()(Geometry const& geometry) const
624 return self_touches<Geometry>::apply(geometry);
629 apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry)
631 return boost::apply_visitor(visitor(), geometry);
635 } // namespace resolve_variant
639 \brief \brief_check{has at least one touching point (self-tangency)}
640 \note This function can be called for one geometry (self-tangency) and
641 also for two geometries (touch)
643 \tparam Geometry \tparam_geometry
644 \param geometry \param_geometry
645 \return \return_check{is self-touching}
647 \qbk{distinguish,one geometry}
648 \qbk{[def __one_parameter__]}
649 \qbk{[include reference/algorithms/touches.qbk]}
651 template <typename Geometry>
652 inline bool touches(Geometry const& geometry)
654 return resolve_variant::self_touches<Geometry>::apply(geometry);
659 \brief \brief_check2{have at least one touching point (tangent - non overlapping)}
661 \tparam Geometry1 \tparam_geometry
662 \tparam Geometry2 \tparam_geometry
663 \param geometry1 \param_geometry
664 \param geometry2 \param_geometry
665 \return \return_check2{touch each other}
667 \qbk{distinguish,two geometries}
668 \qbk{[include reference/algorithms/touches.qbk]}
670 template <typename Geometry1, typename Geometry2>
671 inline bool touches(Geometry1 const& geometry1, Geometry2 const& geometry2)
673 return resolve_variant::touches
676 >::apply(geometry1, geometry2, default_strategy());
680 \brief \brief_check2{have at least one touching point (tangent - non overlapping)}
682 \tparam Geometry1 \tparam_geometry
683 \tparam Geometry2 \tparam_geometry
684 \tparam Strategy \tparam_strategy{Touches}
685 \param geometry1 \param_geometry
686 \param geometry2 \param_geometry
687 \param strategy \param_strategy{touches}
688 \return \return_check2{touch each other}
690 \qbk{distinguish,with strategy}
691 \qbk{[include reference/algorithms/touches.qbk]}
693 template <typename Geometry1, typename Geometry2, typename Strategy>
694 inline bool touches(Geometry1 const& geometry1,
695 Geometry2 const& geometry2,
696 Strategy const& strategy)
698 return resolve_variant::touches
701 >::apply(geometry1, geometry2, strategy);
705 }} // namespace boost::geometry
707 #endif // BOOST_GEOMETRY_ALGORITHMS_TOUCHES_HPP