bone_visible.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // Initialize intersector
  23. const EmbreeIntersector<MatrixXd,MatrixXi,Vector3d> ei =
  24. EmbreeIntersector<MatrixXd,MatrixXi,Vector3d>(V,F);
  25. const double sd_norm = (s-d).norm();
  26. // loop over mesh vertices
  27. // TODO: openmp?
  28. for(int v = 0;v<V.rows();v++)
  29. {
  30. Vector3d Vv = V.row(v);
  31. // Project vertex v onto line segment sd
  32. //embree.intersectSegment
  33. double t,sqrd;
  34. Vector3d projv;
  35. // degenerate bone, just snap to s
  36. if(sd_norm < DOUBLE_EPS)
  37. {
  38. t = 0;
  39. sqrd = (Vv-s).array().pow(2).sum();
  40. projv = s;
  41. }else
  42. {
  43. // project onto (infinite) line
  44. project_to_line(
  45. Vv(0),Vv(1),Vv(2),s(0),s(1),s(2),d(0),d(1),d(2),
  46. projv(0),projv(1),projv(2),t,sqrd);
  47. // handle projections past endpoints
  48. if(t<0)
  49. {
  50. t = 0;
  51. sqrd = (Vv-s).array().pow(2).sum();
  52. projv = s;
  53. } else if(t>1)
  54. {
  55. t = 1;
  56. sqrd = (Vv-d).array().pow(2).sum();
  57. projv = d;
  58. }
  59. }
  60. embree::Hit hit;
  61. // perhaps 1.0 should be 1.0-epsilon, or actually since we checking the
  62. // incident face, perhaps 1.0 should be 1.0+eps
  63. const Vector3d dir = (Vv-projv)*1.0;
  64. if(ei.intersectSegment(projv,dir, hit))
  65. {
  66. //if(v == 1228-1)
  67. //{
  68. // int fi = hit.id0;
  69. // Vector3d bc,P;
  70. // bc << 1 - hit.u - hit.v, hit.u, hit.v; // barycentric
  71. // P = V.row(F(fi,0))*bc(0) +
  72. // V.row(F(fi,1))*bc(1) +
  73. // V.row(F(fi,2))*bc(2);
  74. // cout<<(fi+1)<<endl;
  75. // cout<<bc.transpose()<<endl;
  76. // cout<<P.transpose()<<endl;
  77. // cout<<Vv.transpose()<<endl;
  78. //}
  79. const int fi = hit.id0; // face id
  80. // Assume hit is valid, so not visible
  81. flag(v) = false;
  82. // loop around corners of triangle
  83. for(int c = 0;c<F.cols();c++)
  84. {
  85. if(F(fi,c) == v)
  86. {
  87. // hit self, so no hits before, so vertex v is visible
  88. flag(v) = true;
  89. break;
  90. }
  91. }
  92. // Hit is actually past v
  93. if(!flag(v) && (hit.t*hit.t*dir.squaredNorm())>sqrd)
  94. {
  95. flag(v) = true;
  96. }
  97. }else
  98. {
  99. // no hit so vectex v is visible
  100. flag(v) = true;
  101. }
  102. }
  103. }
  104. #ifndef IGL_HEADER_ONLY
  105. // Explicit template instanciation
  106. 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> >&);
  107. 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> >&);
  108. #endif