1 // Boost.Geometry Index
2 //
3 // squared distance between point and nearest point of the box or point
4 //
5 // Copyright (c) 2011-2014 Adam Wulkiewicz, Lodz, Poland.
6 //
7 // Use, modification and distribution is subject to the Boost Software License,
8 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 
11 #ifndef BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_COMPARABLE_DISTANCE_NEAR_HPP
12 #define BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_COMPARABLE_DISTANCE_NEAR_HPP
13 
14 #include <boost/geometry/index/detail/algorithms/sum_for_indexable.hpp>
15 
16 namespace boost { namespace geometry { namespace index { namespace detail {
17 
18 struct comparable_distance_near_tag {};
19 
20 template <
21     typename Point,
22     typename PointIndexable,
23     size_t N>
24 struct sum_for_indexable<Point, PointIndexable, point_tag, comparable_distance_near_tag, N>
25 {
26     typedef typename geometry::default_comparable_distance_result<Point, PointIndexable>::type result_type;
27 
applyboost::geometry::index::detail::sum_for_indexable28     inline static result_type apply(Point const& pt, PointIndexable const& i)
29     {
30         return geometry::comparable_distance(pt, i);
31     }
32 };
33 
34 template <
35     typename Point,
36     typename BoxIndexable,
37     size_t DimensionIndex>
38 struct sum_for_indexable_dimension<Point, BoxIndexable, box_tag, comparable_distance_near_tag, DimensionIndex>
39 {
40     typedef typename geometry::default_comparable_distance_result<Point, BoxIndexable>::type result_type;
41 
applyboost::geometry::index::detail::sum_for_indexable_dimension42     inline static result_type apply(Point const& pt, BoxIndexable const& i)
43     {
44         typedef typename coordinate_type<Point>::type point_coord_t;
45         typedef typename coordinate_type<BoxIndexable>::type indexable_coord_t;
46 
47         point_coord_t pt_c = geometry::get<DimensionIndex>(pt);
48         indexable_coord_t ind_c_min = geometry::get<geometry::min_corner, DimensionIndex>(i);
49         indexable_coord_t ind_c_max = geometry::get<geometry::max_corner, DimensionIndex>(i);
50 
51         result_type diff = 0;
52 
53         if ( pt_c < ind_c_min )
54             diff = ind_c_min - pt_c;
55         else if ( ind_c_max < pt_c )
56             diff = pt_c - ind_c_max;
57 
58         return diff * diff;
59     }
60 };
61 
62 template <typename Point, typename Indexable>
63 typename geometry::default_comparable_distance_result<Point, Indexable>::type
comparable_distance_near(Point const & pt,Indexable const & i)64 comparable_distance_near(Point const& pt, Indexable const& i)
65 {
66     return detail::sum_for_indexable<
67         Point,
68         Indexable,
69         typename tag<Indexable>::type,
70         detail::comparable_distance_near_tag,
71         dimension<Indexable>::value
72     >::apply(pt, i);
73 }
74 
75 }}}} // namespace boost::geometry::index::detail
76 
77 #endif // BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_COMPARABLE_DISTANCE_NEAR_HPP
78