author | Radek Brich <radek.brich@devl.cz> |
Sun, 20 Apr 2008 16:48:24 +0200 | |
branch | pyrit |
changeset 72 | 7c3f38dff082 |
parent 71 | 4fedf7290929 |
child 74 | 09aedbf5f95f |
permissions | -rw-r--r-- |
34
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
30
diff
changeset
|
1 |
/* |
44 | 2 |
* kdtree.cc: KdTree class |
3 |
* |
|
4 |
* This file is part of Pyrit Ray Tracer. |
|
5 |
* |
|
6 |
* Copyright 2006, 2007 Radek Brich |
|
34
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
30
diff
changeset
|
7 |
* |
44 | 8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9 |
* of this software and associated documentation files (the "Software"), to deal |
|
10 |
* in the Software without restriction, including without limitation the rights |
|
11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
12 |
* copies of the Software, and to permit persons to whom the Software is |
|
13 |
* furnished to do so, subject to the following conditions: |
|
14 |
* |
|
15 |
* The above copyright notice and this permission notice shall be included in |
|
16 |
* all copies or substantial portions of the Software. |
|
17 |
* |
|
18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
21 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
24 |
* THE SOFTWARE. |
|
34
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
30
diff
changeset
|
25 |
*/ |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
30
diff
changeset
|
26 |
|
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
27 |
#include <algorithm> |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
28 |
#include <stack> |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
29 |
|
20
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents:
17
diff
changeset
|
30 |
#include "common.h" |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
31 |
#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
|
32 |
|
71
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
33 |
class ShapeBound |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
34 |
{ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
35 |
public: |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
36 |
Shape *shape; |
71
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
37 |
Float pos; |
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
38 |
bool end; |
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
39 |
ShapeBound(Shape *ashape, const Float apos, const bool aend): |
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
40 |
shape(ashape), pos(apos), end(aend) {}; |
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
41 |
friend bool operator<(const ShapeBound& a, const ShapeBound& b) |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
42 |
{ |
71
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
43 |
if (a.pos == b.pos) |
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
44 |
return a.shape < b.shape; |
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
45 |
else |
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
46 |
return a.pos < b.pos; |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
47 |
}; |
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 |
// stack element for kd-tree traversal |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
51 |
class StackElem |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
52 |
{ |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
53 |
public: |
12
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 */ |
22 | 55 |
Float t; /* the entry/exit signed distance */ |
12
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 */ |
22 | 57 |
StackElem(KdNode *anode, const Float &at, const Vector3 &apb): |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
58 |
node(anode), t(at), pb(apb) {}; |
12
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 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
61 |
// ---------------------------------------- |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
62 |
|
16
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
63 |
KdNode::~KdNode() |
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
64 |
{ |
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
65 |
if (isLeaf()) |
17
5176ba000a67
fix last leak as reported by valgrind
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
66 |
delete shapes; |
16
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
67 |
else |
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
68 |
delete[] children; |
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
69 |
} |
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
70 |
|
71
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
71 |
// kd-tree recursive build algorithm, inspired by PBRT (www.pbrt.org) |
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
72 |
void KdNode::subdivide(BBox bounds, int maxdepth) |
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
|
73 |
{ |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
74 |
if (maxdepth <= 0 || shapes->size() <= 2) |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
75 |
{ |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
76 |
setLeaf(); |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
77 |
return; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
78 |
} |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
79 |
|
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
80 |
// choose split axis |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
81 |
/*axis = 0; |
71
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
82 |
if (bounds.h() > bounds.w() && bounds.h() > bounds.d()) |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
83 |
axis = 1; |
71
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
84 |
if (bounds.d() > bounds.w() && bounds.d() > bounds.h()) |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
85 |
axis = 2; |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
86 |
*/ |
71
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
87 |
// create sorted list of shape bounds (= find all posible splits) |
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
88 |
vector<ShapeBound> edges[3]; |
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
89 |
ShapeList::iterator shape; |
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
90 |
for (shape = shapes->begin(); shape != shapes->end(); shape++) |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
91 |
{ |
71
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
92 |
BBox shapebounds = (*shape)->get_bbox(); |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
93 |
for (int ax = 0; ax < 3; ax++) |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
94 |
{ |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
95 |
edges[ax].push_back(ShapeBound(*shape, shapebounds.L[ax], 0)); |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
96 |
edges[ax].push_back(ShapeBound(*shape, shapebounds.H[ax], 1)); |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
97 |
} |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
98 |
} |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
99 |
for (int ax = 0; ax < 3; ax++) |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
100 |
sort(edges[ax].begin(), edges[ax].end()); |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
101 |
|
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
102 |
// choose best split pos |
22 | 103 |
const Float K = 1.4; // constant, K = cost of traversal / cost of ray-triangle intersection |
71
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
104 |
Float SAV = (bounds.w()*bounds.h() + // surface area of node |
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
105 |
bounds.w()*bounds.d() + bounds.h()*bounds.d()); |
22 | 106 |
Float cost = SAV * (K + shapes->size()); // initial cost = non-split cost |
71
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
107 |
|
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
108 |
vector<ShapeBound>::iterator edge, splitedge = edges[2].end(); |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
109 |
for (int ax = 0; ax < 3; ax++) |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
110 |
{ |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
111 |
int lnum = 0, rnum = shapes->size(); |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
112 |
BBox lbb = bounds; |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
113 |
BBox rbb = bounds; |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
114 |
for (edge = edges[ax].begin(); edge != edges[ax].end(); edge++) |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
115 |
{ |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
116 |
if (edge->end) |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
117 |
rnum--; |
71
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
118 |
|
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
119 |
// calculate SAH cost of this split |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
120 |
lbb.H.cell[ax] = edge->pos; |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
121 |
rbb.L.cell[ax] = edge->pos; |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
122 |
Float SAL = (lbb.w()*lbb.h() + lbb.w()*lbb.d() + lbb.h()*lbb.d()); |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
123 |
Float SAR = (rbb.w()*rbb.h() + rbb.w()*rbb.d() + rbb.h()*rbb.d()); |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
124 |
Float splitcost = K*SAV + SAL*(K + lnum) + SAR*(K + rnum); |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
125 |
|
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
126 |
if (splitcost < cost) |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
127 |
{ |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
128 |
axis = ax; |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
129 |
splitedge = edge; |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
130 |
cost = splitcost; |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
131 |
split = edge->pos; |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
132 |
} |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
133 |
|
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
134 |
if (!edge->end) |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
135 |
lnum++; |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
136 |
} |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
137 |
} |
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
|
138 |
|
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
139 |
if (splitedge == edges[2].end()) |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
140 |
{ |
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
141 |
setLeaf(); |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
142 |
return; |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
143 |
} |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
144 |
|
29
574c34441a1c
new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
28
diff
changeset
|
145 |
#if 0 |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
146 |
// 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
|
147 |
// 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
|
148 |
static ofstream F("kdtree.obj"); |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
149 |
Vector3 v; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
150 |
static int f=0; |
28
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
151 |
v.cell[axis] = split; |
71
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
152 |
v.cell[(axis+1)%3] = bounds.L.cell[(axis+1)%3]; |
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
153 |
v.cell[(axis+2)%3] = bounds.L.cell[(axis+2)%3]; |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
154 |
F << "v " << v.x << " " << v.y << " " << v.z << endl; |
71
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
155 |
v.cell[(axis+1)%3] = bounds.L.cell[(axis+1)%3]; |
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
156 |
v.cell[(axis+2)%3] = bounds.H.cell[(axis+2)%3]; |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
157 |
F << "v " << v.x << " " << v.y << " " << v.z << endl; |
71
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
158 |
v.cell[(axis+1)%3] = bounds.H.cell[(axis+1)%3]; |
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
159 |
v.cell[(axis+2)%3] = bounds.H.cell[(axis+2)%3]; |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
160 |
F << "v " << v.x << " " << v.y << " " << v.z << endl; |
71
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
161 |
v.cell[(axis+1)%3] = bounds.H.cell[(axis+1)%3]; |
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
162 |
v.cell[(axis+2)%3] = bounds.L.cell[(axis+2)%3]; |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
163 |
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
|
164 |
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
|
165 |
f += 4; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
166 |
#endif |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
167 |
|
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
168 |
// split this node |
17
5176ba000a67
fix last leak as reported by valgrind
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
169 |
delete shapes; |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
170 |
BBox lbb = bounds; |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
171 |
BBox rbb = bounds; |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
172 |
lbb.H.cell[axis] = split; |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
173 |
rbb.L.cell[axis] = split; |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
174 |
children = new KdNode[2]; |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
175 |
|
71
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
176 |
for (edge = edges[axis].begin(); edge != splitedge; edge++) |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
177 |
if (!edge->end && edge->shape->intersect_bbox(lbb)) |
71
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
178 |
children[0].addShape(edge->shape); |
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
179 |
for (edge = splitedge; edge < edges[axis].end(); edge++) |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
71
diff
changeset
|
180 |
if (edge->end && edge->shape->intersect_bbox(rbb)) |
71
4fedf7290929
simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents:
44
diff
changeset
|
181 |
children[1].addShape(edge->shape); |
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
182 |
|
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
183 |
children[0].subdivide(lbb, maxdepth-1); |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
184 |
children[1].subdivide(rbb, maxdepth-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
|
185 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
186 |
|
11
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
187 |
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
|
188 |
{ |
30
33f95441790e
pyrit_verbosity: new variable for controlling amount of output, see common.h
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
189 |
dbgmsg(1, "* building kd-tree\n"); |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
190 |
root = new KdNode(); |
17
5176ba000a67
fix last leak as reported by valgrind
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
191 |
ShapeList::iterator shape; |
5176ba000a67
fix last leak as reported by valgrind
Radek Brich <radek.brich@devl.cz>
parents:
16
diff
changeset
|
192 |
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
|
193 |
root->addShape(*shape); |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
194 |
root->subdivide(bbox, max_depth); |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
195 |
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
|
196 |
} |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
197 |
|
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
198 |
/* 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
|
199 |
Shape *KdTree::nearest_intersection(const Shape *origin_shape, const Ray &ray, |
22 | 200 |
Float &nearest_distance) |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
201 |
{ |
22 | 202 |
Float a, b; /* entry/exit signed distance */ |
203 |
Float t; /* signed distance to the splitting plane */ |
|
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
204 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
205 |
/* 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
|
206 |
if (!built) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
207 |
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
|
208 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
209 |
if (!bbox.intersect(ray, a, b)) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
210 |
return NULL; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
211 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
212 |
/* 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
|
213 |
KdNode *farchild, *node; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
214 |
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
|
215 |
|
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
216 |
/* std vector is much faster than stack */ |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
217 |
vector<StackElem*> st; |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
218 |
|
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
219 |
StackElem *enPt = new StackElem(NULL, a, |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
220 |
/* distinguish between internal and external origin of a ray*/ |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
221 |
a >= 0.0 ? |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
222 |
ray.o + ray.dir * a : /* external */ |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
223 |
ray.o); /* internal */ |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
224 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
225 |
/* setup initial exit point in the stack */ |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
226 |
StackElem *exPt = new StackElem(NULL, b, ray.o + ray.dir * b); |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
227 |
st.push_back(exPt); |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
228 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
229 |
/* 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
|
230 |
while (node) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
231 |
{ |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
232 |
exPt = st.back(); |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
233 |
/* loop until a leaf is found */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
234 |
while (!node->isLeaf()) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
235 |
{ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
236 |
/* retrieve position of splitting plane */ |
22 | 237 |
Float splitVal = node->getSplit(); |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
238 |
short axis = node->getAxis(); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
239 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
240 |
if (enPt->pb[axis] <= splitVal) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
241 |
{ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
242 |
if (exPt->pb[axis] <= splitVal) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
243 |
{ /* 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
|
244 |
node = node->getLeftChild(); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
245 |
continue; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
246 |
} |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
247 |
if (exPt->pb[axis] == splitVal) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
248 |
{ /* case Z1 */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
249 |
node = node->getRightChild(); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
250 |
continue; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
251 |
} |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
252 |
/* case N4 */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
253 |
farchild = node->getRightChild(); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
254 |
node = node->getLeftChild(); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
255 |
} |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
256 |
else |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
257 |
{ /* (enPt->pb[axis] > splitVal) */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
258 |
if (splitVal < exPt->pb[axis]) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
259 |
{ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
260 |
/* case P1, P2, P3, and N5 */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
261 |
node = node->getRightChild(); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
262 |
continue; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
263 |
} |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
264 |
/* case P4*/ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
265 |
farchild = node->getLeftChild(); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
266 |
node = node->getRightChild(); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
267 |
} |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
268 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
269 |
/* 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
|
270 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
271 |
/* 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
|
272 |
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
|
273 |
|
16
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
274 |
/* setup the new exit point and push it onto stack */ |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
275 |
exPt = new StackElem(farchild, t, Vector3()); |
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
276 |
exPt->pb.cell[axis] = splitVal; |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
277 |
exPt->pb.cell[(axis+1)%3] = ray.o.cell[(axis+1)%3] + t * ray.dir.cell[(axis+1)%3]; |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
278 |
exPt->pb.cell[(axis+2)%3] = ray.o.cell[(axis+2)%3] + t * ray.dir.cell[(axis+2)%3]; |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
279 |
st.push_back(exPt); |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
280 |
} /* while */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
281 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
282 |
/* 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
|
283 |
/* "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
|
284 |
"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
|
285 |
Shape *nearest_shape = NULL; |
22 | 286 |
Float dist = exPt->t; |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
287 |
ShapeList::iterator shape; |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
288 |
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
|
289 |
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
|
290 |
&& dist >= enPt->t) |
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
291 |
{ |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
292 |
nearest_shape = *shape; |
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
293 |
nearest_distance = dist; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
294 |
} |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
295 |
|
16
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
296 |
delete enPt; |
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
297 |
|
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
298 |
if (nearest_shape) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
299 |
{ |
16
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
300 |
while (!st.empty()) |
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
301 |
{ |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
302 |
delete st.back(); |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
303 |
st.pop_back(); |
16
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
304 |
} |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
305 |
return nearest_shape; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
306 |
} |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
307 |
|
16
20bceb605f48
add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
308 |
enPt = exPt; |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
309 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
310 |
/* 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
|
311 |
node = enPt->node; |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
312 |
st.pop_back(); |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
313 |
} /* while */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
314 |
|
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
|
315 |
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
|
316 |
|
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
317 |
/* ray leaves the scene */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
318 |
return NULL; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
319 |
} |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
320 |
|
11
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
321 |
// 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
|
322 |
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
|
323 |
{ |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
324 |
if (!built) |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
325 |
return; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
326 |
if (node == NULL) |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
327 |
node = root; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
328 |
if (node->isLeaf()) |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
329 |
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
|
330 |
else |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
331 |
{ |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
332 |
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
|
333 |
save(str, node->getLeftChild()); |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
334 |
str << "; R="; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
335 |
save(str, node->getRightChild()); |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
336 |
str << ";)"; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
337 |
} |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
338 |
} |
11
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
339 |
|
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
340 |
// 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
|
341 |
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
|
342 |
{ |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
343 |
|
11
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
344 |
} |