-
Notifications
You must be signed in to change notification settings - Fork 1
/
math_util.hpp
321 lines (275 loc) · 9.99 KB
/
math_util.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* Copyright (c) 2019-2024, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <array>
#include <cmath>
#include <cstring>
#include <stdint.h>
#define M_2PIf (2.0f * 3.141592653589793f)
#define M_PIf 3.1415926535f
#define DEG2RAD(_deg) (M_PIf * (_deg) / 180.0f)
namespace vkdd {
class Angle
{
public:
static Angle radians(float radians) { return Angle(radians); }
static Angle degree(float degree) { return Angle(M_PIf * degree / 180.0f); }
Angle()
: m_radians(0.0f)
{
}
float radians() const { return m_radians; }
float degree() const { return 180.0f * m_radians / M_PIf; }
Angle operator*(float factor) const { return Angle(factor * m_radians); }
inline Angle& operator*=(float factor);
private:
Angle(float radians)
: m_radians(radians)
{
}
float m_radians;
};
Angle& Angle::operator*=(float factor)
{
m_radians *= factor;
return *this;
}
static Angle operator*(float factor, Angle angle)
{
return angle * factor;
}
static float tanf(Angle angle)
{
return std::tanf(angle.radians());
}
static float sinf(Angle angle)
{
return std::sinf(angle.radians());
}
static float cosf(Angle angle)
{
return std::cosf(angle.radians());
}
class Vec3f
{
public:
float x, y, z;
Vec3f(float x, float y, float z)
: x(x)
, y(y)
, z(z)
{
}
Vec3f(float singleValue = 0.0f)
: Vec3f(singleValue, singleValue, singleValue)
{
}
Vec3f operator+(Vec3f const& right) const { return {x + right.x, y + right.y, z + right.z}; }
Vec3f operator-(Vec3f const& right) const { return {x - right.x, y - right.y, z - right.z}; }
Vec3f operator*(Vec3f const& factor) const { return {x * factor.x, y * factor.y, z * factor.z}; }
Vec3f& operator*=(Vec3f const& factor) { return *this = *this * factor; }
Vec3f operator*(float factor) const { return {x * factor, y * factor, z * factor}; }
Vec3f& operator*=(float factor) { return *this = *this * factor; }
};
inline Vec3f operator*(float left, Vec3f const& right)
{
return {left * right.x, left * right.y, left * right.z};
}
class Vec4f
{
public:
float x, y, z, w;
Vec4f(float x, float y, float z, float w)
: x(x)
, y(y)
, z(z)
, w(w)
{
}
Vec4f(float singleValue = 0.0f)
: Vec4f(singleValue, singleValue, singleValue, singleValue)
{
}
Vec4f(Vec3f xyz, float w)
: Vec4f(xyz.x, xyz.y, xyz.z, w)
{
}
Vec4f operator+(Vec4f const& right) const { return {x + right.x, y + right.y, z + right.z, w + right.w}; }
Vec4f operator-(Vec4f const& right) const { return {x - right.x, y - right.y, z - right.z, w - right.w}; }
};
class Mat4x4f
{
public:
static Mat4x4f fromRows(Vec4f const& p_row0, Vec4f const& p_row1, Vec4f const& p_row2, Vec4f const& p_row3)
{
return {{p_row0.x, p_row1.x, p_row2.x, p_row3.x, p_row0.y, p_row1.y, p_row2.y, p_row3.y, p_row0.z, p_row1.z,
p_row2.z, p_row3.z, p_row0.w, p_row1.w, p_row2.w, p_row3.w}};
}
static Mat4x4f fromColumns(Vec4f const& p_col0, Vec4f const& p_col1, Vec4f const& p_col2, Vec4f const& p_col3)
{
return {{p_col0.x, p_col0.y, p_col0.z, p_col0.w, p_col1.x, p_col1.y, p_col1.z, p_col1.w, p_col2.x, p_col2.y,
p_col2.z, p_col2.w, p_col3.x, p_col3.y, p_col3.z, p_col3.w}};
}
static Mat4x4f diagonal(Vec4f const& diagonalValues)
{
return Mat4x4f::fromRows({diagonalValues.x, 0.0f, 0.0f, 0.0f}, {0.0f, diagonalValues.y, 0.0f, 0.0f},
{0.0f, 0.0f, diagonalValues.z, 0.0f}, {0.0f, 0.0f, 0.0f, diagonalValues.w});
}
static Mat4x4f identity() { return Mat4x4f::diagonal(1.0f); }
static Mat4x4f rotationX(Angle amount)
{
float sina = sinf(amount);
float cosa = cosf(amount);
return Mat4x4f::fromRows({1.0f, 0.0f, 0.0f, 0.0f}, {0.0f, cosa, -sina, 0.0f}, {0.0f, sina, cosa, 0.0f},
{0.0f, 0.0f, 0.0f, 1.0f});
}
static Mat4x4f rotationY(Angle amount)
{
float sina = sinf(amount);
float cosa = cosf(amount);
return Mat4x4f::fromRows({cosa, 0.0f, sina, 0.0f}, {0.0f, 1.0f, 0.0f, 0.0f}, {-sina, 0.0f, cosa, 0.0f},
{0.0f, 0.0f, 0.0f, 1.0f});
}
static Mat4x4f rotationZ(Angle amount)
{
float sina = sinf(amount);
float cosa = cosf(amount);
return Mat4x4f::fromRows({cosa, -sina, 0.0f, 0.0f}, {sina, cosa, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 1.0f});
}
static Mat4x4f scaling(Vec3f const& scaling)
{
return Mat4x4f::fromRows({scaling.x, 0.0f, 0.0f, 0.0f}, {0.0f, scaling.y, 0.0f, 0.0f},
{0.0f, 0.0f, scaling.z, 0.0f}, {0.0f, 0.0f, 0.0f, 1.0f});
}
static Mat4x4f translation(Vec3f const& translation)
{
return Mat4x4f::fromRows({1.0f, 0.0f, 0.0f, translation.x}, {0.0f, 1.0f, 0.0f, translation.y},
{0.0f, 0.0f, 1.0f, translation.z}, {0.0f, 0.0f, 0.0f, 1.0f});
}
static Mat4x4f affineLinearTransformation(Vec3f const& scaling, Angle roll, Angle pitch, Angle yaw, Vec3f const& translation)
{
return Mat4x4f::translation(translation) * Mat4x4f::rotationY(yaw) * Mat4x4f::rotationX(pitch)
* Mat4x4f::rotationZ(roll) * Mat4x4f::scaling(scaling);
}
static Mat4x4f perspectiveProjection(Angle horFov, float aspect, float nearZ, float farZ)
{
float tana = tanf(0.5f * horFov);
float tanb = tana / aspect;
return Mat4x4f::fromRows({1.0f / tana, 0.0f, 0.0f, 0.0f}, {0.0f, -1.0f / tanb, 0.0f, 0.0f},
{0.0f, 0.0f, farZ / (farZ - nearZ), -nearZ * farZ / (farZ - nearZ)}, {0.0f, 0.0f, 1.0f, 0.0f});
}
float m_values[16];
Mat4x4f() { memset(m_values, 0, 16 * sizeof(float)); }
Mat4x4f(std::array<float, 16> const& values) { memcpy(m_values, values.data(), 16 * sizeof(float)); }
inline void set(uint32_t row, uint32_t col, float value) { m_values[4 * col + row] = value; }
inline float get(uint32_t row, uint32_t col) const { return m_values[4 * col + row]; }
inline Mat4x4f operator*(Mat4x4f const& right) const;
inline Vec4f operator*(Vec4f const& right) const;
inline Vec3f transformCoord(Vec3f const& v) const;
inline Vec3f transformVector(Vec3f const& v) const;
inline float determinant() const;
inline Mat4x4f invert(float* optOutDeterminant = nullptr) const;
private:
inline float determinant(uint32_t ignoreRow, uint32_t ignoreCol) const;
};
Mat4x4f Mat4x4f::operator*(Mat4x4f const& right) const
{
Mat4x4f result;
for(uint32_t r = 0; r < 4; ++r)
{
for(uint32_t c = 0; c < 4; ++c)
{
float v = 0.0f;
for(uint32_t i = 0; i < 4; ++i)
{
v += this->get(r, i) * right.get(i, c);
}
result.set(r, c, v);
}
}
return result;
}
Vec4f Mat4x4f::operator*(Vec4f const& right) const
{
return {this->get(0, 0) * right.x + this->get(0, 1) * right.y + this->get(0, 2) * right.z + this->get(0, 3) * right.w,
this->get(1, 0) * right.x + this->get(1, 1) * right.y + this->get(1, 2) * right.z + this->get(1, 3) * right.w,
this->get(2, 0) * right.x + this->get(2, 1) * right.y + this->get(2, 2) * right.z + this->get(2, 3) * right.w,
this->get(3, 0) * right.x + this->get(3, 1) * right.y + this->get(3, 2) * right.z + this->get(3, 3) * right.w};
}
Vec3f Mat4x4f::transformCoord(Vec3f const& v) const
{
Vec4f r = *this * Vec4f(v, 1.0f);
return {r.x / r.w, r.y / r.w, r.z / r.w};
}
Vec3f Mat4x4f::transformVector(Vec3f const& v) const
{
return {this->get(0, 0) * v.x + this->get(0, 1) * v.y + this->get(0, 2) * v.z,
this->get(1, 0) * v.x + this->get(1, 1) * v.y + this->get(1, 2) * v.z,
this->get(2, 0) * v.x + this->get(2, 1) * v.y + this->get(2, 2) * v.z};
}
float Mat4x4f::determinant() const
{
return this->get(0, 0) * this->determinant(0, 0) - this->get(0, 1) * this->determinant(0, 1)
+ this->get(0, 2) * this->determinant(0, 2) - this->get(0, 3) * this->determinant(0, 3);
}
float Mat4x4f::determinant(uint32_t ignoreRow, uint32_t ignoreCol) const
{
int32_t rows[3] = {ignoreRow < 1 ? 1 : 0, ignoreRow < 2 ? 2 : 1, ignoreRow < 3 ? 3 : 2};
int32_t cols[3] = {ignoreCol < 1 ? 1 : 0, ignoreCol < 2 ? 2 : 1, ignoreCol < 3 ? 3 : 2};
return this->get(rows[0], cols[0]) * this->get(rows[1], cols[1]) * this->get(rows[2], cols[2])
+ this->get(rows[0], cols[1]) * this->get(rows[1], cols[2]) * this->get(rows[2], cols[0])
+ this->get(rows[0], cols[2]) * this->get(rows[1], cols[0]) * this->get(rows[2], cols[1])
- this->get(rows[0], cols[2]) * this->get(rows[1], cols[1]) * this->get(rows[2], cols[0])
- this->get(rows[0], cols[0]) * this->get(rows[1], cols[2]) * this->get(rows[2], cols[1])
- this->get(rows[0], cols[1]) * this->get(rows[1], cols[0]) * this->get(rows[2], cols[2]);
}
Mat4x4f Mat4x4f::invert(float* optOutDeterminant) const
{
float det = this->determinant();
Mat4x4f inverse;
for(uint32_t r = 0; r < 4; ++r)
{
for(uint32_t c = 0; c < 4; ++c)
{
inverse.set(c, r, ((r + c) % 2 == 0 ? 1.0f : -1.0f) / det * this->determinant(r, c));
}
}
if(optOutDeterminant)
{
*optOutDeterminant = det;
}
return inverse;
}
class Aabb
{
public:
Vec3f const& getMin() const { return m_min; }
Vec3f const& getMax() const { return m_min; }
Vec3f getSize() const { return m_max - m_min; }
private:
Vec3f m_min;
Vec3f m_max;
};
template <typename T>
T lerp(T const& a, T const& b, float t)
{
return a * (1.0f - t) + t * b;
}
} // namespace vkdd