shapeup.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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>& w,
  28. ShapeupData & sudata)
  29. {
  30. using namespace std;
  31. using namespace Eigen;
  32. sudata.P=P;
  33. sudata.SC=SC;
  34. sudata.S=S;
  35. sudata.b=b;
  36. typedef typename DerivedP::Scalar Scalar;
  37. sudata.DShape.conservativeResize(SC.sum(), P.rows()); //Shape matrix (integration);
  38. sudata.DClose.conservativeResize(b.rows(), P.rows()); //Closeness matrix for positional constraints
  39. sudata.DSmooth.conservativeResize(E.rows(), P.rows()); //smoothness matrix
  40. //Building shape matrix
  41. std::vector<Triplet<Scalar> > DShapeTriplets;
  42. int currRow=0;
  43. for (int i=0;i<S.rows();i++){
  44. double avgCoeff=1.0/(Scalar)SC(i);
  45. for (int j=0;j<SC(i);j++){
  46. for (int k=0;k<SC(i);k++){
  47. if (j==k)
  48. DShapeTriplets.push_back(Triplet<Scalar>(currRow+j, S(i,k), (1.0-avgCoeff)));
  49. else
  50. DShapeTriplets.push_back(Triplet<Scalar>(currRow+j, S(i,k), (-avgCoeff)));
  51. }
  52. }
  53. currRow+=SC(i);
  54. }
  55. sudata.DShape.setFromTriplets(DShapeTriplets.begin(), DShapeTriplets.end());
  56. //Building closeness matrix
  57. std::vector<Triplet<Scalar> > DCloseTriplets;
  58. for (int i=0;i<b.size();i++)
  59. DCloseTriplets.push_back(Triplet<Scalar>(i,b(i), 1.0));
  60. sudata.DClose.setFromTriplets(DCloseTriplets.begin(), DCloseTriplets.end());
  61. //Building smoothness matrix
  62. std::vector<Triplet<Scalar> > DSmoothTriplets;
  63. for (int i = 0; i < E.rows(); i++) {
  64. DSmoothTriplets.push_back(Triplet<Scalar>(i, E(i, 0), -1));
  65. DSmoothTriplets.push_back(Triplet<Scalar>(i, E(i, 1), 1));
  66. }
  67. SparseMatrix<Scalar> tempMat;
  68. igl::cat(1, sudata.DShape, sudata.DClose, tempMat);
  69. igl::cat(1, tempMat, sudata.DSmooth, sudata.A);
  70. //weight matrix
  71. vector<Triplet<Scalar> > WTriplets;
  72. //one weight per set in S.
  73. currRow=0;
  74. for (int i=0;i<SC.rows();i++){
  75. for (int j=0;j<SC(i);j++)
  76. WTriplets.push_back(Triplet<double>(currRow+j,currRow+j,sudata.shapeCoeff*w(i)));
  77. currRow+=SC(i);
  78. }
  79. for (int i=0;i<b.size();i++)
  80. WTriplets.push_back(Triplet<double>(SC.sum()+i, SC.sum()+i, sudata.closeCoeff));
  81. for (int i=0;i<E.rows();i++)
  82. WTriplets.push_back(Triplet<double>(SC.sum()+b.size()+i, SC.sum()+b.size()+i, sudata.smoothCoeff));
  83. sudata.W.conservativeResize(SC.sum()+b.size()+E.rows(), SC.sum()+b.size()+E.rows());
  84. sudata.W.setFromTriplets(WTriplets.begin(), WTriplets.end());
  85. sudata.At=sudata.A.transpose(); //for efficieny, as we use the transpose a lot in the iteration
  86. sudata.Q=sudata.At*sudata.W*sudata.A;
  87. return min_quad_with_fixed_precompute(sudata.Q,VectorXi(),SparseMatrix<double>(),true,sudata.solver_data);
  88. }
  89. template <
  90. typename DerivedP,
  91. typename DerivedSC,
  92. typename DerivedS>
  93. IGL_INLINE bool shapeup_solve(const Eigen::PlainObjectBase<DerivedP>& bc,
  94. const std::function<bool(const Eigen::PlainObjectBase<DerivedP>&, const Eigen::PlainObjectBase<DerivedSC>&, const Eigen::PlainObjectBase<DerivedS>&, Eigen::PlainObjectBase<DerivedP>&)>& local_projection,
  95. const Eigen::PlainObjectBase<DerivedP>& P0,
  96. const ShapeupData & sudata,
  97. const bool quiet,
  98. Eigen::PlainObjectBase<DerivedP>& P)
  99. {
  100. using namespace Eigen;
  101. using namespace std;
  102. MatrixXd currP=P0;
  103. MatrixXd prevP=P0;
  104. MatrixXd projP;
  105. MatrixXd rhs(sudata.A.rows(), 3); rhs.setZero();
  106. rhs.block(sudata.DShape.rows(), 0, sudata.b.rows(),3)=bc; //this stays constant throughout the iterations
  107. if (!quiet){
  108. cout<<"Shapeup Iterations, "<<sudata.DShape.rows()<<" constraints, solution size "<<P0.size()<<endl;
  109. cout<<"**********************************************************************************************"<<endl;
  110. }
  111. projP.conservativeResize(sudata.SC.rows(), 3*sudata.SC.maxCoeff());
  112. for (int iter=0;iter<sudata.maxIterations;iter++){
  113. local_projection(currP, sudata.SC,sudata.S,projP);
  114. //constructing the projection part of the (DShape rows of the) right hand side
  115. int currRow=0;
  116. for (int i=0;i<sudata.S.rows();i++)
  117. for (int j=0;j<sudata.SC(i);j++)
  118. rhs.row(currRow++)=projP.block(i, 3*j, 1,3);
  119. Eigen::PlainObjectBase<DerivedP> lsrhs=-sudata.At*sudata.W*rhs;
  120. MatrixXd Y(0,3), Beq(0,3);
  121. min_quad_with_fixed_solve(sudata.solver_data, lsrhs,Y,Beq,currP);
  122. double currChange=(currP-prevP).lpNorm<Infinity>();
  123. if (!quiet)
  124. cout << "Iteration "<<iter<<", integration Linf error: "<<currChange<< endl;
  125. prevP=currP;
  126. if (currChange<sudata.pTolerance)
  127. break;
  128. }
  129. P=currP;
  130. return true;
  131. }
  132. }
  133. #ifdef IGL_STATIC_LIBRARY
  134. 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&, igl::ShapeupData&);
  135. 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 quiet, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >& P);
  136. #endif