init push

This commit is contained in:
toni
2019-08-07 17:59:38 +02:00
commit 89a1542dae
8 changed files with 2013 additions and 0 deletions

318
navMesh/filter.h Normal file
View File

@@ -0,0 +1,318 @@
#ifndef NAV_MESH_FILTER_H
#define NAV_MESH_FILTER_H
#include "mesh.h"
#include "../Settings.h"
#include <omp.h>
#include <Indoor/geo/Heading.h>
#include <Indoor/math/Distributions.h>
#include <Indoor/smc/Particle.h>
#include <Indoor/smc/filtering/ParticleFilter.h>
#include <Indoor/smc/filtering/ParticleFilterInitializer.h>
#include <Indoor/smc/filtering/resampling/ParticleFilterResamplingSimple.h>
#include <Indoor/smc/filtering/resampling/ParticleFilterResamplingKLD.h>
#include <Indoor/smc/filtering/estimation/ParticleFilterEstimationBoxKDE.h>
#include <Indoor/smc/filtering/estimation/ParticleFilterEstimationBoxKDE3D.h>
#include <Indoor/smc/filtering/estimation/ParticleFilterEstimationWeightedAverage.h>
#include <Indoor/smc/filtering/estimation/ParticleFilterEstimationMax.h>
#include <Indoor/navMesh/walk/NavMeshWalkSimple.h>
#include <Indoor/navMesh/walk/NavMeshWalkEval.h>
#include <Indoor/navMesh/walk/NavMeshWalkWifi.h>
#include <Indoor/navMesh/walk/NavMeshWalkWifiRegional.h>
#include <Indoor/navMesh/walk/NavMeshWalkUnblockable.h>
#include <Indoor/navMesh/walk/NavMeshWalkKLD.h>
#include <Indoor/navMesh/walk/NavMeshWalkSinkOrSwim.h>
#include <Indoor/navMesh/NavMeshRandom.h>
#include <Indoor/sensors/radio/WiFiMeasurements.h>
#include <Indoor/data/Timestamp.h>
#include <Indoor/sensors/radio/WiFiProbabilityFree.h>
#include <Indoor/sensors/activity/ActivityDetector.h>
#include <Indoor/math/divergence/KullbackLeibler.h>
#include <Indoor/sensors/radio/WiFiQualityAnalyzer.h>
struct MyState {
/** the state's position (within the mesh) */
MyNavMeshLocation pos;
/** the state's heading */
Heading heading;
MyState() : pos(), heading(0) {}
MyState(Point3 p) : pos(p, nullptr), heading(0){}
MyState& operator += (const MyState& o) {
pos.tria = nullptr; // impossible
pos.pos += o.pos.pos;
return *this;
}
MyState& operator /= (const float val) {
pos.tria = nullptr; // impossible
pos.pos /= val;
return *this;
}
MyState operator * (const float val) const {
MyState res;
res.pos.pos = pos.pos * val;
return res;
}
float getX(){
return pos.pos.x;
}
float getY() {
return pos.pos.y;
}
float getZ() {
return pos.pos.z;
}
float getBinValue(const int dim) const {
switch (dim) {
case 0: return this->pos.pos.x;
case 1: return this->pos.pos.y;
case 2: return this->pos.pos.z;
case 3: return this->heading.getRAD();
}
throw "cant find this value within the bin";
}
};
struct MyControl {
int numStepsSinceLastEval = 0;
float headingChangeSinceLastEval = 0;
void afterEval() {
numStepsSinceLastEval = 0;
headingChangeSinceLastEval = 0;
}
//wifi
WiFiMeasurements wifi;
//time
Timestamp currentTime;
//last estimation
Point3 lastEstimate = Point3(26, 43, 7.5);
};
struct MyObservation {
// pressure
float sigmaPressure = 0.10f;
float relativePressure = 0;
//wifi
WiFiMeasurements wifi;
//time
Timestamp currentTime;
//activity
Activity activity;
};
class MyPFInitUniform : public SMC::ParticleFilterInitializer<MyState> {
const MyNavMesh* mesh;
public:
MyPFInitUniform(const MyNavMesh* mesh) : mesh(mesh) {
}
virtual ~MyPFInitUniform(){}
virtual void initialize(std::vector<SMC::Particle<MyState>>& particles) override {
/** random position and heading within the mesh */
Distribution::Uniform<double> dHead(0, 2*M_PI);
MyNavMeshRandom rnd = mesh->getRandom();
for (SMC::Particle<MyState>& p : particles) {
p.state.pos = rnd.draw();
p.state.heading = static_cast<float>(dHead.draw());
p.weight = 1.0 / particles.size();
}
}
};
class MyPFInitFixed : public SMC::ParticleFilterInitializer<MyState> {
const MyNavMesh* mesh;
const Point3 pos;
public:
MyPFInitFixed(const MyNavMesh* mesh, const Point3 pos) : mesh(mesh), pos(pos) {
}
virtual ~MyPFInitFixed(){}
virtual void initialize(std::vector<SMC::Particle<MyState>>& particles) override {
/** random position and heading within the mesh */
Distribution::Uniform<double> dHead(0, 2*M_PI);
for (SMC::Particle<MyState>& p : particles) {
p.state.pos = mesh->getLocation(pos);
p.state.heading = static_cast<float>(dHead.draw());
p.weight = 1.0 / particles.size();
}
}
};
class MyPFTrans : public SMC::ParticleFilterTransition<MyState, MyControl> {
//using MyNavMeshWalk = NM::NavMeshWalkSimple<MyNavMeshTriangle>;
//using MyNavMeshWalk = NM::NavMeshWalkWifiRegional<MyNavMeshTriangle>;
//using MyNavMeshWalk = NM::NavMeshWalkUnblockable<MyNavMeshTriangle>;
using MyNavMeshWalk = NM::NavMeshWalkSinkOrSwim<MyNavMeshTriangle>;
MyNavMeshWalk walker;
WiFiQualityAnalyzer analyzer;
WiFiObserverFree wifiProbability;
SMC::ParticleFilterEstimationBoxKDE<MyState> estimator;
public:
//std::vector<double> listRadiusSub;
MyPFTrans(MyNavMesh& mesh, WiFiModel& wifiModel) :
walker(mesh),
wifiProbability(Settings::WiFiModel::sigma, wifiModel){
// how to evaluate drawn points
walker.addEvaluator(new NM::WalkEvalHeadingStartEndNormal<MyNavMeshTriangle>(0.04));
walker.addEvaluator(new NM::WalkEvalDistance<MyNavMeshTriangle>(0.1));
//walker.addEvaluator(new NM::WalkEvalApproachesTarget<MyNavMeshTriangle>(0.9)); // 90% for particles moving towards the target
}
void transition(std::vector<SMC::Particle<MyState>>& particles, const MyControl* control) override {
// walking and heading random
Distribution::Normal<float> dStepSizeFloor(0.70f, 0.1f);
Distribution::Normal<float> dStepSizeStair(0.35f, 0.1f);
Distribution::Normal<float> dHeading(0.0, 0.1f);
// #pragma omp parallel for num_threads(3)
for (unsigned long i = 0; i < particles.size(); ++i) {
SMC::Particle<MyState>& p = particles[i];
// how to walk
MyNavMeshWalkParams params;
params.heading = p.state.heading + control->headingChangeSinceLastEval + dHeading.draw();
params.numSteps = control->numStepsSinceLastEval;
params.start = p.state.pos;
params.stepSizes.stepSizeFloor_m = dStepSizeFloor.draw();
params.stepSizes.stepSizeStair_m = dStepSizeStair.draw();
if(params.stepSizes.stepSizeFloor_m < 0.1f || params.stepSizes.stepSizeStair_m < 0.1f){
params.stepSizes.stepSizeFloor_m = 0.1f;
params.stepSizes.stepSizeStair_m = 0.1f;
}
// walk
MyNavMeshWalk::ResultEntry res = walker.getOne(params);
// assign back to particle's state
p.weight *= res.probability;
p.state.pos = res.location;
p.state.heading = res.heading;
}
// reset the control (0 steps, 0 delta-heading)
//control->afterEval();
}
};
class MyPFEval : public SMC::ParticleFilterEvaluation<MyState, MyObservation> {
WiFiModel& wifiModel;
WiFiObserverFree wifiProbability;
//TODO: add this to transition probability
float getStairProb(const SMC::Particle<MyState>& p, const Activity act) {
const float kappa = 0.85f;
switch (act) {
case Activity::WALKING:
if (p.state.pos.tria->getType() == static_cast<int>(NM::NavMeshType::FLOOR_INDOOR)) {return kappa;}
if (p.state.pos.tria->getType() == static_cast<int>(NM::NavMeshType::DOOR)) {return kappa;}
if (p.state.pos.tria->getType() == static_cast<int>(NM::NavMeshType::STAIR_LEVELED)) {return kappa;}
{return 1-kappa;}
case Activity::WALKING_UP:
case Activity::WALKING_DOWN:
if (p.state.pos.tria->getType() == static_cast<int>(NM::NavMeshType::STAIR_SKEWED)) {return kappa;}
if (p.state.pos.tria->getType() == static_cast<int>(NM::NavMeshType::STAIR_LEVELED)) {return kappa;}
if (p.state.pos.tria->getType() == static_cast<int>(NM::NavMeshType::ELEVATOR)) {return kappa;}
{return 1-kappa;}
}
return 1.0;
}
public:
//MyPFEval(WiFiModel& wifiModel) : wifiModel(wifiModel), wifiProbability(Settings::WiFiModel::sigma, wifiModel){}
//MyPFEval(WiFiModel& wifiModel) : wifiModel(wifiModel), wifiProbability(Settings::WiFiModel::sigma, wifiModel, WiFiObserverFree::EvalDist::EXPONENTIAL){}
MyPFEval(WiFiModel& wifiModel) : wifiModel(wifiModel), wifiProbability(Settings::WiFiModel::sigma, wifiModel, WiFiObserverFree::EvalDist::CAPPED_NORMAL_DISTRIBUTION){}
virtual double evaluation(std::vector<SMC::Particle<MyState>>& particles, const MyObservation& observation) override {
double sum = 0;
const WiFiMeasurements wifiObs = Settings::WiFiModel::vg_eval.group(observation.wifi);
//#pragma omp parallel for num_threads(3)
for (size_t i = 0; i < particles.size(); ++i) {
SMC::Particle<MyState>& p = particles[i];
double pWifi = wifiProbability.getProbability(p.state.pos.pos, observation.currentTime, wifiObs);
double pBluetooth =
double pStair = static_cast<double>(getStairProb(p, observation.activity));
double prob = pWifi * pStair;
p.weight *= prob;
#pragma omp atomic
sum += prob;
}
return sum;
}
};
using MyFilter = SMC::ParticleFilter<MyState, MyControl, MyObservation>;
#endif

