EmbreeIntersector.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #include <iostream>
  66. #include <sstream>
  67. template <typename RowVector3>
  68. inline embree::Vec3f toVec3f(const RowVector3 &p) { return embree::Vec3f((float)p[0], (float)p[1], (float)p[2]); }
  69. template <
  70. typename PointMatrixType,
  71. typename FaceMatrixType,
  72. typename RowVector3>
  73. igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  74. ::EmbreeIntersector(const PointMatrixType & V, const FaceMatrixType & F)
  75. :
  76. _accel(),
  77. _intersector()
  78. {
  79. using namespace std;
  80. static bool inited = false;
  81. if(!inited)
  82. {
  83. //embree::TaskScheduler::start();//init();
  84. inited = true;
  85. }
  86. if(V.size() == 0 || F.size() == 0)
  87. {
  88. return;
  89. }
  90. size_t numVertices = 0;
  91. size_t numTriangles = 0;
  92. embree::BuildTriangle *triangles;
  93. embree::BuildVertex *vertices;
  94. triangles = (embree::BuildTriangle*) embree::rtcMalloc(sizeof(embree::BuildTriangle) * F.rows());
  95. vertices = (embree::BuildVertex*) embree::rtcMalloc(sizeof(embree::BuildVertex) * V.rows());
  96. for(int i = 0; i < (int)V.rows(); ++i)
  97. {
  98. vertices[numVertices++] = embree::BuildVertex((float)V(i,0),(float)V(i,1),(float)V(i,2));
  99. }
  100. for(int i = 0; i < (int)F.rows(); ++i)
  101. {
  102. triangles[numTriangles++] = embree::BuildTriangle((int)F(i,0),(int)F(i,1),(int)F(i,2),i);
  103. }
  104. // turn off verbose output by disabling cout
  105. // http://stackoverflow.com/a/8246536/148668
  106. streambuf *old = cout.rdbuf(); // <-- save
  107. stringstream ss;
  108. cout.rdbuf (ss.rdbuf()); // <-- redirect
  109. _accel = embree::rtcCreateAccel("default", "default", triangles, numTriangles, vertices, numVertices);
  110. _intersector = _accel->queryInterface<embree::Intersector>();
  111. cout.rdbuf (old);
  112. }
  113. template <
  114. typename PointMatrixType,
  115. typename FaceMatrixType,
  116. typename RowVector3>
  117. igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  118. ::~EmbreeIntersector()
  119. {
  120. embree::rtcFreeMemory();
  121. }
  122. template <
  123. typename PointMatrixType,
  124. typename FaceMatrixType,
  125. typename RowVector3>
  126. bool
  127. igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  128. ::intersectRay(const RowVector3& origin, const RowVector3& direction, embree::Hit &hit) const
  129. {
  130. embree::Ray ray(toVec3f(origin), toVec3f(direction), 1e-4f);
  131. _intersector->intersect(ray, hit);
  132. return hit ;
  133. }
  134. template <
  135. typename PointMatrixType,
  136. typename FaceMatrixType,
  137. typename RowVector3>
  138. bool
  139. igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  140. ::intersectRay(
  141. const RowVector3& origin,
  142. const RowVector3& direction,
  143. std::vector<embree::Hit > &hits,
  144. int & num_rays) const
  145. {
  146. using namespace std;
  147. num_rays = 0;
  148. hits.clear();
  149. embree::Vec3f o = toVec3f(origin);
  150. embree::Vec3f d = toVec3f(direction);
  151. int last_id0 = -1;
  152. double self_hits = 0;
  153. // This epsilon is directly correleated to the number of missed hits, smaller
  154. // means more accurate and slower
  155. //const double eps = DOUBLE_EPS;
  156. const double eps = FLOAT_EPS;
  157. double min_t = embree::zero;
  158. bool large_hits_warned = false;
  159. while(true)
  160. {
  161. #ifdef VERBOSE
  162. cout<<
  163. o[0]<<" "<<o[1]<<" "<<o[2]<<" + t*"<<
  164. d[0]<<" "<<d[1]<<" "<<d[2]<<" ---> "<<
  165. endl;
  166. #endif
  167. embree::Hit hit;
  168. embree::Ray ray(o,d,min_t);
  169. num_rays++;
  170. _intersector->intersect(ray, hit);
  171. if(hit)
  172. {
  173. // Hit self again, progressively advance
  174. if(hit.id0 == last_id0 || hit.t <= min_t)
  175. {
  176. // sanity check
  177. assert(hit.t<1);
  178. // push min_t a bit more
  179. //double t_push = pow(2.0,self_hits-4)*(hit.t<eps?eps:hit.t);
  180. double t_push = pow(2.0,self_hits)*eps;
  181. #ifdef VERBOSE
  182. cout<<" t_push: "<<t_push<<endl;
  183. #endif
  184. //o = o+t_push*d;
  185. min_t += t_push;
  186. self_hits++;
  187. }else
  188. {
  189. hits.push_back(hit);
  190. #ifdef VERBOSE
  191. cout<<" t: "<<hit.t<<endl;
  192. #endif
  193. // Instead of moving origin, just change min_t. That way calculations
  194. // all use exactly same origin values
  195. min_t = hit.t;
  196. // reset t_scale
  197. self_hits = 0;
  198. }
  199. last_id0 = hit.id0;
  200. //cout<<" id0: "<<hit.id0<<endl;
  201. }else
  202. {
  203. break;
  204. }
  205. if(hits.size()>1000 && !large_hits_warned)
  206. {
  207. cerr<<"Warning: Large number of hits..."<<endl;
  208. cerr<<"[ ";
  209. for(vector<embree::Hit>::iterator hit = hits.begin();
  210. hit != hits.end();
  211. hit++)
  212. {
  213. cerr<<(hit->id0+1)<<" ";
  214. }
  215. cerr.precision(std::numeric_limits< double >::digits10);
  216. cerr<<"[ ";
  217. for(vector<embree::Hit>::iterator hit = hits.begin();
  218. hit != hits.end();
  219. hit++)
  220. {
  221. cerr<<(hit->t)<<endl;;
  222. }
  223. cerr<<"]"<<endl;
  224. large_hits_warned = true;
  225. return hits.empty();
  226. }
  227. }
  228. return hits.empty();
  229. }
  230. template <
  231. typename PointMatrixType,
  232. typename FaceMatrixType,
  233. typename RowVector3>
  234. bool
  235. igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  236. ::intersectSegment(const RowVector3& a, const RowVector3& ab, embree::Hit &hit) const
  237. {
  238. embree::Ray ray(toVec3f(a), toVec3f(ab), embree::zero, embree::one);
  239. _intersector->intersect(ray, hit);
  240. return hit ;
  241. }
  242. //template <
  243. //typename PointMatrixType,
  244. //typename FaceMatrixType,
  245. //typename RowVector3>
  246. //RowVector3
  247. //igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  248. //::hit_position(const embree::Hit & hit) const
  249. //{
  250. // // Barycenteric coordinates
  251. // const double w0 = (1.0-hit.u-hit.v);
  252. // const double w1 = hit.u;
  253. // const double w2 = hit.v;
  254. // RowVector3 hitP;
  255. // hitP.resize(3);
  256. // for(int d = 0;d<3;d++)
  257. // {
  258. // hitP(d) =
  259. // w0 * vertices[triangles[hit.id0](0)](d) +
  260. // w1 * vertices[triangles[hit.id0](1)](d) +
  261. // w2 * vertices[triangles[hit.id0](2)](d);
  262. // }
  263. // return hitP;
  264. //}
  265. #endif //EMBREE_INTERSECTOR_H