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