voxel_grid.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 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 "voxel_grid.h"
  9. #include "grid.h"
  10. template <
  11. typename Scalar,
  12. typename DerivedGV,
  13. typename Derivedside>
  14. IGL_INLINE void igl::voxel_grid(
  15. const Eigen::AlignedBox<Scalar,3> & box,
  16. const int in_s,
  17. const int pad_count,
  18. Eigen::PlainObjectBase<DerivedGV> & GV,
  19. Eigen::PlainObjectBase<Derivedside> & side)
  20. {
  21. using namespace Eigen;
  22. using namespace std;
  23. typename DerivedGV::Index si = -1;
  24. box.diagonal().maxCoeff(&si);
  25. //DerivedGV::Index si = 0;
  26. //assert(si>=0);
  27. const Scalar s_len = box.diagonal()(si);
  28. assert(in_s>(pad_count*2+1) && "s should be > 2*pad_count+1");
  29. const Scalar s = in_s - 2*pad_count;
  30. side(si) = s;
  31. for(int i = 0;i<3;i++)
  32. {
  33. if(i!=si)
  34. {
  35. side(i) = std::ceil(s * (box.max()(i)-box.min()(i))/s_len);
  36. }
  37. }
  38. side.array() += 2*pad_count;
  39. grid(side,GV);
  40. // A * p/s + B = min
  41. // A * (1-p/s) + B = max
  42. // B = min - A * p/s
  43. // A * (1-p/s) + min - A * p/s = max
  44. // A * (1-p/s) - A * p/s = max-min
  45. // A * (1-2p/s) = max-min
  46. // A = (max-min)/(1-2p/s)
  47. const Array<Scalar,3,1> ps=
  48. (Scalar)(pad_count)/(side.transpose().template cast<Scalar>().array()-1.);
  49. const Array<Scalar,3,1> A = box.diagonal().array()/(1.0-2.*ps);
  50. //// This would result in an "anamorphic", but perfectly fit grid:
  51. //const Array<Scalar,3,1> B = box.min().array() - A.array()*ps;
  52. //GV.array().rowwise() *= A.transpose();
  53. //GV.array().rowwise() += B.transpose();
  54. // Instead scale by largest factor and move to match center
  55. typename Array<Scalar,3,1>::Index ai = -1;
  56. Scalar a = A.maxCoeff(&ai);
  57. const Array<Scalar,1,3> ratio =
  58. a*(side.template cast<Scalar>().array()-1.0)/(Scalar)(side(ai)-1.0);
  59. GV.array().rowwise() *= ratio;
  60. const Eigen::Matrix<Scalar,1,3> offset = (box.center().transpose()-GV.colwise().mean()).eval();
  61. GV.rowwise() += offset;
  62. }
  63. #ifdef IGL_STATIC_LIBRARY
  64. // Explicit template instantiation
  65. // generated by autoexplicit.sh
  66. template void igl::voxel_grid<float, Eigen::Matrix<float, -1, -1, 0, -1, -1>, Eigen::Matrix<int, 1, 3, 1, 1, 3> >(Eigen::AlignedBox<float, 3> const&, int, int, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, 1, 3, 1, 1, 3> >&);
  67. template void igl::voxel_grid<float, Eigen::Matrix<float, -1, 3, 0, -1, 3>, Eigen::Matrix<int, 3, 1, 0, 3, 1> >(Eigen::AlignedBox<float, 3> const&, int, int, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, 3, 1, 0, 3, 1> >&);
  68. template void igl::voxel_grid<float, Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<int, 3, 1, 0, 3, 1> >(Eigen::AlignedBox<float, 3> const&, int, int, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, 3, 1, 0, 3, 1> >&);
  69. template void igl::voxel_grid<double, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, 3, 1, 0, 3, 1> >(Eigen::AlignedBox<double, 3> const&, int, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, 3, 1, 0, 3, 1> >&);
  70. template void igl::voxel_grid<double, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, 1, 3, 1, 1, 3> >(Eigen::AlignedBox<double, 3> const&, int, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, 1, 3, 1, 1, 3> >&);
  71. #endif