From 64b83f569c47f98fe50ac6480cac6adf82e14dc8 Mon Sep 17 00:00:00 2001 From: Patrick Brosi Date: Wed, 1 Aug 2018 14:25:54 +0200 Subject: [PATCH] refactoring --- CMakeLists.txt | 4 +- pfaedle.cfg | 2 + src/pfaedle/PfaedleMain.cpp | 6 +- src/pfaedle/eval/Collector.cpp | 4 +- src/pfaedle/netgraph/EdgePL.h | 16 ++- src/pfaedle/netgraph/NodePL.h | 4 +- src/pfaedle/osm/OsmBuilder.cpp | 16 ++- src/pfaedle/router/EdgePL.cpp | 43 +++--- src/pfaedle/router/EdgePL.h | 2 +- src/pfaedle/router/NodePL.h | 5 +- src/pfaedle/trgraph/EdgePL.cpp | 14 +- src/pfaedle/trgraph/EdgePL.h | 2 +- src/pfaedle/trgraph/NodePL.cpp | 22 ++-- src/pfaedle/trgraph/NodePL.h | 2 +- src/util/geo/Box.h | 2 + src/util/geo/Geo.h | 46 +++++-- src/util/geo/GeoGraph.h | 5 +- src/util/geo/PolyLine.h | 1 + src/util/geo/output/GeoGraphJsonOutput.tpp | 13 +- src/util/geo/output/GeoJsonOutput.h | 10 +- src/util/geo/output/GeoJsonOutput.tpp | 10 +- src/util/json/JsonWriter.cpp | 146 --------------------- src/util/json/JsonWriter.h | 70 ---------- src/util/tests/TestMain.cpp | 51 +++++++ 24 files changed, 186 insertions(+), 310 deletions(-) delete mode 100644 src/util/json/JsonWriter.cpp delete mode 100644 src/util/json/JsonWriter.h diff --git a/CMakeLists.txt b/CMakeLists.txt index cc923d6..a9f7545 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,10 +21,10 @@ endif() # set compiler flags, see http://stackoverflow.com/questions/7724569/debug-vs-release-in-cmake if(OPENMP_FOUND) - set(CMAKE_CXX_FLAGS "-fopenmp -Ofast -fno-signed-zeros -fno-trapping-math -Wall -Wno-format-extra-args -Wextra -Wformat-nonliteral -Wformat-security -Wformat=2 -Wfatal-errors -Wextra -Wno-implicit-fallthrough -pedantic") + set(CMAKE_CXX_FLAGS "-fopenmp -Ofast -fno-signed-zeros -fno-trapping-math -Wall -Wno-format-extra-args -Wextra -Wformat-nonliteral -Wformat-security -Wformat=2 -Wextra -Wno-implicit-fallthrough -pedantic") else() message(WARNING "Configuring without OpenMP!") - set(CMAKE_CXX_FLAGS "-Ofast -fno-signed-zeros -fno-trapping-math -Wall -Wno-format-extra-args -Wextra -Wformat-nonliteral -Wformat-security -Wformat=2 -Wfatal-errors -Wextra -Wno-implicit-fallthrough -pedantic") + set(CMAKE_CXX_FLAGS "-Ofast -fno-signed-zeros -fno-trapping-math -Wall -Wno-format-extra-args -Wextra -Wformat-nonliteral -Wformat-security -Wformat=2 -Wextra -Wno-implicit-fallthrough -pedantic") endif() set(CMAKE_CXX_FLAGS_DEBUG "-Og -g -DLOGLEVEL=3 -DPFAEDLE_DBG=1") set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS} -DLOGLEVEL=2") diff --git a/pfaedle.cfg b/pfaedle.cfg index 6ad4ae9..ad04557 100644 --- a/pfaedle.cfg +++ b/pfaedle.cfg @@ -333,8 +333,10 @@ osm_filter_lvl4: highway=living_street highway=pedestrian highway=service + psv=no osm_filter_lvl5: + bus=no service=siding access=permissive access=private diff --git a/src/pfaedle/PfaedleMain.cpp b/src/pfaedle/PfaedleMain.cpp index 7c74e3f..abe9383 100644 --- a/src/pfaedle/PfaedleMain.cpp +++ b/src/pfaedle/PfaedleMain.cpp @@ -21,7 +21,7 @@ #include "pfaedle/trgraph/Graph.h" #include "util/geo/output/GeoGraphJsonOutput.h" #include "util/geo/output/GeoJsonOutput.h" -#include "util/json/JsonWriter.h" +#include "util/json/Writer.h" #include "util/log/Log.h" using std::string; @@ -145,10 +145,10 @@ int main(int argc, char** argv) { if (singleTrip->getShape()) { auto orig = Collector::getWebMercLine(singleTrip->getShape(), -1, -1); - o.print(orig, {{"ver", "old"}}); + o.print(orig, util::json::Dict{{"ver", "old"}}); } - o.print(l, {{"ver", "new"}}); + o.print(l, util::json::Dict{{"ver", "new"}}); o.flush(); pstr.close(); diff --git a/src/pfaedle/eval/Collector.cpp b/src/pfaedle/eval/Collector.cpp index 334641f..5e7e7b7 100644 --- a/src/pfaedle/eval/Collector.cpp +++ b/src/pfaedle/eval/Collector.cpp @@ -107,11 +107,11 @@ double Collector::add(const Trip* t, const Shape* oldS, const Shape* newS, FLine newLCut; for (auto oldL : oldSegs) { - gjout.print(oldL, {{"ver", "old"}}); + gjout.print(oldL, util::json::Dict{{"ver", "old"}}); oldLCut.insert(oldLCut.end(), oldL.begin(), oldL.end()); } for (auto newL : newSegs) { - gjout.print(newL, {{"ver", "new"}}); + gjout.print(newL, util::json::Dict{{"ver", "new"}}); newLCut.insert(newLCut.end(), newL.begin(), newL.end()); } diff --git a/src/pfaedle/netgraph/EdgePL.h b/src/pfaedle/netgraph/EdgePL.h index b568fed..3a245a4 100644 --- a/src/pfaedle/netgraph/EdgePL.h +++ b/src/pfaedle/netgraph/EdgePL.h @@ -5,9 +5,9 @@ #ifndef PFAEDLE_NETGRAPH_EDGEPL_H_ #define PFAEDLE_NETGRAPH_EDGEPL_H_ -#include #include #include +#include #include "ad/cppgtfs/gtfs/Feed.h" #include "util/String.h" #include "util/geo/GeoGraph.h" @@ -33,12 +33,14 @@ class EdgePL : public GeoEdgePL { } } const util::geo::FLine* getGeom() const { return &_l; } - void getAttrs(std::map* obj) const { - (*obj)["num_trips"] = std::to_string(_trips.size()); - (*obj)["route_short_names"] = - util::implode(_routeShortNames.begin(), _routeShortNames.end(), ", "); - (*obj)["trip_short_names"] = - util::implode(_tripShortNames.begin(), _tripShortNames.end(), ", "); + util::json::Dict getAttrs() const { + util::json::Dict obj; + obj["num_trips"] = static_cast(_trips.size()); + obj["route_short_names"] = util::json::Array( + _routeShortNames.begin(), _routeShortNames.end()); + obj["trip_short_names"] = util::json::Array(_tripShortNames.begin(), + _tripShortNames.end()); + return obj; } private: diff --git a/src/pfaedle/netgraph/NodePL.h b/src/pfaedle/netgraph/NodePL.h index 8588536..944c63e 100644 --- a/src/pfaedle/netgraph/NodePL.h +++ b/src/pfaedle/netgraph/NodePL.h @@ -25,8 +25,8 @@ class NodePL : public GeoNodePL { NodePL(const util::geo::FPoint& geom) { _geom = geom; } // NOLINT const util::geo::FPoint* getGeom() const { return &_geom; } - void getAttrs(std::map* attrs) const { - UNUSED(attrs); + util::json::Dict getAttrs() const { + return util::json::Dict(); } private: diff --git a/src/pfaedle/osm/OsmBuilder.cpp b/src/pfaedle/osm/OsmBuilder.cpp index cb7839f..db39e03 100644 --- a/src/pfaedle/osm/OsmBuilder.cpp +++ b/src/pfaedle/osm/OsmBuilder.cpp @@ -190,7 +190,7 @@ void OsmBuilder::read(const std::string& path, const OsmReadOpts& opts, deleteOrphEdgs(g); LOG(VDEBUG) << "Collapsing edges..."; - collapseEdges(g); + // collapseEdges(g); LOG(VDEBUG) << "Deleting orphan nodes..."; deleteOrphNds(g); @@ -212,8 +212,14 @@ void OsmBuilder::read(const std::string& path, const OsmReadOpts& opts, LOG(VDEBUG) << "Write dummy node self-edges..."; writeSelfEdgs(g); - LOG(DEBUG) << "Graph has " << g->getNds()->size() << " nodes and " << comps - << " connected component(s)"; + size_t numEdges = 0; + + for (auto* n : *g->getNds()) { + numEdges += n->getAdjListOut().size(); + } + + LOG(DEBUG) << "Graph has " << g->getNds()->size() << " nodes, " << numEdges + << " edges and " << comps << " connected component(s)"; } // _____________________________________________________________________________ @@ -367,8 +373,7 @@ void OsmBuilder::readWriteWays(xml::File* i, util::xml::XmlWriter* o, } // _____________________________________________________________________________ -NodePL OsmBuilder::plFromGtfs(const Stop* s, - const OsmReadOpts& ops) const { +NodePL OsmBuilder::plFromGtfs(const Stop* s, const OsmReadOpts& ops) const { NodePL ret(util::geo::latLngToWebMerc(s->getLat(), s->getLng()), StatInfo(ops.statNormzer(s->getName()), ops.trackNormzer(s->getPlatformCode()), false)); @@ -1671,7 +1676,6 @@ uint32_t OsmBuilder::writeComps(Graph* g) const { } } - // the last comp was not used delete comp; diff --git a/src/pfaedle/router/EdgePL.cpp b/src/pfaedle/router/EdgePL.cpp index 64c8f15..9473c7c 100644 --- a/src/pfaedle/router/EdgePL.cpp +++ b/src/pfaedle/router/EdgePL.cpp @@ -76,24 +76,27 @@ void EdgePL::setCost(const router::EdgeCost& c) { _cost = c; } // _____________________________________________________________________________ -void EdgePL::getAttrs(std::map* obj) const { - (*obj)["cost"] = std::to_string(_cost.getValue()); - (*obj)["from_edge"] = util::toString(_startE); - (*obj)["to_edge"] = util::toString(_endE); - (*obj)["cost_m_lvl1"] = std::to_string(_cost.meterDistLvl1); - (*obj)["cost_m_lvl0"] = std::to_string(_cost.meterDist); - (*obj)["cost_m_lvl1"] = std::to_string(_cost.meterDistLvl1); - (*obj)["cost_m_lvl2"] = std::to_string(_cost.meterDistLvl2); - (*obj)["cost_m_lvl3"] = std::to_string(_cost.meterDistLvl3); - (*obj)["cost_m_lvl4"] = std::to_string(_cost.meterDistLvl4); - (*obj)["cost_m_lvl5"] = std::to_string(_cost.meterDistLvl5); - (*obj)["cost_m_lvl6"] = std::to_string(_cost.meterDistLvl6); - (*obj)["cost_m_lvl7"] = std::to_string(_cost.meterDistLvl7); - (*obj)["cost_fullturn"] = std::to_string(_cost.fullTurns); - (*obj)["cost_st_passthru"] = std::to_string(_cost.passThruStations); - (*obj)["cost_m_oneway"] = std::to_string(_cost.oneWayMeters); - (*obj)["cost_m_lineunmatch"] = std::to_string(_cost.lineUnmatchedMeters); - (*obj)["cost_reach_node_pen"] = std::to_string(_cost.reachPen); - (*obj)["cost_oneway_event"] = std::to_string(_cost.oneWayEdges); - (*obj)["dummy"] = _edges.size() ? "no" : "yes"; +util::json::Dict EdgePL::getAttrs() const { + util::json::Dict obj; + obj["cost"] = std::to_string(_cost.getValue()); + obj["from_edge"] = util::toString(_startE); + obj["to_edge"] = util::toString(_endE); + obj["cost_m_lvl1"] = std::to_string(_cost.meterDistLvl1); + obj["cost_m_lvl0"] = std::to_string(_cost.meterDist); + obj["cost_m_lvl1"] = std::to_string(_cost.meterDistLvl1); + obj["cost_m_lvl2"] = std::to_string(_cost.meterDistLvl2); + obj["cost_m_lvl3"] = std::to_string(_cost.meterDistLvl3); + obj["cost_m_lvl4"] = std::to_string(_cost.meterDistLvl4); + obj["cost_m_lvl5"] = std::to_string(_cost.meterDistLvl5); + obj["cost_m_lvl6"] = std::to_string(_cost.meterDistLvl6); + obj["cost_m_lvl7"] = std::to_string(_cost.meterDistLvl7); + obj["cost_fullturn"] = std::to_string(_cost.fullTurns); + obj["cost_st_passthru"] = std::to_string(_cost.passThruStations); + obj["cost_m_oneway"] = std::to_string(_cost.oneWayMeters); + obj["cost_m_lineunmatch"] = std::to_string(_cost.lineUnmatchedMeters); + obj["cost_reach_node_pen"] = std::to_string(_cost.reachPen); + obj["cost_oneway_event"] = std::to_string(_cost.oneWayEdges); + obj["dummy"] = _edges.size() ? "no" : "yes"; + + return obj; } diff --git a/src/pfaedle/router/EdgePL.h b/src/pfaedle/router/EdgePL.h index 4838601..a5dee5e 100644 --- a/src/pfaedle/router/EdgePL.h +++ b/src/pfaedle/router/EdgePL.h @@ -19,7 +19,7 @@ class EdgePL : public GeoEdgePL { public: EdgePL() : _cost(), _start(0), _end(0), _startE(0), _endE(0) {} const util::geo::FLine* getGeom() const; - void getAttrs(std::map* attrs) const; + util::json::Dict getAttrs() const; router::EdgeList* getEdges(); const router::EdgeList& getEdges() const; void setStartNode(const trgraph::Node* s); diff --git a/src/pfaedle/router/NodePL.h b/src/pfaedle/router/NodePL.h index 1ab743c..e8aada3 100644 --- a/src/pfaedle/router/NodePL.h +++ b/src/pfaedle/router/NodePL.h @@ -23,8 +23,9 @@ class NodePL : public GeoNodePL { const util::geo::FPoint* getGeom() const { return !_n ? 0 : _n->pl().getGeom(); } - void getAttrs(std::map* attrs) const { - if (_n) _n->pl().getAttrs(attrs); + util::json::Dict getAttrs() const { + if (_n) return _n->pl().getAttrs(); + return util::json::Dict(); } private: diff --git a/src/pfaedle/trgraph/EdgePL.cpp b/src/pfaedle/trgraph/EdgePL.cpp index 9612db7..8b63d04 100644 --- a/src/pfaedle/trgraph/EdgePL.cpp +++ b/src/pfaedle/trgraph/EdgePL.cpp @@ -108,11 +108,12 @@ const util::geo::FLine* EdgePL::getGeom() const { return _l; } util::geo::FLine* EdgePL::getGeom() { return _l; } // _____________________________________________________________________________ -void EdgePL::getAttrs(std::map* obj) const { - (*obj)["m_length"] = std::to_string(_length); - (*obj)["oneway"] = std::to_string(static_cast(_oneWay)); - (*obj)["level"] = std::to_string(_lvl); - (*obj)["restriction"] = isRestricted() ? "yes" : "no"; +util::json::Dict EdgePL::getAttrs() const { + util::json::Dict obj; + obj["m_length"] = std::to_string(_length); + obj["oneway"] = std::to_string(static_cast(_oneWay)); + obj["level"] = std::to_string(_lvl); + obj["restriction"] = isRestricted() ? "yes" : "no"; std::stringstream ss; bool first = false; @@ -127,7 +128,8 @@ void EdgePL::getAttrs(std::map* obj) const { first = true; } - (*obj)["lines"] = ss.str(); + obj["lines"] = ss.str(); + return obj; } // _____________________________________________________________________________ diff --git a/src/pfaedle/trgraph/EdgePL.h b/src/pfaedle/trgraph/EdgePL.h index b467f44..0eb5800 100644 --- a/src/pfaedle/trgraph/EdgePL.h +++ b/src/pfaedle/trgraph/EdgePL.h @@ -57,7 +57,7 @@ class EdgePL : public GeoEdgePL { void addPoint(const util::geo::FPoint& p); // Fill obj with k/v pairs describing the parameters of this payload. - void getAttrs(std::map* obj) const; + util::json::Dict getAttrs() const; // Return the length in meters stored for this edge payload double getLength() const; diff --git a/src/pfaedle/trgraph/NodePL.cpp b/src/pfaedle/trgraph/NodePL.cpp index c4bd10c..53a79cc 100644 --- a/src/pfaedle/trgraph/NodePL.cpp +++ b/src/pfaedle/trgraph/NodePL.cpp @@ -114,18 +114,19 @@ const util::geo::FPoint* NodePL::getGeom() const { return &_geom; } void NodePL::setGeom(const util::geo::FPoint& geom) { _geom = geom; } // _____________________________________________________________________________ -void NodePL::getAttrs(std::map* obj) const { - (*obj)["component"] = std::to_string(reinterpret_cast(_component)); +util::json::Dict NodePL::getAttrs() const { + util::json::Dict obj; + obj["component"] = std::to_string(reinterpret_cast(_component)); #ifdef PFAEDLE_DBG - (*obj)["dijkstra_vis"] = _vis ? "yes" : "no"; + obj["dijkstra_vis"] = _vis ? "yes" : "no"; #endif if (getSI()) { - (*obj)["station_info_ptr"] = util::toString(_si); - (*obj)["station_name"] = _si->getName(); - (*obj)["station_alt_names"] = util::implode(_si->getAltNames(), ","); - (*obj)["from_osm"] = _si->isFromOsm() ? "yes" : "no"; - (*obj)["station_platform"] = _si->getTrack(); - (*obj)["station_group"] = + obj["station_info_ptr"] = util::toString(_si); + obj["station_name"] = _si->getName(); + obj["station_alt_names"] = util::implode(_si->getAltNames(), ","); + obj["from_osm"] = _si->isFromOsm() ? "yes" : "no"; + obj["station_platform"] = _si->getTrack(); + obj["station_group"] = std::to_string(reinterpret_cast(_si->getGroup())); std::stringstream gtfsIds; @@ -135,8 +136,9 @@ void NodePL::getAttrs(std::map* obj) const { } } - (*obj)["station_group_stops"] = gtfsIds.str(); + obj["station_group_stops"] = gtfsIds.str(); } + return obj; } // _____________________________________________________________________________ diff --git a/src/pfaedle/trgraph/NodePL.h b/src/pfaedle/trgraph/NodePL.h index a9bc4a9..74b0483 100644 --- a/src/pfaedle/trgraph/NodePL.h +++ b/src/pfaedle/trgraph/NodePL.h @@ -37,7 +37,7 @@ class NodePL : public GeoNodePL { void setGeom(const util::geo::FPoint& geom); // Fill obj with k/v pairs describing the parameters of this payload. - void getAttrs(std::map* attrs) const; + util::json::Dict getAttrs() const; // Set the station info for this node void setSI(const StatInfo& si); diff --git a/src/util/geo/Box.h b/src/util/geo/Box.h index 43f05e9..9642b31 100644 --- a/src/util/geo/Box.h +++ b/src/util/geo/Box.h @@ -77,6 +77,8 @@ class RotatedBox { const Point& getCenter() const { return _center; } Point& getCenter() { return _center; } + void setDegree(double deg) { _deg = deg; } + private: Box _box; double _deg; diff --git a/src/util/geo/Geo.h b/src/util/geo/Geo.h index 94bb67a..53eabae 100644 --- a/src/util/geo/Geo.h +++ b/src/util/geo/Geo.h @@ -219,15 +219,15 @@ inline Box minbox() { // _____________________________________________________________________________ template inline RotatedBox shrink(const RotatedBox& b, double d) { - double xd = b.b.getUpperRight().getX() - b.b.getLowerLeft().getX(); - double yd = b.b.getUpperRight().getY() - b.b.getLowerLeft().getY(); + double xd = b.getBox().getUpperRight().getX() - b.getBox().getLowerLeft().getX(); + double yd = b.getBox().getUpperRight().getY() - b.getBox().getLowerLeft().getY(); if (xd <= 2 * d) d = xd / 2 - 1; if (yd <= 2 * d) d = yd / 2 - 1; Box r( - Point(b.b.getLowerLeft().getX() + d, b.b.getLowerLeft().getY() + d), - Point(b.b.getUpperRight().getX() - d, b.b.getUpperRight().getY() - d)); + Point(b.getBox().getLowerLeft().getX() + d, b.getBox().getLowerLeft().getY() + d), + Point(b.getBox().getUpperRight().getX() - d, b.getBox().getUpperRight().getY() - d)); return RotatedBox(r, b.getDegree(), b.getCenter()); } @@ -1137,8 +1137,8 @@ inline Polygon convexHull(const RotatedBox& b) { // _____________________________________________________________________________ template -inline size_t convexHullImpl(const Line& a, size_t p1, size_t p2, - Line* h, uint8_t d) { +inline size_t convexHullImpl(const MultiPoint& a, size_t p1, size_t p2, + Line* h, uint8_t d) { // quickhull by Barber, Dobkin & Huhdanpaa Point pa; bool found = false; @@ -1162,7 +1162,7 @@ inline size_t convexHullImpl(const Line& a, size_t p1, size_t p2, // _____________________________________________________________________________ template -inline Polygon convexHull(const Line& l) { +inline Polygon convexHull(const MultiPoint& l) { if (l.size() == 2) return convexHull(LineSegment(l[0], l[1])); if (l.size() == 1) return convexHull(l[0]); @@ -1185,6 +1185,14 @@ inline Polygon convexHull(const Polygon& p) { return convexHull(p.getOuter()); } +// _____________________________________________________________________________ +template +inline Polygon convexHull(const MultiLine& ls) { + MultiPoint mp; + for (const auto& l : ls) mp.insert(mp.end(), l.begin(), l.end()); + return convexHull(mp); +} + // _____________________________________________________________________________ template inline Box extendBox(const Line& l, Box b) { @@ -1200,6 +1208,12 @@ inline Box extendBox(const LineSegment& ls, Box b) { return b; } +// _____________________________________________________________________________ +template +inline Box extendBox(const Polygon& ls, Box b) { + return extendBox(ls.getOuter(), b); +} + // _____________________________________________________________________________ template