include/octree.h
branchpyrit
changeset 43 0b8b968b42d1
parent 36 b490093b0ac3
child 44 3763b26244f0
equal deleted inserted replaced
42:fbdeb3e04543 43:0b8b968b42d1
    10 
    10 
    11 #include "container.h"
    11 #include "container.h"
    12 #include "vector.h"
    12 #include "vector.h"
    13 #include "scene.h"
    13 #include "scene.h"
    14 
    14 
       
    15 #include <assert.h>
       
    16 
    15 using namespace std;
    17 using namespace std;
    16 
    18 
    17 class OctreeNode
    19 class OctreeNode
    18 {
    20 {
    19 	OctreeNode *children; // pointer to first of eight children
    21     union {
       
    22 		OctreeNode *children; // pointer to first of eight children
       
    23 		ShapeList *shapes;    // pointer to shape array, if this is leaf
       
    24 		off_t leaf;             // leaf indicator (bit 0)
       
    25 	};
    20 public:
    26 public:
    21 	ShapeList *shapes;    // pointer to shape array, if this is leaf
    27 	OctreeNode()
    22 
    28 	{
    23 	OctreeNode() : children(NULL) { shapes = new ShapeList(); };
    29 		shapes = new ShapeList();
       
    30 		assert(sizeof(off_t)==sizeof(void*) && !isLeaf());
       
    31 		setLeaf();
       
    32 	};
    24 	~OctreeNode();
    33 	~OctreeNode();
    25 
    34 
    26 	bool isLeaf() { return shapes != NULL; };
    35 	bool isLeaf() { return leaf & 1; };
       
    36 	void setLeaf() { leaf = leaf | 1; };
    27 
    37 
    28 	OctreeNode *getChild(const int num) { return children+num; };
    38 	void makeChildren() { children = new OctreeNode[8]; assert(!isLeaf()); }; // this also cleans leaf bit
       
    39 	OctreeNode *getChild(const int num) { assert(!isLeaf()); return children + num; };
    29 
    40 
    30 	void addShape(Shape* aShape) { shapes->push_back(aShape); };
    41 	void addShape(Shape* aShape) { getShapes()->push_back(aShape); };
       
    42 	ShapeList *getShapes() { return (ShapeList*)((off_t)shapes & ~(off_t)1); };
       
    43 	void setShapes(ShapeList *const ashapes) { shapes = ashapes; assert(!isLeaf()); setLeaf(); };
    31 
    44 
    32 	void subdivide(BBox bbox, int maxdepth);
    45 	void subdivide(BBox bbox, int maxdepth);
    33 };
    46 };
    34 
    47 
    35 class Octree: public Container
    48 class Octree: public Container