-
Notifications
You must be signed in to change notification settings - Fork 2
/
hbb.cpp
240 lines (219 loc) · 4.88 KB
/
hbb.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
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
#include "hbb.h"
#include "mempool.h"
namespace meshlang{
typedef mempool<HBB::AABB> apool;
void HBB::AABB::autoclean(){
if(left==NULL && right==NULL && !isDataNode()){
if(parent){
if(parent->left==this){
parent->left=NULL;
}
if(parent->right==this){
parent->right=NULL;
}
parent->autoclean();
hbb->delAABB(this);
}
}else
if(parent && parent->parent){
if(parent->left && parent->right==NULL)
parent->left=NULL;
else
if(parent->left==NULL && parent->right)
parent->right=NULL;
else
return;
if(parent->parent->left==parent){
parent->parent->left=this;
}else{
parent->parent->right=this;
}
auto tmp=parent;
parent=parent->parent;
hbb->delAABB(tmp);
parent->autoclean();
}
}
void HBB::AABB::add(AABB * in){
if(left){
if(!left->isDataNode() && in->inBox(left)){
left->add(in);
return;
}else
if(right==NULL){
setRight(in);
return;
}
}
if(right){
if(!right->isDataNode() && in->inBox(right)){
right->add(in);
return;
}else
if(left==NULL){
setLeft(in);
return;
}
}
if(right==NULL && left==NULL){
setLeft(in);
return;
}
auto ls=left->getMergeSizeSq(in);
auto rs=right->getMergeSizeSq(in);
auto nnode=hbb->createAABB();
//nnode->parent=this;
if(ls<rs){
in->merge(left,nnode);
nnode->setLeft(left);
nnode->setRight(in);
this->setLeft(nnode);
}else{
in->merge(right,nnode);
nnode->setLeft(right);
nnode->setRight(in);
this->setRight(nnode);
}
//debug
//printf(
// "create:(%f,%f,%f)->(%f,%f,%f)\n",
// nnode->from.X,nnode->from.Y,nnode->from.Z,
// nnode->to.X,nnode->to.Y,nnode->to.Z
//);
}
void HBB::poolInit(){
pool=new apool;
}
void HBB::poolDestroy(){
if(pool)
delete (apool*)pool;
}
HBB::AABB * HBB::createAABB(){
if(pool){
auto p=((apool*)pool)->get();
p->construct();
p->hbb=this;
return p;
}
return NULL;
}
void HBB::delAABB(HBB::AABB * p){
if(pool){
((apool*)pool)->del(p);
}
}
void HBB::AABB::remove(){
if(parent){
if(parent->left==this){
parent->left=NULL;
}
if(parent->right==this){
parent->right=NULL;
}
parent->autoclean();
parent=NULL;
}
}
void HBB::AABB::drop(){
if(left){
left->drop();
left=NULL;
}
if(right){
right->drop();
right=NULL;
}
if(parent){
if(parent->left==this){
parent->left=NULL;
}
if(parent->right==this){
parent->right=NULL;
}
parent=NULL;
}
hbb->delAABB(this);
}
void HBB::AABB::autodrop(){
auto p=parent;
this->drop();
if(p)
p->autoclean();
}
void HBB::add(HBB::AABB * in){
root->add(in);
}
void HBB::remove(HBB::AABB * in){
in->remove();
}
HBB::AABB * HBB::add(const HBB::vec & from,const HBB::vec & to,void * data){
auto p=createAABB();
p->from=from;
p->to=to;
p->data=data;
root->add(p);
return p;
}
void HBB::AABB::collisionTest(
const AABB * in,
void(*callback)(AABB *,void *),
void * arg
){
if(left && left->intersects(in)){
if(left->isDataNode())
callback(left,arg);
else
left->collisionTest(in,callback,arg);
}
if(right && right->intersects(in)){
if(right->isDataNode())
callback(right,arg);
else
right->collisionTest(in,callback,arg);
}
}
void HBB::AABB::fetchByPoint(
const vec & point,
void(*callback)(AABB *,void *),
void * arg
){
if(left && left->inBox(point)){
if(left->isDataNode())
callback(left,arg);
else
left->fetchByPoint(point,callback,arg);
}
if(right && right->inBox(point)){
if(right->isDataNode())
callback(right,arg);
else
right->fetchByPoint(point,callback,arg);
}
}
void HBB::AABB::fetchByStep(
float step,
void(*callback)(AABB *,void *),
void * arg
){
if(left && left->onStep(step)){
if(left->isDataNode())
callback(left,arg);
else
left->fetchByStep(step,callback,arg);
}
if(right && right->onStep(step)){
if(right->isDataNode())
callback(right,arg);
else
right->fetchByStep(step,callback,arg);
}
}
HBB::HBB(){
poolInit();
root=createAABB();
}
HBB::~HBB(){
if(root)
root->drop();
}
}//namespace smoothly