voxel_grid.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "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. side.resize(1, 3);
  25. box.diagonal().maxCoeff(&si);
  26. //DerivedGV::Index si = 0;
  27. //assert(si>=0);
  28. const Scalar s_len = box.diagonal()(si);
  29. assert(in_s>(pad_count*2+1) && "s should be > 2*pad_count+1");
  30. const Scalar s = in_s - 2*pad_count;
  31. side(si) = s;
  32. for(int i = 0;i<3;i++)
  33. {
  34. if(i!=si)
  35. {
  36. side(i) = std::ceil(s * (box.max()(i)-box.min()(i))/s_len);
  37. }
  38. }
  39. side.array() += 2*pad_count;
  40. grid(side,GV);
  41. // A * p/s + B = min
  42. // A * (1-p/s) + B = max
  43. // B = min - A * p/s
  44. // A * (1-p/s) + min - A * p/s = max
  45. // A * (1-p/s) - A * p/s = max-min
  46. // A * (1-2p/s) = max-min
  47. // A = (max-min)/(1-2p/s)
  48. const Array<Scalar,3,1> ps=
  49. (Scalar)(pad_count)/(side.transpose().template cast<Scalar>().array()-1.);
  50. const Array<Scalar,3,1> A = box.diagonal().array()/(1.0-2.*ps);
  51. //// This would result in an "anamorphic", but perfectly fit grid:
  52. //const Array<Scalar,3,1> B = box.min().array() - A.array()*ps;
  53. //GV.array().rowwise() *= A.transpose();
  54. //GV.array().rowwise() += B.transpose();
  55. // Instead scale by largest factor and move to match center
  56. typename Array<Scalar,3,1>::Index ai = -1;
  57. Scalar a = A.maxCoeff(&ai);
  58. const Array<Scalar,1,3> ratio =
  59. a*(side.template cast<Scalar>().array()-1.0)/(Scalar)(side(ai)-1.0);
  60. GV.array().rowwise() *= ratio;
  61. const Eigen::Matrix<Scalar,1,3> offset = (box.center().transpose()-GV.colwise().mean()).eval();
  62. GV.rowwise() += offset;
  63. }
  64. template <
  65. typename DerivedV,
  66. typename DerivedGV,
  67. typename Derivedside>
  68. IGL_INLINE void igl::voxel_grid(
  69. const Eigen::MatrixBase<DerivedV> & V,
  70. const typename DerivedV::Scalar offset,
  71. const int s,
  72. const int pad_count,
  73. Eigen::PlainObjectBase<DerivedGV> & GV,
  74. Eigen::PlainObjectBase<Derivedside> & side)
  75. {
  76. typedef typename DerivedV::Scalar Scalar;
  77. Eigen::AlignedBox<Scalar,3> box;
  78. typedef Eigen::Matrix<Scalar,1,3> RowVector3S;
  79. assert(V.cols() == 3 && "V must contain positions in 3D");
  80. RowVector3S min_ext = V.colwise().minCoeff().array() - offset;
  81. RowVector3S max_ext = V.colwise().maxCoeff().array() + offset;
  82. box.extend(min_ext.transpose());
  83. box.extend(max_ext.transpose());
  84. return igl::voxel_grid(box,s,1,GV,side);
  85. }
  86. #ifdef IGL_STATIC_LIBRARY
  87. // Explicit template instantiation
  88. // generated by autoexplicit.sh
  89. template void igl::voxel_grid<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, 1, 3, 1, 1, 3> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<double, -1, -1, 0, -1, -1>::Scalar, int, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, 1, 3, 1, 1, 3> >&);
  90. // generated by autoexplicit.sh
  91. template void igl::voxel_grid<float, Eigen::Matrix<float, -1, -1, 0, -1, -1>, Eigen::Matrix<int, 3, 1, 0, 3, 1> >(Eigen::AlignedBox<float, 3> const&, int, int, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, 3, 1, 0, 3, 1> >&);
  92. // generated by autoexplicit.sh
  93. 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> >&);
  94. 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> >&);
  95. 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> >&);
  96. 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> >&);
  97. 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> >&);
  98. #endif