EmbreeIntersector.h 9.2 KB

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