EmbreeIntersector.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. double min_t = embree::zero;
  78. bool large_hits_warned = false;
  79. while(true)
  80. {
  81. #ifdef VERBOSE
  82. cout<<
  83. o[0]<<" "<<o[1]<<" "<<o[2]<<" + t*"<<
  84. d[0]<<" "<<d[1]<<" "<<d[2]<<" ---> "<<
  85. endl;
  86. #endif
  87. embree::Hit hit;
  88. embree::Ray ray(o,d,min_t);
  89. num_rays++;
  90. _intersector->intersect(ray, hit);
  91. if(hit)
  92. {
  93. // Hit self again, progressively advance
  94. if(hit.id0 == last_id0 || hit.t <= min_t)
  95. {
  96. // sanity check
  97. assert(hit.t<1);
  98. // push min_t a bit more
  99. //double t_push = pow(2.0,self_hits-4)*(hit.t<eps?eps:hit.t);
  100. double t_push = pow(2.0,self_hits)*eps;
  101. #ifdef VERBOSE
  102. cout<<" t_push: "<<t_push<<endl;
  103. #endif
  104. //o = o+t_push*d;
  105. min_t += t_push;
  106. self_hits++;
  107. }else
  108. {
  109. hits.push_back(hit);
  110. #ifdef VERBOSE
  111. cout<<" t: "<<hit.t<<endl;
  112. #endif
  113. // Instead of moving origin, just change min_t. That way calculations
  114. // all use exactly same origin values
  115. min_t = hit.t;
  116. // reset t_scale
  117. self_hits = 0;
  118. }
  119. last_id0 = hit.id0;
  120. //cout<<" id0: "<<hit.id0<<endl;
  121. }else
  122. {
  123. break;
  124. }
  125. if(hits.size()>1000 && !large_hits_warned)
  126. {
  127. cerr<<"Warning: Large number of hits..."<<endl;
  128. cerr<<"[ ";
  129. for(vector<embree::Hit>::iterator hit = hits.begin();
  130. hit != hits.end();
  131. hit++)
  132. {
  133. cerr<<(hit->id0+1)<<" ";
  134. }
  135. cerr.precision(std::numeric_limits< double >::digits10);
  136. cerr<<"[ ";
  137. for(vector<embree::Hit>::iterator hit = hits.begin();
  138. hit != hits.end();
  139. hit++)
  140. {
  141. cerr<<(hit->t)<<endl;;
  142. }
  143. cerr<<"]"<<endl;
  144. large_hits_warned = true;
  145. return hits.empty();
  146. }
  147. }
  148. return hits.empty();
  149. }
  150. template <
  151. typename PointMatrixType,
  152. typename FaceMatrixType,
  153. typename RowVector3>
  154. bool
  155. igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  156. ::intersectSegment(const RowVector3& a, const RowVector3& ab, embree::Hit &hit) const
  157. {
  158. embree::Ray ray(toVec3f(a), toVec3f(ab), embree::zero, embree::one);
  159. _intersector->intersect(ray, hit);
  160. return hit ;
  161. }
  162. #ifndef IGL_HEADER_ONLY
  163. // Explicit template instanciation
  164. #include <Eigen/Core>
  165. 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> >;
  166. #endif