EmbreeIntersector.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. embree::rtcInit();
  104. #ifdef IGL_VERBOSE
  105. embree::rtcSetVerbose(3);
  106. #endif
  107. embree::rtcStartThreads();
  108. inited = true;
  109. }
  110. if(V.size() == 0 || F.size() == 0)
  111. {
  112. return;
  113. }
  114. mesh = embree::rtcNewTriangleMesh(F.rows(),V.rows(),structure);
  115. // fill vertex buffer
  116. vertices = embree::rtcMapPositionBuffer(mesh);
  117. for(int i=0;i<(int)V.rows();i++)
  118. {
  119. vertices[i] = embree::RTCVertex((float)V(i,0),(float)V(i,1),(float)V(i,2));
  120. }
  121. embree::rtcUnmapPositionBuffer(mesh);
  122. // fill triangle buffer
  123. triangles = embree::rtcMapTriangleBuffer(mesh);
  124. for(int i=0;i<(int)F.rows();i++)
  125. {
  126. triangles[i] = embree::RTCTriangle((int)F(i,0),(int)F(i,1),(int)F(i,2),i);
  127. }
  128. embree::rtcUnmapTriangleBuffer(mesh);
  129. embree::rtcBuildAccel(mesh,builder);
  130. embree::rtcCleanupGeometry(mesh);
  131. intersector = embree::rtcQueryIntersector1(mesh,traverser);
  132. }
  133. template <
  134. typename Scalar,
  135. typename Index>
  136. igl::EmbreeIntersector < Scalar, Index>
  137. ::~EmbreeIntersector()
  138. {
  139. embree::rtcDeleteIntersector1(intersector);
  140. embree::rtcDeleteGeometry(mesh);
  141. // embree::rtcStopThreads();
  142. // embree::rtcExit();
  143. // embree::rtcFreeMemory();
  144. }
  145. template <
  146. typename Scalar,
  147. typename Index>
  148. bool
  149. igl::EmbreeIntersector< Scalar, Index>
  150. ::intersectRay(
  151. const RowVector3& origin,
  152. const RowVector3& direction,
  153. Hit& hit,
  154. float tnear,
  155. float tfar) const
  156. {
  157. embree::Ray ray(toVector3f(origin), toVector3f(direction), tnear, tfar);
  158. intersector->intersect(ray);
  159. if(ray)
  160. {
  161. hit.id = ray.id0;
  162. hit.u = ray.u;
  163. hit.v = ray.v;
  164. hit.t = ray.tfar;
  165. return true;
  166. }
  167. return false;
  168. }
  169. template <
  170. typename Scalar,
  171. typename Index>
  172. bool
  173. igl::EmbreeIntersector < Scalar, Index>
  174. ::intersectRay(
  175. const RowVector3& origin,
  176. const RowVector3& direction,
  177. std::vector<Hit > &hits,
  178. int& num_rays,
  179. float tnear,
  180. float tfar) const
  181. {
  182. using namespace std;
  183. num_rays = 0;
  184. hits.clear();
  185. int last_id0 = -1;
  186. double self_hits = 0;
  187. // This epsilon is directly correleated to the number of missed hits, smaller
  188. // means more accurate and slower
  189. //const double eps = DOUBLE_EPS;
  190. const double eps = FLOAT_EPS;
  191. double min_t = tnear;
  192. bool large_hits_warned = false;
  193. embree::Ray ray(toVector3f(origin),toVector3f(direction));
  194. while(true)
  195. {
  196. ray.tnear = min_t;
  197. ray.tfar = tfar;
  198. ray.id0 = -1;
  199. num_rays++;
  200. intersector->intersect(ray);
  201. if(ray)
  202. {
  203. // Hit self again, progressively advance
  204. if(ray.id0 == last_id0 || ray.tfar <= min_t)
  205. {
  206. // push min_t a bit more
  207. //double t_push = pow(2.0,self_hits-4)*(hit.t<eps?eps:hit.t);
  208. double t_push = pow(2.0,self_hits)*eps;
  209. #ifdef IGL_VERBOSE
  210. cout<<" t_push: "<<t_push<<endl;
  211. #endif
  212. //o = o+t_push*d;
  213. min_t += t_push;
  214. self_hits++;
  215. }
  216. else
  217. {
  218. Hit hit;
  219. hit.id = ray.id0;
  220. hit.u = ray.u;
  221. hit.v = ray.v;
  222. hit.t = ray.tfar;
  223. hits.push_back(hit);
  224. #ifdef IGL_VERBOSE
  225. cout<<" t: "<<hit.t<<endl;
  226. #endif
  227. // Instead of moving origin, just change min_t. That way calculations
  228. // all use exactly same origin values
  229. min_t = ray.tfar;
  230. // reset t_scale
  231. self_hits = 0;
  232. }
  233. last_id0 = ray.id0;
  234. }
  235. else
  236. break; // no more hits
  237. if(hits.size()>1000 && !large_hits_warned)
  238. {
  239. cerr<<"Warning: Large number of hits..."<<endl;
  240. cerr<<"[ ";
  241. for(vector<Hit>::iterator hit = hits.begin(); hit != hits.end();hit++)
  242. {
  243. cerr<<(hit->id+1)<<" ";
  244. }
  245. cerr.precision(std::numeric_limits< double >::digits10);
  246. cerr<<"[ ";
  247. for(vector<Hit>::iterator hit = hits.begin(); hit != hits.end(); hit++)
  248. {
  249. cerr<<(hit->t)<<endl;;
  250. }
  251. cerr<<"]"<<endl;
  252. large_hits_warned = true;
  253. return hits.empty();
  254. }
  255. }
  256. return hits.empty();
  257. }
  258. template <
  259. typename Scalar,
  260. typename Index>
  261. bool
  262. igl::EmbreeIntersector < Scalar, Index>
  263. ::intersectSegment(const RowVector3& a, const RowVector3& ab, Hit &hit) const
  264. {
  265. embree::Ray ray(toVector3f(a), toVector3f(ab), embree::zero, embree::one);
  266. intersector->intersect(ray);
  267. if(ray)
  268. {
  269. hit.id = ray.id0;
  270. hit.u = ray.u;
  271. hit.v = ray.v;
  272. hit.t = ray.tfar;
  273. return true;
  274. }
  275. return false;
  276. }
  277. #endif //EMBREE_INTERSECTOR_H