comb_line_field.cpp 3.8 KB

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