EmbreeIntersector.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #ifndef IGL_EMBREE_INTERSECTOR_H
  2. #define IGL_EMBREE_INTERSECTOR_H
  3. #include "Embree_convenience.h"
  4. #include <vector>
  5. //#include "types.h"
  6. namespace igl
  7. {
  8. template <
  9. typename PointMatrixType,
  10. typename FaceMatrixType,
  11. typename RowVector3>
  12. class EmbreeIntersector
  13. {
  14. public:
  15. // V #V by 3 list of vertex positions
  16. // F #F by 3 list of Oriented triangles
  17. //
  18. // Note: this will only find front-facing hits. To consider all hits then
  19. // pass [F;fliplr(F)]
  20. EmbreeIntersector(const PointMatrixType & V = PointMatrixType(), const FaceMatrixType & F = FaceMatrixType());
  21. virtual ~EmbreeIntersector();
  22. // Given a ray find the first *front-facing* hit
  23. //
  24. // Inputs:
  25. // origin 3d origin point of ray
  26. // direction 3d (not necessarily normalized) direction vector of ray
  27. // Output:
  28. // hit embree information about hit
  29. // Returns true if and only if there was a hit
  30. bool intersectRay(
  31. const RowVector3& origin,
  32. const RowVector3& direction,
  33. embree::Hit &hit) const;
  34. // Given a ray find the all *front-facing* hits in order
  35. //
  36. // Inputs:
  37. // origin 3d origin point of ray
  38. // direction 3d (not necessarily normalized) direction vector of ray
  39. // Output:
  40. // hit embree information about hit
  41. // num_rays number of rays shot (at least one)
  42. // Returns true if and only if there was a hit
  43. bool intersectRay(
  44. const RowVector3& origin,
  45. const RowVector3& direction,
  46. std::vector<embree::Hit > &hits,
  47. int & num_rays) const;
  48. // Given a ray find the first *front-facing* hit
  49. //
  50. // Inputs:
  51. // a 3d first end point of segment
  52. // ab 3d vector from a to other endpoint b
  53. // Output:
  54. // hit embree information about hit
  55. // Returns true if and only if there was a hit
  56. bool intersectSegment(const RowVector3& a, const RowVector3& ab, embree::Hit &hit) const;
  57. //RowVector3 hit_position(const embree::Hit & hit) const;
  58. private:
  59. embree::Ref<embree::Accel> _accel;
  60. embree::Ref<embree::Intersector> _intersector;
  61. };
  62. }
  63. // Implementation
  64. #include <igl/EPS.h>
  65. template <typename RowVector3>
  66. inline embree::Vec3f toVec3f(const RowVector3 &p) { return embree::Vec3f((float)p[0], (float)p[1], (float)p[2]); }
  67. template <
  68. typename PointMatrixType,
  69. typename FaceMatrixType,
  70. typename RowVector3>
  71. igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  72. ::EmbreeIntersector(const PointMatrixType & V, const FaceMatrixType & F)
  73. :
  74. _accel(),
  75. _intersector()
  76. {
  77. static bool inited = false;
  78. if(!inited)
  79. {
  80. //embree::TaskScheduler::start();//init();
  81. inited = true;
  82. }
  83. if(V.size() == 0 || F.size() == 0)
  84. {
  85. return;
  86. }
  87. size_t numVertices = 0;
  88. size_t numTriangles = 0;
  89. embree::BuildTriangle *triangles;
  90. embree::BuildVertex *vertices;
  91. triangles = (embree::BuildTriangle*) embree::rtcMalloc(sizeof(embree::BuildTriangle) * F.rows());
  92. vertices = (embree::BuildVertex*) embree::rtcMalloc(sizeof(embree::BuildVertex) * V.rows());
  93. for(int i = 0; i < (int)V.rows(); ++i)
  94. {
  95. vertices[numVertices++] = embree::BuildVertex((float)V(i,0),(float)V(i,1),(float)V(i,2));
  96. }
  97. for(int i = 0; i < (int)F.rows(); ++i)
  98. {
  99. triangles[numTriangles++] = embree::BuildTriangle((int)F(i,0),(int)F(i,1),(int)F(i,2),i);
  100. }
  101. _accel = embree::rtcCreateAccel("default", "default", triangles, numTriangles, vertices, numVertices);
  102. _intersector = _accel->queryInterface<embree::Intersector>();
  103. }
  104. template <
  105. typename PointMatrixType,
  106. typename FaceMatrixType,
  107. typename RowVector3>
  108. igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  109. ::~EmbreeIntersector()
  110. {
  111. embree::rtcFreeMemory();
  112. }
  113. template <
  114. typename PointMatrixType,
  115. typename FaceMatrixType,
  116. typename RowVector3>
  117. bool
  118. igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  119. ::intersectRay(const RowVector3& origin, const RowVector3& direction, embree::Hit &hit) const
  120. {
  121. embree::Ray ray(toVec3f(origin), toVec3f(direction), 1e-4f);
  122. _intersector->intersect(ray, hit);
  123. return hit ;
  124. }
  125. template <
  126. typename PointMatrixType,
  127. typename FaceMatrixType,
  128. typename RowVector3>
  129. bool
  130. igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  131. ::intersectRay(
  132. const RowVector3& origin,
  133. const RowVector3& direction,
  134. std::vector<embree::Hit > &hits,
  135. int & num_rays) const
  136. {
  137. using namespace std;
  138. num_rays = 0;
  139. hits.clear();
  140. embree::Vec3f o = toVec3f(origin);
  141. embree::Vec3f d = toVec3f(direction);
  142. int last_id0 = -1;
  143. double self_hits = 0;
  144. // This epsilon is directly correleated to the number of missed hits, smaller
  145. // means more accurate and slower
  146. //const double eps = DOUBLE_EPS;
  147. const double eps = FLOAT_EPS;
  148. double min_t = embree::zero;
  149. bool large_hits_warned = false;
  150. while(true)
  151. {
  152. #ifdef VERBOSE
  153. cout<<
  154. o[0]<<" "<<o[1]<<" "<<o[2]<<" + t*"<<
  155. d[0]<<" "<<d[1]<<" "<<d[2]<<" ---> "<<
  156. endl;
  157. #endif
  158. embree::Hit hit;
  159. embree::Ray ray(o,d,min_t);
  160. num_rays++;
  161. _intersector->intersect(ray, hit);
  162. if(hit)
  163. {
  164. // Hit self again, progressively advance
  165. if(hit.id0 == last_id0 || hit.t <= min_t)
  166. {
  167. // sanity check
  168. assert(hit.t<1);
  169. // push min_t a bit more
  170. //double t_push = pow(2.0,self_hits-4)*(hit.t<eps?eps:hit.t);
  171. double t_push = pow(2.0,self_hits)*eps;
  172. #ifdef VERBOSE
  173. cout<<" t_push: "<<t_push<<endl;
  174. #endif
  175. //o = o+t_push*d;
  176. min_t += t_push;
  177. self_hits++;
  178. }else
  179. {
  180. hits.push_back(hit);
  181. #ifdef VERBOSE
  182. cout<<" t: "<<hit.t<<endl;
  183. #endif
  184. // Instead of moving origin, just change min_t. That way calculations
  185. // all use exactly same origin values
  186. min_t = hit.t;
  187. // reset t_scale
  188. self_hits = 0;
  189. }
  190. last_id0 = hit.id0;
  191. //cout<<" id0: "<<hit.id0<<endl;
  192. }else
  193. {
  194. break;
  195. }
  196. if(hits.size()>1000 && !large_hits_warned)
  197. {
  198. cerr<<"Warning: Large number of hits..."<<endl;
  199. cerr<<"[ ";
  200. for(vector<embree::Hit>::iterator hit = hits.begin();
  201. hit != hits.end();
  202. hit++)
  203. {
  204. cerr<<(hit->id0+1)<<" ";
  205. }
  206. cerr.precision(std::numeric_limits< double >::digits10);
  207. cerr<<"[ ";
  208. for(vector<embree::Hit>::iterator hit = hits.begin();
  209. hit != hits.end();
  210. hit++)
  211. {
  212. cerr<<(hit->t)<<endl;;
  213. }
  214. cerr<<"]"<<endl;
  215. large_hits_warned = true;
  216. return hits.empty();
  217. }
  218. }
  219. return hits.empty();
  220. }
  221. template <
  222. typename PointMatrixType,
  223. typename FaceMatrixType,
  224. typename RowVector3>
  225. bool
  226. igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  227. ::intersectSegment(const RowVector3& a, const RowVector3& ab, embree::Hit &hit) const
  228. {
  229. embree::Ray ray(toVec3f(a), toVec3f(ab), embree::zero, embree::one);
  230. _intersector->intersect(ray, hit);
  231. return hit ;
  232. }
  233. //template <
  234. //typename PointMatrixType,
  235. //typename FaceMatrixType,
  236. //typename RowVector3>
  237. //RowVector3
  238. //igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  239. //::hit_position(const embree::Hit & hit) const
  240. //{
  241. // // Barycenteric coordinates
  242. // const double w0 = (1.0-hit.u-hit.v);
  243. // const double w1 = hit.u;
  244. // const double w2 = hit.v;
  245. // RowVector3 hitP;
  246. // hitP.resize(3);
  247. // for(int d = 0;d<3;d++)
  248. // {
  249. // hitP(d) =
  250. // w0 * vertices[triangles[hit.id0](0)](d) +
  251. // w1 * vertices[triangles[hit.id0](1)](d) +
  252. // w2 * vertices[triangles[hit.id0](2)](d);
  253. // }
  254. // return hitP;
  255. //}
  256. #endif //EMBREE_INTERSECTOR_H