grid.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 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 "grid.h"
  9. #include "PlainVector.h"
  10. #include <cassert>
  11. template <
  12. typename Derivedres,
  13. typename DerivedGV>
  14. IGL_INLINE void igl::grid(
  15. const Eigen::MatrixBase<Derivedres> & res,
  16. Eigen::PlainObjectBase<DerivedGV> & GV)
  17. {
  18. typedef typename DerivedGV::Scalar Scalar;
  19. GV.resize(res.array().prod(),res.size());
  20. const auto lerp =
  21. [&res](const Scalar di, const int d)->Scalar{return di/(Scalar)(res(d)-1);};
  22. PlainVector<Derivedres> sub;
  23. sub.resizeLike(res);
  24. sub.setConstant(0);
  25. for(int gi = 0;gi<GV.rows();gi++)
  26. {
  27. // omg, I'm implementing addition...
  28. for(int c = 0;c<res.size()-1;c++)
  29. {
  30. if(sub(c)>=res(c))
  31. {
  32. sub(c) = 0;
  33. // roll over
  34. sub(c+1)++;
  35. }
  36. }
  37. for(int c = 0;c<res.size();c++)
  38. {
  39. GV(gi,c) = lerp(sub(c),c);
  40. }
  41. sub(0)++;
  42. }
  43. }
  44. #ifdef IGL_STATIC_LIBRARY
  45. // Explicit template instantiation
  46. template void igl::grid<Eigen::Matrix<int, 1, 3, 1, 1, 3>, Eigen::Matrix<double, -1, 3, 0, -1, 3>>(Eigen::MatrixBase<Eigen::Matrix<int, 1, 3, 1, 1, 3>> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3>>&);
  47. // generated by autoexplicit.sh
  48. template void igl::grid<Eigen::Matrix<int, 1, 3, 1, 1, 3>, Eigen::Matrix<double, -1, 3, 1, -1, 3> >(Eigen::MatrixBase<Eigen::Matrix<int, 1, 3, 1, 1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> >&);
  49. // generated by autoexplicit.sh
  50. template void igl::grid<Eigen::Matrix<int, 3, 1, 0, 3, 1>, Eigen::Matrix<float, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<int, 3, 1, 0, 3, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >&);
  51. // generated by autoexplicit.sh
  52. template void igl::grid<Eigen::Matrix<int, 2, 1, 0, 2, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<int, 2, 1, 0, 2, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  53. // generated by autoexplicit.sh
  54. template void igl::grid<Eigen::Matrix<int, 1, 3, 1, 1, 3>, Eigen::Matrix<float, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<int, 1, 3, 1, 1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >&);
  55. // generated by autoexplicit.sh
  56. template void igl::grid<Eigen::Matrix<int, 3, 1, 0, 3, 1>, Eigen::Matrix<float, -1, 3, 0, -1, 3> >(Eigen::MatrixBase<Eigen::Matrix<int, 3, 1, 0, 3, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 0, -1, 3> >&);
  57. // generated by autoexplicit.sh
  58. template void igl::grid<Eigen::Matrix<int, 3, 1, 0, 3, 1>, Eigen::Matrix<float, -1, 3, 1, -1, 3> >(Eigen::MatrixBase<Eigen::Matrix<int, 3, 1, 0, 3, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> >&);
  59. // generated by autoexplicit.sh
  60. template void igl::grid<Eigen::Matrix<int, 3, 1, 0, 3, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<int, 3, 1, 0, 3, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  61. template void igl::grid<Eigen::Matrix<int, 1, 3, 1, 1, 3>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<int, 1, 3, 1, 1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  62. #endif