@@ -756,157 +756,6 @@ namespace dsf {
756756 }
757757 }
758758
759- void RoadNetwork::importMatrix (const std::string& fileName,
760- bool isAdj,
761- double defaultSpeed) {
762- // check the file extension
763- std::string fileExt = fileName.substr (fileName.find_last_of (" ." ) + 1 );
764- if (fileExt == " dsf" ) {
765- std::ifstream file{fileName};
766- if (!file.is_open ()) {
767- throw std::runtime_error (" Error opening file \" " + fileName + " \" for reading." );
768- }
769- Size rows, cols;
770- file >> rows >> cols;
771- if (rows != cols) {
772- throw std::invalid_argument (" Adjacency matrix must be square" );
773- }
774- Size n{rows};
775- addNDefaultNodes<Intersection>(n);
776- // each line has 2 elements
777- while (!file.eof ()) {
778- Id index;
779- double val;
780- file >> index >> val;
781- const auto srcId{static_cast <Id>(index / n)};
782- const auto dstId{static_cast <Id>(index % n)};
783- if (isAdj) {
784- addStreet (Street (index, std::make_pair (srcId, dstId)));
785- } else {
786- addStreet (Street (index, std::make_pair (srcId, dstId), val));
787- }
788- edge (index)->setMaxSpeed (defaultSpeed);
789- }
790- } else {
791- // default case: read the file as a matrix with the first two elements being the number of rows and columns and
792- // the following elements being the matrix elements
793- std::ifstream file{fileName};
794- if (!file.is_open ()) {
795- throw std::runtime_error (" Error opening file \" " + fileName + " \" for reading." );
796- }
797- Size rows, cols;
798- file >> rows >> cols;
799- if (rows != cols) {
800- throw std::invalid_argument (
801- " Adjacency matrix must be square. Rows: " + std::to_string (rows) +
802- " Cols: " + std::to_string (cols));
803- }
804- Size n{rows};
805- addNDefaultNodes<Intersection>(n);
806- if (n * n > std::numeric_limits<Id>::max ()) {
807- throw std::invalid_argument (
808- " Matrix size is too large for the current type of Id." );
809- }
810- Id index{0 };
811- while (!file.eof ()) {
812- double value;
813- file >> value;
814- if (value < 0 ) {
815- throw std::invalid_argument (
816- std::format (" Element at index {} is negative ({})." , index, value));
817- }
818- if (value > 0 ) {
819- const auto srcId{static_cast <Id>(index / n)};
820- const auto dstId{static_cast <Id>(index % n)};
821- if (isAdj) {
822- addStreet (Street (index, std::make_pair (srcId, dstId)));
823- } else {
824- addStreet (Street (index, std::make_pair (srcId, dstId), value));
825- }
826- edge (index)->setMaxSpeed (defaultSpeed);
827- }
828- ++index;
829- }
830- }
831- this ->m_updateMaxAgentCapacity ();
832- }
833-
834- void RoadNetwork::importCoordinates (const std::string& fileName) {
835- std::string fileExt = fileName.substr (fileName.find_last_of (" ." ) + 1 );
836- if (fileExt == " dsf" ) {
837- // first input number is the number of nodes
838- std::ifstream file{fileName};
839- if (!file.is_open ()) {
840- throw std::runtime_error (" Error opening file \" " + fileName + " \" for reading." );
841- }
842- Size n;
843- file >> n;
844- if (n < this ->nNodes ()) {
845- throw std::invalid_argument (" Number of node coordinates in file is too small." );
846- }
847- double lat, lon;
848- for (Size i{0 }; i < n; ++i) {
849- file >> lon >> lat;
850- try {
851- node (i)->setGeometry ({lon, lat});
852- } catch (...) {
853- spdlog::warn (std::format (" Node with id {} not found." , i));
854- }
855- }
856- } else if (fileExt == " csv" ) {
857- std::ifstream ifs{fileName};
858- if (!ifs.is_open ()) {
859- throw std::runtime_error (" Error opening file \" " + fileName + " \" for reading." );
860- }
861- // Check if the first line is nodeId;lat;lon
862- std::string line;
863- std::getline (ifs, line);
864- if (line != " id;lat;lon;type" ) {
865- throw std::invalid_argument (" Invalid file format." );
866- }
867- double dLat, dLon;
868- while (!ifs.eof ()) {
869- std::getline (ifs, line);
870- if (line.empty ()) {
871- continue ;
872- }
873- std::istringstream iss{line};
874- std::string nodeId, lat, lon, type;
875- std::getline (iss, nodeId, ' ;' );
876- std::getline (iss, lat, ' ;' );
877- std::getline (iss, lon, ' ;' );
878- std::getline (iss, type, ' \n ' );
879- dLon = lon == " Nan" ? 0 . : std::stod (lon);
880- dLat = lat == " Nan" ? 0 . : std::stod (lat);
881- auto const & nodes{this ->nodes ()};
882- auto it = nodes.find (std::stoul (nodeId));
883- if (it != nodes.cend ()) {
884- auto const & pNode{it->second };
885- pNode->setGeometry (dsf::geometry::Point (dLon, dLat));
886- if (type == " traffic_light" && !pNode->isTrafficLight ()) {
887- makeTrafficLight (it->first , 60 );
888- } else if (type == " roundabout" && !pNode->isRoundabout ()) {
889- makeRoundabout (it->first );
890- }
891- } else {
892- spdlog::warn (" Node with id {} not found. Skipping coordinates ({}, {})." ,
893- nodeId,
894- dLat,
895- dLon);
896- }
897- }
898- } else {
899- throw std::invalid_argument (
900- std::format (" File extension ({}) not supported" , fileExt));
901- }
902- for (auto const & [_, pEdge] : edges ()) {
903- auto const & pSourceNode{node (pEdge->source ())};
904- auto const & pTargetNode{node (pEdge->target ())};
905- pEdge->setGeometry (
906- geometry::PolyLine{*(pSourceNode->geometry ()), *(pTargetNode->geometry ())});
907- }
908- }
909-
910759 void RoadNetwork::importTrafficLights (const std::string& fileName) {
911760 std::ifstream file{fileName};
912761 if (!file.is_open ()) {
@@ -951,24 +800,6 @@ namespace dsf {
951800 }
952801 }
953802
954- void RoadNetwork::exportMatrix (std::string path, bool isAdj) {
955- std::ofstream file{path};
956- if (!file.is_open ()) {
957- throw std::runtime_error (" Error opening file \" " + path + " \" for writing." );
958- }
959- auto const N{nNodes ()};
960- file << N << ' \t ' << N;
961- if (isAdj) {
962- std::for_each (m_edges.cbegin (), m_edges.cend (), [&N, &file](auto const & pair) {
963- file << ' \n ' << pair.second ->source () * N + pair.second ->target () << ' \t ' << 1 ;
964- });
965- } else {
966- std::for_each (m_edges.cbegin (), m_edges.cend (), [&N, &file](auto const & pair) {
967- file << ' \n ' << pair.second ->id () << ' \t ' << pair.second ->length ();
968- });
969- }
970- }
971-
972803 TrafficLight& RoadNetwork::makeTrafficLight (Id const nodeId,
973804 Delay const cycleTime,
974805 Delay const counter) {
0 commit comments