include/kdtree.h
branchpyrit
changeset 92 9af5c039b678
parent 91 9d66d323c354
child 93 96d65f841791
--- a/include/kdtree.h	Fri May 02 13:27:47 2008 +0200
+++ b/include/kdtree.h	Mon May 05 15:31:14 2008 +0200
@@ -31,9 +31,11 @@
 #include <fstream>
 #include <assert.h>
 
-#include "container.h"
+#include "common.h"
 #include "vector.h"
 #include "scene.h"
+#include "container.h"
+#include "mempool.h"
 
 using namespace std;
 
@@ -42,7 +44,10 @@
  */
 class KdNode
 {
-	Float split;
+	union {
+		Float split;
+		void *pad64;  /* pad to 64 bits on 64bit platforms */
+	};
 	union {
 		KdNode *children;
 		ShapeList *shapes;
@@ -62,10 +67,10 @@
 	const Float& getSplit() const { return split; };
 
 	void setChildren(KdNode *node) { children = node; assert((flags & 3) == 0); };
-	KdNode* getLeftChild() const { return (KdNode*)((off_t)children & ~3); };
-	KdNode* getRightChild() const { return (KdNode*)((off_t)children & ~3) + 1; };
+	KdNode* getLeftChild() const { return (KdNode*)((size_t)children & ~3); };
+	KdNode* getRightChild() const { return (KdNode*)(((size_t)children & ~3) + 16); };
 
-	ShapeList* getShapes() const { return (ShapeList*)((off_t)shapes & ~3); };
+	ShapeList* getShapes() const { return (ShapeList*)((size_t)shapes & ~3); };
 	void addShape(Shape* aShape) { getShapes()->push_back(aShape); };
 };
 
@@ -76,25 +81,26 @@
 {
 	KdNode *root;
 	bool built;
-	int max_depth;
+	const int max_depth;
+	MemoryPool<KdNode> mempool;
 
-	void recursive_build(KdNode *node, BBox bbox, int maxdepth);
+	void recursive_build(KdNode *node, const BBox &bbox, int maxdepth);
 	void recursive_load(istream &st, KdNode *node);
 public:
-	KdTree() : Container(), root(NULL), built(false), max_depth(32) {};
+	KdTree(): Container(), root(NULL), built(false), max_depth(32), mempool(64) {};
+	KdTree(int maxdepth): Container(), root(NULL), built(false), max_depth(maxdepth), mempool(64) {};
 	~KdTree() { if (root) delete root; };
 	void addShape(Shape* aShape) { Container::addShape(aShape); built = false; };
 	Shape *nearest_intersection(const Shape *origin_shape, const Ray &ray,
 		Float &nearest_distance);
-#ifndef NO_SSE
-	void packet_intersection(const Shape **origin_shapes, const RayPacket &rays,
+#ifndef NO_SIMD
+	void packet_intersection(const Shape* const* origin_shapes, const RayPacket &rays,
 		Float *nearest_distances, Shape **nearest_shapes);
 #endif
 	void optimize() { build(); };
 	void build();
 	bool isBuilt() const { return built; };
 	KdNode *getRootNode() const { return root; };
-	void setMaxDepth(int md) { max_depth = md; };
 
 	ostream & dump(ostream &st);
 	istream & load(istream &st, Material *mat);