-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath3.h
More file actions
52 lines (42 loc) · 1.49 KB
/
Copy pathmath3.h
File metadata and controls
52 lines (42 loc) · 1.49 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
// Vec3 + Rodrigues (mesh / slab twist).
#pragma once
#include <cmath>
struct Vec3 {
float x = 0.f;
float y = 0.f;
float z = 0.f;
Vec3() = default;
Vec3(float ax, float ay, float az) : x(ax), y(ay), z(az) {}
Vec3 operator+(Vec3 o) const { return {x + o.x, y + o.y, z + o.z}; }
Vec3 operator-(Vec3 o) const { return {x - o.x, y - o.y, z - o.z}; }
Vec3 operator*(float s) const { return {x * s, y * s, z * s}; }
Vec3 operator/(float s) const { return {x / s, y / s, z / s}; }
};
inline float dot(Vec3 a, Vec3 b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
inline Vec3 cross(Vec3 a, Vec3 b) {
return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x};
}
inline float length(Vec3 v) {
return std::sqrt(dot(v, v));
}
inline Vec3 normalize(Vec3 v) {
const float len = length(v);
if (len < 1e-20f) {
return {0.f, 0.f, 0.f};
}
return v * (1.f / len);
}
inline Vec3 rotateAroundAxis(Vec3 v, Vec3 axisUnit, float angleRad) {
const float c = std::cos(angleRad);
const float s = std::sin(angleRad);
const Vec3 k = axisUnit;
const float t = 1.f - c;
return {v.x * (t * k.x * k.x + c) + v.y * (t * k.x * k.y - s * k.z) +
v.z * (t * k.x * k.z + s * k.y),
v.x * (t * k.x * k.y + s * k.z) + v.y * (t * k.y * k.y + c) +
v.z * (t * k.y * k.z - s * k.x),
v.x * (t * k.x * k.z - s * k.y) + v.y * (t * k.y * k.z + s * k.x) +
v.z * (t * k.z * k.z + c)};
}