comb_line_field.cpp 3.8 KB

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