shapeup.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2017 Amir Vaxman <avaxman@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 <igl/shapeup.h>
  9. #include <igl/min_quad_with_fixed.h>
  10. #include <igl/igl_inline.h>
  11. #include <igl/setdiff.h>
  12. #include <igl/cat.h>
  13. #include <Eigen/Core>
  14. #include <vector>
  15. namespace igl
  16. {
  17. template <
  18. typename DerivedP,
  19. typename DerivedSC,
  20. typename DerivedS,
  21. typename Derivedw>
  22. IGL_INLINE bool shapeup_precomputation(const Eigen::PlainObjectBase<DerivedP>& P,
  23. const Eigen::PlainObjectBase<DerivedSC>& SC,
  24. const Eigen::PlainObjectBase<DerivedS>& S,
  25. const Eigen::PlainObjectBase<DerivedS>& E,
  26. const Eigen::PlainObjectBase<DerivedSC>& b,
  27. const Eigen::PlainObjectBase<Derivedw>& wShape,
  28. const Eigen::PlainObjectBase<Derivedw>& wSmooth,
  29. ShapeupData & sudata)
  30. {
  31. using namespace std;
  32. using namespace Eigen;
  33. sudata.P=P;
  34. sudata.SC=SC;
  35. sudata.S=S;
  36. sudata.b=b;
  37. typedef typename DerivedP::Scalar Scalar;
  38. //checking for consistency of the input
  39. assert(SC.rows()==S.rows());
  40. assert(SC.rows()==wShape.rows());
  41. assert(E.rows()==wSmooth.rows());
  42. assert(b.rows()!=0); //would lead to matrix becoming SPD
  43. sudata.DShape.conservativeResize(SC.sum(), P.rows()); //Shape matrix (integration);
  44. sudata.DClose.conservativeResize(b.rows(), P.rows()); //Closeness matrix for positional constraints
  45. sudata.DSmooth.conservativeResize(E.rows(), P.rows()); //smoothness matrix
  46. //Building shape matrix
  47. std::vector<Triplet<Scalar> > DShapeTriplets;
  48. int currRow=0;
  49. for (int i=0;i<S.rows();i++){
  50. Scalar avgCoeff=1.0/(Scalar)SC(i);
  51. for (int j=0;j<SC(i);j++){
  52. for (int k=0;k<SC(i);k++){
  53. if (j==k)
  54. DShapeTriplets.push_back(Triplet<Scalar>(currRow+j, S(i,k), (1.0-avgCoeff)));
  55. else
  56. DShapeTriplets.push_back(Triplet<Scalar>(currRow+j, S(i,k), (-avgCoeff)));
  57. }
  58. }
  59. currRow+=SC(i);
  60. }
  61. sudata.DShape.setFromTriplets(DShapeTriplets.begin(), DShapeTriplets.end());
  62. //Building closeness matrix
  63. std::vector<Triplet<Scalar> > DCloseTriplets;
  64. for (int i=0;i<b.size();i++)
  65. DCloseTriplets.push_back(Triplet<Scalar>(i,b(i), 1.0));
  66. sudata.DClose.setFromTriplets(DCloseTriplets.begin(), DCloseTriplets.end());
  67. //Building smoothness matrix
  68. std::vector<Triplet<Scalar> > DSmoothTriplets;
  69. for (int i=0; i<E.rows(); i++) {
  70. DSmoothTriplets.push_back(Triplet<Scalar>(i, E(i, 0), -1));
  71. DSmoothTriplets.push_back(Triplet<Scalar>(i, E(i, 1), 1));
  72. }
  73. SparseMatrix<Scalar> tempMat;
  74. igl::cat(1, sudata.DShape, sudata.DClose, tempMat);
  75. igl::cat(1, tempMat, sudata.DSmooth, sudata.A);
  76. //weight matrix
  77. vector<Triplet<Scalar> > WTriplets;
  78. //one weight per set in S.
  79. currRow=0;
  80. for (int i=0;i<SC.rows();i++){
  81. for (int j=0;j<SC(i);j++)
  82. WTriplets.push_back(Triplet<double>(currRow+j,currRow+j,sudata.shapeCoeff*wShape(i)));
  83. currRow+=SC(i);
  84. }
  85. for (int i=0;i<b.size();i++)
  86. WTriplets.push_back(Triplet<double>(SC.sum()+i, SC.sum()+i, sudata.closeCoeff));
  87. for (int i=0;i<E.rows();i++)
  88. WTriplets.push_back(Triplet<double>(SC.sum()+b.size()+i, SC.sum()+b.size()+i, sudata.smoothCoeff*wSmooth(i)));
  89. sudata.W.conservativeResize(SC.sum()+b.size()+E.rows(), SC.sum()+b.size()+E.rows());
  90. sudata.W.setFromTriplets(WTriplets.begin(), WTriplets.end());
  91. sudata.At=sudata.A.transpose(); //for efficieny, as we use the transpose a lot in the iteration
  92. sudata.Q=sudata.At*sudata.W*sudata.A;
  93. return min_quad_with_fixed_precompute(sudata.Q,VectorXi(),SparseMatrix<double>(),true,sudata.solver_data);
  94. }
  95. template <
  96. typename DerivedP,
  97. typename DerivedSC,
  98. typename DerivedS>
  99. IGL_INLINE bool shapeup_solve(const Eigen::PlainObjectBase<DerivedP>& bc,
  100. const std::function<bool(const Eigen::PlainObjectBase<DerivedP>&, const Eigen::PlainObjectBase<DerivedSC>&, const Eigen::PlainObjectBase<DerivedS>&, Eigen::PlainObjectBase<DerivedP>&)>& local_projection,
  101. const Eigen::PlainObjectBase<DerivedP>& P0,
  102. const ShapeupData & sudata,
  103. const bool quietIterations,
  104. Eigen::PlainObjectBase<DerivedP>& P)
  105. {
  106. using namespace Eigen;
  107. using namespace std;
  108. MatrixXd currP=P0;
  109. MatrixXd prevP=P0;
  110. MatrixXd projP;
  111. assert(bc.rows()==sudata.b.rows());
  112. MatrixXd rhs(sudata.A.rows(), 3); rhs.setZero();
  113. rhs.block(sudata.DShape.rows(), 0, sudata.b.rows(),3)=bc; //this stays constant throughout the iterations
  114. if (!quietIterations){
  115. cout<<"Shapeup Iterations, "<<sudata.DShape.rows()<<" constraints, solution size "<<P0.size()<<endl;
  116. cout<<"**********************************************************************************************"<<endl;
  117. }
  118. projP.conservativeResize(sudata.SC.rows(), 3*sudata.SC.maxCoeff());
  119. for (int iter=0;iter<sudata.maxIterations;iter++){
  120. local_projection(currP, sudata.SC,sudata.S,projP);
  121. //constructing the projection part of the (DShape rows of the) right hand side
  122. int currRow=0;
  123. for (int i=0;i<sudata.S.rows();i++)
  124. for (int j=0;j<sudata.SC(i);j++)
  125. rhs.row(currRow++)=projP.block(i, 3*j, 1,3);
  126. Eigen::PlainObjectBase<DerivedP> lsrhs=-sudata.At*sudata.W*rhs;
  127. MatrixXd Y(0,3), Beq(0,3); //We do not use the min_quad_solver fixed variables mechanism; they are treated with the closeness energy of ShapeUp.
  128. min_quad_with_fixed_solve(sudata.solver_data, lsrhs,Y,Beq,currP);
  129. double currChange=(currP-prevP).lpNorm<Infinity>();
  130. if (!quietIterations)
  131. cout << "Iteration "<<iter<<", integration Linf error: "<<currChange<< endl;
  132. prevP=currP;
  133. if (currChange<sudata.pTolerance){
  134. P=currP;
  135. return true;
  136. }
  137. }
  138. P=currP;
  139. return false; //we went over maxIterations
  140. }
  141. }
  142. #ifdef IGL_STATIC_LIBRARY
  143. template bool igl::shapeup_precomputation< typename Eigen::Matrix<double, -1, -1, 0, -1, -1>, typename Eigen::Matrix<int, -1, 1, 0, -1, 1>, typename Eigen::Matrix<int, -1, -1, 0, -1, -1>, typename Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, igl::ShapeupData&);
  144. template bool igl::shapeup_solve<typename Eigen::Matrix<double, -1, -1, 0, -1, -1>, typename Eigen::Matrix<int, -1, 1, 0, -1, 1>, typename Eigen::Matrix<int, -1, -1, 0, -1, -1> >(const Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >& bc, const std::function<bool(const Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, const Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, const Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >& ) >& local_projection, const Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >& P0, const igl::ShapeupData & sudata, const bool quietIterations, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >& P);
  145. #endif