lscm.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <daniele.panozzo@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 "lscm.h"
  9. #include "vector_area_matrix.h"
  10. #include "cotmatrix.h"
  11. #include "repdiag.h"
  12. #include "min_quad_with_fixed.h"
  13. #include <iostream>
  14. IGL_INLINE bool igl::lscm(
  15. const Eigen::MatrixXd& V,
  16. const Eigen::MatrixXi& F,
  17. const Eigen::VectorXi& b,
  18. const Eigen::MatrixXd& bc,
  19. Eigen::MatrixXd & V_uv)
  20. {
  21. using namespace Eigen;
  22. using namespace std;
  23. // Assemble the area matrix (note that A is #Vx2 by #Vx2)
  24. SparseMatrix<double> A;
  25. igl::vector_area_matrix(F,A);
  26. // Assemble the cotan laplacian matrix
  27. SparseMatrix<double> L;
  28. igl::cotmatrix(V,F,L);
  29. SparseMatrix<double> L_flat;
  30. repdiag(L,2,L_flat);
  31. VectorXi b_flat(b.size()*bc.cols(),1);
  32. VectorXd bc_flat(bc.size(),1);
  33. for(int c = 0;c<bc.cols();c++)
  34. {
  35. b_flat.block(c*b.size(),0,b.rows(),1) = c*V.rows() + b.array();
  36. bc_flat.block(c*bc.rows(),0,bc.rows(),1) = bc.col(c);
  37. }
  38. // Minimize the LSCM energy
  39. SparseMatrix<double> Q = -L_flat + 2.*A;
  40. const VectorXd B_flat = VectorXd::Zero(V.rows()*2);
  41. igl::min_quad_with_fixed_data<double> data;
  42. if(!igl::min_quad_with_fixed_precompute(Q,b_flat,SparseMatrix<double>(),true,data))
  43. {
  44. return false;
  45. }
  46. MatrixXd W_flat;
  47. if(!min_quad_with_fixed_solve(data,B_flat,bc_flat,VectorXd(),W_flat))
  48. {
  49. return false;
  50. }
  51. assert(W_flat.rows() == V.rows()*2);
  52. V_uv.resize(V.rows(),2);
  53. for (unsigned i=0;i<V_uv.cols();++i)
  54. {
  55. V_uv.col(V_uv.cols()-i-1) = W_flat.block(V_uv.rows()*i,0,V_uv.rows(),1);
  56. }
  57. return true;
  58. }
  59. #ifdef IGL_STATIC_LIBRARY
  60. // Explicit template instantiation
  61. #endif