-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.h
More file actions
174 lines (139 loc) · 4.15 KB
/
Copy pathvector.h
File metadata and controls
174 lines (139 loc) · 4.15 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#pragma once
#include <math.h>
class Vector3 {
public:
float x, y, z;
Vector3(): x(0), y(0), z(0) {}
Vector3(float x, float y, float z): x(x), y(y), z(z) {}
Vector3(float x): x(x), y(x), z(x) {}
Vector3 operator+(const Vector3& other) const {
return Vector3(x + other.x, y + other.y, z + other.z);
}
Vector3 operator+(float scalar) const {
return Vector3(x + scalar, y + scalar, z + scalar);
}
Vector3 operator-(const Vector3& other) const {
return Vector3(x - other.x, y - other.y, z - other.z);
}
Vector3 operator-(float scalar) const {
return Vector3(x - scalar, y - scalar, z - scalar);
}
Vector3 operator*(float scalar) const {
return Vector3(x * scalar, y * scalar, z * scalar);
}
Vector3 operator*(const Vector3& other) const {
return Vector3(x * other.x, y * other.y, z * other.z);
}
Vector3 operator/(const Vector3& other) const {
return Vector3(x / other.x, y / other.y, z / other.z);
}
Vector3 operator/(float scalar) const {
return Vector3(x / scalar, y / scalar, z / scalar);
}
Vector3 operator+=(const Vector3& other) {
x += other.x; y += other.y; z += other.z;
return *this;
}
Vector3 operator+=(float scalar) {
x += scalar; y += scalar; z += scalar;
return *this;
}
Vector3 operator-=(const Vector3& other) {
x -= other.x; y -= other.y; z -= other.z;
return *this;
}
Vector3 operator-=(float scalar) {
x -= scalar; y -= scalar; z -= scalar;
return *this;
}
Vector3 operator*=(const Vector3& other) {
x *= other.x; y *= other.y; z *= other.z;
return *this;
}
Vector3 operator*=(float scalar) {
x *= scalar; y *= scalar; z *= scalar;
return *this;
}
Vector3 operator/=(const Vector3& other) {
x /= other.x; y /= other.y; z /= other.z;
return *this;
}
Vector3 operator/=(float scalar) {
x /= scalar; y /= scalar; z /= scalar;
return *this;
}
bool operator==(const Vector3& other) const {
return x == other.x && y == other.y && z == other.z;
}
bool operator!=(const Vector3& other) const {
return !(*this == other);
}
float dot(const Vector3& other) const {
return x * other.x + y * other.y + z * other.z;
}
Vector3 operator+() {
return *this;
}
Vector3 operator-() {
return *this * -1;
}
Vector3 cross(const Vector3& other) const {
return Vector3(
y * other.z - z * other.y,
z * other.x - x * other.z,
x * other.y - y * other.x
);
}
float magnitude() const {
return sqrt(x * x + y * y + z * z);
}
Vector3 normalize() const {
float mag = magnitude();
return Vector3(x / mag, y / mag, z / mag);
}
Vector3 linear_to_srgb() const {
return Vector3(
powf(x, 1.0f / 2.2f),
powf(y, 1.0f / 2.2f),
powf(z, 1.0f / 2.2f)
);
}
Vector3 srgb_to_linear() const {
return Vector3(
powf(x, 2.2f),
powf(y, 2.2f),
powf(z, 2.2f)
);
}
Vector3 reinhard_tone_map() const {
return Vector3(
x / (1.0f + x),
y / (1.0f + y),
z / (1.0f + z)
);
}
Vector3 aces_tone_map() const {
const float a = 2.51f;
const float b = 0.03f;
const float c = 2.43f;
const float d = 0.59f;
const float e = 0.14f;
return Vector3(
(x * (a * x + b)) / (x * (c * x + d) + e),
(y * (a * y + b)) / (y * (c * y + d) + e),
(z * (a * z + b)) / (z * (c * z + d) + e)
);
}
};
inline Vector3 operator+(const float lhs, const Vector3 rhs) {
return rhs + lhs;
}
inline Vector3 operator-(const float lhs, const Vector3 rhs) {
return rhs * -1 + lhs;
}
inline Vector3 operator*(const float lhs, const Vector3 rhs) {
return rhs * lhs;
}
inline Vector3 operator/(const float lhs, const Vector3 rhs) {
return Vector3(lhs / rhs.x, lhs / rhs.y, lhs / rhs.z);
}