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_DETAIL_TOUCHES_IMPLEMENTATION_HPP
21 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_TOUCHES_IMPLEMENTATION_HPP
24 #include <boost/geometry/algorithms/detail/for_each_range.hpp>
25 #include <boost/geometry/algorithms/detail/overlay/overlay.hpp>
26 #include <boost/geometry/algorithms/detail/overlay/self_turn_points.hpp>
27 #include <boost/geometry/algorithms/detail/sub_range.hpp>
28 #include <boost/geometry/algorithms/detail/relate/relate_impl.hpp>
29 #include <boost/geometry/algorithms/detail/touches/interface.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/relate.hpp>
35 #include <boost/geometry/policies/robustness/no_rescale_policy.hpp>
38 namespace boost { namespace geometry
41 #ifndef DOXYGEN_NO_DETAIL
42 namespace detail { namespace touches
49 std::size_t Dimension,
50 std::size_t DimensionCount
54 template <typename Box1, typename Box2>
55 static inline bool apply(Box1 const& b1, Box2 const& b2, bool & touch)
57 typedef typename coordinate_type<Box1>::type coordinate_type1;
58 typedef typename coordinate_type<Box2>::type coordinate_type2;
60 coordinate_type1 const& min1 = get<min_corner, Dimension>(b1);
61 coordinate_type1 const& max1 = get<max_corner, Dimension>(b1);
62 coordinate_type2 const& min2 = get<min_corner, Dimension>(b2);
63 coordinate_type2 const& max2 = get<max_corner, Dimension>(b2);
65 // TODO assert or exception?
66 //BOOST_GEOMETRY_ASSERT(min1 <= max1 && min2 <= max2);
68 if (max1 < min2 || max2 < min1)
73 if (max1 == min2 || max2 == min1)
82 >::apply(b1, b2, touch);
88 std::size_t DimensionCount
90 struct box_box_loop<DimensionCount, DimensionCount>
92 template <typename Box1, typename Box2>
93 static inline bool apply(Box1 const& , Box2 const&, bool &)
101 template <typename Box1, typename Box2, typename Strategy>
102 static inline bool apply(Box1 const& b1, Box2 const& b2, Strategy const& /*strategy*/)
104 BOOST_STATIC_ASSERT((boost::is_same
106 typename geometry::coordinate_system<Box1>::type,
107 typename geometry::coordinate_system<Box2>::type
110 assert_dimension_equal<Box1, Box2>();
112 bool touches = false;
113 bool ok = box_box_loop
116 dimension<Box1>::type::value
117 >::apply(b1, b2, touches);
119 return ok && touches;
125 struct areal_interrupt_policy
127 static bool const enabled = true;
129 bool found_not_touch;
131 // dummy variable required by self_get_turn_points::get_turns
132 static bool const has_intersections = false;
136 return found_touch && !found_not_touch;
139 inline areal_interrupt_policy()
140 : found_touch(false), found_not_touch(false)
143 template <typename Range>
144 inline bool apply(Range const& range)
146 // if already rejected (temp workaround?)
147 if ( found_not_touch )
150 typedef typename boost::range_iterator<Range const>::type iterator;
151 for ( iterator it = boost::begin(range) ; it != boost::end(range) ; ++it )
153 if ( it->has(overlay::operation_intersection) )
155 found_not_touch = true;
161 case overlay::method_crosses:
162 found_not_touch = true;
164 case overlay::method_equal:
165 // Segment spatially equal means: at the right side
166 // the polygon internally overlaps. So return false.
167 found_not_touch = true;
169 case overlay::method_touch:
170 case overlay::method_touch_interior:
171 case overlay::method_collinear:
172 if ( ok_for_touch(*it) )
178 found_not_touch = true;
182 case overlay::method_none :
183 case overlay::method_disjoint :
184 case overlay::method_error :
192 template <typename Turn>
193 inline bool ok_for_touch(Turn const& turn)
195 return turn.both(overlay::operation_union)
196 || turn.both(overlay::operation_blocked)
197 || turn.combination(overlay::operation_union, overlay::operation_blocked)
202 template<typename Geometry, typename PointInRingStrategy>
203 struct check_each_ring_for_within
206 Geometry const& m_geometry;
207 PointInRingStrategy const& m_strategy;
209 inline check_each_ring_for_within(Geometry const& g, PointInRingStrategy const& strategy)
212 , m_strategy(strategy)
215 template <typename Range>
216 inline void apply(Range const& range)
218 typename geometry::point_type<Range>::type p;
219 geometry::point_on_border(p, range);
220 if ( !has_within && geometry::within(p, m_geometry, m_strategy) )
227 template <typename FirstGeometry, typename SecondGeometry, typename IntersectionStrategy>
228 inline bool rings_containing(FirstGeometry const& geometry1,
229 SecondGeometry const& geometry2,
230 IntersectionStrategy const& strategy)
232 // NOTE: This strategy could be defined inside IntersectionStrategy
233 typedef typename IntersectionStrategy::template point_in_geometry_strategy
235 FirstGeometry, SecondGeometry
236 >::type point_in_ring_strategy_type;
238 point_in_ring_strategy_type point_in_ring_strategy
239 = strategy.template get_point_in_geometry_strategy<FirstGeometry, SecondGeometry>();
241 check_each_ring_for_within
243 FirstGeometry, point_in_ring_strategy_type
244 > checker(geometry1, point_in_ring_strategy);
245 geometry::detail::for_each_range(geometry2, checker);
246 return checker.has_within;
249 template <typename Geometry1, typename Geometry2>
252 template <typename IntersectionStrategy>
253 static inline bool apply(Geometry1 const& geometry1,
254 Geometry2 const& geometry2,
255 IntersectionStrategy const& strategy)
257 typedef detail::no_rescale_policy rescale_policy_type;
258 typedef typename geometry::point_type<Geometry1>::type point_type;
259 typedef detail::overlay::turn_info
262 typename segment_ratio_type<point_type, rescale_policy_type>::type
265 std::deque<turn_info> turns;
266 detail::touches::areal_interrupt_policy policy;
267 rescale_policy_type robust_policy;
268 boost::geometry::get_turns
270 detail::overlay::do_reverse<geometry::point_order<Geometry1>::value>::value,
271 detail::overlay::do_reverse<geometry::point_order<Geometry2>::value>::value,
272 detail::overlay::assign_null_policy
273 >(geometry1, geometry2, strategy, robust_policy, turns, policy);
275 return policy.result()
276 && ! geometry::detail::touches::rings_containing(geometry1, geometry2, strategy)
277 && ! geometry::detail::touches::rings_containing(geometry2, geometry1, strategy);
283 struct use_point_in_geometry
285 template <typename Point, typename Geometry, typename Strategy>
286 static inline bool apply(Point const& point, Geometry const& geometry, Strategy const& strategy)
288 return detail::within::point_in_geometry(point, geometry, strategy) == 0;
294 #endif // DOXYGEN_NO_DETAIL
296 #ifndef DOXYGEN_NO_DISPATCH
301 template <typename Geometry1, typename Geometry2, typename Tag2>
302 struct touches<Geometry1, Geometry2, point_tag, Tag2, pointlike_tag, pointlike_tag, false>
304 template <typename Strategy>
305 static inline bool apply(Geometry1 const& , Geometry2 const& , Strategy const&)
311 template <typename Geometry1, typename Geometry2, typename Tag2>
312 struct touches<Geometry1, Geometry2, multi_point_tag, Tag2, pointlike_tag, pointlike_tag, false>
314 template <typename Strategy>
315 static inline bool apply(Geometry1 const&, Geometry2 const&, Strategy const&)
323 template <typename Point, typename Geometry, typename Tag2, typename CastedTag2>
324 struct touches<Point, Geometry, point_tag, Tag2, pointlike_tag, CastedTag2, false>
325 : detail::touches::use_point_in_geometry
328 template <typename MultiPoint, typename MultiGeometry, typename Tag2, typename CastedTag2>
329 struct touches<MultiPoint, MultiGeometry, multi_point_tag, Tag2, pointlike_tag, CastedTag2, false>
330 : detail::relate::relate_impl
332 detail::de9im::static_mask_touches_type,
338 template <typename Geometry, typename MultiPoint, typename Tag1, typename CastedTag1>
339 struct touches<Geometry, MultiPoint, Tag1, multi_point_tag, CastedTag1, pointlike_tag, false>
340 : detail::relate::relate_impl
342 detail::de9im::static_mask_touches_type,
350 template <typename Box1, typename Box2, typename CastedTag1, typename CastedTag2>
351 struct touches<Box1, Box2, box_tag, box_tag, CastedTag1, CastedTag2, false>
352 : detail::touches::box_box
355 template <typename Box1, typename Box2>
356 struct touches<Box1, Box2, box_tag, box_tag, areal_tag, areal_tag, false>
357 : detail::touches::box_box
362 template <typename Linear1, typename Linear2, typename Tag1, typename Tag2>
363 struct touches<Linear1, Linear2, Tag1, Tag2, linear_tag, linear_tag, false>
364 : detail::relate::relate_impl
366 detail::de9im::static_mask_touches_type,
374 template <typename Linear, typename Areal, typename Tag1, typename Tag2>
375 struct touches<Linear, Areal, Tag1, Tag2, linear_tag, areal_tag, false>
376 : detail::relate::relate_impl
378 detail::de9im::static_mask_touches_type,
385 template <typename Linear, typename Areal, typename Tag1, typename Tag2>
386 struct touches<Areal, Linear, Tag1, Tag2, areal_tag, linear_tag, false>
387 : detail::relate::relate_impl
389 detail::de9im::static_mask_touches_type,
397 template <typename Areal1, typename Areal2, typename Tag1, typename Tag2>
398 struct touches<Areal1, Areal2, Tag1, Tag2, areal_tag, areal_tag, false>
399 : detail::relate::relate_impl
401 detail::de9im::static_mask_touches_type,
407 template <typename Areal1, typename Areal2>
408 struct touches<Areal1, Areal2, ring_tag, ring_tag, areal_tag, areal_tag, false>
409 : detail::touches::areal_areal<Areal1, Areal2>
412 } // namespace dispatch
413 #endif // DOXYGEN_NO_DISPATCH
416 namespace resolve_variant
419 template <typename Geometry>
422 static bool apply(Geometry const& geometry)
424 concepts::check<Geometry const>();
426 typedef typename strategy::relate::services::default_strategy
429 >::type strategy_type;
430 typedef detail::no_rescale_policy rescale_policy_type;
431 typedef typename geometry::point_type<Geometry>::type point_type;
432 typedef detail::overlay::turn_info
435 typename segment_ratio_type<point_type, rescale_policy_type>::type
438 typedef detail::overlay::get_turn_info
440 detail::overlay::assign_null_policy
443 std::deque<turn_info> turns;
444 detail::touches::areal_interrupt_policy policy;
445 strategy_type strategy;
446 rescale_policy_type robust_policy;
447 // TODO: skip_adjacent should be set to false
448 detail::self_get_turn_points::get_turns
451 >::apply(geometry, strategy, robust_policy, turns, policy, 0, true);
453 return policy.result();
459 }} // namespace boost::geometry
461 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_TOUCHES_IMPLEMENTATION_HPP