src/kdtree.h
branchpyrit
changeset 10 f9fad94cd0cc
parent 9 3239f749e394
child 11 4d192e13ee84
equal deleted inserted replaced
9:3239f749e394 10:f9fad94cd0cc
     1 #ifndef KDTREE_H
     1 #ifndef KDTREE_H
     2 #define KDTREE_H
     2 #define KDTREE_H
     3 
     3 
     4 #include <vector>
     4 #include <vector>
       
     5 #include <iostream>
       
     6 #include <fstream>
     5 
     7 
     6 #include "scene.h"
     8 #include "scene.h"
     7 
     9 
     8 using namespace std;
    10 using namespace std;
     9 
    11 
    42 class SplitPos
    44 class SplitPos
    43 {
    45 {
    44 public:
    46 public:
    45 	float pos;
    47 	float pos;
    46 	int lnum, rnum;
    48 	int lnum, rnum;
    47 	SplitPos(float &aPos): pos(aPos) {};
    49 	SplitPos(): pos(0.0), lnum(0), rnum(0) {};
       
    50 	SplitPos(float &aPos): pos(aPos), lnum(0), rnum(0) {};
    48 	friend bool operator<(const SplitPos& a, const SplitPos& b)
    51 	friend bool operator<(const SplitPos& a, const SplitPos& b)
    49 		{ return a.pos < b.pos; };
    52 		{ return a.pos < b.pos; };
       
    53 	friend bool operator==(const SplitPos& a, const SplitPos& b)
       
    54 		{ return a.pos == b.pos; };
    50 };
    55 };
    51 
    56 
    52 class SplitList: public vector<SplitPos>
    57 class SplitList: public vector<SplitPos>
    53 {
    58 {
    54 };
    59 };
    55 
    60 
    56 class Container
    61 class Container
    57 {
    62 {
    58 protected:
    63 protected:
    59 	ShapeList shapes;
       
    60 	BBox bbox;
    64 	BBox bbox;
    61 public:
    65 public:
    62 	Container(): shapes(), bbox() {};
    66 	ShapeList shapes;
    63 	void addShape(Shape* aShape);
    67 	Container(): bbox(), shapes() {};
       
    68 	virtual void addShape(Shape* aShape);
    64 	//void addShapeNoExtend(Shape* aShape) { shapes.push_back(aShape); };
    69 	//void addShapeNoExtend(Shape* aShape) { shapes.push_back(aShape); };
       
    70 	virtual void optimize() {};
    65 };
    71 };
    66 
    72 
    67 class KdNode
    73 class KdNode
    68 {
    74 {
    69 	float split;
    75 	float split;
    93 };
    99 };
    94 
   100 
    95 class KdTree: public Container
   101 class KdTree: public Container
    96 {
   102 {
    97 	KdNode *root;
   103 	KdNode *root;
       
   104 	bool built;
    98 public:
   105 public:
    99 	KdTree() {};
   106 	KdTree() : Container(), built(false) {};
   100 	void build();
   107 	void addShape(Shape* aShape) { Container::addShape(aShape); built = false; };
       
   108 	void optimize(); // build kd-tree
       
   109 	void save(ostream &str, KdNode *node = NULL);
   101 };
   110 };
   102 
   111 
   103 #endif
   112 #endif