31
navMesh/mesh.h Normal file
View File

@@ -0,0 +1,31 @@
#ifndef NAV_MESH_MESH_H
#define NAV_MESH_MESH_H
#include <Indoor/navMesh/NavMesh.h>
#include <Indoor/navMesh/NavMeshLocation.h>
#include <Indoor/navMesh/NavMeshRandom.h>
#include <Indoor/navMesh/NavMeshFactory.h>
#include <Indoor/navMesh/walk/NavMeshWalkSimple.h>
#include <Indoor/navMesh/meta/NavMeshDijkstra.h>
/** the triangle to use with the nav-mesh */
class MyNavMeshTriangle : public NM::NavMeshTriangle, public NM::NavMeshTriangleDijkstra {
// add own parameters here
public:
MyNavMeshTriangle(const Point3 p1, const Point3 p2, const Point3 p3, uint8_t type) : NM::NavMeshTriangle(p1, p2, p3, type) {
}
};
using MyNavMeshFactory = NM::NavMeshFactory<MyNavMeshTriangle>;
using MyNavMesh = NM::NavMesh<MyNavMeshTriangle>;
using MyNavMeshLocation = NM::NavMeshLocation<MyNavMeshTriangle>;
using MyNavMeshRandom = NM::NavMeshRandom<MyNavMeshTriangle>;
using MyNavMeshWalkParams = NM::NavMeshWalkParams<MyNavMeshTriangle>;
#endif

