author | Radek Brich <radek.brich@devl.cz> |
Wed, 12 Dec 2007 19:59:19 +0100 | |
branch | pyrit |
changeset 35 | fb170fccb19f |
child 36 | b490093b0ac3 |
permissions | -rw-r--r-- |
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
1 |
/* |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
2 |
* Pyrit Ray Tracer |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
3 |
* file: octree.cc |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
4 |
* |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
5 |
* Radek Brich, 2006-2007 |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
6 |
*/ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
7 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
8 |
#include "octree.h" |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
9 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
10 |
OctreeNode::~OctreeNode() |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
11 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
12 |
if (shapes != NULL) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
13 |
delete shapes; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
14 |
else |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
15 |
delete[] children; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
16 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
17 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
18 |
void OctreeNode::subdivide(BBox bbox, int maxdepth) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
19 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
20 |
if (maxdepth <= 0 || shapes->size() <= 4) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
21 |
return; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
22 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
23 |
// make children |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
24 |
children = new OctreeNode[8]; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
25 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
26 |
// evaluate centres for axes |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
27 |
const Float xsplit = (bbox.L.x + bbox.H.x)*0.5; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
28 |
const Float ysplit = (bbox.L.y + bbox.H.y)*0.5; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
29 |
const Float zsplit = (bbox.L.z + bbox.H.z)*0.5; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
30 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
31 |
// set bounding boxes for children |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
32 |
BBox childbb[8] = {bbox, bbox, bbox, bbox, bbox, bbox, bbox, bbox}; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
33 |
for (int i = 0; i < 4; i++) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
34 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
35 |
// this is little obfuscated, so on right are listed affected children |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
36 |
// the idea is to cut every axis once per child, making 8 combinations |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
37 |
childbb[i].H.x = xsplit; // 0,1,2,3 |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
38 |
childbb[i+4].L.x = xsplit; // 4,5,6,7 |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
39 |
childbb[i+(i>>1<<1)].H.y = ysplit; // 0,1,4,5 |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
40 |
childbb[i+(i>>1<<1)+2].L.y = ysplit;// 2,3,6,7 |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
41 |
childbb[i<<1].H.z = zsplit; // 0,2,4,6 |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
42 |
childbb[(i<<1)+1].L.z = zsplit; // 1,3,5,7 |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
43 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
44 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
45 |
// distribute shapes to children |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
46 |
ShapeList::iterator sh; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
47 |
BBox shbb; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
48 |
int child, both; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
49 |
unsigned int shapenum = 0; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
50 |
for (sh = shapes->begin(); sh != shapes->end(); sh++) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
51 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
52 |
child = 0; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
53 |
both = 0; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
54 |
shbb = (*sh)->get_bbox(); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
55 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
56 |
if (shbb.L.x >= xsplit) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
57 |
child |= 4; //right |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
58 |
else if (shbb.H.x > xsplit) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
59 |
both |= 4; // both |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
60 |
// for left, do nothing |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
61 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
62 |
if (shbb.L.y >= ysplit) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
63 |
child |= 2; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
64 |
else if (shbb.H.y > ysplit) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
65 |
both |= 2; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
66 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
67 |
if (shbb.L.z >= zsplit) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
68 |
child |= 1; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
69 |
else if (shbb.H.z > zsplit) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
70 |
both |= 1; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
71 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
72 |
if (!both) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
73 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
74 |
getChild(child)->addShape(*sh); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
75 |
shapenum++; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
76 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
77 |
else |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
78 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
79 |
// shape goes to more than one child |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
80 |
if (both == 7) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
81 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
82 |
for (int i = 0; i < 8; i++) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
83 |
getChild(i)->addShape(*sh); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
84 |
shapenum += 8; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
85 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
86 |
else if (both == 3 || both >= 5) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
87 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
88 |
if (both == 3) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
89 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
90 |
for (int i = 0; i < 4; i++) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
91 |
getChild(child + i)->addShape(*sh); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
92 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
93 |
else if (both == 5) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
94 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
95 |
for (int i = 0; i < 4; i++) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
96 |
getChild(child + i+(i>>1<<1))->addShape(*sh); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
97 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
98 |
else if (both == 6) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
99 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
100 |
for (int i = 0; i < 4; i++) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
101 |
getChild(child + (i<<1))->addShape(*sh); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
102 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
103 |
shapenum += 4; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
104 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
105 |
else |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
106 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
107 |
getChild(child)->addShape(*sh); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
108 |
getChild(child+both)->addShape(*sh); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
109 |
shapenum += 2; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
110 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
111 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
112 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
113 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
114 |
if (shapes->size() <= 8 && shapenum > 2*shapes->size()) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
115 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
116 |
// bad subdivision, revert |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
117 |
delete[] children; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
118 |
return; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
119 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
120 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
121 |
// remove shapes and set this node to non-leaf |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
122 |
delete shapes; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
123 |
shapes = NULL; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
124 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
125 |
// recursive subdivision |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
126 |
for (int i = 0; i < 8; i++) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
127 |
children[i].subdivide(childbb[i], maxdepth-1); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
128 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
129 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
130 |
void Octree::build() |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
131 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
132 |
dbgmsg(1, "* building octree\n"); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
133 |
root = new OctreeNode(); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
134 |
ShapeList::iterator shape; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
135 |
for (shape = shapes.begin(); shape != shapes.end(); shape++) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
136 |
root->addShape(*shape); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
137 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
138 |
root->subdivide(bbox, max_depth); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
139 |
built = true; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
140 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
141 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
142 |
static inline int first_node(const Float tx0, const Float ty0, const Float tz0, |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
143 |
const Float txm, const Float tym, const Float tzm) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
144 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
145 |
int res = 0; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
146 |
if (tx0 > ty0) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
147 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
148 |
if (tx0 > tz0) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
149 |
{ // YZ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
150 |
if (tym < tx0) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
151 |
res |= 2; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
152 |
if (tzm < tx0) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
153 |
res |= 1; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
154 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
155 |
else |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
156 |
{ // XY |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
157 |
if (txm < tz0) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
158 |
res |= 4; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
159 |
if (tym < tz0) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
160 |
res |= 2; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
161 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
162 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
163 |
else |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
164 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
165 |
if (ty0 > tz0) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
166 |
{ // XZ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
167 |
if (txm < ty0) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
168 |
res |= 4; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
169 |
if (tzm < ty0) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
170 |
res |= 1; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
171 |
return res; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
172 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
173 |
else |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
174 |
{ // XY |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
175 |
if (txm < tz0) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
176 |
res |= 4; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
177 |
if (tym < tz0) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
178 |
res |= 2; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
179 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
180 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
181 |
return res; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
182 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
183 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
184 |
static inline int next_node(const Float txm, const int xnode, |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
185 |
const Float tym, const int ynode, const Float tzm, const int znode) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
186 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
187 |
if (txm < tym) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
188 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
189 |
if (txm < tzm) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
190 |
return xnode; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
191 |
else |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
192 |
return znode; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
193 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
194 |
else |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
195 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
196 |
if (tym < tzm) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
197 |
return ynode; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
198 |
else |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
199 |
return znode; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
200 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
201 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
202 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
203 |
static Shape *proc_subtree(const int a, const Float tx0, const Float ty0, const Float tz0, |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
204 |
const Float tx1, const Float ty1, const Float tz1, OctreeNode *node, |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
205 |
const Shape *origin_shape, const Ray &ray, Float &nearest_distance) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
206 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
207 |
Float txm, tym, tzm; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
208 |
int curr_node; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
209 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
210 |
// if ray does not intersect this node |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
211 |
if (tx1 < 0.0 || ty1 < 0.0 || tz1 < 0.0) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
212 |
return NULL; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
213 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
214 |
if (node->isLeaf()) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
215 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
216 |
Shape *nearest_shape = NULL; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
217 |
ShapeList::iterator shape; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
218 |
Float mindist = max(max(tx0,ty0),tz0); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
219 |
Float dist = min(min(min(tx1,ty1),tz1),nearest_distance); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
220 |
for (shape = node->shapes->begin(); shape != node->shapes->end(); shape++) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
221 |
if (*shape != origin_shape && (*shape)->intersect(ray, dist) && dist >= mindist) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
222 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
223 |
nearest_shape = *shape; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
224 |
nearest_distance = dist; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
225 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
226 |
return nearest_shape; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
227 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
228 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
229 |
txm = 0.5 * (tx0+tx1); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
230 |
tym = 0.5 * (ty0+ty1); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
231 |
tzm = 0.5 * (tz0+tz1); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
232 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
233 |
curr_node = first_node(tx0,ty0,tz0,txm,tym,tzm); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
234 |
Shape *shape = NULL; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
235 |
while (curr_node < 8) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
236 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
237 |
switch (curr_node) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
238 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
239 |
case 0: |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
240 |
shape =proc_subtree (a,tx0,ty0,tz0,txm,tym,tzm,node->getChild(a), origin_shape, ray, nearest_distance); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
241 |
curr_node = next_node(txm, 4, tym, 2, tzm, 1); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
242 |
break; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
243 |
case 1: |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
244 |
shape =proc_subtree (a,tx0,ty0,tzm,txm,tym,tz1,node->getChild(1^a), origin_shape, ray, nearest_distance); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
245 |
curr_node = next_node(txm, 5, tym, 3, tz1, 8); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
246 |
break; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
247 |
case 2: |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
248 |
shape =proc_subtree (a,tx0,tym,tz0,txm,ty1,tzm,node->getChild(2^a), origin_shape, ray, nearest_distance); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
249 |
curr_node = next_node(txm, 6, ty1, 8, tzm, 3); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
250 |
break; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
251 |
case 3: |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
252 |
shape =proc_subtree (a,tx0,tym,tzm,txm,ty1,tz1,node->getChild(3^a), origin_shape, ray, nearest_distance); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
253 |
curr_node = next_node(txm, 7, ty1, 8, tz1, 8); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
254 |
break; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
255 |
case 4: |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
256 |
shape =proc_subtree (a,txm,ty0,tz0,tx1,tym,tzm,node->getChild(4^a), origin_shape, ray, nearest_distance); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
257 |
curr_node = next_node(tx1, 8, tym, 6, tzm, 5); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
258 |
break; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
259 |
case 5: |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
260 |
shape =proc_subtree (a,txm,ty0,tzm,tx1,tym,tz1,node->getChild(5^a), origin_shape, ray, nearest_distance); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
261 |
curr_node = next_node(tx1, 8, tym, 7, tz1, 8); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
262 |
break; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
263 |
case 6: |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
264 |
shape =proc_subtree (a,txm,tym,tz0,tx1,ty1,tzm,node->getChild(6^a), origin_shape, ray, nearest_distance); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
265 |
curr_node = next_node(tx1, 8, ty1, 8, tzm, 7); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
266 |
break; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
267 |
case 7: |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
268 |
shape =proc_subtree (a,txm,tym,tzm,tx1,ty1,tz1,node->getChild(7^a), origin_shape, ray, nearest_distance); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
269 |
curr_node = 8; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
270 |
break; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
271 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
272 |
if (shape != NULL) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
273 |
return shape; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
274 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
275 |
return NULL; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
276 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
277 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
278 |
/* |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
279 |
traversal algorithm paper as described in paper |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
280 |
"An Efficient Parametric Algorithm for Octree Traversal" |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
281 |
by J. Revelles, C. Urena and M. Lastra. |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
282 |
*/ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
283 |
Shape * Octree::nearest_intersection(const Shape *origin_shape, const Ray &ray, |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
284 |
Float &nearest_distance) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
285 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
286 |
/* if we have no tree, fall back to naive test */ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
287 |
if (!built) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
288 |
return Container::nearest_intersection(origin_shape, ray, nearest_distance); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
289 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
290 |
int a = 0; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
291 |
Vector3 ro = ray.o; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
292 |
Vector3 rdir = ray.dir; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
293 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
294 |
if (rdir.x < 0.0) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
295 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
296 |
ro.x = (bbox.L.x+bbox.H.x) - ro.x; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
297 |
rdir.x = -rdir.x; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
298 |
a |= 4; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
299 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
300 |
if (rdir.y < 0.0) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
301 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
302 |
ro.y = (bbox.L.y+bbox.H.y) - ro.y; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
303 |
rdir.y = -rdir.y; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
304 |
a |= 2; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
305 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
306 |
if (rdir.z < 0.0) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
307 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
308 |
ro.z = (bbox.L.z+bbox.H.z) - ro.z; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
309 |
rdir.z = -rdir.z; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
310 |
a |= 1; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
311 |
} |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
312 |
Float tx0 = (bbox.L.x - ro.x) / rdir.x; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
313 |
Float tx1 = (bbox.H.x - ro.x) / rdir.x; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
314 |
Float ty0 = (bbox.L.y - ro.y) / rdir.y; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
315 |
Float ty1 = (bbox.H.y - ro.y) / rdir.y; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
316 |
Float tz0 = (bbox.L.z - ro.z) / rdir.z; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
317 |
Float tz1 = (bbox.H.z - ro.z) / rdir.z; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
318 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
319 |
//Octree *node = root; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
320 |
if (max(max(tx0,ty0),tz0) < min (min(tx1,ty1),tz1)) |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
321 |
return proc_subtree(a,tx0,ty0,tz0,tx1,ty1,tz1,root, |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
322 |
origin_shape, ray, nearest_distance); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
323 |
else |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
324 |
return NULL; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
325 |
} |