| author | Radek Brich <radek.brich@devl.cz> |
| Sun, 30 Dec 2007 00:11:47 +0100 | |
| branch | pyrit |
| changeset 43 | 0b8b968b42d1 |
| parent 36 | b490093b0ac3 |
| child 44 | 3763b26244f0 |
| permissions | -rw-r--r-- |
|
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
1 |
/* |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
2 |
* Pyrit Ray Tracer |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
3 |
* file: octree.h |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
4 |
* |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
5 |
* Radek Brich, 2006-2007 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
6 |
*/ |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
7 |
|
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
8 |
#ifndef OCTREE_H |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
9 |
#define OCTREE_H |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
10 |
|
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
11 |
#include "container.h" |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
12 |
#include "vector.h" |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
13 |
#include "scene.h" |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
14 |
|
|
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
15 |
#include <assert.h> |
|
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
16 |
|
|
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
17 |
using namespace std; |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
18 |
|
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
19 |
class OctreeNode |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
20 |
{
|
|
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
21 |
union {
|
|
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
22 |
OctreeNode *children; // pointer to first of eight children |
|
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
23 |
ShapeList *shapes; // pointer to shape array, if this is leaf |
|
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
24 |
off_t leaf; // leaf indicator (bit 0) |
|
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
25 |
}; |
|
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
26 |
public: |
|
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
27 |
OctreeNode() |
|
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
28 |
{
|
|
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
29 |
shapes = new ShapeList(); |
|
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
30 |
assert(sizeof(off_t)==sizeof(void*) && !isLeaf()); |
|
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
31 |
setLeaf(); |
|
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
32 |
}; |
|
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
33 |
~OctreeNode(); |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
34 |
|
|
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
35 |
bool isLeaf() { return leaf & 1; };
|
|
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
36 |
void setLeaf() { leaf = leaf | 1; };
|
|
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
37 |
|
|
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
38 |
void makeChildren() { children = new OctreeNode[8]; assert(!isLeaf()); }; // this also cleans leaf bit
|
|
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
39 |
OctreeNode *getChild(const int num) { assert(!isLeaf()); return children + num; };
|
|
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
40 |
|
|
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
41 |
void addShape(Shape* aShape) { getShapes()->push_back(aShape); };
|
|
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
42 |
ShapeList *getShapes() { return (ShapeList*)((off_t)shapes & ~(off_t)1); };
|
|
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
43 |
void setShapes(ShapeList *const ashapes) { shapes = ashapes; assert(!isLeaf()); setLeaf(); };
|
|
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
44 |
|
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
45 |
void subdivide(BBox bbox, int maxdepth); |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
46 |
}; |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
47 |
|
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
48 |
class Octree: public Container |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
49 |
{
|
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
50 |
OctreeNode *root; |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
51 |
bool built; |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
52 |
int max_depth; |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
53 |
public: |
|
36
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
54 |
Octree() : Container(), root(NULL), built(false), max_depth(10) {};
|
|
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
55 |
~Octree() { if (root) delete root; };
|
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
56 |
void addShape(Shape* aShape) { Container::addShape(aShape); built = false; };
|
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
57 |
Shape *nearest_intersection(const Shape *origin_shape, const Ray &ray, |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
58 |
Float &nearest_distance); |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
59 |
void optimize() { build(); };
|
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
60 |
void build(); |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
61 |
void save(ostream &str, OctreeNode *node = NULL) {};
|
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
62 |
void load(istream &str, OctreeNode *node = NULL) {};
|
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
63 |
void setMaxDepth(int md) { max_depth = md; };
|
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
64 |
}; |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
65 |
|
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
66 |
#endif |