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>
40 namespace boost { namespace geometry
43 #ifndef DOXYGEN_NO_DETAIL
44 namespace detail { namespace touches
51 std::size_t Dimension,
52 std::size_t DimensionCount
56 template <typename Box1, typename Box2>
57 static inline bool apply(Box1 const& b1, Box2 const& b2, bool & touch)
59 typedef typename coordinate_type<Box1>::type coordinate_type1;
60 typedef typename coordinate_type<Box2>::type coordinate_type2;
62 coordinate_type1 const& min1 = get<min_corner, Dimension>(b1);
63 coordinate_type1 const& max1 = get<max_corner, Dimension>(b1);
64 coordinate_type2 const& min2 = get<min_corner, Dimension>(b2);
65 coordinate_type2 const& max2 = get<max_corner, Dimension>(b2);
67 // TODO assert or exception?
68 //BOOST_GEOMETRY_ASSERT(min1 <= max1 && min2 <= max2);
70 if (max1 < min2 || max2 < min1)
75 if (max1 == min2 || max2 == min1)
84 >::apply(b1, b2, touch);
90 std::size_t DimensionCount
92 struct box_box_loop<DimensionCount, DimensionCount>
94 template <typename Box1, typename Box2>
95 static inline bool apply(Box1 const& , Box2 const&, bool &)
103 template <typename Box1, typename Box2, typename Strategy>
104 static inline bool apply(Box1 const& b1, Box2 const& b2, Strategy const& /*strategy*/)
106 BOOST_STATIC_ASSERT((std::is_same
108 typename geometry::coordinate_system<Box1>::type,
109 typename geometry::coordinate_system<Box2>::type
112 assert_dimension_equal<Box1, Box2>();
114 bool touches = false;
115 bool ok = box_box_loop
118 dimension<Box1>::type::value
119 >::apply(b1, b2, touches);
121 return ok && touches;
127 struct areal_interrupt_policy
129 static bool const enabled = true;
131 bool found_not_touch;
133 // dummy variable required by self_get_turn_points::get_turns
134 static bool const has_intersections = false;
138 return found_touch && !found_not_touch;
141 inline areal_interrupt_policy()
142 : found_touch(false), found_not_touch(false)
145 template <typename Range>
146 inline bool apply(Range const& range)
148 // if already rejected (temp workaround?)
149 if ( found_not_touch )
152 typedef typename boost::range_iterator<Range const>::type iterator;
153 for ( iterator it = boost::begin(range) ; it != boost::end(range) ; ++it )
155 if ( it->has(overlay::operation_intersection) )
157 found_not_touch = true;
163 case overlay::method_crosses:
164 found_not_touch = true;
166 case overlay::method_equal:
167 // Segment spatially equal means: at the right side
168 // the polygon internally overlaps. So return false.
169 found_not_touch = true;
171 case overlay::method_touch:
172 case overlay::method_touch_interior:
173 case overlay::method_collinear:
174 if ( ok_for_touch(*it) )
180 found_not_touch = true;
184 case overlay::method_start :
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 Geometry1, typename Geometry2, typename Strategy>
206 inline bool point_on_border_within(Geometry1 const& geometry1,
207 Geometry2 const& geometry2,
208 Strategy const& strategy)
210 typename geometry::point_type<Geometry1>::type pt;
211 return geometry::point_on_border(pt, geometry1)
212 && geometry::within(pt, geometry2, strategy);
215 template <typename FirstGeometry, typename SecondGeometry, typename IntersectionStrategy>
216 inline bool rings_containing(FirstGeometry const& geometry1,
217 SecondGeometry const& geometry2,
218 IntersectionStrategy const& strategy)
220 // TODO: This will be removed when IntersectionStrategy is replaced with
222 auto const point_in_ring_strategy
223 = strategy.template get_point_in_geometry_strategy<FirstGeometry, SecondGeometry>();
225 return geometry::detail::any_range_of(geometry2, [&](auto const& range)
227 return point_on_border_within(range, geometry1, point_in_ring_strategy);
231 template <typename Geometry1, typename Geometry2>
234 template <typename IntersectionStrategy>
235 static inline bool apply(Geometry1 const& geometry1,
236 Geometry2 const& geometry2,
237 IntersectionStrategy const& strategy)
239 typedef typename geometry::point_type<Geometry1>::type point_type;
240 typedef detail::overlay::turn_info<point_type> turn_info;
242 std::deque<turn_info> turns;
243 detail::touches::areal_interrupt_policy policy;
244 boost::geometry::get_turns
246 detail::overlay::do_reverse<geometry::point_order<Geometry1>::value>::value,
247 detail::overlay::do_reverse<geometry::point_order<Geometry2>::value>::value,
248 detail::overlay::assign_null_policy
249 >(geometry1, geometry2, strategy, detail::no_rescale_policy(), turns, policy);
251 return policy.result()
252 && ! geometry::detail::touches::rings_containing(geometry1, geometry2, strategy)
253 && ! geometry::detail::touches::rings_containing(geometry2, geometry1, strategy);
259 struct use_point_in_geometry
261 template <typename Point, typename Geometry, typename Strategy>
262 static inline bool apply(Point const& point, Geometry const& geometry, Strategy const& strategy)
264 return detail::within::point_in_geometry(point, geometry, strategy) == 0;
270 #endif // DOXYGEN_NO_DETAIL
272 #ifndef DOXYGEN_NO_DISPATCH
277 template <typename Geometry1, typename Geometry2>
278 struct touches<Geometry1, Geometry2, point_tag, point_tag, pointlike_tag, pointlike_tag, false>
280 template <typename Strategy>
281 static inline bool apply(Geometry1 const& , Geometry2 const& , Strategy const&)
287 template <typename Geometry1, typename Geometry2>
288 struct touches<Geometry1, Geometry2, point_tag, multi_point_tag, pointlike_tag, pointlike_tag, false>
290 template <typename Strategy>
291 static inline bool apply(Geometry1 const& , Geometry2 const& , Strategy const&)
297 template <typename Geometry1, typename Geometry2>
298 struct touches<Geometry1, Geometry2, multi_point_tag, multi_point_tag, pointlike_tag, pointlike_tag, false>
300 template <typename Strategy>
301 static inline bool apply(Geometry1 const&, Geometry2 const&, Strategy const&)
309 template <typename Point, typename Geometry, typename Tag2, typename CastedTag2>
310 struct touches<Point, Geometry, point_tag, Tag2, pointlike_tag, CastedTag2, false>
311 : detail::touches::use_point_in_geometry
314 template <typename MultiPoint, typename MultiGeometry, typename Tag2, typename CastedTag2>
315 struct touches<MultiPoint, MultiGeometry, multi_point_tag, Tag2, pointlike_tag, CastedTag2, false>
316 : detail::relate::relate_impl
318 detail::de9im::static_mask_touches_type,
326 template <typename Geometry, typename MultiPoint, typename Tag1, typename CastedTag1>
327 struct touches<Geometry, MultiPoint, Tag1, multi_point_tag, CastedTag1, pointlike_tag, false>
328 : detail::relate::relate_impl
330 detail::de9im::static_mask_touches_type,
338 template <typename Box1, typename Box2, typename CastedTag1, typename CastedTag2>
339 struct touches<Box1, Box2, box_tag, box_tag, CastedTag1, CastedTag2, false>
340 : detail::touches::box_box
343 template <typename Box1, typename Box2>
344 struct touches<Box1, Box2, box_tag, box_tag, areal_tag, areal_tag, false>
345 : detail::touches::box_box
350 template <typename Linear1, typename Linear2, typename Tag1, typename Tag2>
351 struct touches<Linear1, Linear2, Tag1, Tag2, linear_tag, linear_tag, false>
352 : detail::relate::relate_impl
354 detail::de9im::static_mask_touches_type,
362 template <typename Linear, typename Areal, typename Tag1, typename Tag2>
363 struct touches<Linear, Areal, Tag1, Tag2, linear_tag, areal_tag, false>
364 : detail::relate::relate_impl
366 detail::de9im::static_mask_touches_type,
373 template <typename Linear, typename Areal, typename Tag1, typename Tag2>
374 struct touches<Areal, Linear, Tag1, Tag2, areal_tag, linear_tag, false>
375 : detail::relate::relate_impl
377 detail::de9im::static_mask_touches_type,
385 template <typename Areal1, typename Areal2, typename Tag1, typename Tag2>
386 struct touches<Areal1, Areal2, Tag1, Tag2, areal_tag, areal_tag, false>
387 : detail::relate::relate_impl
389 detail::de9im::static_mask_touches_type,
395 template <typename Areal1, typename Areal2>
396 struct touches<Areal1, Areal2, ring_tag, ring_tag, areal_tag, areal_tag, false>
397 : detail::touches::areal_areal<Areal1, Areal2>
400 } // namespace dispatch
401 #endif // DOXYGEN_NO_DISPATCH
404 namespace resolve_variant
407 template <typename Geometry>
410 static bool apply(Geometry const& geometry)
412 concepts::check<Geometry const>();
414 typedef typename strategy::relate::services::default_strategy
417 >::type strategy_type;
418 typedef typename geometry::point_type<Geometry>::type point_type;
419 typedef detail::overlay::turn_info<point_type> turn_info;
421 typedef detail::overlay::get_turn_info
423 detail::overlay::assign_null_policy
426 std::deque<turn_info> turns;
427 detail::touches::areal_interrupt_policy policy;
428 strategy_type strategy;
429 // TODO: skip_adjacent should be set to false
430 detail::self_get_turn_points::get_turns
433 >::apply(geometry, strategy, detail::no_rescale_policy(), turns, policy, 0, true);
435 return policy.result();
441 }} // namespace boost::geometry
443 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_TOUCHES_IMPLEMENTATION_HPP