EmbreeIntersector.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. };
  99. }
  100. // Implementation
  101. #include <igl/EPS.h>
  102. // This unfortunately cannot be a static field of EmbreeIntersector because it
  103. // would depend on the template and then we might end up with initializing
  104. // embree twice. If only there was a way to ask embree if it's already
  105. // initialized...
  106. namespace igl
  107. {
  108. // Keeps track of whether the **Global** Embree intersector has been
  109. // initialized. This should never been done at the global scope.
  110. static bool EmbreeIntersector_inited = false;
  111. }
  112. template <typename RowVector3>
  113. inline embree::Vector3f toVector3f(const RowVector3 &p) { return embree::Vector3f((float)p[0], (float)p[1], (float)p[2]); }
  114. inline void igl::EmbreeIntersector::global_init()
  115. {
  116. if(!EmbreeIntersector_inited)
  117. {
  118. embree::rtcInit();
  119. #ifdef IGL_VERBOSE
  120. embree::rtcSetVerbose(3);
  121. #endif
  122. embree::rtcStartThreads();
  123. EmbreeIntersector_inited = true;
  124. }
  125. }
  126. inline void igl::EmbreeIntersector::global_deinit()
  127. {
  128. EmbreeIntersector_inited = false;
  129. embree::rtcStopThreads();
  130. embree::rtcExit();
  131. embree::rtcFreeMemory();
  132. }
  133. inline igl::EmbreeIntersector::EmbreeIntersector()
  134. :
  135. mesh(NULL),
  136. triangles(NULL),
  137. vertices(NULL),
  138. intersector(NULL)
  139. {
  140. }
  141. inline igl::EmbreeIntersector::EmbreeIntersector(
  142. const EmbreeIntersector & that)
  143. {
  144. assert(false && "Copying EmbreeIntersector is not allowed");
  145. }
  146. inline igl::EmbreeIntersector igl::EmbreeIntersector::operator=(
  147. const EmbreeIntersector & that)
  148. {
  149. assert(false && "Assigning an EmbreeIntersector is not allowed");
  150. return *this;
  151. }
  152. inline void igl::EmbreeIntersector::init(
  153. const PointMatrixType & V,
  154. const FaceMatrixType & F,
  155. const char* structure,
  156. const char* builder,
  157. const char* traverser)
  158. {
  159. using namespace std;
  160. global_init();
  161. if(V.size() == 0 || F.size() == 0)
  162. {
  163. return;
  164. }
  165. mesh = embree::rtcNewTriangleMesh(F.rows(),V.rows(),structure);
  166. // fill vertex buffer
  167. vertices = embree::rtcMapPositionBuffer(mesh);
  168. for(int i=0;i<(int)V.rows();i++)
  169. {
  170. vertices[i] = embree::RTCVertex((float)V(i,0),(float)V(i,1),(float)V(i,2));
  171. }
  172. embree::rtcUnmapPositionBuffer(mesh);
  173. // fill triangle buffer
  174. triangles = embree::rtcMapTriangleBuffer(mesh);
  175. for(int i=0;i<(int)F.rows();i++)
  176. {
  177. triangles[i] = embree::RTCTriangle((int)F(i,0),(int)F(i,1),(int)F(i,2),i);
  178. }
  179. embree::rtcUnmapTriangleBuffer(mesh);
  180. embree::rtcBuildAccel(mesh,builder);
  181. embree::rtcCleanupGeometry(mesh);
  182. intersector = embree::rtcQueryIntersector1(mesh,traverser);
  183. }
  184. igl::EmbreeIntersector
  185. ::~EmbreeIntersector()
  186. {
  187. deinit();
  188. }
  189. void igl::EmbreeIntersector::deinit()
  190. {
  191. embree::rtcDeleteIntersector1(intersector);
  192. embree::rtcDeleteGeometry(mesh);
  193. }
  194. inline bool igl::EmbreeIntersector::intersectRay(
  195. const RowVector3& origin,
  196. const RowVector3& direction,
  197. Hit& hit,
  198. float tnear,
  199. float tfar) const
  200. {
  201. embree::Ray ray(toVector3f(origin), toVector3f(direction), tnear, tfar);
  202. intersector->intersect(ray);
  203. if(ray)
  204. {
  205. hit.id = ray.id0;
  206. hit.u = ray.u;
  207. hit.v = ray.v;
  208. hit.t = ray.tfar;
  209. return true;
  210. }
  211. return false;
  212. }
  213. inline bool
  214. igl::EmbreeIntersector
  215. ::intersectRay(
  216. const RowVector3& origin,
  217. const RowVector3& direction,
  218. std::vector<Hit > &hits,
  219. int& num_rays,
  220. float tnear,
  221. float tfar) const
  222. {
  223. using namespace std;
  224. num_rays = 0;
  225. hits.clear();
  226. int last_id0 = -1;
  227. double self_hits = 0;
  228. // This epsilon is directly correleated to the number of missed hits, smaller
  229. // means more accurate and slower
  230. //const double eps = DOUBLE_EPS;
  231. const double eps = FLOAT_EPS;
  232. double min_t = tnear;
  233. bool large_hits_warned = false;
  234. embree::Ray ray(toVector3f(origin),toVector3f(direction));
  235. while(true)
  236. {
  237. ray.tnear = min_t;
  238. ray.tfar = tfar;
  239. ray.id0 = -1;
  240. num_rays++;
  241. intersector->intersect(ray);
  242. if(ray)
  243. {
  244. // Hit self again, progressively advance
  245. if(ray.id0 == last_id0 || ray.tfar <= min_t)
  246. {
  247. // push min_t a bit more
  248. //double t_push = pow(2.0,self_hits-4)*(hit.t<eps?eps:hit.t);
  249. double t_push = pow(2.0,self_hits)*eps;
  250. #ifdef IGL_VERBOSE
  251. cout<<" t_push: "<<t_push<<endl;
  252. #endif
  253. //o = o+t_push*d;
  254. min_t += t_push;
  255. self_hits++;
  256. }
  257. else
  258. {
  259. Hit hit;
  260. hit.id = ray.id0;
  261. hit.u = ray.u;
  262. hit.v = ray.v;
  263. hit.t = ray.tfar;
  264. hits.push_back(hit);
  265. #ifdef IGL_VERBOSE
  266. cout<<" t: "<<hit.t<<endl;
  267. #endif
  268. // Instead of moving origin, just change min_t. That way calculations
  269. // all use exactly same origin values
  270. min_t = ray.tfar;
  271. // reset t_scale
  272. self_hits = 0;
  273. }
  274. last_id0 = ray.id0;
  275. }
  276. else
  277. break; // no more hits
  278. if(hits.size()>1000 && !large_hits_warned)
  279. {
  280. cerr<<"Warning: Large number of hits..."<<endl;
  281. cerr<<"[ ";
  282. for(vector<Hit>::iterator hit = hits.begin(); hit != hits.end();hit++)
  283. {
  284. cerr<<(hit->id+1)<<" ";
  285. }
  286. cerr.precision(std::numeric_limits< double >::digits10);
  287. cerr<<"[ ";
  288. for(vector<Hit>::iterator hit = hits.begin(); hit != hits.end(); hit++)
  289. {
  290. cerr<<(hit->t)<<endl;;
  291. }
  292. cerr<<"]"<<endl;
  293. large_hits_warned = true;
  294. return hits.empty();
  295. }
  296. }
  297. return hits.empty();
  298. }
  299. inline bool
  300. igl::EmbreeIntersector
  301. ::intersectSegment(const RowVector3& a, const RowVector3& ab, Hit &hit) const
  302. {
  303. embree::Ray ray(toVector3f(a), toVector3f(ab), embree::zero, embree::one);
  304. intersector->intersect(ray);
  305. if(ray)
  306. {
  307. hit.id = ray.id0;
  308. hit.u = ray.u;
  309. hit.v = ray.v;
  310. hit.t = ray.tfar;
  311. return true;
  312. }
  313. return false;
  314. }
  315. #endif //EMBREE_INTERSECTOR_H