Repository navigation
Expand file tree
/
Copy pathKDTree.cpp
More file actions
358 lines (290 loc) · 11.3 KB
/
Copy pathKDTree.cpp
File metadata and controls
358 lines (290 loc) · 11.3 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
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
#include "KDTree.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cassert>
#include <thread>
void KDTree::traverse(const Node *n, int level) const{
if(level == 0)
std::cout << "Starting with root:" << std::endl;
std::string s = "";
for(int i = 0; i< level; ++i)
s+='\t';
//print current data:
std::cout<<s<<"split: "<<n->split<<std::endl;
std::cout<<std::boolalpha;
std::cout<<s<<"is a leaf: "<<(n->isLeaf)<<std::endl;
if(n->isLeaf){//print data
if(n->triags != nullptr){
std::cout<<s<<"data: ";
for (unsigned int i =0; i< n->triags->size(); ++i)
std::cout<< n->triags->at(i) <<"; ";
std::cout<<"\n"<<std::endl;
}else{
std::cout<<s<<"no data contained\n"<< std::endl;
}
}else{//recursive calls
std::cout<<s<<"right:"<<std::endl;
traverse(&(n->left[1]),level+1);//left
std::cout<<s<<"left:"<<std::endl;
traverse(&(n->left[0]), level+1);//right
}
}
int KDTree::addTriangle(Triangle t){
if (_triangleStorage.empty()){
//init bounds with the dimension of the first point in the triangle
_boxMin[0] = _boxMax[0] = t.getV1()[0];
_boxMin[1] = _boxMax[1] = t.getV1()[1];
_boxMin[2] = _boxMax[2] = t.getV1()[2];
}
//set the real max,min positions
for(int dim = 0; dim <3; ++dim){
_boxMin[dim] = std::min( _boxMin[dim], t.getV1()[dim]);
_boxMax[dim] = std::max( _boxMax[dim], t.getV1()[dim]);
_boxMin[dim] = std::min( _boxMin[dim], t.getV2()[dim]);
_boxMax[dim] = std::max( _boxMax[dim], t.getV2()[dim]);
_boxMin[dim] = std::min( _boxMin[dim], t.getV3()[dim]);
_boxMax[dim] = std::max( _boxMax[dim], t.getV3()[dim]);
}
_triangleStorage.push_back(t);
return _triangleStorage.size()-1;
}
void KDTree::init(){
trianglePointers = 0;
_root = std::move(std::unique_ptr<Node>(new Node()));
std::vector<Triangle*> contained;
for(unsigned int i=0; i<_triangleStorage.size(); ++i)
contained.push_back(&_triangleStorage.at(i));
std::cout<<"initialising kd-tree node with bounds:"
<< "["<<_boxMin[0]<<";" << _boxMax[0]<<"]x["
<< _boxMin[1]<<";" << _boxMax[1]<<"]x["
<< _boxMin[2]<<";" << _boxMax[2]<<"]"<<std::endl;
std::cout<<" triangles: "<<contained.size()<<std::endl;
_buildkdtree(_root.get(), contained, _boxMin, _boxMax, 0);
std::cout<<" triangle pointers: "<<trianglePointers<<std::endl;
}
real KDTree::_getOptimalSplitPosition(std::vector<Triangle*> &containedTrigs,
const Vector3 &boxMin, const Vector3 &boxMax, int level){
// area = 2 * ((width * height) + (height * depth) + (depth * width))
//cost = Ctraversal + area * prims * Cintersect
//no split:
real area = _area(boxMin, boxMax);
real no_split = area*containedTrigs.size()*COST_INTERSECTION ;
real split_pos = boxMin[level%3];
real best_cost = no_split;
std::vector<real> positions;
for(Triangle *t: containedTrigs){
positions.push_back(t->getV1()[level%3]);
positions.push_back(t->getV2()[level%3]);
positions.push_back(t->getV3()[level%3]);
}
//move the splitting position
//it starts with all max positions and in the right level the min position
Vector3 split_position_max = boxMax;
Vector3 split_position_min = boxMin;
for(real pos: positions){
//adjust the center and size of the boxes
split_position_min[level%3] = pos;
split_position_max[level%3] = pos;
//count triangels in right and left set
real triags_left = 0, triags_right = 0;
for(const Triangle *t: containedTrigs){
if( t->overlapAABB( boxMin, split_position_max) )
++triags_left;
if( t->overlapAABB( split_position_min, boxMax) )
++triags_right;
}
//calculate areas
real area_left = _area(boxMin, split_position_max), area_right = _area(split_position_min, boxMax);
//cost = COST_TRAVERSAL + cost left +cost right
real cost = COST_TRAVERSAL +
area_left*triags_left*COST_INTERSECTION +
area_right*triags_right*COST_INTERSECTION;//cost left
if (cost < best_cost){//better split position?
cost = best_cost;
split_pos = pos;
}
}
return split_pos;
}
//TODO: do this in parallel to speed things up!
void KDTree::_buildkdtree(Node* node, std::vector<Triangle*> containedTrigs,
const Vector3 &boxMin, const Vector3 &boxMax, int level){
//splitpos: splitpos < bounds -> do not split, single node
//IF IS LEAF: NO SPLITTING
real splitpos = _getOptimalSplitPosition(containedTrigs,boxMin,boxMax,level);
//is the new node a leaf?
//do not split such that the resulting cube has a volumue of zero
if(splitpos-eps <= boxMin[level%3] or splitpos+eps >= boxMax[level%3]){
node->isLeaf = true;
node->split = 0.f;
node->triags = std::move(std::unique_ptr<std::vector<Triangle*> >(new std::vector<Triangle*>));
for(Triangle* t: containedTrigs)
node->triags->push_back(t);
trianglePointers+= node->triags->size();
std::cout<<"building leaf at level "<<level<<" with "<<containedTrigs.size()<<" triangles"
<<" and bounds: "<< "["<<boxMin[0]<<";" << boxMax[0]<<"]x["
<< boxMin[1]<<";" << boxMax[1]<<"]x["
<< boxMin[2]<<";" << boxMax[2]<<"]"<<std::endl;
return;
}
//find all correct triangles
std::vector<Triangle*> contained_left, contained_right;
Vector3 split_position_max = boxMax;
Vector3 split_position_min = boxMin;
split_position_max[level%3] = splitpos;
split_position_min[level%3] = splitpos;
//assign triangles to sides(can be on both)
for(Triangle *t: containedTrigs){
int counter = 0;
if ( t->overlapAABB(boxMin, split_position_max) ){
contained_left.push_back(t);
++counter;
}
if ( t->overlapAABB(split_position_min, boxMax) ){
contained_right.push_back(t);
++counter;
}
if(counter==0){
std::cout<< "triangle NOT added!!!"<<std::endl;
std::cout<<"triangle: "<< *t<<std::endl;
std::cout<<"box left: "<< "["<<boxMin[0]<<";" << split_position_max[0]<<"]x["
<< boxMin[1]<<";" << split_position_max[1]<<"]x["
<< boxMin[2]<<";" << split_position_max[2]<<"]"<<std::endl;
std::cout<<"box right: "<< "["<<split_position_min[0]<<";" << boxMax[0]<<"]x["
<< split_position_min[1]<<";" << boxMax[1]<<"]x["
<< split_position_min[2]<<";" << boxMax[2]<<"]"<<std::endl;
}
assert(counter >=1);
}
//set correct date for the current node:
//isLeaf, split and left or triags
node->isLeaf = false;
node->split = splitpos;
node->triags = std::move(std::unique_ptr<std::vector<Triangle*> >(new std::vector<Triangle*>));
for(Triangle* t: containedTrigs)
node->triags->push_back(t);
std::cout<< "Splitting node at level "<<level<<" at position "<<splitpos <<std::endl;
node->left = std::move(std::unique_ptr<Node[]>(new Node[2]));
Node *leftnode = &node->left[0];
Node *rightnode = &node->left[1];
//this can be done with two parallel calls => c++11 threads.
//join() at this point not needed! => how to wait till everyone is ready?
_buildkdtree( leftnode, contained_left, boxMin, split_position_max, level+1 );
_buildkdtree( rightnode, contained_right, split_position_min, boxMax, level+1 );
}
void KDTree::_getIntersection(Vector3 &position, const Ray &r, IntersectionCompound &inter) const{
//TODO: delete this bad version
/*
real nearest = std::numeric_limits<real>::infinity();
foreach(const Triangle& triag, _triangleStorage){
IntersectionCompound tmp = triag.getIntersection(r);
//test if there is a real hit AND if the hit triangle is nearer than the nearest so far
if( tmp.t > 0 and tmp.t < nearest){
inter = tmp;
nearest = tmp.t;
}
}
*/
if( position[0]-2*eps >= _boxMax[0] or position[1]-2*eps >= _boxMax[1] or position[2]-2*eps >= _boxMax[2]
or position[0]+2*eps <= _boxMin[0] or position[1]+2*eps <= _boxMin[1] or position[2]+2*eps <= _boxMin[2]){
inter.t = -1;//no intersection, set dist to -1
return;
}
//find position in kdtree and store the bounds
Node *leaf = _root.get();
int level = 0;
Vector3 boxMax = _boxMax;
Vector3 boxMin = _boxMin;
while(not (leaf->isLeaf)){
//to right or to left?
if(position[level%3] <= leaf->split){
leaf = &leaf->left[0];
boxMax[level%3] = leaf->split;
}else{
leaf = &leaf->left[1];
boxMin[level%3] = leaf->split;
}
++level;
}
//check for nearest intersection
real nearest = std::numeric_limits<real>::infinity();
for(Triangle *triag: *(leaf->triags)){
IntersectionCompound tmp = triag->getIntersection(r);
//test if there is a real hit AND if the hit triangle is nearer than the nearest so far
if( tmp.t > 0 and tmp.t < nearest){
inter = tmp;
nearest = tmp.t;
}
}
//if intersection => return inter.
if (nearest < std::numeric_limits<real>::infinity())
return;
//if no intersection => extend oring to next border, add EPS
//boxMin/boxMax = position + dist * ray.origin
real distToBox = r.intersectBox(boxMin, boxMax, position);
if(distToBox < -eps){
inter.t = -1;
return;
}
//std::cout<<"old pos: "<<position <<std::endl;
Vector3 oldPos = position;
position += (distToBox+eps) * r.getDirection();//edd eps to be sureto be inside a box
//std::cout<<"new pos: "<<position <<std::endl;
//no next border => end(no intersection)
if( position[0] >= _boxMax[0] or position[1] >= _boxMax[1] or position[2] >= _boxMax[2]
or position[0] <= _boxMin[0] or position[1] <= _boxMin[1] or position[2] <= _boxMin[2]){
inter.t = -1;//no intersection, set dist to -1
return;
}
//TODO remove this after debugging
/*
std::cout<<"warning: this should not happen -> one move should be outside the box"<<std::endl;
std::cout<<"old pos: "<<oldPos <<std::endl;
std::cout<<"new pos: "<<position <<std::endl;
std::cout<<"dir: "<<r.getDirection()<<std::endl;
std::cout<<"disToBox: "<<distToBox<<std::endl;
std::cout<<"box min: "<<boxMin<<" max: "<<boxMax <<std::endl;
r.intersectBox(boxMin, boxMax, oldPos,true);
//assert(false);
//recursive call of this function
std::cout<<"was at: "<<oldPos <<"\tnow at: "<<position<<std::endl;
*/
//TODO: replace this with a while(true) loop
_getIntersection(position, r, inter);
}
IntersectionCompound KDTree::getIntersection(const Ray& r) const{
IntersectionCompound inter;
Vector3 position = r.getOrigin();
//move position inside the cube!
//if not possible: return
if( _boxMin[0] > position[0] or position[0] > _boxMax[0]
or _boxMin[1] > position[1] or position[1] > _boxMax[2]
or _boxMin[2] > position[2] or position[2] > _boxMax[2]){
//std::cout<<"initial position "<<position<<" not in box min: "<<_boxMin<<" max:" << _boxMax <<std::endl;
real distToBox = r.intersectBox(_boxMin, _boxMax);
if(distToBox<0){//no intersection with the kdtree
//std::cout<<"distToBox: "<< distToBox
//<<" ,missed the kd-tree => abort" <<std::endl;
//std::cout<<"starting at: "<<position<<" direction: "<<r.getDirection()<<std::endl;
/*
real t = (_boxMin[2]-position[2])/r.getDirection()[2];
auto newpos = (t+eps)*r.getDirection()+position;
std::cout<<"t: "<<t<<" pos+ t*dir: "<< newpos<<std::endl;
if( _boxMin[0] > newpos[0] or newpos[0] > _boxMax[0]
or _boxMin[1] > newpos[1] or newpos[1] > _boxMax[2]
or _boxMin[2] > newpos[2] or newpos[2] > _boxMax[2])
std::cout<<"inside box "<<std::endl;
assert(false);
*/
return inter;
}
position += (distToBox+eps)*r.getDirection();
//std::cout<<"moved to: "<<position <<std::endl;
}
//find the nearest intersection
//std::cout<<"finding real intersection" <<std::endl;
_getIntersection(position, r, inter);
return inter;
}