comparable_distance_near.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // This file was modified by Oracle on 2021.
  8. // Modifications copyright (c) 2021 Oracle and/or its affiliates.
  9. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  10. //
  11. // Use, modification and distribution is subject to the Boost Software License,
  12. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_COMPARABLE_DISTANCE_NEAR_HPP
  15. #define BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_COMPARABLE_DISTANCE_NEAR_HPP
  16. #include <boost/geometry/algorithms/detail/comparable_distance/interface.hpp>
  17. #include <boost/geometry/core/access.hpp>
  18. #include <boost/geometry/index/detail/algorithms/sum_for_indexable.hpp>
  19. namespace boost { namespace geometry { namespace index { namespace detail {
  20. struct comparable_distance_near_tag {};
  21. template <
  22. typename Point,
  23. typename PointIndexable,
  24. size_t N>
  25. struct sum_for_indexable<Point, PointIndexable, point_tag, comparable_distance_near_tag, N>
  26. {
  27. typedef typename geometry::default_comparable_distance_result<Point, PointIndexable>::type result_type;
  28. inline static result_type apply(Point const& pt, PointIndexable const& i)
  29. {
  30. return geometry::comparable_distance(pt, i);
  31. }
  32. };
  33. template <
  34. typename Point,
  35. typename BoxIndexable,
  36. size_t DimensionIndex>
  37. struct sum_for_indexable_dimension<Point, BoxIndexable, box_tag, comparable_distance_near_tag, DimensionIndex>
  38. {
  39. typedef typename geometry::default_comparable_distance_result<Point, BoxIndexable>::type result_type;
  40. inline static result_type apply(Point const& pt, BoxIndexable const& i)
  41. {
  42. typedef typename coordinate_type<Point>::type point_coord_t;
  43. typedef typename coordinate_type<BoxIndexable>::type indexable_coord_t;
  44. point_coord_t pt_c = geometry::get<DimensionIndex>(pt);
  45. indexable_coord_t ind_c_min = geometry::get<geometry::min_corner, DimensionIndex>(i);
  46. indexable_coord_t ind_c_max = geometry::get<geometry::max_corner, DimensionIndex>(i);
  47. result_type diff = 0;
  48. if ( pt_c < ind_c_min )
  49. diff = ind_c_min - pt_c;
  50. else if ( ind_c_max < pt_c )
  51. diff = pt_c - ind_c_max;
  52. return diff * diff;
  53. }
  54. };
  55. template <typename Point, typename Indexable>
  56. typename geometry::default_comparable_distance_result<Point, Indexable>::type
  57. comparable_distance_near(Point const& pt, Indexable const& i)
  58. {
  59. return detail::sum_for_indexable<
  60. Point,
  61. Indexable,
  62. typename tag<Indexable>::type,
  63. detail::comparable_distance_near_tag,
  64. dimension<Indexable>::value
  65. >::apply(pt, i);
  66. }
  67. }}}} // namespace boost::geometry::index::detail
  68. #endif // BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_COMPARABLE_DISTANCE_NEAR_HPP