src/kdtree.h
branchpyrit
changeset 12 f4fcabf05785
parent 11 4d192e13ee84
child 15 a0a3e334744f
equal deleted inserted replaced
11:4d192e13ee84 12:f4fcabf05785
    11 
    11 
    12 class ShapeList: public vector<Shape*>
    12 class ShapeList: public vector<Shape*>
    13 {
    13 {
    14 };
    14 };
    15 
    15 
    16 class SortableShape
       
    17 {
       
    18 public:
       
    19 	Shape *shape;
       
    20 	BBox bbox;
       
    21 	short axis;
       
    22 	short mark;
       
    23 
       
    24 	SortableShape(Shape *aShape, short &aAxis): shape(aShape), axis(aAxis), mark(0)
       
    25 		{ bbox = shape->get_bbox(); };
       
    26 	friend bool operator<(const SortableShape& a, const SortableShape& b)
       
    27 		{ return a.bbox.L.cell[a.axis] < b.bbox.L.cell[b.axis]; };
       
    28 	void setAxis(short aAxis) { axis = aAxis; };
       
    29 	void setMark() { mark = 1; };
       
    30 	short hasMark() { return mark; };
       
    31 };
       
    32 
       
    33 class SortableShapeList: public vector<SortableShape>
       
    34 {
       
    35 public:
       
    36 	SortableShapeList(ShapeList &shapes, short axis)
       
    37 	{
       
    38 		ShapeList::iterator shape;
       
    39 		for (shape = shapes.begin(); shape != shapes.end(); shape++)
       
    40 			push_back(SortableShape(*shape, axis));
       
    41 	};
       
    42 };
       
    43 
       
    44 class SplitPos
       
    45 {
       
    46 public:
       
    47 	float pos;
       
    48 	int lnum, rnum;
       
    49 	SplitPos(): pos(0.0), lnum(0), rnum(0) {};
       
    50 	SplitPos(float &aPos): pos(aPos), lnum(0), rnum(0) {};
       
    51 	friend bool operator<(const SplitPos& a, const SplitPos& b)
       
    52 		{ return a.pos < b.pos; };
       
    53 	friend bool operator==(const SplitPos& a, const SplitPos& b)
       
    54 		{ return a.pos == b.pos; };
       
    55 };
       
    56 
       
    57 class SplitList: public vector<SplitPos>
       
    58 {
       
    59 };
       
    60 
       
    61 class Container
    16 class Container
    62 {
    17 {
    63 protected:
    18 protected:
    64 	BBox bbox;
    19 	BBox bbox;
    65 public:
    20 public:
    66 	ShapeList shapes;
    21 	ShapeList shapes;
    67 	Container(): bbox(), shapes() {};
    22 	Container(): bbox(), shapes() {};
    68 	virtual ~Container() {};
    23 	virtual ~Container() {};
    69 	virtual void addShape(Shape* aShape);
    24 	virtual void addShape(Shape* aShape);
    70 	//void addShapeNoExtend(Shape* aShape) { shapes.push_back(aShape); };
    25 	//void addShapeNoExtend(Shape* aShape) { shapes.push_back(aShape); };
    71         virtual Shape *nearest_intersection(const Shape *origin_shape, const Ray &ray,
    26 	virtual Shape *nearest_intersection(const Shape *origin_shape, const Ray &ray,
    72 		float &nearest_distance);
    27 		float &nearest_distance);
    73 	virtual void optimize() {};
    28 	virtual void optimize() {};
    74 };
    29 };
    75 
    30 
    76 class KdNode
    31 class KdNode
   106 	KdNode *root;
    61 	KdNode *root;
   107 	bool built;
    62 	bool built;
   108 public:
    63 public:
   109 	KdTree() : Container(), built(false) {};
    64 	KdTree() : Container(), built(false) {};
   110 	void addShape(Shape* aShape) { Container::addShape(aShape); built = false; };
    65 	void addShape(Shape* aShape) { Container::addShape(aShape); built = false; };
       
    66 	Shape *nearest_intersection(const Shape *origin_shape, const Ray &ray,
       
    67 		float &nearest_distance);
       
    68 	void optimize() { build(); };
   111 	void build();
    69 	void build();
   112 	void optimize() { build(); };
       
   113 	void save(ostream &str, KdNode *node = NULL);
    70 	void save(ostream &str, KdNode *node = NULL);
   114 	void load(istream &str, KdNode *node = NULL);
    71 	void load(istream &str, KdNode *node = NULL);
   115 };
    72 };
   116 
    73 
   117 #endif
    74 #endif