worked on floorplan (v2)

worked on grid-generation (v2)
new helper methods for geometry
new test cases
This commit is contained in:
2016-07-13 19:11:18 +02:00
parent cc21cbb0ea
commit 34e52cd7f0
26 changed files with 2083 additions and 272 deletions

View File

@@ -10,7 +10,9 @@
namespace Floorplan {
using namespace tinyxml2;
using XMLAttr = tinyxml2::XMLAttribute;
using XMLElem = tinyxml2::XMLElement;
using XMLDoc = tinyxml2::XMLDocument;
/**
* write floorplans as XML
@@ -20,36 +22,29 @@ namespace Floorplan {
public:
static void writeToFile(const IndoorMap* map, const std::string& file) {
XMLDocument doc;
XMLDoc doc;
construct(doc, map);
doc.SaveFile(file.c_str(), false);
}
static std::string writeToString(const IndoorMap* map) {
XMLDocument doc;
XMLDoc doc;
construct(doc, map);
XMLPrinter printer;
tinyxml2::XMLPrinter printer;
doc.Accept( &printer );
return printer.CStr();
}
private:
static std::string toString(const ObstacleType t) {
return std::to_string((int)t);
}
static std::string toString(const ObstacleType t) { return std::to_string((int)t); }
static std::string toString(const DoorType t) { return std::to_string((int)t); }
static std::string toString(const Material m) { return std::to_string((int)m); }
static std::string toString(const OutlineMethod m) { return std::to_string((int)m); }
static std::string toString(const Material m) {
return std::to_string((int)m);
}
static void construct(XMLDoc& doc, const IndoorMap* map) {
static std::string toString(const OutlineMethod m) {
return std::to_string((int)m);
}
static void construct(XMLDocument& doc, const IndoorMap* map) {
XMLElement* root = doc.NewElement("map");
XMLElem* root = doc.NewElement("map");
doc.InsertEndChild(root);
root->SetAttribute("width", map->width);
root->SetAttribute("depth", map->depth);
@@ -60,16 +55,16 @@ namespace Floorplan {
}
/** add all floors to the map */
static void addFloors(XMLDocument& doc, XMLElement* root, const IndoorMap* map) {
XMLElement* floors = doc.NewElement("floors");
static void addFloors(XMLDoc& doc, XMLElem* root, const IndoorMap* map) {
XMLElem* floors = doc.NewElement("floors");
for(const Floor* mf : map->floors) {addFloor(doc, floors, mf);}
root->InsertEndChild(floors);
}
/** add one floor to the floors-node */
static void addFloor(XMLDocument& doc, XMLElement* floors, const Floor* mf) {
static void addFloor(XMLDoc& doc, XMLElem* floors, const Floor* mf) {
XMLElement* floor = doc.NewElement("floor");
XMLElem* floor = doc.NewElement("floor");
floor->SetAttribute("atHeight", mf->atHeight);
floor->SetAttribute("height", mf->height);
floor->SetAttribute("name", mf->name.c_str());
@@ -77,19 +72,64 @@ namespace Floorplan {
addFloorOutline(doc, floor, mf);
addFloorObstacles(doc, floor, mf);
addFloorUnderlays(doc, floor, mf);
addFloorUnderlays(doc, floor, mf);
// add all sorts of POI
addFloorPOI(doc, floor, mf);
addStairs(doc, floor, mf);
}
/** add all stairs to the floor */
static void addStairs(XMLDoc& doc, XMLElem* floor, const Floor* mf) {
XMLElem* stairs = doc.NewElement("stairs");
for (const Stair* _stair : mf->stairs) {
XMLElem* elem = doc.NewElement("stair");
addMetaElement(doc, elem, _stair->meta);
if (dynamic_cast<const StairFreeform*>(_stair)) {
const StairFreeform* stair = (StairFreeform*) _stair;
elem->SetAttribute("type", 0); // TODO: other types?
for (const StairPart& part : stair->parts) {
XMLElem* elemPart = doc.NewElement("part");
elemPart->SetAttribute("connect", part.connectWithPrev);
elemPart->SetAttribute("x1", part.start.x);
elemPart->SetAttribute("y1", part.start.y);
elemPart->SetAttribute("z1", part.start.z);
elemPart->SetAttribute("x2", part.end.x);
elemPart->SetAttribute("y2", part.end.y);
elemPart->SetAttribute("z2", part.end.z);
elemPart->SetAttribute("w", part.width);
elem->InsertEndChild(elemPart);
}
} else {
throw "not yet implemented";
}
stairs->InsertEndChild(elem);
}
floor->InsertEndChild(stairs);
}
/** add all sorts of POI to the floor */
static void addFloorPOI(XMLDocument& doc, XMLElement* floor, const Floor* mf) {
static void addFloorPOI(XMLDoc& doc, XMLElem* floor, const Floor* mf) {
XMLElement* accesspoints = doc.NewElement("accesspoints");
XMLElem* pois = doc.NewElement("pois");
for (const POI* poi : mf->pois) {
XMLElem* elem = doc.NewElement("poi");
elem->SetAttribute("name", poi->name.c_str());
elem->SetAttribute("type", (int) poi->type);
elem->SetAttribute("x", poi->pos.x);
elem->SetAttribute("y", poi->pos.y);
pois->InsertEndChild(elem);
}
floor->InsertEndChild(pois);
XMLElem* accesspoints = doc.NewElement("accesspoints");
for (const AccessPoint* ap : mf->accesspoints) {
XMLElement* accesspoint = doc.NewElement("accesspoint");
XMLElem* accesspoint = doc.NewElement("accesspoint");
accesspoint->SetAttribute("name", ap->name.c_str());
accesspoint->SetAttribute("mac", ap->mac.c_str());
accesspoint->SetAttribute("x", ap->pos.x);
@@ -99,9 +139,9 @@ namespace Floorplan {
}
floor->InsertEndChild(accesspoints);
XMLElement* beacons = doc.NewElement("beacons");
XMLElem* beacons = doc.NewElement("beacons");
for (const Beacon* b : mf->beacons) {
XMLElement* beacon = doc.NewElement("beacon");
XMLElem* beacon = doc.NewElement("beacon");
beacon->SetAttribute("name", b->name.c_str());
beacon->SetAttribute("mac", b->mac.c_str());
beacon->SetAttribute("x", b->pos.x);
@@ -114,32 +154,32 @@ namespace Floorplan {
}
static void addFloorOutline(XMLDocument& doc, XMLElement* floor, const Floor* mf) {
static void addFloorOutline(XMLDoc& doc, XMLElem* floor, const Floor* mf) {
XMLElement* outline = doc.NewElement("outline");
XMLElem* outline = doc.NewElement("outline");
floor->InsertEndChild(outline);
// XMLElement* add = doc.NewElement("add");
// XMLElem* add = doc.NewElement("add");
// outline->InsertEndChild(add);
for (const FloorOutlinePolygon* poly : mf->outline) { addFloorOutlinePolygon(doc, outline, poly); }
// XMLElement* remove = doc.NewElement("remove");
// XMLElem* remove = doc.NewElement("remove");
// outline->InsertEndChild(remove);
// for (const FloorOutlinePolygon& poly : mf.outline.remove) { addFloorOutlinePolygon(doc, remove, poly); }
}
static void addFloorOutlinePolygon(XMLDocument& doc, XMLElement* dst, const FloorOutlinePolygon* poly) {
static void addFloorOutlinePolygon(XMLDoc& doc, XMLElem* dst, const FloorOutlinePolygon* poly) {
std::string method = toString(poly->method);
XMLElement* polygon = doc.NewElement("polygon");
XMLElem* polygon = doc.NewElement("polygon");
polygon->SetAttribute("name", poly->name.c_str());
polygon->SetAttribute("method", method.c_str());
dst->InsertEndChild(polygon);
for (Point2 p : poly->poly.points) {
XMLElement* point = doc.NewElement("point");
XMLElem* point = doc.NewElement("point");
point->SetAttribute("x", p.x);
point->SetAttribute("y", p.y);
// skip the z-attribute as the value is also given by the polygons floor: floor.atHeight
@@ -148,58 +188,49 @@ namespace Floorplan {
}
/** write one <keyval> element */
static void addKeyValueElement(XMLDocument& doc, XMLElement* other, const KeyValueElement* kv) {
XMLElement* elem = doc.NewElement("keyval");
for (auto it : kv->params) {
/** write one <meta> element */
static void addMetaElement(XMLDoc& doc, XMLElem* other, const Meta* meta) {
if (meta == nullptr) {return;} // nothing to add
XMLElem* elem = doc.NewElement("meta");
for (auto it : meta->params) {
elem->Attribute(it.first.c_str(), it.second.c_str());
}
other->InsertEndChild(elem);
}
/** write the <underlays> tag */
static void addFloorUnderlays(XMLDocument& doc, XMLElement* floor, const Floor* mf) {
XMLElement* other = doc.NewElement("underlays");
for (const UnderlayImage* kv : mf->underlays) {
addFloorUnderlay(doc, other, kv);
}
floor->InsertEndChild(other);
}
/** write the <underlays> tag */
static void addFloorUnderlays(XMLDoc& doc, XMLElem* floor, const Floor* mf) {
XMLElem* other = doc.NewElement("underlays");
for (const UnderlayImage* kv : mf->underlays) {
addFloorUnderlay(doc, other, kv);
}
floor->InsertEndChild(other);
}
/** write one underlay */
static void addFloorUnderlay(XMLDocument& doc, XMLElement* underlays, const UnderlayImage* img) {
XMLElement* underlay = doc.NewElement("underlay");
underlay->SetAttribute("x", img->anchor.x);
underlay->SetAttribute("y", img->anchor.y);
underlay->SetAttribute("sx", img->scaleX);
underlay->SetAttribute("sy", img->scaleY);
underlay->SetAttribute("name", img->name.c_str());
underlay->SetAttribute("file", img->filename.c_str());
underlays->InsertEndChild(underlay);
}
/** write one underlay */
static void addFloorUnderlay(XMLDoc& doc, XMLElem* underlays, const UnderlayImage* img) {
XMLElem* underlay = doc.NewElement("underlay");
underlay->SetAttribute("x", img->anchor.x);
underlay->SetAttribute("y", img->anchor.y);
underlay->SetAttribute("sx", img->scaleX);
underlay->SetAttribute("sy", img->scaleY);
underlay->SetAttribute("name", img->name.c_str());
underlay->SetAttribute("file", img->filename.c_str());
underlays->InsertEndChild(underlay);
}
static void addFloorObstacles(XMLDoc& doc, XMLElem* floor, const Floor* mf) {
// /** write the <other> tag */
// static void addFloorKeyValue(XMLDocument& doc, XMLElement* floor, const Floor* mf) {
// XMLElement* other = doc.NewElement("other");
// for (const KeyValueElement* kv : mf->other.elements) {
// addKeyValueElement(doc, other, kv);
// }
// floor->InsertEndChild(other);
// }
static void addFloorObstacles(XMLDocument& doc, XMLElement* floor, const Floor* mf) {
XMLElement* obstacles = doc.NewElement("obstacles");
XMLElem* obstacles = doc.NewElement("obstacles");
for (FloorObstacle* fo : mf->obstacles) {
if (dynamic_cast<FloorObstacleLine*>(fo)) {
addFloorObstacleLine(doc, obstacles, (FloorObstacleLine*)fo);
} else if (dynamic_cast<FloorObstacleCircle*>(fo)) {
addFloorObstacleCircle(doc, obstacles, (FloorObstacleCircle*)fo);
} else if (dynamic_cast<FloorObstacleDoor*>(fo)) {
addFloorObstacleDoor(doc, obstacles, (FloorObstacleDoor*)fo);
}
}
@@ -207,37 +238,46 @@ namespace Floorplan {
}
static void addFloorObstacleLine(XMLDocument& doc, XMLElement* obstacles, FloorObstacleLine* line) {
XMLElement* obstacle = doc.NewElement("line");
const std::string type = toString(line->type);
const std::string material = toString(line->material);
obstacle->SetAttribute("material", material.c_str());
obstacle->SetAttribute("type", type.c_str());
/** write a line obstacle (wall, handrail, ..) */
static void addFloorObstacleLine(XMLDoc& doc, XMLElem* obstacles, FloorObstacleLine* line) {
XMLElem* obstacle = doc.NewElement("line");
obstacle->SetAttribute("material", toString(line->material).c_str());
obstacle->SetAttribute("type", toString(line->type).c_str());
obstacle->SetAttribute("x1", line->from.x);
obstacle->SetAttribute("y1", line->from.y);
obstacle->SetAttribute("x2", line->to.x);
obstacle->SetAttribute("y2", line->to.y);
obstacles->InsertEndChild(obstacle);
}
static void addFloorObstacleCircle(XMLDocument& doc, XMLElement* obstacles, FloorObstacleCircle* circle) {
XMLElement* obstacle = doc.NewElement("line");
const std::string type = toString(circle->type);
const std::string material = toString(circle->material);
obstacle->SetAttribute("material", material.c_str());
obstacle->SetAttribute("type", type.c_str());
/** write a circle obstacle (pillar, ..) */
static void addFloorObstacleCircle(XMLDoc& doc, XMLElem* obstacles, FloorObstacleCircle* circle) {
XMLElem* obstacle = doc.NewElement("circle");
obstacle->SetAttribute("material", toString(circle->material).c_str());
obstacle->SetAttribute("cx", circle->center.x);
obstacle->SetAttribute("cy", circle->center.y);
obstacle->SetAttribute("radius", circle->radius);
obstacles->InsertEndChild(obstacle);
}
/** write a door obstacle */
static void addFloorObstacleDoor(XMLDoc& doc, XMLElem* obstacles, FloorObstacleDoor* door) {
XMLElem* obstacle = doc.NewElement("door");
obstacle->SetAttribute("material", toString(door->material).c_str());
obstacle->SetAttribute("type", toString(door->type).c_str());
obstacle->SetAttribute("x1", door->from.x);
obstacle->SetAttribute("y1", door->from.y);
obstacle->SetAttribute("x2", door->to.x);
obstacle->SetAttribute("y2", door->to.y);
obstacle->SetAttribute("height", door->height);
obstacle->SetAttribute("swap", door->swap);
obstacles->InsertEndChild(obstacle);
}
};
}
#endif // FLOORPLANWRITER_H