bijective_composite_harmonic_mapping.cpp 3.2 KB

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