bone_visible.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "bone_visible.h"
  2. #include "EmbreeIntersector.h"
  3. #include <igl/project_to_line.h>
  4. #include <igl/EPS.h>
  5. #include <iostream>
  6. template <
  7. typename DerivedV,
  8. typename DerivedF,
  9. typename DerivedSD,
  10. typename Derivedflag>
  11. void bone_visible(
  12. const Eigen::PlainObjectBase<DerivedV> & V,
  13. const Eigen::PlainObjectBase<DerivedF> & F,
  14. const Eigen::PlainObjectBase<DerivedSD> & s,
  15. const Eigen::PlainObjectBase<DerivedSD> & d,
  16. Eigen::PlainObjectBase<Derivedflag> & flag)
  17. {
  18. using namespace igl;
  19. using namespace std;
  20. using namespace Eigen;
  21. flag.resize(V.rows());
  22. // "double sided lighting"
  23. Eigen::PlainObjectBase<DerivedF> FF;
  24. FF.resize(F.rows()*2,F.cols());
  25. FF << F, F.rowwise().reverse();
  26. // Initialize intersector
  27. const EmbreeIntersector<MatrixXd,MatrixXi,Vector3d> ei =
  28. EmbreeIntersector<MatrixXd,MatrixXi,Vector3d>(V,FF);
  29. const double sd_norm = (s-d).norm();
  30. // loop over mesh vertices
  31. // TODO: openmp?
  32. for(int v = 0;v<V.rows();v++)
  33. {
  34. Vector3d Vv = V.row(v);
  35. // Project vertex v onto line segment sd
  36. //embree.intersectSegment
  37. double t,sqrd;
  38. Vector3d projv;
  39. // degenerate bone, just snap to s
  40. if(sd_norm < DOUBLE_EPS)
  41. {
  42. t = 0;
  43. sqrd = (Vv-s).array().pow(2).sum();
  44. projv = s;
  45. }else
  46. {
  47. // project onto (infinite) line
  48. project_to_line(
  49. Vv(0),Vv(1),Vv(2),s(0),s(1),s(2),d(0),d(1),d(2),
  50. projv(0),projv(1),projv(2),t,sqrd);
  51. // handle projections past endpoints
  52. if(t<0)
  53. {
  54. t = 0;
  55. sqrd = (Vv-s).array().pow(2).sum();
  56. projv = s;
  57. } else if(t>1)
  58. {
  59. t = 1;
  60. sqrd = (Vv-d).array().pow(2).sum();
  61. projv = d;
  62. }
  63. }
  64. embree::Hit hit;
  65. // perhaps 1.0 should be 1.0-epsilon, or actually since we checking the
  66. // incident face, perhaps 1.0 should be 1.0+eps
  67. const Vector3d dir = (Vv-projv)*1.0;
  68. if(ei.intersectSegment(projv,dir, hit))
  69. {
  70. // mod for double sided lighting
  71. const int fi = hit.id0 % F.rows();
  72. //if(v == 1228-1)
  73. //{
  74. // Vector3d bc,P;
  75. // bc << 1 - hit.u - hit.v, hit.u, hit.v; // barycentric
  76. // P = V.row(F(fi,0))*bc(0) +
  77. // V.row(F(fi,1))*bc(1) +
  78. // V.row(F(fi,2))*bc(2);
  79. // cout<<(fi+1)<<endl;
  80. // cout<<bc.transpose()<<endl;
  81. // cout<<P.transpose()<<endl;
  82. // cout<<hit.t<<endl;
  83. // cout<<(projv + dir*hit.t).transpose()<<endl;
  84. // cout<<Vv.transpose()<<endl;
  85. //}
  86. // Assume hit is valid, so not visible
  87. flag(v) = false;
  88. // loop around corners of triangle
  89. for(int c = 0;c<F.cols();c++)
  90. {
  91. if(F(fi,c) == v)
  92. {
  93. // hit self, so no hits before, so vertex v is visible
  94. flag(v) = true;
  95. break;
  96. }
  97. }
  98. // Hit is actually past v
  99. if(!flag(v) && (hit.t*hit.t*dir.squaredNorm())>sqrd)
  100. {
  101. flag(v) = true;
  102. }
  103. }else
  104. {
  105. // no hit so vectex v is visible
  106. flag(v) = true;
  107. }
  108. }
  109. }
  110. #ifndef IGL_HEADER_ONLY
  111. // Explicit template instanciation
  112. template void bone_visible<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::Matrix<bool, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> >&);
  113. template void bone_visible<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<bool, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> >&);
  114. #endif