1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
6 // Copyright (c) 2013 Adam Wulkiewicz, Lodz, Poland.
8 // This file was modified by Oracle on 2013, 2014.
9 // Modifications copyright (c) 2013, 2014, Oracle and/or its affiliates.
11 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
12 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
14 // Use, modification and distribution is subject to the Boost Software License,
15 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
16 // http://www.boost.org/LICENSE_1_0.txt)
18 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
20 #ifndef BOOST_GEOMETRY_ALGORITHMS_TOUCHES_HPP
21 #define BOOST_GEOMETRY_ALGORITHMS_TOUCHES_HPP
26 #include <boost/geometry/geometries/concepts/check.hpp>
27 #include <boost/geometry/algorithms/detail/for_each_range.hpp>
28 #include <boost/geometry/algorithms/detail/overlay/overlay.hpp>
29 #include <boost/geometry/algorithms/detail/overlay/self_turn_points.hpp>
30 #include <boost/geometry/algorithms/disjoint.hpp>
31 #include <boost/geometry/algorithms/intersects.hpp>
32 #include <boost/geometry/algorithms/num_geometries.hpp>
33 #include <boost/geometry/algorithms/detail/sub_range.hpp>
34 #include <boost/geometry/policies/robustness/no_rescale_policy.hpp>
35 #include <boost/variant/static_visitor.hpp>
36 #include <boost/variant/apply_visitor.hpp>
37 #include <boost/variant/variant_fwd.hpp>
39 #include <boost/geometry/algorithms/detail/relate/relate.hpp>
41 namespace boost { namespace geometry
44 #ifndef DOXYGEN_NO_DETAIL
45 namespace detail { namespace touches
52 std::size_t Dimension,
53 std::size_t DimensionCount
57 template <typename Box1, typename Box2>
58 static inline bool apply(Box1 const& b1, Box2 const& b2, bool & touch)
60 typedef typename coordinate_type<Box1>::type coordinate_type1;
61 typedef typename coordinate_type<Box2>::type coordinate_type2;
63 coordinate_type1 const& min1 = get<min_corner, Dimension>(b1);
64 coordinate_type1 const& max1 = get<max_corner, Dimension>(b1);
65 coordinate_type2 const& min2 = get<min_corner, Dimension>(b2);
66 coordinate_type2 const& max2 = get<max_corner, Dimension>(b2);
68 // TODO assert or exception?
69 //BOOST_ASSERT(min1 <= max1 && min2 <= max2);
71 if ( max1 < min2 || max2 < min1 )
76 if ( max1 == min2 || max2 == min1 )
85 >::apply(b1, b2, touch);
91 std::size_t DimensionCount
93 struct box_box_loop<DimensionCount, DimensionCount>
95 template <typename Box1, typename Box2>
96 static inline bool apply(Box1 const& , Box2 const&, bool &)
104 template <typename Box1, typename Box2>
105 static inline bool apply(Box1 const& b1, Box2 const& b2)
107 BOOST_STATIC_ASSERT((boost::is_same
109 typename geometry::coordinate_system<Box1>::type,
110 typename geometry::coordinate_system<Box2>::type
113 assert_dimension_equal<Box1, Box2>();
115 bool touches = false;
116 bool ok = box_box_loop
119 dimension<Box1>::type::value
120 >::apply(b1, b2, touches);
122 return ok && touches;
128 struct areal_interrupt_policy
130 static bool const enabled = true;
132 bool found_not_touch;
134 // dummy variable required by self_get_turn_points::get_turns
135 static bool const has_intersections = false;
139 return found_touch && !found_not_touch;
142 inline areal_interrupt_policy()
143 : found_touch(false), found_not_touch(false)
146 template <typename Range>
147 inline bool apply(Range const& range)
149 // if already rejected (temp workaround?)
150 if ( found_not_touch )
153 typedef typename boost::range_iterator<Range const>::type iterator;
154 for ( iterator it = boost::begin(range) ; it != boost::end(range) ; ++it )
156 if ( it->has(overlay::operation_intersection) )
158 found_not_touch = true;
164 case overlay::method_crosses:
165 found_not_touch = true;
167 case overlay::method_equal:
168 // Segment spatially equal means: at the right side
169 // the polygon internally overlaps. So return false.
170 found_not_touch = true;
172 case overlay::method_touch:
173 case overlay::method_touch_interior:
174 case overlay::method_collinear:
175 if ( ok_for_touch(*it) )
181 found_not_touch = true;
185 case overlay::method_none :
186 case overlay::method_disjoint :
187 case overlay::method_error :
195 template <typename Turn>
196 inline bool ok_for_touch(Turn const& turn)
198 return turn.both(overlay::operation_union)
199 || turn.both(overlay::operation_blocked)
200 || turn.combination(overlay::operation_union, overlay::operation_blocked)
205 template<typename Geometry>
206 struct check_each_ring_for_within
209 Geometry const& m_geometry;
211 inline check_each_ring_for_within(Geometry const& g)
216 template <typename Range>
217 inline void apply(Range const& range)
219 typename geometry::point_type<Range>::type p;
220 geometry::point_on_border(p, range);
221 if ( !has_within && geometry::within(p, m_geometry) )
228 template <typename FirstGeometry, typename SecondGeometry>
229 inline bool rings_containing(FirstGeometry const& geometry1,
230 SecondGeometry const& geometry2)
232 check_each_ring_for_within<FirstGeometry> checker(geometry1);
233 geometry::detail::for_each_range(geometry2, checker);
234 return checker.has_within;
237 template <typename Geometry1, typename Geometry2>
241 bool apply(Geometry1 const& geometry1, Geometry2 const& geometry2)
243 typedef detail::no_rescale_policy rescale_policy_type;
244 typedef typename geometry::point_type<Geometry1>::type point_type;
245 typedef detail::overlay::turn_info
248 typename segment_ratio_type<point_type, rescale_policy_type>::type
251 std::deque<turn_info> turns;
252 detail::touches::areal_interrupt_policy policy;
253 rescale_policy_type robust_policy;
254 boost::geometry::get_turns
256 detail::overlay::do_reverse<geometry::point_order<Geometry1>::value>::value,
257 detail::overlay::do_reverse<geometry::point_order<Geometry2>::value>::value,
258 detail::overlay::assign_null_policy
259 >(geometry1, geometry2, robust_policy, turns, policy);
261 return policy.result()
262 && ! geometry::detail::touches::rings_containing(geometry1, geometry2)
263 && ! geometry::detail::touches::rings_containing(geometry2, geometry1);
269 struct use_point_in_geometry
271 template <typename Point, typename Geometry>
272 static inline bool apply(Point const& point, Geometry const& geometry)
274 return detail::within::point_in_geometry(point, geometry) == 0;
279 #endif // DOXYGEN_NO_DETAIL
281 #ifndef DOXYGEN_NO_DISPATCH
284 // TODO: Since CastedTags are used is Reverse needed?
288 typename Geometry1, typename Geometry2,
289 typename Tag1 = typename tag<Geometry1>::type,
290 typename Tag2 = typename tag<Geometry2>::type,
291 typename CastedTag1 = typename tag_cast<Tag1, pointlike_tag, linear_tag, areal_tag>::type,
292 typename CastedTag2 = typename tag_cast<Tag2, pointlike_tag, linear_tag, areal_tag>::type,
293 bool Reverse = reverse_dispatch<Geometry1, Geometry2>::type::value
296 : not_implemented<Tag1, Tag2>
299 // If reversal is needed, perform it
302 typename Geometry1, typename Geometry2,
303 typename Tag1, typename Tag2,
304 typename CastedTag1, typename CastedTag2
306 struct touches<Geometry1, Geometry2, Tag1, Tag2, CastedTag1, CastedTag2, true>
307 : touches<Geometry2, Geometry1, Tag2, Tag1, CastedTag2, CastedTag1, false>
309 static inline bool apply(Geometry1 const& g1, Geometry2 const& g2)
311 return touches<Geometry2, Geometry1>::apply(g2, g1);
317 template <typename Geometry1, typename Geometry2, typename Tag1, typename Tag2>
318 struct touches<Geometry1, Geometry2, Tag1, Tag2, pointlike_tag, pointlike_tag, false>
320 static inline bool apply(Geometry1 const& , Geometry2 const& )
328 template <typename Point, typename Geometry, typename Tag2, typename CastedTag2>
329 struct touches<Point, Geometry, point_tag, Tag2, pointlike_tag, CastedTag2, false>
330 : detail::touches::use_point_in_geometry
333 // TODO: support touches(MPt, Linear/Areal)
337 template <typename Box1, typename Box2, typename CastedTag1, typename CastedTag2>
338 struct touches<Box1, Box2, box_tag, box_tag, CastedTag1, CastedTag2, false>
339 : detail::touches::box_box
342 template <typename Box1, typename Box2>
343 struct touches<Box1, Box2, box_tag, box_tag, areal_tag, areal_tag, false>
344 : detail::touches::box_box
349 template <typename Linear1, typename Linear2, typename Tag1, typename Tag2>
350 struct touches<Linear1, Linear2, Tag1, Tag2, linear_tag, linear_tag, false>
351 : detail::relate::relate_base
353 detail::relate::static_mask_touches_type,
361 template <typename Linear, typename Areal, typename Tag1, typename Tag2>
362 struct touches<Linear, Areal, Tag1, Tag2, linear_tag, areal_tag, false>
363 : detail::relate::relate_base
365 detail::relate::static_mask_touches_type,
372 template <typename Linear, typename Areal, typename Tag1, typename Tag2>
373 struct touches<Linear, Areal, Tag1, Tag2, linear_tag, areal_tag, true>
374 : detail::relate::relate_base
376 detail::relate::static_mask_touches_type,
384 template <typename Areal1, typename Areal2, typename Tag1, typename Tag2>
385 struct touches<Areal1, Areal2, Tag1, Tag2, areal_tag, areal_tag, false>
386 : detail::touches::areal_areal<Areal1, Areal2>
389 } // namespace dispatch
390 #endif // DOXYGEN_NO_DISPATCH
393 namespace resolve_variant {
395 template <typename Geometry1, typename Geometry2>
398 static bool apply(Geometry1 const& geometry1, Geometry2 const& geometry2)
400 concept::check<Geometry1 const>();
401 concept::check<Geometry2 const>();
403 return dispatch::touches<Geometry1, Geometry2>
404 ::apply(geometry1, geometry2);
408 template <BOOST_VARIANT_ENUM_PARAMS(typename T), typename Geometry2>
409 struct touches<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Geometry2>
411 struct visitor: boost::static_visitor<bool>
413 Geometry2 const& m_geometry2;
415 visitor(Geometry2 const& geometry2): m_geometry2(geometry2) {}
417 template <typename Geometry1>
418 bool operator()(Geometry1 const& geometry1) const
420 return touches<Geometry1, Geometry2>::apply(geometry1, m_geometry2);
425 apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry1,
426 Geometry2 const& geometry2)
428 return boost::apply_visitor(visitor(geometry2), geometry1);
432 template <typename Geometry1, BOOST_VARIANT_ENUM_PARAMS(typename T)>
433 struct touches<Geometry1, boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
435 struct visitor: boost::static_visitor<bool>
437 Geometry1 const& m_geometry1;
439 visitor(Geometry1 const& geometry1): m_geometry1(geometry1) {}
441 template <typename Geometry2>
442 bool operator()(Geometry2 const& geometry2) const
444 return touches<Geometry1, Geometry2>::apply(m_geometry1, geometry2);
449 apply(Geometry1 const& geometry1,
450 boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry2)
452 return boost::apply_visitor(visitor(geometry1), geometry2);
456 template <BOOST_VARIANT_ENUM_PARAMS(typename T1),
457 BOOST_VARIANT_ENUM_PARAMS(typename T2)>
458 struct touches<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T1)>,
459 boost::variant<BOOST_VARIANT_ENUM_PARAMS(T2)> >
461 struct visitor: boost::static_visitor<bool>
463 template <typename Geometry1, typename Geometry2>
464 bool operator()(Geometry1 const& geometry1,
465 Geometry2 const& geometry2) const
467 return touches<Geometry1, Geometry2>::apply(geometry1, geometry2);
472 apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T1)> const& geometry1,
473 boost::variant<BOOST_VARIANT_ENUM_PARAMS(T2)> const& geometry2)
475 return boost::apply_visitor(visitor(), geometry1, geometry2);
479 template <typename Geometry>
482 static bool apply(Geometry const& geometry)
484 concept::check<Geometry const>();
486 typedef detail::no_rescale_policy rescale_policy_type;
487 typedef typename geometry::point_type<Geometry>::type point_type;
488 typedef detail::overlay::turn_info
491 typename segment_ratio_type<point_type, rescale_policy_type>::type
494 typedef detail::overlay::get_turn_info
496 detail::overlay::assign_null_policy
499 std::deque<turn_info> turns;
500 detail::touches::areal_interrupt_policy policy;
501 rescale_policy_type robust_policy;
502 detail::self_get_turn_points::get_turns
505 >::apply(geometry, robust_policy, turns, policy);
507 return policy.result();
511 template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
512 struct self_touches<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
514 struct visitor: boost::static_visitor<bool>
516 template <typename Geometry>
517 bool operator()(Geometry const& geometry) const
519 return self_touches<Geometry>::apply(geometry);
524 apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry)
526 return boost::apply_visitor(visitor(), geometry);
530 } // namespace resolve_variant
534 \brief \brief_check{has at least one touching point (self-tangency)}
535 \note This function can be called for one geometry (self-tangency) and
536 also for two geometries (touch)
538 \tparam Geometry \tparam_geometry
539 \param geometry \param_geometry
540 \return \return_check{is self-touching}
542 \qbk{distinguish,one geometry}
543 \qbk{[def __one_parameter__]}
544 \qbk{[include reference/algorithms/touches.qbk]}
546 template <typename Geometry>
547 inline bool touches(Geometry const& geometry)
549 return resolve_variant::self_touches<Geometry>::apply(geometry);
554 \brief \brief_check2{have at least one touching point (tangent - non overlapping)}
556 \tparam Geometry1 \tparam_geometry
557 \tparam Geometry2 \tparam_geometry
558 \param geometry1 \param_geometry
559 \param geometry2 \param_geometry
560 \return \return_check2{touch each other}
562 \qbk{distinguish,two geometries}
563 \qbk{[include reference/algorithms/touches.qbk]}
565 template <typename Geometry1, typename Geometry2>
566 inline bool touches(Geometry1 const& geometry1, Geometry2 const& geometry2)
568 return resolve_variant::touches<Geometry1, Geometry2>::apply(geometry1, geometry2);
572 }} // namespace boost::geometry
574 #endif // BOOST_GEOMETRY_ALGORITHMS_TOUCHES_HPP