added new data-structures

added new test-cases
added flexible dijkstra calculation
added debugging log
modified: plotting, grid-generation, grid-importance,
refactoring
This commit is contained in:
2016-01-22 18:47:06 +01:00
parent 12084fe147
commit cdf97322f8
21 changed files with 720 additions and 141 deletions

View File

@@ -6,7 +6,7 @@
#include <gtest/gtest.h>
static inline std::string getDataFile(const std::string& name) {
return "/apps/workspaces/Indoor/tests/data/" + name;
return "/mnt/data/workspaces/Indoor/tests/data/" + name;
}
#endif

View File

@@ -1,26 +1,26 @@
#ifdef WITH_TESTS
//#include "../Tests.h"
//#include "../../floorplan/FloorplanFactorySVG.h"
//#include <cstdlib>
#include "../Tests.h"
#include "../../floorplan/FloorplanFactorySVG.h"
#include <cstdlib>
//TEST(FloorplanFactorySVG, parse) {
TEST(FloorplanFactorySVG, parse) {
// const std::string filename = getDataFile("test.svg");
// FloorplanFactorySVG factory(filename, 1.0);
const std::string filename = getDataFile("test.svg");
FloorplanFactorySVG factory(filename, 1.0);
// Floor f1 = factory.getFloor("1");
// ASSERT_EQ(30, f1.getObstacles().size());
Floor f1 = factory.getFloor("1");
ASSERT_EQ(30, f1.getObstacles().size());
// Floor f2 = factory.getFloor("2");
// ASSERT_EQ(30, f2.getObstacles().size());
Floor f2 = factory.getFloor("2");
ASSERT_EQ(30, f2.getObstacles().size());
// Floor f3 = factory.getFloor("1_2");
// ASSERT_EQ(12, f3.getObstacles().size());
Floor f3 = factory.getFloor("1_2");
ASSERT_EQ(12, f3.getObstacles().size());
//}
}

View File

@@ -22,7 +22,7 @@ TEST(GridImportance, a) {
GridImportance gi;
gi.addImportance(g, 20);
plot(g);
Plot p; p.build(g).fire();
}

View File

