worked on 2D/3D raytracing
adjusted BVH improved 2D/3D BVH new bounding volumes new test cases renamed some test-cases for grouping reasons made GPC header-only using slight adjustments
This commit is contained in:
102
geo/volume/BVH.h
102
geo/volume/BVH.h
@@ -4,29 +4,36 @@
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
#include "../Ray2.h"
|
||||
#include "../Ray3.h"
|
||||
|
||||
#include "BoundingVolume.h"
|
||||
#include "BoundingVolumeAABB.h"
|
||||
#include "BoundingVolumeSphere.h"
|
||||
|
||||
#include "BoundingVolumeAABB2.h"
|
||||
#include "BoundingVolumeCircle2.h"
|
||||
|
||||
#include "BoundingVolumeAABB3.h"
|
||||
#include "BoundingVolumeSphere3.h"
|
||||
|
||||
|
||||
|
||||
|
||||
template <typename Element, typename Volume, typename Wrapper> class BVH {
|
||||
template <typename Element, typename Ray, typename Point, typename Volume, typename Wrapper> class BVH {
|
||||
|
||||
protected:
|
||||
|
||||
/** one node within the tree */
|
||||
struct BVHNode {
|
||||
bool isLeaf = true;
|
||||
bool isLeaf;
|
||||
bool check;
|
||||
Volume boundingVolume;
|
||||
std::vector<BVHNode*> childNodes;
|
||||
BVHNode(bool isLeaf = false) : isLeaf(isLeaf) {;}
|
||||
BVHNode(bool isLeaf = false, bool check = true) : isLeaf(isLeaf), check(check) {;}
|
||||
};
|
||||
|
||||
/** one leaf within the tree */
|
||||
struct BVHLeaf : public BVHNode {
|
||||
Element element;
|
||||
BVHLeaf(const Element& e) : BVHNode(true), element(e) {;}
|
||||
BVHLeaf(const Element& e, const bool check) : BVHNode(true, check), element(e) {;}
|
||||
};
|
||||
|
||||
/** the tree's root */
|
||||
@@ -40,10 +47,10 @@ public:
|
||||
}
|
||||
|
||||
/** add a new volume to the tree */
|
||||
void add(const Element& element) {
|
||||
void add(const Element& element, const bool leafCheck = true) {
|
||||
|
||||
// create a new leaf for this element
|
||||
BVHLeaf* leaf = new BVHLeaf(element);
|
||||
BVHLeaf* leaf = new BVHLeaf(element, leafCheck);
|
||||
|
||||
// get the element's boundin volume
|
||||
leaf->boundingVolume = getBoundingVolume(element);
|
||||
@@ -63,17 +70,17 @@ public:
|
||||
return max;
|
||||
}
|
||||
|
||||
void getHits(const Ray3 ray, std::function<void(const Element&)> func) const {
|
||||
//int tests = 0; int leafs = 0;
|
||||
|
||||
void getHits(const Ray& ray, const std::function<void(const Element&)>& func) const {
|
||||
getHits(ray, &root, func);
|
||||
//std::cout << tests << " " << leafs << std::endl;
|
||||
}
|
||||
|
||||
void getHits(const Ray3 ray, const BVHNode* node, std::function<void(const Element&)> func) const {
|
||||
// this one has to be as fast as possible!
|
||||
static void getHits(const Ray& ray, const BVHNode* node, const std::function<void(const Element&)>& func) {
|
||||
for (const BVHNode* sub : node->childNodes) {
|
||||
if (sub->boundingVolume.intersects(ray)) {
|
||||
if (!sub->check || sub->boundingVolume.intersects(ray)) {
|
||||
if (sub->isLeaf) {
|
||||
BVHLeaf* leaf = (BVHLeaf*)(sub); // TODO: cast
|
||||
const BVHLeaf* leaf = static_cast<const BVHLeaf*>(sub);
|
||||
func(leaf->element);
|
||||
} else {
|
||||
getHits(ray, sub, func);
|
||||
@@ -82,8 +89,50 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
/** get the tree's depth */
|
||||
int getDepth() const {
|
||||
return getDepth(&root, 1);
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
/** call the given function for each leaf within the given subtree */
|
||||
void forEachLeaf(const BVHNode* n, std::function<void(const BVHNode*)> func) const {
|
||||
if (n->isLeaf) {
|
||||
func(n);
|
||||
} else {
|
||||
for (BVHNode* child : n->childNodes) {
|
||||
forEachLeaf(child, func);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** determine/approximate a new bounding volume around n1+n2 */
|
||||
Volume getVolAround(const BVHNode* n1, const BVHNode* n2) const {
|
||||
//return getVolAroundExact(n1, n2);
|
||||
return getVolAroundAPX(n1, n2);
|
||||
}
|
||||
|
||||
/** determine the bounding-volume around n1 and n2 by (slowly) calculating a new, exact volume based on all leaf-elements */
|
||||
Volume getVolAroundExact(const BVHNode* n1, const BVHNode* n2) const {
|
||||
std::vector<Point> verts;
|
||||
auto onLeaf = [&] (const BVHNode* n) {
|
||||
BVHLeaf* leaf = (BVHLeaf*) n;
|
||||
std::vector<Point> subVerts = Wrapper::getVertices(leaf->element);
|
||||
verts.insert(verts.end(), subVerts.begin(), subVerts.end());
|
||||
};
|
||||
forEachLeaf(n1, onLeaf);
|
||||
forEachLeaf(n2, onLeaf);
|
||||
return Volume::fromVertices(verts);
|
||||
}
|
||||
|
||||
/** approximate the bounding-volume around n1 and n2 by (quickly) joining their current volumes. the result might be unnecessarily large */
|
||||
Volume getVolAroundAPX(const BVHNode* n1, const BVHNode* n2) const {
|
||||
return Volume::join(n1->boundingVolume, n2->boundingVolume);
|
||||
}
|
||||
|
||||
|
||||
bool combineBest() {
|
||||
|
||||
// nothing to do?
|
||||
@@ -104,7 +153,7 @@ private:
|
||||
BVHNode* n1 = root.childNodes[i];
|
||||
BVHNode* n2 = root.childNodes[j];
|
||||
|
||||
const Volume newVol = Volume::join(n1->boundingVolume, n2->boundingVolume);
|
||||
const Volume newVol = getVolAround(n1,n2);
|
||||
const float newVolSize = newVol.getVolumeSize();
|
||||
if (newVolSize < best.volSize) {
|
||||
best.vol = newVol;
|
||||
@@ -226,13 +275,32 @@ private:
|
||||
|
||||
}
|
||||
|
||||
int getDepth(const BVHNode* node, const int cur) const {
|
||||
if (node->isLeaf) {
|
||||
return cur;
|
||||
} else {
|
||||
int res = cur;
|
||||
for (const BVHNode* sub : node->childNodes) {
|
||||
const int subDepth = getDepth(sub, cur+1);
|
||||
if (subDepth > res) {res = subDepth;}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/** get a bounding-volume for the given element */
|
||||
Volume getBoundingVolume(const Element& element) {
|
||||
const std::vector<Point3> verts = Wrapper::getVertices(element);
|
||||
const std::vector<Point> verts = Wrapper::getVertices(element);
|
||||
return Volume::fromVertices(verts);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template <typename Element, typename Volume, typename Wrapper> class BVH3 : public BVH<Element, Ray3, Point3, Volume, Wrapper> {
|
||||
|
||||
};
|
||||
|
||||
template <typename Element, typename Volume, typename Wrapper> class BVH2 : public BVH<Element, Ray2, Point2, Volume, Wrapper> {
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user