bone_visible.cpp 4.3 KB

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