-
Notifications
You must be signed in to change notification settings - Fork 0
/
aabb.h
69 lines (54 loc) · 1.15 KB
/
aabb.h
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
#pragma once
#include "vector.h"
#include "matrix.h"
struct aabb
{
aabb()
{
}
aabb(const vec3& c, const vec3& s)
{
center = c; size = s;
}
vec3 min_point() const
{
return center - size;
}
vec3 max_point() const
{
return center + size;
}
float radius() const
{
return size.length();
}
float radius_squared() const
{
return dot(size, size);
}
void merge(const aabb& b)
{
vec3 minA = min_point();
vec3 maxA = max_point();
vec3 minB = b.min_point();
vec3 maxB = b.max_point();
minA.x = minA.x < minB.x ? minA.x : minB.x;
minA.y = minA.y < minB.y ? minA.y : minB.y;
minA.z = minA.z < minB.z ? minA.z : minB.z;
maxA.x = maxA.x > maxB.x ? maxA.x : maxB.x;
maxA.y = maxA.y > maxB.y ? maxA.y : maxB.y;
maxA.z = maxA.z > maxB.z ? maxA.z : maxB.z;
center = (minA + maxA) / 2.0;
size = (maxA - minA) / 2.0;
}
void mul(const mat4& mat)
{
mat3 absMat(mat);
for(unsigned i = 0; i < 9; i++)
absMat.mat[i] = absMat.mat[i] < 0.0f ? -absMat.mat[i] : absMat.mat[i];
center = mat * center;
size = absMat * size;
}
vec3 center;
vec3 size;
};