cgv
Loading...
Searching...
No Matches
view.cxx
1#include <cgv/render/view.h>
2#include <cgv/math/geom.h>
3
4using namespace cgv::math;
5
6namespace cgv {
7 namespace render {
8
11 : focus(0,0,0), view_up_dir(0,1,0), view_dir(0,0,-1), y_view_angle(0), y_extent_at_focus(2)
12{
13}
14
18dvec3& view::ref_view_up_dir() { return view_up_dir; }
20dvec3& view::ref_view_dir() { return view_dir; }
22double& view::ref_y_view_angle() { return y_view_angle; }
24double& view::ref_y_extent_at_focus() { return y_extent_at_focus; }
25
27int view::compute_axis_and_angle(const dvec3& target_view_dir, const dvec3& target_view_up_dir,
28 dvec3& axis, double& angle)
29{
30 dmat3 R = cgv::math::build_orthogonal_frame(view_dir, view_up_dir);
31 R.transpose();
32 R = cgv::math::build_orthogonal_frame(target_view_dir, target_view_up_dir)*R;
33 dvec3 daxis;
34 double dangle;
35 int res = cgv::math::decompose_rotation_to_axis_and_angle(R, daxis, dangle);
36 axis = daxis;
37 angle = dangle;
38 return res;
39}
40
42double view::get_tan_of_half_of_fovy(bool ensure_non_zero) const
43{
44 return tan(.008726646260*((ensure_non_zero && (y_view_angle <= 0.01)) ? 0.01 : y_view_angle));
45}
46
48const dvec3& view::get_focus() const
49{
50 return focus;
51}
54{
55 return view_up_dir;
56}
59{
60 return view_dir;
61}
64{
65 return y_view_angle;
66}
69{
70 return y_extent_at_focus;
71}
74{
75 return 0.5f*y_extent_at_focus / get_tan_of_half_of_fovy(true);
76}
77
79double view::get_y_extent_at_depth(double depth, bool ensure_non_zero) const
80{
81 return 2.0f*depth*get_tan_of_half_of_fovy(ensure_non_zero);
82}
83
85void view::set_focus(const dvec3& foc)
86{
87 focus = foc;
88}
89void view::set_focus(double x, double y, double z) { set_focus(dvec3(x,y,z)); }
90
93{
94 view_up_dir = normalize(vud);
95}
96void view::set_view_up_dir(double x, double y, double z) { set_view_up_dir(dvec3(x,y,z)); }
99{
100 view_dir = normalize(vd);
101}
102void view::set_view_dir(double x, double y, double z) { set_view_dir(dvec3(x,y,z)); }
105{
106 y_extent_at_focus = ext;
107}
109void view::set_y_view_angle(double angle)
110{
111 y_view_angle = angle;
112}
113
116{
117 return y_view_angle == 0;
118}
119
121
123const dvec3 view::get_eye() const
124{
125 return focus - (0.5f*y_extent_at_focus / get_tan_of_half_of_fovy(true))*view_dir;
126}
127
129
133{
134 dvec3 new_view_dir = focus - eye;
135 dvec3::value_type l = new_view_dir.length();
136 if (l < 10 * std::numeric_limits<dvec3::value_type>::epsilon())
137 return false;
138 dvec3::value_type inv_l = dvec3::value_type(1) / l;
139 view_dir = inv_l*new_view_dir;
140 y_extent_at_focus = get_y_extent_at_depth(l, true);
141 return true;
142}
143
145
149{
150 dvec3 new_view_dir = focus - eye;
151 dvec3::value_type l = new_view_dir.length();
152 if (l < 10 * std::numeric_limits<dvec3::value_type>::epsilon())
153 return false;
154 dvec3::value_type inv_l = dvec3::value_type(1) / l;
155 view_dir = inv_l*new_view_dir;
156 y_view_angle = atan(0.5*y_extent_at_focus*inv_l)*114.5915590;
157 return true;
158}
159
161void view::view_look_at_keep_extent(const dvec3& e, const dvec3& foc, const dvec3& vud)
162{
163 set_focus(foc);
164 set_view_up_dir(vud);
166}
167
169void view::view_look_at_keep_view_angle(const dvec3& e, const dvec3& foc, const dvec3& vud)
170{
171 set_focus(foc);
172 set_view_up_dir(vud);
174}
175
177void view::enable_viewport_splitting(unsigned nr_cols, unsigned nr_rows)
178{}
180bool view::is_viewport_splitting_enabled(unsigned* nr_cols_ptr, unsigned* nr_rows_ptr) const
181{
182 return false;
183}
188void view::activate_split_viewport(context& ctx, unsigned col_index, unsigned row_index)
189{}
193
195void view::enable_viewport_individual_view(unsigned col_index, unsigned row_index, bool enable)
196{
197}
198
200bool view::does_viewport_use_individual_view(unsigned col_index, unsigned row_index) const
201{
202 return false;
203}
204
206view& view::ref_viewport_view(unsigned col_index, unsigned row_index)
207{
208 return *this;
209}
210
212{
213 z = -view_dir;
214 z.normalize();
215 x = cross(view_up_dir, z);
216 x.normalize();
217 y = cross(z, x);
218}
219
220
221void view::put_coordinate_system(vec3& x, vec3& y, vec3& z) const
222{
223 dvec3 dx, dy, dz;
224 put_coordinate_system(dx, dy, dz);
225 x = dx;
226 y = dy;
227 z = dz;
228}
229
231void view::roll(double angle)
232{
233 view_up_dir = normalize(cgv::math::rotate(view_up_dir, view_dir, angle));
234}
235
237
242void view::rotate(double axis_direction_x, double axis_direction_y, double axis_point_depth)
243{
244 dvec3 x, y, z;
245 put_coordinate_system(x, y, z);
246 dvec3 axis_dir = axis_direction_x*x + axis_direction_y*y;
247 double angle = axis_dir.length();
248 if (angle < 10 * std::numeric_limits<double>::epsilon())
249 return;
250 axis_dir *= 1.0 / angle;
251 dvec3 axis_point = get_eye() + axis_point_depth*view_dir;
252 focus = cgv::math::rotate(focus - axis_point, axis_dir, angle) + axis_point;
253 view_dir = normalize(cgv::math::rotate(view_dir, axis_dir, angle));
254 view_up_dir = normalize(cgv::math::rotate(view_up_dir, axis_dir, angle));
255}
256
258void view::move(double step)
259{
260 focus += step*view_dir;
261}
262
264void view::pan(double step_x, double step_y)
265{
266 dvec3 x, y, z;
267 put_coordinate_system(x, y, z);
268 focus += step_x*x + step_y* y;
269}
270
272void view::zoom(double factor)
273{
274 y_extent_at_focus *= factor;
275}
276
277int view::get_modelview_projection_window_matrices(int x, int y, int width, int height,
278 const dmat4** DPV_pptr,
279 const dmat4** DPV_other_pptr, int* x_other_ptr, int* y_other_ptr,
280 int* vp_col_idx_ptr, int* vp_row_idx_ptr,
281 int* vp_width_ptr, int *vp_height_ptr,
282 int* vp_center_x_ptr, int* vp_center_y_ptr,
283 int* vp_center_x_other_ptr, int* vp_center_y_other_ptr) const
284{
285 return 0;
286}
288
290double view::get_z_and_unproject(context& ctx, int x, int y, dvec3& p)
291{
292 return 0.0;
293}
294
295double view::get_z_and_unproject(context& ctx, int x, int y, vec3& p)
296{
297 dvec3 dp(p);
298 double res = get_z_and_unproject(ctx, x, y, dp);
299 p = dp;
300 return res;
301}
302
304void view::compute_screen_rectangle(std::vector<dvec3>& rect, double depth, double aspect) const
305{
306 // compute view aligned coordinate system
307 dvec3 x, y, z;
308 put_coordinate_system(x, y, z);
309
310 // compute center of screen covering rectangle
311 dvec3 c = get_eye() - z*depth;
312
313 // scale x- and y-direction vectors to cover screen rectangle
314 double y_scale = 0.5*get_y_extent_at_depth(depth, true);
315 y *= y_scale;
316 x *= y_scale*aspect;
317
318 // construct rectangle corners
319 rect.push_back(c + x + y);
320 rect.push_back(c - x + y);
321 rect.push_back(c - x - y);
322 rect.push_back(c + x - y);
323}
324
325void view::compute_screen_rectangle(std::vector<vec3>& rect, double depth, double aspect) const
326{
327 std::vector<dvec3> drect;
328 compute_screen_rectangle(drect, depth, aspect);
329 rect.clear();
330 for (const auto& p : drect)
331 rect.push_back(vec3(p));
332}
333
334
335 }
336}
matrix of fixed size dimensions
Definition fmat.h:23
void transpose()
transpose matrix
Definition fmat.h:231
T normalize()
normalize the vector using the L2-Norm and return the length
Definition fvec.h:302
T length() const
length of the vector L2-Norm
Definition fvec.h:249
base class for all drawables, which is independent of the used rendering API.
Definition context.h:621
defines a symmetric view with the following quantities:
Definition view.h:22
virtual double get_z_and_unproject(context &ctx, int x, int y, dvec3 &p)
given an opengl pixel location x,y return the window z-value from the depth buffer
Definition view.cxx:290
double & ref_y_extent_at_focus()
write access to extent at focus
Definition view.cxx:24
bool set_eye_keep_view_angle(const dvec3 &eye)
set the view dir and y extent at focus keeping focus and y view angle such that get_eye() returns the...
Definition view.cxx:132
virtual int get_modelview_projection_window_matrices(int x, int y, int width, int height, const dmat4 **MPW_pptr, const dmat4 **MPW_other_pptr=0, int *x_other_ptr=0, int *y_other_ptr=0, int *vp_col_idx_ptr=0, int *vp_row_idx_ptr=0, int *vp_width_ptr=0, int *vp_height_ptr=0, int *vp_center_x_ptr=0, int *vp_center_y_ptr=0, int *vp_center_x_other_ptr=0, int *vp_center_y_other_ptr=0) const
given an opengl pixel location and the size in pixels of the opengl window, return the modelview_proj...
Definition view.cxx:277
const dvec3 & get_focus() const
query focus point
Definition view.cxx:48
virtual bool is_viewport_splitting_enabled(unsigned *nr_cols_ptr=0, unsigned *nr_rows_ptr=0) const
check whether viewport splitting is activated and optionally set the number of columns and rows if co...
Definition view.cxx:180
double get_y_extent_at_focus() const
query y extent of viewing window at focus point
Definition view.cxx:68
virtual bool does_viewport_use_individual_view(unsigned col_index, unsigned row_index) const
check whether viewport manage its own view
Definition view.cxx:200
void rotate(double axis_direction_x, double axis_direction_y, double axis_point_depth)
rotated view around axis by angle
Definition view.cxx:242
dvec3 & ref_view_up_dir()
write access to view up direction
Definition view.cxx:18
bool set_eye_keep_extent(const dvec3 &eye)
set view dir and y view angle keeping focus and y extent such that get_eye() returns the passed point...
Definition view.cxx:148
const dvec3 get_eye() const
query the eye point, which is computed from focus, view dir, y extent at focus and y view angle
Definition view.cxx:123
virtual void set_view_dir(const dvec3 &vd)
set view direction without ensuring orthogonality to view up direction
Definition view.cxx:98
double get_y_extent_at_depth(double depth, bool ensure_non_zero) const
get y extent of viewing window at an arbitrary depth, if ensure_non_zero is true, replace y view angl...
Definition view.cxx:79
dvec3 & ref_focus()
write access to focus point
Definition view.cxx:16
double & ref_y_view_angle()
write access to view angle
Definition view.cxx:22
view()
construct a parallel view with focus in the world origin looking in negative z-direction and the y-di...
Definition view.cxx:10
virtual void set_y_extent_at_focus(double ext)
set y extent of viewing window at focus point keeping y view angle resulting in a zoom
Definition view.cxx:104
dvec3 focus
focus of the view
Definition view.h:25
virtual void set_view_up_dir(const dvec3 &vud)
set view up direction without ensuring orthogonality to view direction
Definition view.cxx:92
double get_y_view_angle() const
query opening angle (degrees) of view in y-direction
Definition view.cxx:63
dvec3 & ref_view_dir()
write access to view dir
Definition view.cxx:20
const dvec3 & get_view_dir() const
query current view direction
Definition view.cxx:58
virtual view & ref_viewport_view(unsigned col_index, unsigned row_index)
access the view of a given viewport
Definition view.cxx:206
void put_coordinate_system(dvec3 &x, dvec3 &y, dvec3 &z) const
construct coordinate system with z in negative view direction and x and y aligned with the right and ...
Definition view.cxx:211
void compute_screen_rectangle(std::vector< dvec3 > &rect, double depth, double aspect) const
fill rect with four points covering the screen rectangle at given depth from eye with given aspect ra...
Definition view.cxx:304
void view_look_at_keep_view_angle(const dvec3 &eye, const dvec3 &foc, const dvec3 &vud)
set the view according to the standard view lookat definition from eye, focus and view up direction k...
Definition view.cxx:169
virtual void enable_viewport_splitting(unsigned nr_cols, unsigned nr_rows)
call this function before a drawing process to support viewport splitting inside the draw call via th...
Definition view.cxx:177
double get_tan_of_half_of_fovy(bool ensure_non_zero) const
compute tan of half of y view angle, if ensure_non_zero is true, replace y view angles < 0....
Definition view.cxx:42
virtual void enable_viewport_individual_view(unsigned col_index, unsigned row_index, bool enable=true)
make a viewport manage its own view
Definition view.cxx:195
virtual void deactivate_split_viewport(context &ctx)
deactivate the previously split viewport
Definition view.cxx:191
double get_depth_of_focus() const
return the depth of the focus point
Definition view.cxx:73
const dvec3 & get_view_up_dir() const
query current view up direction
Definition view.cxx:53
virtual void disable_viewport_splitting()
disable viewport splitting
Definition view.cxx:185
int compute_axis_and_angle(const dvec3 &target_view_dir, const dvec3 &target_view_up_dir, dvec3 &axis, double &angle)
compute axis and angle of a rotation that the current view_dir and view_up_dir to the given target_vi...
Definition view.cxx:27
virtual void set_y_view_angle(double angle)
set opening angle (degrees) of view in y-direction keeping y extent at focus resulting in a dolly zoo...
Definition view.cxx:109
void move(double step)
move along view direction by given step length in world coordinates
Definition view.cxx:258
virtual void activate_split_viewport(context &ctx, unsigned col_index, unsigned row_index)
inside the drawing process activate the sub-viewport with the given column and row indices,...
Definition view.cxx:188
void view_look_at_keep_extent(const dvec3 &eye, const dvec3 &foc, const dvec3 &vud)
set the view according to the standard view lookat definition from eye, focus and view up direction k...
Definition view.cxx:161
void pan(double step_x, double step_y)
move in screen x and screen y directions by given step lengths in world coordinates
Definition view.cxx:264
void roll(double angle)
roll view around view direction by angle
Definition view.cxx:231
virtual void set_focus(const dvec3 &foc)
set a new focus point keeping y extent at focus and y view angle fix, such that the eye position is a...
Definition view.cxx:85
void zoom(double factor)
zoom by given factor
Definition view.cxx:272
bool is_parallel() const
return whether the y view angle is zero
Definition view.cxx:115
the cgv namespace
Definition print.h:11
cgv::math::fvec< double, 3 > dvec3
declare type of 3d double precision floating point vectors
Definition fvec.h:676