include/octree.h
author Radek Brich <radek.brich@devl.cz>
Thu, 03 Jan 2008 18:06:34 +0100
branchpyrit
changeset 44 3763b26244f0
parent 43 0b8b968b42d1
child 46 6493fb65f0b1
permissions -rw-r--r--
MIT license for sources update README, DEVNOTES and TODO
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
/*
44
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
     2
 * octree.h: Octree class
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
     3
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
     4
 * This file is part of Pyrit Ray Tracer.
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
     5
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
     6
 * Copyright 2007  Radek Brich
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
 *
44
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
     8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
     9
 * of this software and associated documentation files (the "Software"), to deal
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    10
 * in the Software without restriction, including without limitation the rights
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    12
 * copies of the Software, and to permit persons to whom the Software is
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    13
 * furnished to do so, subject to the following conditions:
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    14
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    15
 * The above copyright notice and this permission notice shall be included in
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    16
 * all copies or substantial portions of the Software.
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    17
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    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
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
class OctreeNode
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
{
43
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    40
    union {
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    41
		OctreeNode *children; // pointer to first of eight children
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    42
		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
    43
		off_t leaf;             // leaf indicator (bit 0)
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    44
	};
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
public:
43
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    46
	OctreeNode()
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    47
	{
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    48
		shapes = new ShapeList();
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    49
		assert(sizeof(off_t)==sizeof(void*) && !isLeaf());
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    50
		setLeaf();
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    51
	};
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
	~OctreeNode();
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
43
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    54
	bool isLeaf() { return leaf & 1; };
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    55
	void setLeaf() { leaf = leaf | 1; };
35
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
	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
    58
	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
    59
43
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    60
	void addShape(Shape* aShape) { getShapes()->push_back(aShape); };
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    61
	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
    62
	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
    63
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    64
	void subdivide(BBox bbox, int maxdepth);
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
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
class Octree: public Container
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
	OctreeNode *root;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
	bool built;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    71
	int max_depth;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    72
public:
36
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 35
diff changeset
    73
	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
    74
	~Octree() { if (root) delete root; };
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
	void addShape(Shape* aShape) { Container::addShape(aShape); built = false; };
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
	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
    77
		Float &nearest_distance);
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    78
	void optimize() { build(); };
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    79
	void build();
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    80
	void save(ostream &str, OctreeNode *node = NULL) {};
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    81
	void load(istream &str, OctreeNode *node = NULL) {};
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    82
	void setMaxDepth(int md) { max_depth = md; };
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    83
};
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    84
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    85
#endif