-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBSP.cpp
88 lines (62 loc) · 1.47 KB
/
BSP.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
#include "src/BSP.h"
namespace level{
Leaf::Leaf(int width, int height, int x, int y)
:_width(width),_height(height),_x(x),_y(y)
{}
Leaf::~Leaf()
{}
bool Leaf::split()
{
bool lateralCut;
if((_left!=NULL)||(_right!=NULL))
return false;
lateralCut = util::coinToss();
if((_width > _height)&&(_width/_height >= 0.05))
lateralCut = false;
else if((_height > _width)&&(_height/_width >= 0.05))
lateralCut = true;
int tooSmall = (lateralCut ? _height:_width) - MIN_LEAF_SIZE;
if(tooSmall <= MIN_LEAF_SIZE)
return false;
int cutLocation = util::randomRange(MIN_LEAF_SIZE,tooSmall);
if(lateralCut)
{
_left = new Leaf(_x,_y,_width,cutLocation);
_right = new Leaf(_x+cutLocation,_y,_width-cutLocation,_height);
}
else
{
_left = new Leaf(_x,_y,cutLocation,_height);
_right = new Leaf(_x+cutLocation,_y,_width-cutLocation,_height);
}
return true;
}
/**************************************************************/
PartitionTree::PartitionTree()
{
_root = new Leaf(0,0,MAPWIDTH,MAPHEIGHT);
}
void PartitionTree::build()
{
_splitsLeft = true;
while(_splitsLeft)
{
_splitsLeft = false;
for( auto helper : _leaves)
{
if((helper->_left==NULL)&&(helper->_right==NULL))
{
if((helper->_width > Leaf::MAX_LEAF_SIZE)||(helper->_height > Leaf::MIN_LEAF_SIZE))
{
if(helper->split())
{
_leaves.push_back(helper->_left);
_leaves.push_back(helper->_right);
_splitsLeft = true;
}
}
}
}
}
}
}//namespace level