gradMat.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "gradMat.h"
  9. #include <Eigen/Geometry>
  10. #include <vector>
  11. template <typename T, typename S>
  12. IGL_INLINE void igl::gradMat(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &V,
  13. const Eigen::Matrix<S, Eigen::Dynamic, Eigen::Dynamic> &F,
  14. Eigen::SparseMatrix<T> &G )
  15. {
  16. Eigen::PlainObjectBase<Eigen::Matrix<T,Eigen::Dynamic,3> > eperp21, eperp13;
  17. eperp21.resize(F.rows(),3);
  18. eperp13.resize(F.rows(),3);
  19. for (int i=0;i<F.rows();++i)
  20. {
  21. // renaming indices of vertices of triangles for convenience
  22. int i1 = F(i,0);
  23. int i2 = F(i,1);
  24. int i3 = F(i,2);
  25. // #F x 3 matrices of triangle edge vectors, named after opposite vertices
  26. Eigen::Matrix<T, 1, 3> v32 = V.row(i3) - V.row(i2);
  27. Eigen::Matrix<T, 1, 3> v13 = V.row(i1) - V.row(i3);
  28. Eigen::Matrix<T, 1, 3> v21 = V.row(i2) - V.row(i1);
  29. // area of parallelogram is twice area of triangle
  30. // area of parallelogram is || v1 x v2 ||
  31. Eigen::Matrix<T, 1, 3> n = v32.cross(v13);
  32. // This does correct l2 norm of rows, so that it contains #F list of twice
  33. // triangle areas
  34. double dblA = std::sqrt(n.dot(n));
  35. // now normalize normals to get unit normals
  36. Eigen::Matrix<T, 1, 3> u = n / dblA;
  37. // rotate each vector 90 degrees around normal
  38. double norm21 = std::sqrt(v21.dot(v21));
  39. double norm13 = std::sqrt(v13.dot(v13));
  40. eperp21.row(i) = u.cross(v21);
  41. eperp21.row(i) = eperp21.row(i) / std::sqrt(eperp21.row(i).dot(eperp21.row(i)));
  42. eperp21.row(i) *= norm21 / dblA;
  43. eperp13.row(i) = u.cross(v13);
  44. eperp13.row(i) = eperp13.row(i) / std::sqrt(eperp13.row(i).dot(eperp13.row(i)));
  45. eperp13.row(i) *= norm13 / dblA;
  46. }
  47. std::vector<int> rs;
  48. rs.reserve(F.rows()*4*3);
  49. std::vector<int> cs;
  50. cs.reserve(F.rows()*4*3);
  51. std::vector<double> vs;
  52. vs.reserve(F.rows()*4*3);
  53. // row indices
  54. for(int r=0;r<3;r++)
  55. {
  56. for(int j=0;j<4;j++)
  57. {
  58. for(int i=r*F.rows();i<(r+1)*F.rows();i++) rs.push_back(i);
  59. }
  60. }
  61. // column indices
  62. for(int r=0;r<3;r++)
  63. {
  64. for(int i=0;i<F.rows();i++) cs.push_back(F(i,1));
  65. for(int i=0;i<F.rows();i++) cs.push_back(F(i,0));
  66. for(int i=0;i<F.rows();i++) cs.push_back(F(i,2));
  67. for(int i=0;i<F.rows();i++) cs.push_back(F(i,0));
  68. }
  69. // values
  70. for(int i=0;i<F.rows();i++) vs.push_back(eperp13(i,0));
  71. for(int i=0;i<F.rows();i++) vs.push_back(-eperp13(i,0));
  72. for(int i=0;i<F.rows();i++) vs.push_back(eperp21(i,0));
  73. for(int i=0;i<F.rows();i++) vs.push_back(-eperp21(i,0));
  74. for(int i=0;i<F.rows();i++) vs.push_back(eperp13(i,1));
  75. for(int i=0;i<F.rows();i++) vs.push_back(-eperp13(i,1));
  76. for(int i=0;i<F.rows();i++) vs.push_back(eperp21(i,1));
  77. for(int i=0;i<F.rows();i++) vs.push_back(-eperp21(i,1));
  78. for(int i=0;i<F.rows();i++) vs.push_back(eperp13(i,2));
  79. for(int i=0;i<F.rows();i++) vs.push_back(-eperp13(i,2));
  80. for(int i=0;i<F.rows();i++) vs.push_back(eperp21(i,2));
  81. for(int i=0;i<F.rows();i++) vs.push_back(-eperp21(i,2));
  82. // create sparse gradient operator matrix
  83. G.resize(3*F.rows(),V.rows());
  84. std::vector<Eigen::Triplet<T> > triplets;
  85. for (int i=0;i<(int)vs.size();++i)
  86. {
  87. triplets.push_back(Eigen::Triplet<T>(rs[i],cs[i],vs[i]));
  88. }
  89. G.setFromTriplets(triplets.begin(), triplets.end());
  90. }
  91. #ifndef IGL_HEADER_ONLY
  92. // Explicit template specialization
  93. template void igl::gradMat<double, int>(Eigen::Matrix<double, -1, -1, 0, -1,-1> const&, Eigen::Matrix<int, -1, -1, 0, -1, -1> const&,Eigen::SparseMatrix<double, 0, int>&);
  94. #endif