Distance.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libbasicvector - A simple vector library
  4. * See file License for license information.
  5. */
  6. #ifndef __VECTOR_DISTANCE_H__
  7. #define __VECTOR_DISTANCE_H__
  8. #include "core/vector/ippwrapper.h"
  9. #include "core/vector/VectorT.h"
  10. namespace NICE {
  11. /**
  12. * @struct VectorDistance
  13. * @brief baseclass for vector distances
  14. */
  15. template<class T>
  16. class VectorDistance {
  17. public:
  18. virtual ~VectorDistance(){};
  19. inline T calculate(const VectorT<T>& v1 , const VectorT<T>& v2) const;
  20. inline T operator()(const VectorT<T>& v1 , const VectorT<T>& v2) const;
  21. protected:
  22. virtual T doCalculate(const VectorT<T>& v1 , const VectorT<T>& v2) const = 0;
  23. };
  24. /**
  25. * @struct ManhattanDistance
  26. * @brief calculate the Manhattan Distance (L1 Norm)
  27. */
  28. template<class T>
  29. class ManhattanDistance : public VectorDistance<T> {
  30. public:
  31. virtual ~ManhattanDistance(){};
  32. protected:
  33. T doCalculate(const VectorT<T>& v1, const VectorT<T>& v2) const;
  34. };
  35. /**
  36. * @struct EuclidianDistance
  37. * @brief calculate the Euclidean Distance (L2 Norm)
  38. */
  39. template<class T>
  40. class EuclidianDistance : public VectorDistance<T> {
  41. public:
  42. virtual ~EuclidianDistance(){};
  43. protected:
  44. T doCalculate(const VectorT<T>& v1, const VectorT<T>& v2) const;
  45. };
  46. template<>
  47. Ipp32f EuclidianDistance<Ipp32f>::doCalculate(const VectorT<Ipp32f>& v1, const VectorT<Ipp32f>& v2) const;
  48. /**
  49. * @struct MahalanobisDistance
  50. * @brief calculate the Mahalanobis Distance
  51. */
  52. template<class T>
  53. class MahalanobisDistance : public VectorDistance<T>
  54. {
  55. public:
  56. //! @param matrix the mahalanobis matrix
  57. MahalanobisDistance (const MatrixT<T>& matrix) : m_matrix(matrix) {};
  58. virtual ~MahalanobisDistance() {};
  59. protected:
  60. T doCalculate( const VectorT<T>& v1, const VectorT<T>& v2 ) const;
  61. private:
  62. MatrixT<T> m_matrix;
  63. };
  64. /**
  65. * @struct MaximumDistance
  66. * @brief calculate the Maximum Distance (L-inf Norm)
  67. */
  68. template<class T>
  69. class MaximumDistance : public VectorDistance<T> {
  70. public:
  71. virtual ~MaximumDistance(){};
  72. protected:
  73. T doCalculate(const VectorT<T>& v1, const VectorT<T>& v2) const;
  74. };
  75. /**
  76. * @struct MedianDistance
  77. * @brief calculate the Median of the absolute distances
  78. */
  79. template<class T>
  80. class MedianDistance : public VectorDistance<T> {
  81. public:
  82. virtual ~MedianDistance(){};
  83. protected:
  84. T doCalculate(const VectorT<T>& v1, const VectorT<T>& v2) const;
  85. };
  86. /**
  87. * @struct Chi2Distance
  88. * @brief calculate the chi^2 distance
  89. */
  90. template<class T>
  91. class Chi2Distance : public VectorDistance<T> {
  92. public:
  93. virtual ~Chi2Distance(){};
  94. protected:
  95. T doCalculate(const VectorT<T>& v1, const VectorT<T>& v2) const;
  96. };
  97. /**
  98. * @struct SphericalDistance
  99. * @brief calculate the distance between two sphereical angles
  100. * @note first parameter is the longitude and the second the latitude of the sphere
  101. */
  102. template<class T>
  103. class SphericalDistance : public VectorDistance<T> {
  104. public:
  105. virtual ~SphericalDistance(){};
  106. protected:
  107. T doCalculate(const VectorT<T>& v1, const VectorT<T>& v2) const;
  108. };
  109. /**
  110. * @struct SinDistance
  111. * @brief calculate the sinus of the in-between angle
  112. */
  113. template<class T>
  114. class SinDistance : public VectorDistance<T> {
  115. public:
  116. virtual ~SinDistance(){};
  117. protected:
  118. T doCalculate(const VectorT<T>& v1, const VectorT<T>& v2) const;
  119. };
  120. /**
  121. * @struct CosDistance
  122. * @brief calculates the cosinus distance
  123. */
  124. template<class T>
  125. class CosDistance : public VectorDistance<T> {
  126. public:
  127. virtual ~CosDistance(){};
  128. protected:
  129. T doCalculate(const VectorT<T>& v1, const VectorT<T>& v2) const;
  130. };
  131. /**
  132. * @struct KLDistance
  133. * @brief calculate the Kullback Leibler distance
  134. * @note value of the i-th entry of \c v1 and \c v2 is only calculated
  135. * if v1[i]>0 and v2[i]>0
  136. */
  137. template<class T>
  138. class KLDistance : public VectorDistance<T> {
  139. public:
  140. virtual ~KLDistance(){};
  141. protected:
  142. T doCalculate(const VectorT<T>& v1, const VectorT<T>& v2) const;
  143. };
  144. /**
  145. * @struct SwappedKLDistance
  146. * @brief calculate the swapped Kullback Leibler distance
  147. */
  148. template<class T>
  149. class SwappedKLDistance : public VectorDistance<T> {
  150. public:
  151. SwappedKLDistance();
  152. ~SwappedKLDistance();
  153. protected:
  154. T doCalculate(const VectorT<T>& v1, const VectorT<T>& v2) const;
  155. private:
  156. KLDistance<T>* pKLDistance;
  157. };
  158. /**
  159. * @struct ExtendedKLDistance
  160. * @brief calculate the extended Kullback Leibler distance
  161. */
  162. template<class T>
  163. class ExtendedKLDistance : public VectorDistance<T> {
  164. public:
  165. ExtendedKLDistance();
  166. ~ExtendedKLDistance();
  167. protected:
  168. T doCalculate(const VectorT<T>& v1, const VectorT<T>& v2) const;
  169. private:
  170. KLDistance<T>* pKLDistance;
  171. };
  172. /**
  173. * @struct BhattacharyyaDistance
  174. * @brief calculate the Bhattacharyya distance
  175. */
  176. template<class T>
  177. class BhattacharyyaDistance : public VectorDistance<T> {
  178. public:
  179. virtual ~BhattacharyyaDistance(){};
  180. protected:
  181. T doCalculate(const VectorT<T>& v1, const VectorT<T>& v2) const;
  182. };
  183. template<>
  184. Ipp32f BhattacharyyaDistance<Ipp32f>::doCalculate(const VectorT<Ipp32f>& v1, const VectorT<Ipp32f>& v2) const;
  185. /**
  186. * @struct ScalarProductDistance
  187. * @brief calculate the scalar product distance
  188. */
  189. template<class T>
  190. class ScalarProductDistance : public VectorDistance<T> {
  191. public:
  192. virtual ~ScalarProductDistance(){};
  193. protected:
  194. T doCalculate(const VectorT<T>& v1, const VectorT<T>& v2) const;
  195. };
  196. } // namespace NICE
  197. //#ifdef __GNUC__
  198. #include <core/vector/Distance.tcc>
  199. //#endif
  200. #endif // __VECTOR_DISTANCE_H__