#ifndef ZOOM_BSPTREE_H_ #define ZOOM_BSPTREE_H_ #include "Triangle.h" #include #include #include namespace Zoom { class BSPTree { private: class Plane { public: Plane() : d(0) {} Plane(const vmml::vec3f &n, float d0) : normal(n), d(d0) {} Plane(const Triangle &t) : normal(t.getNormal()), d(t.getVertex(0).dot(normal)) {} bool contains(const vmml::vec3f &v) const { return (fabsf(normal.dot(v) - d) < 1E-6); } bool isBehind(const vmml::vec3f &v) const { return (normal.dot(v) - d) < 0; } bool isInFront(const vmml::vec3f &v) const { return (normal.dot(v) - d) > 0; } bool contains(const Triangle &t) const { for(int i = 0; i < 3; ++i) { if(!contains(t.getVertex(i))) return false; } return true; } bool isBehind(const Triangle &t) const { for(int i = 0; i < 3; ++i) { if(!isBehind(t.getVertex(i)) && !contains(t.getVertex(i))) return false; } return true; } bool isInFront(const Triangle &t) const { for(int i = 0; i < 3; ++i) { if(!isInFront(t.getVertex(i)) && !contains(t.getVertex(i))) return false; } return true; } const vmml::vec3f& getNormal() const { return normal; } vmml::vec3f intersection(const vmml::vec3f &p, const vmml::vec3f &dir) const; void partition(const Triangle &t, std::list *front, std::list *back) const; private: vmml::vec3f normal; float d; }; public: BSPTree(const std::list &triangles); BSPTree(const BSPTree &tree) : frontTree(0), backTree(0) { *this = tree; } virtual ~BSPTree() { if(frontTree) delete frontTree; if(backTree) delete backTree; } BSPTree& operator=(const BSPTree &tree); template void visit(const T& visitor, const vmml::vec3f &p) { doVisit(visitor, p); } template void visit(T& visitor, const vmml::vec3f &p) { doVisit(visitor, p); } template void visit(const T& visitor, const vmml::vec3f &p) const { doVisit(visitor, p); } template void visit(T& visitor, const vmml::vec3f &p) const { doVisit(visitor, p); } private: Plane plane; std::list triangles; BSPTree *frontTree, *backTree; template void doVisit(T& visitor, const vmml::vec3f &p) { if(plane.isBehind(p)) { if(frontTree) frontTree->visit(visitor, p); for(std::list::iterator t = triangles.begin(); t != triangles.end(); ++t) { visitor(*t); } if(backTree) backTree->visit(visitor, p); } else { if(backTree) backTree->visit(visitor, p); for(std::list::iterator t = triangles.begin(); t != triangles.end(); ++t) { visitor(*t); } if(frontTree) frontTree->visit(visitor, p); } } template void doVisit(T& visitor, const vmml::vec3f &p) const { if(plane.isBehind(p)) { if(frontTree) frontTree->visit(visitor, p); for(std::list::const_iterator t = triangles.begin(); t != triangles.end(); ++t) { visitor(*t); } if(backTree) backTree->visit(visitor, p); } else { if(backTree) backTree->visit(visitor, p); for(std::list::const_iterator t = triangles.begin(); t != triangles.end(); ++t) { visitor(*t); } if(frontTree) frontTree->visit(visitor, p); } } static vmml::vec3f findCenter(const std::list &triangles); static const Triangle* findNearestTriangle(const std::list &triangles, const vmml::vec3f &v); }; } #endif /* ZOOM_BSPTREE_H_ */