include/kdtree.h
branchpyrit
changeset 78 9569e9f35374
parent 76 3b60fd0bea64
child 80 907929fa9b59
equal deleted inserted replaced
77:dbe8438d5dca 78:9569e9f35374
    51 public:
    51 public:
    52 	KdNode() { shapes = new ShapeList(); assert((flags & 3) == 0); setLeaf(); };
    52 	KdNode() { shapes = new ShapeList(); assert((flags & 3) == 0); setLeaf(); };
    53 	~KdNode();
    53 	~KdNode();
    54 
    54 
    55 	void setLeaf() { flags |= 3; };
    55 	void setLeaf() { flags |= 3; };
    56 	bool isLeaf() { return (flags & 3) == 3; };
    56 	const bool isLeaf() const { return (flags & 3) == 3; };
    57 
    57 
    58 	void setAxis(int aAxis) { flags &= ~3; flags |= aAxis; };
    58 	void setAxis(int aAxis) { flags &= ~3; flags |= aAxis; };
    59 	short getAxis() { return flags & 3; };
    59 	const int getAxis() const { return flags & 3; };
    60 
    60 
    61 	void setSplit(Float aSplit) { split = aSplit; };
    61 	void setSplit(Float aSplit) { split = aSplit; };
    62 	Float& getSplit() { return split; };
    62 	const Float& getSplit() const { return split; };
    63 
    63 
    64 	void setChildren(KdNode *node) { children = node; assert((flags & 3) == 0); };
    64 	void setChildren(KdNode *node) { children = node; assert((flags & 3) == 0); };
    65 	KdNode* getLeftChild() { return (KdNode*)((off_t)children & ~3); };
    65 	KdNode* getLeftChild() const { return (KdNode*)((off_t)children & ~3); };
    66 	KdNode* getRightChild() { return (KdNode*)((off_t)children & ~3) + 1; };
    66 	KdNode* getRightChild() const { return (KdNode*)((off_t)children & ~3) + 1; };
    67 
    67 
    68 	ShapeList* getShapes() { return (ShapeList*)((off_t)shapes & ~3); };
    68 	ShapeList* getShapes() const { return (ShapeList*)((off_t)shapes & ~3); };
    69 	void addShape(Shape* aShape) { getShapes()->push_back(aShape); };
    69 	void addShape(Shape* aShape) { getShapes()->push_back(aShape); };
    70 };
    70 };
    71 
    71 
    72 /**
    72 /**
    73  * kd-tree
    73  * kd-tree
    77 	KdNode *root;
    77 	KdNode *root;
    78 	bool built;
    78 	bool built;
    79 	int max_depth;
    79 	int max_depth;
    80 
    80 
    81 	void recursive_build(KdNode *node, BBox bbox, int maxdepth);
    81 	void recursive_build(KdNode *node, BBox bbox, int maxdepth);
       
    82 	void recursive_load(istream &st, KdNode *node);
    82 public:
    83 public:
    83 	KdTree() : Container(), root(NULL), built(false), max_depth(32) {};
    84 	KdTree() : Container(), root(NULL), built(false), max_depth(32) {};
    84 	~KdTree() { if (root) delete root; };
    85 	~KdTree() { if (root) delete root; };
    85 	void addShape(Shape* aShape) { Container::addShape(aShape); built = false; };
    86 	void addShape(Shape* aShape) { Container::addShape(aShape); built = false; };
    86 	Shape *nearest_intersection(const Shape *origin_shape, const Ray &ray,
    87 	Shape *nearest_intersection(const Shape *origin_shape, const Ray &ray,
    87 		Float &nearest_distance);
    88 		Float &nearest_distance);
    88 	void optimize() { build(); };
    89 	void optimize() { build(); };
    89 	void build();
    90 	void build();
    90 	void save(ostream &str, KdNode *node = NULL);
    91 	const bool isBuilt() const { return built; };
    91 	void load(istream &str, KdNode *node = NULL);
    92 	KdNode *getRootNode() const { return root; };
    92 	void setMaxDepth(int md) { max_depth = md; };
    93 	void setMaxDepth(int md) { max_depth = md; };
       
    94 
       
    95 	ostream & dump(ostream &st);
       
    96 	istream & load(istream &st);
    93 };
    97 };
    94 
    98 
    95 #endif
    99 #endif