cgv
Loading...
Searching...
No Matches
fray.h
1#pragma once
2
3#include "fmat.h"
4#include "fvec.h"
5
6namespace cgv {
7namespace math {
8
10template <typename T, cgv::type::uint32_type N>
11struct fray {
12 fvec<T, N> origin;
13 fvec<T, N> direction;
14
16 fray() {
17 origin = fvec<T, N>(0);
18 direction = fvec<T, N>(0);
19 }
20
22 fray(const fvec<T, N>& origin, const fvec<T, N>& direction) : origin(origin), direction(direction) {}
23
25 fvec<T, N> position(float t) const {
26 return origin + t * direction;
27 }
28};
29
31template <typename T>
32struct fray<T, 3> {
33 fvec<T, 3> origin;
34 fvec<T, 3> direction;
35
37 fray() {
38 origin = fvec<T, 3>(0);
39 direction = fvec<T, 3>(0);
40 }
41
43 fray(const fvec<T, 3>& origin, const fvec<T, 3>& direction) : origin(origin), direction(direction) {}
44
52 fray(const fvec<T, 2>& screen_coord, const fvec<unsigned, 2>& viewport_size, const fvec<T, 3>& eye_position, const fmat<T, 4, 4>& view_projection_matrix) {
53 fvec<T, 2> window_coord = T(2) * screen_coord / static_cast<fvec<T, 2>>(viewport_size) - T(1);
54
55 fvec<T, 4> world_coord(window_coord.x(), window_coord.y(), T(1), T(1));
56 world_coord = inverse(view_projection_matrix) * world_coord;
57 world_coord /= world_coord.w();
58
59 origin = eye_position;
60 direction = normalize(fvec<T, 3>(world_coord) - eye_position);
61 }
62
64 fvec<T, 3> position(T t) const {
65 return origin + t * direction;
66 }
67};
68
69 } // namespace math
70
71// Predefined ray types
96
97} // namespace cgv
matrix of fixed size dimensions
Definition fmat.h:23
A vector with zero based index.
Definition fvec.h:29
T & y()
return second component
Definition fvec.h:146
T & x()
return first component
Definition fvec.h:142
T & w()
return fourth component
Definition fvec.h:154
this header is dependency free
Definition print.h:11
cgv::math::fray< float, 3 > ray3
3d ray using 32 bit single precision floating point type
Definition fray.h:87
cgv::math::fray< float, 3 > ray4
4d ray using 32 bit single precision floating point type
Definition fray.h:89
cgv::math::fray< double, 3 > dray4
4d ray using 32 bit double precision floating point type
Definition fray.h:95
cgv::math::fray< unsigned, 3 > uray3
3d ray using 32 bit unsigned integer type
Definition fray.h:75
cgv::math::fray< int, 2 > iray2
2d ray using 32 bit signed integer type
Definition fray.h:79
cgv::math::fray< double, 3 > dray3
3d ray using 32 bit double precision floating point type
Definition fray.h:93
cgv::math::fray< double, 2 > dray2
2d ray using 32 bit double precision floating point type
Definition fray.h:91
cgv::math::fray< int, 3 > iray3
3d ray using 32 bit signed integer type
Definition fray.h:81
cgv::math::fray< float, 2 > ray2
2d ray using 32 bit single precision floating point type
Definition fray.h:85
cgv::math::fray< int, 3 > iray4
4d ray using 32 bit signed integer type
Definition fray.h:83
cgv::math::fray< unsigned, 3 > uray4
4d ray using 32 bit unsigned integer type
Definition fray.h:77
cgv::math::fray< unsigned, 2 > uray2
2d ray using 32 bit unsigned integer type
Definition fray.h:73
fray(const fvec< T, 2 > &screen_coord, const fvec< unsigned, 2 > &viewport_size, const fvec< T, 3 > &eye_position, const fmat< T, 4, 4 > &view_projection_matrix)
Construct a ray from a given screen coordinate and view parameters.
Definition fray.h:52
fray(const fvec< T, 3 > &origin, const fvec< T, 3 > &direction)
Construct a ray with given origin and direction.
Definition fray.h:43
fray()
Construct an invalid ray with origin at zero and undefined direction.
Definition fray.h:37
fvec< T, 3 > position(T t) const
Return the position of the ray at the given distance (ray parameter t) from its origin.
Definition fray.h:64
Struct template for fixed n-dimensional rays with arbitrary data type defined by origin and direction...
Definition fray.h:11
fray()
Construct an invalid ray with origin at zero and undefined direction.
Definition fray.h:16
fray(const fvec< T, N > &origin, const fvec< T, N > &direction)
Construct a ray with given origin and direction.
Definition fray.h:22
fvec< T, N > position(float t) const
Return the position of the ray at the given distance (ray parameter t) from its origin.
Definition fray.h:25