EmbreeIntersector.h 9.6 KB

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