bijective_composite_harmonic_mapping.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2017 Alec Jacobson <alecjacobson@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 "bijective_composite_harmonic_mapping.h"
  9. #include "slice.h"
  10. #include "doublearea.h"
  11. #include "harmonic.h"
  12. //#include "matlab/MatlabWorkspace.h"
  13. template <
  14. typename DerivedV,
  15. typename DerivedF,
  16. typename Derivedb,
  17. typename Derivedbc,
  18. typename DerivedU>
  19. IGL_INLINE bool igl::bijective_composite_harmonic_mapping(
  20. const Eigen::MatrixBase<DerivedV> & V,
  21. const Eigen::MatrixBase<DerivedF> & F,
  22. const Eigen::MatrixBase<Derivedb> & b,
  23. const Eigen::MatrixBase<Derivedbc> & bc,
  24. Eigen::PlainObjectBase<DerivedU> & U)
  25. {
  26. typedef typename Derivedbc::Scalar Scalar;
  27. assert(V.cols() == 2 && bc.cols() == 2 && "Input should be 2D");
  28. assert(F.cols() == 3 && "F should contain triangles");
  29. int tries = 0;
  30. const int min_steps = 1;
  31. const int max_steps = 64;
  32. int nsteps = min_steps;
  33. Derivedbc bc0;
  34. slice(V,b,1,bc0);
  35. // It's difficult to check for flips "robustly" in the sense that the input
  36. // mesh might not have positive/consistent sign to begin with.
  37. while(nsteps<=max_steps)
  38. {
  39. U = V;
  40. int flipped = 0;
  41. int nans = 0;
  42. int step = 0;
  43. for(;step<=nsteps;step++)
  44. {
  45. const Scalar t = ((Scalar)step)/((Scalar)nsteps);
  46. // linearly interpolate boundary conditions
  47. // TODO: replace this with something that guarantees a homotopic "morph"
  48. // of the boundary conditions. Something like "Homotopic Morphing of
  49. // Planar Curves" [Dym et al. 2015] but also handling multiple connected
  50. // components.
  51. Derivedbc bct = bc0 + t*(bc - bc0);
  52. // Compute dsicrete harmonic map using metric of previous step
  53. const int ninnersteps = 8;
  54. for(int iter = 0;iter<8;iter++)
  55. {
  56. //std::cout<<nsteps<<" t: "<<t<<" iter: "<<iter;
  57. //igl::matlab::MatlabWorkspace mw;
  58. //mw.save(U,"U");
  59. //mw.save_index(F,"F");
  60. //mw.save_index(b,"b");
  61. //mw.save(bct,"bct");
  62. //mw.write("numerical.mat");
  63. harmonic(DerivedU(U),F,b,bct,1,U);
  64. Eigen::Matrix<Scalar,Eigen::Dynamic,1> A;
  65. doublearea(U,F,A);
  66. flipped = (A.array() < 0 ).count();
  67. //std::cout<<" "<<flipped<<" nan? "<<(U.array() != U.array()).any()<<std::endl;
  68. nans = (U.array() != U.array()).count();
  69. if(flipped == 0 && nans == 0) break;
  70. igl::slice(U,b,1,bct);
  71. }
  72. if(flipped > 0 || nans>0) break;
  73. }
  74. if(flipped == 0 && nans == 0)
  75. {
  76. return step == nsteps+1;
  77. }
  78. nsteps *= 2;
  79. }
  80. return false;
  81. }
  82. #ifdef IGL_STATIC_LIBRARY
  83. // Explicit template instantiation
  84. // generated by autoexplicit.sh
  85. template bool igl::bijective_composite_harmonic_mapping<Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 1, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> >&);
  86. #endif