-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.cpp
More file actions
191 lines (160 loc) · 4.6 KB
/
math.cpp
File metadata and controls
191 lines (160 loc) · 4.6 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
module;
#include <cassert>
#include <ostream>
#include <random>
#include <utility>
export module math;
export using scalar = float;
export constexpr auto INF = std::numeric_limits<scalar>::infinity();
export scalar randreal(scalar x, scalar y) {
static std::random_device rd;
static std::mt19937 e2(rd());
assert(x <= y);
return std::uniform_real_distribution<>(x, y)(e2);
}
export template <typename T> class vector_ {
public:
scalar x = 0, y = 0, z = 0;
vector_() = default;
constexpr vector_(scalar n) : x{n}, y{n}, z{n} {}
constexpr vector_(scalar x, scalar y, scalar z) : x{x}, y{y}, z{z} {}
constexpr vector_(const vector_ &v) : x{v.x}, y{v.y}, z{v.z} {};
vector_ operator-() const { return vector_(-x, -y, -z); }
vector_ operator+(const vector_ &v) const {
return vector_(x + v.x, y + v.y, z + v.z);
}
vector_ operator-(const vector_ &v) const {
return vector_(x - v.x, y - v.y, z - v.z);
}
vector_ operator*(const vector_ &v) const {
return vector_(x * v.x, y * v.y, z * v.z);
}
vector_ operator/(const vector_ &v) const {
return vector_(x / v.x, y / v.y, z / v.z);
}
void operator+=(const vector_ &v) {
x += v.x;
y += v.y;
z += v.z;
}
void operator-=(const vector_ &v) {
x -= v.x;
y -= v.y;
z -= v.z;
}
void operator*=(const vector_ &v) {
x *= v.x;
y *= v.y;
z *= v.z;
}
void operator/=(const vector_ &v) {
x /= v.x;
y /= v.y;
z /= v.z;
}
void operator*=(scalar n) {
x *= n;
y *= n;
z *= n;
}
void operator/=(scalar n) {
x /= n;
y /= n;
z /= n;
}
void operator+=(scalar n) {
x += n;
y += n;
z += n;
}
void operator-=(scalar n) {
x -= n;
y -= n;
z -= n;
}
bool operator==(const vector_ &) const = default;
bool operator!=(const vector_ &) const = default;
bool operator<(scalar n) const { return x < n && y < n && z < n; }
scalar operator[](int n) const {
switch (n) {
case 0:
return x;
case 1:
return y;
case 2:
return z;
}
assert(false);
std::unreachable();
};
scalar dot(const vector_ &v) const { return x * v.x + y * v.y + z * v.z; }
scalar norm() const { return std::sqrt(x * x + y * y + z * z); }
vector_ normalize() const {
scalar n = norm();
return n == 0 ? vector_(x, y, z) : vector_(x / n, y / n, z / n);
}
vector_ min(const vector_ &v) const {
return vector_(std::min(x, v.x), std::min(y, v.y), std::min(z, v.z));
}
vector_ max(const vector_ &v) const {
return vector_(std::max(x, v.x), std::max(y, v.y), std::max(z, v.z));
}
vector_ cross(const vector_ &v) const {
return vector_(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
}
// Should totally precompute the rotation matrix. Maybe make transformation
// class? TODO
vector_ rotate(scalar a, scalar b, scalar y) const {
return vector_(
(cos(b) * cos(y) * this->x) +
((sin(a) * sin(b) * cos(y) - cos(a) * sin(y)) * this->y) +
((cos(a) * sin(b) * cos(y) + sin(a) * sin(y)) * this->z),
(cos(b) * sin(y) * this->x) +
((sin(a) * sin(b) * sin(y) + cos(a) * cos(y)) * this->y) +
((cos(a) * sin(b) * sin(y) - sin(a) * cos(y)) * this->z),
(-sin(b) * this->x) + (sin(a) * cos(b) * this->y) +
(cos(a) * cos(b) * this->z));
}
vector_ rotate(vector_ v) const {
return vector_(x, y, z).rotate(v.x, v.y, v.z);
}
vector_ floor() {
x = (int)x;
y = (int)y;
z = (int)z;
return *this;
}
vector_ pow(scalar n) {
return vector_(std::pow(x, n), std::pow(y, n), std::pow(z, n));
}
vector_ square() { return vector_(x * x, y * y, z * z); }
scalar sum() { return x + y + z; }
vector_ clamp(scalar lo, scalar hi) {
x = std::min(std::max(x, lo), hi);
y = std::min(std::max(y, lo), hi);
z = std::min(std::max(z, lo), hi);
return *this;
}
};
export template <typename T>
vector_<T> operator*(scalar n, const vector_<T> &v) {
return vector_<T>(v.x * n, v.y * n, v.z * n);
}
export template <typename T>
vector_<T> operator*(const vector_<T> &v, scalar n) {
return vector_<T>(v.x * n, v.y * n, v.z * n);
}
export template <typename T>
vector_<T> operator/(scalar n, const vector_<T> &v) {
return vector_<T>(v.x / n, v.y / n, v.z / n);
}
export template <typename T>
vector_<T> operator/(const vector_<T> &v, scalar n) {
return vector_<T>(v.x / n, v.y / n, v.z / n);
}
template <typename T>
std::ostream &operator<<(std::ostream &os, const vector_<T> &v) {
return os << "[" << v.x << "," << v.y << "," << v.z << "]\n";
}
export using vec = vector_<scalar>;
export using veci = vector_<int>;