author | Radek Brich <radek.brich@devl.cz> |
Sun, 25 Nov 2007 17:58:29 +0100 | |
branch | pyrit |
changeset 15 | a0a3e334744f |
parent 12 | f4fcabf05785 |
child 16 | 20bceb605f48 |
permissions | -rw-r--r-- |
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
|
1 |
#ifndef KDTREE_H |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
2 |
#define KDTREE_H |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
3 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
4 |
#include <vector> |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
5 |
#include <iostream> |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
6 |
#include <fstream> |
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
|
7 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
8 |
#include "scene.h" |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
9 |
|
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
10 |
using namespace std; |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
11 |
|
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
12 |
class ShapeList: public vector<Shape*> |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
13 |
{ |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
14 |
}; |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
15 |
|
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
16 |
class Container |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
17 |
{ |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
18 |
protected: |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
19 |
BBox bbox; |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
20 |
public: |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
21 |
ShapeList shapes; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
22 |
Container(): bbox(), shapes() {}; |
11
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
23 |
virtual ~Container() {}; |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
24 |
virtual void addShape(Shape* aShape); |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
25 |
//void addShapeNoExtend(Shape* aShape) { shapes.push_back(aShape); }; |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
26 |
virtual Shape *nearest_intersection(const Shape *origin_shape, const Ray &ray, |
11
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
27 |
float &nearest_distance); |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
28 |
virtual void optimize() {}; |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
29 |
}; |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
30 |
|
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
31 |
class KdNode |
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 |
{ |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
33 |
float split; |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
34 |
short axis; /* 0,1,2 => x,y,z; 3 => leaf */ |
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
|
35 |
public: |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
36 |
union { |
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
37 |
KdNode *children; |
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
38 |
ShapeList *shapes; |
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
39 |
}; |
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
|
40 |
|
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
41 |
KdNode() : axis(3) { shapes = new ShapeList(); }; |
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
|
42 |
|
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
43 |
void setAxis(short aAxis) { axis = aAxis; }; |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
44 |
short getAxis() { return axis; }; |
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
|
45 |
|
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
46 |
void setSplit(float aSplit) { split = aSplit; }; |
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
|
47 |
float getSplit() { return split; }; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
48 |
|
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
49 |
void setLeaf() { axis = 3; }; |
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
50 |
bool isLeaf() { return axis == 3; }; |
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
|
51 |
|
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
52 |
KdNode *getLeftChild() { return children; }; |
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
53 |
KdNode *getRightChild() { return children+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
|
54 |
|
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
55 |
void addShape(Shape* aShape) { shapes->push_back(aShape); }; |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
56 |
|
9
3239f749e394
kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
57 |
void 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
|
58 |
}; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
59 |
|
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
60 |
class KdTree: public Container |
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
|
61 |
{ |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
62 |
KdNode *root; |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
63 |
bool built; |
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
|
64 |
public: |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
65 |
KdTree() : Container(), built(false) {}; |
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
66 |
void addShape(Shape* aShape) { Container::addShape(aShape); built = false; }; |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
67 |
Shape *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
|
68 |
float &nearest_distance); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
69 |
void optimize() { build(); }; |
11
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
70 |
void build(); |
10
f9fad94cd0cc
kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
71 |
void save(ostream &str, KdNode *node = NULL); |
11
4d192e13ee84
move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
72 |
void load(istream &str, KdNode *node = NULL); |
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 |
}; |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
74 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
75 |
#endif |