EmbreeIntersector.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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,
  64. int & num_rays) const
  65. {
  66. using namespace std;
  67. num_rays = 0;
  68. hits.clear();
  69. embree::Vec3f o = toVec3f(origin);
  70. embree::Vec3f d = toVec3f(direction);
  71. int last_id0 = -1;
  72. double self_hits = 0;
  73. // This epsilon is directly correleated to the number of missed hits, smaller
  74. // means more accurate and slower
  75. //const double eps = DOUBLE_EPS;
  76. const double eps = FLOAT_EPS;
  77. while(true)
  78. {
  79. #ifdef VERBOSE
  80. cout<<
  81. o[0]<<" "<<o[1]<<" "<<o[2]<<" + t*"<<
  82. d[0]<<" "<<d[1]<<" "<<d[2]<<" ---> "<<
  83. endl;
  84. #endif
  85. embree::Hit hit;
  86. embree::Ray ray(o,d,embree::zero);
  87. num_rays++;
  88. _intersector->intersect(ray, hit);
  89. if(hit)
  90. {
  91. // Hit self again, progressively advance
  92. if(hit.id0 == last_id0)
  93. {
  94. // sanity check
  95. assert(hit.t<1);
  96. // move off origin
  97. //double t_push = pow(2.0,self_hits-4)*(hit.t<eps?eps:hit.t);
  98. double t_push = pow(2.0,self_hits)*eps;
  99. #ifdef VERBOSE
  100. cout<<" t_push: "<<t_push<<endl;
  101. #endif
  102. o = o+t_push*d;
  103. self_hits++;
  104. }else
  105. {
  106. hits.push_back(hit);
  107. #ifdef VERBOSE
  108. cout<<" t: "<<hit.t<<endl;
  109. #endif
  110. o = o+hit.t*d;
  111. // reset t_scale
  112. self_hits = 0;
  113. }
  114. last_id0 = hit.id0;
  115. //cout<<" id0: "<<hit.id0<<endl;
  116. }else
  117. {
  118. break;
  119. }
  120. }
  121. return hits.empty();
  122. }
  123. template <
  124. typename PointMatrixType,
  125. typename FaceMatrixType,
  126. typename RowVector3>
  127. bool
  128. igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  129. ::intersectSegment(const RowVector3& a, const RowVector3& ab, embree::Hit &hit) const
  130. {
  131. embree::Ray ray(toVec3f(a), toVec3f(ab), embree::zero, embree::one);
  132. _intersector->intersect(ray, hit);
  133. return hit ;
  134. }
  135. #ifndef IGL_HEADER_ONLY
  136. // Explicit template instanciation
  137. #include <Eigen/Core>
  138. 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> >;
  139. #endif