comb_cross_field.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <[email protected]>, Olga Diamanti <[email protected]>
  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 "comb_cross_field.h"
  9. #include <vector>
  10. #include <deque>
  11. #include <Eigen/Geometry>
  12. #include "per_face_normals.h"
  13. #include "is_border_vertex.h"
  14. #include "rotation_matrix_from_directions.h"
  15. #include "PlainMatrix.h"
  16. #include "triangle_triangle_adjacency.h"
  17. namespace igl {
  18. template <typename DerivedV, typename DerivedF>
  19. class Comb
  20. {
  21. public:
  22. const Eigen::MatrixBase<DerivedV> &V;
  23. const Eigen::MatrixBase<DerivedF> &F;
  24. const Eigen::MatrixBase<DerivedV> &PD1;
  25. const Eigen::MatrixBase<DerivedV> &PD2;
  26. PlainMatrix<DerivedV> N;
  27. private:
  28. // internal
  29. PlainMatrix<DerivedF> TT;
  30. PlainMatrix<DerivedF> TTi;
  31. private:
  32. static inline double Sign(double a){return (double)((a>0)?+1:-1);}
  33. private:
  34. // returns the 90 deg rotation of a (around n) most similar to target b
  35. /// a and b should be in the same plane orthogonal to N
  36. static inline Eigen::Matrix<typename DerivedV::Scalar, 3, 1> K_PI_new(const Eigen::Matrix<typename DerivedV::Scalar, 3, 1>& a,
  37. const Eigen::Matrix<typename DerivedV::Scalar, 3, 1>& b,
  38. const Eigen::Matrix<typename DerivedV::Scalar, 3, 1>& n)
  39. {
  40. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> c = (a.cross(n)).normalized();
  41. typename DerivedV::Scalar scorea = a.dot(b);
  42. typename DerivedV::Scalar scorec = c.dot(b);
  43. if (fabs(scorea)>=fabs(scorec))
  44. return a*Sign(scorea);
  45. else
  46. return c*Sign(scorec);
  47. }
  48. public:
  49. inline Comb(const Eigen::MatrixBase<DerivedV> &_V,
  50. const Eigen::MatrixBase<DerivedF> &_F,
  51. const Eigen::MatrixBase<DerivedV> &_PD1,
  52. const Eigen::MatrixBase<DerivedV> &_PD2
  53. ):
  54. V(_V),
  55. F(_F),
  56. PD1(_PD1),
  57. PD2(_PD2)
  58. {
  59. igl::per_face_normals(V,F,N);
  60. igl::triangle_triangle_adjacency(F,TT,TTi);
  61. }
  62. inline void comb(Eigen::PlainObjectBase<DerivedV> &PD1out,
  63. Eigen::PlainObjectBase<DerivedV> &PD2out)
  64. {
  65. // PD1out = PD1;
  66. // PD2out = PD2;
  67. PD1out.setZero(F.rows(),3);PD1out<<PD1;
  68. PD2out.setZero(F.rows(),3);PD2out<<PD2;
  69. Eigen::VectorXi mark = Eigen::VectorXi::Constant(F.rows(),false);
  70. std::deque<int> d;
  71. while (!mark.all()) // Stop until all vertices are marked
  72. {
  73. int unmarked = 0;
  74. while (mark(unmarked))
  75. unmarked++;
  76. d.push_back(unmarked);
  77. mark(unmarked) = true;
  78. while (!d.empty())
  79. {
  80. int f0 = d.at(0);
  81. d.pop_front();
  82. for (int k=0; k<3; k++)
  83. {
  84. int f1 = TT(f0,k);
  85. if (f1==-1) continue;
  86. if (mark(f1)) continue;
  87. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> dir0 = PD1out.row(f0);
  88. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> dir1 = PD1out.row(f1);
  89. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> n0 = N.row(f0);
  90. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> n1 = N.row(f1);
  91. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> dir0Rot = igl::rotation_matrix_from_directions(n0, n1)*dir0;
  92. dir0Rot.normalize();
  93. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> targD = K_PI_new(dir1,dir0Rot,n1);
  94. PD1out.row(f1) = targD;
  95. PD2out.row(f1) = n1.cross(targD).normalized();
  96. mark(f1) = true;
  97. d.push_back(f1);
  98. }
  99. }
  100. }
  101. // everything should be marked
  102. for (int i=0; i<F.rows(); i++)
  103. {
  104. assert(mark(i));
  105. }
  106. }
  107. };
  108. }
  109. template <typename DerivedV, typename DerivedF>
  110. IGL_INLINE void igl::comb_cross_field(const Eigen::MatrixBase<DerivedV> &V,
  111. const Eigen::MatrixBase<DerivedF> &F,
  112. const Eigen::MatrixBase<DerivedV> &PD1,
  113. const Eigen::MatrixBase<DerivedV> &PD2,
  114. Eigen::PlainObjectBase<DerivedV> &PD1out,
  115. Eigen::PlainObjectBase<DerivedV> &PD2out)
  116. {
  117. igl::Comb<DerivedV, DerivedF> cmb(V, F, PD1, PD2);
  118. cmb.comb(PD1out, PD2out);
  119. }
  120. #ifdef IGL_STATIC_LIBRARY
  121. // Explicit template instantiation
  122. template void igl::comb_cross_field<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&);
  123. template void igl::comb_cross_field<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -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::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  124. #endif