shapeup_local_projections.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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_local_projections.h>
  9. #include <igl/igl_inline.h>
  10. #include <igl/setdiff.h>
  11. #include <igl/cat.h>
  12. #include <Eigen/Core>
  13. #include <vector>
  14. //This file implements several basic local projection functions for the shapeup algorithm in shapeup.h
  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. }