author | Radek Brich <radek.brich@devl.cz> |
Tue, 26 Jul 2016 17:41:36 +0200 | |
branch | pyrit |
changeset 103 | 3b3257a410fe |
parent 98 | 64638385798a |
permissions | -rw-r--r-- |
94 | 1 |
/** |
2 |
* @file octree.h |
|
3 |
* @brief Octree class |
|
44 | 4 |
* |
5 |
* This file is part of Pyrit Ray Tracer. |
|
6 |
* |
|
94 | 7 |
* Copyright 2007, 2008 Radek Brich |
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
8 |
* |
44 | 9 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
10 |
* of this software and associated documentation files (the "Software"), to deal |
|
11 |
* in the Software without restriction, including without limitation the rights |
|
12 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
13 |
* copies of the Software, and to permit persons to whom the Software is |
|
14 |
* furnished to do so, subject to the following conditions: |
|
15 |
* |
|
16 |
* The above copyright notice and this permission notice shall be included in |
|
17 |
* all copies or substantial portions of the Software. |
|
18 |
* |
|
19 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
20 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
21 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
22 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
23 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
24 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
25 |
* THE SOFTWARE. |
|
35
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 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
28 |
#ifndef OCTREE_H |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
29 |
#define OCTREE_H |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
30 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
31 |
#include "container.h" |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
32 |
#include "vector.h" |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
33 |
#include "scene.h" |
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 |
#include <assert.h> |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
36 |
|
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
37 |
using namespace std; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
38 |
|
46 | 39 |
/** |
40 |
* a node of octree |
|
41 |
*/ |
|
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
42 |
class OctreeNode |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
43 |
{ |
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
44 |
union { |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
45 |
OctreeNode *children; // pointer to first of eight children |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
46 |
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
|
47 |
off_t leaf; // leaf indicator (bit 0) |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
48 |
}; |
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
49 |
public: |
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
50 |
OctreeNode() |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
51 |
{ |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
52 |
shapes = new ShapeList(); |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
53 |
assert(sizeof(off_t)==sizeof(void*) && !isLeaf()); |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
54 |
setLeaf(); |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
55 |
}; |
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
56 |
~OctreeNode(); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
57 |
|
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
58 |
bool isLeaf() { return leaf & 1; }; |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
59 |
void setLeaf() { leaf = leaf | 1; }; |
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
60 |
|
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
61 |
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
|
62 |
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
|
63 |
|
95
ca7d4c665531
build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
64 |
void addShape(const Shape* aShape) { getShapes()->push_back(aShape); }; |
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
46
diff
changeset
|
65 |
ShapeList *getShapes() { return (ShapeList*)((size_t)shapes & ~(size_t)1); }; |
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
66 |
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
|
67 |
|
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
46
diff
changeset
|
68 |
void subdivide(const BBox &bbox, int maxdepth); |
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
69 |
}; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
70 |
|
46 | 71 |
/** |
72 |
* optimized octree |
|
73 |
*/ |
|
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
74 |
class Octree: public Container |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
75 |
{ |
103 | 76 |
static const int MAX_DEPTH; |
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
77 |
OctreeNode *root; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
78 |
bool built; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
79 |
public: |
103 | 80 |
Octree() : Container(), root(NULL), built(false) {}; |
81 |
~Octree() { if (root) delete root; }; |
|
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
82 |
|
95
ca7d4c665531
build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
83 |
void addShape(const Shape* aShape) { Container::addShape(aShape); built = false; }; |
ca7d4c665531
build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
84 |
const Shape *nearest_intersection(const Shape *origin_shape, const Ray &ray, |
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
85 |
Float &nearest_distance); |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
86 |
void optimize() { build(); }; |
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
87 |
|
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
88 |
/** build the octree |
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
89 |
* this is alias of optimize() */ |
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
90 |
void build(); |
98
64638385798a
add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents:
95
diff
changeset
|
91 |
|
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
92 |
void save(ostream &str, OctreeNode *node = NULL) {}; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
93 |
void load(istream &str, OctreeNode *node = NULL) {}; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
94 |
}; |
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
95 |
|
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
96 |
#endif |