snap_points.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  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::PlainObjectBase<DerivedC > & C,
  19. const Eigen::PlainObjectBase<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::PlainObjectBase<DerivedC > & C,
  39. const Eigen::PlainObjectBase<DerivedV > & V,
  40. Eigen::PlainObjectBase<DerivedI > & I,
  41. Eigen::PlainObjectBase<DerivedminD > & minD)
  42. {
  43. using namespace std;
  44. const int n = V.rows();
  45. const int m = C.rows();
  46. assert(V.cols() == C.cols() && "Dimensions should match");
  47. // O(m*n)
  48. //
  49. // I believe there should be a way to do this in O(m*log(n) + n) assuming
  50. // reasonably distubed points.
  51. I.resize(m,1);
  52. typedef typename DerivedV::Scalar Scalar;
  53. minD.setConstant(m,1,numeric_limits<Scalar>::max());
  54. for(int v = 0;v<n;v++)
  55. {
  56. for(int c = 0;c<m;c++)
  57. {
  58. const Scalar d = (C.row(c) - V.row(v)).squaredNorm();
  59. if(d < minD(c))
  60. {
  61. minD(c,0) = d;
  62. I(c,0) = v;
  63. }
  64. }
  65. }
  66. }
  67. template <
  68. typename DerivedC,
  69. typename DerivedV,
  70. typename DerivedI>
  71. IGL_INLINE void igl::snap_points(
  72. const Eigen::PlainObjectBase<DerivedC > & C,
  73. const Eigen::PlainObjectBase<DerivedV > & V,
  74. Eigen::PlainObjectBase<DerivedI > & I)
  75. {
  76. Eigen::Matrix<typename DerivedC::Scalar,DerivedC::RowsAtCompileTime,1> minD;
  77. return igl::snap_points(C,V,I,minD);
  78. }
  79. #ifdef IGL_STATIC_LIBRARY
  80. // Explicit template specialization
  81. 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::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, Eigen::PlainObjectBase<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> >&);
  82. 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::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  83. #endif