-
Notifications
You must be signed in to change notification settings - Fork 0
/
AABB.cpp
190 lines (159 loc) · 4.31 KB
/
AABB.cpp
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
#include "AABB.h"
#include "Frustum.h"
namespace Math
{
AABB::AABB()
{}
AABB::AABB(XMFLOAT3 min, XMFLOAT3 max)
{
Min = min;
Max = max;
}
AABB::AABB(XMVECTOR min, XMVECTOR max)
{
XMStoreFloat3(&Min, min);
XMStoreFloat3(&Max, max);
}
void AABB::CreateFromPoints(XMFLOAT3 points[], size_t count)
{
XMVECTOR minVec = XMVectorSet(points[0].x, points[0].y, points[0].z, 1.0);
XMVECTOR maxVec = XMVectorSet(points[0].x, points[0].y, points[0].z, 1.0);
for(int i = 0; i < count; i++)
{
XMVECTOR point = XMLoadFloat3(&points[i]);
minVec = XMVectorMin(minVec, point);
maxVec = XMVectorMax(maxVec, point);
}
XMStoreFloat3(&Min, minVec);
XMStoreFloat3(&Max, maxVec);
}
XMFLOAT3* AABB::GetCorners()
{
XMFLOAT3 corners[]
{
{Min.x, Min.y, Min.z},
{Max.x, Min.y, Min.z},
{Min.x, Max.y, Min.z},
{Max.x, Max.y, Min.z},
{Min.x, Min.y, Max.z},
{Max.x, Min.y, Max.z},
{Min.x, Max.y, Max.z},
{Max.x, Max.y, Max.z}
};
for (size_t i = 0; i < 8; i++)
Corners[i] = corners[i];
return Corners;
}
void AABB::Transform(XMMATRIX matrix)
{
XMStoreFloat3(&Min, XMVector3Transform(XMLoadFloat3(&Min), matrix));
XMStoreFloat3(&Max, XMVector3Transform(XMLoadFloat3(&Max), matrix));
}
void AABB::Transform(AABB &aabb, XMMATRIX matrix)
{
XMStoreFloat3(&aabb.Min, XMVector3Transform(XMLoadFloat3(&Min), matrix));
XMStoreFloat3(&aabb.Max, XMVector3Transform(XMLoadFloat3(&Max), matrix));
}
bool AABB::Intersects(XMFLOAT3 point)
{
if(point.x < Min.x ||
point.x > Max.x ||
point.y < Min.y ||
point.y > Max.y ||
point.z < Min.z ||
point.z > Max.z)
{
return false;
}
else
{
return true;
}
}
bool AABB::Intersects(XMFLOAT4 plane)
{
float d = std::max(Min.x * plane.x, Max.x * plane.x)
+ std::max(Min.y * plane.y, Max.y * plane.y)
+ std::max(Min.z * plane.z, Max.z * plane.z)
+ plane.w;
return d > 0.0;
//bool intersect = false;
//if(plane.x * Min.x + plane.y * Min.y + plane.z * Min.z + plane.w > 0.0)
// intersect = true;
//if(plane.x * Max.x + plane.y * Min.y + plane.z * Min.z + plane.w > 0.0)
// intersect = true;
//if(plane.x * Min.x + plane.y * Max.y + plane.z * Min.z + plane.w > 0.0)
// intersect = true;
//if(plane.x * Max.x + plane.y * Max.y + plane.z * Min.z + plane.w > 0.0)
// intersect = true;
//if(plane.x * Min.x + plane.y * Min.y + plane.z * Max.z + plane.w > 0.0)
// intersect = true;
//if(plane.x * Max.x + plane.y * Min.y + plane.z * Max.z + plane.w > 0.0)
// intersect = true;
//if(plane.x * Min.x + plane.y * Max.y + plane.z * Max.z + plane.w > 0.0)
// intersect = true;
//if(plane.x * Max.x + plane.y * Max.y + plane.z * Max.z + plane.w > 0.0)
// intersect = true;
//return intersect;
/*XMFLOAT3 PP = Min;
if(plane.x >= 0)
PP.x = Max.x;
if(plane.y >= 0)
PP.y = Max.y;
if(plane.z >= 0)
PP.z = Max.z;
XMVECTOR V = XMLoadFloat3(&PP);
XMVECTOR P = XMLoadFloat4(&plane);
float d = XMVectorGetX(XMPlaneDotCoord(P, V));
return d > 0.0;*/
}
bool AABB::Contains(Frustum frustum)
{
/* TODO: bad done here need a fix.
* Because the question is not if frustum contains box but the reverse and
* this is not the same.
*/
int i;
ContainmentType contained;
auto corners = frustum.GetCorners();
bool result;
// First we check if frustum is in box.
for(i = 0; i < 8; i += 1)
{
result = Intersects( corners[i]);
if(result==false)
{
break;
}
}
// This means we checked all the corners and they were all contain or instersect
if(i == 8)
{
contained= ContainmentType::CONTAINS;
}
// If i is not equal to zero, we can fastpath and say that this box intersects
if(i != 0)
{
contained =ContainmentType::INTERSECTS;
}
/* If we get here, it means the first (and only) point we checked was
* actually contained in the frustum. So we assume that all other points
* will also be contained. If one of the points is disjoint, we can
* exit immediately saying that the result is Intersects
*/
i += 1;
for(; i < 8; i += 1)
{
result = Intersects( corners[i]);
if(result==false)
{
contained = ContainmentType::INTERSECTS;
}
}
return contained == ContainmentType::CONTAINS;
/* If we get here, then we know all the points were actually contained,
* therefore result is Contains.
*/
return false;
}
}