EmbreeIntersector.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. 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. 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. 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. EmbreeIntersector < PointMatrixType, FaceMatrixType, RowVector3>
  59. ::intersectSegment(const RowVector3& a, const RowVector3& ab, embree::Hit &hit) const
  60. {
  61. embree::Ray ray(toVec3f(a), toVec3f(ab), embree::zero, embree::one);
  62. _intersector->intersect(ray, hit);
  63. return hit ;
  64. }
  65. #ifndef IGL_HEADER_ONLY
  66. // Explicit template instanciation
  67. #include <Eigen/Core>
  68. template class 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> >;
  69. #endif