EmbreeIntersector.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. // igl function interface for Embree2.0
  2. //
  3. // Necessary changes to switch from previous Embree versions:
  4. // * Use igl:Hit instead of embree:Hit (where id0 -> id)
  5. // * Embree2.0 finds now also back face intersections
  6. #ifndef IGL_EMBREE_INTERSECTOR_H
  7. #define IGL_EMBREE_INTERSECTOR_H
  8. #include "Hit.h"
  9. #include <Eigen/Core>
  10. #include "Embree_convenience.h"
  11. #include <vector>
  12. namespace igl
  13. {
  14. template <
  15. typename Scalar,
  16. typename Index>
  17. class EmbreeIntersector
  18. {
  19. public:
  20. // Initialize embree engine. This will be called on instance `init()`
  21. // calls. If already inited then this function does nothing: it is harmless
  22. // to call more than once.
  23. static void global_init();
  24. private:
  25. // Deinitialize the embree engine. This should probably never be called by
  26. // the user. Hence it's private. Do you really want to do this?
  27. static void global_deinit();
  28. public:
  29. typedef Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic> PointMatrixType;
  30. typedef Eigen::Matrix<Index,Eigen::Dynamic,Eigen::Dynamic> FaceMatrixType;
  31. typedef Eigen::Matrix<Scalar,1,3> RowVector3;
  32. public:
  33. EmbreeIntersector();
  34. private:
  35. // Copying and assignment are not allowed.
  36. EmbreeIntersector(const EmbreeIntersector & that);
  37. EmbreeIntersector operator=(const EmbreeIntersector &);
  38. public:
  39. virtual ~EmbreeIntersector();
  40. // Initialize with a given mesh.
  41. //
  42. // Inputs:
  43. // V #V by 3 list of vertex positions
  44. // F #F by 3 list of Oriented triangles
  45. // Side effects:
  46. // The first time this is ever called the embree engine is initialized.
  47. void init(
  48. const PointMatrixType & V,
  49. const FaceMatrixType & F,
  50. const char* structure = "default",
  51. const char* builder = "default",
  52. const char* traverser = "default");
  53. // Deinitialize embree datasctructures for current mesh. Also called on
  54. // destruction: no need to call if you just want to init() once and
  55. // destroy.
  56. void deinit();
  57. // Given a ray find the first hit
  58. //
  59. // Inputs:
  60. // origin 3d origin point of ray
  61. // direction 3d (not necessarily normalized) direction vector of ray
  62. // Output:
  63. // hit information about hit
  64. // Returns true if and only if there was a hit
  65. bool intersectRay(
  66. const RowVector3& origin,
  67. const RowVector3& direction,
  68. Hit& hit,
  69. float tnear = 0,
  70. float tfar = embree::inf) const;
  71. // Given a ray find the all hits in order
  72. //
  73. // Inputs:
  74. // origin 3d origin point of ray
  75. // direction 3d (not necessarily normalized) direction vector of ray
  76. // Output:
  77. // hit information about hit
  78. // num_rays number of rays shot (at least one)
  79. // Returns true if and only if there was a hit
  80. bool intersectRay(
  81. const RowVector3& origin,
  82. const RowVector3& direction,
  83. std::vector<Hit > &hits,
  84. int& num_rays,
  85. float tnear = 0,
  86. float tfar = embree::inf) const;
  87. // Given a ray find the first hit
  88. //
  89. // Inputs:
  90. // a 3d first end point of segment
  91. // ab 3d vector from a to other endpoint b
  92. // Output:
  93. // hit information about hit
  94. // Returns true if and only if there was a hit
  95. bool intersectSegment(const RowVector3& a, const RowVector3& ab, Hit &hit) const;
  96. private:
  97. embree::RTCGeometry* mesh;
  98. embree::RTCTriangle* triangles;
  99. embree::RTCVertex *vertices;
  100. embree::Intersector1 *intersector;
  101. };
  102. }
  103. // Implementation
  104. #include <igl/EPS.h>
  105. // This unfortunately cannot be a static field of EmbreeIntersector because it
  106. // would depend on the template and then we might end up with initializing
  107. // embree twice. If only there was a way to ask embree if it's already
  108. // initialized...
  109. namespace igl
  110. {
  111. // Keeps track of whether the **Global** Embree intersector has been
  112. // initialized. This should never been done at the global scope.
  113. static bool EmbreeIntersector_inited = false;
  114. }
  115. template <typename RowVector3>
  116. inline embree::Vector3f toVector3f(const RowVector3 &p) { return embree::Vector3f((float)p[0], (float)p[1], (float)p[2]); }
  117. template <
  118. typename Scalar,
  119. typename Index>
  120. void igl::EmbreeIntersector < Scalar, Index>::global_init()
  121. {
  122. if(!EmbreeIntersector_inited)
  123. {
  124. embree::rtcInit();
  125. #ifdef IGL_VERBOSE
  126. embree::rtcSetVerbose(3);
  127. #endif
  128. embree::rtcStartThreads();
  129. EmbreeIntersector_inited = true;
  130. }
  131. }
  132. template <
  133. typename Scalar,
  134. typename Index>
  135. void igl::EmbreeIntersector < Scalar, Index>::global_deinit()
  136. {
  137. EmbreeIntersector_inited = false;
  138. embree::rtcStopThreads();
  139. embree::rtcExit();
  140. embree::rtcFreeMemory();
  141. }
  142. template <
  143. typename Scalar,
  144. typename Index>
  145. igl::EmbreeIntersector < Scalar, Index>::EmbreeIntersector()
  146. :
  147. mesh(NULL),
  148. triangles(NULL),
  149. vertices(NULL),
  150. intersector(NULL)
  151. {
  152. }
  153. template <
  154. typename Scalar,
  155. typename Index>
  156. igl::EmbreeIntersector < Scalar, Index>::EmbreeIntersector(
  157. const EmbreeIntersector & that)
  158. {
  159. assert(false && "Copying EmbreeIntersector is not allowed");
  160. }
  161. template <
  162. typename Scalar,
  163. typename Index>
  164. igl::EmbreeIntersector <Scalar,Index>
  165. igl::EmbreeIntersector < Scalar, Index>::operator=(
  166. const EmbreeIntersector<Scalar, Index> & that)
  167. {
  168. assert(false && "Assigning an EmbreeIntersector is not allowed");
  169. return *this;
  170. }
  171. template <
  172. typename Scalar,
  173. typename Index>
  174. void igl::EmbreeIntersector < Scalar, Index>::init(
  175. const PointMatrixType & V,
  176. const FaceMatrixType & F,
  177. const char* structure,
  178. const char* builder,
  179. const char* traverser)
  180. {
  181. using namespace std;
  182. global_init();
  183. if(V.size() == 0 || F.size() == 0)
  184. {
  185. return;
  186. }
  187. mesh = embree::rtcNewTriangleMesh(F.rows(),V.rows(),structure);
  188. // fill vertex buffer
  189. vertices = embree::rtcMapPositionBuffer(mesh);
  190. for(int i=0;i<(int)V.rows();i++)
  191. {
  192. vertices[i] = embree::RTCVertex((float)V(i,0),(float)V(i,1),(float)V(i,2));
  193. }
  194. embree::rtcUnmapPositionBuffer(mesh);
  195. // fill triangle buffer
  196. triangles = embree::rtcMapTriangleBuffer(mesh);
  197. for(int i=0;i<(int)F.rows();i++)
  198. {
  199. triangles[i] = embree::RTCTriangle((int)F(i,0),(int)F(i,1),(int)F(i,2),i);
  200. }
  201. embree::rtcUnmapTriangleBuffer(mesh);
  202. embree::rtcBuildAccel(mesh,builder);
  203. embree::rtcCleanupGeometry(mesh);
  204. intersector = embree::rtcQueryIntersector1(mesh,traverser);
  205. }
  206. template <
  207. typename Scalar,
  208. typename Index>
  209. igl::EmbreeIntersector < Scalar, Index>
  210. ::~EmbreeIntersector()
  211. {
  212. deinit();
  213. }
  214. template <
  215. typename Scalar,
  216. typename Index>
  217. void igl::EmbreeIntersector < Scalar, Index>::deinit()
  218. {
  219. embree::rtcDeleteIntersector1(intersector);
  220. embree::rtcDeleteGeometry(mesh);
  221. }
  222. template <
  223. typename Scalar,
  224. typename Index>
  225. bool
  226. igl::EmbreeIntersector< Scalar, Index>
  227. ::intersectRay(
  228. const RowVector3& origin,
  229. const RowVector3& direction,
  230. Hit& hit,
  231. float tnear,
  232. float tfar) const
  233. {
  234. embree::Ray ray(toVector3f(origin), toVector3f(direction), tnear, tfar);
  235. intersector->intersect(ray);
  236. if(ray)
  237. {
  238. hit.id = ray.id0;
  239. hit.u = ray.u;
  240. hit.v = ray.v;
  241. hit.t = ray.tfar;
  242. return true;
  243. }
  244. return false;
  245. }
  246. template <
  247. typename Scalar,
  248. typename Index>
  249. bool
  250. igl::EmbreeIntersector < Scalar, Index>
  251. ::intersectRay(
  252. const RowVector3& origin,
  253. const RowVector3& direction,
  254. std::vector<Hit > &hits,
  255. int& num_rays,
  256. float tnear,
  257. float tfar) const
  258. {
  259. using namespace std;
  260. num_rays = 0;
  261. hits.clear();
  262. int last_id0 = -1;
  263. double self_hits = 0;
  264. // This epsilon is directly correleated to the number of missed hits, smaller
  265. // means more accurate and slower
  266. //const double eps = DOUBLE_EPS;
  267. const double eps = FLOAT_EPS;
  268. double min_t = tnear;
  269. bool large_hits_warned = false;
  270. embree::Ray ray(toVector3f(origin),toVector3f(direction));
  271. while(true)
  272. {
  273. ray.tnear = min_t;
  274. ray.tfar = tfar;
  275. ray.id0 = -1;
  276. num_rays++;
  277. intersector->intersect(ray);
  278. if(ray)
  279. {
  280. // Hit self again, progressively advance
  281. if(ray.id0 == last_id0 || ray.tfar <= min_t)
  282. {
  283. // push min_t a bit more
  284. //double t_push = pow(2.0,self_hits-4)*(hit.t<eps?eps:hit.t);
  285. double t_push = pow(2.0,self_hits)*eps;
  286. #ifdef IGL_VERBOSE
  287. cout<<" t_push: "<<t_push<<endl;
  288. #endif
  289. //o = o+t_push*d;
  290. min_t += t_push;
  291. self_hits++;
  292. }
  293. else
  294. {
  295. Hit hit;
  296. hit.id = ray.id0;
  297. hit.u = ray.u;
  298. hit.v = ray.v;
  299. hit.t = ray.tfar;
  300. hits.push_back(hit);
  301. #ifdef IGL_VERBOSE
  302. cout<<" t: "<<hit.t<<endl;
  303. #endif
  304. // Instead of moving origin, just change min_t. That way calculations
  305. // all use exactly same origin values
  306. min_t = ray.tfar;
  307. // reset t_scale
  308. self_hits = 0;
  309. }
  310. last_id0 = ray.id0;
  311. }
  312. else
  313. break; // no more hits
  314. if(hits.size()>1000 && !large_hits_warned)
  315. {
  316. cerr<<"Warning: Large number of hits..."<<endl;
  317. cerr<<"[ ";
  318. for(vector<Hit>::iterator hit = hits.begin(); hit != hits.end();hit++)
  319. {
  320. cerr<<(hit->id+1)<<" ";
  321. }
  322. cerr.precision(std::numeric_limits< double >::digits10);
  323. cerr<<"[ ";
  324. for(vector<Hit>::iterator hit = hits.begin(); hit != hits.end(); hit++)
  325. {
  326. cerr<<(hit->t)<<endl;;
  327. }
  328. cerr<<"]"<<endl;
  329. large_hits_warned = true;
  330. return hits.empty();
  331. }
  332. }
  333. return hits.empty();
  334. }
  335. template <
  336. typename Scalar,
  337. typename Index>
  338. bool
  339. igl::EmbreeIntersector < Scalar, Index>
  340. ::intersectSegment(const RowVector3& a, const RowVector3& ab, Hit &hit) const
  341. {
  342. embree::Ray ray(toVector3f(a), toVector3f(ab), embree::zero, embree::one);
  343. intersector->intersect(ray);
  344. if(ray)
  345. {
  346. hit.id = ray.id0;
  347. hit.u = ray.u;
  348. hit.v = ray.v;
  349. hit.t = ray.tfar;
  350. return true;
  351. }
  352. return false;
  353. }
  354. #endif //EMBREE_INTERSECTOR_H