crouzeix_raviart_massmatrix.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 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 "crouzeix_raviart_massmatrix.h"
  9. #include "unique_simplices.h"
  10. #include "oriented_facets.h"
  11. #include "is_edge_manifold.h"
  12. #include "doublearea.h"
  13. #include "volume.h"
  14. #include <cassert>
  15. #include <vector>
  16. template <typename MT, typename DerivedV, typename DerivedF, typename DerivedE, typename DerivedEMAP>
  17. void igl::crouzeix_raviart_massmatrix(
  18. const Eigen::MatrixBase<DerivedV> & V,
  19. const Eigen::MatrixBase<DerivedF> & F,
  20. Eigen::SparseMatrix<MT> & M,
  21. Eigen::PlainObjectBase<DerivedE> & E,
  22. Eigen::PlainObjectBase<DerivedEMAP> & EMAP)
  23. {
  24. // All occurrences of directed "facets"
  25. Eigen::Matrix<typename DerivedF::Scalar, Eigen::Dynamic, Eigen::Dynamic> allE;
  26. oriented_facets(F,allE);
  27. Eigen::Matrix<typename DerivedF::Scalar, Eigen::Dynamic, 1> _1;
  28. unique_simplices(allE,E,_1,EMAP);
  29. return crouzeix_raviart_massmatrix(V,F,E,EMAP,M);
  30. }
  31. template <typename MT, typename DerivedV, typename DerivedF, typename DerivedE, typename DerivedEMAP>
  32. void igl::crouzeix_raviart_massmatrix(
  33. const Eigen::MatrixBase<DerivedV> & V,
  34. const Eigen::MatrixBase<DerivedF> & F,
  35. const Eigen::MatrixBase<DerivedE> & E,
  36. const Eigen::MatrixBase<DerivedEMAP> & EMAP,
  37. Eigen::SparseMatrix<MT> & M)
  38. {
  39. // Mesh should be edge-manifold (TODO: replace `is_edge_manifold` with
  40. // `is_facet_manifold`)
  41. assert(F.cols() != 3 || is_edge_manifold(F));
  42. // number of elements (triangles)
  43. const int m = F.rows();
  44. // Get triangle areas/volumes
  45. Eigen::VectorXd TA;
  46. // Element simplex size
  47. const int ss = F.cols();
  48. switch(ss)
  49. {
  50. default:
  51. assert(false && "Unsupported simplex size");
  52. case 3:
  53. doublearea(V,F,TA);
  54. TA *= 0.5;
  55. break;
  56. case 4:
  57. volume(V,F,TA);
  58. break;
  59. }
  60. std::vector<Eigen::Triplet<MT> > MIJV(ss*m);
  61. assert(EMAP.size() == m*ss);
  62. for(int f = 0;f<m;f++)
  63. {
  64. for(int c = 0;c<ss;c++)
  65. {
  66. MIJV[f+m*c] = Eigen::Triplet<MT>(EMAP(f+m*c, 0),EMAP(f+m*c, 0),TA(f)/(double)(ss));
  67. }
  68. }
  69. M.resize(E.rows(),E.rows());
  70. M.setFromTriplets(MIJV.begin(),MIJV.end());
  71. }
  72. #ifdef IGL_STATIC_LIBRARY
  73. // Explicit template instantiation
  74. // generated by autoexplicit.sh
  75. template void igl::crouzeix_raviart_massmatrix<double, 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::SparseMatrix<double, 0, int>&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  76. template void igl::crouzeix_raviart_massmatrix<double, 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::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::SparseMatrix<double, 0, int>&);
  77. template void igl::crouzeix_raviart_massmatrix<float, Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::SparseMatrix<float, 0, int>&);
  78. template void igl::crouzeix_raviart_massmatrix<double, 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::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::SparseMatrix<double, 0, int>&);
  79. #endif