author | Radek Brich <radek.brich@devl.cz> |
Sun, 25 Nov 2007 22:22:40 +0100 | |
branch | pyrit |
changeset 17 | 5176ba000a67 |
parent 16 | 20bceb605f48 |
child 20 | f22952603f29 |
permissions | -rw-r--r-- |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
1 |
#include <algorithm> |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
2 |
#include <stack> |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
3 |
|
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
4 |
#include "kdtree.h" |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
5 |
|
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
6 |
class SortableShape |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
7 |
{ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
8 |
public: |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
9 |
Shape *shape; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
10 |
BBox bbox; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
11 |
short axis; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
12 |
short mark; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
13 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
14 |
SortableShape(Shape *aShape, short &aAxis): shape(aShape), axis(aAxis), mark(0) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
15 |
{ bbox = shape->get_bbox(); }; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
16 |
friend bool operator<(const SortableShape& a, const SortableShape& b) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
17 |
{ return a.bbox.L.cell[a.axis] < b.bbox.L.cell[b.axis]; }; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
18 |
void setAxis(short aAxis) { axis = aAxis; }; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
19 |
void setMark() { mark = 1; }; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
20 |
short hasMark() { return mark; }; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
21 |
}; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
22 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
23 |
class SortableShapeList: public vector<SortableShape> |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
24 |
{ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
25 |
public: |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
26 |
SortableShapeList(ShapeList *shapes, short axis) |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
27 |
{ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
28 |
ShapeList::iterator shape; |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
29 |
for (shape = shapes->begin(); shape != shapes->end(); shape++) |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
30 |
push_back(SortableShape(*shape, axis)); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
31 |
}; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
32 |
}; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
33 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
34 |
class SplitPos |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
35 |
{ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
36 |
public: |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
37 |
float pos; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
38 |
int lnum, rnum; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
39 |
SplitPos(): pos(0.0), lnum(0), rnum(0) {}; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
40 |
SplitPos(float &aPos): pos(aPos), lnum(0), rnum(0) {}; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
41 |
friend bool operator<(const SplitPos& a, const SplitPos& b) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
42 |
{ return a.pos < b.pos; }; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
43 |
friend bool operator==(const SplitPos& a, const SplitPos& b) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
44 |
{ return a.pos == b.pos; }; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
45 |
}; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
46 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
47 |
class SplitList: public vector<SplitPos> |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
48 |
{ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
49 |
}; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
50 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
51 |
// stack element for kd-tree traversal |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
52 |
struct StackElem |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
53 |
{ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
54 |
KdNode* node; /* pointer to far child */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
55 |
float t; /* the entry/exit signed distance */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
56 |
Vector3 pb; /* the coordinates of entry/exit point */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
57 |
}; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
58 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
59 |
// ---------------------------------------- |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
60 |
|
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
61 |
void Container::addShape(Shape* aShape) |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
62 |
{ |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
63 |
shapes.push_back(aShape); |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
64 |
if (shapes.size() == 0) { |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
65 |
/* initialize bounding box */ |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
66 |
bbox = aShape->get_bbox(); |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
67 |
} else { |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
68 |
/* adjust bounding box */ |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
69 |
BBox shapebb = aShape->get_bbox(); |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
70 |
if (shapebb.L.x < bbox.L.x) bbox.L.x = shapebb.L.x; |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
71 |
if (shapebb.L.y < bbox.L.y) bbox.L.y = shapebb.L.y; |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
72 |
if (shapebb.L.z < bbox.L.z) bbox.L.z = shapebb.L.z; |
14
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
73 |
if (shapebb.H.x > bbox.H.x) bbox.H.x = shapebb.H.x; |
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
74 |
if (shapebb.H.y > bbox.H.y) bbox.H.y = shapebb.H.y; |
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
75 |
if (shapebb.H.z > bbox.H.z) bbox.H.z = shapebb.H.z; |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
76 |
} |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
77 |
}; |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
78 |
|
11
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
79 |
Shape *Container::nearest_intersection(const Shape *origin_shape, const Ray &ray, |
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
80 |
float &nearest_distance) |
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
81 |
{ |
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
82 |
Shape *nearest_shape = NULL; |
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
83 |
ShapeList::iterator shape; |
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
84 |
for (shape = shapes.begin(); shape != shapes.end(); shape++) |
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
85 |
if (*shape != origin_shape && (*shape)->intersect(ray, nearest_distance)) |
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
86 |
nearest_shape = *shape; |
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
87 |
return nearest_shape; |
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
88 |
} |
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
89 |
|
16
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
90 |
KdNode::~KdNode() |
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
91 |
{ |
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
92 |
if (isLeaf()) |
17
5176ba000a67
fix last leak as reported by valgrind
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
93 |
delete shapes; |
16
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
94 |
else |
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
95 |
delete[] children; |
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
96 |
} |
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
97 |
|
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
98 |
void KdNode::subdivide(BBox bbox, int depth) |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
99 |
{ |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
100 |
if (depth >= 20 || shapes->size() <= 4) |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
101 |
{ |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
102 |
setLeaf(); |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
103 |
return; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
104 |
} |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
105 |
|
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
106 |
// choose split axis |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
107 |
axis = 0; |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
108 |
if (bbox.h() > bbox.w() && bbox.h() > bbox.d()) |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
109 |
axis = 1; |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
110 |
if (bbox.d() > bbox.w() && bbox.d() > bbox.h()) |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
111 |
axis = 2; |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
112 |
|
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
113 |
// *** find optimal split position |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
114 |
SortableShapeList sslist(shapes, axis); |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
115 |
sort(sslist.begin(), sslist.end()); |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
116 |
|
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
117 |
SplitList splitlist; |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
118 |
|
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
119 |
SortableShapeList::iterator sh; |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
120 |
for (sh = sslist.begin(); sh != sslist.end(); sh++) |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
121 |
{ |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
122 |
splitlist.push_back(SplitPos(sh->bbox.L.cell[axis])); |
14
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
123 |
splitlist.push_back(SplitPos(sh->bbox.H.cell[axis])); |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
124 |
} |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
125 |
sort(splitlist.begin(), splitlist.end()); |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
126 |
// remove duplicate splits |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
127 |
splitlist.resize(unique(splitlist.begin(), splitlist.end()) - splitlist.begin()); |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
128 |
|
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
129 |
// find all posible splits |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
130 |
SplitList::iterator spl; |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
131 |
int rest; |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
132 |
for (spl = splitlist.begin(); spl != splitlist.end(); spl++) |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
133 |
{ |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
134 |
for (sh = sslist.begin(), rest = sslist.size(); sh != sslist.end(); sh++, rest--) |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
135 |
{ |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
136 |
if (sh->hasMark()) |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
137 |
{ |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
138 |
spl->lnum++; |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
139 |
continue; |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
140 |
} |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
141 |
// if shape is completely contained in split plane |
14
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
142 |
if (spl->pos == sh->bbox.L.cell[axis] == sh->bbox.H.cell[axis]) |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
143 |
{ |
14
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
144 |
if (spl->pos - bbox.L.cell[axis] < bbox.H.cell[axis] - spl->pos) |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
145 |
{ |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
146 |
// left subcell is smaller -> if not empty, put shape here |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
147 |
if (spl->lnum) |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
148 |
spl->lnum++; |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
149 |
else |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
150 |
spl->rnum++; |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
151 |
} else { |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
152 |
// right subcell is smaller |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
153 |
if (spl->rnum) |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
154 |
spl->rnum++; |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
155 |
else |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
156 |
spl->lnum++; |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
157 |
} |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
158 |
sh->setMark(); |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
159 |
} else |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
160 |
// if shape is on left side of split plane |
14
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
161 |
if (sh->bbox.H.cell[axis] <= spl->pos) |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
162 |
{ |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
163 |
spl->lnum++; |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
164 |
sh->setMark(); |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
165 |
} else |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
166 |
// if shape occupies both sides of split plane |
14
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
167 |
if (sh->bbox.L.cell[axis] < spl->pos && sh->bbox.H.cell[axis] > spl->pos) |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
168 |
{ |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
169 |
spl->lnum++; |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
170 |
spl->rnum++; |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
171 |
} else |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
172 |
// if shape is on right side of split plane |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
173 |
if (sh->bbox.L.cell[axis] >= spl->pos) |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
174 |
{ |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
175 |
spl->rnum += rest; |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
176 |
break; |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
177 |
} |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
178 |
} |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
179 |
} |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
180 |
|
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
181 |
// choose best split pos |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
182 |
const float K = 1.4; // constant, K = cost of traversal / cost of ray-triangle intersection |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
183 |
float SAV = 2*(bbox.w()*bbox.h() + bbox.w()*bbox.d() + bbox.h()*bbox.d()); // surface area of node |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
184 |
float cost = SAV * (K + shapes->size()); // initial cost = non-split cost |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
185 |
SplitPos *splitpos = NULL; |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
186 |
bool leaf = true; |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
187 |
BBox lbb = bbox; |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
188 |
BBox rbb = bbox; |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
189 |
for (spl = splitlist.begin(); spl != splitlist.end(); spl++) |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
190 |
{ |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
191 |
// calculate SAH cost of this split |
14
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
192 |
lbb.H.cell[axis] = spl->pos; |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
193 |
rbb.L.cell[axis] = spl->pos; |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
194 |
float SAL = 2*(lbb.w()*lbb.h() + lbb.w()*lbb.d() + lbb.h()*lbb.d()); |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
195 |
float SAR = 2*(rbb.w()*rbb.h() + rbb.w()*rbb.d() + rbb.h()*rbb.d()); |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
196 |
float splitcost = K + SAL/SAV*(K+spl->lnum) + SAR/SAV*(K+spl->rnum); |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
197 |
|
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
198 |
if (splitcost < cost) |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
199 |
{ |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
200 |
leaf = false; |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
201 |
cost = splitcost; |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
202 |
splitpos = &*spl; |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
203 |
} |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
204 |
} |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
205 |
|
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
206 |
if (leaf) |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
207 |
{ |
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
208 |
setLeaf(); |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
209 |
return; |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
210 |
} |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
211 |
|
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
212 |
#if 0 |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
213 |
// export kd-tree as .obj for visualization |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
214 |
// this would be hard to reconstruct later |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
215 |
static ofstream F("kdtree.obj"); |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
216 |
Vector3 v; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
217 |
static int f=0; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
218 |
v.cell[axis] = splitpos->pos; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
219 |
v.cell[(axis+1)%3] = bbox.L.cell[(axis+1)%3]; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
220 |
v.cell[(axis+2)%3] = bbox.L.cell[(axis+2)%3]; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
221 |
F << "v " << v.x << " " << v.y << " " << v.z << endl; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
222 |
v.cell[(axis+1)%3] = bbox.L.cell[(axis+1)%3]; |
14
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
223 |
v.cell[(axis+2)%3] = bbox.H.cell[(axis+2)%3]; |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
224 |
F << "v " << v.x << " " << v.y << " " << v.z << endl; |
14
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
225 |
v.cell[(axis+1)%3] = bbox.H.cell[(axis+1)%3]; |
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
226 |
v.cell[(axis+2)%3] = bbox.H.cell[(axis+2)%3]; |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
227 |
F << "v " << v.x << " " << v.y << " " << v.z << endl; |
14
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
228 |
v.cell[(axis+1)%3] = bbox.H.cell[(axis+1)%3]; |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
229 |
v.cell[(axis+2)%3] = bbox.L.cell[(axis+2)%3]; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
230 |
F << "v " << v.x << " " << v.y << " " << v.z << endl; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
231 |
F << "f " << f+1 << " " << f+2 << " " << f+3 << " " << f+4 << endl; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
232 |
f += 4; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
233 |
#endif |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
234 |
|
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
235 |
split = splitpos->pos; |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
236 |
float lnum = splitpos->lnum; |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
237 |
float rnum = splitpos->rnum; |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
238 |
|
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
239 |
// split this node |
17
5176ba000a67
fix last leak as reported by valgrind
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
240 |
delete shapes; |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
241 |
children = new KdNode[2]; |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
242 |
int state = 0; |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
243 |
for (sh = sslist.begin(); sh != sslist.end(); sh++) |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
244 |
{ |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
245 |
// if shape is on left side of split plane |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
246 |
if (state == 1) |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
247 |
{ // only right |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
248 |
children[1].addShape(sh->shape); |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
249 |
continue; |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
250 |
} |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
251 |
if (state == 0) |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
252 |
{ |
14
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
253 |
if (sh->bbox.H.cell[axis] < split) |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
254 |
{ // left |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
255 |
children[0].addShape(sh->shape); |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
256 |
} else |
14
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
257 |
if (sh->bbox.H.cell[axis] > split) |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
258 |
{ |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
259 |
if (sh->bbox.L.cell[axis] < split) |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
260 |
{ // both |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
261 |
children[0].addShape(sh->shape); |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
262 |
children[1].addShape(sh->shape); |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
263 |
} else |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
264 |
{ // right |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
265 |
children[1].addShape(sh->shape); |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
266 |
state = 1; |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
267 |
} |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
268 |
} else |
14
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
269 |
{ // H == split |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
270 |
if (sh->bbox.L.cell[axis] < split) |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
271 |
{ // left |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
272 |
children[0].addShape(sh->shape); |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
273 |
} else |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
274 |
{ // contained |
14
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
275 |
if (split - bbox.L.cell[axis] < bbox.H.cell[axis] - split) |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
276 |
{ |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
277 |
// left subcell is smaller -> if not empty, put shape here |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
278 |
if (lnum) |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
279 |
children[0].addShape(sh->shape); |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
280 |
else |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
281 |
children[1].addShape(sh->shape); |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
282 |
} else { |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
283 |
// right subcell is smaller |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
284 |
if (rnum) |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
285 |
children[1].addShape(sh->shape); |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
286 |
else |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
287 |
children[0].addShape(sh->shape); |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
288 |
} |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
289 |
} |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
290 |
} |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
291 |
} |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
292 |
} |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
293 |
|
14
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
294 |
lbb.H.cell[axis] = split; |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
295 |
rbb.L.cell[axis] = split; |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
296 |
|
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
297 |
children[0].subdivide(lbb, depth+1); |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
298 |
children[1].subdivide(rbb, depth+1); |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
299 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
300 |
|
11
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
301 |
void KdTree::build() |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
302 |
{ |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
303 |
root = new KdNode(); |
17
5176ba000a67
fix last leak as reported by valgrind
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
304 |
ShapeList::iterator shape; |
5176ba000a67
fix last leak as reported by valgrind
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
305 |
for (shape = shapes.begin(); shape != shapes.end(); shape++) |
5176ba000a67
fix last leak as reported by valgrind
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
306 |
root->addShape(*shape); |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
307 |
root->subdivide(bbox, 0); |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
308 |
built = true; |
0
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
309 |
} |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
310 |
|
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
311 |
/* algorithm by Vlastimil Havran, Heuristic Ray Shooting Algorithms, appendix C */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
312 |
Shape *KdTree::nearest_intersection(const Shape *origin_shape, const Ray &ray, |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
313 |
float &nearest_distance) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
314 |
{ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
315 |
float a, b; /* entry/exit signed distance */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
316 |
float t; /* signed distance to the splitting plane */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
317 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
318 |
/* if we have no tree, fall back to naive test */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
319 |
if (!built) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
320 |
return Container::nearest_intersection(origin_shape, ray, nearest_distance); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
321 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
322 |
if (!bbox.intersect(ray, a, b)) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
323 |
return NULL; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
324 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
325 |
/* pointers to the far child node and current node */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
326 |
KdNode *farchild, *node; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
327 |
node = root; /* start from the kd-tree root node */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
328 |
|
16
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
329 |
stack<StackElem*> st; |
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
330 |
|
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
331 |
StackElem *enPt = new StackElem(); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
332 |
enPt->t = a; /* set the signed distance */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
333 |
enPt->node = NULL; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
334 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
335 |
/* distinguish between internal and external origin */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
336 |
if (a >= 0.0) /* a ray with external origin */ |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
337 |
enPt->pb = ray.o + ray.dir * a; |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
338 |
else /* a ray with internal origin */ |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
339 |
enPt->pb = ray.o; |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
340 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
341 |
/* setup initial exit point in the stack */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
342 |
StackElem *exPt = new StackElem(); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
343 |
exPt->t = b; |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
344 |
exPt->pb = ray.o + ray.dir * b; |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
345 |
exPt->node = NULL; /* set termination flag */ |
16
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
346 |
st.push(exPt); |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
347 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
348 |
/* loop, traverse through the whole kd-tree, until an object is intersected or ray leaves the scene */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
349 |
while (node) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
350 |
{ |
16
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
351 |
exPt = st.top(); |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
352 |
/* loop until a leaf is found */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
353 |
while (!node->isLeaf()) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
354 |
{ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
355 |
/* retrieve position of splitting plane */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
356 |
float splitVal = node->getSplit(); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
357 |
short axis = node->getAxis(); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
358 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
359 |
if (enPt->pb[axis] <= splitVal) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
360 |
{ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
361 |
if (exPt->pb[axis] <= splitVal) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
362 |
{ /* case N1, N2, N3, P5, Z2, and Z3 */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
363 |
node = node->getLeftChild(); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
364 |
continue; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
365 |
} |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
366 |
if (exPt->pb[axis] == splitVal) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
367 |
{ /* case Z1 */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
368 |
node = node->getRightChild(); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
369 |
continue; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
370 |
} |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
371 |
/* case N4 */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
372 |
farchild = node->getRightChild(); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
373 |
node = node->getLeftChild(); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
374 |
} |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
375 |
else |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
376 |
{ /* (enPt->pb[axis] > splitVal) */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
377 |
if (splitVal < exPt->pb[axis]) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
378 |
{ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
379 |
/* case P1, P2, P3, and N5 */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
380 |
node = node->getRightChild(); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
381 |
continue; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
382 |
} |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
383 |
/* case P4*/ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
384 |
farchild = node->getLeftChild(); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
385 |
node = node->getRightChild(); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
386 |
} |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
387 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
388 |
/* case P4 or N4 . . . traverse both children */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
389 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
390 |
/* signed distance to the splitting plane */ |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
391 |
t = (splitVal - ray.o.cell[axis]) / ray.dir.cell[axis]; |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
392 |
|
16
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
393 |
/* setup the new exit point and push it onto stack */ |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
394 |
exPt = new StackElem(); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
395 |
exPt->t = t; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
396 |
exPt->node = farchild; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
397 |
exPt->pb[axis] = splitVal; |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
398 |
exPt->pb[(axis+1)%3] = ray.o.cell[(axis+1)%3] + t * ray.dir.cell[(axis+1)%3]; |
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
399 |
exPt->pb[(axis+2)%3] = ray.o.cell[(axis+2)%3] + t * ray.dir.cell[(axis+2)%3]; |
16
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
400 |
st.push(exPt); |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
401 |
} /* while */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
402 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
403 |
/* current node is the leaf . . . empty or full */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
404 |
/* "intersect ray with each object in the object list, discarding " |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
405 |
"those lying before stack[enPt].t or farther than stack[exPt].t" */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
406 |
Shape *nearest_shape = NULL; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
407 |
float dist = exPt->t; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
408 |
ShapeList::iterator shape; |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
409 |
for (shape = node->shapes->begin(); shape != node->shapes->end(); shape++) |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
410 |
if (*shape != origin_shape && (*shape)->intersect(ray, dist) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
411 |
&& dist >= enPt->t) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
412 |
nearest_shape = *shape; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
413 |
|
16
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
414 |
delete enPt; |
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
415 |
|
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
416 |
if (nearest_shape) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
417 |
{ |
16
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
418 |
while (!st.empty()) |
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
419 |
{ |
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
420 |
delete st.top(); |
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
421 |
st.pop(); |
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
422 |
} |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
423 |
nearest_distance = dist; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
424 |
return nearest_shape; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
425 |
} |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
426 |
|
16
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
427 |
enPt = exPt; |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
428 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
429 |
/* retrieve the pointer to the next node, it is possible that ray traversal terminates */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
430 |
node = enPt->node; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
431 |
st.pop(); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
432 |
} /* while */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
433 |
|
14
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
434 |
delete enPt; |
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
435 |
|
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
436 |
/* ray leaves the scene */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
437 |
return NULL; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
438 |
} |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
439 |
|
11
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
440 |
// this should save whole kd-tree with triangles distributed into leaves |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
441 |
void KdTree::save(ostream &str, KdNode *node) |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
442 |
{ |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
443 |
if (!built) |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
444 |
return; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
445 |
if (node == NULL) |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
446 |
node = root; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
447 |
if (node->isLeaf()) |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
448 |
str << "(leaf: " << node->shapes->size() << " shapes)"; |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
449 |
else |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
450 |
{ |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
451 |
str << "(split " << (char)('x'+node->getAxis()) << " at " << node->getSplit() << "; L="; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
452 |
save(str, node->getLeftChild()); |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
453 |
str << "; R="; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
454 |
save(str, node->getRightChild()); |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
455 |
str << ";)"; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
456 |
} |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
457 |
} |
11
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
458 |
|
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
459 |
// load kd-tree from file/stream |
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
460 |
void KdTree::load(istream &str, KdNode *node) |
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
461 |
{ |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
462 |
|
11
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
463 |
} |