exploded_view.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2020 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 "exploded_view.h"
  9. #include "barycenter.h"
  10. #include "volume.h"
  11. template <
  12. typename DerivedV,
  13. typename DerivedT,
  14. typename DerivedEV,
  15. typename DerivedEF,
  16. typename DerivedI,
  17. typename DerivedJ>
  18. IGL_INLINE void igl::exploded_view(
  19. const Eigen::MatrixBase<DerivedV> & V,
  20. const Eigen::MatrixBase<DerivedT> & T,
  21. const typename DerivedV::Scalar s,
  22. const typename DerivedV::Scalar t,
  23. Eigen::PlainObjectBase<DerivedEV> & EV,
  24. Eigen::PlainObjectBase<DerivedEF> & EF,
  25. Eigen::PlainObjectBase<DerivedI> & I,
  26. Eigen::PlainObjectBase<DerivedJ> & J)
  27. {
  28. assert(T.cols() == 4 && "T should be a tet mesh");
  29. EV.resize(4*T.rows(),3);
  30. EF.resize(4*T.rows(),3);
  31. I.resize(EV.rows());
  32. J.resize(EF.rows());
  33. Eigen::MatrixXd BC;
  34. igl::barycenter(V,T,BC);
  35. Eigen::VectorXd vol;
  36. igl::volume(V,T,vol);
  37. const Eigen::RowVectorXd c = vol.transpose()*BC/vol.array().sum();
  38. for(int i = 0;i<T.rows();i++)
  39. {
  40. // scale the barycenters outward
  41. const auto tbc = ((BC.row(i)-c)*t+c).eval();
  42. for(int j = 0;j<4;j++)
  43. {
  44. // vector to barycenter
  45. const auto v = V.row(T(i,j))-BC.row(i);
  46. // scale vector and add to scaled barycenter
  47. EV.row(i*4+j) = v*s+tbc;
  48. I(i*4+j) = T(i,j);
  49. J(i*4+j) = i;
  50. if(j%2)
  51. {
  52. EF(i*4+j,0) = i*4+((j+3)%4);
  53. EF(i*4+j,1) = i*4+((j+2)%4);
  54. EF(i*4+j,2) = i*4+((j+1)%4);
  55. }else
  56. {
  57. EF(i*4+j,0) = i*4+((j+1)%4);
  58. EF(i*4+j,1) = i*4+((j+2)%4);
  59. EF(i*4+j,2) = i*4+((j+3)%4);
  60. }
  61. }
  62. }
  63. }
  64. #ifdef IGL_STATIC_LIBRARY
  65. // Explicit template instantiation
  66. // generated by autoexplicit.sh
  67. template void igl::exploded_view<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::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -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<int, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<double, -1, -1, 0, -1, -1>::Scalar, Eigen::Matrix<double, -1, -1, 0, -1, -1>::Scalar, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  68. #endif