shapeup.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. //This projection does nothing but render points into projP. Mostly used for "echoing" the global step
  18. IGL_INLINE bool shapeup_identity_projection(const Eigen::PlainObjectBase<Eigen::MatrixXd>& P, const Eigen::PlainObjectBase<Eigen::VectorXi>& SC, const Eigen::PlainObjectBase<Eigen::MatrixXi>& S, Eigen::PlainObjectBase<Eigen::MatrixXd>& projP){
  19. projP.conservativeResize(SC.rows(), 3*SC.maxCoeff());
  20. for (int i=0;i<S.rows();i++){
  21. Eigen::RowVector3d avgCurrP=Eigen::RowVector3d::Zero();
  22. for (int j=0;j<SC(i);j++)
  23. avgCurrP+=P.row(S(i,j))/(double)(SC(i));
  24. for (int j=0;j<SC(i);j++)
  25. projP.block(i,3*j,1,3)=P.row(S(i,j))-avgCurrP;
  26. }
  27. return true;
  28. }
  29. //the projection assumes that the sets are vertices of polygons in order
  30. IGL_INLINE bool shapeup_regular_face_projection(const Eigen::PlainObjectBase<Eigen::MatrixXd>& P, const Eigen::PlainObjectBase<Eigen::VectorXi>& SC, const Eigen::PlainObjectBase<Eigen::MatrixXi>& S, Eigen::PlainObjectBase<Eigen::MatrixXd>& projP){
  31. projP.conservativeResize(SC.rows(), 3*SC.maxCoeff());
  32. for (int currRow=0;currRow<SC.rows();currRow++){
  33. //computing average
  34. int N=SC(currRow);
  35. const Eigen::RowVectorXi SRow=S.row(currRow);
  36. Eigen::RowVector3d avgCurrP=Eigen::RowVector3d::Zero();
  37. Eigen::MatrixXd targetPolygon(N, 3);
  38. Eigen::MatrixXd sourcePolygon(N, 3);
  39. for (int j=0;j<N;j++)
  40. avgCurrP+=P.row(SRow(j))/(double)(N);
  41. for (int j=0;j<N;j++)
  42. targetPolygon.row(j)=P.row(SRow(j))-avgCurrP;
  43. //creating perfectly regular source polygon
  44. for (int j=0;j<N;j++)
  45. sourcePolygon.row(j)<<cos(2*M_PI*(double)j/(double(N))), sin(2*M_PI*(double)j/(double(N))),0.0;
  46. //finding closest similarity transformation between source and target
  47. Eigen::MatrixXd corrMat=sourcePolygon.transpose()*targetPolygon;
  48. Eigen::JacobiSVD<Eigen::Matrix3d> svd(corrMat, Eigen::ComputeFullU | Eigen::ComputeFullV);
  49. Eigen::MatrixXd R=svd.matrixU()*svd.matrixV().transpose();
  50. //getting scale by edge length change average. TODO: by singular values
  51. Eigen::VectorXd sourceEdgeLengths(N);
  52. Eigen::VectorXd targetEdgeLengths(N);
  53. for (int j=0;j<N;j++){
  54. sourceEdgeLengths(j)=(sourcePolygon.row((j+1)%N)-sourcePolygon.row(j)).norm();
  55. targetEdgeLengths(j)=(targetPolygon.row((j+1)%N)-targetPolygon.row(j)).norm();
  56. }
  57. double scale=(targetEdgeLengths.cwiseQuotient(sourceEdgeLengths)).mean();
  58. for (int j=0;j<N;j++)
  59. projP.block(currRow,3*j,1,3)=sourcePolygon.row(j)*R*scale;
  60. }
  61. return true;
  62. }
  63. template <
  64. typename DerivedP,
  65. typename DerivedSC,
  66. typename DerivedS,
  67. typename Derivedw>
  68. IGL_INLINE bool shapeup_precomputation(const Eigen::PlainObjectBase<DerivedP>& P,
  69. const Eigen::PlainObjectBase<DerivedSC>& SC,
  70. const Eigen::PlainObjectBase<DerivedS>& S,
  71. const Eigen::PlainObjectBase<DerivedS>& E,
  72. const Eigen::PlainObjectBase<DerivedSC>& b,
  73. const Eigen::PlainObjectBase<Derivedw>& wShape,
  74. const Eigen::PlainObjectBase<Derivedw>& wSmooth,
  75. ShapeupData & sudata)
  76. {
  77. using namespace std;
  78. using namespace Eigen;
  79. sudata.P=P;
  80. sudata.SC=SC;
  81. sudata.S=S;
  82. sudata.b=b;
  83. typedef typename DerivedP::Scalar Scalar;
  84. //checking for consistency of the input
  85. assert(SC.rows()==S.rows());
  86. assert(SC.rows()==wShape.rows());
  87. assert(E.rows()==wSmooth.rows());
  88. assert(b.rows()!=0); //would lead to matrix becoming SPD
  89. sudata.DShape.conservativeResize(SC.sum(), P.rows()); //Shape matrix (integration);
  90. sudata.DClose.conservativeResize(b.rows(), P.rows()); //Closeness matrix for positional constraints
  91. sudata.DSmooth.conservativeResize(E.rows(), P.rows()); //smoothness matrix
  92. //Building shape matrix
  93. std::vector<Triplet<Scalar> > DShapeTriplets;
  94. int currRow=0;
  95. for (int i=0;i<S.rows();i++){
  96. Scalar avgCoeff=1.0/(Scalar)SC(i);
  97. for (int j=0;j<SC(i);j++){
  98. for (int k=0;k<SC(i);k++){
  99. if (j==k)
  100. DShapeTriplets.push_back(Triplet<Scalar>(currRow+j, S(i,k), (1.0-avgCoeff)));
  101. else
  102. DShapeTriplets.push_back(Triplet<Scalar>(currRow+j, S(i,k), (-avgCoeff)));
  103. }
  104. }
  105. currRow+=SC(i);
  106. }
  107. sudata.DShape.setFromTriplets(DShapeTriplets.begin(), DShapeTriplets.end());
  108. //Building closeness matrix
  109. std::vector<Triplet<Scalar> > DCloseTriplets;
  110. for (int i=0;i<b.size();i++)
  111. DCloseTriplets.push_back(Triplet<Scalar>(i,b(i), 1.0));
  112. sudata.DClose.setFromTriplets(DCloseTriplets.begin(), DCloseTriplets.end());
  113. //Building smoothness matrix
  114. std::vector<Triplet<Scalar> > DSmoothTriplets;
  115. for (int i=0; i<E.rows(); i++) {
  116. DSmoothTriplets.push_back(Triplet<Scalar>(i, E(i, 0), -1));
  117. DSmoothTriplets.push_back(Triplet<Scalar>(i, E(i, 1), 1));
  118. }
  119. SparseMatrix<Scalar> tempMat;
  120. igl::cat(1, sudata.DShape, sudata.DClose, tempMat);
  121. igl::cat(1, tempMat, sudata.DSmooth, sudata.A);
  122. //weight matrix
  123. vector<Triplet<Scalar> > WTriplets;
  124. //one weight per set in S.
  125. currRow=0;
  126. for (int i=0;i<SC.rows();i++){
  127. for (int j=0;j<SC(i);j++)
  128. WTriplets.push_back(Triplet<double>(currRow+j,currRow+j,sudata.shapeCoeff*wShape(i)));
  129. currRow+=SC(i);
  130. }
  131. for (int i=0;i<b.size();i++)
  132. WTriplets.push_back(Triplet<double>(SC.sum()+i, SC.sum()+i, sudata.closeCoeff));
  133. for (int i=0;i<E.rows();i++)
  134. WTriplets.push_back(Triplet<double>(SC.sum()+b.size()+i, SC.sum()+b.size()+i, sudata.smoothCoeff*wSmooth(i)));
  135. sudata.W.conservativeResize(SC.sum()+b.size()+E.rows(), SC.sum()+b.size()+E.rows());
  136. sudata.W.setFromTriplets(WTriplets.begin(), WTriplets.end());
  137. sudata.At=sudata.A.transpose(); //for efficieny, as we use the transpose a lot in the iteration
  138. sudata.Q=sudata.At*sudata.W*sudata.A;
  139. return min_quad_with_fixed_precompute(sudata.Q,VectorXi(),SparseMatrix<double>(),true,sudata.solver_data);
  140. }
  141. template <
  142. typename DerivedP,
  143. typename DerivedSC,
  144. typename DerivedS>
  145. IGL_INLINE bool shapeup_solve(const Eigen::PlainObjectBase<DerivedP>& bc,
  146. const std::function<bool(const Eigen::PlainObjectBase<DerivedP>&, const Eigen::PlainObjectBase<DerivedSC>&, const Eigen::PlainObjectBase<DerivedS>&, Eigen::PlainObjectBase<DerivedP>&)>& local_projection,
  147. const Eigen::PlainObjectBase<DerivedP>& P0,
  148. const ShapeupData & sudata,
  149. const bool quietIterations,
  150. Eigen::PlainObjectBase<DerivedP>& P)
  151. {
  152. using namespace Eigen;
  153. using namespace std;
  154. MatrixXd currP=P0;
  155. MatrixXd prevP=P0;
  156. MatrixXd projP;
  157. assert(bc.rows()==sudata.b.rows());
  158. MatrixXd rhs(sudata.A.rows(), 3); rhs.setZero();
  159. rhs.block(sudata.DShape.rows(), 0, sudata.b.rows(),3)=bc; //this stays constant throughout the iterations
  160. if (!quietIterations){
  161. cout<<"Shapeup Iterations, "<<sudata.DShape.rows()<<" constraints, solution size "<<P0.size()<<endl;
  162. cout<<"**********************************************************************************************"<<endl;
  163. }
  164. projP.conservativeResize(sudata.SC.rows(), 3*sudata.SC.maxCoeff());
  165. for (int iter=0;iter<sudata.maxIterations;iter++){
  166. local_projection(currP, sudata.SC,sudata.S,projP);
  167. //constructing the projection part of the (DShape rows of the) right hand side
  168. int currRow=0;
  169. for (int i=0;i<sudata.S.rows();i++)
  170. for (int j=0;j<sudata.SC(i);j++)
  171. rhs.row(currRow++)=projP.block(i, 3*j, 1,3);
  172. Eigen::PlainObjectBase<DerivedP> lsrhs=-sudata.At*sudata.W*rhs;
  173. 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.
  174. min_quad_with_fixed_solve(sudata.solver_data, lsrhs,Y,Beq,currP);
  175. double currChange=(currP-prevP).lpNorm<Infinity>();
  176. if (!quietIterations)
  177. cout << "Iteration "<<iter<<", integration Linf error: "<<currChange<< endl;
  178. prevP=currP;
  179. if (currChange<sudata.pTolerance){
  180. P=currP;
  181. return true;
  182. }
  183. }
  184. P=currP;
  185. return false; //we went over maxIterations
  186. }
  187. }
  188. #ifdef IGL_STATIC_LIBRARY
  189. 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&);
  190. 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);
  191. #endif