This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
neofx-zoom-plusplus/BSPTree.h

178 lines
4.5 KiB
C
Raw Normal View History

2009-12-14 13:54:34 +01:00
/*
* BSPTree.h
*
* Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2009-12-13 18:49:36 +01:00
#ifndef ZOOM_BSPTREE_H_
#define ZOOM_BSPTREE_H_
#include "Triangle.h"
#include <list>
#include <cmath>
2009-12-14 13:54:34 +01:00
2009-12-13 18:49:36 +01:00
#include <vmmlib/vector.hpp>
2009-12-14 13:54:34 +01:00
#include <boost/shared_ptr.hpp>
2009-12-13 18:49:36 +01:00
namespace Zoom {
class BSPTree {
2009-12-14 13:54:34 +01:00
public:
class TriangleData {
protected:
TriangleData() {}
public:
virtual ~TriangleData() {}
};
struct TriangleRecord {
public:
TriangleRecord(Triangle triangle0, boost::shared_ptr<TriangleData> data0)
: triangle(triangle0), data(data0) {}
TriangleRecord(Triangle triangle0) : triangle(triangle0) {}
TriangleRecord() {}
Triangle triangle;
boost::shared_ptr<TriangleData> data;
};
2009-12-13 18:49:36 +01:00
private:
class Plane {
public:
Plane() : d(0) {}
Plane(const vmml::vec3f &n, float d0) : normal(n), d(d0) {}
2009-12-14 13:54:34 +01:00
Plane(const Triangle &t) : normal(t.computeNormal()), d(t.getVertex(0).dot(normal)) {}
2009-12-13 18:49:36 +01:00
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;
2009-12-14 13:54:34 +01:00
void partition(const TriangleRecord &t, std::list<TriangleRecord> *front, std::list<TriangleRecord> *back) const;
2009-12-13 18:49:36 +01:00
private:
vmml::vec3f normal;
float d;
};
public:
2009-12-14 13:54:34 +01:00
BSPTree(const std::list<TriangleRecord> &triangles);
2009-12-13 18:49:36 +01:00
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<typename T>
void visit(const T& visitor, const vmml::vec3f &p) const {
doVisit<const T>(visitor, p);
}
template<typename T>
void visit(T& visitor, const vmml::vec3f &p) const {
doVisit(visitor, p);
}
private:
Plane plane;
2009-12-14 13:54:34 +01:00
std::list<TriangleRecord> triangles;
2009-12-13 18:49:36 +01:00
BSPTree *frontTree, *backTree;
template<typename T>
void doVisit(T& visitor, const vmml::vec3f &p) const {
if(plane.isBehind(p)) {
if(frontTree)
frontTree->visit(visitor, p);
2009-12-14 13:54:34 +01:00
for(std::list<TriangleRecord>::const_iterator t = triangles.begin(); t != triangles.end(); ++t) {
2009-12-13 18:49:36 +01:00
visitor(*t);
}
if(backTree)
backTree->visit(visitor, p);
}
else {
if(backTree)
backTree->visit(visitor, p);
2009-12-14 13:54:34 +01:00
for(std::list<TriangleRecord>::const_iterator t = triangles.begin(); t != triangles.end(); ++t) {
2009-12-13 18:49:36 +01:00
visitor(*t);
}
if(frontTree)
frontTree->visit(visitor, p);
}
}
2009-12-14 13:54:34 +01:00
static vmml::vec3f findCenter(const std::list<TriangleRecord> &triangles);
static const Triangle* findNearestTriangle(const std::list<TriangleRecord> &triangles, const vmml::vec3f &v);
2009-12-13 18:49:36 +01:00
};
}
#endif /* ZOOM_BSPTREE_H_ */