-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoctree.cpp
More file actions
259 lines (246 loc) · 5.99 KB
/
octree.cpp
File metadata and controls
259 lines (246 loc) · 5.99 KB
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
#include "Ray.h"
#include "Hit.h"
#include "Vector3f.h"
#include "Mesh.hpp"
#include "octree.hpp"
#include <vector>
///@brief two intervals intersect
bool intersect(float * a, float * b)
{
if(a[0]>b[1]){
float * tmp = a;
a = b;
b = tmp;
}
return b[0]<=a[1];
}
///@brief two boxes intersect
bool boxOverlap(Box * a, Box * b)
{
for(int dim = 0 ;dim<3;dim++){
float ia[2]={a->mn[dim], a->mx[dim]};
float ib[2]={b->mn[dim], b->mx[dim]};
bool inter = intersect(ia, ib);
if(!inter){
return false;
}
}
return true;
}
bool inside(const Box & a, const Box & b){
for(int dim = 0 ;dim<3;dim++){
if(a.mn[dim]<b.mn[dim]
||a.mx[dim]>b.mx[dim]){
return false;
}
}
return true;
}
///@brief bounding box for a triangle
Box trigBox(int t, const Mesh & m ){
Box b;
b.mn = m.v[m.t[t][0]];
b.mx = m.v[m.t[t][0]];
for(int ii = 1;ii<3;ii++){
for(int dim = 0; dim <3 ; dim++){
if(b.mn[dim]> m.v[m.t[t][ii]][dim]){
b.mn[dim] = m.v[m.t[t][ii]][dim];
}
if(b.mx[dim]<m.v[m.t[t][ii]][dim]){
b.mx[dim] = m.v[m.t[t][ii]][dim];
}
}
}
return b;
}
///@brief pbox parent's box
void Octree::buildNode(OctNode & parent, const Box & pbox ,
const std::vector<int>&trigs,
const Mesh & m, int level)
{
if(trigs.size() <= Octree :: max_trig
|| level>maxLevel){
parent.obj = trigs;
return;
}
level++;
//initialize 8 children
for(int ii = 0; ii<8;ii++){
parent.child[ii]=new OctNode();
}
const Vector3f & mn = pbox.mn;
const Vector3f & mx = pbox.mx;
Vector3f mid = (mn + mx)/2;
//childBox;
Box cBox[8];
//ewww....
cBox[0] = Box(mn,mid);
cBox[1] = Box(mn[0], mn[1], mid[2], mid[0], mid[1], mx[2] );
cBox[2] = Box(mn[0], mid[1], mn[2], mid[0], mx[1], mid[2] );
cBox[3] = Box(mn[0], mid[1], mid[2], mid[0], mx[1], mx[2] );
cBox[4] = Box(mid[0], mn[1], mn[2], mx[0], mid[1], mid[2] );
cBox[5] = Box(mid[0], mn[1], mid[2], mx[0], mid[1], mx[2] );
cBox[6] = Box(mid[0], mid[1], mn[2], mx[0], mx[1], mid[2] );
cBox[7] = Box(mid[0], mid[1], mid[2], mx[0], mx[1], mx[2] );
//std::vector<bool>added (trigs.size(),false);
for(int ii = 0 ; ii<8;ii++){
std::vector<int> childTrigs;
for(unsigned int vi =0; vi<trigs.size();vi++){
int trigIdx = trigs[vi];
Box tBox = trigBox(trigIdx, m);
if(inside(tBox, cBox[ii])
||boxOverlap(&tBox, &(cBox[ii]))
){
childTrigs.push_back(trigIdx);
// added[vi]=true;
}
}
buildNode(*(parent.child[ii]),cBox[ii], childTrigs,m,level);
}
// for (unsigned int ii = 0 ;ii<added.size();ii++){
// if(!added[ii]){
// std::cout<<ii<<" not added\n";
// }
// }
}
void Octree::build(const Mesh & m)
{
///compute bounding box for m
box.mn = m.v[0];
box.mx = m.v[0];
for(unsigned int ii = 1 ; ii< m.v.size();ii++){
for(int dim = 0; dim < 3; dim++){
if(box.mn[dim]>m.v[ii][dim]){
box.mn[dim] = m.v[ii][dim];
}
if(box.mx[dim]<m.v[ii][dim]){
box.mx[dim] = m.v[ii][dim];
}
}
}
std::vector<int>trigs(m.t.size());
for(unsigned int ii = 0 ; ii < trigs.size();ii++){
trigs[ii] = ii;
}
buildNode(root,box,trigs, m,0);
}
int first_node(float tx0,float ty0,float tz0, float txm, float tym,float tzm){
char bits = 0;
///find max x0 y0 z0
if(tx0 > ty0){
if(tx0 > tz0){ // PLANE YZ
if(tym < tx0) {bits|=2;}
if(tzm < tx0) {bits|=1;}
return bits;
}
}
else {
if(ty0 > tz0){
if(txm < ty0) {bits|=4;}
if(tzm < ty0) {bits|=1;}
return bits;
}
}
if(txm < tz0){bits|=4;}
if(tym < tz0){bits|=2;}
return bits;
}
int new_node(float txm, int x, float tym, int y, float tzm, int z)
{
if(txm < tym){
if(txm < tzm){return x;}
}
else{
if(tym < tzm){return y;}
}
return z;
}
void Octree::proc_subtree (float tx0, float ty0, float tz0, float tx1, float ty1, float tz1, OctNode* node)
{
float txm, tym, tzm;
int currNode;
if(tx1 < 0 || ty1 < 0 || tz1 < 0) {return;}
if(node->isTerm()){
//loop over things
for(unsigned int ii = 0 ; ii<node->obj.size();ii++){
termFunc(node->obj[ii],arg);
}
return;
}
txm = 0.5*(tx0 + tx1);
tym = 0.5*(ty0 + ty1);
tzm = 0.5*(tz0 + tz1);
currNode = first_node(tx0,ty0,tz0,txm,tym,tzm);
do{
switch (currNode){
case 0: {
proc_subtree(tx0,ty0,tz0,txm,tym,tzm,node->child[aa]);
currNode = new_node(txm,4,tym,2,tzm,1);
break;}
case 1: {
proc_subtree(tx0,ty0,tzm,txm,tym,tz1,node->child[1^aa]);
currNode = new_node(txm,5,tym,3,tz1,8);
break;}
case 2: {
proc_subtree(tx0,tym,tz0,txm,ty1,tzm,node->child[2^aa]);
currNode = new_node(txm,6,ty1,8,tzm,3);
break;}
case 3: {
proc_subtree(tx0,tym,tzm,txm,ty1,tz1,node->child[3^aa]);
currNode = new_node(txm,7,ty1,8,tz1,8);
break;}
case 4: {
proc_subtree(txm,ty0,tz0,tx1,tym,tzm,node->child[4^aa]);
currNode = new_node(tx1,8,tym,6,tzm,5);
break;}
case 5: {
proc_subtree(txm,ty0,tzm,tx1,tym,tz1,node->child[5^aa]);
currNode = new_node(tx1,8,tym,7,tz1,8);
break;
}
case 6: {
proc_subtree(txm,tym,tz0,tx1,ty1,tzm,node->child[6^aa]);
currNode = new_node(tx1,8,ty1,8,tzm,7);
break;}
case 7: {
proc_subtree(txm,tym,tzm,tx1,ty1,tz1,node->child[7^aa]);
currNode = 8;
break;}
}
} while (currNode<8);
}
void Octree::intersect(const Ray & ray){
Vector3f rd=ray.getDirection();
//assumes rd normalized
rd.normalize();
Vector3f ro=ray.getOrigin();
aa=0;
Vector3f size = box.mx + box.mn;
if(rd[0]<0.0f){
ro[0] = size[0] - ro[0];
rd[0] = - rd[0];
aa |= 4 ;
}
if(rd[1] < 0.0f){
ro[1] = size[1] - ro[1];
rd[1] = - rd[1];
aa |= 2 ;
}
if(rd[2] < 0.0f){
ro[2] = size[2] - ro[2];
rd[2] = - rd[2];
aa |= 1 ;
}
float divx = 1 / rd[0]; // IEEE stability fix
float divy = 1 / rd[1];
float divz = 1 / rd[2];
float tx0 = (box.mn[0] - ro[0]) * divx;
float tx1 = (box.mx[0] - ro[0]) * divx;
float ty0 = (box.mn[1] - ro[1]) * divy;
float ty1 = (box.mx[1] - ro[1]) * divy;
float tz0 = (box.mn[2] - ro[2]) * divz;
float tz1 = (box.mx[2] - ro[2]) * divz;
if( max(max(tx0,ty0),tz0) <= min(min(tx1,ty1),tz1) ){
proc_subtree(tx0,ty0,tz0,tx1,ty1,tz1, &root);
}
}