snap_points.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "snap_points.h"
  9. #include <cassert>
  10. #include <limits>
  11. template <
  12. typename DerivedC,
  13. typename DerivedV,
  14. typename DerivedI,
  15. typename DerivedminD,
  16. typename DerivedVI>
  17. IGL_INLINE void igl::snap_points(
  18. const Eigen::MatrixBase<DerivedC > & C,
  19. const Eigen::MatrixBase<DerivedV > & V,
  20. Eigen::PlainObjectBase<DerivedI > & I,
  21. Eigen::PlainObjectBase<DerivedminD > & minD,
  22. Eigen::PlainObjectBase<DerivedVI > & VI)
  23. {
  24. snap_points(C,V,I,minD);
  25. const int m = C.rows();
  26. VI.resize(m,V.cols());
  27. for(int c = 0;c<m;c++)
  28. {
  29. VI.row(c) = V.row(I(c));
  30. }
  31. }
  32. template <
  33. typename DerivedC,
  34. typename DerivedV,
  35. typename DerivedI,
  36. typename DerivedminD>
  37. IGL_INLINE void igl::snap_points(
  38. const Eigen::MatrixBase<DerivedC > & C,
  39. const Eigen::MatrixBase<DerivedV > & V,
  40. Eigen::PlainObjectBase<DerivedI > & I,
  41. Eigen::PlainObjectBase<DerivedminD > & minD)
  42. {
  43. const int n = V.rows();
  44. const int m = C.rows();
  45. assert(V.cols() == C.cols() && "Dimensions should match");
  46. // O(m*n)
  47. //
  48. // I believe there should be a way to do this in O(m*log(n) + n) assuming
  49. // reasonably distubed points.
  50. I.resize(m,1);
  51. typedef typename DerivedV::Scalar Scalar;
  52. minD.setConstant(m,1,std::numeric_limits<Scalar>::max());
  53. for(int v = 0;v<n;v++)
  54. {
  55. for(int c = 0;c<m;c++)
  56. {
  57. const Scalar d = (C.row(c) - V.row(v)).squaredNorm();
  58. if(d < minD(c))
  59. {
  60. minD(c,0) = d;
  61. I(c,0) = v;
  62. }
  63. }
  64. }
  65. }
  66. template <
  67. typename DerivedC,
  68. typename DerivedV,
  69. typename DerivedI>
  70. IGL_INLINE void igl::snap_points(
  71. const Eigen::MatrixBase<DerivedC > & C,
  72. const Eigen::MatrixBase<DerivedV > & V,
  73. Eigen::PlainObjectBase<DerivedI > & I)
  74. {
  75. Eigen::Matrix<typename DerivedC::Scalar,DerivedC::RowsAtCompileTime,1> minD;
  76. return igl::snap_points(C,V,I,minD);
  77. }
  78. #ifdef IGL_STATIC_LIBRARY
  79. // Explicit template instantiation
  80. template void igl::snap_points<Eigen::Matrix<double, 1, 3, 1, 1, 3>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  81. template void igl::snap_points<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  82. template void igl::snap_points<Eigen::Matrix<double, 1, 3, 1, 1, 3>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  83. #endif