massmatrix.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 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 "massmatrix.h"
  9. #include "massmatrix_intrinsic.h"
  10. #include "edge_lengths.h"
  11. #include "sparse.h"
  12. #include "doublearea.h"
  13. #include "volume.h"
  14. #include "voronoi_mass.h"
  15. #include "repmat.h"
  16. #include <Eigen/Geometry>
  17. #include <iostream>
  18. // For std::enable_if
  19. #include <type_traits>
  20. namespace igl
  21. {
  22. template <typename DerivedV, typename DerivedF, typename Scalar, int simplex_size = DerivedF::ColsAtCompileTime>
  23. struct MassMatrixHelper;
  24. // This would be easier with C++17 if constexpr
  25. // Specialization for triangles
  26. template <typename DerivedV, typename DerivedF, typename Scalar>
  27. struct MassMatrixHelper<DerivedV, DerivedF, Scalar, 3> {
  28. static void compute(
  29. const Eigen::MatrixBase<DerivedV>& V,
  30. const Eigen::MatrixBase<DerivedF>& F,
  31. const MassMatrixType type,
  32. Eigen::SparseMatrix<Scalar>& M)
  33. {
  34. MassMatrixType eff_type =
  35. type == MASSMATRIX_TYPE_DEFAULT? MASSMATRIX_TYPE_VORONOI : type;
  36. Eigen::Matrix<Scalar, Eigen::Dynamic, 3> l;
  37. igl::edge_lengths(V, F, l);
  38. return massmatrix_intrinsic(l, F, eff_type, M);
  39. }
  40. };
  41. // Specialization for tetrahedra
  42. template <typename DerivedV, typename DerivedF, typename Scalar>
  43. struct MassMatrixHelper<DerivedV, DerivedF, Scalar, 4> {
  44. static void compute(
  45. const Eigen::MatrixBase<DerivedV>& V,
  46. const Eigen::MatrixBase<DerivedF>& F,
  47. const MassMatrixType type,
  48. Eigen::SparseMatrix<Scalar>& M)
  49. {
  50. const int n = V.rows();
  51. const int m = F.rows();
  52. using Eigen::Matrix;
  53. using Eigen::Dynamic;
  54. MassMatrixType eff_type =
  55. type == MASSMATRIX_TYPE_DEFAULT? MASSMATRIX_TYPE_BARYCENTRIC: type;
  56. Eigen::Matrix<Scalar, Eigen::Dynamic, 1> vol;
  57. volume(V, F, vol);
  58. vol = vol.array().abs();
  59. Eigen::Matrix<typename DerivedF::Scalar ,Eigen::Dynamic,1> MI;
  60. Eigen::Matrix<typename DerivedF::Scalar ,Eigen::Dynamic,1> MJ;
  61. Eigen::Matrix<Scalar ,Eigen::Dynamic,1> MV;
  62. switch (eff_type)
  63. {
  64. case MASSMATRIX_TYPE_BARYCENTRIC:
  65. MI.resize(m*4,1); MJ.resize(m*4,1); MV.resize(m*4,1);
  66. MI.block(0*m,0,m,1) = F.col(0);
  67. MI.block(1*m,0,m,1) = F.col(1);
  68. MI.block(2*m,0,m,1) = F.col(2);
  69. MI.block(3*m,0,m,1) = F.col(3);
  70. MJ = MI;
  71. repmat(vol,4,1,MV);
  72. assert(MV.rows()==m*4&&MV.cols()==1);
  73. MV.array() /= 4.;
  74. break;
  75. case MASSMATRIX_TYPE_VORONOI:
  76. {
  77. MI = decltype(MI)::LinSpaced(n,0,n-1);
  78. MJ = MI;
  79. voronoi_mass(V,F,MV);
  80. break;
  81. }
  82. case MASSMATRIX_TYPE_FULL:
  83. MI.resize(m*16,1); MJ.resize(m*16,1); MV.resize(m*16,1);
  84. // indicies and values of the element mass matrix entries in the order
  85. // (1,0),(2,0),(3,0),(2,1),(3,1),(0,1),(3,2),(0,2),(1,2),(0,3),(1,3),(2,3),(0,0),(1,1),(2,2),(3,3);
  86. MI<<F.col(1),F.col(2),F.col(3),F.col(2),F.col(3),F.col(0),F.col(3),F.col(0),F.col(1),F.col(0),F.col(1),F.col(2),F.col(0),F.col(1),F.col(2),F.col(3);
  87. MJ<<F.col(0),F.col(0),F.col(0),F.col(1),F.col(1),F.col(1),F.col(2),F.col(2),F.col(2),F.col(3),F.col(3),F.col(3),F.col(0),F.col(1),F.col(2),F.col(3);
  88. repmat(vol,16,1,MV);
  89. assert(MV.rows()==m*16&&MV.cols()==1);
  90. MV.block(0*m,0,12*m,1) /= 20.;
  91. MV.block(12*m,0,4*m,1) /= 10.;
  92. break;
  93. default:
  94. assert(false && "Unknown Mass matrix eff_type");
  95. }
  96. sparse(MI,MJ,MV,n,n,M);
  97. }
  98. };
  99. // General template for handling Eigen::Dynamic at runtime
  100. template <typename DerivedV, typename DerivedF, typename Scalar>
  101. struct MassMatrixHelper<DerivedV, DerivedF, Scalar, Eigen::Dynamic> {
  102. static void compute(
  103. const Eigen::MatrixBase<DerivedV>& V,
  104. const Eigen::MatrixBase<DerivedF>& F,
  105. const MassMatrixType type,
  106. Eigen::SparseMatrix<Scalar>& M)
  107. {
  108. if (F.cols() == 3) {
  109. MassMatrixHelper<DerivedV, DerivedF, Scalar, 3>::compute(V, F, type, M);
  110. } else if (F.cols() == 4) {
  111. MassMatrixHelper<DerivedV, DerivedF, Scalar, 4>::compute(V, F, type, M);
  112. } else {
  113. // Handle unsupported simplex size at runtime
  114. assert(false && "Unsupported simplex size");
  115. }
  116. }
  117. };
  118. }
  119. template <typename DerivedV, typename DerivedF, typename Scalar>
  120. IGL_INLINE void igl::massmatrix(
  121. const Eigen::MatrixBase<DerivedV> & V,
  122. const Eigen::MatrixBase<DerivedF> & F,
  123. const MassMatrixType type,
  124. Eigen::SparseMatrix<Scalar>& M)
  125. {
  126. MassMatrixHelper<DerivedV, DerivedF, Scalar, DerivedF::ColsAtCompileTime>::compute(V, F, type, M);
  127. }
  128. #ifdef IGL_STATIC_LIBRARY
  129. // Explicit template instantiation
  130. // generated by autoexplicit.sh
  131. template void igl::massmatrix<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, double>(Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, igl::MassMatrixType, Eigen::SparseMatrix<double, 0, int>&);
  132. // generated by autoexplicit.sh
  133. template void igl::massmatrix<Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, double>(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, igl::MassMatrixType, Eigen::SparseMatrix<double, 0, int>&);
  134. // generated by autoexplicit.sh
  135. template void igl::massmatrix<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 4, 0, -1, 4>, double>(Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 4, 0, -1, 4> > const&, igl::MassMatrixType, Eigen::SparseMatrix<double, 0, int>&);
  136. // generated by autoexplicit.sh
  137. template void igl::massmatrix<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, double>(Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, igl::MassMatrixType, Eigen::SparseMatrix<double, 0, int>&);
  138. template void igl::massmatrix<Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3>, double>(Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&, igl::MassMatrixType, Eigen::SparseMatrix<double, 0, int>&);
  139. template void igl::massmatrix<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, double>(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, igl::MassMatrixType, Eigen::SparseMatrix<double, 0, int>&);
  140. #endif