summaryrefslogtreecommitdiffstats
path: root/BSPTree.h
diff options
context:
space:
mode:
authorMatthias Schiffer <matthias@gamezock.de>2009-12-09 16:25:16 +0100
committerMatthias Schiffer <matthias@gamezock.de>2009-12-09 16:25:16 +0100
commit3035d4c9e76d6f28579d0b5a024e1c1b2a538919 (patch)
tree497d32485f6510e39d82a2227fb1aaf3129f0206 /BSPTree.h
parent482852aceacb54879c08e6fffc909a2cc160d483 (diff)
downloadc3d-3035d4c9e76d6f28579d0b5a024e1c1b2a538919.tar
c3d-3035d4c9e76d6f28579d0b5a024e1c1b2a538919.zip
BSPTrees angefangen
Diffstat (limited to 'BSPTree.h')
-rw-r--r--BSPTree.h58
1 files changed, 58 insertions, 0 deletions
diff --git a/BSPTree.h b/BSPTree.h
index 345f143..dc7e2df 100644
--- a/BSPTree.h
+++ b/BSPTree.h
@@ -3,10 +3,68 @@
#include "Triangle.h"
#include <list>
+#include <cmath>
class BSPTree {
private:
+ class Plane {
+ public:
+ Plane() : d(0) {}
+ Plane(const Vector &n, float d0) : normal(n), d(d0) {}
+ bool contains(Vertex v) {
+ return (fabsf(normal.dot(Vector(v)) - d) < 1E-6);
+ }
+
+ bool isBehind(Vertex v) {
+ return (normal.dot(Vector(v)) - d) < 0;
+ }
+
+ const Vector& getNormal() {
+ return normal;
+ }
+
+ private:
+ Vector normal;
+ float d;
+ };
+
+ public:
+ BSPTree(const std::list<Triangle> &triangles);
+
+ template<typename T>
+ void visit(T& visitor, const Vector &v) {
+ if(plane.getNormal().dot(v) > 0) {
+ if(frontTree)
+ frontTree->visit(visitor, v);
+
+ for(std::list<Triangle>::iterator t = triangles.begin(); t != triangles.end(); ++t) {
+ visitor(*t);
+ }
+
+ if(backTree)
+ backTree->visit(visitor, v);
+ }
+ else {
+ if(backTree)
+ backTree->visit(visitor, v);
+
+ for(std::list<Triangle>::iterator t = triangles.begin(); t != triangles.end(); ++t) {
+ visitor(*t);
+ }
+
+ if(frontTree)
+ frontTree->visit(visitor, v);
+ }
+ }
+
+ private:
+ Plane plane;
+ std::list<Triangle> triangles;
+ BSPTree *frontTree, *backTree;
+
+ static Vertex findCenter(const std::list<Triangle> &triangles);
+ static const Triangle* findNearestTriangle(const std::list<Triangle> &triangles, const Vertex &v);
};
#endif /* _BSPTREE_H_ */