EmbreeIntersector.h 7.5 KB

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