Spatial C++ Library
Generic Multi-Dimensional Containers and Spatial Operations
metric.hpp
1 // -*- C++ -*-
2 //
3 // Copyright Sylvain Bougerel 2009 - 2013.
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file COPYING or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 
16 #ifndef SPATIAL_METRIC_HPP
17 #define SPATIAL_METRIC_HPP
18 
19 #include "function.hpp"
20 #include "traits.hpp"
21 #include "bits/spatial_math.hpp"
22 #include "bits/spatial_builtin.hpp"
23 
24 namespace spatial
25 {
41  template<typename Ct, typename DistanceType, typename Diff>
42  class euclidian : Diff
43  {
44  // Check that DistanceType is a fundamental floating point type
47 
48  public:
53  typedef typename details::rebind_builtin_difference
54  <Diff, DistanceType>::type difference_type;
55 
56  private:
61 
62  public:
66  typedef DistanceType distance_type;
67 
69  explicit euclidian(const difference_type& diff = Diff()) : Diff(diff) { }
70 
72  template <typename AnyDistanceType>
74  : Diff(other.difference()) { }
75 
80  distance_type
82  const key_type& origin, const key_type& key) const
83  {
85  <key_type, difference_type, DistanceType>(rank, origin, key,
86  difference());
87  }
88 
99  distance_type
101  const key_type& origin, const key_type& key) const
102  {
104  <key_type, difference_type, DistanceType>(dim, origin, key,
105  difference());
106  }
107 
111  difference_type difference() const
112  { return *static_cast<const Diff*>(this); }
113  };
114 
137  template<typename Ct, typename DistanceType, typename Diff>
138  class quadrance : Diff
139  {
140  // Check that DistanceType is a fundamental floating point type
141  typedef typename enable_if<import::is_arithmetic<DistanceType> >::type
143 
144  public:
149  typedef typename details::rebind_builtin_difference
150  <Diff, DistanceType>::type difference_type;
151 
152  private:
157 
158  public:
159  typedef DistanceType distance_type;
160 
162  explicit quadrance(const difference_type& diff = Diff()) : Diff(diff) { }
163 
165  template <typename AnyDistanceType>
167  : Diff(other.difference()) { }
168 
173  distance_type
175  const key_type& origin, const key_type& key) const
176  {
178  <key_type, difference_type, DistanceType>(rank, origin, key,
179  difference());
180  }
181 
188  distance_type
190  const key_type& origin, const key_type& key) const
191  {
193  <key_type, difference_type, DistanceType>(dim, origin, key,
194  difference());
195  }
196 
200  difference_type difference() const
201  { return *static_cast<const Diff*>(this); }
202  };
203 
230  template<typename Ct, typename DistanceType, typename Diff>
231  class manhattan : Diff
232  {
233  // Check that DistanceType is a fundamental floating point type
234  typedef typename enable_if<import::is_arithmetic<DistanceType> >::type
236 
237  public:
242  typedef typename details::rebind_builtin_difference
243  <Diff, DistanceType>::type difference_type;
244 
245  private:
250 
251  public:
252  typedef DistanceType distance_type;
253 
255  explicit manhattan(const difference_type& diff = Diff()) : Diff(diff) { }
256 
258  template <typename AnyDistanceType>
260  : Diff(other.difference()) { }
261 
266  distance_type
268  const key_type& origin, const key_type& key) const
269  {
271  <key_type, difference_type, DistanceType>(rank, origin, key,
272  difference());
273  }
274 
282  distance_type
284  const key_type& origin, const key_type& key) const
285  {
287  <key_type, difference_type, DistanceType>(dim, origin, key,
288  difference());
289  }
290 
294  difference_type difference() const
295  { return *static_cast<const Diff*>(this); }
296  };
297 
298 } // namespace spatial
299 
300 #endif // SPATIAL_METRIC_HPP
details::rebind_builtin_difference< Diff, DistanceType >::type difference_type
The type used to compute the difference between 2 keys along the same dimension.
Definition: metric.hpp:54
euclidian(const euclidian< Ct, AnyDistanceType, Diff > &other)
Copy the metric from another metric with any DistanceType.
Definition: metric.hpp:73
difference_type difference() const
Returns the difference functor used in this type.
Definition: metric.hpp:294
distance_type distance_to_key(dimension_type rank, const key_type &origin, const key_type &key) const
Compute the distance between the point of origin and the key.
Definition: metric.hpp:174
manhattan(const difference_type &diff=Diff())
A constructor that allows you to specify the Difference type.
Definition: metric.hpp:255
enable_if< import::is_arithmetic< Unit >, Unit >::type square_euclid_distance_to_key(dimension_type rank, const Key &origin, const Key &key, Difference diff)
Compute the square value of the distance between origin and key.
DistanceType distance_type
Definition: metric.hpp:252
quadrance(const quadrance< Ct, AnyDistanceType, Diff > &other)
Copy the metric from another metric with any DistanceType.
Definition: metric.hpp:166
enable_if< import::is_floating_point< Unit >, Unit >::type euclid_distance_to_key(dimension_type rank, const Key &origin, const Key &key, Difference diff)
Computes the euclidian distance between 2 points.
Defines a metric in the Euclidian space where only the square of the distances are being computed int...
Definition: metric.hpp:138
enable_if< import::is_arithmetic< DistanceType > >::type check_concept_distance_type_is_arithmetic
Definition: metric.hpp:142
enable_if< import::is_arithmetic< DistanceType > >::type check_concept_distance_type_is_arithmetic
Definition: metric.hpp:235
difference_type difference() const
Returns the difference functor used in this type.
Definition: metric.hpp:200
container_traits< Ct >::key_type key_type
The key_type of the container being used for calculations.
Definition: metric.hpp:60
euclidian(const difference_type &diff=Diff())
The constructors allows you to specify a custom difference type.
Definition: metric.hpp:69
quadrance(const difference_type &diff=Diff())
The constructor allows you to specify a custom difference type.
Definition: metric.hpp:162
distance_type distance_to_key(dimension_type rank, const key_type &origin, const key_type &key) const
Compute the distance between the point of origin and the key.
Definition: metric.hpp:81
enable_if< import::is_arithmetic< Unit >, Unit >::type manhattan_distance_to_key(dimension_type rank, const Key &origin, const Key &key, Difference diff)
Compute the manhattan distance between origin and key.
container_traits< Ct >::key_type key_type
The key_type of the container being used for calculations.
Definition: metric.hpp:156
distance_type distance_to_plane(dimension_type, dimension_type dim, const key_type &origin, const key_type &key) const
The distance between the point of origin and the closest point to the plane orthogonal to the axis of...
Definition: metric.hpp:100
details::rebind_builtin_difference< Diff, DistanceType >::type difference_type
The type used to compute the difference between 2 keys along the same dimension.
Definition: metric.hpp:243
The traits type for all containers in the spatial namespace.
Definition: traits.hpp:42
std::size_t dimension_type
Defines the type for the dimension as being a size.
Definition: spatial.hpp:71
If Diff is a builtin difference type, change the current unit of Diff to the DistanceType specified i...
manhattan(const manhattan< Ct, AnyDistanceType, Diff > &other)
Copy the metric from another metric with any DistanceType.
Definition: metric.hpp:259
container_traits< Ct >::key_type key_type
The key_type of the container being used for calculations.
Definition: metric.hpp:249
Defines a metric working on the Euclidian space where distances are expressed in one of C++'s floatin...
Definition: metric.hpp:42
details::rebind_builtin_difference< Diff, DistanceType >::type difference_type
The type used to compute the difference between 2 keys along the same dimension.
Definition: metric.hpp:150
The main namespace used in the library.
Definition: algorithm.hpp:23
Defines a metric for the a space where distances are the sum of all the elements of the vector...
Definition: metric.hpp:231
enable_if< import::is_floating_point< Unit >, Unit >::type euclid_distance_to_plane(dimension_type dim, const Key &origin, const Key &key, Difference diff)
Compute the distance between the origin and the closest point to the plane orthogonal to the axis of ...
distance_type distance_to_key(dimension_type rank, const key_type &origin, const key_type &key) const
Compute the distance between the point of origin and the key.
Definition: metric.hpp:267
DistanceType distance_type
The distance type being used for distance calculations.
Definition: metric.hpp:66
enable_if< import::is_arithmetic< Unit >, Unit >::type square_euclid_distance_to_plane(dimension_type dim, const Key &origin, const Key &key, Difference diff)
Compute the distance between the origin and the closest point to the plane orthogonal to the axis of ...
enable_if< import::is_floating_point< DistanceType > >::type check_concept_distance_type_is_floating_point
Definition: metric.hpp:46
distance_type distance_to_plane(dimension_type, dimension_type dim, const key_type &origin, const key_type &key) const
The distance between the point of origin and the closest point to the plane orthogonal to the axis of...
Definition: metric.hpp:283
DistanceType distance_type
Definition: metric.hpp:159
distance_type distance_to_plane(dimension_type, dimension_type dim, const key_type &origin, const key_type &key) const
The distance between the point of origin and the closest point to the plane orthogonal to the axis of...
Definition: metric.hpp:189
difference_type difference() const
Returns the difference functor used in this type.
Definition: metric.hpp:111
enable_if< import::is_arithmetic< Unit >, Unit >::type manhattan_distance_to_plane(dimension_type dim, const Key &origin, const Key &key, Difference diff)
Compute the distance between the origin and the closest point to the plane orthogonal to the axis of ...