Distance.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <core/vector/Distance.h>
  2. namespace NICE {
  3. template<>
  4. Ipp32f EuclidianDistance<Ipp32f>::doCalculate(const VectorT<Ipp32f>& v1, const VectorT<Ipp32f>& v2) const {
  5. if(v1.size()!=v2.size())
  6. fthrow(Exception, "Input vectors must have the same size.");
  7. Ipp32f dist = 0;
  8. #ifdef NICE_USELIB_IPP
  9. VectorT<Ipp32f> res(v1.size());
  10. ippsSub_32f(v1.getDataPointer(), v2.getDataPointer(), res.getDataPointer(), v1.size());
  11. ippsSqr_32f(res.getDataPointer(), res.getDataPointer(), res.size());
  12. dist = res.Sum();
  13. #else // NICE_USELIB_IPP
  14. const Ipp32f* pSrc1 = v1.getDataPointer();
  15. const Ipp32f* pSrc2 = v2.getDataPointer();
  16. for(Ipp32u i=0; i<v1.size(); ++i,++pSrc1,++pSrc2)
  17. dist += (*pSrc1-*pSrc2)*(*pSrc1-*pSrc2);
  18. #endif // NICE_USELIB_IPP
  19. dist = std::sqrt(dist);
  20. return dist;
  21. }
  22. template<>
  23. Ipp32f BhattacharyyaDistance<Ipp32f>::doCalculate(const VectorT<Ipp32f>& v1, const VectorT<Ipp32f>& v2) const {
  24. if(v1.size()!=v2.size())
  25. fthrow(Exception, "Input vectors must have the same size.");
  26. Ipp32f B;
  27. #ifdef NICE_USELIB_IPP
  28. VectorT<Ipp32f> v1f(v1);
  29. v1f *= v2;
  30. ippsSqrt(v1f.getDataPointer(), v1f.getDataPointer(), v1f.size());
  31. ippsSum(v1f.getDataPointer(), v1f.size(), &B);
  32. #else // NICE_USELIB_IPP
  33. B = 0.0;
  34. for(uint i=0; i<v1.size(); ++i)
  35. B += std::sqrt(v1[i]*v2[i]);
  36. #endif // NICE_USELIB_IPP
  37. return std::sqrt(1-B);
  38. }
  39. }