-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #188 from imadhammani/main
XCore intersectMesh: BVH
- Loading branch information
Showing
9 changed files
with
295 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include "AABB.h" | ||
#include "mesh.h" | ||
|
||
AABB::AABB() | ||
: xmin(EFLOATMAX), ymin(EFLOATMAX), zmin(EFLOATMAX), | ||
xmax(EFLOATMIN), ymax(EFLOATMIN), zmax(EFLOATMIN) | ||
{} | ||
|
||
AABB::AABB(const IMesh &M, E_Int *ids, E_Int count) | ||
{ | ||
xmin = ymin = zmin = EFLOATMAX; | ||
xmax = ymax = zmax = EFLOATMIN; | ||
|
||
for (E_Int i = 0; i < count; i++) { | ||
E_Int fid = ids[i]; | ||
|
||
const auto &pn = M.F[fid]; | ||
|
||
for (E_Int p : pn) { | ||
E_Float x = M.X[p]; | ||
E_Float y = M.Y[p]; | ||
E_Float z = M.Z[p]; | ||
if (x < xmin) xmin = x; | ||
if (y < ymin) ymin = y; | ||
if (z < zmin) zmin = z; | ||
if (x > xmax) xmax = x; | ||
if (y > ymax) ymax = y; | ||
if (z > zmax) zmax = z; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
#pragma once | ||
|
||
#include "xcore.h" | ||
|
||
struct IMesh; | ||
|
||
struct AABB { | ||
E_Float xmin = EFLOATMAX; | ||
E_Float xmax = EFLOATMIN; | ||
E_Float ymin = EFLOATMAX; | ||
E_Float ymax = EFLOATMIN; | ||
E_Float zmin = EFLOATMAX; | ||
E_Float zmax = EFLOATMIN; | ||
E_Float xmin; | ||
E_Float ymin; | ||
E_Float zmin; | ||
E_Float xmax; | ||
E_Float ymax; | ||
E_Float zmax; | ||
|
||
AABB(); | ||
|
||
AABB(const IMesh &M, E_Int *ids, E_Int count); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#include "BVH.h" | ||
#include "mesh.h" | ||
|
||
// Creates the tree | ||
|
||
static size_t leaf_count = 0; | ||
|
||
BVH_node *BVH_create_node(AABB aabb, BVH_node *left_child, BVH_node *right_child, | ||
E_Int *ids, E_Int count, Mem_arena &arena) | ||
{ | ||
BVH_node *node = (BVH_node *)ARENA_ALLOC(sizeof(BVH_node)); | ||
|
||
node->bbox = aabb; | ||
node->left = left_child; | ||
node->right = right_child; | ||
node->elements = (E_Int *)ARENA_ALLOC(count * sizeof(E_Int)); | ||
assert(count > 0); | ||
memcpy(node->elements, ids, count * sizeof(E_Int)); | ||
|
||
leaf_count++; | ||
|
||
return node; | ||
} | ||
|
||
|
||
BVH_node *BVH_create_node(AABB aabb, BVH_node *left_child, | ||
BVH_node *right_child, Mem_arena &arena) | ||
{ | ||
BVH_node *node = (BVH_node *)ARENA_ALLOC(sizeof(BVH_node)); | ||
|
||
node->bbox = aabb; | ||
node->left = left_child; | ||
node->right = right_child; | ||
node->elements = NULL; | ||
|
||
return node; | ||
} | ||
|
||
BVH_node *BVH_create_node(E_Int *ids, E_Int count, const IMesh &M, | ||
Mem_arena &arena) | ||
{ | ||
AABB aabb(M, ids, count); | ||
|
||
if (count <= MAX_FACES_PER_LEAF) { | ||
return BVH_create_node(aabb, NULL, NULL, ids, count, arena); | ||
} | ||
|
||
E_Float dx = aabb.xmax - aabb.xmin; | ||
E_Float dy = aabb.ymax - aabb.ymin; | ||
E_Float dz = aabb.zmax - aabb.zmin; | ||
|
||
E_Float *pCoor; | ||
|
||
if (dx >= dy && dx >= dz) { | ||
pCoor = (E_Float *)M.X.data(); | ||
} else if (dy >= dz) { | ||
assert(dy >= dx); | ||
pCoor = (E_Float *)M.Y.data(); | ||
} else { | ||
assert(dz >= dx && dz >= dy); | ||
pCoor = (E_Float *)M.Z.data(); | ||
} | ||
|
||
std::sort(ids, ids + count, [&] (E_Int i, E_Int j) | ||
{ | ||
E_Float cci = 0.0, ccj = 0.0; | ||
|
||
const auto &pni = M.F[i]; | ||
for (const E_Int p : pni) cci += pCoor[p]; | ||
cci /= pni.size(); | ||
|
||
const auto &pnj = M.F[j]; | ||
for (const E_Int p : pnj) ccj += pCoor[p]; | ||
ccj /= pnj.size(); | ||
|
||
return cci < ccj; | ||
}); | ||
|
||
BVH_node *left = BVH_create_node(ids, count/2, M, arena); | ||
BVH_node *right = BVH_create_node(ids + count/2, count - count/2, M, arena); | ||
|
||
return BVH_create_node(aabb, left, right, arena); | ||
} | ||
|
||
BVH::BVH(const IMesh &M) | ||
{ | ||
if (M.skin.empty()) { | ||
puts("empty skin!"); | ||
exit(1); | ||
} | ||
|
||
size_t NF = M.skin.size(); | ||
|
||
printf("Number of faces: %zu\n", NF); | ||
|
||
arena.reserve(10 * NF * sizeof(E_Int)); | ||
|
||
E_Int *ids = (E_Int *)ARENA_ALLOC(NF * sizeof(E_Int)); | ||
|
||
for (size_t i = 0; i < NF; i++) ids[i] = M.skin[i]; | ||
|
||
root = BVH_create_node(ids, NF, M, arena); | ||
|
||
printf("Leaves: %zu\n", leaf_count); | ||
|
||
arena.print_stats(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
|
||
#include "AABB.h" | ||
#include "common/mem.h" | ||
|
||
#define MAX_FACES_PER_LEAF 10 | ||
|
||
struct IMesh; | ||
|
||
struct BVH_node { | ||
AABB bbox; | ||
BVH_node *left; | ||
BVH_node *right; | ||
E_Int *elements; | ||
}; | ||
|
||
BVH_node *BVH_create_node(AABB aabb, BVH_node *left_child, | ||
BVH_node *right_child, Mem_arena &arena); | ||
|
||
BVH_node *BVH_create_node(AABB aabb, BVH_node *left_child, BVH_node *right_child, | ||
E_Int *ids, E_Int count, Mem_arena &arena); | ||
|
||
BVH_node *BVH_create_node(E_Int *ids, E_Int count, const IMesh &M, | ||
Mem_arena &arena); | ||
|
||
struct BVH { | ||
BVH_node *root; | ||
|
||
Mem_arena arena; | ||
|
||
BVH(const IMesh &M); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters