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.
7 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
8 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
10 // Use, modification and distribution is subject to the Boost Software License,
11 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
12 // http://www.boost.org/LICENSE_1_0.txt)
14 #ifndef BOOST_GEOMETRY_ALGORITHMS_WITHIN_HPP
15 #define BOOST_GEOMETRY_ALGORITHMS_WITHIN_HPP
20 #include <boost/range.hpp>
21 #include <boost/typeof/typeof.hpp>
23 #include <boost/geometry/algorithms/make.hpp>
24 #include <boost/geometry/algorithms/not_implemented.hpp>
26 #include <boost/geometry/core/access.hpp>
27 #include <boost/geometry/core/closure.hpp>
28 #include <boost/geometry/core/cs.hpp>
29 #include <boost/geometry/core/exterior_ring.hpp>
30 #include <boost/geometry/core/interior_rings.hpp>
31 #include <boost/geometry/core/point_order.hpp>
32 #include <boost/geometry/core/ring_type.hpp>
33 #include <boost/geometry/core/interior_rings.hpp>
34 #include <boost/geometry/core/tags.hpp>
36 #include <boost/geometry/geometries/concepts/check.hpp>
37 #include <boost/geometry/strategies/within.hpp>
38 #include <boost/geometry/strategies/concepts/within_concept.hpp>
39 #include <boost/geometry/util/math.hpp>
40 #include <boost/geometry/util/order_as_direction.hpp>
41 #include <boost/geometry/views/closeable_view.hpp>
42 #include <boost/geometry/views/reversible_view.hpp>
45 namespace boost { namespace geometry
48 #ifndef DOXYGEN_NO_DETAIL
49 namespace detail { namespace within
57 iterate_direction Direction,
58 closure_selector Closure,
63 BOOST_CONCEPT_ASSERT( (geometry::concept::WithinStrategyPolygonal<Strategy>) );
65 static inline int apply(Point const& point, Ring const& ring,
66 Strategy const& strategy)
69 < core_detail::closure::minimum_ring_size<Closure>::value)
74 typedef typename reversible_view<Ring const, Direction>::type rev_view_type;
75 typedef typename closeable_view
77 rev_view_type const, Closure
79 typedef typename boost::range_iterator<cl_view_type const>::type iterator_type;
81 rev_view_type rev_view(ring);
82 cl_view_type view(rev_view);
83 typename Strategy::state_type state;
84 iterator_type it = boost::begin(view);
85 iterator_type end = boost::end(view);
88 for (iterator_type previous = it++;
92 if (! strategy.apply(point, *previous, *it, state))
98 return strategy.result(state);
103 // Polygon: in exterior ring, and if so, not within interior ring(s)
108 iterate_direction Direction,
109 closure_selector Closure,
112 struct point_in_polygon
114 BOOST_CONCEPT_ASSERT( (geometry::concept::WithinStrategyPolygonal<Strategy>) );
116 static inline int apply(Point const& point, Polygon const& poly,
117 Strategy const& strategy)
119 int const code = point_in_ring
122 typename ring_type<Polygon>::type,
126 >::apply(point, exterior_ring(poly), strategy);
130 typename interior_return_type<Polygon const>::type rings
131 = interior_rings(poly);
132 for (BOOST_AUTO_TPL(it, boost::begin(rings));
133 it != boost::end(rings);
136 int const interior_code = point_in_ring
139 typename ring_type<Polygon>::type,
143 >::apply(point, *it, strategy);
145 if (interior_code != -1)
147 // If 0, return 0 (touch)
148 // If 1 (inside hole) return -1 (outside polygon)
149 // If -1 (outside hole) check other holes if any
150 return -interior_code;
158 }} // namespace detail::within
159 #endif // DOXYGEN_NO_DETAIL
162 #ifndef DOXYGEN_NO_DISPATCH
170 typename Tag1 = typename tag<Geometry1>::type,
171 typename Tag2 = typename tag<Geometry2>::type
173 struct within: not_implemented<Tag1, Tag2>
177 template <typename Point, typename Box>
178 struct within<Point, Box, point_tag, box_tag>
180 template <typename Strategy>
181 static inline bool apply(Point const& point, Box const& box, Strategy const& strategy)
183 return strategy.apply(point, box);
187 template <typename Box1, typename Box2>
188 struct within<Box1, Box2, box_tag, box_tag>
190 template <typename Strategy>
191 static inline bool apply(Box1 const& box1, Box2 const& box2, Strategy const& strategy)
193 assert_dimension_equal<Box1, Box2>();
194 return strategy.apply(box1, box2);
200 template <typename Point, typename Ring>
201 struct within<Point, Ring, point_tag, ring_tag>
203 template <typename Strategy>
204 static inline bool apply(Point const& point, Ring const& ring, Strategy const& strategy)
206 return detail::within::point_in_ring
210 order_as_direction<geometry::point_order<Ring>::value>::value,
211 geometry::closure<Ring>::value,
213 >::apply(point, ring, strategy) == 1;
217 template <typename Point, typename Polygon>
218 struct within<Point, Polygon, point_tag, polygon_tag>
220 template <typename Strategy>
221 static inline bool apply(Point const& point, Polygon const& polygon, Strategy const& strategy)
223 return detail::within::point_in_polygon
227 order_as_direction<geometry::point_order<Polygon>::value>::value,
228 geometry::closure<Polygon>::value,
230 >::apply(point, polygon, strategy) == 1;
234 } // namespace dispatch
235 #endif // DOXYGEN_NO_DISPATCH
239 \brief \brief_check12{is completely inside}
241 \details \details_check12{within, is completely inside}.
242 \tparam Geometry1 \tparam_geometry
243 \tparam Geometry2 \tparam_geometry
244 \param geometry1 \param_geometry which might be within the second geometry
245 \param geometry2 \param_geometry which might contain the first geometry
246 \return true if geometry1 is completely contained within geometry2,
248 \note The default strategy is used for within detection
251 \qbk{[include reference/algorithms/within.qbk]}
259 template<typename Geometry1, typename Geometry2>
260 inline bool within(Geometry1 const& geometry1, Geometry2 const& geometry2)
262 concept::check<Geometry1 const>();
263 concept::check<Geometry2 const>();
264 assert_dimension_equal<Geometry1, Geometry2>();
266 typedef typename point_type<Geometry1>::type point_type1;
267 typedef typename point_type<Geometry2>::type point_type2;
269 typedef typename strategy::within::services::default_strategy
271 typename tag<Geometry1>::type,
272 typename tag<Geometry2>::type,
273 typename tag<Geometry1>::type,
274 typename tag_cast<typename tag<Geometry2>::type, areal_tag>::type,
277 typename cs_tag<point_type1>::type, spherical_tag
281 typename cs_tag<point_type2>::type, spherical_tag
285 >::type strategy_type;
287 return dispatch::within
291 >::apply(geometry1, geometry2, strategy_type());
295 \brief \brief_check12{is completely inside} \brief_strategy
297 \details \details_check12{within, is completely inside}, \brief_strategy. \details_strategy_reasons
298 \tparam Geometry1 \tparam_geometry
299 \tparam Geometry2 \tparam_geometry
300 \param geometry1 \param_geometry which might be within the second geometry
301 \param geometry2 \param_geometry which might contain the first geometry
302 \param strategy strategy to be used
303 \return true if geometry1 is completely contained within geometry2,
306 \qbk{distinguish,with strategy}
307 \qbk{[include reference/algorithms/within.qbk]}
309 [heading Available Strategies]
310 \* [link geometry.reference.strategies.strategy_within_winding Winding (coordinate system agnostic)]
311 \* [link geometry.reference.strategies.strategy_within_franklin Franklin (cartesian)]
312 \* [link geometry.reference.strategies.strategy_within_crossings_multiply Crossings Multiply (cartesian)]
316 [within_strategy_output]
320 template<typename Geometry1, typename Geometry2, typename Strategy>
321 inline bool within(Geometry1 const& geometry1, Geometry2 const& geometry2,
322 Strategy const& strategy)
324 concept::within::check
326 typename tag<Geometry1>::type,
327 typename tag<Geometry2>::type,
328 typename tag_cast<typename tag<Geometry2>::type, areal_tag>::type,
331 concept::check<Geometry1 const>();
332 concept::check<Geometry2 const>();
333 assert_dimension_equal<Geometry1, Geometry2>();
335 return dispatch::within
339 >::apply(geometry1, geometry2, strategy);
342 }} // namespace boost::geometry
344 #endif // BOOST_GEOMETRY_ALGORITHMS_WITHIN_HPP