EmbreeIntersector.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "EmbreeIntersector.h"
  2. template <typename RowVector3>
  3. inline embree::Vec3f toVec3f(const RowVector3 &p) { return embree::Vec3f((float)p[0], (float)p[1], (float)p[2]); }
  4. template <
  5. typename PointMatrixType,
  6. typename FaceMatrixType,
  7. typename RowVector3>
  8. igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  9. ::EmbreeIntersector(const PointMatrixType & V, const FaceMatrixType & F)
  10. {
  11. static bool inited = false;
  12. if(!inited)
  13. {
  14. //embree::TaskScheduler::start();//init();
  15. inited = true;
  16. }
  17. size_t numVertices = 0;
  18. size_t numTriangles = 0;
  19. triangles = (embree::BuildTriangle*) embree::rtcMalloc(sizeof(embree::BuildTriangle) * F.rows());
  20. vertices = (embree::BuildVertex*) embree::rtcMalloc(sizeof(embree::BuildVertex) * V.rows());
  21. for(int i = 0; i < (int)V.rows(); ++i)
  22. {
  23. vertices[numVertices++] = embree::BuildVertex((float)V(i,0),(float)V(i,1),(float)V(i,2));
  24. }
  25. for(int i = 0; i < (int)F.rows(); ++i)
  26. {
  27. triangles[numTriangles++] = embree::BuildTriangle((int)F(i,0),(int)F(i,1),(int)F(i,2),i);
  28. }
  29. _accel = embree::rtcCreateAccel("default", "default", triangles, numTriangles, vertices, numVertices);
  30. _intersector = _accel->queryInterface<embree::Intersector>();
  31. }
  32. template <
  33. typename PointMatrixType,
  34. typename FaceMatrixType,
  35. typename RowVector3>
  36. igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  37. ::~EmbreeIntersector()
  38. {
  39. embree::rtcFreeMemory();
  40. }
  41. template <
  42. typename PointMatrixType,
  43. typename FaceMatrixType,
  44. typename RowVector3>
  45. bool
  46. igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  47. ::intersectRay(const RowVector3& origin, const RowVector3& direction, embree::Hit &hit) const
  48. {
  49. embree::Ray ray(toVec3f(origin), toVec3f(direction), 1e-4f);
  50. _intersector->intersect(ray, hit);
  51. return hit ;
  52. }
  53. template <
  54. typename PointMatrixType,
  55. typename FaceMatrixType,
  56. typename RowVector3>
  57. bool
  58. igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  59. ::intersectRay(
  60. const RowVector3& origin,
  61. const RowVector3& direction,
  62. std::vector<embree::Hit > &hits) const
  63. {
  64. hits.clear();
  65. embree::Hit hit;
  66. embree::Vec3f o = toVec3f(origin);
  67. embree::Vec3f d = toVec3f(direction);
  68. while(true)
  69. {
  70. embree::Ray ray(o,d,embree::zero);
  71. _intersector->intersect(ray, hit);
  72. if(hit)
  73. {
  74. hits.push_back(hit);
  75. o = o+hit.t*d;
  76. }else
  77. {
  78. break;
  79. }
  80. }
  81. return hits.empty();
  82. }
  83. template <
  84. typename PointMatrixType,
  85. typename FaceMatrixType,
  86. typename RowVector3>
  87. bool
  88. igl::EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  89. ::intersectSegment(const RowVector3& a, const RowVector3& ab, embree::Hit &hit) const
  90. {
  91. embree::Ray ray(toVec3f(a), toVec3f(ab), embree::zero, embree::one);
  92. _intersector->intersect(ray, hit);
  93. return hit ;
  94. }
  95. #ifndef IGL_HEADER_ONLY
  96. // Explicit template instanciation
  97. #include <Eigen/Core>
  98. template class igl::EmbreeIntersector<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1> >;
  99. #endif