#ifndef FLOORPLANREADER_H #define FLOORPLANREADER_H #include #include "Floorplan.h" #include "../../misc/Debug.h" #include "../../Assertions.h" #include "../../lib/tinyxml/tinyxml2.h" namespace Floorplan { using XMLAttr = tinyxml2::XMLAttribute; using XMLElem = tinyxml2::XMLElement; /** * read an IndoorMaps from XML-data */ class Reader { private: static constexpr const char* name = "FPReader"; public: /** read an IndoorMap from the given XML-file */ static IndoorMap* readFromFile(const std::string& file) { Log::add(name, "reading floorplan from file: " + file); setlocale(LC_NUMERIC, "C"); tinyxml2::XMLDocument doc; const tinyxml2::XMLError res = doc.LoadFile(file.c_str()); if (res != tinyxml2::XMLError::XML_SUCCESS) {throw Exception("error while loading XML " + file);} return parse(doc); } /** read an IndoorMap from the given XMl-string */ static IndoorMap* readFromString(const std::string& str) { Log::add(name, "reading floorplan from string"); setlocale(LC_NUMERIC, "C"); tinyxml2::XMLDocument doc; const tinyxml2::XMLError res = doc.Parse(str.c_str(), str.length()); if (res != tinyxml2::XMLError::XML_SUCCESS) {throw Exception("error while parsing XML");} return parse(doc); } private: #define FOREACH_NODE(out, in) for( const XMLElem* out = (XMLElem*) in->FirstChild(); out; out = (XMLElem*) out->NextSibling() ) static void assertNode(const std::string& node, const XMLElem* el) { std::string err = std::string("unexpected node '") + el->Name() + "' expected '" + node + "'"; Assert::equal(node, std::string(el->Name()), err); } /** parse the complete document */ static IndoorMap* parse(tinyxml2::XMLDocument& doc) { return parseMap((XMLElem*)doc.FirstChild()); } /** parse the node */ static IndoorMap* parseMap(const XMLElem* el) { Log::add(name, "parsing the map"); IndoorMap* map = new IndoorMap(); map->width = el->FloatAttribute("width"); map->depth = el->FloatAttribute("depth"); FOREACH_NODE(n, el) { if (std::string("floors") == n->Name()) {map->floors = parseFloors(n);} } return map; } /** parse the node */ static std::vector parseFloors(const XMLElem* el) { std::vector floors; FOREACH_NODE(n, el) { if (std::string("floor") == n->Name()) {floors.push_back(parseFloor(n));} } return floors; } /** parse one node */ static Floor* parseFloor(const XMLElem* el) { Floor* floor = new Floor(); Log::add(name, std::string("parsing floor ") + el->Attribute("name")); floor->atHeight = el->FloatAttribute("atHeight"); floor->height = el->FloatAttribute("height"); floor->name = el->Attribute("name"); FOREACH_NODE(n, el) { if (std::string("outline") == n->Name()) {floor->outline = parseFloorOutline(n);} if (std::string("obstacles") == n->Name()) {floor->obstacles = parseFloorObstacles(n);} if (std::string("accesspoints") == n->Name()) {floor->accesspoints = parseFloorAccessPoints(n);} if (std::string("beacons") == n->Name()) {floor->beacons = parseFloorBeacons(n);} if (std::string("regions") == n->Name()) {floor->regions = parseFloorRegions(n);} if (std::string("underlays") == n->Name()) {floor->underlays = parseFloorUnderlays(n);} if (std::string("pois") == n->Name()) {floor->pois = parseFloorPOIs(n);} if (std::string("stairs") == n->Name()) {floor->stairs = parseFloorStairs(n);} if (std::string("elevators") == n->Name()) {floor->elevators = parseFloorElevators(n);} } return floor; } /** parse the tag */ static std::vector parseFloorElevators(const XMLElem* el) { std::vector vec; FOREACH_NODE(n, el) { if (std::string("elevator") == n->Name()) { vec.push_back(parseFloorElevator(n)); } } return vec; } /** parse the tag */ static std::vector parseFloorStairs(const XMLElem* el) { std::vector vec; FOREACH_NODE(n, el) { if (std::string("stair") == n->Name()) { vec.push_back(parseFloorStair(n)); } } return vec; } /** parse an tag */ static Elevator* parseFloorElevator(const XMLElem* el) { Elevator* elev = new Elevator(); elev->center = Point2(el->FloatAttribute("cx"), el->FloatAttribute("cy")); elev->depth = el->FloatAttribute("depth"); elev->width = el->FloatAttribute("width"); elev->rotation = el->FloatAttribute("rotation"); return elev; } /** parse a tag */ static Stair* parseFloorStair(const XMLElem* el) { Stair* stair = nullptr; if (el->IntAttribute("type") == 0) { stair = new StairFreeform(); FOREACH_NODE(n, el) { if (std::string("part") == n->Name()) { StairPart part; part.connectWithPrev = n->BoolAttribute("connect"); part.start.x = n->FloatAttribute("x1"); part.start.y = n->FloatAttribute("y1"); part.start.z = n->FloatAttribute("z1"); part.end.x = n->FloatAttribute("x2"); part.end.y = n->FloatAttribute("y2"); part.end.z = n->FloatAttribute("z2"); part.width = n->FloatAttribute("w"); ((StairFreeform*)stair)->parts.push_back(part); } else { throw "not yet implemented"; } } } else { throw "not yet implemented"; } // stair meta information? const XMLElem* meta = el->FirstChildElement("meta"); if (meta) { stair->meta = parseMetaElement(meta); } return stair; } /** parse the tag */ static std::vector parseFloorPOIs(const XMLElem* el) { std::vector vec; FOREACH_NODE(n, el) { if (std::string("poi") == n->Name()) { vec.push_back(parseFloorPOI(n)); } } return vec; } /** parse a tag */ static POI* parseFloorPOI(const XMLElem* el) { POI* poi = new POI(); poi->name = el->Attribute("name"); poi->type = (POIType) el->IntAttribute("type"); poi->pos = parsePoint2(el); return poi; } /** parse the tag */ static std::vector parseFloorAccessPoints(const XMLElem* el) { std::vector vec; FOREACH_NODE(n, el) { if (std::string("accesspoint") == n->Name()) { vec.push_back(parseAccessPoint(n)); } } return vec; } /** parse the tag */ static FloorUnderlays parseFloorUnderlays(const XMLElem* el) { FloorUnderlays res; FOREACH_NODE(n, el) { if (std::string("underlay") == n->Name()) { res.push_back(parseFloorUnderlay(n)); } } return res; } /** parse an underlay image */ static UnderlayImage* parseFloorUnderlay(const XMLElem* el) { UnderlayImage* img = new UnderlayImage(); img->anchor.x = el->FloatAttribute("x"); img->anchor.y = el->FloatAttribute("y"); img->scaleX = el->FloatAttribute("sx"); img->scaleY = el->FloatAttribute("sy"); img->name = el->Attribute("name"); img->filename = el->Attribute("file"); return img; } // /** parse the tag */ // static FloorKeyValue parseFloorKeyValue(const XMLElem* el) { // FloorKeyValue res; // FOREACH_NODE(n, el) { // if (std::string("keyval") == n->Name()) { res.elements.push_back(parseKeyValueElement(n)); } // } // return res; // } /** parse one element */ static Meta* parseMetaElement(const XMLElem* n) { Meta* elem = new Meta(); const XMLAttr* attr = n->FirstAttribute(); while (attr) { elem->params[attr->Name()] = attr->Value(); attr = attr->Next(); } return elem; } static AccessPoint* parseAccessPoint(const XMLElem* n) { assertNode("accesspoint", n); AccessPoint* ap = new AccessPoint(); ap->mac = n->Attribute("mac"); ap->name = n->Attribute("name"); ap->pos = parsePoint3(n); return ap; } /** parse the tag */ static std::vector parseFloorBeacons(const XMLElem* el) { std::vector vec; FOREACH_NODE(n, el) { if (std::string("beacon") == n->Name()) { vec.push_back(parseBeacon(n)); } } return vec; } static Beacon* parseBeacon(const XMLElem* n) { assertNode("beacon", n); Beacon* b = new Beacon(); b->mac = n->Attribute("mac"); b->name = n->Attribute("name"); b->pos = parsePoint3(n); return b; } static std::vector parseFloorRegions(const XMLElem* el) { std::vector vec; FOREACH_NODE(n, el) { if (std::string("region") == n->Name()) { vec.push_back(parseFloorRegion(n)); } } return vec; } static FloorRegion* parseFloorRegion(const XMLElem* n) { assertNode("region", n); FloorRegion* reg = new FloorRegion(); reg->name = n->Attribute("name"); reg->poly = parsePoly2(n); return reg; } /** parse the tag */ static std::vector parseFloorObstacles(const XMLElem* el) { assertNode("obstacles", el); std::vector obstacles; FOREACH_NODE(n, el) { // 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("window") == n->Name()) {obstacles.push_back(parseFloorObstacleWindow(n));} // if (std::string("pillar") == n->Name()) {obstacles.push_back(parseFloorObstaclePillar(n));} //if (std::string("obstacle") == n->Name()) {obstacles.push_back(parseFloorObstacleLine(n));} // OLD if (std::string("line") == n->Name()) {obstacles.push_back(parseFloorObstacleLine(n));} if (std::string("circle") == n->Name()) {obstacles.push_back(parseFloorObstacleCircle(n));} if (std::string("door") == n->Name()) {obstacles.push_back(parseFloorObstacleDoor(n));} } return obstacles; } /** parse one line */ static FloorObstacleLine* parseFloorObstacleLine(const XMLElem* el) { return new FloorObstacleLine( parseObstacleType(el->Attribute("type")), parseMaterial(el->Attribute("material")), el->FloatAttribute("x1"), el->FloatAttribute("y1"), el->FloatAttribute("x2"), el->FloatAttribute("y2") ); } /** parse one cirlce */ static FloorObstacleCircle* parseFloorObstacleCircle(const XMLElem* el) { return new FloorObstacleCircle( parseMaterial(el->Attribute("material")), el->FloatAttribute("cx"), el->FloatAttribute("cy"), el->FloatAttribute("radius") ); } /** parse one door */ static FloorObstacleDoor* parseFloorObstacleDoor(const XMLElem* el) { return new FloorObstacleDoor( parseDoorType(el->Attribute("type")), parseMaterial(el->Attribute("material")), el->FloatAttribute("x1"), el->FloatAttribute("y1"), el->FloatAttribute("x2"), el->FloatAttribute("y2"), el->FloatAttribute("height"), el->BoolAttribute("swap") ); } /** parse a floor's tag */ static FloorOutline parseFloorOutline(const XMLElem* el) { FloorOutline outline; FOREACH_NODE(n, el) { if (std::string("polygon") == n->Name()) { outline.push_back(parseFloorPolygon(n)); // TODO } } return outline; } /** parse one polygon */ static FloorOutlinePolygon* parseFloorPolygon(const XMLElem* el) { FloorOutlinePolygon* poly = new FloorOutlinePolygon(); poly->name = el->Attribute("name"); poly->method = parseOutlineMethod(el->Attribute("method")); poly->poly = parsePoly2(el); return poly; } /** parse a 2d-polygon denoted by several points */ static Polygon2 parsePoly2(const XMLElem* el) { Polygon2 poly; FOREACH_NODE(n, el) { if (std::string("point") == n->Name()) { const Point2 p2 = parsePoint2(n); poly.points.push_back(p2); } } return poly; } /** parse a 2D point (x,y) using the given tag's attributes. missing attributes are set to 0 */ static Point2 parsePoint2(const XMLElem* el) { Point2 point; point.x = el->FloatAttribute("x"); point.y = el->FloatAttribute("y"); return point; } /** parse a 3D point (x,y,z) using the given tag's attributes. missing attributes are set to 0 */ static Point3 parsePoint3(const XMLElem* el) { Point3 point; point.x = el->FloatAttribute("x"); point.y = el->FloatAttribute("y"); point.z = el->FloatAttribute("z"); return point; } static ObstacleType parseObstacleType(const std::string type) { try { return (ObstacleType) std::stoi(type); } catch (...) { return ObstacleType::UNKNOWN; } } static DoorType parseDoorType(const std::string type) { try { return (DoorType) std::stoi(type); } catch (...) { return DoorType::UNKNOWN; } } static Material parseMaterial(const std::string material) { try { return (Material) std::stoi(material); } catch (...) { return Material::UNKNOWN; } } static OutlineMethod parseOutlineMethod(const std::string method) { try { return (OutlineMethod) std::stoi(method); } catch (...) { return OutlineMethod::ADD; } } }; } #endif // FLOORPLANREADER_H