-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMath.hpp
More file actions
88 lines (68 loc) · 2.35 KB
/
Math.hpp
File metadata and controls
88 lines (68 loc) · 2.35 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
#ifndef MATH_HPP
#define MATH_HPP
#include <algorithm>
#include <limits>
#include <cmath>
#include <initializer_list>
struct Vector3 {
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 operator-(const Vector3& other) const {
return Vector3(x - other.x, y - other.y, z - other.z);
}
float dot(const Vector3& other) const {
return x * other.x + y * other.y + z * other.z;
}
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
);
}
};
struct Triangle {
int a, b, c;
Triangle() : a(0), b(0), c(0) {}
Triangle(int a_, int b_, int c_) : a(a_), b(b_), c(c_) {}
};
struct AABB {
Vector3 min;
Vector3 max;
bool RayIntersects(const Vector3& rayOrigin, const Vector3& rayDir) const {
float tmin = std::numeric_limits<float>::lowest();
float tmax = std::numeric_limits<float>::max();
const float* rayOriginArr = &rayOrigin.x;
const float* rayDirArr = &rayDir.x;
const float* minArr = &min.x;
const float* maxArr = &max.x;
for (int i = 0; i < 3; ++i) {
float invDir = 1.0f / rayDirArr[i];
float t0 = (minArr[i] - rayOriginArr[i]) * invDir;
float t1 = (maxArr[i] - rayOriginArr[i]) * invDir;
if (invDir < 0.0f) std::swap(t0, t1);
tmin = std::max(tmin, t0);
tmax = std::min(tmax, t1);
}
return tmax >= tmin && tmax >= 0;
}
};
struct TriangleCombined {
Vector3 v0, v1, v2;
TriangleCombined() = default;
TriangleCombined(const Vector3& v0_, const Vector3& v1_, const Vector3& v2_)
: v0(v0_), v1(v1_), v2(v2_) {
}
AABB ComputeAABB() const {
Vector3 min_point, max_point;
min_point.x = std::min({ v0.x, v1.x, v2.x });
min_point.y = std::min({ v0.y, v1.y, v2.y });
min_point.z = std::min({ v0.z, v1.z, v2.z });
max_point.x = std::max({ v0.x, v1.x, v2.x });
max_point.y = std::max({ v0.y, v1.y, v2.y });
max_point.z = std::max({ v0.z, v1.z, v2.z });
return { min_point, max_point };
}
};
#endif