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:
22
geo/BBox2.h
22
geo/BBox2.h
@@ -1,8 +1,9 @@
|
||||
#ifndef BBOX2_H
|
||||
#define BBOX2_H
|
||||
#ifndef GEO_BBOX2_H
|
||||
#define GEO_BBOX2_H
|
||||
|
||||
#include "Point2.h"
|
||||
#include "Line2.h"
|
||||
#include <vector>
|
||||
|
||||
class BBox2 {
|
||||
|
||||
@@ -105,6 +106,21 @@ public:
|
||||
p2 += Point2(val, val); // increase maximum
|
||||
}
|
||||
|
||||
/** grow the bbox by the amount given for each dimension */
|
||||
void growRel(const float val) {
|
||||
const Point2 center = (p1+p2)/2;
|
||||
p1 += (p1-center)*val; // decrease minimum
|
||||
p2 += (p2-center)*val; // increase maximum
|
||||
}
|
||||
|
||||
|
||||
/** combine two bboxes */
|
||||
static BBox2 join(const BBox2& bb1, const BBox2& bb2) {
|
||||
const Point2 min( std::min(bb1.p1.x, bb2.p1.x), std::min(bb1.p1.y, bb2.p1.y) );
|
||||
const Point2 max( std::max(bb1.p2.x, bb2.p2.x), std::max(bb1.p2.y, bb2.p2.y) );
|
||||
return BBox2(min, max);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // BBOX2_H
|
||||
#endif // GEO_BBOX2_H
|
||||
|
||||
@@ -103,6 +103,13 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
/** combine two bboxes */
|
||||
static BBox3 join(const BBox3& bb1, const BBox3& bb2) {
|
||||
const Point3 min( std::min(bb1.p1.x, bb2.p1.x), std::min(bb1.p1.y, bb2.p1.y), std::min(bb1.p1.z, bb2.p1.z) );
|
||||
const Point3 max( std::max(bb1.p2.x, bb2.p2.x), std::max(bb1.p2.y, bb2.p2.y), std::max(bb1.p2.z, bb2.p2.z) );
|
||||
return BBox3(min,max);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // BBOX3_H
|
||||
|
||||
304
geo/Circle2.h
Normal file
304
geo/Circle2.h
Normal file
@@ -0,0 +1,304 @@
|
||||
#ifndef CIRCLE2_H
|
||||
#define CIRCLE2_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "Point2.h"
|
||||
#include "Ray2.h"
|
||||
|
||||
#include "../Assertions.h"
|
||||
|
||||
//#include <KLib/misc/gnuplot/Gnuplot.h>
|
||||
//#include <KLib/misc/gnuplot/GnuplotPlot.h>
|
||||
//#include <KLib/misc/gnuplot/GnuplotPlotElementLines.h>
|
||||
//#include <KLib/misc/gnuplot/GnuplotPlotElementPoints.h>
|
||||
|
||||
struct Circle2 {
|
||||
|
||||
Point2 center;
|
||||
|
||||
float radius;
|
||||
|
||||
public:
|
||||
|
||||
/** empty ctor */
|
||||
Circle2() : center(), radius(0) {;}
|
||||
|
||||
/** ctor */
|
||||
Circle2(const Point2 center, const float radius) : center(center), radius(radius) {;}
|
||||
|
||||
|
||||
/** does this circle contain the given point? */
|
||||
bool contains(const Point2 p) const {
|
||||
return center.getDistance(p) <= radius;
|
||||
}
|
||||
|
||||
/** does this circle contain the given point? */
|
||||
bool containsAll(const std::vector<Point2>& pts) const {
|
||||
for (const Point2& p : pts) {
|
||||
if (!contains(p)) {return false;}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/** get a point on the circle for the given radians */
|
||||
Point2 getPointAt(const float rad) const {
|
||||
return center + Point2(std::cos(rad), std::sin(rad)) * radius;
|
||||
}
|
||||
|
||||
|
||||
/** does this circle contain the given circle? */
|
||||
bool contains(const Circle2 c) const {
|
||||
return (center.getDistance(c.center)+c.radius) <= radius;
|
||||
}
|
||||
|
||||
/** does this circle intersect with the given ray? */
|
||||
bool intersects(const Ray2 ray) const {
|
||||
|
||||
// https://math.stackexchange.com/questions/311921/get-location-of-vector-circle-intersection
|
||||
const float a = ray.dir.x*ray.dir.x + ray.dir.y*ray.dir.y;
|
||||
const float b = 2 * ray.dir.x * (ray.start.x-center.x) + 2 * ray.dir.y * (ray.start.y - center.y);
|
||||
const float c = (ray.start.x-center.x) * (ray.start.x-center.x) + (ray.start.y - center.y)*(ray.start.y - center.y) - radius*radius;
|
||||
const float discr = b*b - 4*a*c;
|
||||
|
||||
return discr >= 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/** configure this sphere to contain the given point-set */
|
||||
void adjustToPointSet(const std::vector<Point2>& lst) {
|
||||
|
||||
//adjustToPointSetAPX(lst);
|
||||
adjustToPointSetExact(lst);
|
||||
|
||||
// validate
|
||||
for (const Point2& p : lst) {
|
||||
Assert::isTrue(this->contains(p), "calculated circle seems incorrect");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** combine two circles into a new one containing both */
|
||||
static Circle2 join(const Circle2& a, const Circle2& b) {
|
||||
|
||||
// if one already contains the other, just return it as-is
|
||||
if (a.contains(b)) {return a;}
|
||||
if (b.contains(a)) {return b;}
|
||||
|
||||
// create both maximum ends
|
||||
const Point2 out1 = a.center + (a.center-b.center).normalized() * a.radius;
|
||||
const Point2 out2 = b.center + (b.center-a.center).normalized() * b.radius;
|
||||
|
||||
// center is within both ends, so is the radius
|
||||
Point2 newCen = (out1 + out2) / 2;
|
||||
float newRad = out1.getDistance(out2) / 2 * 1.0001; // slightly larger to prevent rounding issues
|
||||
Circle2 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;
|
||||
|
||||
}
|
||||
|
||||
float getArea() const {
|
||||
return M_PI * (radius*radius);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/*
|
||||
void show(std::vector<Point2> pts, const Point2 P, const Point2 Q, const Point2 R = Point2(NAN, NAN)) {
|
||||
|
||||
static K::Gnuplot gp;
|
||||
|
||||
K::GnuplotPlot plot;
|
||||
K::GnuplotPlotElementPoints gPoints; plot.add(&gPoints); gPoints.setColor(K::GnuplotColor::fromHexStr("#0000ff")); gPoints.setPointSize(1);
|
||||
K::GnuplotPlotElementLines gLines; plot.add(&gLines);
|
||||
|
||||
for (const Point2 p : pts) {
|
||||
K::GnuplotPoint2 p2(p.x, p.y);
|
||||
gPoints.add(p2);
|
||||
}
|
||||
|
||||
K::GnuplotPoint2 gP(P.x, P.y);
|
||||
K::GnuplotPoint2 gQ(Q.x, Q.y);
|
||||
gLines.addSegment(gP, gQ);
|
||||
|
||||
K::GnuplotPoint2 gR(R.x, R.y);
|
||||
//gLines.addSegment(gP, gR);
|
||||
gLines.addSegment(gQ, gR);
|
||||
|
||||
for (float f = 0; f < M_PI*2; f += 0.1) {
|
||||
Point2 p = center + Point2(std::cos(f), std::sin(f)) * radius;
|
||||
K::GnuplotPoint2 gp (p.x, p.y);
|
||||
gLines.add(gp);
|
||||
}
|
||||
|
||||
gp << "set size ratio -1\n";
|
||||
gp.draw(plot);
|
||||
gp.flush();
|
||||
|
||||
int i = 0; (void) i;
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
// Graphic Gems 2 - Jon Rokne
|
||||
void adjustToPointSetExact(const std::vector<Point2>& _lst) {
|
||||
|
||||
if (_lst.size() == 2) {
|
||||
this->center = (_lst[0] + _lst[1]) / 2;
|
||||
this->radius = _lst[0].getDistance(_lst[1]) / 2 * 1.0001f;
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<Point2> lst = _lst;
|
||||
|
||||
// find point P having min(p.y)
|
||||
// NOTE: seems like the original work uses another coordinate system. so we search for max(p.y) instead!
|
||||
auto compMinY = [] (const Point2 p1, const Point2 p2) {return p1.y < p2.y;};
|
||||
auto it1 = std::max_element(lst.begin(), lst.end(), compMinY);
|
||||
Point2 P = *it1;
|
||||
lst.erase(it1);
|
||||
|
||||
// find a point Q such that the angle of the line segment
|
||||
// PQ with the x axis is minimal
|
||||
auto compMinAngleX = [&] (const Point2 p1, const Point2 p2) {
|
||||
|
||||
const Point2 PQ1 = p1 - P;
|
||||
const Point2 PQ2 = p2 - P;
|
||||
|
||||
const float angle1 = dot(PQ1.normalized(), Point2(0,1));
|
||||
const float angle2 = dot(PQ2.normalized(), Point2(0,1));
|
||||
|
||||
return std::acos(angle1) < std::acos(angle2);
|
||||
|
||||
};
|
||||
auto it2 = std::min_element(lst.begin(), lst.end(), compMinAngleX);
|
||||
Point2 Q = *it2;
|
||||
lst.erase(it2);
|
||||
|
||||
// get the angle abc which is the angle at "b"
|
||||
auto angle = [] (const Point2 a, const Point2 b, const Point2 c) {
|
||||
const Point2 ba = a - b;
|
||||
const Point2 bc = c - b;
|
||||
return std::acos(dot(ba.normalized(), bc.normalized()));
|
||||
};
|
||||
|
||||
// TODO: how many loops?
|
||||
for (size_t xx = 0; xx < lst.size()*10; ++xx) {
|
||||
|
||||
auto compMinAnglePRQ = [P,Q,angle] (const Point2 p1, const Point2 p2) {
|
||||
return std::abs(angle(P,p1,Q)) < std::abs(angle(P,p2,Q));
|
||||
};
|
||||
|
||||
auto it3 = std::min_element(lst.begin(), lst.end(), compMinAnglePRQ);
|
||||
|
||||
Point2 R = *it3;
|
||||
lst.erase(it3);
|
||||
|
||||
const float anglePRQ = angle(P,R,Q);
|
||||
const float angleRPQ = angle(R,P,Q);
|
||||
const float anglePQR = angle(P,Q,R);
|
||||
|
||||
//check for case 1 (angle PRQ is obtuse), the circle is determined
|
||||
//by two points, P and Q. radius = |(P-Q)/2|, center = (P+Q)/2
|
||||
if (anglePRQ > M_PI/2) {
|
||||
|
||||
this->radius = P.getDistance(Q) / 2 * 1.001f;
|
||||
this->center = (P+Q)/2;
|
||||
//if (!containsAll(_lst)) {show(_lst, P, Q, R);}
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
if (angleRPQ > M_PI/2) {
|
||||
lst.push_back(P);
|
||||
P = R;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (anglePQR > M_PI/2) {
|
||||
lst.push_back(Q);
|
||||
Q = R;
|
||||
continue;
|
||||
}
|
||||
|
||||
const Point2 mPQ = (P+Q)/2;
|
||||
const Point2 mQR = (Q+R)/2;
|
||||
|
||||
const float numer = -(-mPQ.y * R.y + mPQ.y * Q.y + mQR.y * R.y - mQR.y * Q.y - mPQ.x * R.x + mPQ.x * Q.x + mQR.x * R.x - mQR.x * Q.x);
|
||||
const float denom = (-Q.x * R.y + P.x * R.y - P.x * Q.y + Q.y * R.x - P.y * R.x + P.y * Q.x);
|
||||
const float t = numer / denom;
|
||||
|
||||
const float cx = -t * (Q.y - P.y) + mPQ.x;
|
||||
const float cy = t * (Q.x - P.x) + mPQ.y;
|
||||
this->center = Point2(cx, cy);
|
||||
this->radius = this->center.getDistance(P) * 1.001f;
|
||||
|
||||
//if (!containsAll(_lst)) {show(_lst, P, Q, R);}
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
throw Exception("should not happen");
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
void adjustToPointSetRefine(const std::vector<Point2>& lst) {
|
||||
|
||||
float bestArea = 99999999;
|
||||
|
||||
for (size_t i = 0; i < lst.size(); ++i) {
|
||||
for (size_t j = 0; j < lst.size(); ++j) {
|
||||
if (i == j) {continue;}
|
||||
|
||||
const Point2 center = (lst[i] + lst[j]) / 3;
|
||||
const float radius = lst[i].getDistance(lst[j]);
|
||||
const Circle2 test(center, radius);
|
||||
|
||||
if (test.containsAll(lst)) {
|
||||
if (test.getArea() < bestArea) {
|
||||
bestArea = test.getArea();
|
||||
this->radius = test.radius;
|
||||
this->center = test.center;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
void adjustToPointSetAPX(const std::vector<Point2>& lst) {
|
||||
|
||||
// NOT OPTIMAL but fast
|
||||
|
||||
// calculate the point set's center
|
||||
Point2 sum(0,0);
|
||||
for (const Point2 p : lst) {sum += p;}
|
||||
const Point2 center = sum / lst.size();
|
||||
|
||||
// calculate the sphere's radius (maximum distance from center
|
||||
float radius = 0;
|
||||
for (const Point2 p : lst) {
|
||||
const float dist = center.getDistance(p);
|
||||
if (dist > radius) {radius = dist;}
|
||||
}
|
||||
|
||||
this->radius = radius;
|
||||
this->center = center;
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // CIRCLE2_H
|
||||
@@ -68,6 +68,11 @@ struct Point2 {
|
||||
|
||||
};
|
||||
|
||||
inline float dot(const Point2 p1, const Point2 p2) {
|
||||
return (p1.x*p2.x) + (p1.y*p2.y);
|
||||
}
|
||||
|
||||
|
||||
inline void swap(Point2& p1, Point2& p2) {
|
||||
std::swap(p1.x, p2.x);
|
||||
std::swap(p1.y, p2.y);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef POINT3_H
|
||||
#define POINT3_H
|
||||
#ifndef GEO_POINT3_H
|
||||
#define GEO_POINT3_H
|
||||
|
||||
#include "../Assertions.h"
|
||||
#include <cmath>
|
||||
@@ -128,4 +128,4 @@ inline Point3 cross(const Point3 a, const Point3 b) {
|
||||
);
|
||||
}
|
||||
|
||||
#endif // POINT3_H
|
||||
#endif // GEO_POINT3_H
|
||||
|
||||
29
geo/Ray2.h
Normal file
29
geo/Ray2.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef GEO_RAY2_H
|
||||
#define GEO_RAY2_H
|
||||
|
||||
|
||||
#include "Point2.h"
|
||||
|
||||
struct Ray2 {
|
||||
|
||||
/** starting point */
|
||||
Point2 start;
|
||||
|
||||
/** ray's direction */
|
||||
Point2 dir;
|
||||
|
||||
public:
|
||||
|
||||
/** empty */
|
||||
Ray2() : start(), dir() {
|
||||
;
|
||||
}
|
||||
|
||||
/** ctor */
|
||||
Ray2(Point2 start, Point2 dir) : start(start), dir(dir) {
|
||||
;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // GEO_RAY2_H
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef RAY3_H
|
||||
#define RAY3_H
|
||||
#ifndef GEO_RAY3_H
|
||||
#define GEO_RAY3_H
|
||||
|
||||
#include "Point3.h"
|
||||
|
||||
@@ -25,4 +25,4 @@ public:
|
||||
|
||||
};
|
||||
|
||||
#endif // RAY3_H
|
||||
#endif // GEO_RAY3_H
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef SPHERE3_H
|
||||
#define SPHERE3_H
|
||||
#ifndef GEO_SPHERE3_H
|
||||
#define GEO_SPHERE3_H
|
||||
|
||||
#include "Point3.h"
|
||||
#include "Ray3.h"
|
||||
@@ -172,7 +172,7 @@ public:
|
||||
|
||||
// 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
|
||||
float newRad = out1.getDistance(out2) / 2 * 1.0001; // slightly larger to prevent rounding issues
|
||||
Sphere3 res(newCen, newRad);
|
||||
|
||||
// check
|
||||
@@ -185,4 +185,4 @@ public:
|
||||
|
||||
};
|
||||
|
||||
#endif // SPHERE3_H
|
||||
#endif // GEO_SPHERE3_H
|
||||
|
||||
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> {
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -2,39 +2,52 @@
|
||||
#define BVHDEBUG_H
|
||||
|
||||
#include "BVH.h"
|
||||
|
||||
|
||||
#include <KLib/misc/gnuplot/Gnuplot.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 <KLib/misc/gnuplot/GnuplotPlot.h>
|
||||
#include <KLib/misc/gnuplot/GnuplotPlotElementPoints.h>
|
||||
#include <KLib/misc/gnuplot/GnuplotPlotElementColorLines.h>
|
||||
#include <KLib/misc/gnuplot/GnuplotPlotElementLines.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> {
|
||||
///** adds some debug helpers to the BVH */
|
||||
//template <typename Element, typename Ray, typename Point, typename Volume, typename Wrapper> class BVHDebug : public BVH<Element, Ray, Point, Volume, Wrapper> {
|
||||
|
||||
|
||||
//};
|
||||
|
||||
template <typename Element, typename Volume, typename Wrapper> class BVH3Debug : public BVH<Element, Ray3, Point3, Volume, Wrapper> {
|
||||
|
||||
using BVHNode = typename BVH<Element, Ray3, Point3, Volume, Wrapper>::BVHNode;
|
||||
using BVHLeaf = typename BVH<Element, Ray3, Point3, Volume, Wrapper>::BVHLeaf;
|
||||
|
||||
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);
|
||||
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));
|
||||
|
||||
plot.getAxisCB().setRange(0, depth);
|
||||
const int maxDepth = this->getDepth();
|
||||
recurse(maxPts, showLeafs, 0, &this->root, pVol, pElemPoints, pElemLines);
|
||||
|
||||
plot.getAxisCB().setRange(0, maxDepth);
|
||||
|
||||
gp << "set view equal xyz\n";
|
||||
gp.draw(plot);
|
||||
@@ -44,6 +57,31 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
Point3 getRandomPoint(BoundingVolumeSphere3 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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int recurse(int maxPts, bool showLeafs, int curDepth, const BVHNode* node, K::GnuplotSplotElementColorPoints& vol, K::GnuplotSplotElementPoints& pElemPoints, K::GnuplotSplotElementLines& elemLines) {
|
||||
|
||||
int resDepth = curDepth;
|
||||
@@ -76,24 +114,53 @@ private:
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
template <typename Element, typename Volume, typename Wrapper> class BVH2Debug : public BVH<Element, Ray2, Point2, Volume, Wrapper> {
|
||||
|
||||
using BVHNode = typename BVH<Element, Ray2, Point2, Volume, Wrapper>::BVHNode;
|
||||
using BVHLeaf = typename BVH<Element, Ray2, Point2, Volume, Wrapper>::BVHLeaf;
|
||||
|
||||
public:
|
||||
|
||||
void show(int maxPts = 1500, bool showLeafs = true) {
|
||||
|
||||
std::stringstream out;
|
||||
|
||||
static K::Gnuplot gp;
|
||||
K::GnuplotPlot plot;
|
||||
|
||||
|
||||
K::GnuplotPlotElementColorLines pVolLines; plot.add(&pVolLines);
|
||||
K::GnuplotPlotElementPoints pElemPoints; plot.add(&pElemPoints); pElemPoints.setColor(K::GnuplotColor::fromRGB(0,0,255));
|
||||
K::GnuplotPlotElementLines pElemLines; plot.add(&pElemLines); pElemLines.getStroke().setColor(K::GnuplotColor::fromRGB(0,0,255));
|
||||
|
||||
const int maxDepth = this->getDepth();
|
||||
recurse(maxDepth, showLeafs, 0, &this->root, plot, pVolLines, pElemPoints, pElemLines);
|
||||
|
||||
plot.getObjects().reOrderByZIndex();
|
||||
plot.getAxisCB().setRange(0, maxDepth);
|
||||
|
||||
gp << "set size ratio -1\n";
|
||||
gp.draw(plot);
|
||||
gp.flush();
|
||||
|
||||
}
|
||||
|
||||
void addLines(const Element& elem, K::GnuplotSplotElementLines& elemLines) {
|
||||
private:
|
||||
|
||||
std::vector<Point3> pts = Wrapper::getDebugLines(elem);
|
||||
|
||||
void addLines(const Element& elem, K::GnuplotPlotElementLines& elemLines) {
|
||||
|
||||
std::vector<Point2> 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];
|
||||
const Point2 p1 = pts[i+0];
|
||||
const Point2 p2 = pts[i+1];
|
||||
|
||||
K::GnuplotPoint3 gp1(p1.x, p1.y, p1.z);
|
||||
K::GnuplotPoint3 gp2(p2.x, p2.y, p2.z);
|
||||
K::GnuplotPoint2 gp1(p1.x, p1.y);
|
||||
K::GnuplotPoint2 gp2(p2.x, p2.y);
|
||||
|
||||
elemLines.addSegment(gp1, gp2);
|
||||
|
||||
@@ -101,6 +168,75 @@ private:
|
||||
|
||||
}
|
||||
|
||||
std::vector<std::string> colors = {
|
||||
"#888800", "#444400", "#008888", "#004444", "#880088", "#440044", "#ee0000", "#880000", "#00ee00", "#008800", "#0000ee", "#000088",
|
||||
"#888800", "#444400", "#008888", "#004444", "#880088", "#440044", "#ee0000", "#880000", "#00ee00", "#008800", "#0000ee", "#000088",
|
||||
"#888800", "#444400", "#008888", "#004444", "#880088", "#440044", "#ee0000", "#880000", "#00ee00", "#008800", "#0000ee", "#000088"
|
||||
};
|
||||
|
||||
void showVolume(const BoundingVolumeCircle2& circle, int maxDepth, int curDepth, K::GnuplotPlot& plot, K::GnuplotPlotElementColorLines& pVolLines) {
|
||||
K::GnuplotObjectPolygon* poly = new K::GnuplotObjectPolygon();
|
||||
for (int i = 0; i < 20; ++i) {
|
||||
const float f = M_PI*2 * i / 19;
|
||||
const Point2 p = circle.getPointAt(f);
|
||||
poly->add(K::GnuplotCoordinate2(p.x, p.y, K::GnuplotCoordinateSystem::FIRST));
|
||||
poly->getFill().setColor(K::GnuplotColor::fromHexStr(colors[maxDepth-curDepth]));
|
||||
poly->getFill().setStyle(K::GnuplotFillStyle::SOLID);
|
||||
poly->setZIndex(curDepth);
|
||||
}
|
||||
plot.getObjects().add(poly);
|
||||
}
|
||||
|
||||
void showVolume(const BoundingVolumeAABB2& _aabb, int maxDepth, int curDepth, K::GnuplotPlot& plot, K::GnuplotPlotElementColorLines& pVolLines) {
|
||||
BBox2 bbox2 = _aabb;
|
||||
bbox2.grow( (10-curDepth) / 100.0f );
|
||||
// pVolLines.add(K::GnuplotPoint2(bbox2.getMin().x, bbox2.getMin().y), curDepth);
|
||||
// pVolLines.add(K::GnuplotPoint2(bbox2.getMax().x, bbox2.getMin().y), curDepth);
|
||||
// pVolLines.add(K::GnuplotPoint2(bbox2.getMax().x, bbox2.getMax().y), curDepth);
|
||||
// pVolLines.add(K::GnuplotPoint2(bbox2.getMin().x, bbox2.getMax().y), curDepth);
|
||||
// pVolLines.add(K::GnuplotPoint2(bbox2.getMin().x, bbox2.getMin().y), curDepth);
|
||||
// pVolLines.splitFace();
|
||||
K::GnuplotObjectPolygon* poly = new K::GnuplotObjectPolygon();
|
||||
poly->getStroke().setColor(K::GnuplotColor::fromHexStr(colors[maxDepth-curDepth]));
|
||||
//poly->getFill().setColor(K::GnuplotColor::fromHexStr(colors[maxDepth-curDepth]));
|
||||
//poly->getFill().setStyle(K::GnuplotFillStyle::SOLID);
|
||||
poly->add(K::GnuplotCoordinate2(bbox2.getMin().x, bbox2.getMin().y, K::GnuplotCoordinateSystem::FIRST));
|
||||
poly->add(K::GnuplotCoordinate2(bbox2.getMax().x, bbox2.getMin().y, K::GnuplotCoordinateSystem::FIRST));
|
||||
poly->add(K::GnuplotCoordinate2(bbox2.getMax().x, bbox2.getMax().y, K::GnuplotCoordinateSystem::FIRST));
|
||||
poly->add(K::GnuplotCoordinate2(bbox2.getMin().x, bbox2.getMax().y, K::GnuplotCoordinateSystem::FIRST));
|
||||
poly->close();
|
||||
poly->setZIndex(curDepth);
|
||||
plot.getObjects().add(poly);
|
||||
}
|
||||
|
||||
int recurse(int maxDepth, bool showLeafs, int curDepth, const BVHNode* node, K::GnuplotPlot& plot, K::GnuplotPlotElementColorLines& pVolLines, K::GnuplotPlotElementPoints& pElemPoints, K::GnuplotPlotElementLines& elemLines) {
|
||||
|
||||
int resDepth = curDepth;
|
||||
|
||||
for (BVHNode* sub : node->childNodes) {
|
||||
resDepth = recurse(maxDepth, showLeafs, curDepth+1, sub, plot, pVolLines, pElemPoints, elemLines);
|
||||
}
|
||||
|
||||
if (!node->isLeaf || showLeafs) {
|
||||
if (node != &this->root) {
|
||||
//const int numPts = maxPts / (curDepth+1);
|
||||
showVolume(node->boundingVolume, maxDepth, curDepth, plot, pVolLines);
|
||||
}
|
||||
}
|
||||
|
||||
if (node->isLeaf) {
|
||||
BVHLeaf* leaf = (BVHLeaf*) node;
|
||||
std::vector<Point2> verts = Wrapper::getVertices(leaf->element);
|
||||
for (const Point2 p : verts) {
|
||||
pElemPoints.add(K::GnuplotPoint2(p.x, p.y));
|
||||
}
|
||||
addLines(leaf->element, elemLines);
|
||||
}
|
||||
|
||||
return resDepth;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // BVHDEBUG_H
|
||||
|
||||
36
geo/volume/BoundingVolumeAABB2.h
Normal file
36
geo/volume/BoundingVolumeAABB2.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef BOUNDINGVOLUMEAABB2_H
|
||||
#define BOUNDINGVOLUMEAABB2_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "BoundingVolume.h"
|
||||
#include "../BBox2.h"
|
||||
|
||||
class BoundingVolumeAABB2 : public BBox2 {
|
||||
|
||||
public:
|
||||
|
||||
BoundingVolumeAABB2() {;}
|
||||
|
||||
BoundingVolumeAABB2(const BBox2& bb) : BBox2(bb) {;}
|
||||
|
||||
float getVolumeSize() const {
|
||||
const float dx = getMax().x - getMin().x;
|
||||
const float dy = getMax().y - getMin().y;
|
||||
return (dx*dy);
|
||||
}
|
||||
|
||||
/** construct a volume around the given point-set */
|
||||
static BoundingVolumeAABB2 fromVertices(const std::vector<Point2>& verts) {
|
||||
BoundingVolumeAABB2 bvs;
|
||||
for (const Point2 p : verts) {bvs.add(p);}
|
||||
return bvs;
|
||||
}
|
||||
|
||||
static BoundingVolumeAABB2 join(const BoundingVolumeAABB2 a, const BoundingVolumeAABB2 b) {
|
||||
return BoundingVolumeAABB2(BBox2::join(a, b));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // BOUNDINGVOLUMEAABB2_H
|
||||
@@ -1,4 +0,0 @@
|
||||
#ifndef BOUNDINGVOLUMEBOX_H
|
||||
#define BOUNDINGVOLUMEBOX_H
|
||||
|
||||
#endif // BOUNDINGVOLUMEBOX_H
|
||||
38
geo/volume/BoundingVolumeCircle2.h
Normal file
38
geo/volume/BoundingVolumeCircle2.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef BOUDINGVOLUMECIRCLE2_H
|
||||
#define BOUDINGVOLUMECIRCLE2_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "BoundingVolume.h"
|
||||
#include "../Circle2.h"
|
||||
|
||||
class BoundingVolumeCircle2 : public Circle2 {
|
||||
|
||||
public:
|
||||
|
||||
BoundingVolumeCircle2() {;}
|
||||
|
||||
BoundingVolumeCircle2(const Circle2& c) : Circle2(c) {;}
|
||||
|
||||
float getVolumeSize() const {
|
||||
return M_PI * (radius*radius);
|
||||
}
|
||||
|
||||
bool intersects(const Ray2 ray) const {
|
||||
return Circle2::intersects(ray);
|
||||
}
|
||||
|
||||
/** construct a volume around the given point-set */
|
||||
static BoundingVolumeCircle2 fromVertices(const std::vector<Point2>& verts) {
|
||||
BoundingVolumeCircle2 bvs;
|
||||
bvs.adjustToPointSet(verts);
|
||||
return bvs;
|
||||
}
|
||||
|
||||
static BoundingVolumeCircle2 join(const BoundingVolumeCircle2 a, const BoundingVolumeCircle2 b) {
|
||||
return BoundingVolumeCircle2(Circle2::join(a, b));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // BOUDINGVOLUMECIRCLE2_H
|
||||
@@ -1,4 +0,0 @@
|
||||
#ifndef BOUNDINGVOLUMEHIERARCHY_H
|
||||
#define BOUNDINGVOLUMEHIERARCHY_H
|
||||
|
||||
#endif // BOUNDINGVOLUMEHIERARCHY_H
|
||||
@@ -5,13 +5,13 @@
|
||||
#include "../Sphere3.h"
|
||||
#include "../Point3.h"
|
||||
|
||||
class BoundingVolumeSphere : public BoundingVolume, public Sphere3 {
|
||||
class BoundingVolumeSphere3 : public BoundingVolume, public Sphere3 {
|
||||
|
||||
public:
|
||||
|
||||
BoundingVolumeSphere() {;}
|
||||
BoundingVolumeSphere3() {;}
|
||||
|
||||
BoundingVolumeSphere(const Sphere3& s) : Sphere3(s) {;}
|
||||
BoundingVolumeSphere3(const Sphere3& s) : Sphere3(s) {;}
|
||||
|
||||
float getVolumeSize() const {
|
||||
return (4.0f / 3.0f) * M_PI * (radius*radius*radius);
|
||||
@@ -27,25 +27,25 @@ public:
|
||||
|
||||
/** does the volume intersect with the given volume? */
|
||||
bool intersects(const BoundingVolume& other) const {
|
||||
const BoundingVolumeSphere& sphere = (const BoundingVolumeSphere&) other;
|
||||
const BoundingVolumeSphere3& sphere = (const BoundingVolumeSphere3&) other;
|
||||
return Sphere3::intersects(sphere);
|
||||
}
|
||||
|
||||
/** does the volume contain the given volume? */
|
||||
bool contains(const BoundingVolume& other) const {
|
||||
const BoundingVolumeSphere& sphere = (const BoundingVolumeSphere&) other;
|
||||
const BoundingVolumeSphere3& sphere = (const BoundingVolumeSphere3&) other;
|
||||
return Sphere3::contains(sphere);
|
||||
}
|
||||
|
||||
/** construct a volume around the given point-set */
|
||||
static BoundingVolumeSphere fromVertices(const std::vector<Point3>& verts) {
|
||||
BoundingVolumeSphere bvs;
|
||||
static BoundingVolumeSphere3 fromVertices(const std::vector<Point3>& verts) {
|
||||
BoundingVolumeSphere3 bvs;
|
||||
bvs.adjustToPointSet(verts);
|
||||
return bvs;
|
||||
}
|
||||
|
||||
static BoundingVolumeSphere join(const BoundingVolumeSphere a, const BoundingVolumeSphere b) {
|
||||
return BoundingVolumeSphere(Sphere3::join(a, b));
|
||||
static BoundingVolumeSphere3 join(const BoundingVolumeSphere3 a, const BoundingVolumeSphere3 b) {
|
||||
return BoundingVolumeSphere3(Sphere3::join(a, b));
|
||||
}
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user