lscm.cpp 1.9 KB

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