EmbreeIntersector.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "EmbreeIntersector.h"
  2. #include <igl/EPS.h>
  3. template <typename RowVector3>
  4. inline embree::Vec3f toVec3f(const RowVector3 &p) { return embree::Vec3f((float)p[0], (float)p[1], (float)p[2]); }
  5. template <
  6. typename PointMatrixType,
  7. typename FaceMatrixType,
  8. typename RowVector3>
  9. igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  10. ::EmbreeIntersector(const PointMatrixType & V, const FaceMatrixType & F)
  11. {
  12. static bool inited = false;
  13. if(!inited)
  14. {
  15. //embree::TaskScheduler::start();//init();
  16. inited = true;
  17. }
  18. size_t numVertices = 0;
  19. size_t numTriangles = 0;
  20. triangles = (embree::BuildTriangle*) embree::rtcMalloc(sizeof(embree::BuildTriangle) * F.rows());
  21. vertices = (embree::BuildVertex*) embree::rtcMalloc(sizeof(embree::BuildVertex) * V.rows());
  22. for(int i = 0; i < (int)V.rows(); ++i)
  23. {
  24. vertices[numVertices++] = embree::BuildVertex((float)V(i,0),(float)V(i,1),(float)V(i,2));
  25. }
  26. for(int i = 0; i < (int)F.rows(); ++i)
  27. {
  28. triangles[numTriangles++] = embree::BuildTriangle((int)F(i,0),(int)F(i,1),(int)F(i,2),i);
  29. }
  30. _accel = embree::rtcCreateAccel("default", "default", triangles, numTriangles, vertices, numVertices);
  31. _intersector = _accel->queryInterface<embree::Intersector>();
  32. }
  33. template <
  34. typename PointMatrixType,
  35. typename FaceMatrixType,
  36. typename RowVector3>
  37. igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  38. ::~EmbreeIntersector()
  39. {
  40. embree::rtcFreeMemory();
  41. }
  42. template <
  43. typename PointMatrixType,
  44. typename FaceMatrixType,
  45. typename RowVector3>
  46. bool
  47. igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  48. ::intersectRay(const RowVector3& origin, const RowVector3& direction, embree::Hit &hit) const
  49. {
  50. embree::Ray ray(toVec3f(origin), toVec3f(direction), 1e-4f);
  51. _intersector->intersect(ray, hit);
  52. return hit ;
  53. }
  54. template <
  55. typename PointMatrixType,
  56. typename FaceMatrixType,
  57. typename RowVector3>
  58. bool
  59. igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  60. ::intersectRay(
  61. const RowVector3& origin,
  62. const RowVector3& direction,
  63. std::vector<embree::Hit > &hits) const
  64. {
  65. using namespace std;
  66. hits.clear();
  67. embree::Vec3f o = toVec3f(origin);
  68. embree::Vec3f d = toVec3f(direction);
  69. int last_id0 = -1;
  70. double self_hits = 0;
  71. const double eps = FLOAT_EPS*2.0;
  72. while(true)
  73. {
  74. //cout<<
  75. // o[0]<<" "<<o[1]<<" "<<o[2]<<" + t*"<<
  76. // d[0]<<" "<<d[1]<<" "<<d[2]<<" ---> "<<
  77. // endl;
  78. embree::Hit hit;
  79. embree::Ray ray(o,d,embree::zero);
  80. _intersector->intersect(ray, hit);
  81. if(hit)
  82. {
  83. // Hit self again
  84. if(hit.id0 == last_id0)
  85. {
  86. // sanity check
  87. assert(hit.t<1);
  88. // move off origin
  89. double t_push = pow(2.0,self_hits)*(hit.t<eps?eps:hit.t);
  90. o = o+t_push*d;
  91. self_hits++;
  92. }else
  93. {
  94. hits.push_back(hit);
  95. //cout<<" t: "<<hit.t<<endl;
  96. o = o+hit.t*d;
  97. // reset t_scale
  98. self_hits = 0;
  99. }
  100. last_id0 = hit.id0;
  101. //cout<<" id0: "<<hit.id0<<endl;
  102. }else
  103. {
  104. break;
  105. }
  106. }
  107. return hits.empty();
  108. }
  109. template <
  110. typename PointMatrixType,
  111. typename FaceMatrixType,
  112. typename RowVector3>
  113. bool
  114. igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  115. ::intersectSegment(const RowVector3& a, const RowVector3& ab, embree::Hit &hit) const
  116. {
  117. embree::Ray ray(toVec3f(a), toVec3f(ab), embree::zero, embree::one);
  118. _intersector->intersect(ray, hit);
  119. return hit ;
  120. }
  121. #ifndef IGL_HEADER_ONLY
  122. // Explicit template instanciation
  123. #include <Eigen/Core>
  124. template class igl::EmbreeIntersector<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1> >;
  125. #endif