include/kdtree.h
branchpyrit
changeset 92 9af5c039b678
parent 91 9d66d323c354
child 93 96d65f841791
equal deleted inserted replaced
91:9d66d323c354 92:9af5c039b678
    29 
    29 
    30 #include <iostream>
    30 #include <iostream>
    31 #include <fstream>
    31 #include <fstream>
    32 #include <assert.h>
    32 #include <assert.h>
    33 
    33 
    34 #include "container.h"
    34 #include "common.h"
    35 #include "vector.h"
    35 #include "vector.h"
    36 #include "scene.h"
    36 #include "scene.h"
       
    37 #include "container.h"
       
    38 #include "mempool.h"
    37 
    39 
    38 using namespace std;
    40 using namespace std;
    39 
    41 
    40 /**
    42 /**
    41  * a node of kd-tree
    43  * a node of kd-tree
    42  */
    44  */
    43 class KdNode
    45 class KdNode
    44 {
    46 {
    45 	Float split;
    47 	union {
       
    48 		Float split;
       
    49 		void *pad64;  /* pad to 64 bits on 64bit platforms */
       
    50 	};
    46 	union {
    51 	union {
    47 		KdNode *children;
    52 		KdNode *children;
    48 		ShapeList *shapes;
    53 		ShapeList *shapes;
    49 		int flags; /* 0,1,2 => x,y,z; 3 => leaf */
    54 		int flags; /* 0,1,2 => x,y,z; 3 => leaf */
    50 	};
    55 	};
    60 
    65 
    61 	void setSplit(Float aSplit) { split = aSplit; };
    66 	void setSplit(Float aSplit) { split = aSplit; };
    62 	const Float& getSplit() const { return split; };
    67 	const Float& getSplit() const { return split; };
    63 
    68 
    64 	void setChildren(KdNode *node) { children = node; assert((flags & 3) == 0); };
    69 	void setChildren(KdNode *node) { children = node; assert((flags & 3) == 0); };
    65 	KdNode* getLeftChild() const { return (KdNode*)((off_t)children & ~3); };
    70 	KdNode* getLeftChild() const { return (KdNode*)((size_t)children & ~3); };
    66 	KdNode* getRightChild() const { return (KdNode*)((off_t)children & ~3) + 1; };
    71 	KdNode* getRightChild() const { return (KdNode*)(((size_t)children & ~3) + 16); };
    67 
    72 
    68 	ShapeList* getShapes() const { return (ShapeList*)((off_t)shapes & ~3); };
    73 	ShapeList* getShapes() const { return (ShapeList*)((size_t)shapes & ~3); };
    69 	void addShape(Shape* aShape) { getShapes()->push_back(aShape); };
    74 	void addShape(Shape* aShape) { getShapes()->push_back(aShape); };
    70 };
    75 };
    71 
    76 
    72 /**
    77 /**
    73  * kd-tree
    78  * kd-tree
    74  */
    79  */
    75 class KdTree: public Container
    80 class KdTree: public Container
    76 {
    81 {
    77 	KdNode *root;
    82 	KdNode *root;
    78 	bool built;
    83 	bool built;
    79 	int max_depth;
    84 	const int max_depth;
       
    85 	MemoryPool<KdNode> mempool;
    80 
    86 
    81 	void recursive_build(KdNode *node, BBox bbox, int maxdepth);
    87 	void recursive_build(KdNode *node, const BBox &bbox, int maxdepth);
    82 	void recursive_load(istream &st, KdNode *node);
    88 	void recursive_load(istream &st, KdNode *node);
    83 public:
    89 public:
    84 	KdTree() : Container(), root(NULL), built(false), max_depth(32) {};
    90 	KdTree(): Container(), root(NULL), built(false), max_depth(32), mempool(64) {};
       
    91 	KdTree(int maxdepth): Container(), root(NULL), built(false), max_depth(maxdepth), mempool(64) {};
    85 	~KdTree() { if (root) delete root; };
    92 	~KdTree() { if (root) delete root; };
    86 	void addShape(Shape* aShape) { Container::addShape(aShape); built = false; };
    93 	void addShape(Shape* aShape) { Container::addShape(aShape); built = false; };
    87 	Shape *nearest_intersection(const Shape *origin_shape, const Ray &ray,
    94 	Shape *nearest_intersection(const Shape *origin_shape, const Ray &ray,
    88 		Float &nearest_distance);
    95 		Float &nearest_distance);
    89 #ifndef NO_SSE
    96 #ifndef NO_SIMD
    90 	void packet_intersection(const Shape **origin_shapes, const RayPacket &rays,
    97 	void packet_intersection(const Shape* const* origin_shapes, const RayPacket &rays,
    91 		Float *nearest_distances, Shape **nearest_shapes);
    98 		Float *nearest_distances, Shape **nearest_shapes);
    92 #endif
    99 #endif
    93 	void optimize() { build(); };
   100 	void optimize() { build(); };
    94 	void build();
   101 	void build();
    95 	bool isBuilt() const { return built; };
   102 	bool isBuilt() const { return built; };
    96 	KdNode *getRootNode() const { return root; };
   103 	KdNode *getRootNode() const { return root; };
    97 	void setMaxDepth(int md) { max_depth = md; };
       
    98 
   104 
    99 	ostream & dump(ostream &st);
   105 	ostream & dump(ostream &st);
   100 	istream & load(istream &st, Material *mat);
   106 	istream & load(istream &st, Material *mat);
   101 };
   107 };
   102 
   108