modified lib GPC for header only

worked on 3d traytracing
This commit is contained in:
k-a-z-u
2017-09-06 17:04:19 +02:00
parent 845d89774d
commit c19d18a3a6
20 changed files with 884 additions and 299 deletions

View File

@@ -18,11 +18,16 @@ private:
public:
/** empty ctor */
/** empty ctor */
BBox3() : p1(MAX,MAX,MAX), p2(MIN,MIN,MIN) {;}
/** ctor with min and max */
BBox3(const Point3 min, const Point3 max) : p1(min), p2(max) {;}
/** ctor with min and max */
BBox3(const Point3 min, const Point3 max) : p1(min), p2(max) {;}
/** create a bbox around the given point */
static BBox3 around(const Point3 center, const Point3 size) {
return BBox3(center-size/2, center+size/2);
}
/** adjust the bounding-box by adding this point */
void add(const Point3& p) {

View File

@@ -116,11 +116,11 @@ private:
};
inline float dot(const Point3& p1, const Point3& p2) {
inline float dot(const Point3 p1, const Point3 p2) {
return (p1.x*p2.x) + (p1.y*p2.y) + (p1.z*p2.z);
}
inline Point3 cross(const Point3& a, const Point3& b) {
inline Point3 cross(const Point3 a, const Point3 b) {
return Point3(
a.y*b.z - a.z*b.y,
a.z*b.x - a.x*b.z,

View File

@@ -19,38 +19,11 @@ struct Sphere3 {
/** ctor */
Sphere3(const Point3 center, const float radius) : center(center), radius(radius) {;}
/** create a sphere that fully contains the given point-set */
static Sphere3 around(const std::vector<Point3>& lst) {
// NOT OPTIMAL but fast
Point3 sum(0,0,0);
for (const Point3 p : lst) {sum += p;}
const Point3 center = sum / lst.size();
float radius = 0;
for (const Point3 p : lst) {
const float dist = center.getDistance(p);
if (dist > radius) {radius = dist;}
}
return Sphere3(center, radius);
}
static Sphere3 join(const Sphere3& a, const Sphere3& b) {
if (a.contains(b)) {return a;}
if (b.contains(a)) {return b;}
Point3 newCen = (a.center + b.center) / 2;
float newRad = (a.center.getDistance(b.center) + std::max(a.radius, b.radius) * 2) / 2;
return Sphere3(newCen, newRad);
}
/** does this sphere contain the given sphere? */
bool contains(const Sphere3& o) const {
return (o.center.getDistance(this->center) + o.radius) < this->radius;
return (o.center.getDistance(this->center) + o.radius) <= this->radius;
}
/** does the sphere contain the given point? */
@@ -58,13 +31,21 @@ struct Sphere3 {
return center.getDistance(p) <= radius;
}
/** does the sphere intersect with the given sphere? */
bool intersects(const Sphere3& other) const {
return center.getDistance(other.center) < (radius + other.radius);
}
/** does the sphere intersect with the given ray? */
bool intersects(const Ray3& ray) const {
if (contains(ray.start)) {return true;}
// if the sphere contains the ray's start -> done
//if (contains(ray.start)) {return true;}
/*
// https://stackoverflow.com/questions/6533856/ray-sphere-intersection
/*
const float xA = ray.start.x;
const float yA = ray.start.y;
const float zA = ray.start.z;
@@ -85,42 +66,121 @@ struct Sphere3 {
// intersection?
return delta >= 0;
*/
*/
// http://www.ccs.neu.edu/home/fell/CSU540/programs/RayTracingFormulas.htm
const double x0 = ray.start.x;
const double y0 = ray.start.y;
const double z0 = ray.start.z;
const double cx = center.x;
const double cy = center.y;
const double cz = center.z;
const double dx = ray.dir.x;
const double dy = ray.dir.y;
const double dz = ray.dir.z;
const double a = dx*dx + dy*dy + dz*dz;
const double b = 2*dx*(x0-cx) + 2*dy*(y0-cy) + 2*dz*(z0-cz);
const double c = cx*cx + cy*cy + cz*cz + x0*x0 + y0*y0 + z0*z0 + -2*(cx*x0 + cy*y0 + cz*z0) - radius*radius;
const double discriminant = (b*b) - (4*a*c);
return discriminant >= 0;
/*
// http://www.pixelnerve.com/v/2009/02/08/ray-sphere-intersection/
const float a = ray.dir.length();
//if (a == 0.0) return 0;
const float b = 2.0f * ( dot(ray.start, ray.dir) - dot(ray.dir, center)) ;
const Point3 tempDiff = center - ray.start;
const float c = tempDiff.length() - (radius*radius);
const float disc = b * b - 4 * a * c;
return disc >= 0.0f;
const float x0 = ray.start.x;
const float y0 = ray.start.y;
const float z0 = ray.start.z;
const float cx = center.x;
const float cy = center.y;
const float cz = center.z;
const float dx = ray.dir.x;
const float dy = ray.dir.y;
const float dz = ray.dir.z;
const float a = dx*dx + dy*dy + dz*dz;
const float b = 2*dx*(x0-cx) + 2*dy*(y0-cy) + 2*dz*(z0-cz);
const float c = cx*cx + cy*cy + cz*cz + x0*x0 + y0*y0 + z0*z0 + -2*(cx*x0 + cy*y0 + cz*z0) - radius*radius;
const float discriminant = (b*b) - (4*a*c);
return discriminant >= 0;
*/
//https://gamedev.stackexchange.com/questions/96459/fast-ray-sphere-collision-code
const Point3 m = ray.start - center;
const float b = dot(m, ray.dir);
const float c = dot(m, m) - radius*radius;
// Exit if rs origin outside s (c > 0) and r pointing away from s (b > 0)
if (c > 0.0f && b > 0.0f) {return false;}
const float discr = b*b - c;
// A negative discriminant corresponds to ray missing sphere
if (discr < 0.0f) {return false;}
return true;
// // Ray now found to intersect sphere, compute smallest t value of intersection
// t = -b - Sqrt(discr);
// // If t is negative, ray started inside sphere so clamp t to zero
// if (t < 0.0f) t = 0.0f;
// q = p + t * d;
// return 1;
}
/** configure this sphere to contain the given point-set */
void adjustToPointSet(const std::vector<Point3>& lst) {
// NOT OPTIMAL but fast
// calculate the point set's center
Point3 sum(0,0,0);
for (const Point3 p : lst) {sum += p;}
const Point3 center = sum / lst.size();
// calculate the sphere's radius (maximum distance from center
float radius = 0;
for (const Point3 p : lst) {
const float dist = center.getDistance(p);
if (dist > radius) {radius = dist;}
}
this->radius = radius;
this->center = center;
}
public:
/** create a sphere that fully contains the given point-set */
static Sphere3 around(const std::vector<Point3>& lst) {
Sphere3 sphere;
sphere.adjustToPointSet(lst);
return sphere;
}
/** combine two spheres into a new one containing both */
static Sphere3 join(const Sphere3& a, const Sphere3& b) {
// if one already contains the other, just return it as-is
if (a.contains(b)) {return a;}
if (b.contains(a)) {return b;}
// calculate the new center and radius
// Point3 newCen = (a.center + b.center) / 2;
// float newRad = (a.center.getDistance(b.center) + std::max(a.radius, b.radius) * 2) / 2;
// return Sphere3(newCen, newRad);
// Point3 newCen = (a.center*a.radius + b.center*b.radius) / (a.radius+b.radius);
// float newRad = (a.center.getDistance(b.center) + a.radius + b.radius) / 2 * 1.02f; // slightly larger to prevent rounding issues
// Sphere3 res(newCen, newRad);
// create both maximum ends
const Point3 out1 = a.center + (a.center-b.center).normalized() * a.radius;
const Point3 out2 = b.center + (b.center-a.center).normalized() * b.radius;
// center is within both ends, so is the radius
Point3 newCen = (out1 + out2) / 2;
float newRad = out1.getDistance(out2) / 2 * 1.001; // slightly larger to prevent rounding issues
Sphere3 res(newCen, newRad);
// check
Assert::isTrue(res.contains(a), "sphere joining error. base-spheres are not contained.");
Assert::isTrue(res.contains(b), "sphere joining error. base-spheres are not contained.");
return res;
}
};

View File

View File

@@ -41,7 +41,7 @@ public:
// http://www.lighthouse3d.com/tutorials/maths/ray-triangle-intersection/
bool intersects(Ray3 ray, Point3& pos) const {
bool intersects(const Ray3& ray, Point3& pos) const {
const Point3 e1 = p2-p1;
const Point3 e2 = p3-p1;

View File

@@ -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
View 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

View File

@@ -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;
};

View File

@@ -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);
}

View File

@@ -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));
}
};