comparable_distance_near.hpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_COMPARABLE_DISTANCE_NEAR_HPP
  11. #define BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_COMPARABLE_DISTANCE_NEAR_HPP
  12. #include <boost/geometry/index/detail/algorithms/sum_for_indexable.hpp>
  13. namespace boost { namespace geometry { namespace index { namespace detail {
  14. struct comparable_distance_near_tag {};
  15. template <
  16. typename Point,
  17. typename PointIndexable,
  18. size_t N>
  19. struct sum_for_indexable<Point, PointIndexable, point_tag, comparable_distance_near_tag, N>
  20. {
  21. typedef typename geometry::default_comparable_distance_result<Point, PointIndexable>::type result_type;
  22. inline static result_type apply(Point const& pt, PointIndexable const& i)
  23. {
  24. return geometry::comparable_distance(pt, i);
  25. }
  26. };
  27. template <
  28. typename Point,
  29. typename BoxIndexable,
  30. size_t DimensionIndex>
  31. struct sum_for_indexable_dimension<Point, BoxIndexable, box_tag, comparable_distance_near_tag, DimensionIndex>
  32. {
  33. typedef typename geometry::default_comparable_distance_result<Point, BoxIndexable>::type result_type;
  34. inline static result_type apply(Point const& pt, BoxIndexable const& i)
  35. {
  36. typedef typename coordinate_type<Point>::type point_coord_t;
  37. typedef typename coordinate_type<BoxIndexable>::type indexable_coord_t;
  38. point_coord_t pt_c = geometry::get<DimensionIndex>(pt);
  39. indexable_coord_t ind_c_min = geometry::get<geometry::min_corner, DimensionIndex>(i);
  40. indexable_coord_t ind_c_max = geometry::get<geometry::max_corner, DimensionIndex>(i);
  41. result_type diff = 0;
  42. if ( pt_c < ind_c_min )
  43. diff = ind_c_min - pt_c;
  44. else if ( ind_c_max < pt_c )
  45. diff = pt_c - ind_c_max;
  46. return diff * diff;
  47. }
  48. };
  49. template <typename Point, typename Indexable>
  50. typename geometry::default_comparable_distance_result<Point, Indexable>::type
  51. comparable_distance_near(Point const& pt, Indexable const& i)
  52. {
  53. return detail::sum_for_indexable<
  54. Point,
  55. Indexable,
  56. typename tag<Indexable>::type,
  57. detail::comparable_distance_near_tag,
  58. dimension<Indexable>::value
  59. >::apply(pt, i);
  60. }
  61. }}}} // namespace boost::geometry::index::detail
  62. #endif // BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_COMPARABLE_DISTANCE_NEAR_HPP