EmbreeIntersector.h 7.6 KB

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