2
0

EmbreeIntersector.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <[email protected]>
  4. // 2014 Christian Schüller <[email protected]>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla Public License
  7. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  8. // obtain one at http://mozilla.org/MPL/2.0/.
  9. // igl function interface for Embree2.2
  10. //
  11. // Necessary changes to switch from previous Embree versions:
  12. // * Use igl:Hit instead of embree:Hit (where id0 -> id)
  13. // * For Embree2.2
  14. // * Uncomment #define __USE_RAY_MASK__ in platform.h to enable masking
  15. #ifndef IGL_EMBREE_EMBREE_INTERSECTOR_H
  16. #define IGL_EMBREE_EMBREE_INTERSECTOR_H
  17. #include "../Hit.h"
  18. #include <Eigen/Geometry>
  19. #include <Eigen/Core>
  20. #include <embree4/rtcore.h>
  21. #include <embree4/rtcore_ray.h>
  22. #include <iostream>
  23. #include <vector>
  24. #include "EmbreeDevice.h"
  25. namespace igl
  26. {
  27. namespace embree
  28. {
  29. /// Simple class to wrap Embree's ray tracing functionality
  30. class EmbreeIntersector
  31. {
  32. public:
  33. typedef Eigen::Matrix<float,Eigen::Dynamic,3> PointMatrixType;
  34. typedef Eigen::Matrix<int,Eigen::Dynamic,3> FaceMatrixType;
  35. public:
  36. EmbreeIntersector();
  37. private:
  38. // Copying and assignment are not allowed.
  39. EmbreeIntersector(const EmbreeIntersector & that);
  40. EmbreeIntersector & operator=(const EmbreeIntersector &);
  41. public:
  42. virtual ~EmbreeIntersector();
  43. /// Initialize with a given mesh.
  44. ///
  45. /// @param[in] V #V by 3 list of vertex positions
  46. /// @param[in] F #F by 3 list of Oriented triangles
  47. /// @param[in] isStatic scene is optimized for static geometry
  48. /// #### Side effects:
  49. /// The first time this is ever called the embree engine is initialized.
  50. void init(
  51. const PointMatrixType& V,
  52. const FaceMatrixType& F,
  53. bool isStatic = false);
  54. /// Initialize with a given mesh.
  55. ///
  56. /// @param[in] V vector of #V by 3 list of vertex positions for each geometry
  57. /// @param[in] F vector of #F by 3 list of Oriented triangles for each geometry
  58. /// @param[in] masks a 32 bit mask to identify active geometries.
  59. /// @param[in] isStatic scene is optimized for static geometry
  60. ///
  61. /// ##### Side effects:
  62. /// The first time this is ever called the embree engine is initialized.
  63. void init(
  64. const std::vector<const PointMatrixType*>& V,
  65. const std::vector<const FaceMatrixType*>& F,
  66. const std::vector<int>& masks,
  67. bool isStatic = false);
  68. /// Deinitialize embree datasctructures for current mesh. Also called on
  69. /// destruction: no need to call if you just want to init() once and
  70. /// destroy.
  71. void deinit();
  72. /// Given a ray find the first hit
  73. ///
  74. /// @param[in] origin 3d origin point of ray
  75. /// @param[in] direction 3d (not necessarily normalized) direction vector of ray
  76. /// @param[in] tnear start of ray segment
  77. /// @param[in] tfar end of ray segment
  78. /// @param[in] masks a 32 bit mask to identify active geometries.
  79. /// @param[out] hit information about hit
  80. /// @return true if and only if there was a hit
  81. bool intersectRay(
  82. const Eigen::RowVector3f& origin,
  83. const Eigen::RowVector3f& direction,
  84. Hit<float>& hit,
  85. float tnear = 0,
  86. float tfar = std::numeric_limits<float>::infinity(),
  87. int mask = 0xFFFFFFFF) const;
  88. /// Given a ray find the first hit
  89. /// This is a conservative hit test where multiple rays within a small radius
  90. /// will be tested and only the closesest hit is returned.
  91. ///
  92. /// @param[in] origin 3d origin point of ray
  93. /// @param[in] direction 3d (not necessarily normalized) direction vector of ray
  94. /// @param[in] tnear start of ray segment
  95. /// @param[in] tfar end of ray segment
  96. /// @param[in] masks a 32 bit mask to identify active geometries.
  97. /// @param[in] geoId id of geometry mask (default std::numeric_limits<float>::infinity() if no: no masking)
  98. /// @param[in] closestHit true for gets closest hit, false for furthest hit
  99. /// @param[out] hit information about hit
  100. /// @return true if and only if there was a hit
  101. bool intersectBeam(
  102. const Eigen::RowVector3f& origin,
  103. const Eigen::RowVector3f& direction,
  104. Hit<float>& hit,
  105. float tnear = 0,
  106. float tfar = std::numeric_limits<float>::infinity(),
  107. int mask = 0xFFFFFFFF,
  108. int geoId = -1,
  109. bool closestHit = true,
  110. unsigned int samples = 4) const;
  111. /// Given a ray find all hits in order
  112. ///
  113. /// @param[in] origin 3d origin point of ray
  114. /// @param[in] direction 3d (not necessarily normalized) direction vector of ray
  115. /// @param[in] tnear start of ray segment
  116. /// @param[in] tfar end of ray segment
  117. /// @param[in] masks a 32 bit mask to identify active geometries.
  118. /// @param[out] hit information about hit
  119. /// @param[out] num_rays number of rays shot (at least one)
  120. /// @return true if and only if there was a hit
  121. bool intersectRay(
  122. const Eigen::RowVector3f& origin,
  123. const Eigen::RowVector3f& direction,
  124. std::vector<Hit<float>> &hits,
  125. int& num_rays,
  126. float tnear = 0,
  127. float tfar = std::numeric_limits<float>::infinity(),
  128. int mask = 0xFFFFFFFF) const;
  129. /// Given a ray find the first hit
  130. ///
  131. /// @param[in] a 3d first end point of segment
  132. /// @param[in] ab 3d vector from a to other endpoint b
  133. /// @param[out] hit information about hit
  134. /// @return true if and only if there was a hit
  135. bool intersectSegment(
  136. const Eigen::RowVector3f& a,
  137. const Eigen::RowVector3f& ab,
  138. Hit<float> &hit,
  139. int mask = 0xFFFFFFFF) const;
  140. private:
  141. struct Vertex {float x,y,z,a;};
  142. struct Triangle {int v0, v1, v2;};
  143. RTCScene scene;
  144. unsigned geomID;
  145. Vertex* vertices;
  146. Triangle* triangles;
  147. bool initialized;
  148. RTCDevice device;
  149. void createRay(
  150. RTCRayHit& ray,
  151. const Eigen::RowVector3f& origin,
  152. const Eigen::RowVector3f& direction,
  153. float tnear,
  154. float tfar,
  155. int mask) const;
  156. };
  157. }
  158. }
  159. #ifndef IGL_STATIC_LIBRARY
  160. # include "EmbreeIntersector.cpp"
  161. #endif
  162. #endif //EMBREE_INTERSECTOR_H