cotmatrix.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 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 "cotmatrix.h"
  9. #include <vector>
  10. // For error printing
  11. #include <cstdio>
  12. #include "cotmatrix_entries.h"
  13. // Bug in unsupported/Eigen/SparseExtra needs iostream first
  14. #include <iostream>
  15. #include <unsupported/Eigen/SparseExtra>
  16. template <typename DerivedV, typename DerivedF, typename Scalar>
  17. IGL_INLINE void igl::cotmatrix(
  18. const Eigen::PlainObjectBase<DerivedV> & V,
  19. const Eigen::PlainObjectBase<DerivedF> & F,
  20. Eigen::SparseMatrix<Scalar>& L)
  21. {
  22. using namespace igl;
  23. using namespace Eigen;
  24. using namespace std;
  25. L.resize(V.rows(),V.rows());
  26. Matrix<int,Dynamic,2> edges;
  27. int simplex_size = F.cols();
  28. // 3 for triangles, 4 for tets
  29. assert(simplex_size == 3 || simplex_size == 4);
  30. if(simplex_size == 3)
  31. {
  32. // This is important! it could decrease the comptuation time by a factor of 2
  33. // Laplacian for a closed 2d manifold mesh will have on average 7 entries per
  34. // row
  35. L.reserve(10*V.rows());
  36. edges.resize(3,2);
  37. edges <<
  38. 1,2,
  39. 2,0,
  40. 0,1;
  41. }else if(simplex_size == 4)
  42. {
  43. L.reserve(17*V.rows());
  44. edges.resize(6,2);
  45. edges <<
  46. 1,2,
  47. 2,0,
  48. 0,1,
  49. 3,0,
  50. 3,1,
  51. 3,2;
  52. }else
  53. {
  54. return;
  55. }
  56. // Gather cotangents
  57. Matrix<Scalar,Dynamic,Dynamic> C;
  58. cotmatrix_entries(V,F,C);
  59. vector<Triplet<Scalar> > IJV;
  60. IJV.reserve(F.rows()*edges.rows()*4);
  61. // Loop over triangles
  62. for(int i = 0; i < F.rows(); i++)
  63. {
  64. // loop over edges of element
  65. for(int e = 0;e<edges.rows();e++)
  66. {
  67. int source = F(i,edges(e,0));
  68. int dest = F(i,edges(e,1));
  69. IJV.push_back(Triplet<Scalar>(source,dest,C(i,e)));
  70. IJV.push_back(Triplet<Scalar>(dest,source,C(i,e)));
  71. IJV.push_back(Triplet<Scalar>(source,source,-C(i,e)));
  72. IJV.push_back(Triplet<Scalar>(dest,dest,-C(i,e)));
  73. }
  74. }
  75. L.setFromTriplets(IJV.begin(),IJV.end());
  76. }
  77. #ifdef IGL_STATIC_LIBRARY
  78. // Explicit template specialization
  79. // generated by autoexplicit.sh
  80. template void igl::cotmatrix<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, double>(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::SparseMatrix<double, 0, int>&);
  81. #endif