include/kdtree.h
branchpyrit
changeset 74 09aedbf5f95f
parent 46 6493fb65f0b1
child 76 3b60fd0bea64
--- a/include/kdtree.h	Sun Apr 20 19:27:59 2008 +0200
+++ b/include/kdtree.h	Mon Apr 21 08:47:36 2008 +0200
@@ -29,6 +29,7 @@
 
 #include <iostream>
 #include <fstream>
+#include <assert.h>
 
 #include "container.h"
 #include "vector.h"
@@ -42,29 +43,30 @@
 class KdNode
 {
 	Float split;
-	short axis; /* 0,1,2 => x,y,z; 3 => leaf */
-public:
 	union {
 		KdNode *children;
 		ShapeList *shapes;
+		int flags; /* 0,1,2 => x,y,z; 3 => leaf */
 	};
-
-	KdNode() : axis(3) { shapes = new ShapeList(); };
+public:
+	KdNode() { shapes = new ShapeList(); assert((flags & 3) == 0); setLeaf(); };
 	~KdNode();
 
-	void setAxis(short aAxis) { axis = aAxis; };
-	short getAxis() { return axis; };
+	void setLeaf() { flags |= 3; };
+	bool isLeaf() { return (flags & 3) == 3; };
+
+	void setAxis(int aAxis) { flags &= ~3; flags |= aAxis; };
+	short getAxis() { return flags & 3; };
 
 	void setSplit(Float aSplit) { split = aSplit; };
 	Float getSplit() { return split; };
 
-	void setLeaf() { axis = 3; };
-	bool isLeaf() { return axis == 3; };
+	void setChildren(KdNode *node) { children = node; assert((flags & 3) == 0); };
+	KdNode *getLeftChild() { return (KdNode*)((off_t)children & ~3); };
+	KdNode *getRightChild() { return (KdNode*)((off_t)children & ~3) + 1; };
 
-	KdNode *getLeftChild() { return children; };
-	KdNode *getRightChild() { return children+1; };
-
-	void addShape(Shape* aShape) { shapes->push_back(aShape); };
+	ShapeList *getShapes() { return (ShapeList*)((off_t)shapes & ~3); };
+	void addShape(Shape* aShape) { getShapes()->push_back(aShape); };
 
 	void subdivide(BBox bbox, int maxdepth);
 };