comparable_distance_centroid.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Boost.Geometry Index
  2. //
  3. // squared distance between point and centroid 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_CENTROID_HPP
  15. #define BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_COMPARABLE_DISTANCE_CENTROID_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. #include <boost/geometry/index/detail/algorithms/diff_abs.hpp>
  20. namespace boost { namespace geometry { namespace index { namespace detail {
  21. struct comparable_distance_centroid_tag {};
  22. template <
  23. typename Point,
  24. typename PointIndexable,
  25. size_t N>
  26. struct sum_for_indexable<Point, PointIndexable, point_tag, comparable_distance_centroid_tag, N>
  27. {
  28. typedef typename geometry::default_comparable_distance_result<Point, PointIndexable>::type result_type;
  29. inline static result_type apply(Point const& pt, PointIndexable const& i)
  30. {
  31. return geometry::comparable_distance(pt, i);
  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_centroid_tag, DimensionIndex>
  39. {
  40. typedef typename geometry::default_comparable_distance_result<Point, BoxIndexable>::type result_type;
  41. inline static result_type apply(Point const& pt, BoxIndexable const& i)
  42. {
  43. typedef typename coordinate_type<Point>::type point_coord_t;
  44. typedef typename coordinate_type<BoxIndexable>::type indexable_coord_t;
  45. point_coord_t pt_c = geometry::get<DimensionIndex>(pt);
  46. indexable_coord_t ind_c_min = geometry::get<geometry::min_corner, DimensionIndex>(i);
  47. indexable_coord_t ind_c_max = geometry::get<geometry::max_corner, DimensionIndex>(i);
  48. indexable_coord_t ind_c_avg = ind_c_min + (ind_c_max - ind_c_min) / 2;
  49. // TODO: awulkiew - is (ind_c_min + ind_c_max) / 2 safe?
  50. result_type diff = detail::diff_abs(ind_c_avg, pt_c);
  51. return diff * diff;
  52. }
  53. };
  54. template <typename Point, typename Indexable>
  55. typename geometry::default_comparable_distance_result<Point, Indexable>::type
  56. comparable_distance_centroid(Point const& pt, Indexable const& i)
  57. {
  58. return detail::sum_for_indexable<
  59. Point,
  60. Indexable,
  61. typename tag<Indexable>::type,
  62. detail::comparable_distance_centroid_tag,
  63. dimension<Indexable>::value
  64. >::apply(pt, i);
  65. }
  66. }}}} // namespace boost::geometry::index::detail
  67. #endif // #define BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_COMPARABLE_DISTANCE_CENTROID_HPP