worked on 3D model creation

This commit is contained in:
k-a-z-u
2018-02-06 17:34:29 +01:00
parent 0bb1b707de
commit a35e043196
15 changed files with 1442 additions and 1091 deletions

View File

@@ -191,17 +191,39 @@ namespace Floorplan {
struct Elevator; struct Elevator;
struct GroundTruthPoint; struct GroundTruthPoint;
using FloorOutline = std::vector<FloorOutlinePolygon*>; struct FloorOutline : public std::vector<FloorOutlinePolygon*> {
using FloorObstacles = std::vector<FloorObstacle*>; bool enabled = true;
using FloorAccessPoints = std::vector<AccessPoint*>; };
using FloorBeacons = std::vector<Beacon*>; struct FloorObstacles : public std::vector<FloorObstacle*> {
using FloorFingerprintLocations = std::vector<FingerprintLocation*>; bool enabled = true;
using FloorRegions = std::vector<FloorRegion*>; };
using FloorUnderlays = std::vector<UnderlayImage*>; struct FloorAccessPoints : public std::vector<AccessPoint*> {
using FloorPOIs = std::vector<POI*>; bool enabled = true;
using FloorStairs = std::vector<Stair*>; };
using FloorElevators = std::vector<Elevator*>; struct FloorBeacons : public std::vector<Beacon*> {
using FloorGroundTruthPoints = std::vector<GroundTruthPoint*>; bool enabled = true;
};
struct FloorFingerprintLocations : public std::vector<FingerprintLocation*> {
bool enabled = true;
};
struct FloorRegions : public std::vector<FloorRegion*> {
bool enabled = true;
};
struct FloorUnderlays : public std::vector<UnderlayImage*> {
bool enabled = true;
};
struct FloorPOIs : public std::vector<POI*> {
bool enabled = true;
};
struct FloorStairs : public std::vector<Stair*> {
bool enabled = true;
};
struct FloorElevators : public std::vector<Elevator*> {
bool enabled = true;
};
struct FloorGroundTruthPoints : public std::vector<GroundTruthPoint*> {
bool enabled = true;
};
/** describes one floor within the map, starting at a given height */ /** describes one floor within the map, starting at a given height */
struct Floor { struct Floor {
@@ -567,6 +589,8 @@ namespace Floorplan {
/** describe the floorplan's location on earth */ /** describe the floorplan's location on earth */
struct EarthRegistration { struct EarthRegistration {
bool enabled = true;
/** all available correspondences: earth <-> map */ /** all available correspondences: earth <-> map */
std::vector<EarthPosMapPos*> correspondences; std::vector<EarthPosMapPos*> correspondences;

View File

@@ -144,8 +144,8 @@ namespace Floorplan {
/** parse the <elevators> tag */ /** parse the <elevators> tag */
static std::vector<Elevator*> parseFloorElevators(const XMLElem* el) { static FloorElevators parseFloorElevators(const XMLElem* el) {
std::vector<Elevator*> vec; FloorElevators vec;
FOREACH_NODE(n, el) { FOREACH_NODE(n, el) {
if (std::string("elevator") == n->Name()) { vec.push_back(parseFloorElevator(n)); } if (std::string("elevator") == n->Name()) { vec.push_back(parseFloorElevator(n)); }
} }
@@ -153,8 +153,8 @@ namespace Floorplan {
} }
/** parse the <stairs> tag */ /** parse the <stairs> tag */
static std::vector<Stair*> parseFloorStairs(const XMLElem* el) { static FloorStairs parseFloorStairs(const XMLElem* el) {
std::vector<Stair*> vec; FloorStairs vec;
FOREACH_NODE(n, el) { FOREACH_NODE(n, el) {
if (std::string("stair") == n->Name()) { vec.push_back(parseFloorStair(n)); } if (std::string("stair") == n->Name()) { vec.push_back(parseFloorStair(n)); }
} }
@@ -207,8 +207,8 @@ namespace Floorplan {
/** parse the <pois> tag */ /** parse the <pois> tag */
static std::vector<POI*> parseFloorPOIs(const XMLElem* el) { static FloorPOIs parseFloorPOIs(const XMLElem* el) {
std::vector<POI*> vec; FloorPOIs vec;
FOREACH_NODE(n, el) { FOREACH_NODE(n, el) {
if (std::string("poi") == n->Name()) { vec.push_back(parseFloorPOI(n)); } if (std::string("poi") == n->Name()) { vec.push_back(parseFloorPOI(n)); }
} }
@@ -226,8 +226,8 @@ namespace Floorplan {
/** parse the <gtpoints> tag */ /** parse the <gtpoints> tag */
static std::vector<GroundTruthPoint*> parseFloorGroundTruthPoints(const XMLElem* el) { static FloorGroundTruthPoints parseFloorGroundTruthPoints(const XMLElem* el) {
std::vector<GroundTruthPoint*> vec; FloorGroundTruthPoints vec;
FOREACH_NODE(n, el) { FOREACH_NODE(n, el) {
if (std::string("gtpoint") == n->Name()) { vec.push_back(parseFloorGroundTruthPoint(n)); } if (std::string("gtpoint") == n->Name()) { vec.push_back(parseFloorGroundTruthPoint(n)); }
} }
@@ -242,10 +242,9 @@ namespace Floorplan {
return gtp; return gtp;
} }
/** parse the <accesspoints> tag */ /** parse the <accesspoints> tag */
static std::vector<AccessPoint*> parseFloorAccessPoints(const XMLElem* el) { static FloorAccessPoints parseFloorAccessPoints(const XMLElem* el) {
std::vector<AccessPoint*> vec; FloorAccessPoints vec;
FOREACH_NODE(n, el) { FOREACH_NODE(n, el) {
if (std::string("accesspoint") == n->Name()) { vec.push_back(parseAccessPoint(n)); } if (std::string("accesspoint") == n->Name()) { vec.push_back(parseAccessPoint(n)); }
} }
@@ -317,8 +316,8 @@ namespace Floorplan {
/** parse the <beacons> tag */ /** parse the <beacons> tag */
static std::vector<Beacon*> parseFloorBeacons(const XMLElem* el) { static FloorBeacons parseFloorBeacons(const XMLElem* el) {
std::vector<Beacon*> vec; FloorBeacons vec;
FOREACH_NODE(n, el) { FOREACH_NODE(n, el) {
if (std::string("beacon") == n->Name()) { vec.push_back(parseBeacon(n)); } if (std::string("beacon") == n->Name()) { vec.push_back(parseBeacon(n)); }
} }
@@ -341,9 +340,9 @@ namespace Floorplan {
} }
/** parse <fingerprints> <location>s */ /** parse <fingerprints> <location>s */
static std::vector<FingerprintLocation*> parseFingerprintLocations(const XMLElem* el) { static FloorFingerprintLocations parseFingerprintLocations(const XMLElem* el) {
assertNode("fingerprints", el); assertNode("fingerprints", el);
std::vector<FingerprintLocation*> vec; FloorFingerprintLocations vec;
FOREACH_NODE(n, el) { FOREACH_NODE(n, el) {
if (std::string("location") == n->Name()) { vec.push_back(parseFingerprintLocation(n)); } if (std::string("location") == n->Name()) { vec.push_back(parseFingerprintLocation(n)); }
} }
@@ -363,8 +362,8 @@ namespace Floorplan {
return fpl; return fpl;
} }
static std::vector<FloorRegion*> parseFloorRegions(const XMLElem* el) { static FloorRegions parseFloorRegions(const XMLElem* el) {
std::vector<FloorRegion*> vec; FloorRegions vec;
FOREACH_NODE(n, el) { FOREACH_NODE(n, el) {
if (std::string("region") == n->Name()) { vec.push_back(parseFloorRegion(n)); } if (std::string("region") == n->Name()) { vec.push_back(parseFloorRegion(n)); }
} }
@@ -380,9 +379,9 @@ namespace Floorplan {
} }
/** parse the <obstacles> tag */ /** parse the <obstacles> tag */
static std::vector<FloorObstacle*> parseFloorObstacles(const XMLElem* el) { static FloorObstacles parseFloorObstacles(const XMLElem* el) {
assertNode("obstacles", el); assertNode("obstacles", el);
std::vector<FloorObstacle*> obstacles; FloorObstacles obstacles;
FOREACH_NODE(n, el) { FOREACH_NODE(n, el) {
// if (std::string("wall") == n->Name()) {obstacles.push_back(parseFloorObstacleWall(n));} // if (std::string("wall") == n->Name()) {obstacles.push_back(parseFloorObstacleWall(n));}
// if (std::string("door") == n->Name()) {obstacles.push_back(parseFloorObstacleDoor(n));} // if (std::string("door") == n->Name()) {obstacles.push_back(parseFloorObstacleDoor(n));}

View File

@@ -111,7 +111,7 @@ int main(int argc, char** argv) {
//::testing::GTEST_FLAG(filter) = "*Matrix4*"; //::testing::GTEST_FLAG(filter) = "*Matrix4*";
//::testing::GTEST_FLAG(filter) = "*Sphere3*"; //::testing::GTEST_FLAG(filter) = "*Sphere3*";
::testing::GTEST_FLAG(filter) = "FileReader*"; ::testing::GTEST_FLAG(filter) = "Ray.ModelFac*";
//::testing::GTEST_FLAG(filter) = "Timestamp*"; //::testing::GTEST_FLAG(filter) = "Timestamp*";
//::testing::GTEST_FLAG(filter) = "*RayTrace3*"; //::testing::GTEST_FLAG(filter) = "*RayTrace3*";

View File

@@ -10,6 +10,8 @@
#include "../../floorplan/v2/FloorplanReader.h" #include "../../floorplan/v2/FloorplanReader.h"
#include "../../wifi/estimate/ray3/ModelFactory.h" #include "../../wifi/estimate/ray3/ModelFactory.h"
using namespace Ray3D;
struct Wrapper { struct Wrapper {
static std::vector<Point3> getVertices(const BBox3& bbox) { static std::vector<Point3> getVertices(const BBox3& bbox) {

View File

@@ -2,6 +2,7 @@
#include "../Tests.h" #include "../Tests.h"
#include "../../wifi/estimate/ray3/DataMap3.h" #include "../../wifi/estimate/ray3/DataMap3.h"
using namespace Ray3D;
TEST(DataMap3, test) { TEST(DataMap3, test) {

View File

@@ -4,17 +4,20 @@
#include "../../wifi/estimate/ray3/ModelFactory.h" #include "../../wifi/estimate/ray3/ModelFactory.h"
#include "../../floorplan/v2/FloorplanReader.h" #include "../../floorplan/v2/FloorplanReader.h"
#include <fstream> #include <fstream>
using namespace Ray3D;
TEST(Ray, ModelFac) { TEST(Ray, ModelFac) {
std::string file = "/mnt/data/workspaces/IndoorMap/maps/SHL39.xml"; std::string file = "/apps/paper/diss/data/maps/SHL41_nm.xml";
Floorplan::IndoorMap* map = Floorplan::Reader::readFromFile(file); Floorplan::IndoorMap* map = Floorplan::Reader::readFromFile(file);
ModelFactory fac(map); ModelFactory fac(map);
fac.triangulize(); //fac.triangulize();
std::ofstream out("/mnt/vm/fhws.obj"); FloorplanMesh mesh = fac.getMesh();
out << fac.toOBJ() << std::endl;
std::ofstream out("/tmp/fhws.ply");
out << mesh.toPLY() << std::endl;
out.close(); out.close();
} }

View File

@@ -4,6 +4,7 @@
#include "../../wifi/estimate/ray3/WifiRayTrace3D.h" #include "../../wifi/estimate/ray3/WifiRayTrace3D.h"
#include "../../floorplan/v2/FloorplanReader.h" #include "../../floorplan/v2/FloorplanReader.h"
#include <fstream> #include <fstream>
using namespace Ray3D;
TEST(RayTrace3, test) { TEST(RayTrace3, test) {
@@ -19,7 +20,7 @@ TEST(RayTrace3, test) {
ModelFactory fac(map); ModelFactory fac(map);
std::ofstream outOBJ("/tmp/vm/map.obj"); std::ofstream outOBJ("/tmp/vm/map.obj");
outOBJ << fac.toOBJ(); outOBJ << fac.getMesh().toOBJ();
outOBJ.close(); outOBJ.close();
const int gs_cm = 50; const int gs_cm = 50;

View File

@@ -5,13 +5,15 @@
#include "../../../geo/Triangle3.h" #include "../../../geo/Triangle3.h"
#include "../../../math/Matrix4.h" #include "../../../math/Matrix4.h"
class Cube { namespace Ray3D {
private: class Cube {
private:
std::vector<Triangle3> trias; std::vector<Triangle3> trias;
public: public:
/** ctor */ /** ctor */
Cube() { Cube() {
@@ -60,7 +62,7 @@ public:
return res; return res;
} }
private: private:
/** build unit-cube faces */ /** build unit-cube faces */
void unitCube(const bool topAndBottom) { void unitCube(const bool topAndBottom) {
@@ -136,6 +138,8 @@ private:
trias.push_back( Triangle3(p1,p3,p4) ); trias.push_back( Triangle3(p1,p3,p4) );
} }
}; };
}
#endif // QUBE_H #endif // QUBE_H

View File

@@ -7,9 +7,11 @@
#include <functional> #include <functional>
#include <mutex> #include <mutex>
template <typename T> class DataMap3 { namespace Ray3D {
private: template <typename T> class DataMap3 {
private:
float sx_m; float sx_m;
float sy_m; float sy_m;
@@ -29,7 +31,7 @@ private:
T* data = nullptr; T* data = nullptr;
public: public:
/** ctor */ /** ctor */
DataMap3() { DataMap3() {
@@ -155,14 +157,14 @@ public:
std::ofstream os("/tmp/1.dat"); std::ofstream os("/tmp/1.dat");
const float s = 1;//gridSize_cm / 100.0f; const float s = 1;//gridSize_cm / 100.0f;
// for (int y = 0; y < ny; ++y) { // for (int y = 0; y < ny; ++y) {
// for (int x = 0; x < nx; ++x) { // for (int x = 0; x < nx; ++x) {
// float rssi = data[x+y*nx]; // float rssi = data[x+y*nx];
// rssi = (rssi == 0) ? (-100) : (rssi); // rssi = (rssi == 0) ? (-100) : (rssi);
// os << (x*s) << " " << (y*s) << " " << rssi << "\n"; // os << (x*s) << " " << (y*s) << " " << rssi << "\n";
// } // }
// os << "\n"; // os << "\n";
// } // }
for (int y = 0; y < ny; ++y) { for (int y = 0; y < ny; ++y) {
for (int x = 0; x < nx; ++x) { for (int x = 0; x < nx; ++x) {
float rssi = data[x+y*nx]; float rssi = data[x+y*nx];
@@ -176,7 +178,7 @@ public:
} }
*/ */
private: private:
void cleanup() { void cleanup() {
delete[] data; delete[] data;
@@ -184,11 +186,11 @@ private:
} }
}; };
struct DataMap3SignalEntry { struct DataMap3SignalEntry {
@@ -216,11 +218,11 @@ struct DataMap3SignalEntry {
} }
}; };
class DataMap3Signal : public DataMap3<DataMap3SignalEntry> { class DataMap3Signal : public DataMap3<DataMap3SignalEntry> {
public: public:
/** update average */ /** update average */
void update(const float x_m, const float y_m, const float z_m, const float rssi, const float distanceToAP) { void update(const float x_m, const float y_m, const float z_m, const float rssi, const float distanceToAP) {
@@ -230,6 +232,8 @@ public:
} }
}; };
}
#endif // DATAMAP3_H #endif // DATAMAP3_H

View File

@@ -3,10 +3,12 @@
#include "Obstacle3.h" #include "Obstacle3.h"
/** namespace Ray3D {
/**
* meshed version of the floorplan * meshed version of the floorplan
*/ */
struct FloorplanMesh { struct FloorplanMesh {
std::vector<Obstacle3D> elements; std::vector<Obstacle3D> elements;
@@ -43,6 +45,112 @@ struct FloorplanMesh {
} }
}; /** convert to .ply file format */
std::string toPLY() const {
std::stringstream res;
res << "ply\n";
res << "format ascii 1.0\n";
int faces = 0;
int vertices = 0;
for (const Obstacle3D& obs : elements) {
vertices += obs.triangles.size() * 3;
faces += obs.triangles.size();
}
// material
std::vector<Material> mats = {
Material(0,128,0,255), // ground outdoor
Material(64,64,64,255), // ground outdoor
Material(255,96,96,255), // stair
Material(128,128,128,255), // concrete
Material(64,128,255,64), // glass
Material(200,200,200,255), // default
};
res << "element material " << mats.size() << "\n";
res << "property uchar red\n";
res << "property uchar green\n";
res << "property uchar blue\n";
res << "property uchar alpha\n";
res << "element vertex " << vertices << "\n";
res << "property float x\n";
res << "property float y\n";
res << "property float z\n";
res << "property float nx\n";
res << "property float ny\n";
res << "property float nz\n";
res << "property int material_index\n";
res << "property uchar red\n";
res << "property uchar green\n";
res << "property uchar blue\n";
res << "property uchar alpha\n";
res << "element face " << faces << "\n";
res << "property list uchar int vertex_indices\n";
res << "end_header\n";
for (const Material& mat : mats) {
res << mat.r << " " << mat.g << " " << mat.b << " " << mat.a << "\n";
}
for (const Obstacle3D& obs : elements) {
const int matIdx = getMaterial(obs);
const Material& mat = mats[matIdx];
for (const Triangle3& tria : obs.triangles) {
const Point3 n = cross(tria.p2-tria.p1, tria.p3-tria.p1).normalized();
res << tria.p1.x << " " << tria.p1.y << " " << tria.p1.z << " " << n.x << " " << n.y << " " << n.z << " " << matIdx << " " << mat.r << " " << mat.g << " " << mat.b << " " << mat.a << "\n";
res << tria.p2.x << " " << tria.p2.y << " " << tria.p2.z << " " << n.x << " " << n.y << " " << n.z << " " << matIdx << " " << mat.r << " " << mat.g << " " << mat.b << " " << mat.a <<"\n";
res << tria.p3.x << " " << tria.p3.y << " " << tria.p3.z << " " << n.x << " " << n.y << " " << n.z << " " << matIdx << " " << mat.r << " " << mat.g << " " << mat.b << " " << mat.a <<"\n";
}
}
int vidx = 0;
for (const Obstacle3D& obs : elements) {
for (const Triangle3& tria : obs.triangles) {
(void) tria;
res << "3 " << vidx++ << " " << vidx++ << " " << vidx++ << "\n";
}
}
// done
return res.str();
}
struct Material {
int r, g, b, a;
Material(int r, int g, int b, int a) : r(r), g(g), b(b), a(a) {;}
};
int getMaterial(const Obstacle3D& o) const {
if (o.type == Obstacle3D::Type::GROUND_OUTDOOR) {return 0;}
if (o.type == Obstacle3D::Type::GROUND_INDOOR) {return 1;}
if (o.type == Obstacle3D::Type::STAIR) {return 2;}
if (o.mat == Floorplan::Material::CONCRETE) {return 3;}
if (o.mat == Floorplan::Material::GLASS) {return 4;}
return 5;
}
// Color getColor(const Obstacle3D& o) const {
// if (o.type == Obstacle3D::Type::GROUND_OUTDOOR) {return Color(0,128,0,255);}
// if (o.type == Obstacle3D::Type::GROUND_INDOOR) {return Color(64,64,64,255);}
// if (o.mat == Floorplan::Material::CONCRETE) {return Color(128,128,128,255);}
// if (o.mat == Floorplan::Material::GLASS) {return Color(128,128,255,64);}
// return Color(200,200,200,255);
// }
};
}
#endif // FLOORPLANMESH_H #endif // FLOORPLANMESH_H

View File

@@ -5,8 +5,10 @@
#include "../../../floorplan/v2/Floorplan.h" #include "../../../floorplan/v2/Floorplan.h"
#include "../../../Assertions.h" #include "../../../Assertions.h"
/** raytracing attributes for one material */ namespace Ray3D {
struct MaterialAttributes {
/** raytracing attributes for one material */
struct MaterialAttributes {
struct Shadowing { struct Shadowing {
float attenuation; float attenuation;
@@ -24,11 +26,11 @@ struct MaterialAttributes {
MaterialAttributes() {;} MaterialAttributes() {;}
}; };
class Materials { class Materials {
public: public:
/** singleton access */ /** singleton access */
static Materials& get() { static Materials& get() {
@@ -43,7 +45,7 @@ public:
return materials[idx]; return materials[idx];
} }
private: private:
std::vector<MaterialAttributes> materials; std::vector<MaterialAttributes> materials;
@@ -59,6 +61,8 @@ private:
} }
}; };
}
#endif // MATERIALOPTIONS_H #endif // MATERIALOPTIONS_H

View File

@@ -8,21 +8,27 @@
#include "Cube.h" #include "Cube.h"
#include "FloorplanMesh.h" #include "FloorplanMesh.h"
/** namespace Ray3D {
/**
* convert an indoor map into a 3D model based on triangles * convert an indoor map into a 3D model based on triangles
*/ */
class ModelFactory { class ModelFactory {
private: private:
bool exportCeilings = true; bool exportCeilings = true;
bool exportObstacles = true; bool exportObstacles = true;
bool exportStairs = true;
bool exportHandrails = true;
bool exportDoors = false;
bool exportWallTops = false; bool exportWallTops = false;
std::vector<Floorplan::Floor*> exportFloors; std::vector<Floorplan::Floor*> exportFloors;
/** the to-be-exported map */
const Floorplan::IndoorMap* map; const Floorplan::IndoorMap* map;
public: public:
/** ctor */ /** ctor */
@@ -30,6 +36,7 @@ public:
} }
/** whether or not to export ceilings */
void setExportCeilings(bool exp) { void setExportCeilings(bool exp) {
this->exportCeilings = exp; this->exportCeilings = exp;
} }
@@ -48,7 +55,7 @@ public:
} }
private: private:
/** get all triangles grouped by obstacle */ /** get all triangles grouped by obstacle */
@@ -62,6 +69,8 @@ private:
// process each floor // process each floor
for (const Floorplan::Floor* f : floors) { for (const Floorplan::Floor* f : floors) {
if (!f->enabled) {continue;}
// triangulize the floor itself (floor/ceiling) // triangulize the floor itself (floor/ceiling)
if (exportCeilings) { if (exportCeilings) {
std::vector<Obstacle3D> tmp = getFloor(f); std::vector<Obstacle3D> tmp = getFloor(f);
@@ -69,15 +78,20 @@ private:
} }
// process each obstacle within the floor // process each obstacle within the floor
if (f->obstacles.enabled) {
for (const Floorplan::FloorObstacle* fo : f->obstacles) { for (const Floorplan::FloorObstacle* fo : f->obstacles) {
std::vector<Obstacle3D> tmp = getObstacle(f, fo);
// handle line obstacles res.insert(res.end(), tmp.begin(), tmp.end());
const Floorplan::FloorObstacleLine* fol = dynamic_cast<const Floorplan::FloorObstacleLine*>(fo); }
if (fol) {
if (fol->type == Floorplan::ObstacleType::HANDRAIL) {continue;}
if (exportObstacles) {res.push_back(getTriangles(f, fol));}
} }
// stairs
if (f->stairs.enabled) {
for (const Floorplan::Stair* stair : f->stairs) {
if (exportStairs) {res.push_back(getStairs(f, stair));}
}
} }
// TODO: remove // TODO: remove
@@ -92,11 +106,15 @@ private:
private: private:
/** convert a floor (floor/ceiling) into triangles */ /** convert a floor (floor/ceiling) into triangles */
std::vector<Obstacle3D> getFloor(const Floorplan::Floor* f) { std::vector<Obstacle3D> getFloor(const Floorplan::Floor* f) {
std::vector<Obstacle3D> res;
if (!f->enabled) {return res;}
if (!f->outline.enabled) {return res;}
// floor uses an outline based on "add" and "remove" areas // floor uses an outline based on "add" and "remove" areas
// we need to create the apropriate triangles to model the polygon // we need to create the apropriate triangles to model the polygon
// including all holes (remove-areas) // including all holes (remove-areas)
@@ -123,8 +141,6 @@ private:
} }
} }
std::vector<Obstacle3D> res;
// create an obstacle for each type (indoor, outdoor) // create an obstacle for each type (indoor, outdoor)
for (auto& it : types) { for (auto& it : types) {
@@ -163,40 +179,82 @@ private:
} }
/** convert a line obstacle to 3D triangles */ /**
Obstacle3D getTriangles(const Floorplan::Floor* f, const Floorplan::FloorObstacleLine* fol) { * @brief build the given obstacle
* @param f the floor
/* * @param fo the obstacle
Obstacle3D res(fol->material); * @param aboveDoor whether to place this obstacle ABOVE the given door (overwrite)
* @return
Point3 p1(fol->from.x, fol->from.y, f->getStartingZ());
Point3 p2(fol->to.x, fol->to.y, f->getStartingZ());
Point3 p3(fol->to.x, fol->to.y, f->getEndingZ());
Point3 p4(fol->from.x, fol->from.y, f->getEndingZ());
Triangle3 t1(p1,p2,p3);
Triangle3 t2(p1,p3,p4);
res.triangles.push_back(t1);
res.triangles.push_back(t2);
*/ */
std::vector<Obstacle3D> getObstacle(const Floorplan::Floor* f, const Floorplan::FloorObstacle* fo, const Floorplan::FloorObstacleDoor* aboveDoor = nullptr) const {
std::vector<Obstacle3D> res;
// handle line obstacles
const Floorplan::FloorObstacleLine* fol = dynamic_cast<const Floorplan::FloorObstacleLine*>(fo);
if (fol) {
if (exportObstacles) {
if (fol->type != Floorplan::ObstacleType::HANDRAIL || exportHandrails) {
res.push_back(getObstacleLine(f, fol, aboveDoor));
}
}
}
const Floorplan::FloorObstacleDoor* door = dynamic_cast<const Floorplan::FloorObstacleDoor*>(fo);
if (door) {
if (exportObstacles) {
if (exportDoors) {
res.push_back(getDoor(f, door));
}
//std::vector<Obstacle3D> tmp = getDoorAbove(f, door);
//res.insert(res.end(), tmp.begin(), tmp.end());
res.push_back(getDoorAbove(f, door));
}
}
return res;
}
/** convert a line obstacle to 3D triangles */
Obstacle3D getObstacleLine(const Floorplan::Floor* f, const Floorplan::FloorObstacleLine* fol, const Floorplan::FloorObstacleDoor* aboveDoor = nullptr) const {
switch (fol->type) {
case Floorplan::ObstacleType::HANDRAIL:
return getHandrail(f, fol);
case Floorplan::ObstacleType::WINDOW:
return getWindow(f, fol, aboveDoor);
case Floorplan::ObstacleType::WALL:
return getWall(f, fol, aboveDoor);
default:
throw Exception("invalid obstacle type");
}
}
Obstacle3D getWindow(const Floorplan::Floor* f, const Floorplan::FloorObstacleLine* fol, const Floorplan::FloorObstacleDoor* aboveDoor) const {
return getWall(f, fol, aboveDoor);
}
Obstacle3D getWall(const Floorplan::Floor* f, const Floorplan::FloorObstacleLine* fol, const Floorplan::FloorObstacleDoor* aboveDoor) const {
const float thickness_m = fol->thickness_m; const float thickness_m = fol->thickness_m;
const Point2 from = fol->from; const Point2 from = (!aboveDoor) ? (fol->from) : (aboveDoor->from);
const Point2 to = fol->to; const Point2 to = (!aboveDoor) ? (fol->to) : (aboveDoor->to);
const Point2 cen2 = (from+to)/2; const Point2 cen2 = (from+to)/2;
const float rad = std::atan2(to.y - from.y, to.x - from.x); const float rad = std::atan2(to.y - from.y, to.x - from.x);
const float deg = rad * 180 / M_PI; const float deg = rad * 180 / M_PI;
// cube's destination center // cube's destination center
const Point3 pos(cen2.x, cen2.y, f->atHeight + f->height/2); const float cenZ = (!aboveDoor) ? (f->atHeight + f->height/2) : (f->getEndingZ() - (f->height - aboveDoor->height) / 2);
const float height = (!aboveDoor) ? (f->height) : (f->height - aboveDoor->height);
const Point3 pos(cen2.x, cen2.y, cenZ);
// div by 2.01 to prevent overlapps and z-fi // div by 2.01 to prevent overlapps and z-fighting
const float sx = from.getDistance(to) / 2.01f; const float sx = from.getDistance(to) / 2;
const float sy = thickness_m / 2.01f; const float sy = thickness_m / 2;
const float sz = f->height / 2.01f; // prevent overlaps const float sz = height / 2.01f; // prevent overlaps
const Point3 size(sx, sy, sz); const Point3 size(sx, sy, sz);
const Point3 rot(0,0,deg); const Point3 rot(0,0,deg);
@@ -204,14 +262,147 @@ private:
Cube cube(pos, size, rot); Cube cube(pos, size, rot);
// done // done
Obstacle3D res(Obstacle3D::Type::WALL, fol->material); Obstacle3D res(getType(fol), fol->material);
res.triangles = cube.getTriangles(); res.triangles = cube.getTriangles();
return res; return res;
} }
Obstacle3D getDoor(const Floorplan::Floor* f, const Floorplan::FloorObstacleDoor* door) const {
const float thickness_m = 0.10; // TODO??
const Point2 from = door->from;
const Point2 to = door->to;
const Point2 cen2 = (from+to)/2;
const float rad = std::atan2(to.y - from.y, to.x - from.x);
const float deg = rad * 180 / M_PI;
// cube's destination center
const Point3 pos(cen2.x, cen2.y, f->atHeight + door->height/2);
// div by 2.01 to prevent overlapps and z-fighting
const float sx = from.getDistance(to) / 2;
const float sy = thickness_m / 2;
const float sz = door->height / 2.01f; // prevent overlaps
const Point3 size(sx, sy, sz);
const Point3 rot(0,0,deg);
// build
Cube cube(pos, size, rot);
// done
Obstacle3D res(Obstacle3D::Type::DOOR, door->material);
res.triangles = cube.getTriangles();
return res;
}
/** get the missing part/gap, above the given door */
Obstacle3D getDoorAbove(const Floorplan::Floor* f, const Floorplan::FloorObstacleDoor* door) const {
// find the element above the door (= a connected element)
auto comp = [door] (const Floorplan::FloorObstacle* obs) {
if (obs == door) {return false;}
const Floorplan::FloorObstacleLine* line = dynamic_cast<const Floorplan::FloorObstacleLine*>(obs);
if (!line) {return false;}
return (line->from == door->from || line->to == door->from || line->from == door->to || line->to == door->to);
};
auto it = std::find_if(f->obstacles.begin(), f->obstacles.end(), comp);
const Floorplan::FloorObstacleLine* line = dynamic_cast<const Floorplan::FloorObstacleLine*> (*it);
if (!line) {
throw Exception("did not find a matching element to place above the door");
}
// get the obstacle to place above the door
return getObstacleLine(f, line, door);
}
Obstacle3D getHandrail(const Floorplan::Floor* f, const Floorplan::FloorObstacleLine* fol) const {
// target
Obstacle3D res(getType(fol), fol->material);
if (!exportHandrails) {return res;}
const float thickness_m = 0.05;
const Point2 from = fol->from;
const Point2 to = fol->to;
const Point2 cen2 = (from+to)/2;
// edges
const float z1 = f->atHeight;
const float z2 = f->atHeight + 1.0;
Point3 p1 = Point3(from.x, from.y, z1);
Point3 p2 = Point3(to.x, to.y, z1);
Point3 p3 = Point3(from.x, from.y, z2);
Point3 p4 = Point3(to.x, to.y, z2);
const float rad = std::atan2(to.y - from.y, to.x - from.x);
const float deg = rad * 180 / M_PI;
// cube's destination center
const Point3 pUp(cen2.x, cen2.y, z2);
const float sx = from.getDistance(to) / 2;
const float sy = thickness_m / 2;
const float sz = thickness_m / 2;
const Point3 size(sx, sy, sz);
const Point3 rot(0,0,deg);
// upper bar
const Cube cubeUpper(pUp, size, rot);
const std::vector<Triangle3> tmp = cubeUpper.getTriangles();
res.triangles.insert(res.triangles.end(), tmp.begin(), tmp.end());
const Point3 d1 = p2-p1;
const Point3 d2 = p4-p3;
const int numBars = d2.length() / 0.75f;
for (int i = 1; i < numBars; ++i) {
const Point3 s = p1 + d1 * i / numBars;
const Point3 e = p3 + d2 * i / numBars;
const Point3 c = (s+e)/2;
const Point3 size(thickness_m/2, thickness_m/2, s.getDistance(e)/2 - thickness_m);
const Cube cube(c, size, rot);
const std::vector<Triangle3> tmp = cube.getTriangles();
res.triangles.insert(res.triangles.end(), tmp.begin(), tmp.end());
}
// done
return res;
}
/** convert a line obstacle to 3D triangles */
Obstacle3D getStairs(const Floorplan::Floor* f, const Floorplan::Stair* s) {
Obstacle3D res(Obstacle3D::Type::STAIR, Floorplan::Material::CONCRETE);
std::vector<Floorplan::Quad3> quads = Floorplan::getQuads(s->getParts(), f);
for (const Floorplan::Quad3& quad : quads) {
const Triangle3 t1(quad.p1, quad.p2, quad.p3);
const Triangle3 t2(quad.p3, quad.p4, quad.p1);
res.triangles.push_back(t1);
res.triangles.push_back(t2);
}
return res;
}
}; static Obstacle3D::Type getType(const Floorplan::FloorObstacleLine* l) {
switch (l->type) {
case Floorplan::ObstacleType::WALL: return Obstacle3D::Type::WALL;
case Floorplan::ObstacleType::WINDOW: return Obstacle3D::Type::WINDOW;
case Floorplan::ObstacleType::HANDRAIL: return Obstacle3D::Type::HANDRAIL;
default: return Obstacle3D::Type::UNKNOWN;
}
}
};
}
#endif // MODELFACTORY_H #endif // MODELFACTORY_H

View File

@@ -4,7 +4,9 @@
#include <Indoor/floorplan/v2/Floorplan.h> #include <Indoor/floorplan/v2/Floorplan.h>
#include "../../../lib/gpc/gpc.h" #include "../../../lib/gpc/gpc.h"
class Polygon { namespace Ray3D {
class Polygon {
struct GPCPolygon : gpc_polygon { struct GPCPolygon : gpc_polygon {
GPCPolygon() { GPCPolygon() {
@@ -32,11 +34,11 @@ class Polygon {
} }
}; };
private: private:
GPCPolygon state; GPCPolygon state;
public: public:
void add(const Floorplan::Polygon2& poly) { void add(const Floorplan::Polygon2& poly) {
GPCPolygon cur = toGPC(poly); GPCPolygon cur = toGPC(poly);
@@ -80,7 +82,7 @@ public:
} }
private: private:
GPCPolygon toGPC(Floorplan::Polygon2 poly) { GPCPolygon toGPC(Floorplan::Polygon2 poly) {
@@ -100,6 +102,8 @@ private:
} }
}; };
}
#endif // MODELFACTORYHELPER_H #endif // MODELFACTORYHELPER_H

View File

@@ -7,20 +7,24 @@
#include "../../../floorplan/v2/Floorplan.h" #include "../../../floorplan/v2/Floorplan.h"
/** namespace Ray3D {
/**
* 3D obstacle * 3D obstacle
* based on multiple triangles * based on multiple triangles
* has a material and a type * has a material and a type
*/ */
struct Obstacle3D { struct Obstacle3D {
enum class Type { enum class Type {
UNKNOWN, UNKNOWN,
GROUND_INDOOR, GROUND_INDOOR,
GROUND_OUTDOOR, GROUND_OUTDOOR,
STAIR, STAIR,
HANDRAIL,
DOOR, DOOR,
WALL, WALL,
WINDOW,
}; };
Type type; Type type;
@@ -33,7 +37,8 @@ struct Obstacle3D {
/** ctor */ /** ctor */
Obstacle3D(Type type, Floorplan::Material mat) : type(type), mat(mat) {;} Obstacle3D(Type type, Floorplan::Material mat) : type(type), mat(mat) {;}
}; };
}
#endif // OBSTACLE3_H #endif // OBSTACLE3_H

View File

@@ -34,13 +34,15 @@
// http://graphics.stanford.edu/courses/cs148-10-summer/docs/2006--degreve--reflection_refraction.pdf // http://graphics.stanford.edu/courses/cs148-10-summer/docs/2006--degreve--reflection_refraction.pdf
struct Intersection { namespace Ray3D {
struct Intersection {
Point3 pos; Point3 pos;
const Obstacle3D* obs; const Obstacle3D* obs;
Intersection(const Point3 pos, const Obstacle3D* obs) : pos(pos), obs(obs) {;} Intersection(const Point3 pos, const Obstacle3D* obs) : pos(pos), obs(obs) {;}
}; };
struct StateRay3 : public Ray3 { struct StateRay3 : public Ray3 {
//std::vector<Intersection> stack; //std::vector<Intersection> stack;
@@ -87,7 +89,7 @@ struct StateRay3 : public Ray3 {
} }
private: private:
StateRay3 getNext(const Point3 hitPos) const { StateRay3 getNext(const Point3 hitPos) const {
StateRay3 next = *this; StateRay3 next = *this;
@@ -97,7 +99,7 @@ private:
return next; return next;
} }
public: public:
@@ -116,9 +118,9 @@ public:
return totalLen; return totalLen;
} }
}; };
struct Hit3 { struct Hit3 {
const Obstacle3D* obstacle; const Obstacle3D* obstacle;
Triangle3 obstacleTria; Triangle3 obstacleTria;
@@ -133,13 +135,13 @@ struct Hit3 {
Hit3() {;} Hit3() {;}
Hit3(const float dist, const Point3 pos, const Point3 normal) : dist(dist), pos(pos), normal(normal) {;} Hit3(const float dist, const Point3 pos, const Point3 normal) : dist(dist), pos(pos), normal(normal) {;}
}; };
struct Obstacle3DWrapper { struct Obstacle3DWrapper {
static std::vector<Point3> getVertices(const Obstacle3D& obs) { static std::vector<Point3> getVertices(const Obstacle3D& obs) {
std::vector<Point3> pts; std::vector<Point3> pts;
@@ -161,13 +163,13 @@ struct Obstacle3DWrapper {
return pts; return pts;
} }
}; };
class WiFiRaytrace3D { class WiFiRaytrace3D {
private: private:
BBox3 bbox; BBox3 bbox;
Point3 apPos; Point3 apPos;
@@ -186,7 +188,7 @@ private:
std::vector<Point3> hitLeave; std::vector<Point3> hitLeave;
std::vector<Point3> hitStop; std::vector<Point3> hitStop;
public: public:
/** ctor */ /** ctor */
WiFiRaytrace3D(const Floorplan::IndoorMap* map, const int gs, const Point3 apPos) : apPos(apPos) { WiFiRaytrace3D(const Floorplan::IndoorMap* map, const int gs, const Point3 apPos) : apPos(apPos) {
@@ -198,7 +200,7 @@ public:
dm.resize(bbox, gs); dm.resize(bbox, gs);
ModelFactory fac(map); ModelFactory fac(map);
std::vector<Obstacle3D> obstacles = fac.triangulize(); std::vector<Obstacle3D> obstacles = fac.getMesh().elements;
// build bounding volumes // build bounding volumes
for (Obstacle3D& obs : obstacles) { for (Obstacle3D& obs : obstacles) {
@@ -250,10 +252,10 @@ public:
} }
//#define USE_DEBUG //#define USE_DEBUG
private: private:
void trace(const StateRay3& ray) { void trace(const StateRay3& ray) {
@@ -262,9 +264,9 @@ private:
// stop? // stop?
if (nextHit.invalid) { if (nextHit.invalid) {
#ifdef USE_DEBUG #ifdef USE_DEBUG
hitStop.push_back(nextHit.pos); hitStop.push_back(nextHit.pos);
#endif #endif
return; return;
} }
@@ -273,9 +275,9 @@ private:
// continue? // continue?
if ((nextHit.stopHere) || (ray.getRSSI(nextHit.dist) < Limit::RSSI) || (ray.getDepth() > Limit::HITS)) { if ((nextHit.stopHere) || (ray.getRSSI(nextHit.dist) < Limit::RSSI) || (ray.getDepth() > Limit::HITS)) {
#ifdef USE_DEBUG #ifdef USE_DEBUG
hitStop.push_back(nextHit.pos); hitStop.push_back(nextHit.pos);
#endif #endif
return; return;
} }
@@ -283,15 +285,15 @@ private:
// apply effects // apply effects
if (ray.isWithin) { if (ray.isWithin) {
leave(ray, nextHit); leave(ray, nextHit);
#ifdef USE_DEBUG #ifdef USE_DEBUG
hitLeave.push_back(nextHit.pos); hitLeave.push_back(nextHit.pos);
#endif #endif
} else { } else {
enter(ray, nextHit); enter(ray, nextHit);
reflectAt(ray, nextHit); reflectAt(ray, nextHit);
#ifdef USE_DEBUG #ifdef USE_DEBUG
hitEnter.push_back(nextHit.pos); hitEnter.push_back(nextHit.pos);
#endif #endif
} }
@@ -409,15 +411,15 @@ private:
} else { } else {
// // check intersection with all walls // // check intersection with all walls
// for (const Obstacle3D& obs : obstacles) { // for (const Obstacle3D& obs : obstacles) {
// // fast opt-out // // fast opt-out
// //if (!obs.boundingSphere.intersects(ray)) {continue;} // //if (!obs.boundingSphere.intersects(ray)) {continue;}
// hitTest(ray, obs, nearest); // hitTest(ray, obs, nearest);
// } // }
auto onHit = [&] (const Obstacle3D& obs) { auto onHit = [&] (const Obstacle3D& obs) {
@@ -452,12 +454,12 @@ private:
// sanity check // sanity check
// ensure the direction towards the nearest intersection is the same as the ray's direction // ensure the direction towards the nearest intersection is the same as the ray's direction
// otherwise the intersection-test is invalid // otherwise the intersection-test is invalid
#ifdef WITH_ASSERTIONS #ifdef WITH_ASSERTIONS
if (dir.normalized().getDistance(ray.dir) > 0.1) { if (dir.normalized().getDistance(ray.dir) > 0.1) {
return; return;
std::cout << "direction to the nearest hit is not the same direction as the ray has. incorrect intersection test?!" << std::endl; std::cout << "direction to the nearest hit is not the same direction as the ray has. incorrect intersection test?!" << std::endl;
} }
#endif #endif
for (int i = 0; i <= steps; ++i) { for (int i = 0; i <= steps; ++i) {
@@ -465,14 +467,14 @@ private:
const float partLen = ray.start.getDistance(dst); const float partLen = ray.start.getDistance(dst);
//const float len = ray.totalLength + partLen; //const float len = ray.totalLength + partLen;
// const float curRSSI = dm.get(dst.x, dst.y); // const float curRSSI = dm.get(dst.x, dst.y);
const float newRSSI = ray.getRSSI(partLen); const float newRSSI = ray.getRSSI(partLen);
const float totalLen = ray.getLength() + partLen; const float totalLen = ray.getLength() + partLen;
// // ray stronger than current rssi? // // ray stronger than current rssi?
// if (curRSSI == 0 || curRSSI < newRSSI) { // if (curRSSI == 0 || curRSSI < newRSSI) {
// dm.set(dst.x, dst.y, newRSSI); // dm.set(dst.x, dst.y, newRSSI);
// } // }
dm.update(dst.x, dst.y, dst.z, newRSSI, totalLen); dm.update(dst.x, dst.y, dst.z, newRSSI, totalLen);
@@ -503,9 +505,8 @@ private:
} }
*/ */
};
}
};
#endif // WIFIRAYTRACE3D_H #endif // WIFIRAYTRACE3D_H