modified lib GPC for header only
worked on 3d traytracing
This commit is contained in:
224
geo/volume/BVH.h
224
geo/volume/BVH.h
@@ -1,17 +1,235 @@
|
||||
#ifndef BOUNDINGVOLUMEHIERARCHY_H
|
||||
#define BOUNDINGVOLUMEHIERARCHY_H
|
||||
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
#include "BoundingVolume.h"
|
||||
#include "BoundingVolumeAABB.h"
|
||||
#include "BoundingVolumeSphere.h"
|
||||
|
||||
class BVH {
|
||||
|
||||
|
||||
|
||||
template <typename Element, typename Volume, typename Wrapper> class BVH {
|
||||
|
||||
protected:
|
||||
|
||||
/** one node within the tree */
|
||||
struct BVHNode {
|
||||
bool isLeaf = true;
|
||||
Volume boundingVolume;
|
||||
std::vector<BVHNode*> childNodes;
|
||||
BVHNode(bool isLeaf = false) : isLeaf(isLeaf) {;}
|
||||
};
|
||||
|
||||
/** one leaf within the tree */
|
||||
struct BVHLeaf : public BVHNode {
|
||||
Element element;
|
||||
BVHLeaf(const Element& e) : BVHNode(true), element(e) {;}
|
||||
};
|
||||
|
||||
/** the tree's root */
|
||||
BVHNode root;
|
||||
|
||||
public:
|
||||
|
||||
/** add a new volume to the tree */
|
||||
void add(BoundingVolume* bv) {
|
||||
/** get the tree's root node */
|
||||
const BVHNode& getRoot() const {
|
||||
return root;
|
||||
}
|
||||
|
||||
/** add a new volume to the tree */
|
||||
void add(const Element& element) {
|
||||
|
||||
// create a new leaf for this element
|
||||
BVHLeaf* leaf = new BVHLeaf(element);
|
||||
|
||||
// get the element's boundin volume
|
||||
leaf->boundingVolume = getBoundingVolume(element);
|
||||
|
||||
// add the leaf to the tree
|
||||
root.childNodes.push_back(leaf);
|
||||
|
||||
}
|
||||
|
||||
/** optimize the tree */
|
||||
int optimize(const int max = 9999) {
|
||||
for (int i = 0; i < max; ++i) {
|
||||
//const bool did = concat(); // faster
|
||||
const bool did = combineBest(); // better
|
||||
if (!did) {return i;}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void getHits(const Ray3 ray, std::function<void(const Element&)> func) const {
|
||||
//int tests = 0; int leafs = 0;
|
||||
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 {
|
||||
for (const BVHNode* sub : node->childNodes) {
|
||||
if (sub->boundingVolume.intersects(ray)) {
|
||||
if (sub->isLeaf) {
|
||||
BVHLeaf* leaf = (BVHLeaf*)(sub); // TODO: cast
|
||||
func(leaf->element);
|
||||
} else {
|
||||
getHits(ray, sub, func);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
bool combineBest() {
|
||||
|
||||
// nothing to do?
|
||||
if (root.childNodes.size() < 2) {return false;}
|
||||
|
||||
struct Best {
|
||||
BVHNode* n1 = nullptr;
|
||||
BVHNode* n2 = nullptr;
|
||||
Volume vol;
|
||||
float volSize = 99999999;
|
||||
} best;
|
||||
|
||||
for (size_t i = 0; i < root.childNodes.size(); ++i) {
|
||||
for (size_t j = 0; j < root.childNodes.size(); ++j) {
|
||||
|
||||
if (i == j) {continue;}
|
||||
|
||||
BVHNode* n1 = root.childNodes[i];
|
||||
BVHNode* n2 = root.childNodes[j];
|
||||
|
||||
const Volume newVol = Volume::join(n1->boundingVolume, n2->boundingVolume);
|
||||
const float newVolSize = newVol.getVolumeSize();
|
||||
if (newVolSize < best.volSize) {
|
||||
best.vol = newVol;
|
||||
best.volSize = newVolSize;
|
||||
best.n1 = n1;
|
||||
best.n2 = n2;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
root.childNodes.erase(std::remove(root.childNodes.begin(), root.childNodes.end(), best.n1), root.childNodes.end());
|
||||
root.childNodes.erase(std::remove(root.childNodes.begin(), root.childNodes.end(), best.n2), root.childNodes.end());
|
||||
|
||||
// combine both into a new node
|
||||
BVHNode* newNode = new BVHNode();
|
||||
newNode->childNodes.push_back(best.n1);
|
||||
newNode->childNodes.push_back(best.n2);
|
||||
newNode->boundingVolume = best.vol;
|
||||
|
||||
// does the newly created node contain any other nodes?
|
||||
// THIS SHOULD NEVER BE THE CASE!
|
||||
// for (size_t i = 0; i < root.childNodes.size(); ++i) {
|
||||
// BVHNode* n3 = root.childNodes[i];
|
||||
// if (newNode->boundingVolume.contains(n3->boundingVolume)) {
|
||||
// newNode->childNodes.push_back(n3);
|
||||
// root.childNodes.erase(root.childNodes.begin()+i);
|
||||
// --i;
|
||||
// }
|
||||
// }
|
||||
|
||||
// attach the node
|
||||
root.childNodes.push_back(newNode);
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
bool concat() {
|
||||
|
||||
// nothing to do?
|
||||
if (root.childNodes.size() < 2) {return false;}
|
||||
|
||||
|
||||
bool concated = false;
|
||||
|
||||
// first, sort all elements by volume (smallest first)
|
||||
auto compVolume = [] (const BVHNode* n1, const BVHNode* n2) {
|
||||
return n1->boundingVolume.getVolumeSize() < n2->boundingVolume.getVolumeSize();
|
||||
};
|
||||
std::sort(root.childNodes.begin(), root.childNodes.end(), compVolume);
|
||||
|
||||
|
||||
// elements will be grouped into this new root
|
||||
BVHNode newRoot;
|
||||
|
||||
// combine nearby elements
|
||||
while(true) {
|
||||
|
||||
// get [and remove] the next element
|
||||
BVHNode* n0 = (BVHNode*) root.childNodes[0];
|
||||
root.childNodes.erase(root.childNodes.begin()+0);
|
||||
|
||||
// find another element that yields minimal increase in volume
|
||||
auto compNear = [n0] (const BVHNode* n1, const BVHNode* n2) {
|
||||
const float v1 = Volume::join(n0->boundingVolume, n1->boundingVolume).getVolumeSize();
|
||||
const float v2 = Volume::join(n0->boundingVolume, n2->boundingVolume).getVolumeSize();
|
||||
return v1 < v2;
|
||||
};
|
||||
auto it = std::min_element(root.childNodes.begin(), root.childNodes.end(), compNear);
|
||||
BVHNode* n1 = *it;
|
||||
|
||||
// calculate the resulting increment in volume
|
||||
const Volume joined = Volume::join(n0->boundingVolume, n1->boundingVolume);
|
||||
const float increment = joined.getVolumeSize() / n0->boundingVolume.getVolumeSize();
|
||||
const bool intersects = n0->boundingVolume.intersects(n1->boundingVolume);
|
||||
|
||||
const bool combine = true; //(intersects); //(increment < 15.0);
|
||||
|
||||
if (combine) {
|
||||
|
||||
// remove from current root
|
||||
root.childNodes.erase(it);
|
||||
|
||||
// combine both into a new node
|
||||
BVHNode* node = new BVHNode();
|
||||
node->childNodes.push_back(n0);
|
||||
node->childNodes.push_back(n1);
|
||||
node->boundingVolume = joined;
|
||||
newRoot.childNodes.push_back(node);
|
||||
|
||||
concated = true;
|
||||
|
||||
} else {
|
||||
|
||||
BVHNode* node = new BVHNode();
|
||||
node->childNodes.push_back(n0);
|
||||
node->boundingVolume = n0->boundingVolume;
|
||||
newRoot.childNodes.push_back(node);
|
||||
|
||||
}
|
||||
|
||||
// done?
|
||||
if (root.childNodes.size() == 1) {
|
||||
BVHNode* node = new BVHNode();
|
||||
node->childNodes.push_back(root.childNodes.front());
|
||||
node->boundingVolume = root.childNodes.front()->boundingVolume;
|
||||
newRoot.childNodes.push_back(node);
|
||||
break;
|
||||
} else if (root.childNodes.size() == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
root = newRoot;
|
||||
return concated;
|
||||
|
||||
}
|
||||
|
||||
/** get a bounding-volume for the given element */
|
||||
Volume getBoundingVolume(const Element& element) {
|
||||
const std::vector<Point3> verts = Wrapper::getVertices(element);
|
||||
return Volume::fromVertices(verts);
|
||||
}
|
||||
|
||||
|
||||
|
||||
106
geo/volume/BVHDebug.h
Normal file
106
geo/volume/BVHDebug.h
Normal file
@@ -0,0 +1,106 @@
|
||||
#ifndef BVHDEBUG_H
|
||||
#define BVHDEBUG_H
|
||||
|
||||
#include "BVH.h"
|
||||
#include <KLib/misc/gnuplot/GnuplotSplot.h>
|
||||
#include <KLib/misc/gnuplot/GnuplotSplotElementPoints.h>
|
||||
#include <KLib/misc/gnuplot/GnuplotSplotElementColorPoints.h>
|
||||
#include <KLib/misc/gnuplot/GnuplotSplotElementLines.h>
|
||||
#include <KLib/misc/gnuplot/Gnuplot.h>
|
||||
#include "../BBox3.h"
|
||||
#include <random>
|
||||
|
||||
/** adds some debug helpers to the BVH */
|
||||
template <typename Element, typename Volume, typename Wrapper> class BVHDebug : public BVH<Element, Volume, Wrapper> {
|
||||
|
||||
using BVHNode = typename BVH<Element, Volume, Wrapper>::BVHNode;
|
||||
using BVHLeaf = typename BVH<Element, Volume, Wrapper>::BVHLeaf;
|
||||
|
||||
public:
|
||||
|
||||
// std::vecto<std::string> colors {
|
||||
// "#888888", "#888800", "#008888", "#880088", "#ee0000", "#00ee00", "#0000ee"
|
||||
// };
|
||||
|
||||
void show(int maxPts = 1500, bool showLeafs = true) {
|
||||
|
||||
std::stringstream out;
|
||||
|
||||
static K::Gnuplot gp;
|
||||
K::GnuplotSplot plot;
|
||||
K::GnuplotSplotElementColorPoints pVol; plot.add(&pVol); //pVol.setColor(K::GnuplotColor::fromRGB(128,128,128));
|
||||
K::GnuplotSplotElementPoints pElemPoints; plot.add(&pElemPoints); pElemPoints.setColor(K::GnuplotColor::fromRGB(0,0,255));
|
||||
K::GnuplotSplotElementLines pElemLines; plot.add(&pElemLines); pElemLines.getStroke().setColor(K::GnuplotColor::fromRGB(0,0,255));
|
||||
|
||||
const int depth = recurse(maxPts, showLeafs, 0, &this->root, pVol, pElemPoints, pElemLines);
|
||||
|
||||
plot.getAxisCB().setRange(0, depth);
|
||||
|
||||
gp << "set view equal xyz\n";
|
||||
gp.draw(plot);
|
||||
gp.flush();
|
||||
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
int recurse(int maxPts, bool showLeafs, int curDepth, const BVHNode* node, K::GnuplotSplotElementColorPoints& vol, K::GnuplotSplotElementPoints& pElemPoints, K::GnuplotSplotElementLines& elemLines) {
|
||||
|
||||
int resDepth = curDepth;
|
||||
|
||||
for (BVHNode* sub : node->childNodes) {
|
||||
resDepth = recurse(maxPts, showLeafs, curDepth+1, sub, vol, pElemPoints, elemLines);
|
||||
}
|
||||
|
||||
if (!node->isLeaf || showLeafs) {
|
||||
|
||||
const int numPts = maxPts / (curDepth+1);
|
||||
|
||||
for (int i = 0; i < numPts; ++i) {
|
||||
const Point3 p = getRandomPoint(node->boundingVolume);
|
||||
vol.add(K::GnuplotPoint3(p.x, p.y, p.z), curDepth);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (node->isLeaf) {
|
||||
BVHLeaf* leaf = (BVHLeaf*) node;
|
||||
std::vector<Point3> verts = Wrapper::getVertices(leaf->element);
|
||||
for (const Point3 p : verts) {
|
||||
pElemPoints.add(K::GnuplotPoint3(p.x, p.y, p.z));
|
||||
}
|
||||
addLines(leaf->element, elemLines);
|
||||
}
|
||||
|
||||
return resDepth;
|
||||
|
||||
}
|
||||
|
||||
Point3 getRandomPoint(BoundingVolumeSphere sphere) {
|
||||
static std::minstd_rand gen;
|
||||
std::uniform_real_distribution<float> dist(-1, +1);
|
||||
Point3 dir = Point3(dist(gen), dist(gen), dist(gen)).normalized() * sphere.radius;
|
||||
return sphere.center + dir;
|
||||
}
|
||||
|
||||
void addLines(const Element& elem, K::GnuplotSplotElementLines& elemLines) {
|
||||
|
||||
std::vector<Point3> pts = Wrapper::getDebugLines(elem);
|
||||
|
||||
for (size_t i = 0; i< pts.size(); i+=2) {
|
||||
|
||||
const Point3 p1 = pts[i+0];
|
||||
const Point3 p2 = pts[i+1];
|
||||
|
||||
K::GnuplotPoint3 gp1(p1.x, p1.y, p1.z);
|
||||
K::GnuplotPoint3 gp2(p2.x, p2.y, p2.z);
|
||||
|
||||
elemLines.addSegment(gp1, gp2);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // BVHDEBUG_H
|
||||
@@ -2,16 +2,26 @@
|
||||
#define BOUNDINGVOLUME_H
|
||||
|
||||
#include "../Point3.h"
|
||||
#include "../Ray3.h"
|
||||
|
||||
class BoundingVolume {
|
||||
|
||||
public:
|
||||
|
||||
/** get the volume's size (something like m^3) */
|
||||
virtual float getVolumeSize() const = 0;
|
||||
// /** get the volume's size (something like m^3) */
|
||||
// virtual float getVolumeSize() const = 0;
|
||||
|
||||
/** does the volume contain the given point? */
|
||||
virtual bool contains(const Point3 p) const = 0;
|
||||
// /** does the volume contain the given point? */
|
||||
// virtual bool contains(const Point3 p) const = 0;
|
||||
|
||||
// /** does the volume contain the given volume? */
|
||||
// virtual bool contains(const BoundingVolume& other) const = 0;
|
||||
|
||||
// /** does the volume intersect with the given ray? */
|
||||
// virtual bool intersects(const Ray3& ray) const = 0;
|
||||
|
||||
// /** does the volume intersect with the given volume? */
|
||||
// virtual bool intersects(const BoundingVolume& other) const = 0;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ public:
|
||||
/** empty ctor */
|
||||
BoundingVolumeAABB() : p1(MAX,MAX,MAX), p2(MIN,MIN,MIN) {;}
|
||||
|
||||
float getVolumeSize() const override {
|
||||
float getVolumeSize() const {
|
||||
return (p2.x-p1.x) * (p2.y-p1.y) * (p2.z-p1.z);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,25 +1,51 @@
|
||||
#ifndef BOUNDINGVOLUMESPHERE_H
|
||||
#define BOUNDINGVOLUMESPHERE_H
|
||||
|
||||
#include "BoundingVolume.h"
|
||||
#include "../Sphere3.h"
|
||||
#include "../Point3.h"
|
||||
|
||||
class BoundingVolumeSphere : public BoundingVolume {
|
||||
|
||||
private:
|
||||
|
||||
Point3 center;
|
||||
|
||||
float radius;
|
||||
class BoundingVolumeSphere : public BoundingVolume, public Sphere3 {
|
||||
|
||||
public:
|
||||
|
||||
float getVolumeSize() const override {
|
||||
BoundingVolumeSphere() {;}
|
||||
|
||||
BoundingVolumeSphere(const Sphere3& s) : Sphere3(s) {;}
|
||||
|
||||
float getVolumeSize() const {
|
||||
return (4.0f / 3.0f) * M_PI * (radius*radius*radius);
|
||||
}
|
||||
|
||||
/** does the volume contain the given point? */
|
||||
virtual bool contains(const Point3 p) const {
|
||||
return (center-p).length() <= radius;
|
||||
bool contains(const Point3 p) const {
|
||||
return Sphere3::contains(p);
|
||||
}
|
||||
|
||||
bool intersects(const Ray3& ray) const {
|
||||
return Sphere3::intersects(ray);
|
||||
}
|
||||
|
||||
/** does the volume intersect with the given volume? */
|
||||
bool intersects(const BoundingVolume& other) const {
|
||||
const BoundingVolumeSphere& sphere = (const BoundingVolumeSphere&) other;
|
||||
return Sphere3::intersects(sphere);
|
||||
}
|
||||
|
||||
/** does the volume contain the given volume? */
|
||||
bool contains(const BoundingVolume& other) const {
|
||||
const BoundingVolumeSphere& sphere = (const BoundingVolumeSphere&) other;
|
||||
return Sphere3::contains(sphere);
|
||||
}
|
||||
|
||||
/** construct a volume around the given point-set */
|
||||
static BoundingVolumeSphere fromVertices(const std::vector<Point3>& verts) {
|
||||
BoundingVolumeSphere bvs;
|
||||
bvs.adjustToPointSet(verts);
|
||||
return bvs;
|
||||
}
|
||||
|
||||
static BoundingVolumeSphere join(const BoundingVolumeSphere a, const BoundingVolumeSphere b) {
|
||||
return BoundingVolumeSphere(Sphere3::join(a, b));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user