bone_visible.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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<double,int> ei;
  29. ei.init(V,FF);
  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(projv,dir, hit))
  71. {
  72. // mod for double sided lighting
  73. const int fi = hit.id % F.rows();
  74. //if(v == 1228-1)
  75. //{
  76. // Vector3d bc,P;
  77. // bc << 1 - hit.u - hit.v, hit.u, hit.v; // barycentric
  78. // P = V.row(F(fi,0))*bc(0) +
  79. // V.row(F(fi,1))*bc(1) +
  80. // V.row(F(fi,2))*bc(2);
  81. // cout<<(fi+1)<<endl;
  82. // cout<<bc.transpose()<<endl;
  83. // cout<<P.transpose()<<endl;
  84. // cout<<hit.t<<endl;
  85. // cout<<(projv + dir*hit.t).transpose()<<endl;
  86. // cout<<Vv.transpose()<<endl;
  87. //}
  88. // Assume hit is valid, so not visible
  89. flag(v) = false;
  90. // loop around corners of triangle
  91. for(int c = 0;c<F.cols();c++)
  92. {
  93. if(F(fi,c) == v)
  94. {
  95. // hit self, so no hits before, so vertex v is visible
  96. flag(v) = true;
  97. break;
  98. }
  99. }
  100. // Hit is actually past v
  101. if(!flag(v) && (hit.t*hit.t*dir.squaredNorm())>sqrd)
  102. {
  103. flag(v) = true;
  104. }
  105. }else
  106. {
  107. // no hit so vectex v is visible
  108. flag(v) = true;
  109. }
  110. }
  111. }
  112. #ifndef IGL_HEADER_ONLY
  113. // Explicit template instanciation
  114. 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> >&);
  115. 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> >&);
  116. #endif