cgv
Loading...
Searching...
No Matches
ray.h
1#pragma once
2
3#include "fmat.h"
4#include "fvec.h"
5#include "inv.h"
6
7namespace cgv {
8 namespace math {
9
13template <typename T, cgv::type::uint32_type N>
14class ray {
15public:
16 fvec<T, N> origin;
17 fvec<T, N> direction;
18
20 ray() {
21 origin = fvec<T, N>(0);
22 direction = fvec<T, N>(0);
23 }
24
26 ray(const fvec<T, N>& origin, const fvec<T, N>& direction) : origin(origin), direction(direction) {}
27
29 fvec<T, N> position(float t) const {
30
31 return origin + t * direction;
32 }
33};
34
38template <typename T>
39class ray<T, 3> {
40public:
41 fvec<T, 3> origin;
42 fvec<T, 3> direction;
43
45 ray() {
46 origin = fvec<T, 3>(0);
47 direction = fvec<T, 3>(0);
48 }
49
51 ray(const fvec<T, 3>& origin, const fvec<T, 3>& direction) : origin(origin), direction(direction) {}
52
61 ray(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) {
62
63 fvec<T, 2> window_coord = T(2) * screen_coord / static_cast<fvec<T, 2>>(viewport_size) - T(1);
64
65 fvec<T, 4> world_coord(window_coord.x(), window_coord.y(), T(1), T(1));
66 world_coord = inv(view_projection_matrix) * world_coord;
67 world_coord /= world_coord.w();
68
69 origin = eye_position;
70 direction = normalize(fvec<T, 3>(world_coord) - eye_position);
71 }
72
74 fvec<T, 3> position(float t) const {
75
76 return origin + t * direction;
77 }
78};
79
80// Definition of some typical ray types
81typedef ray<unsigned, 2> uray2;
82typedef ray<unsigned, 3> uray3;
83typedef ray<unsigned, 3> uray4;
84typedef ray<int, 2> iray2;
85typedef ray<int, 3> iray3;
86typedef ray<int, 3> iray4;
87typedef ray<float, 2> ray2;
88typedef ray<float, 3> ray3;
89typedef ray<float, 3> ray4;
90typedef ray<double, 2> dray2;
91typedef ray<double, 3> dray3;
92typedef ray<double, 3> dray4;
93
94 }
95}
matrix of fixed size dimensions
Definition fmat.h:23
A vector with zero based index.
Definition fvec.h:26
T & y()
second element
Definition fvec.h:159
T & x()
first element
Definition fvec.h:155
T & w()
fourth element
Definition fvec.h:167
fvec< T, 3 > position(float t) const
Returns the position of the ray at the given distance (ray parameter t) from its origin.
Definition ray.h:74
ray()
Construct an invalid ray with origin at zero and undefined direction.
Definition ray.h:45
ray(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 ray.h:61
ray(const fvec< T, 3 > &origin, const fvec< T, 3 > &direction)
Construct a ray with given origin and direction.
Definition ray.h:51
This class defines a template for n-dimensional rays with arbitrary data type defined by origin and d...
Definition ray.h:14
fvec< T, N > position(float t) const
Returns the position of the ray at the given distance (ray parameter t) from its origin.
Definition ray.h:29
ray(const fvec< T, N > &origin, const fvec< T, N > &direction)
Construct a ray with given origin and direction.
Definition ray.h:26
ray()
Construct an invalid ray with origin at zero and undefined direction.
Definition ray.h:20
the cgv namespace
Definition print.h:11