shapeup.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #ifndef IGL_SHAPEUP_H
  9. #define IGL_SHAPEUP_H
  10. #include <igl/min_quad_with_fixed.h>
  11. #include <igl/igl_inline.h>
  12. #include <igl/setdiff.h>
  13. #include <igl/cat.h>
  14. #include <Eigen/Core>
  15. #include <vector>
  16. #include <igl/PI.h>
  17. //This file implements the following algorithm:
  18. //Boaziz et al.
  19. //Shape-Up: Shaping Discrete Geometry with Projections
  20. //Computer Graphics Forum (Proc. SGP) 31(5), 2012
  21. namespace igl
  22. {
  23. struct ShapeupData{
  24. //input data
  25. Eigen::MatrixXd P;
  26. Eigen::VectorXi SC;
  27. Eigen::MatrixXi S;
  28. Eigen::VectorXi b;
  29. int maxIterations; //referring to number of local-global pairs.
  30. double pTolerance; //algorithm stops when max(|P_k-P_{k-1}|)<pTolerance.
  31. double shapeCoeff, closeCoeff, smoothCoeff;
  32. //Internally-used matrices
  33. Eigen::SparseMatrix<double> DShape, DClose, DSmooth, Q, A, At, W;
  34. min_quad_with_fixed_data<double> solver_data;
  35. ShapeupData():
  36. maxIterations(50),
  37. pTolerance(10e-6),
  38. shapeCoeff(1.0),
  39. closeCoeff(100.0),
  40. smoothCoeff(0.0){}
  41. };
  42. //Every function here defines a local projection for ShapeUp, and must have the following structure to qualify:
  43. //Input:
  44. // P #P by 3 the set of points, either the initial solution, or from previous iteration.
  45. // SC #Set by 1 cardinalities of sets in S
  46. // S #Sets by max(SC) independent sets where the local projection applies. Values beyond column SC(i)-1 in row S(i,:) are "don't care"
  47. //Output:
  48. // projP #S by 3*max(SC) in format xyzxyzxyz, where the projected points correspond to each set in S in the same order.
  49. typedef std::function<bool(const Eigen::PlainObjectBase<Eigen::MatrixXd>&, const Eigen::PlainObjectBase<Eigen::VectorXi>&, const Eigen::PlainObjectBase<Eigen::MatrixXi>&, Eigen::PlainObjectBase<Eigen::MatrixXd>&)> shapeup_projection_function;
  50. //This projection does nothing but render points into projP. Mostly used for "echoing" the global step
  51. 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);
  52. //the projection assumes that the sets are vertices of polygons in cyclic order
  53. 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);
  54. //This function precomputation the necessary matrices for the ShapeUp process, and prefactorizes them.
  55. //input:
  56. // P #P by 3 point positions
  57. // SC #Set by 1 cardinalities of sets in S
  58. // S #Sets by max(SC) independent sets where the local projection applies. Values beyond column SC(i)-1 in row S(i,:) are "don't care"
  59. // E #E by 2 the "edges" of the set P; used for the smoothness energy.
  60. // b #b by 1 boundary (fixed) vertices from P.
  61. // wShape, #Set by 1
  62. // wSmooth #b by 1 weights for constraints from S and positional constraints (used in the global step)
  63. // Output:
  64. // sudata struct ShapeupData the data necessary to solve the system in shapeup_solve
  65. template <
  66. typename DerivedP,
  67. typename DerivedSC,
  68. typename DerivedS,
  69. typename Derivedw>
  70. IGL_INLINE bool shapeup_precomputation(const Eigen::PlainObjectBase<DerivedP>& P,
  71. const Eigen::PlainObjectBase<DerivedSC>& SC,
  72. const Eigen::PlainObjectBase<DerivedS>& S,
  73. const Eigen::PlainObjectBase<DerivedS>& E,
  74. const Eigen::PlainObjectBase<DerivedSC>& b,
  75. const Eigen::PlainObjectBase<Derivedw>& wShape,
  76. const Eigen::PlainObjectBase<Derivedw>& wSmooth,
  77. ShapeupData & sudata);
  78. //This function solve the shapeup project optimization. shapeup_precompute must be called before with the same sudata, or results are unpredictable
  79. //Input:
  80. //bc #b by 3 fixed point values corresonding to "b" in sudata
  81. //local_projection function pointer taking (P,SC,S,projP),
  82. // where the first three parameters are as defined, and "projP" is the output, as a #S by 3*max(SC) function in format xyzxyzxyz, and where it returns the projected points corresponding to each set in S in the same order.
  83. //NOTE: the input values in P0 don't need to correspond to prescribed values in bc; the iterations will project them automatically (by design).
  84. //P0 #P by 3 initial solution (point positions)
  85. //sudata the ShapeUpData structure computed in shapeup_precomputation()
  86. //quietIterations flagging if to output iteration information.
  87. //Output:
  88. //P the solution to the problem, indices corresponding to P0.
  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 quietIterations,
  98. Eigen::PlainObjectBase<DerivedP>& P);
  99. }
  100. #ifndef IGL_STATIC_LIBRARY
  101. #include "shapeup.cpp"
  102. #endif
  103. #endif