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-2020.
9 // Modifications copyright (c) 2013-2020, 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_DETAIL_TOUCHES_IMPLEMENTATION_HPP
21 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_TOUCHES_IMPLEMENTATION_HPP
24 #include <type_traits>
26 #include <boost/geometry/algorithms/detail/for_each_range.hpp>
27 #include <boost/geometry/algorithms/detail/overlay/overlay.hpp>
28 #include <boost/geometry/algorithms/detail/overlay/self_turn_points.hpp>
29 #include <boost/geometry/algorithms/detail/sub_range.hpp>
30 #include <boost/geometry/algorithms/detail/relate/relate_impl.hpp>
31 #include <boost/geometry/algorithms/detail/touches/interface.hpp>
32 #include <boost/geometry/algorithms/disjoint.hpp>
33 #include <boost/geometry/algorithms/intersects.hpp>
34 #include <boost/geometry/algorithms/num_geometries.hpp>
35 #include <boost/geometry/algorithms/relate.hpp>
37 #include <boost/geometry/policies/robustness/no_rescale_policy.hpp>
39 #include <boost/geometry/strategies/relate/cartesian.hpp>
40 #include <boost/geometry/strategies/relate/geographic.hpp>
41 #include <boost/geometry/strategies/relate/spherical.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((std::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_start :
189 case overlay::method_none :
190 case overlay::method_disjoint :
191 case overlay::method_error :
199 template <typename Turn>
200 inline bool ok_for_touch(Turn const& turn)
202 return turn.both(overlay::operation_union)
203 || turn.both(overlay::operation_blocked)
204 || turn.combination(overlay::operation_union, overlay::operation_blocked)
209 template <typename Geometry1, typename Geometry2, typename Strategy>
210 inline bool point_on_border_within(Geometry1 const& geometry1,
211 Geometry2 const& geometry2,
212 Strategy const& strategy)
214 typename geometry::point_type<Geometry1>::type pt;
215 return geometry::point_on_border(pt, geometry1)
216 && geometry::within(pt, geometry2, strategy);
219 template <typename FirstGeometry, typename SecondGeometry, typename Strategy>
220 inline bool rings_containing(FirstGeometry const& geometry1,
221 SecondGeometry const& geometry2,
222 Strategy const& strategy)
224 return geometry::detail::any_range_of(geometry2, [&](auto const& range)
226 return point_on_border_within(range, geometry1, strategy);
230 template <typename Geometry1, typename Geometry2>
233 template <typename Strategy>
234 static inline bool apply(Geometry1 const& geometry1,
235 Geometry2 const& geometry2,
236 Strategy const& strategy)
238 typedef typename geometry::point_type<Geometry1>::type point_type;
239 typedef detail::overlay::turn_info<point_type> turn_info;
241 std::deque<turn_info> turns;
242 detail::touches::areal_interrupt_policy policy;
245 detail::overlay::do_reverse<geometry::point_order<Geometry1>::value>::value,
246 detail::overlay::do_reverse<geometry::point_order<Geometry2>::value>::value,
247 detail::overlay::assign_null_policy
248 >(geometry1, geometry2, strategy, detail::no_rescale_policy(), turns, policy);
250 return policy.result()
251 && ! geometry::detail::touches::rings_containing(geometry1, geometry2, strategy)
252 && ! geometry::detail::touches::rings_containing(geometry2, geometry1, strategy);
258 struct use_point_in_geometry
260 template <typename Point, typename Geometry, typename Strategy>
261 static inline bool apply(Point const& point, Geometry const& geometry, Strategy const& strategy)
263 return detail::within::point_in_geometry(point, geometry, strategy) == 0;
269 #endif // DOXYGEN_NO_DETAIL
271 #ifndef DOXYGEN_NO_DISPATCH
276 template <typename Geometry1, typename Geometry2>
277 struct touches<Geometry1, Geometry2, point_tag, point_tag, pointlike_tag, pointlike_tag, false>
279 template <typename Strategy>
280 static inline bool apply(Geometry1 const& , Geometry2 const& , Strategy const&)
286 template <typename Geometry1, typename Geometry2>
287 struct touches<Geometry1, Geometry2, point_tag, multi_point_tag, pointlike_tag, pointlike_tag, false>
289 template <typename Strategy>
290 static inline bool apply(Geometry1 const& , Geometry2 const& , Strategy const&)
296 template <typename Geometry1, typename Geometry2>
297 struct touches<Geometry1, Geometry2, multi_point_tag, multi_point_tag, pointlike_tag, pointlike_tag, false>
299 template <typename Strategy>
300 static inline bool apply(Geometry1 const&, Geometry2 const&, Strategy const&)
308 template <typename Point, typename Geometry, typename Tag2, typename CastedTag2>
309 struct touches<Point, Geometry, point_tag, Tag2, pointlike_tag, CastedTag2, false>
310 : detail::touches::use_point_in_geometry
313 template <typename MultiPoint, typename MultiGeometry, typename Tag2, typename CastedTag2>
314 struct touches<MultiPoint, MultiGeometry, multi_point_tag, Tag2, pointlike_tag, CastedTag2, false>
315 : detail::relate::relate_impl
317 detail::de9im::static_mask_touches_type,
325 template <typename Geometry, typename MultiPoint, typename Tag1, typename CastedTag1>
326 struct touches<Geometry, MultiPoint, Tag1, multi_point_tag, CastedTag1, pointlike_tag, false>
327 : detail::relate::relate_impl
329 detail::de9im::static_mask_touches_type,
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_impl
353 detail::de9im::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_impl
365 detail::de9im::static_mask_touches_type,
372 template <typename Linear, typename Areal, typename Tag1, typename Tag2>
373 struct touches<Areal, Linear, Tag1, Tag2, areal_tag, linear_tag, false>
374 : detail::relate::relate_impl
376 detail::de9im::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::relate::relate_impl
388 detail::de9im::static_mask_touches_type,
394 template <typename Areal1, typename Areal2>
395 struct touches<Areal1, Areal2, ring_tag, ring_tag, areal_tag, areal_tag, false>
396 : detail::touches::areal_areal<Areal1, Areal2>
399 } // namespace dispatch
400 #endif // DOXYGEN_NO_DISPATCH
403 namespace resolve_variant
406 template <typename Geometry>
409 static bool apply(Geometry const& geometry)
411 concepts::check<Geometry const>();
413 typedef typename strategies::relate::services::default_strategy
416 >::type strategy_type;
417 typedef typename geometry::point_type<Geometry>::type point_type;
418 typedef detail::overlay::turn_info<point_type> turn_info;
420 typedef detail::overlay::get_turn_info
422 detail::overlay::assign_null_policy
425 std::deque<turn_info> turns;
426 detail::touches::areal_interrupt_policy policy;
427 strategy_type strategy;
428 // TODO: skip_adjacent should be set to false
429 detail::self_get_turn_points::get_turns
432 >::apply(geometry, strategy, detail::no_rescale_policy(),
433 turns, policy, 0, true);
435 return policy.result();
441 }} // namespace boost::geometry
443 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_TOUCHES_IMPLEMENTATION_HPP