@@ -1,6 +1,8 @@
#ifndef PLOT_H
#define PLOT_H
#include "../../grid/Grid.h"
#include <set>
#include <KLib/misc/gnuplot/Gnuplot.h>
#include <KLib/misc/gnuplot/GnuplotSplot.h>
@@ -18,49 +20,62 @@ public:
};
template <int gridSize_cm, typename T> void plot(Grid<gridSize_cm, T>& g) {
class Plot {
public:
K::Gnuplot gp;
K::GnuplotSplot splot;
K::GnuplotSplotElementColorPoints points;
K::GnuplotSplotElementLines lines;
gp << "set ticslevel 0\n";
gp << "set view equal xyz\n";
gp << "set cbrange[0.5:1.0]\n";
gp << "set palette gray negative\n";
template <int gridSize_cm, typename T> Plot& build(Grid<gridSize_cm, T>& g) {
std::set<uint64_t> done;
gp << "set ticslevel 0\n";
gp << "set view equal xyz\n";
gp << "set cbrange[0.5:1.5]\n";
gp << "set palette gray negative\n";
int cnt = 0;
for (int i = 0; i < g.getNumNodes(); ++i) {
const GP& n1 = g[i];
points.add(K::GnuplotPoint3(n1.x_cm, n1.y_cm, n1.z_cm), n1.imp);
std::set<uint64_t> done;
for (int n = 0; n < n1.getNumNeighbors(); ++n) {
const GP& n2 = n1.getNeighbor(n, g);
if (done.find(g.getUID(n2)) == done.end()) {
K::GnuplotPoint3 p1(n1.x_cm, n1.y_cm, n1.z_cm);
K::GnuplotPoint3 p2(n2.x_cm, n2.y_cm, n2.z_cm);
lines.addSegment(p1, p2);
++cnt;
int cnt = 0;
for (int i = 0; i < g.getNumNodes(); ++i) {
const GP& n1 = g[i];
points.add(K::GnuplotPoint3(n1.x_cm, n1.y_cm, n1.z_cm), n1.imp);
for (const T& n2 : g.neighbors(n1)) {
//for (int n = 0; n < n1.getNumNeighbors(); ++n) {
// const GP& n2 = n1.getNeighbor(n, g);
if (done.find(g.getUID(n2)) == done.end()) {
K::GnuplotPoint3 p1(n1.x_cm, n1.y_cm, n1.z_cm);
K::GnuplotPoint3 p2(n2.x_cm, n2.y_cm, n2.z_cm);
lines.addSegment(p1, p2);
++cnt;
}
}
done.insert(g.getUID(n1));
}
done.insert(g.getUID(n1));
points.setPointSize(1);
//splot.add(&lines);
splot.add(&points);
return *this;
}
points.setPointSize(1);
//splot.add(&lines);
splot.add(&points);
Plot& fire() {
gp.draw(splot);
gp.flush();
sleep(1000);
return *this;
}
gp.draw(splot);
gp.flush();
sleep(100);
}
};
#endif // PLOT_H

69
tests/grid/TestAll.cpp Normal file
View File

@@ -0,0 +1,69 @@
#ifdef WITH_TESTS
#include "../Tests.h"
#include "../../grid/factory/GridImportance.h"
#include "../../grid/factory/GridFactory.h"
#include "../../floorplan/FloorplanFactorySVG.h"
#include "../../nav/dijkstra/Dijkstra.h"
#include "Plot.h"
TEST(TestAll, Nav) {
Grid<20, GP> g;
// dijkstra mapper
class TMP {
Grid<20, GP>& grid;
public:
TMP(Grid<20, GP>& grid) : grid(grid) {;}
int getNumNeighbors(const GP& node) const {return node.getNumNeighbors();}
const GP* getNeighbor(const GP& node, const int idx) const {return &grid.getNeighbor(node, idx);}
float getWeightBetween(const GP& n1, const GP& n2) const {
float d = ((Point3)n1 - (Point3)n2).length(2.5);
//if (d > 20) {d*= 1.30;}
return d / std::pow(n2.imp, 3);
}
} tmp(g);
GridFactory<20, GP> gf(g);
FloorplanFactorySVG fpf(getDataFile("fp1.svg"), 6);
Floor f1 = fpf.getFloor("1");
Floor f2 = fpf.getFloor("2");
Stairs s1_2 = fpf.getStairs("1_2");
gf.addFloor(f1, 20);
gf.addFloor(f2, 340);
gf.addStairs(s1_2, 20, 340);
gf.removeIsolated();
GridImportance gi;
gi.addImportance(g, 20);
gi.addImportance(g, 340);
Dijkstra<GP> d;
const GP& start = g.getNodeFor(GridPoint(500,200,20));
//const GP& end = g.getNodeFor(GridPoint(1400,1400,20));
const GP& end = g.getNodeFor(GridPoint(1200,200,340));
d.build(start, end, tmp);
// plot path
K::GnuplotSplotElementLines path; path.setColorHex("#0000ff"); path.setLineWidth(2);
DijkstraNode<GP>* dn = d.getNode(end);
while (dn->previous != nullptr) {
path.add(K::GnuplotPoint3(dn->element->x_cm, dn->element->y_cm, dn->element->z_cm));
dn = dn->previous;
}
Plot p;
p.build(g);
p.splot.add(&path);
p.fire();
}
#endif

View File

@@ -1,17 +1,11 @@
#ifdef WITH_TESTS
#include "Plot.h"
#include "../Tests.h"
#include "../../grid/Grid.h"
#include "../../grid/GridPoint.h"
#include "../../grid/GridNode.h"
class GP : public GridNode, public GridPoint {
public:
GP() : GridNode(), GridPoint() {;}
GP(int x, int y, int z) : GridNode(), GridPoint(x,y,z) {;}
};
TEST(Grid, add) {
Grid<20, GP> grid;
@@ -191,11 +185,11 @@ TEST(Grid, bbox) {
Grid<1, GP> grid;
int idx1 = grid.add(GP( 0, 0, 0));
int idx2 = grid.add(GP( 0, 1, 0));
int idx3 = grid.add(GP( 0,-1, 0));
int idx4 = grid.add(GP( 1, 0, 0));
int idx5 = grid.add(GP(-1, 0, 0));
grid.add(GP( 0, 0, 0));
grid.add(GP( 0, 1, 0));
grid.add(GP( 0,-1, 0));
grid.add(GP( 1, 0, 0));
grid.add(GP(-1, 0, 0));
BBox3 bb = grid.getBBox();

View File

@@ -28,7 +28,7 @@ TEST(GridFactory, create) {
gfInv.addInverted(g, 20);
gfInv.addInverted(g, 340);
// plot(gInv);
//plot(gInv);
}

View File

@@ -0,0 +1,51 @@
#ifdef WITH_TESTS
#include "../../Tests.h"
#include "../../../grid/Grid.h"
#include "../../../nav/dijkstra/Dijkstra.h"
#include "../../grid/Plot.h"
TEST(Dijkstra, build) {
Grid<1, GP> grid;
int idx1 = grid.add(GP( 0, 0, 0));
int idx2 = grid.add(GP( 0, 1, 0));
int idx3 = grid.add(GP( 0,-1, 0));
int idx4 = grid.add(GP( 1, 0, 0));
int idx5 = grid.add(GP(-1, 0, 0));
grid.connectBiDir(idx1, idx2);
grid.connectBiDir(idx1, idx3);
grid.connectBiDir(idx1, idx4);
grid.connectBiDir(idx1, idx5);
class TMP {
Grid<1, GP>& grid;
public:
TMP(Grid<1, GP>& grid) : grid(grid) {;}
int getNumNeighbors(const GP& node) const {return node.getNumNeighbors();}
const GP* getNeighbor(const GP& node, const int idx) const {return &grid.getNeighbor(node, idx);}
float getWeightBetween(const GP& n1, const GP& n2) const {return ((Point3)n1 - (Point3)n2).length();}
} tmp(grid);
Dijkstra<GP> d;
d.build(grid[idx5], grid[idx3], tmp);
// start node must be "idx5"
DijkstraNode<GP>* n = d.getNode(grid[idx5]);
ASSERT_EQ(&grid[idx5], n->element); ASSERT_EQ(nullptr, n->previous); ASSERT_EQ(0, n->cumWeight);
// "idx1" (the center) is reached via idx5
DijkstraNode<GP>* n2 = d.getNode(grid[idx1]);
ASSERT_EQ(&grid[idx1], n2->element); ASSERT_EQ(&grid[idx5], n2->previous->element);
// "idx3" (the target) is reached via idx1 (the center)
DijkstraNode<GP>* n3 = d.getNode(grid[idx3]);
ASSERT_EQ(&grid[idx3], n3->element); ASSERT_EQ(&grid[idx1], n3->previous->element);
}
#endif