author | Radek Brich <radek.brich@devl.cz> |
Thu, 10 Apr 2008 23:20:36 +0200 | |
branch | pyrit |
changeset 65 | 242839c6d27d |
parent 46 | 6493fb65f0b1 |
child 92 | 9af5c039b678 |
permissions | -rw-r--r-- |
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
1 |
/* |
44 | 2 |
* octree.h: Octree class |
3 |
* |
|
4 |
* This file is part of Pyrit Ray Tracer. |
|
5 |
* |
|
6 |
* Copyright 2007 Radek Brich |
|
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
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. |
|
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
25 |
*/ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
26 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
27 |
#ifndef OCTREE_H |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
28 |
#define OCTREE_H |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
29 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
30 |
#include "container.h" |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
31 |
#include "vector.h" |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
32 |
#include "scene.h" |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
33 |
|
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
34 |
#include <assert.h> |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
35 |
|
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
36 |
using namespace std; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
37 |
|
46 | 38 |
/** |
39 |
* a node of octree |
|
40 |
*/ |
|
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
41 |
class OctreeNode |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
42 |
{ |
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
43 |
union { |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
44 |
OctreeNode *children; // pointer to first of eight children |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
45 |
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
|
46 |
off_t leaf; // leaf indicator (bit 0) |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
47 |
}; |
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
48 |
public: |
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
49 |
OctreeNode() |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
50 |
{ |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
51 |
shapes = new ShapeList(); |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
52 |
assert(sizeof(off_t)==sizeof(void*) && !isLeaf()); |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
53 |
setLeaf(); |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
54 |
}; |
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
55 |
~OctreeNode(); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
56 |
|
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
57 |
bool isLeaf() { return leaf & 1; }; |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
58 |
void setLeaf() { leaf = leaf | 1; }; |
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
59 |
|
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
60 |
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
|
61 |
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
|
62 |
|
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
63 |
void addShape(Shape* aShape) { getShapes()->push_back(aShape); }; |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
64 |
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
|
65 |
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
|
66 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
67 |
void subdivide(BBox bbox, int maxdepth); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
68 |
}; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
69 |
|
46 | 70 |
/** |
71 |
* optimized octree |
|
72 |
*/ |
|
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
73 |
class Octree: public Container |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
74 |
{ |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
75 |
OctreeNode *root; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
76 |
bool built; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
77 |
int max_depth; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
78 |
public: |
36
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
79 |
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
|
80 |
~Octree() { if (root) delete root; }; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
81 |
void addShape(Shape* aShape) { Container::addShape(aShape); built = false; }; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
82 |
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
|
83 |
Float &nearest_distance); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
84 |
void optimize() { build(); }; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
85 |
void build(); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
86 |
void save(ostream &str, OctreeNode *node = NULL) {}; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
87 |
void load(istream &str, OctreeNode *node = NULL) {}; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
88 |
void setMaxDepth(int md) { max_depth = md; }; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
89 |
}; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
90 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
91 |
#endif |