Repository navigation
Expand file tree
/
Copy pathOctree.cpp
More file actions
292 lines (231 loc) · 8 KB
/
Copy pathOctree.cpp
File metadata and controls
292 lines (231 loc) · 8 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
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
#define NOMINMAX
#include "Octree.h"
#include <algorithm>
#include "FBXmodel.h"
using namespace DirectX;
static void CalcTriangleBounds(const TriangleData& tri, XMFLOAT3& minOut, XMFLOAT3& maxOut)
{
minOut.x = std::min(std::min(tri.v0.x, tri.v1.x), tri.v2.x);
minOut.y = std::min(std::min(tri.v0.y, tri.v1.y), tri.v2.y);
minOut.z = std::min(std::min(tri.v0.z, tri.v1.z), tri.v2.z);
maxOut.x = std::max(std::max(tri.v0.x, tri.v1.x), tri.v2.x);
maxOut.y = std::max(std::max(tri.v0.y, tri.v1.y), tri.v2.y);
maxOut.z = std::max(std::max(tri.v0.z, tri.v1.z), tri.v2.z);
}
static bool TriangleInBox(const TriangleData& tri, const XMFLOAT3& boxMin, const XMFLOAT3& boxMax)
{
XMFLOAT3 triMin, triMax;
CalcTriangleBounds(tri, triMin, triMax);
return !(triMax.x < boxMin.x || triMin.x > boxMax.x ||
triMax.y < boxMin.y || triMin.y > boxMax.y ||
triMax.z < boxMin.z || triMin.z > boxMax.z);
}
static bool RayIntersectAABB(XMVECTOR rayOrigin, XMVECTOR rayDir, const XMFLOAT3& boxMin, const XMFLOAT3& boxMax)
{
XMVECTOR min = XMLoadFloat3(&boxMin);
XMVECTOR max = XMLoadFloat3(&boxMax);
XMVECTOR invDir = XMVectorReciprocal(rayDir);
XMVECTOR t1 = XMVectorMultiply(XMVectorSubtract(min, rayOrigin), invDir);
XMVECTOR t2 = XMVectorMultiply(XMVectorSubtract(max, rayOrigin), invDir);
XMVECTOR tmin = XMVectorMin(t1, t2);
XMVECTOR tmax = XMVectorMax(t1, t2);
float tNear = std::max({ XMVectorGetX(tmin), XMVectorGetY(tmin), XMVectorGetZ(tmin) });
float tFar = std::min({ XMVectorGetX(tmax), XMVectorGetY(tmax), XMVectorGetZ(tmax) });
return tNear <= tFar && tFar > 0.0f;
}
bool TriangleRayIntersect(XMVECTOR rayOrigin, XMVECTOR rayDir, XMVECTOR v0, XMVECTOR v1, XMVECTOR v2, float* outDist)
{
const float EPSILON = 1e-6f;
XMVECTOR edge1 = XMVectorSubtract(v1, v0);
XMVECTOR edge2 = XMVectorSubtract(v2, v0);
XMVECTOR h = XMVector3Cross(rayDir, edge2);
float a = XMVectorGetX(XMVector3Dot(edge1, h));
if (fabs(a) < EPSILON) return false;
float f = 1.0f / a;
XMVECTOR s = XMVectorSubtract(rayOrigin, v0);
float u = f * XMVectorGetX(XMVector3Dot(s, h));
if (u < 0.0f || u > 1.0f) return false;
XMVECTOR q = XMVector3Cross(s, edge1);
float v = f * XMVectorGetX(XMVector3Dot(rayDir, q));
if (v < 0.0f || u + v > 1.0f) return false;
float t = f * XMVectorGetX(XMVector3Dot(edge2, q));
if (t > EPSILON) {
*outDist = t;
return true;
}
return false;
}
OctreeNode* BuildOctree(const std::vector<TriangleData>& triangleList, const XMFLOAT3& minBound, const XMFLOAT3& maxBound, int depth, int maxDepth, int minTri)
{
OctreeNode* node = new OctreeNode;
node->minBound = minBound;
node->maxBound = maxBound;
node->isSubdivided = false;
for (int i = 0; i < triangleList.size(); ++i) {
if (TriangleInBox(triangleList[i], minBound, maxBound)) {
node->triangleIndices.push_back(i);
}
}
/*if (depth == 0) {
XMFLOAT3 center = {
(minBound.x + maxBound.x) * 0.5f,
(minBound.y + maxBound.y) * 0.5f,
(minBound.z + maxBound.z) * 0.5f
};
for (int i = 0; i < 8; i++) {
XMFLOAT3 cmin = minBound, cmax = center;
if (i & 1) { cmin.x = center.x; cmax.x = maxBound.x; }
if (i & 2) { cmin.y = center.y; cmax.y = maxBound.y; }
if (i & 4) { cmin.z = center.z; cmax.z = maxBound.z; }
node->children[i] = BuildOctree(triangleList, cmin, cmax, depth + 1, 1, minTri);
}
}*/
return node;
}
bool RayHitOctree(OctreeNode* node, const std::vector<TriangleData>& triangleList,
const XMFLOAT3& origin, const XMFLOAT3& dir,
float* closestDist, XMFLOAT3* hitPos, XMFLOAT3* hitNormal,
int depth, int maxDepth, int minTri)
{
XMVECTOR rayOrigin = XMLoadFloat3(&origin);
XMVECTOR rayDir = XMVector3Normalize(XMLoadFloat3(&dir));
if (!RayIntersectAABB(rayOrigin, rayDir, node->minBound, node->maxBound))
return false;
if (!node->isSubdivided && node->triangleIndices.size() > minTri && depth < maxDepth) {
Subdivide(node, triangleList, depth, maxDepth, minTri);
}
bool hit = false;
float minDist = *closestDist;
for (int idx : node->triangleIndices) {
const TriangleData& tri = triangleList[idx];
float dist;
if (TriangleRayIntersect(
rayOrigin, rayDir,
XMLoadFloat3(&tri.v0),
XMLoadFloat3(&tri.v1),
XMLoadFloat3(&tri.v2),
&dist)) {
if (dist < minDist && dist > 0.0f) {
minDist = dist;
XMVECTOR hitPoint = XMVectorAdd(rayOrigin, XMVectorScale(rayDir, dist));
XMStoreFloat3(hitPos, hitPoint);
XMVECTOR v0 = XMLoadFloat3(&tri.v0);
XMVECTOR v1 = XMLoadFloat3(&tri.v1);
XMVECTOR v2 = XMLoadFloat3(&tri.v2);
XMVECTOR edge1 = XMVectorSubtract(v1, v0);
XMVECTOR edge2 = XMVectorSubtract(v2, v0);
XMVECTOR normal = XMVector3Normalize(XMVector3Cross(edge1, edge2));
if (XMVectorGetX(XMVector3Dot(rayDir, normal)) > 0) {
normal = XMVectorNegate(normal);
}
XMStoreFloat3(hitNormal, normal);
hit = true;
}
}
}
if (node->isSubdivided) {
for (int i = 0; i < 8; ++i) {
if (!node->children[i]) continue;
XMFLOAT3 childHitPos;
XMFLOAT3 childHitNormal;
float childMinDist = minDist;
if (RayHitOctree(node->children[i], triangleList, origin, dir,
&childMinDist, &childHitPos, &childHitNormal,
depth + 1, maxDepth, minTri)) {
if (childMinDist < minDist) {
minDist = childMinDist;
*hitPos = childHitPos;
*hitNormal = childHitNormal;
hit = true;
}
}
}
}
if (hit) {
*closestDist = minDist;
}
return hit;
}
void DeleteOctree(OctreeNode* node)
{
if (!node) return;
for (int i = 0; i < 8; i++) {
DeleteOctree(node->children[i]);
node->children[i] = nullptr;
}
delete node;
}
bool AABBvsTriangle(const XMFLOAT3& boxMin, const XMFLOAT3& boxMax,
const XMFLOAT3& v0, const XMFLOAT3& v1, const XMFLOAT3& v2)
{
XMFLOAT3 triMin, triMax;
triMin.x = std::min({ v0.x, v1.x, v2.x });
triMin.y = std::min({ v0.y, v1.y, v2.y });
triMin.z = std::min({ v0.z, v1.z, v2.z });
triMax.x = std::max({ v0.x, v1.x, v2.x });
triMax.y = std::max({ v0.y, v1.y, v2.y });
triMax.z = std::max({ v0.z, v1.z, v2.z });
bool overlap =
!(triMax.x < boxMin.x || triMin.x > boxMax.x ||
triMax.y < boxMin.y || triMin.y > boxMax.y ||
triMax.z < boxMin.z || triMin.z > boxMax.z);
return overlap;
}
bool AABBHitOctree(OctreeNode* node, const std::vector<TriangleData>& triangleList,
const XMFLOAT3& boxMin, const XMFLOAT3& boxMax,
int depth, int maxDepth, int minTri)
{
XMFLOAT3 nmin = node->minBound;
XMFLOAT3 nmax = node->maxBound;
bool overlap =
!(nmax.x < boxMin.x || nmin.x > boxMax.x ||
nmax.y < boxMin.y || nmin.y > boxMax.y ||
nmax.z < boxMin.z || nmin.z > boxMax.z);
if (!overlap) return false;
if (!node->isSubdivided && node->triangleIndices.size() > minTri && depth < maxDepth) {
Subdivide(node, triangleList, depth, maxDepth, minTri);
}
for (int idx : node->triangleIndices) {
const TriangleData& tri = triangleList[idx];
if (AABBvsTriangle(boxMin, boxMax, tri.v0, tri.v1, tri.v2)) {
return true;
}
}
if (node->isSubdivided) {
for (int i = 0; i < 8; i++) {
if (!node->children[i]) continue;
if (AABBHitOctree(node->children[i], triangleList, boxMin, boxMax,
depth + 1, maxDepth, minTri)) {
return true;
}
}
}
return false;
}
void Subdivide(OctreeNode* node, const std::vector<TriangleData>& triangleList, int depth, int maxDepth, int minTri)
{
if (node->isSubdivided || depth >= maxDepth || node->triangleIndices.size() <= minTri)
return;
node->isSubdivided = true;
XMFLOAT3 center = {
(node->minBound.x + node->maxBound.x) * 0.5f,
(node->minBound.y + node->maxBound.y) * 0.5f,
(node->minBound.z + node->maxBound.z) * 0.5f
};
for (int i = 0; i < 8; i++) {
XMFLOAT3 cmin = node->minBound, cmax = center;
if (i & 1) { cmin.x = center.x; cmax.x = node->maxBound.x; }
if (i & 2) { cmin.y = center.y; cmax.y = node->maxBound.y; }
if (i & 4) { cmin.z = center.z; cmax.z = node->maxBound.z; }
OctreeNode* child = new OctreeNode;
child->minBound = cmin;
child->maxBound = cmax;
for (int idx : node->triangleIndices) {
if (TriangleInBox(triangleList[idx], cmin, cmax)) {
child->triangleIndices.push_back(idx);
}
}
node->children[i] = child;
}
node->triangleIndices.clear();
}