-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOctreeBuilder.cpp
More file actions
85 lines (74 loc) · 2.52 KB
/
OctreeBuilder.cpp
File metadata and controls
85 lines (74 loc) · 2.52 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
#include "OctreeBuilder.h"
#include "voxelizationMath.cu"
/** Forward declaration of CUDA calls **/
//Return AABB of triangle meshes
extern void getBoundingBox(uint n, const glm::fmat3x3* d_tri_meshes,
glm::fmat2x3& bounding_box);
//Return true if there is a intersection between a node and triangle meshes
extern bool isBoxleIntersectsTriangle(uint n, const glm::fmat3x3* d_tri_meshes,
const glm::fvec3& min,const glm::fvec3& max);
//Return the shortest dist between a node and triangle meshes.
extern float calcLeafNodeSignedDistance(uint n, const glm::fmat3x3* d_tri_meshes,
const glm::fvec3& center);
////////////////////////////////////////////////////////////////////////////////////
Octree* OctreeBuilder::buildOctree(const std::vector<glm::fmat3x3>& tri_meshes,
float voxel_size)
{
//copy tri_meshes to divice pointer
m_num_tri_meshes = tri_meshes.size();
cudaMalloc(&m_d_tri_meshes, m_num_tri_meshes*sizeof(glm::fmat3x3));
cudaMemcpy(m_d_tri_meshes, tri_meshes.data(), m_num_tri_meshes*sizeof(glm::fmat3x3));
//create a new octree
m_octree = new Octree(voxel_size);
//get AABB of triangle meshes to create a root node
Node* root = m_octree.getRoot();
root = new Node();
glm::fmat2x3 bounding_box;
getBoundingBox(m_num_tri_meshes, m_d_tri_meshes, bounding_box);
root->AABB = bounding_box;
//start subdivide
recSubDivision(root);
}
void OctreeBuilder::recSubDivision(Node* pNode)
{
//parallel
bool is_intersect = isBoxleIntersectsTriangle(m_num_tri_meshes, d_tri_meshes,
pNode->m_box.min,
pNode->m_box.max);
if (is_intersect)
{
//LEAF Nodes
if (pNode->dim == octree->voxel_dim()) // default voxel_dim is '1'
{
pNode->m_node_type = NT_LEAF;
//parallel
float dist = calcLeafNodeSignedDistance(m_num_tri_meshes, d_tri_meshes,
pNode->m_box.center);
pNode->m_signed_dist = dist;
}
//Internel Nodes
else
{
pNode->m_node_type = NT_INTERNEL;
pNode->m_signed_dist = 0.0f;
pNode->m_children = new Node[8];
for (int i = 0; i < 8; ++i)
{
Node* cNode = new Node();
cNode.m_parent = pNode;
setAABB(cNode, i); //set m_box for current child Node
pNode->m_children[i] = cNode;
recSubDivision(cNode);
}
}
}
//EMPTY Nodes
else
{
pNode->m_type = NT_EMPTY;
//parallel
float dist = calcLeafNodeSignedDistance(m_num_tri_meshes, d_tri_meshes,
pNode->m_box.center);
pNode->m_signed_dist = dist;
}
}