EmbreeIntersector.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "include/embree.h"
  9. #include "include/intersector1.h"
  10. #include "common/ray.h"
  11. #include <vector>
  12. namespace igl
  13. {
  14. struct Hit
  15. {
  16. int id; // primitive id
  17. float u,v; // barycentric coordinates
  18. float t; // distance = direction*t to intersection
  19. };
  20. template <
  21. typename Scalar,
  22. typename Index>
  23. class EmbreeIntersector
  24. {
  25. typedef Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic> PointMatrixType;
  26. typedef Eigen::Matrix<Index,Eigen::Dynamic,Eigen::Dynamic> FaceMatrixType;
  27. typedef Eigen::Matrix<Scalar,1,3> RowVector3;
  28. public:
  29. // V #V by 3 list of vertex positions
  30. // F #F by 3 list of Oriented triangles
  31. EmbreeIntersector();
  32. EmbreeIntersector(
  33. const PointMatrixType & V,
  34. const FaceMatrixType & F,
  35. const char* structure = "default",
  36. const char* builder = "default",
  37. const char* traverser = "default");
  38. virtual ~EmbreeIntersector();
  39. // Given a ray find the first hit
  40. //
  41. // Inputs:
  42. // origin 3d origin point of ray
  43. // direction 3d (not necessarily normalized) direction vector of ray
  44. // Output:
  45. // hit information about hit
  46. // Returns true if and only if there was a hit
  47. bool intersectRay(
  48. const RowVector3& origin,
  49. const RowVector3& direction,
  50. Hit& hit,
  51. float tnear = 0,
  52. float tfar = embree::inf) const;
  53. // Given a ray find the all hits in order
  54. //
  55. // Inputs:
  56. // origin 3d origin point of ray
  57. // direction 3d (not necessarily normalized) direction vector of ray
  58. // Output:
  59. // hit information about hit
  60. // num_rays number of rays shot (at least one)
  61. // Returns true if and only if there was a hit
  62. bool intersectRay(
  63. const RowVector3& origin,
  64. const RowVector3& direction,
  65. std::vector<Hit > &hits,
  66. int& num_rays,
  67. float tnear = 0,
  68. float tfar = embree::inf) const;
  69. // Given a ray find the first hit
  70. //
  71. // Inputs:
  72. // a 3d first end point of segment
  73. // ab 3d vector from a to other endpoint b
  74. // Output:
  75. // hit information about hit
  76. // Returns true if and only if there was a hit
  77. bool intersectSegment(const RowVector3& a, const RowVector3& ab, Hit &hit) const;
  78. private:
  79. embree::RTCGeometry* mesh;
  80. embree::RTCTriangle* triangles;
  81. embree::RTCVertex *vertices;
  82. embree::Intersector1 *intersector;
  83. };
  84. }
  85. // Implementation
  86. #include <igl/EPS.h>
  87. template <typename RowVector3>
  88. inline embree::Vector3f toVector3f(const RowVector3 &p) { return embree::Vector3f((float)p[0], (float)p[1], (float)p[2]); }
  89. template <
  90. typename Scalar,
  91. typename Index>
  92. igl::EmbreeIntersector < Scalar, Index>
  93. ::EmbreeIntersector()
  94. {
  95. static bool inited = false;
  96. if(!inited)
  97. {
  98. embree::rtcInit();
  99. #ifdef VERBOSE
  100. embree::rtcSetVerbose(3);
  101. #endif
  102. embree::rtcStartThreads();
  103. inited = true;
  104. }
  105. }
  106. template <
  107. typename Scalar,
  108. typename Index>
  109. igl::EmbreeIntersector < Scalar, Index>
  110. ::EmbreeIntersector(const PointMatrixType & V,
  111. const FaceMatrixType & F,
  112. const char* structure,
  113. const char* builder,
  114. const char* traverser)
  115. {
  116. static bool inited = false;
  117. if(!inited)
  118. {
  119. embree::rtcInit();
  120. #ifdef VERBOSE
  121. embree::rtcSetVerbose(3);
  122. #endif
  123. embree::rtcStartThreads();
  124. inited = true;
  125. }
  126. mesh = embree::rtcNewTriangleMesh(F.rows(),V.rows(),structure);
  127. // fill vertex buffer
  128. vertices = embree::rtcMapPositionBuffer(mesh);
  129. for(int i=0;i<(int)V.rows();i++)
  130. {
  131. vertices[i] = embree::RTCVertex((float)V(i,0),(float)V(i,1),(float)V(i,2));
  132. }
  133. embree::rtcUnmapPositionBuffer(mesh);
  134. // fill triangle buffer
  135. triangles = embree::rtcMapTriangleBuffer(mesh);
  136. for(int i=0;i<(int)F.rows();i++)
  137. {
  138. triangles[i] = embree::RTCTriangle((int)F(i,0),(int)F(i,1),(int)F(i,2),i);
  139. }
  140. embree::rtcUnmapTriangleBuffer(mesh);
  141. embree::rtcBuildAccel(mesh,builder);
  142. embree::rtcCleanupGeometry(mesh);
  143. intersector = embree::rtcQueryIntersector1(mesh,traverser);
  144. }
  145. template <
  146. typename Scalar,
  147. typename Index>
  148. igl::EmbreeIntersector < Scalar, Index>
  149. ::~EmbreeIntersector()
  150. {
  151. embree::rtcDeleteIntersector1(intersector);
  152. embree::rtcDeleteGeometry(mesh);
  153. // embree::rtcStopThreads();
  154. // embree::rtcExit();
  155. // embree::rtcFreeMemory();
  156. }
  157. template <
  158. typename Scalar,
  159. typename Index>
  160. bool
  161. igl::EmbreeIntersector< Scalar, Index>
  162. ::intersectRay(
  163. const RowVector3& origin,
  164. const RowVector3& direction,
  165. Hit& hit,
  166. float tnear,
  167. float tfar) const
  168. {
  169. embree::Ray ray(toVector3f(origin), toVector3f(direction), tnear, tfar);
  170. intersector->intersect(ray);
  171. if(ray)
  172. {
  173. hit.id = ray.id0;
  174. hit.u = ray.u;
  175. hit.v = ray.v;
  176. hit.t = ray.tfar;
  177. return true;
  178. }
  179. return false;
  180. }
  181. template <
  182. typename Scalar,
  183. typename Index>
  184. bool
  185. igl::EmbreeIntersector < Scalar, Index>
  186. ::intersectRay(
  187. const RowVector3& origin,
  188. const RowVector3& direction,
  189. std::vector<Hit > &hits,
  190. int& num_rays,
  191. float tnear,
  192. float tfar) const
  193. {
  194. using namespace std;
  195. num_rays = 0;
  196. hits.clear();
  197. int last_id0 = -1;
  198. double self_hits = 0;
  199. // This epsilon is directly correleated to the number of missed hits, smaller
  200. // means more accurate and slower
  201. //const double eps = DOUBLE_EPS;
  202. const double eps = FLOAT_EPS;
  203. double min_t = tnear;
  204. bool large_hits_warned = false;
  205. embree::Ray ray(toVector3f(origin),toVector3f(direction));
  206. while(true)
  207. {
  208. ray.tnear = min_t;
  209. ray.tfar = tfar;
  210. ray.id0 = -1;
  211. num_rays++;
  212. intersector->intersect(ray);
  213. if(ray)
  214. {
  215. // Hit self again, progressively advance
  216. if(ray.id0 == last_id0 || ray.tfar <= min_t)
  217. {
  218. // push min_t a bit more
  219. //double t_push = pow(2.0,self_hits-4)*(hit.t<eps?eps:hit.t);
  220. double t_push = pow(2.0,self_hits)*eps;
  221. #ifdef VERBOSE
  222. cout<<" t_push: "<<t_push<<endl;
  223. #endif
  224. //o = o+t_push*d;
  225. min_t += t_push;
  226. self_hits++;
  227. }
  228. else
  229. {
  230. Hit hit;
  231. hit.id = ray.id0;
  232. hit.u = ray.u;
  233. hit.v = ray.v;
  234. hit.t = ray.tfar;
  235. hits.push_back(hit);
  236. #ifdef VERBOSE
  237. cout<<" t: "<<hit.t<<endl;
  238. #endif
  239. // Instead of moving origin, just change min_t. That way calculations
  240. // all use exactly same origin values
  241. min_t = ray.tfar;
  242. // reset t_scale
  243. self_hits = 0;
  244. }
  245. last_id0 = ray.id0;
  246. }
  247. else
  248. break; // no more hits
  249. if(hits.size()>1000 && !large_hits_warned)
  250. {
  251. cerr<<"Warning: Large number of hits..."<<endl;
  252. cerr<<"[ ";
  253. for(vector<Hit>::iterator hit = hits.begin(); hit != hits.end();hit++)
  254. {
  255. cerr<<(hit->id+1)<<" ";
  256. }
  257. cerr.precision(std::numeric_limits< double >::digits10);
  258. cerr<<"[ ";
  259. for(vector<Hit>::iterator hit = hits.begin(); hit != hits.end(); hit++)
  260. {
  261. cerr<<(hit->t)<<endl;;
  262. }
  263. cerr<<"]"<<endl;
  264. large_hits_warned = true;
  265. return hits.empty();
  266. }
  267. }
  268. return hits.empty();
  269. }
  270. template <
  271. typename Scalar,
  272. typename Index>
  273. bool
  274. igl::EmbreeIntersector < Scalar, Index>
  275. ::intersectSegment(const RowVector3& a, const RowVector3& ab, Hit &hit) const
  276. {
  277. embree::Ray ray(toVector3f(a), toVector3f(ab), embree::zero, embree::one);
  278. intersector->intersect(ray);
  279. if(ray)
  280. {
  281. hit.id = ray.id0;
  282. hit.u = ray.u;
  283. hit.v = ray.v;
  284. hit.t = ray.tfar;
  285. return true;
  286. }
  287. return false;
  288. }
  289. #endif //EMBREE_INTERSECTOR_H