include/kdtree.h
branchpyrit
changeset 74 09aedbf5f95f
parent 46 6493fb65f0b1
child 76 3b60fd0bea64
equal deleted inserted replaced
73:a5127346fbcd 74:09aedbf5f95f
    27 #ifndef KDTREE_H
    27 #ifndef KDTREE_H
    28 #define KDTREE_H
    28 #define KDTREE_H
    29 
    29 
    30 #include <iostream>
    30 #include <iostream>
    31 #include <fstream>
    31 #include <fstream>
       
    32 #include <assert.h>
    32 
    33 
    33 #include "container.h"
    34 #include "container.h"
    34 #include "vector.h"
    35 #include "vector.h"
    35 #include "scene.h"
    36 #include "scene.h"
    36 
    37 
    40  * a node of kd-tree
    41  * a node of kd-tree
    41  */
    42  */
    42 class KdNode
    43 class KdNode
    43 {
    44 {
    44 	Float split;
    45 	Float split;
    45 	short axis; /* 0,1,2 => x,y,z; 3 => leaf */
       
    46 public:
       
    47 	union {
    46 	union {
    48 		KdNode *children;
    47 		KdNode *children;
    49 		ShapeList *shapes;
    48 		ShapeList *shapes;
       
    49 		int flags; /* 0,1,2 => x,y,z; 3 => leaf */
    50 	};
    50 	};
    51 
    51 public:
    52 	KdNode() : axis(3) { shapes = new ShapeList(); };
    52 	KdNode() { shapes = new ShapeList(); assert((flags & 3) == 0); setLeaf(); };
    53 	~KdNode();
    53 	~KdNode();
    54 
    54 
    55 	void setAxis(short aAxis) { axis = aAxis; };
    55 	void setLeaf() { flags |= 3; };
    56 	short getAxis() { return axis; };
    56 	bool isLeaf() { return (flags & 3) == 3; };
       
    57 
       
    58 	void setAxis(int aAxis) { flags &= ~3; flags |= aAxis; };
       
    59 	short getAxis() { return flags & 3; };
    57 
    60 
    58 	void setSplit(Float aSplit) { split = aSplit; };
    61 	void setSplit(Float aSplit) { split = aSplit; };
    59 	Float getSplit() { return split; };
    62 	Float getSplit() { return split; };
    60 
    63 
    61 	void setLeaf() { axis = 3; };
    64 	void setChildren(KdNode *node) { children = node; assert((flags & 3) == 0); };
    62 	bool isLeaf() { return axis == 3; };
    65 	KdNode *getLeftChild() { return (KdNode*)((off_t)children & ~3); };
       
    66 	KdNode *getRightChild() { return (KdNode*)((off_t)children & ~3) + 1; };
    63 
    67 
    64 	KdNode *getLeftChild() { return children; };
    68 	ShapeList *getShapes() { return (ShapeList*)((off_t)shapes & ~3); };
    65 	KdNode *getRightChild() { return children+1; };
    69 	void addShape(Shape* aShape) { getShapes()->push_back(aShape); };
    66 
       
    67 	void addShape(Shape* aShape) { shapes->push_back(aShape); };
       
    68 
    70 
    69 	void subdivide(BBox bbox, int maxdepth);
    71 	void subdivide(BBox bbox, int maxdepth);
    70 };
    72 };
    71 
    73 
    72 /**
    74 /**