EmbreeRenderer.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. //
  4. // Copyright (C) 2020 Vladimir Fonov <[email protected]>
  5. // 2013 Alec Jacobson <[email protected]>
  6. // 2014 Christian Schüller <[email protected]>
  7. //
  8. // This Source Code Form is subject to the terms of the Mozilla Public License
  9. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  10. // obtain one at http://mozilla.org/MPL/2.0/.
  11. //
  12. #ifndef IGL_EMBREE_EMBREE_RENDERER_H
  13. #define IGL_EMBREE_EMBREE_RENDERER_H
  14. #include "../colormap.h"
  15. #include <Eigen/Geometry>
  16. #include <Eigen/Core>
  17. #include <Eigen/Geometry>
  18. #include <embree3/rtcore.h>
  19. #include <embree3/rtcore_ray.h>
  20. #include <iostream>
  21. #include <vector>
  22. #include "EmbreeDevice.h"
  23. namespace igl
  24. {
  25. namespace embree
  26. {
  27. /// @private
  28. /// embree-based mesh renderer
  29. class EmbreeRenderer
  30. {
  31. public:
  32. typedef Eigen::RowVector3f Vec3f;
  33. struct Hit
  34. {
  35. int id; // primitive id
  36. int gid; // geometry id
  37. float u,v; // barycentric coordinates
  38. float t; // distance = direction*t to intersection
  39. Vec3f N; // element normal
  40. };
  41. public:
  42. typedef Eigen::Matrix<float,Eigen::Dynamic,3> PointMatrixType;
  43. typedef Eigen::Matrix<float,Eigen::Dynamic,3> ColorMatrixType;
  44. typedef Eigen::Matrix<int, Eigen::Dynamic,3> FaceMatrixType;
  45. typedef Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> PixelMatrixType;
  46. public:
  47. EmbreeRenderer();
  48. private:
  49. // Copying and assignment are not allowed.
  50. EmbreeRenderer(const EmbreeRenderer & that);
  51. EmbreeRenderer & operator=(const EmbreeRenderer &);
  52. public:
  53. virtual ~EmbreeRenderer();
  54. // Specify mesh, this call reinitializes embree structures
  55. // Inputs:
  56. // V #V x dim matrix of vertex coordinates
  57. // F #F x simplex_size matrix of indices of simplex corners into V
  58. // is_static - optimize for static thene (HQ rendering)
  59. template <typename DerivedV, typename DerivedF>
  60. void set_mesh(const Eigen::MatrixBase<DerivedV> & V,
  61. const Eigen::MatrixBase<DerivedF> & F,
  62. bool is_static=true);
  63. // Specify per-vertex or per-face color
  64. // Inputs:
  65. // C #V x 3 matrix of vertex colors
  66. // or #F x 3 matrix of face colors
  67. // or 1 x 3 matrix of uniform color
  68. template <typename DerivedC>
  69. void set_colors(const Eigen::MatrixBase<DerivedC> & C);
  70. // Use min(D) and max(D) to set caxis.
  71. template <typename DerivedD>
  72. void set_data(const Eigen::MatrixBase<DerivedD> & D,
  73. igl::ColorMapType cmap = igl::COLOR_MAP_TYPE_VIRIDIS);
  74. // Specify per-vertex or per-face scalar field
  75. // that will be converted to color using jet color map
  76. // Inputs:
  77. // caxis_min caxis minimum bound
  78. // caxis_max caxis maximum bound
  79. // D #V by 1 list of scalar values
  80. // cmap colormap type
  81. // num_steps number of intervals to discretize the colormap
  82. template <typename DerivedD, typename T>
  83. void set_data(
  84. const Eigen::MatrixBase<DerivedD> & D,
  85. T caxis_min,
  86. T caxis_max,
  87. igl::ColorMapType cmap = igl::COLOR_MAP_TYPE_VIRIDIS);
  88. // Specify mesh rotation
  89. // Inputs:
  90. // r 3 x 3 rotaton matrix
  91. template <typename Derivedr>
  92. void set_rot(const Eigen::MatrixBase<Derivedr> &r);
  93. // Specify mesh magnification
  94. // Inputs:
  95. // z magnification ratio
  96. template <typename T>
  97. void set_zoom(T z);
  98. // Specify mesh translation
  99. // Inputs:
  100. // tr translation vector
  101. template <typename Derivedtr>
  102. void set_translation(const Eigen::MatrixBase<Derivedtr> &tr);
  103. // Specify that color is face based
  104. // Inputs:
  105. // f - face or vertex colours
  106. void set_face_based(bool f);
  107. // Use orthographic projection
  108. // Inputs:
  109. // f - orthographic or perspective projection
  110. void set_orthographic(bool f );
  111. // Render both sides of triangles
  112. // Inputs:
  113. // f - double sided
  114. void set_double_sided(bool f);
  115. // render full buffer
  116. // Outputs:
  117. // all outputs should have the same size (size of the output picture)
  118. // area outside of the visible object will have zero alpha component (transparant)
  119. // R - red channel
  120. // G - green channel
  121. // B - blue channel
  122. // A - alpha channel
  123. void render_buffer(PixelMatrixType &R,
  124. PixelMatrixType &G,
  125. PixelMatrixType &B,
  126. PixelMatrixType &A);
  127. // Given a ray find the first hit
  128. //
  129. // Inputs:
  130. // origin 3d origin point of ray
  131. // direction 3d (not necessarily normalized) direction vector of ray
  132. // tnear start of ray segment
  133. // tfar end of ray segment
  134. // mask a 32 bit mask to identify active geometries.
  135. // Output:
  136. // hit information about hit
  137. // Returns true if and only if there was a hit
  138. bool intersect_ray(
  139. const Eigen::RowVector3f& origin,
  140. const Eigen::RowVector3f& direction,
  141. Hit& hit,
  142. float tnear = 0,
  143. float tfar = std::numeric_limits<float>::infinity(),
  144. int mask = 0xFFFFFFFF) const;
  145. private:
  146. // Initialize with a given mesh.
  147. //
  148. // Inputs:
  149. // V #V by 3 list of vertex positions
  150. // F #F by 3 list of Oriented triangles
  151. // isStatic scene is optimized for static geometry
  152. // Side effects:
  153. // The first time this is ever called the embree engine is initialized.
  154. void init(
  155. const PointMatrixType& V,
  156. const FaceMatrixType& F,
  157. bool isStatic = false);
  158. // Initialize embree with a given mesh.
  159. //
  160. // Inputs:
  161. // V vector of #V by 3 list of vertex positions for each geometry
  162. // F vector of #F by 3 list of Oriented triangles for each geometry
  163. // masks a 32 bit mask to identify active geometries.
  164. // isStatic scene is optimized for static geometry
  165. // Side effects:
  166. // The first time this is ever called the embree engine is initialized.
  167. void init(
  168. const std::vector<const PointMatrixType*>& V,
  169. const std::vector<const FaceMatrixType*>& F,
  170. const std::vector<int>& masks,
  171. bool isStatic = false);
  172. // Deinitialize embree datasctructures for current mesh. Also called on
  173. // destruction: no need to call if you just want to init() once and
  174. // destroy.
  175. void deinit();
  176. // initialize view parameters
  177. void init_view();
  178. // scene data
  179. PointMatrixType V; // vertices
  180. FaceMatrixType F; // faces
  181. ColorMatrixType C; // colours
  182. Eigen::RowVector3f uC; // uniform color
  183. bool face_based;
  184. bool uniform_color;
  185. bool double_sided ;
  186. // Camera parameters
  187. float camera_base_zoom;
  188. float camera_zoom;
  189. Eigen::Vector3f camera_base_translation;
  190. Eigen::Vector3f camera_translation;
  191. Eigen::Vector3f camera_eye;
  192. Eigen::Vector3f camera_up;
  193. Eigen::Vector3f camera_center;
  194. float camera_view_angle;
  195. float camera_dnear;
  196. float camera_dfar;
  197. // projection matrixes
  198. Eigen::Matrix4f view;
  199. Eigen::Matrix4f proj;
  200. Eigen::Matrix4f norm;
  201. Eigen::Matrix3f rot_matrix;
  202. bool orthographic;
  203. // embree data
  204. RTCScene scene;
  205. unsigned geomID;
  206. bool initialized;
  207. RTCDevice device;
  208. void create_ray(
  209. RTCRayHit& ray,
  210. const Eigen::RowVector3f& origin,
  211. const Eigen::RowVector3f& direction,
  212. float tnear,
  213. float tfar,
  214. int mask) const;
  215. };
  216. }
  217. }
  218. #ifndef IGL_STATIC_LIBRARY
  219. # include "EmbreeRenderer.cpp"
  220. #endif
  221. #endif //IGL_EMBREE_EMBREE_RENDERER_H