-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhbb.h
231 lines (195 loc) · 7.48 KB
/
hbb.h
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
#ifndef MESHLANG_HBB
#define MESHLANG_HBB
#include <math.h>
#include <cmath>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
namespace meshlang{
class HBB{
public:
struct vec{
float X,Y;
vec(){
X=0;
Y=0;
}
vec(const vec & i){
X=i.X;
Y=i.Y;
}
vec(float iX , float iY){
X=iX;
Y=iY;
}
const vec & operator=(const vec & i){
X=i.X;
Y=i.Y;
return * this;
}
void set(float iX , float iY){
X=iX;
Y=iY;
}
vec operator+(const vec & i)const{
vec tmp;
tmp.X = X+i.X;
tmp.Y = Y+i.Y;
return tmp;
}
vec operator-(const vec & i)const{
vec tmp;
tmp.X = X-i.X;
tmp.Y = Y-i.Y;
return tmp;
}
vec operator*(float i)const{
vec tmp;
tmp.X = X*i;
tmp.Y = Y*i;
return tmp;
}
vec operator/(float i)const{
vec tmp;
tmp.X = X/i;
tmp.Y = Y/i;
return tmp;
}
};
class AABB{
public:
AABB * left ,
* right ,
* parent ,
* next;
HBB * hbb;
vec from,to;
void * data;
inline bool isDataNode(){
return data!=NULL;
}
inline void setLeft(AABB * in){
left=in;
in->parent=this;
}
inline void setRight(AABB * in){
right=in;
in->parent=this;
}
inline float getMergeSizeSq(const AABB * other)const{
vec tf,tt;
tf.X = std::min(from.X, other->from.X);
tf.Y = std::min(from.Y, other->from.Y);
tt.X = std::max(to.X, other->to.X);
tt.Y = std::max(to.Y, other->to.Y);
auto l=tt-tf;
return (l.X*l.X + l.Y*l.Y);
}
inline void merge(const AABB * other , AABB * out)const{
out->from.X = std::min(from.X, other->from.X);
out->from.Y = std::min(from.Y, other->from.Y);
out->to.X = std::max(to.X, other->to.X);
out->to.Y = std::max(to.Y, other->to.Y);
}
inline bool isEmpty()const{
return from.X > to.X || from.Y > to.Y;
}
inline bool inBox(const vec & point)const{
return (
(point.X>=from.X && point.X<=to.X) &&
(point.Y>=from.Y && point.Y<=to.Y)
);
}
inline bool onStep(float p)const{
return (p>=from.X) && (p<=to.X);
}
inline bool intersects(const AABB * in)const{
return (
(from.X >= in->from.X && from.X <= in->to.X) ||
(in->from.X >= from.X && in->from.X <= to.X)
) &&(
(from.Y >= in->from.Y && from.Y <= in->to.Y) ||
(in->from.Y >= from.Y && in->from.Y <= to.Y)
);
}
inline bool inBox(const AABB * in)const{
return (
((from.X >= in->from.X) && (to.X <= in->to.X)) &&
((from.Y >= in->from.Y) && (to.Y <= in->to.Y))
);
}
inline float getSizeSq()const{
auto l=to-from;
return (l.X*l.X + l.Y*l.Y);
}
inline vec getCenter() const{
return (from + to) / 2;
}
inline vec getExtent() const{
return to - from;
}
void construct(){
left=NULL;
right=NULL;
parent=NULL;
data=NULL;
from.set(0,0);
to.set(0,0);
}
void collisionTest(
const AABB * in,
void(*callback)(AABB *,void *),
void * arg=NULL
);
void fetchByPoint(
const vec & point,
void(*callback)(AABB *,void *),
void * arg=NULL
);
void fetchByStep(
float step,
void(*callback)(AABB *,void *),
void * arg=NULL
);
void autoclean();
void autodrop();
void add(AABB * in);
void remove();
void drop();
};
AABB * root;
AABB * createAABB();
void delAABB(AABB *);
void add(AABB * in);
void remove(AABB * in);
AABB * add(const vec & from,const vec & to,void * data);
inline void collisionTest(
const AABB * in,
void(*callback)(AABB *,void *),
void * arg=NULL
){
root->collisionTest(in,callback,arg);
}
inline void fetchByPoint(
const vec & point,
void(*callback)(AABB *,void *),
void * arg=NULL
){
root->fetchByPoint(point,callback,arg);
}
void fetchByStep(
float step,
void(*callback)(AABB *,void *),
void * arg=NULL
){
root->fetchByStep(step,callback,arg);
}
HBB();
~HBB();
private:
void poolInit();
void poolDestroy();
void * pool;
};
}
#endif