include/octree.h
author Radek Brich <radek.brich@devl.cz>
Mon, 17 Dec 2007 22:03:50 +0100
branchpyrit
changeset 40 929aad02c5f2
parent 36 b490093b0ac3
child 43 0b8b968b42d1
permissions -rw-r--r--
Makefile: added help and distclean target, plus small fixes ccdemos/common_sdl.h: print fps to window caption instead of console update and key callbacks fixed segfault when resizing window pressing c now causes print out of camera coordinates ccdemos/spheres_shadow.cc: controlling position of a light and focal length of camera
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
/*
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
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
using namespace std;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
class OctreeNode
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
	OctreeNode *children; // pointer to first of eight children
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
public:
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
	ShapeList *shapes;    // pointer to shape array, if this is leaf
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
	OctreeNode() : children(NULL) { shapes = new ShapeList(); };
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
	~OctreeNode();
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
	bool isLeaf() { return shapes != NULL; };
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
	OctreeNode *getChild(const int num) { return children+num; };
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
	void addShape(Shape* aShape) { shapes->push_back(aShape); };
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
	void subdivide(BBox bbox, int maxdepth);
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
};
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
class Octree: public Container
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
	OctreeNode *root;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
	bool built;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
	int max_depth;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
public:
36
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 35
diff changeset
    41
	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
    42
	~Octree() { if (root) delete root; };
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
	void addShape(Shape* aShape) { Container::addShape(aShape); built = false; };
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
	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
    45
		Float &nearest_distance);
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
	void optimize() { build(); };
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
	void build();
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
	void save(ostream &str, OctreeNode *node = NULL) {};
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
	void load(istream &str, OctreeNode *node = NULL) {};
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
	void setMaxDepth(int md) { max_depth = md; };
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
};
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
#endif