-
Notifications
You must be signed in to change notification settings - Fork 74
/
TA-B.h
396 lines (336 loc) · 10.8 KB
/
TA-B.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
The header/source code in C for traversal of
Binary Space Partitioning Trees
Appendix to the paper:
Fast Robust Traversal Algorithms for BSP trees
Authors: Vlastimil Havran, Tomas Kopal, Jiri Bittner, Jiri Zara
E-mail: [email protected], [email protected],
Appeared in Journal of Graphics Tools, No.4, 1998
*/
/* -------------------------------------------------------------------
Header file
*/
/* forward declarations */
/* the representation of an object*/
struct Object3D;
/* the container (list) of objects */
struct ObjectContainer;
/* the axis aligned box in the scene */
struct AxisAlignedBox;
/* this represents the ray */
struct Ray
{
float loc_x, loc_y, loc_z; /* the coordinates of the origin of a ray */
float dir_x, dir_y, dir_z; /* the coordinates of the direction of the ray */
}
/* ===================================================================
Definition of axes of spatial subdivision
*/
enum Axes { X_axis = 0, Y_axis = 1, Z_axis = 2, No_axis = 3};
/* ====================================================================
Representation of one node of BSP tree
*/
struct BSPNode {
BSPNode *left; /* pointer to the left child */
union {
ObjectContainer* objlist; /* object list for a leaf */
BSPNode *right; /* pointer to the right child */
};
/* the node can contain the list of primitives, when the node is not
a leaf .. (ternary) BSP = BSP with tagged object list */
float splitPlane; /* the position of splitting plane */
Axes splitAxis; /* the axis, where cut is performed */
};
/* the stack item required for the traversal */
struct SStackElem {
BSPNode *nodep; /* pointer to the node */
float x, y, z; /* the coordinates of the point */
float t; /* the signed distance of the point */
SStackElem *prev; /* the pointer to the previous item on the stack (trick) */
};
/* the height of the stack for traversal */
#define MAX_HEIGHT 50
/* the representation has one leaf for all empty leaves in BSP tree */
static struct BSPNode emptyLeaf;
/* the representation of the axis aligned box enclosing the whole scene */
static struct AxisAlignedBox rootBox;
/* the initialization of empty leaf */
void Init();
/* Query functions */
int IsEmptyLeaf_(BSPNode *p);
int IsFullLeaf_(BSPNode *p);
int IsLeaf_(BSPNode *p);
/* Information functions */
float GetSplitValue(BSPNode *nodep);
Axes GetSplitAxis(BSPNode *nodep);
BSPNode* GetLeft(BSPNode *nodep);
BSPNode* GetRight(BSPNode *nodep);
ObjectContainer* GetObjList(BSPNode *nodep);
/* For a given ray and rectangular bor returns minimum and maximum
signed distance corresponding to intersection of the ray with the box
Implementation can be found e.g. Graphics Gems .......
*/
int GetMinMaxT(AxisAlignedBox *bbox, Ray *ray, float *tmin, float *tmax)
/* Test objects in full leaf p and if finds the closest intersection
with object so tmin<= t <= tmax, returns the pointer to that
object, t is returned in tmax, otherwise NULL
*/
Object3D* TestFullLeaf(Ray *ray, BSPNode *p, float *tmin, float *tmax);
/* Finds the closest objects intersected by a given ray
If there is no such an object, returns NULL
*/
Object3D* FindNearest(Ray *ray, float *t);
/* -------------------------------------------------------------------
Source file
*/
/* the initialization of empty leaf */
void Init()
{
emptyLeaf.left = emptyLeaf.right = NULL;
emptyLeaf.splitPlane = 0.0;
emptyLeaf.splitAxis = No_axis;
}
/* Query functions */
int IsEmptyLeaf_(BSPNode *p)
{
return (p == &emptyLeaf);
}
int IsFullLeaf_(BSPNode *p)
{
return ((p->splitAxis == No_axis) && (p->objlist != NULL));
}
int IsLeaf_(BSPNode *p)
{
return (p->splitAxis == No_axis);
}
/* Information functions */
float GetSplitValue(BSPNode *nodep)
{
return nodep->splitPlane;
}
Axes GetSplitAxis(BSPNode *nodep)
{
return nodep->splitAxis;
}
BSPNode* GetLeft(BSPNode *nodep)
{
return nodep->left;
}
BSPNode* GetRight(BSPNode *nodep)
{
return nodep->right;
}
ObjectContainer* GetObjList(BSPNode *nodep)
{
return nodep->objlist;
}
/* Test all objects in the leaf for intersection with ray
and returns the pointer to closest one if exists
and passing through parameter returns in tmax
*/
Object3D* TestFullLeaf(Ray *ray, BSPNode *p, float *tmin, float *tmax)
{
float t; /* signed distance */
float tminc = tmin;
Object3D *retObject = NULL; /* pointer to the intersected object */
Object3D *obj; /* currently tested object in the leaf */
/* iterate the whole list and find out the nearest intersection */
for (obj = all objects in the node of BSP tree .. GetObjectList(p))
{
/* if the intersection really lies in the node */
if (obj->NearestInt(ray, t, tmax)) {
if ((t >= tminc) && (t <= tmax)) {
tmax = t;
retObject = obj;
}
}
}
return retObject;
}
/* Finds the closest objects intersected by a given ray, returns the pointer
to this object. If there is no such an object, returns NULL.
*/
Object3D* FindNearest(Ray *ray, float *t)
{
static struct SStackElem stack[MAX_HEIGHT]; /* the stack of elems */
/* signed distances */
float tdist, tmin, tmax;
/* test if the whole BSP tree is missed by the input ray or not */
if (!GetMinMaxT(&bbox, ray, &tmin, &tmax))
return NULL; /* no object can be intersected */
BSPNode *currNode = root; /* start from the root node */
/* exit point setting */
struct SStackElem *extp = &(stack[1]);
extp->x = ray->loc_x + ray->dir_x * tmax;
extp->y = ray->loc_y + ray->dir_y * tmax;
extp->z = ray->loc_z + ray->dir_z * tmax;
extp->nodep = NULL;
extp->prev = NULL;
extp->t = tmax;
/* entry point setting */
struct SStackElem *entp = &(stack[0]);
entp->nodep = NULL;
entp->prev = NULL;
/* entry point setting, tmin > 0.0 */
if (tmin > 0.0)
{ /* a ray with external origin */
entp->x = ray->loc_x + ray->dir_x * tmin;
entp->y = ray->loc_y + ray->dir_y * tmin;
entp->z = ray->loc_z + ray->dir_z * tmin;
entp->t = tmin;
}
else
{ /* a ray with internal origin */
entp->x = ray->loc_x;
entp->y = ray->loc_y;
entp->z = ray->loc_z;
entp->t = 0.0;
}
/* the pointer to the far child if any */
BSPNode *farChild;
/* loop .. traverse through whole BSP tree */
while (1)
{
/* loop .. until current node is not the leaf */
while (1)
{
/* the position of the splitting plane */
float splitVal = GetSplitValue(currNode);
/* decision based on the axis given by splitting plane */
switch (GetSplitAxis(currNode))
{
case X_axis:
{
if (entp->x <= splitVal)
{
if (extp->x <= splitVal)
{
currNode = GetLeft(currNode); /* cases N1,N2,N3,P5,Z2,Z3 */
continue;
}
/* case N4 */
farChild = GetRight(currNode);
currNode = GetLeft(currNode);
}
else {
if (splitVal <= extp->x)
{
currNode = GetRight(currNode); /* cases P1,P2,P3,N5,Z1 */
continue;
}
farChild = GetLeft(currNode); /* case P4 */
currNode = GetRight(currNode);
}
/* case N4 or P4 */
tdist = (splitVal - ray->loc_x) / ray->dir_x;
struct SStackElem *tmp = extp;
if (++extp == entp)
extp++;
extp->prev = tmp;
extp->nodep = farChild;
extp->t = tdist;
extp->x = splitVal;
extp->y = ray->loc_y + tdist * ray->dir_y;
extp->z = ray->loc_z + tdist * ray->dir_z;
continue;
}
case Y_axis:
{
if (entp->y <= splitVal)
{
if (extp->y <= splitVal)
{
currNode = GetLeft(currNode); /* case N1,N2,N3,P5,Z2,Z3 */
continue;
}
/* case N4 */
farChild = GetRight(currNode);
currNode = GetLeft(currNode);
}
else {
if (splitVal <= extp->y)
{
currNode = GetRight(currNode); /* case P1,P2,P3,N5 */
continue;
}
farChild = GetLeft(currNode); /* case P4 */
currNode = GetRight(currNode);
}
/* case N4 or P4 */
tdist = (splitVal - ray->loc_y) / ray->dir_y;
struct SStackElem *tmp = extp;
if (++extp == entp)
extp++;
extp->prev = tmp;
extp->nodep = farChild;
extp->t = tdist;
extp->x = ray->loc_x + tdist * ray->dir_x;
extp->y = splitVal;
extp->z = ray->loc_z + tdist * ray->dir_z;
continue;
}
case Z_axis:
{
if (entp->z <= splitVal)
{
if (extp->z <= splitVal)
{
currNode = GetLeft(currNode); /* case N1,N2,N3,P5,Z2,Z3 */
continue;
}
/* case N4 */
farChild = GetRight(currNode);
currNode = GetLeft(currNode);
}
else {
if (splitVal <= extp->z)
{
currNode = GetRight(currNode); /* case P1,P2,P3,N5 */
continue;
}
farChild = GetLeft(currNode); /* case P4 */
currNode = GetRight(currNode);
}
/* case N4 or P4 */
tdist = (splitVal - ray->loc_z) / ray->dir_z;
struct SStackElem *tmp = extp;
if (++extp == entp)
extp++;
extp->prev = tmp;
extp->nodep = farChild;
extp->t = tdist;
extp->x = ray->loc_x + tdist * ray->dir_x;
extp->y = ray->loc_y + tdist * ray->dir_y;
extp->z = splitVal;
continue;
}
/* test objects for intersection */
case BSPAxes::No_axis: {
goto TEST_OBJECTS;
}
} /* switch */
} /* while .. current node is not the leaf */
/* leaf can be empty or full here */
TEST_OBJECTS:
if (!IsEmptyLeaf_(currNode)) {
/* leaf contains the references to some objects */
Object3D *retObject;
tmax = extp->t;
/* test the objects in the full leaf against the ray */
if ((retObject = TestFullLeaf(ray, currNode, entp->t, &tmax)) != NULL) {
*t = tmax; /* set the signed distance for the intersection point */
return retObject; /* the first object intersected was found */
}
}
TRAVERSE_UP:
/* pop farChild from the stack */
/* restore the current values */
entp = extp;
currNode = entp->nodep;
if (currNode == NULL) /* test if the whole BSP tree was traversed */
return NULL; /* no objects found on the path of a ray */
extp = extp->prev;
} /* while .. traverse through whole BSP tree */
} /* FindNearest */
----- End of TA-B algorithm -----