comparable_distance_centroid.hpp 2.7 KB

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