all_pairs_distances.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #ifndef IGL_ALL_PAIRS_DISTANCES_H
  2. #define IGL_ALL_PAIRS_DISTANCES_H
  3. namespace igl
  4. {
  5. // ALL_PAIRS_DISTANCES compute distances between each point i in V and point j
  6. // in U
  7. //
  8. // D = all_pairs_distances(V,U)
  9. //
  10. // Templates:
  11. // Mat matrix class like MatrixXd
  12. // Inputs:
  13. // V #V by dim list of points
  14. // U #U by dim list of points
  15. // squared whether to return squared distances
  16. // Outputs:
  17. // D #V by #U matrix of distances, where D(i,j) gives the distance or
  18. // squareed distance between V(i,:) and U(j,:)
  19. //
  20. template <typename Mat>
  21. inline void all_pairs_distances(
  22. const Mat & V,
  23. const Mat & U,
  24. const bool squared,
  25. Mat & D);
  26. }
  27. // Implementation
  28. template <typename Mat>
  29. inline void igl::all_pairs_distances(
  30. const Mat & V,
  31. const Mat & U,
  32. const bool squared,
  33. Mat & D)
  34. {
  35. // dimension should be the same
  36. assert(V.cols() == U.cols());
  37. // resize output
  38. D.resize(V.rows(),U.rows());
  39. for(int i = 0;i<V.rows();i++)
  40. {
  41. for(int j=0;j<U.rows();j++)
  42. {
  43. D(i,j) = (V.row(i)-U.row(j)).array().pow(2).sum();
  44. if(!squared)
  45. {
  46. D(i,j) = sqrt(D(i,j));
  47. }
  48. }
  49. }
  50. }
  51. #endif