include/octree.h
branchpyrit
changeset 43 0b8b968b42d1
parent 36 b490093b0ac3
child 44 3763b26244f0
--- a/include/octree.h	Sat Dec 29 13:53:33 2007 +0100
+++ b/include/octree.h	Sun Dec 30 00:11:47 2007 +0100
@@ -12,22 +12,35 @@
 #include "vector.h"
 #include "scene.h"
 
+#include <assert.h>
+
 using namespace std;
 
 class OctreeNode
 {
-	OctreeNode *children; // pointer to first of eight children
+    union {
+		OctreeNode *children; // pointer to first of eight children
+		ShapeList *shapes;    // pointer to shape array, if this is leaf
+		off_t leaf;             // leaf indicator (bit 0)
+	};
 public:
-	ShapeList *shapes;    // pointer to shape array, if this is leaf
-
-	OctreeNode() : children(NULL) { shapes = new ShapeList(); };
+	OctreeNode()
+	{
+		shapes = new ShapeList();
+		assert(sizeof(off_t)==sizeof(void*) && !isLeaf());
+		setLeaf();
+	};
 	~OctreeNode();
 
-	bool isLeaf() { return shapes != NULL; };
+	bool isLeaf() { return leaf & 1; };
+	void setLeaf() { leaf = leaf | 1; };
 
-	OctreeNode *getChild(const int num) { return children+num; };
+	void makeChildren() { children = new OctreeNode[8]; assert(!isLeaf()); }; // this also cleans leaf bit
+	OctreeNode *getChild(const int num) { assert(!isLeaf()); return children + num; };
 
-	void addShape(Shape* aShape) { shapes->push_back(aShape); };
+	void addShape(Shape* aShape) { getShapes()->push_back(aShape); };
+	ShapeList *getShapes() { return (ShapeList*)((off_t)shapes & ~(off_t)1); };
+	void setShapes(ShapeList *const ashapes) { shapes = ashapes; assert(!isLeaf()); setLeaf(); };
 
 	void subdivide(BBox bbox, int maxdepth);
 };