284 lines
9.3 KiB
C++
284 lines
9.3 KiB
C++
#ifndef FLOORPLANWRITER_H
|
|
#define FLOORPLANWRITER_H
|
|
|
|
#include <iostream>
|
|
|
|
#include "Floorplan.h"
|
|
|
|
#include "../../Assertions.h"
|
|
#include "../../lib/tinyxml/tinyxml2.h"
|
|
|
|
namespace Floorplan {
|
|
|
|
using XMLAttr = tinyxml2::XMLAttribute;
|
|
using XMLElem = tinyxml2::XMLElement;
|
|
using XMLDoc = tinyxml2::XMLDocument;
|
|
|
|
/**
|
|
* write floorplans as XML
|
|
*/
|
|
class Writer {
|
|
|
|
public:
|
|
|
|
static void writeToFile(const IndoorMap* map, const std::string& file) {
|
|
XMLDoc doc;
|
|
construct(doc, map);
|
|
doc.SaveFile(file.c_str(), false);
|
|
}
|
|
|
|
static std::string writeToString(const IndoorMap* map) {
|
|
XMLDoc doc;
|
|
construct(doc, map);
|
|
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 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 void construct(XMLDoc& doc, const IndoorMap* map) {
|
|
|
|
XMLElem* root = doc.NewElement("map");
|
|
doc.InsertEndChild(root);
|
|
root->SetAttribute("width", map->width);
|
|
root->SetAttribute("depth", map->depth);
|
|
|
|
// add all floors to the map
|
|
addFloors(doc, root, map);
|
|
|
|
}
|
|
|
|
/** add all floors to the map */
|
|
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(XMLDoc& doc, XMLElem* floors, const Floor* mf) {
|
|
|
|
XMLElem* floor = doc.NewElement("floor");
|
|
floor->SetAttribute("atHeight", mf->atHeight);
|
|
floor->SetAttribute("height", mf->height);
|
|
floor->SetAttribute("name", mf->name.c_str());
|
|
floors->InsertEndChild(floor);
|
|
|
|
addFloorOutline(doc, floor, mf);
|
|
addFloorObstacles(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(XMLDoc& doc, XMLElem* floor, const Floor* mf) {
|
|
|
|
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) {
|
|
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);
|
|
accesspoint->SetAttribute("y", ap->pos.y);
|
|
accesspoint->SetAttribute("z", ap->pos.z);
|
|
accesspoints->InsertEndChild(accesspoint);
|
|
}
|
|
floor->InsertEndChild(accesspoints);
|
|
|
|
XMLElem* beacons = doc.NewElement("beacons");
|
|
for (const Beacon* b : mf->beacons) {
|
|
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);
|
|
beacon->SetAttribute("y", b->pos.y);
|
|
beacon->SetAttribute("z", b->pos.z);
|
|
beacons->InsertEndChild(beacon);
|
|
}
|
|
floor->InsertEndChild(beacons);
|
|
|
|
|
|
}
|
|
|
|
static void addFloorOutline(XMLDoc& doc, XMLElem* floor, const Floor* mf) {
|
|
|
|
XMLElem* outline = doc.NewElement("outline");
|
|
floor->InsertEndChild(outline);
|
|
|
|
// XMLElem* add = doc.NewElement("add");
|
|
// outline->InsertEndChild(add);
|
|
for (const FloorOutlinePolygon* poly : mf->outline) { addFloorOutlinePolygon(doc, outline, poly); }
|
|
|
|
// XMLElem* remove = doc.NewElement("remove");
|
|
// outline->InsertEndChild(remove);
|
|
// for (const FloorOutlinePolygon& poly : mf.outline.remove) { addFloorOutlinePolygon(doc, remove, poly); }
|
|
|
|
}
|
|
|
|
static void addFloorOutlinePolygon(XMLDoc& doc, XMLElem* dst, const FloorOutlinePolygon* poly) {
|
|
|
|
std::string method = toString(poly->method);
|
|
|
|
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) {
|
|
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
|
|
polygon->InsertEndChild(point);
|
|
}
|
|
|
|
}
|
|
|
|
/** 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(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(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) {
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
floor->InsertEndChild(obstacles);
|
|
|
|
}
|
|
|
|
/** 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);
|
|
}
|
|
|
|
/** 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
|