242
navMesh/meshPlotter.h Normal file
View File

@@ -0,0 +1,242 @@
#ifndef MESHPLOTTER_H
#define MESHPLOTTER_H
#include <KLib/misc/gnuplot/Gnuplot.h>
#include <KLib/misc/gnuplot/GnuplotSplot.h>
#include <KLib/misc/gnuplot/GnuplotSplotElementLines.h>
#include <KLib/misc/gnuplot/GnuplotSplotElementPoints.h>
#include <KLib/misc/gnuplot/GnuplotSplotElementColorPoints.h>
#include <KLib/misc/gnuplot/objects/GnuplotObjectPolygon.h>
#include <Indoor/math/Distributions.h>
#include <Indoor/navMesh/NavMesh.h>
#include <Indoor/floorplan/v2/Floorplan.h>
class NavMeshTriangleDijkstra;
/**
* debug plot NavMeshes
*/
class MeshPlotter {
public:
K::Gnuplot gp;
K::GnuplotSplot plot;
K::GnuplotSplotElementLines pFloor;
K::GnuplotSplotElementLines pOutline;
K::GnuplotSplotElementLines lines;
K::GnuplotSplotElementPoints border;
K::GnuplotSplotElementColorPoints particles;
K::GnuplotSplotElementColorPoints distances;
K::GnuplotSplotElementLines pathEstimated;
K::GnuplotSplotElementLines shortestPath;
K::GnuplotSplotElementLines groundtruthPath;
private:
K::GnuplotFill gFill[6] = {
K::GnuplotFill(K::GnuplotFillStyle::SOLID, K::GnuplotColor::fromHexStr("#0000ff"), 1), // unknown
K::GnuplotFill(K::GnuplotFillStyle::SOLID, K::GnuplotColor::fromHexStr("#999999"), 1), // indoor
K::GnuplotFill(K::GnuplotFillStyle::SOLID, K::GnuplotColor::fromHexStr("#44ffee"), 1), // outdoor
K::GnuplotFill(K::GnuplotFillStyle::SOLID, K::GnuplotColor::fromHexStr("#666699"), 1), // door
K::GnuplotFill(K::GnuplotFillStyle::SOLID, K::GnuplotColor::fromHexStr("#444444"), 1), // stairs_level
K::GnuplotFill(K::GnuplotFillStyle::SOLID, K::GnuplotColor::fromHexStr("#666666"), 1) // stairs_skewed
};
public:
MeshPlotter() {
gp << "set view equal xy\n";
plot.add(&lines); lines.setShowPoints(true);
plot.add(&border);
plot.add(&particles); particles.setPointType(7); particles.setPointSize(0.2);
plot.add(&pathEstimated); pathEstimated.getStroke().setWidth(2); pathEstimated.setShowPoints(false); pathEstimated.getStroke().getColor().setHexStr("#00ff00");
plot.add(&groundtruthPath); groundtruthPath.getStroke().setWidth(2); groundtruthPath.getStroke().getColor().setHexStr("#000000");
plot.add(&distances); distances.setPointSize(0.75); distances.setPointType(7);
plot.add(&shortestPath); shortestPath.getStroke().setWidth(3);
plot.add(&pFloor);
plot.add(&pOutline); pOutline.getStroke().getColor().setHexStr("#999999");
}
void draw() {
gp.draw(plot);
gp.flush();
}
template <typename T> void showParticles(const std::vector<T>& particles) {
this->particles.clear();
double min = +999;
double max = -999;
for (const T& p : particles) {
const K::GnuplotPoint3 p3(p.state.pos.pos.x, p.state.pos.pos.y, p.state.pos.pos.z);
const double prob = std::pow(p.weight, 0.25);
this->particles.add(p3, prob);
if (prob > max) {max = prob;}
if (prob < min) {min = prob;}
}
plot.getAxisCB().setRange(min, max + 0.000001);
}
template <typename Tria> void addMesh(NM::NavMesh<Tria>& nm) {
K::GnuplotStroke gStroke = K::GnuplotStroke(K::GnuplotDashtype::SOLID, 1, K::GnuplotColor::fromHexStr("#666600"));
const BBox3 bbox = nm.getBBox();
border.add(K::GnuplotPoint3(bbox.getMin().x,bbox.getMin().y,bbox.getMin().z));
border.add(K::GnuplotPoint3(bbox.getMax().x,bbox.getMax().y,bbox.getMax().z));
// lines.add(K::GnuplotPoint3(bbox.getMin().x,bbox.getMin().y,bbox.getMin().z), K::GnuplotPoint3(bbox.getMax().x, 0, 0));
// lines.add(K::GnuplotPoint3(bbox.getMin().x,bbox.getMin().y,bbox.getMin().z), K::GnuplotPoint3(0,bbox.getMax().y,0));
// lines.addSegment(K::GnuplotPoint3(bbox.getMin().x,bbox.getMin().y,bbox.getMin().z), K::GnuplotPoint3(0,0,bbox.getMax().z));
//stairs in eigene group? vlt gehen dann auch die dellen weg?
for (const Tria* tria : nm) {
const uint8_t type = tria->getType();
if (type < 0 || type > 5) {
throw std::runtime_error("out of type-bounds");
}
K::GnuplotObjectPolygon* pol = new K::GnuplotObjectPolygon(gFill[type], gStroke);
pol->add(K::GnuplotCoordinate3(tria->getP1().x, tria->getP1().y, tria->getP1().z, K::GnuplotCoordinateSystem::FIRST));
pol->add(K::GnuplotCoordinate3(tria->getP2().x, tria->getP2().y, tria->getP2().z, K::GnuplotCoordinateSystem::FIRST));
pol->add(K::GnuplotCoordinate3(tria->getP3().x, tria->getP3().y, tria->getP3().z, K::GnuplotCoordinateSystem::FIRST));
pol->close();
pol->setZIndex(tria->getP3().z);
plot.getObjects().add(pol);
//for (int i = 0; i < nm.getNumNeighbors(tria); ++i) {
// const Tria* o = nm.getNeighbor(tria, i);
// const Point3 p1 = tria->getCenter();
// const Point3 p2 = o.getCenter();
// //lines.addSegment(K::GnuplotPoint3(p1.x,p1.y,p1.z+0.1), K::GnuplotPoint3(p2.x,p2.y,p2.z+0.1));
//}
for (const NM::NavMeshTriangle* o : *tria) {
const Point3 p1 = tria->getCenter();
const Point3 p2 = o->getCenter();
// lines.addSegment(K::GnuplotPoint3(p1.x,p1.y,p1.z+0.1), K::GnuplotPoint3(p2.x,p2.y,p2.z+0.1));
}
}
plot.getObjects().reOrderByZIndex();
}
template <typename Tria> void addDijkstra(NM::NavMesh<Tria>& mesh) {
distances.clear();
// ensure Tria extends NavMeshTriangleDijkstra
StaticAssert::AinheritsB<Tria, NavMeshTriangleDijkstra>();
NM::NavMeshRandom<Tria> rnd = mesh.getRandom();
for (int i = 0; i < 5000; ++i) {
NM::NavMeshLocation<Tria> loc = rnd.draw();
float v = loc.tria->interpolate(loc.pos, loc.tria->spFromP1.distance, loc.tria->spFromP2.distance, loc.tria->spFromP3.distance);
distances.add(K::GnuplotPoint3(loc.pos.x, loc.pos.y, loc.pos.z), v);
}
// Distribution::Uniform<float> dist (-0.5, +0.5);
// for (const Tria* t : mesh) {
// const Point3 posC = t->getCenter();
// distances.add(K::GnuplotPoint3(posC.x+dist.draw(), posC.y+dist.draw(), posC.z), t->distAtCenter);
// const Point3 pos1 = t->getP1();
// distances.add(K::GnuplotPoint3(pos1.x+dist.draw(), pos1.y+dist.draw(), pos1.z), t->distAtP1);
// const Point3 pos2 = t->getP2();
// distances.add(K::GnuplotPoint3(pos2.x+dist.draw(), pos2.y+dist.draw(), pos2.z), t->distAtP2);
// const Point3 pos3 = t->getP3();
// distances.add(K::GnuplotPoint3(pos3.x+dist.draw(), pos3.y+dist.draw(), pos3.z), t->distAtP3);
// }
}
template <typename Tria> void addDijkstra(std::vector<NM::NavMeshLocation<Tria>>& path) {
shortestPath.clear();
for (auto& e : path) {
K::GnuplotPoint3 gp(e.pos.x, e.pos.y, e.pos.z);
shortestPath.add(gp);
}
}
void addGroundTruthNode(const Point3 pos) {
K::GnuplotPoint3 gp(pos.x, pos.y, std::round(pos.z * 10) / 10);
groundtruthPath.add(gp);
}
void addEstimationNode(const Point3 pos){
K::GnuplotPoint3 est(pos.x, pos.y, std::round(pos.z * 10) / 10);
pathEstimated.add(est);
}
void setTimeInMinute(const int minutes, const int seconds) {
gp << "set label 1002 at screen 0.02, 0.94 'Time: " << minutes << ":" << seconds << "'\n";
}
void setGT(const Point3 pt) {
gp << "set arrow 31337 from " << pt.x << "," << pt.y << "," << (pt.z+1.4) << " to " << pt.x << "," << pt.y << "," << pt.z << " front \n";
}
void setCurPos(const Point3 pt) {
gp << "set arrow 31338 from " << pt.x << "," << pt.y << "," << (pt.z+0.9) << " to " << pt.x << "," << pt.y << "," << pt.z << " lw 2 lc 'green' front \n";
}
void saveToFile(std::ofstream& stream){
gp.draw(plot);
stream << "set terminal x11 size 2000,1500\n";
stream << gp.getBuffer();
stream << "pause -1\n";
gp.flush();
}
void printOverview(const std::string& path) {
gp << "set terminal png size 1280,720\n";
gp << "set output '" << path << "_overview" << ".png'\n";
gp << "set view 75,60\n";
gp << "set autoscale xy\n";
gp << "set autoscale z\n";
draw();
}
//meshless drawing
void addFloors(Floorplan::IndoorMap* map) {
for (Floorplan::Floor* f : map->floors) {
for (Floorplan::FloorObstacle* obs : f->obstacles) {
Floorplan::FloorObstacleLine* line = dynamic_cast<Floorplan::FloorObstacleLine*>(obs);
if (line) {
K::GnuplotPoint3 p1(line->from.x, line->from.y, f->atHeight);
K::GnuplotPoint3 p2(line->to.x, line->to.y, f->atHeight);
pFloor.addSegment(p1, p2);
}
}
}
}
void addOutline(Floorplan::IndoorMap* map) {
for (Floorplan::Floor* f : map->floors) {
for (Floorplan::FloorOutlinePolygon* poly : f->outline) {
const int cnt = poly->poly.points.size();
for (int i = 0; i < cnt; ++i) {
Point2 p1 = poly->poly.points[(i+0)];
Point2 p2 = poly->poly.points[(i+1)%cnt];
K::GnuplotPoint3 gp1(p1.x, p1.y, f->atHeight);
K::GnuplotPoint3 gp2(p2.x, p2.y, f->atHeight);
pOutline.addSegment(gp1, gp2);
}
}
}
}
};
#endif // MESHPLOTTER_H