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

@@ -5,6 +5,7 @@
#include "../../../geo/BBox3.h"
#include <vector>
#include <functional>
#include <mutex>
template <typename T> class DataMap3 {
@@ -189,6 +190,8 @@ private:
struct DataMap3SignalEntry {
struct Entry {
float rssi;
float distanceToAP;
@@ -198,8 +201,11 @@ struct DataMap3SignalEntry {
std::vector<Entry> entries;
void add(const float rssi, const float distanceToAP) {
static std::mutex mtx;
Entry e(rssi, distanceToAP);
mtx.lock();
entries.push_back(e);
mtx.unlock();
}
float getMaxRSSI() const {

View File

@@ -17,6 +17,7 @@ private:
bool exportCeilings = true;
bool exportObstacles = true;
bool exportWallTops = false;
std::vector<Floorplan::Floor*> exportFloors;
const Floorplan::IndoorMap* map;
@@ -28,14 +29,25 @@ public:
}
void setExportCeilings(bool exp) {
this->exportCeilings = exp;
}
/** limit to-be-exported floors */
void setFloors(const std::vector<Floorplan::Floor*> floors) {
this->exportFloors = floors;
}
/** get all triangles grouped by obstacle */
std::vector<Obstacle3D> triangulize() {
std::vector<Obstacle3D> res;
// get the to-be-exported floors (either "all" or "user defined")
const std::vector<Floorplan::Floor*>& floors = (exportFloors.empty()) ? (map->floors) : (exportFloors);
// process each floor
for (const Floorplan::Floor* f : map->floors) {
for (const Floorplan::Floor* f : floors) {
// triangulize the floor itself (floor/ceiling)
if (exportCeilings) {res.push_back(getTriangles(f));}

View File

@@ -1,139 +0,0 @@
#ifndef OBSTACLETREE_H
#define OBSTACLETREE_H
#include "../../../geo/Sphere3.h"
#include "Obstacle3.h"
#include <algorithm>
struct ObstacleNode {
bool isLeaf = true;
Sphere3 boundSphere;
std::vector<ObstacleNode*> sub;
ObstacleNode(bool isLeaf = false) : isLeaf(isLeaf) {;}
};
struct ObstacleLeaf : public ObstacleNode {
Obstacle3D obs;
ObstacleLeaf() : ObstacleNode(true) {;}
};
class ObstacleTree {
ObstacleNode root;
public:
/** append a new leaf */
void add(ObstacleLeaf* leaf) {
root.sub.push_back(leaf);
}
void optimize() {
while(true) {
bool did = concat();
if (!did) {break;}
}
}
std::vector<Obstacle3D*> getHits(const Ray3 ray) const {
std::vector<Obstacle3D*> obs;
getHits(ray, &root, obs);
return obs;
}
void getHits(const Ray3 ray, const ObstacleNode* node, std::vector<Obstacle3D*>& hits) const {
for (const ObstacleNode* sub : node->sub) {
if (sub->boundSphere.intersects(ray)) {
if (sub->isLeaf) {
ObstacleLeaf* leaf = (ObstacleLeaf*)(sub);
hits.push_back(&leaf->obs);
} else {
if (sub->boundSphere.intersects(ray)) {getHits(ray, sub, hits);}
}
}
}
}
bool concat() {
bool concated = false;
// first, sort all elements by radius (smallest first)
auto compRadius = [] (const ObstacleNode* l1, const ObstacleNode* l2) {
return l1->boundSphere.radius < l2->boundSphere.radius;
};
std::sort(root.sub.begin(), root.sub.end(), compRadius);
ObstacleNode newRoot;
// combine nearby elements
//for (size_t i = 0; i < root.sub.size(); ++i) {
while(true) {
// get [and remove] the next element
ObstacleLeaf* l0 = (ObstacleLeaf*) root.sub[0];
root.sub.erase(root.sub.begin()+0);
// find another element that yields minimal increase in volume
auto compNear = [l0] (const ObstacleNode* l1, const ObstacleNode* l2) {
const float d1 = Sphere3::join(l0->boundSphere, l1->boundSphere).radius;
const float d2 = Sphere3::join(l0->boundSphere, l2->boundSphere).radius;
return d1 < d2;
};
auto it = std::min_element(root.sub.begin(), root.sub.end(), compNear);
ObstacleNode* l1 = *it;
float increment = Sphere3::join(l0->boundSphere, l1->boundSphere).radius / l0->boundSphere.radius;
const bool combine = (root.sub.size() > 1) && (it != root.sub.end()) && (increment < 1.75);
if (combine) {
// combine both into a new node
ObstacleNode* node = new ObstacleNode();
node->sub.push_back(l0);
node->sub.push_back(*it);
node->boundSphere = Sphere3::join(l0->boundSphere, (*it)->boundSphere);
root.sub.erase(it);
newRoot.sub.push_back(node);
concated = true;
} else {
ObstacleNode* node = new ObstacleNode();
node->sub.push_back(l0);
node->boundSphere = l0->boundSphere;
newRoot.sub.push_back(node);
}
// done?
if (root.sub.size() == 1) {
ObstacleNode* node = new ObstacleNode();
node->sub.push_back(root.sub.front());
node->boundSphere = root.sub.front()->boundSphere;
newRoot.sub.push_back(node);
break;
} else if (root.sub.size() == 0) {
break;
}
//--i;
}
root = newRoot;
return concated;
}
};
#endif // OBSTACLETREE_H

View File

@@ -5,6 +5,9 @@
#include "../../../geo/Line2.h"
#include "../../../geo/BBox2.h"
#include "../../../geo/volume/BVH.h"
#include "../../../geo/volume/BVHDebug.h"
#include "../../../floorplan/v2/Floorplan.h"
#include "../../../floorplan/v2/FloorplanHelper.h"
@@ -13,7 +16,9 @@
#include "MaterialOptions.h"
#include "Obstacle3.h"
#include "ModelFactory.h"
#include "ObstacleTree.h"
//#include "ObstacleTree.h"
#include <random>
@@ -133,8 +138,29 @@ struct Hit3 {
struct Obstacle3DWrapper {
static std::vector<Point3> getVertices(const Obstacle3D& obs) {
std::vector<Point3> pts;
for (const Triangle3& tria : obs.triangles) {
pts.push_back(tria.p1);
pts.push_back(tria.p2);
pts.push_back(tria.p3);
}
return pts;
}
static std::vector<Point3> getDebugLines(const Obstacle3D& obs) {
std::vector<Point3> pts;
for (const Triangle3& tria : obs.triangles) {
pts.push_back(tria.p1); pts.push_back(tria.p2);
pts.push_back(tria.p2); pts.push_back(tria.p3);
pts.push_back(tria.p3); pts.push_back(tria.p1);
}
return pts;
}
};
@@ -147,12 +173,11 @@ private:
DataMap3Signal dm;
//std::vector<Obstacle3D> obstacles;
ObstacleTree tree;
BVHDebug<Obstacle3D, BoundingVolumeSphere, Obstacle3DWrapper> tree;
struct Limit {
static constexpr int RAYS = 1000;
static constexpr int HITS = 16;
static constexpr int RAYS = 15000;
static constexpr int HITS = 25;
static constexpr float RSSI = -110;
};
@@ -174,19 +199,13 @@ public:
ModelFactory fac(map);
std::vector<Obstacle3D> obstacles = fac.triangulize();
// build bounding volumes
for (Obstacle3D& obs : obstacles) {
ObstacleLeaf* leaf = new ObstacleLeaf();
leaf->obs = obs;
leaf->boundSphere = getSphereAround(obs.triangles);
if (leaf->boundSphere.radius == 0) {
throw Exception("invalid item detected");
}
tree.add(leaf);
tree.add(obs);
}
tree.optimize();
//tree.optimize();
//tree.show(500, false);
int xxx = 0; (void) xxx;
@@ -209,6 +228,7 @@ public:
std::uniform_real_distribution<float> dy(-1.0, +1.0);
std::uniform_real_distribution<float> dz(-1.0, +1.0);
#pragma omp parallel for
for (int i = 0; i < Limit::RAYS; ++i) {
std::cout << "ray: " << i << std::endl;
@@ -229,6 +249,8 @@ public:
}
//#define USE_DEBUG
private:
@@ -239,7 +261,9 @@ private:
// stop?
if (nextHit.invalid) {
#ifdef USE_DEBUG
hitStop.push_back(nextHit.pos);
#endif
return;
}
@@ -248,7 +272,9 @@ private:
// continue?
if ((nextHit.stopHere) || (ray.getRSSI(nextHit.dist) < Limit::RSSI) || (ray.getDepth() > Limit::HITS)) {
#ifdef USE_DEBUG
hitStop.push_back(nextHit.pos);
#endif
return;
}
@@ -256,11 +282,15 @@ private:
// apply effects
if (ray.isWithin) {
leave(ray, nextHit);
#ifdef USE_DEBUG
hitLeave.push_back(nextHit.pos);
#endif
} else {
enter(ray, nextHit);
reflectAt(ray, nextHit);
#ifdef USE_DEBUG
hitEnter.push_back(nextHit.pos);
#endif
}
@@ -365,6 +395,7 @@ private:
Assert::isNear(1.0f, ray.dir.length(), 0.01f, "not normalized!");
int hits = 0;
const float MAX = 999999;
Hit3 nearest; nearest.dist = MAX;
@@ -387,10 +418,13 @@ private:
// }
std::vector<Obstacle3D*> obs = tree.getHits(ray);
for (const Obstacle3D* o : obs) {
hitTest(ray, *o, nearest);
}
auto onHit = [&] (const Obstacle3D& obs) {
++hits;
hitTest(ray, obs, nearest);
};
tree.getHits(ray, onHit);
}
@@ -399,6 +433,8 @@ private:
nearest.invalid = true;
}
//std::cout << hits << std::endl;
return nearest;
}
@@ -445,6 +481,7 @@ private:
}
/*
Sphere3 getSphereAround(const std::vector<Triangle3>& trias) {
std::vector<Point3> pts;
@@ -463,7 +500,7 @@ private:
return sphere;
}
*/