-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec3.hpp
99 lines (81 loc) · 1.78 KB
/
vec3.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#pragma once
#include <cmath>
template <typename FloatT> struct unit3;
template <typename FloatT> struct point3;
template <typename FloatT> struct ray3;
template <typename FloatT>
struct vec3
{
using unit_type = unit3<FloatT>;
using point_type = point3<FloatT>;
using ray_type = ray3<FloatT>;
const FloatT x, y, z;
vec3(FloatT x_, FloatT y_, FloatT z_)
: x(x_), y(y_), z(z_)
{}
FloatT magnitude() const
{
return sqrt(dot(*this));
}
FloatT sqrMagnitude() const
{
return dot(*this);
}
unit_type unit() const;
vec3 operator *(FloatT scalar) const
{
return {
x * scalar,
y * scalar,
z * scalar
};
}
vec3 operator /(FloatT denominator) const
{
return operator *(1 / denominator);
}
vec3 operator +(const vec3& rhs) const
{
return {
x + rhs.x,
y + rhs.y,
z + rhs.z
};
}
vec3 operator -(const vec3& rhs) const
{
return {
x - rhs.x,
y - rhs.y,
z - rhs.z
};
}
vec3 operator -() const
{
return {
-x,
-y,
-z
};
}
vec3 cross(const vec3& rhs) const
{
return {
y * rhs.z - z * rhs.y,
z * rhs.x - x * rhs.z,
x * rhs.y - y * rhs.x
};
}
FloatT dot(const vec3& rhs) const
{
return x * rhs.x + y * rhs.y + z * rhs.z;
}
vec3 reflectedIn(const unit_type& normal) const;
vec3 projectedOn(const vec3& rhs) const;
point_type projectedOn(const ray_type& r) const;
};
template <typename FloatT>
vec3<FloatT> operator *(FloatT scalar, const vec3<FloatT>& v)
{
return v * scalar;
}