diff --git a/src/SaxParser.cpp b/src/SaxParser.cpp index 7aeb42fb1174b3f16ad691ae44cde4d68593535b..532458ebea6a3fc3f1188b33a966bb0c1dc78ade 100644 --- a/src/SaxParser.cpp +++ b/src/SaxParser.cpp @@ -601,12 +601,13 @@ bool SaxParser::parseGeometryJPS(QString fileName, FacilityGeometry *geometry) VTK_CREATE(vtkCellArray,floor_polygons); VTK_CREATE(vtkCellArray,obstacles_polygons); - for (int i = 0; i < building->GetNumberOfRooms(); i++) { - Room* r = building->GetRoom(i); - //string caption = r->GetCaption(); + for(auto&& itr_room: building->GetAllRooms()) + { + for(auto&& itr_subroom: itr_room.second->GetAllSubRooms()) + { - for (int k = 0; k < r->GetNumberOfSubRooms(); k++) { - SubRoom* sub = r->GetSubRoom(k); + //string caption = r->GetCaption(); + SubRoom* sub = itr_subroom.second.get(); vector<Point> poly = sub->GetPolygon(); if(sub->IsClockwise()==true) { @@ -639,7 +640,7 @@ bool SaxParser::parseGeometryJPS(QString fileName, FacilityGeometry *geometry) } //insert the subroom caption - string caption=r->GetCaption()+" ( " + QString::number(sub->GetSubRoomID()).toStdString() + " ) "; + string caption=itr_room.second->GetCaption()+" ( " + QString::number(sub->GetSubRoomID()).toStdString() + " ) "; const Point& p=sub->GetCentroid(); double z= sub->GetElevation(p); double pos[3]= {p._x*FAKTOR,p._y*FAKTOR,z*FAKTOR}; diff --git a/src/TimerCallback.cpp b/src/TimerCallback.cpp index e5e71f43bdaef9a34fa808b3864892f8f39bab9d..708e2110cb6ae541a22a2800798789f2f2aae5a1 100644 --- a/src/TimerCallback.cpp +++ b/src/TimerCallback.cpp @@ -45,7 +45,6 @@ #define TRAVISTO_FFMPEG #endif - #ifdef __linux__ #ifdef __vtkFFMPEGWriter_h #include <vtkFFMPEGWriter.h> diff --git a/src/geometry/Building.cpp b/src/geometry/Building.cpp index 6e8cf45d4f704850cc8ed1e185464e9ce15e18ed..ea9c3e00a7aa61fe30292524ff51660cbe60f43f 100644 --- a/src/geometry/Building.cpp +++ b/src/geometry/Building.cpp @@ -56,7 +56,6 @@ Building::Building() _caption = "no_caption"; _projectFilename = ""; _geometryFilename= ""; - _rooms = vector<Room*>(); _routingEngine = nullptr; _linkedCellGrid = nullptr; _savePathway = false; @@ -64,10 +63,9 @@ Building::Building() #ifdef _SIMULATOR Building::Building(const std::string& filename, const std::string& rootDir, RoutingEngine& engine, PedDistributor& distributor, double linkedCellSize) - :_projectFilename(filename), _projectRootDir(rootDir), _routingEngine(&engine) +:_projectFilename(filename), _projectRootDir(rootDir), _routingEngine(&engine) { _caption = "no_caption"; - _rooms = vector<Room*>(); _savePathway = false; _linkedCellGrid = nullptr; @@ -82,42 +80,43 @@ Building::Building(const std::string& filename, const std::string& rootDir, Rout _routingEngine->Init(this); this->SanityCheck(); } -#endif //Simulator +#endif Building::~Building() { - for (int i = 0; i < GetNumberOfRooms(); i++) - delete _rooms[i]; + // + // for (int i = 0; i < GetNumberOfRooms(); i++) + // delete _rooms[i]; #ifdef _SIMULATOR - for(unsigned int p=0;p<_allPedestians.size();p++) - { - //delete _allPedestians[p]; - } - _allPedestians.clear(); - delete _linkedCellGrid; + for(unsigned int p=0;p<_allPedestians.size();p++) + { + //delete _allPedestians[p]; + } + _allPedestians.clear(); + delete _linkedCellGrid; #endif - if (_pathWayStream.is_open()) - _pathWayStream.close(); - - - for (map<int, Crossing*>::const_iterator iter = _crossings.begin(); - iter != _crossings.end(); ++iter) { - delete iter->second; - } - for (map<int, Transition*>::const_iterator iter = _transitions.begin(); - iter != _transitions.end(); ++iter) { - delete iter->second; - } - for (map<int, Hline*>::const_iterator iter = _hLines.begin(); - iter != _hLines.end(); ++iter) { - delete iter->second; - } - for (map<int, Goal*>::const_iterator iter = _goals.begin(); - iter != _goals.end(); ++iter) { - delete iter->second; - } + if (_pathWayStream.is_open()) + _pathWayStream.close(); + + + for (map<int, Crossing*>::const_iterator iter = _crossings.begin(); + iter != _crossings.end(); ++iter) { + delete iter->second; + } + for (map<int, Transition*>::const_iterator iter = _transitions.begin(); + iter != _transitions.end(); ++iter) { + delete iter->second; + } + for (map<int, Hline*>::const_iterator iter = _hLines.begin(); + iter != _hLines.end(); ++iter) { + delete iter->second; + } + for (map<int, Goal*>::const_iterator iter = _goals.begin(); + iter != _goals.end(); ++iter) { + delete iter->second; + } } /************************************************************ @@ -133,17 +132,6 @@ void Building::SetRoutingEngine(RoutingEngine* r) _routingEngine = r; } -void Building::SetRoom(Room* room, int index) -{ - if ((index >= 0) && (index < (int) _rooms.size())) { - _rooms[index] = room; - } else { - Log->Write("ERROR: \tWrong Index in CBuilding::SetRoom()"); - exit(EXIT_FAILURE); - } -} - - /************************************************************* Getter-Funktionen ************************************************************/ @@ -168,21 +156,22 @@ int Building::GetNumberOfGoals() const return _transitions.size() + _hLines.size() + _crossings.size(); } -const vector<Room*>& Building::GetAllRooms() const +const std::map<int, std::unique_ptr<Room>>& Building::GetAllRooms() const { return _rooms; } Room* Building::GetRoom(int index) const { - if ((index >= 0) && (index < (int) _rooms.size())) { - return _rooms[index]; - } else { + //todo: obsolete since the check is done by .at() + if(_rooms.count(index)==0) + { Log->Write("ERROR: Wrong 'index' in CBuiling::GetRoom() Room ID: %d size: %d",index, _rooms.size()); Log->Write("\tControl your rooms ID and make sure they are in the order 0, 1, 2,.. "); - //exit(EXIT_FAILURE); - return NULL; + return nullptr; } + //return _rooms[index]; + return _rooms.at(index).get(); } @@ -193,7 +182,7 @@ LCGrid* Building::GetGrid() const void Building::AddRoom(Room* room) { - _rooms.push_back(room); + _rooms[room->GetID()]=std::unique_ptr<Room>(room); } void Building::AddSurroundingRoom() @@ -206,17 +195,17 @@ void Building::AddSurroundingRoom() double y_max = -FLT_MAX; //finding the bounding of the grid // and collect the pedestrians - for (unsigned int r = 0; r < _rooms.size(); r++) { - Room* room = _rooms[r]; - for (int j = 0; j < room->GetNumberOfSubRooms(); j++) { - SubRoom* sub = room->GetSubRoom(j); - const vector<Wall>& allWalls = sub->GetAllWalls(); - - for (unsigned int a = 0; a < allWalls.size(); a++) { - double x1 = allWalls[a].GetPoint1().GetX(); - double y1 = allWalls[a].GetPoint1().GetY(); - double x2 = allWalls[a].GetPoint2().GetX(); - double y2 = allWalls[a].GetPoint2().GetY(); + + for(auto&& itr_room: _rooms) + { + for(auto&& itr_subroom: itr_room.second->GetAllSubRooms()) + { + for (auto&& wall:itr_subroom.second->GetAllWalls()) + { + double x1 = wall.GetPoint1().GetX(); + double y1 = wall.GetPoint1().GetY(); + double x2 = wall.GetPoint2().GetX(); + double y2 = wall.GetPoint2().GetY(); double xmax = (x1 > x2) ? x1 : x2; double xmin = (x1 > x2) ? x2 : x1; @@ -231,16 +220,14 @@ void Building::AddSurroundingRoom() } } - for (map<int, Goal*>::const_iterator itr = _goals.begin(); - itr != _goals.end(); ++itr) { - - const vector<Wall>& allWalls = itr->second->GetAllWalls(); - - for (unsigned int a = 0; a < allWalls.size(); a++) { - double x1 = allWalls[a].GetPoint1().GetX(); - double y1 = allWalls[a].GetPoint1().GetY(); - double x2 = allWalls[a].GetPoint2().GetX(); - double y2 = allWalls[a].GetPoint2().GetY(); + for(auto&& itr_goal:_goals) + { + for(auto&& wall: itr_goal.second->GetAllWalls()) + { + double x1 = wall.GetPoint1().GetX(); + double y1 = wall.GetPoint1().GetY(); + double x2 = wall.GetPoint2().GetX(); + double y2 = wall.GetPoint2().GetY(); double xmax = (x1 > x2) ? x1 : x2; double xmin = (x1 > x2) ? x2 : x1; @@ -253,7 +240,6 @@ void Building::AddSurroundingRoom() y_min = (ymin <= y_min) ? ymin : y_min; } } - //make the grid slightly larger. x_min = x_min - 10.0; x_max = x_max + 10.0; @@ -278,45 +264,40 @@ void Building::AddSurroundingRoom() bool Building::InitGeometry() { - int r; - unsigned int i; Log->Write("INFO: \tInit Geometry"); - for (r = 0; r < GetNumberOfRooms(); r++) { - Room* room = GetRoom(r); - // Polygone berechnen - for (int j = 0; j < room->GetNumberOfSubRooms(); j++) { - SubRoom* s = room->GetSubRoom(j); - // Alle Übergänge in diesem Raum bestimmen - // Übergänge müssen zu Wänden ergänzt werden + + for(auto&& itr_room: _rooms) + { + for(auto&& itr_subroom: itr_room.second->GetAllSubRooms()) + { + //create a close polyline out of everything vector<Line*> goals = vector<Line*>(); - // crossings - const vector<Crossing*>& crossings = s->GetAllCrossings(); - for (i = 0; i < crossings.size(); i++) { - goals.push_back(crossings[i]); + // collect all crossings + for(auto&& cros:itr_subroom.second->GetAllCrossings()) + { + goals.push_back(cros); } - - // and transitions - const vector<Transition*>& transitions = s->GetAllTransitions(); - for (i = 0; i < transitions.size(); i++) { - goals.push_back(transitions[i]); + //collect all transitions + for(auto&& trans:itr_subroom.second->GetAllTransitions()) + { + goals.push_back(trans); } - // initialize the poly - if(! s->ConvertLineToPoly(goals)) - return false; - s->CalculateArea(); - goals.clear(); + if(! itr_subroom.second->ConvertLineToPoly(goals)) + return false; + itr_subroom.second->CalculateArea(); //do the same for the obstacles that are closed - const vector<Obstacle*>& obstacles = s->GetAllObstacles(); - for (unsigned int obs = 0; obs < obstacles.size(); ++obs) { - if (obstacles[obs]->GetClosed() == 1) - if(!obstacles[obs]->ConvertLineToPoly()) - return false; + for(auto&& obst:itr_subroom.second->GetAllObstacles()) + { + if (obst->GetClosed() == 1) + if(!obst->ConvertLineToPoly()) + return false; } } } + Log->Write("INFO: \tInit Geometry successful!!!\n"); return true; @@ -418,6 +399,7 @@ bool Building::LoadGeometry(const std::string &geometryfile) xRoom = xRoom->NextSiblingElement("room")) { Room* room = new Room(); + //make_unique<Song> string room_id = xmltoa(xRoom->Attribute("id"), "-1"); room->SetID(xmltoi(room_id.c_str(), -1)); @@ -612,12 +594,12 @@ bool Building::LoadGeometry(const std::string &geometryfile) subroom->AddTransition(t); } if (room2_id != -1 && subroom2_id != -1) { - Room* room = _rooms[room2_id]; + auto&& room = _rooms[room2_id]; SubRoom* subroom = room->GetSubRoom(subroom2_id); //subroom->AddGoalID(t->GetUniqueID()); //MPI room->AddTransitionID(t->GetUniqueID()); - t->SetRoom2(room); + t->SetRoom2(room.get()); t->SetSubRoom2(subroom); //new implementation @@ -660,9 +642,10 @@ void Building::WriteToErrorLog() const Room* Building::GetRoom(string caption) const { - for (unsigned int r = 0; r < _rooms.size(); r++) { - if (_rooms[r]->GetCaption() == caption) - return _rooms[r]; + for(const auto& it: _rooms) + { + if(it.second->GetCaption()==caption) + return it.second.get(); } Log->Write("ERROR: Room not found with caption " + caption); //return NULL; @@ -674,8 +657,8 @@ void Building::AddCrossing(Crossing* line) if (_crossings.count(line->GetID()) != 0) { char tmp[CLENGTH]; sprintf(tmp, - "ERROR: Duplicate index for crossing found [%d] in Routing::AddCrossing()", - line->GetID()); + "ERROR: Duplicate index for crossing found [%d] in Routing::AddCrossing()", + line->GetID()); Log->Write(tmp); exit(EXIT_FAILURE); } @@ -687,8 +670,8 @@ void Building::AddTransition(Transition* line) if (_transitions.count(line->GetID()) != 0) { char tmp[CLENGTH]; sprintf(tmp, - "ERROR: Duplicate index for transition found [%d] in Routing::AddTransition()", - line->GetID()); + "ERROR: Duplicate index for transition found [%d] in Routing::AddTransition()", + line->GetID()); Log->Write(tmp); exit(EXIT_FAILURE); } @@ -705,8 +688,8 @@ void Building::AddHline(Hline* line) return; } else { Log->Write( - "ERROR: Duplicate index for hlines found [%d] in Routing::AddHline(). You have [%d] hlines", - line->GetID(), _hLines.size()); + "ERROR: Duplicate index for hlines found [%d] in Routing::AddHline(). You have [%d] hlines", + line->GetID(), _hLines.size()); exit(EXIT_FAILURE); } } @@ -717,8 +700,8 @@ void Building::AddGoal(Goal* goal) { if (_goals.count(goal->GetId()) != 0) { Log->Write( - "ERROR: Duplicate index for goal found [%d] in Routing::AddGoal()", - goal->GetId()); + "ERROR: Duplicate index for goal found [%d] in Routing::AddGoal()", + goal->GetId()); exit(EXIT_FAILURE); } _goals[goal->GetId()] = goal; @@ -766,8 +749,8 @@ Transition* Building::GetTransition(int ID) return NULL; else { Log->Write( - "ERROR: I could not find any transition with the 'ID' [%d]. You have defined [%d] transitions", - ID, _transitions.size()); + "ERROR: I could not find any transition with the 'ID' [%d]. You have defined [%d] transitions", + ID, _transitions.size()); exit(EXIT_FAILURE); } } @@ -782,8 +765,8 @@ Goal* Building::GetFinalGoal(int ID) return NULL; else { Log->Write( - "ERROR: I could not find any goal with the 'ID' [%d]. You have defined [%d] goals", - ID, _goals.size()); + "ERROR: I could not find any goal with the 'ID' [%d]. You have defined [%d] goals", + ID, _goals.size()); exit(EXIT_FAILURE); } } @@ -848,11 +831,12 @@ Hline* Building::GetTransOrCrossByUID(int id) const SubRoom* Building::GetSubRoomByUID( int uid) { - for (unsigned int i = 0; i < _rooms.size(); i++) { - Room* room = _rooms[i]; - for (int j = 0; j < room->GetNumberOfSubRooms(); j++) { - SubRoom* sub = room->GetSubRoom(j); - if (sub->GetUID()==uid) return sub;; + for(auto&& itr_room: _rooms) + { + for(auto&& itr_subroom: itr_room.second->GetAllSubRooms()) + { + if(itr_subroom.second->GetUID()==uid) + return itr_subroom.second.get(); } } Log->Write("ERROR:\t No subroom exits with the unique id %d",uid); @@ -861,11 +845,12 @@ SubRoom* Building::GetSubRoomByUID( int uid) bool Building::IsVisible(Line* l1, Line* l2, bool considerHlines) { - for (unsigned int i = 0; i < _rooms.size(); i++) { - Room* room = _rooms[i]; - for (int j = 0; j < room->GetNumberOfSubRooms(); j++) { - SubRoom* sub = room->GetSubRoom(j); - if(sub->IsVisible(l1,l2,considerHlines)==false) return false; + + for(auto&& itr_room: _rooms) + { + for(auto&& itr_subroom: itr_room.second->GetAllSubRooms()) + { + if(itr_subroom.second->IsVisible(l1,l2,considerHlines)==false) return false; } } return true; @@ -873,13 +858,14 @@ bool Building::IsVisible(Line* l1, Line* l2, bool considerHlines) bool Building::IsVisible(const Point& p1, const Point& p2, bool considerHlines) { - for (unsigned int i = 0; i < _rooms.size(); i++) { - Room* room = _rooms[i]; - for (int j = 0; j < room->GetNumberOfSubRooms(); j++) { - SubRoom* sub = room->GetSubRoom(j); - if(sub->IsVisible(p1,p2,considerHlines)==false) return false; + for(auto&& itr_room: _rooms) + { + for(auto&& itr_subroom: itr_room.second->GetAllSubRooms()) + { + if(itr_subroom.second->IsVisible(p1,p2,considerHlines)==false) return false; } } + return true; } @@ -887,15 +873,16 @@ bool Building::SanityCheck() { Log->Write("INFO: \tChecking the geometry for artifacts"); bool status = true; - for (unsigned int i = 0; i < _rooms.size(); i++) { - Room* room = _rooms[i]; - for (int j = 0; j < room->GetNumberOfSubRooms(); j++) { - SubRoom* sub = room->GetSubRoom(j); - if (!sub->SanityCheck()) + for(auto&& itr_room: _rooms) + { + for(auto&& itr_subroom: itr_room.second->GetAllSubRooms()) + { + if (!itr_subroom.second->SanityCheck()) status = false; } } + Log->Write("INFO: \t...Done!!!\n"); return status; } @@ -919,17 +906,16 @@ void Building::InitGrid(double cellSize) //finding the bounding of the grid // and collect the pedestrians - for (unsigned int r = 0; r < _rooms.size(); r++) { - Room* room = _rooms[r]; - for (int j = 0; j < room->GetNumberOfSubRooms(); j++) { - SubRoom* sub = room->GetSubRoom(j); - const vector<Wall>& allWalls = sub->GetAllWalls(); - - for (unsigned int a = 0; a < allWalls.size(); a++) { - double x1 = allWalls[a].GetPoint1().GetX(); - double y1 = allWalls[a].GetPoint1().GetY(); - double x2 = allWalls[a].GetPoint2().GetX(); - double y2 = allWalls[a].GetPoint2().GetY(); + for(auto&& itr_room: _rooms) + { + for(auto&& itr_subroom: itr_room.second->GetAllSubRooms()) + { + for (auto&& wall:itr_subroom.second->GetAllWalls()) + { + double x1 = wall.GetPoint1().GetX(); + double y1 = wall.GetPoint1().GetY(); + double x2 = wall.GetPoint2().GetX(); + double y2 = wall.GetPoint2().GetY(); double xmax = (x1 > x2) ? x1 : x2; double xmin = (x1 > x2) ? x2 : x1; @@ -1016,7 +1002,7 @@ bool Building::LoadRoutingInfo(const string &filename) xPolyVertices = xPolyVertices->NextSiblingElement("polygon")) { for (TiXmlElement* xVertex = xPolyVertices->FirstChildElement( - "vertex"); + "vertex"); xVertex && xVertex != xPolyVertices->LastChild("vertex"); xVertex = xVertex->NextSiblingElement("vertex")) { @@ -1029,7 +1015,7 @@ bool Building::LoadRoutingInfo(const string &filename) } if(!goal->ConvertLineToPoly()) - return false; + return false; AddGoal(goal); _routingEngine->AddFinalDestinationID(goal->GetId()); @@ -1223,7 +1209,7 @@ void Building::InitSavePedPathway(const string &filename) void Building::StringExplode(string str, string separator, - vector<string>* results) + vector<string>* results) { size_t found; found = str.find_first_of(separator); @@ -1276,37 +1262,37 @@ bool Building::SaveGeometry(const std::string &filename) //write the rooms geometry<<"<rooms>"<<endl; - for (Room* room : _rooms) + for (auto&& itroom : _rooms) { + auto&& room=itroom.second; geometry<<"\t<room id =\""<<room->GetID()<<"\" caption =\""<<room->GetCaption()<<"\">"<<endl; - const vector<SubRoom*>& allSubs=room->GetAllSubRooms(); - for(SubRoom* sub : allSubs) + for(auto&& itr_sub : room->GetAllSubRooms()) { + auto&& sub=itr_sub.second; const double* plane=sub->GetPlaneEquation(); geometry<<"\t\t<subroom id =\""<<sub->GetSubRoomID() - <<"\" closed=\""<<sub->GetClosed() - <<"\" class=\""<<sub->GetType() - <<"\" A_x=\""<<plane[0] - <<"\" B_y=\""<<plane[1] - <<"\" C_z=\""<<plane[2]<<"\">"<<endl; + <<"\" closed=\""<<sub->GetClosed() + <<"\" class=\""<<sub->GetType() + <<"\" A_x=\""<<plane[0] + <<"\" B_y=\""<<plane[1] + <<"\" C_z=\""<<plane[2]<<"\">"<<endl; - const vector<Wall>& walls=sub->GetAllWalls(); - for (Wall wall : walls) + for (auto&& wall : sub->GetAllWalls()) { const Point& p1=wall.GetPoint1(); const Point& p2=wall.GetPoint2(); geometry<<"\t\t\t<polygon caption=\"wall\" type=\""<<wall.GetType()<<"\">"<<endl - <<"\t\t\t\t<vertex px=\""<<p1._x<<"\" py=\""<<p1._y<<"\"/>"<<endl - <<"\t\t\t\t<vertex px=\""<<p2._x<<"\" py=\""<<p2._y<<"\"/>"<<endl - <<"\t\t\t</polygon>"<<endl; + <<"\t\t\t\t<vertex px=\""<<p1._x<<"\" py=\""<<p1._y<<"\"/>"<<endl + <<"\t\t\t\t<vertex px=\""<<p2._x<<"\" py=\""<<p2._y<<"\"/>"<<endl + <<"\t\t\t</polygon>"<<endl; } if(sub->GetType()=="stair") { - const Point& up = ((Stair*)sub)->GetUp(); - const Point& down = ((Stair*)sub)->GetDown(); + const Point& up = ((Stair*)sub.get())->GetUp(); + const Point& down = ((Stair*)sub.get())->GetDown(); geometry<<"\t\t\t<up px=\""<<up._x<<"\" py=\""<<up._y<<"\"/>"<<endl; geometry<<"\t\t\t<down px=\""<<down._x<<"\" py=\""<<down._y<<"\"/>"<<endl; } @@ -1317,23 +1303,23 @@ bool Building::SaveGeometry(const std::string &filename) //write the crossings geometry<<"\t\t<crossings>"<<endl; for (auto const& mapcross : _crossings) - { - Crossing* cross=mapcross.second; + { + Crossing* cross=mapcross.second; - //only write the crossings in this rooms - if(cross->GetRoom1()->GetID()!=room->GetID()) continue; + //only write the crossings in this rooms + if(cross->GetRoom1()->GetID()!=room->GetID()) continue; - const Point& p1=cross->GetPoint1(); - const Point& p2=cross->GetPoint2(); + const Point& p1=cross->GetPoint1(); + const Point& p2=cross->GetPoint2(); - geometry<<"\t<crossing id =\""<<cross->GetID() - <<"\" subroom1_id=\""<<cross->GetSubRoom1()->GetSubRoomID() - <<"\" subroom2_id=\""<<cross->GetSubRoom2()->GetSubRoomID()<<"\">"<<endl; + geometry<<"\t<crossing id =\""<<cross->GetID() + <<"\" subroom1_id=\""<<cross->GetSubRoom1()->GetSubRoomID() + <<"\" subroom2_id=\""<<cross->GetSubRoom2()->GetSubRoomID()<<"\">"<<endl; - geometry<<"\t\t<vertex px=\""<<p1._x<<"\" py=\""<<p1._y<<"\"/>"<<endl - <<"\t\t<vertex px=\""<<p2._x<<"\" py=\""<<p2._y<<"\"/>"<<endl - <<"\t</crossing>"<<endl; - } + geometry<<"\t\t<vertex px=\""<<p1._x<<"\" py=\""<<p1._y<<"\"/>"<<endl + <<"\t\t<vertex px=\""<<p2._x<<"\" py=\""<<p2._y<<"\"/>"<<endl + <<"\t</crossing>"<<endl; + } geometry<<"\t\t</crossings>"<<endl; geometry<<"\t</room>"<<endl; } @@ -1357,16 +1343,16 @@ bool Building::SaveGeometry(const std::string &filename) } geometry<<"\t<transition id =\""<<trans->GetID() - <<"\" caption=\""<<trans->GetCaption() - <<"\" type=\""<<trans->GetType() - <<"\" room1_id=\""<<trans->GetRoom1()->GetID() - <<"\" subroom1_id=\""<<trans->GetSubRoom1()->GetSubRoomID() - <<"\" room2_id=\""<<room2_id - <<"\" subroom2_id=\""<<subroom2_id<<"\">"<<endl; + <<"\" caption=\""<<trans->GetCaption() + <<"\" type=\""<<trans->GetType() + <<"\" room1_id=\""<<trans->GetRoom1()->GetID() + <<"\" subroom1_id=\""<<trans->GetSubRoom1()->GetSubRoomID() + <<"\" room2_id=\""<<room2_id + <<"\" subroom2_id=\""<<subroom2_id<<"\">"<<endl; geometry<<"\t\t<vertex px=\""<<p1._x<<"\" py=\""<<p1._y<<"\"/>"<<endl - <<"\t\t<vertex px=\""<<p2._x<<"\" py=\""<<p2._y<<"\"/>"<<endl - <<"\t</transition>"<<endl; + <<"\t\t<vertex px=\""<<p2._x<<"\" py=\""<<p2._y<<"\"/>"<<endl + <<"\t</transition>"<<endl; } diff --git a/src/geometry/Building.h b/src/geometry/Building.h index 932fb93e209ba05daff3f68ae4e047931aa57ed9..9e876244283bff38fc960fdd8e239fd52d80f3b1 100644 --- a/src/geometry/Building.h +++ b/src/geometry/Building.h @@ -34,6 +34,7 @@ #include <fstream> #include <cfloat> #include <map> +#include <memory> #include "Room.h" #include "NavLine.h" @@ -49,7 +50,6 @@ class LCGrid; class ForceModel; class PedDistributor; - class Building { private: @@ -59,10 +59,9 @@ private: std::string _geometryFilename; RoutingEngine* _routingEngine; LCGrid* _linkedCellGrid; - //TODO: change the type to (unorder) map <int , Room*> - std::vector<Room*> _rooms; std::vector<Pedestrian*> _allPedestians; + std::map<int, std::unique_ptr<Room> > _rooms; std::map<int, Crossing*> _crossings; std::map<int, Transition*> _transitions; std::map<int, Hline*> _hLines; @@ -81,7 +80,6 @@ public: void SetCaption(const std::string& s); void SetRoutingEngine(RoutingEngine* r); - void SetRoom(Room* room, int index); /// delete the ped from the ped vector void DeletePedestrian(Pedestrian* &ped); @@ -91,7 +89,7 @@ public: std::string GetCaption() const; RoutingEngine* GetRoutingEngine() const; - const std::vector<Room*>& GetAllRooms() const; + const std::map<int, std::unique_ptr<Room>>& GetAllRooms() const; const std::vector<Pedestrian*>& GetAllPedestrians() const; Pedestrian* GetPedestrian( int pedID) const; int GetNumberOfRooms() const; diff --git a/src/geometry/Line.cpp b/src/geometry/Line.cpp index 0ed90c3e14cad08125da2508c520ac0488628a65..e6383fdd0cf04d3b814348a9a8f9d1f30f751772 100644 --- a/src/geometry/Line.cpp +++ b/src/geometry/Line.cpp @@ -307,7 +307,6 @@ bool Line::operator==(const Line& l) const /* Zwei Linien sind ungleich, wenn ihre beiden Punkte * ungleich sind. - * TODO: check this * */ bool Line::operator!=(const Line& l) const { @@ -325,53 +324,50 @@ double Line::LengthSquare() const return (_point1 - _point2).NormSquare(); } +bool Line::IntersectionWith(const Point &p1, const Point &p2) const +{ + double deltaACy = _point1.GetY() - p1.GetY(); + double deltaDCx = p2.GetX() - p1.GetX(); + double deltaACx = _point1.GetX() - p1.GetX(); + double deltaDCy = p2.GetY() - p1.GetY(); + double deltaBAx = _point2.GetX() - _point1.GetX(); + double deltaBAy = _point2.GetY() - _point1.GetY(); + double denominator = deltaBAx * deltaDCy - deltaBAy * deltaDCx; + double numerator = deltaACy * deltaDCx - deltaACx * deltaDCy; + + double r = numerator / denominator; + if (r < 0.0 || r > 1.0) { + return false; + } + + double s = (deltaACy * deltaBAx - deltaACx * deltaBAy) / denominator; + if (s < 0.0 || s > 1.0) { + return false; + } + + if (denominator == 0.0) { + + // the lines are superposed + if (numerator == 0.0) { + + // the segment are superposed + if(IsInLineSegment(p1) || + IsInLineSegment(p2) ) return true; + else return false; + + } else { // the lines are just parallel and do not share a common point + + return false; + } + } + + return true; + +} + bool Line::IntersectionWith(const Line& l) const { - - //if(ShareCommonPointWith(l)) return true; - - double deltaACy = _point1.GetY() - l.GetPoint1().GetY(); - double deltaDCx = l.GetPoint2().GetX() - l.GetPoint1().GetX(); - double deltaACx = _point1.GetX() - l.GetPoint1().GetX(); - double deltaDCy = l.GetPoint2().GetY() - l.GetPoint1().GetY(); - double deltaBAx = _point2.GetX() - _point1.GetX(); - double deltaBAy = _point2.GetY() - _point1.GetY(); - - double denominator = deltaBAx * deltaDCy - deltaBAy * deltaDCx; - double numerator = deltaACy * deltaDCx - deltaACx * deltaDCy; - - // the lines are parallel - if (denominator == 0.0) { - - // the lines are superposed - if (numerator == 0.0) { - - // the segment are superposed - if(IsInLineSegment(l.GetPoint1()) || - IsInLineSegment(l.GetPoint2()) ) return true; - else return false; - - } else { // the lines are just parallel and do not share a common point - - return false; - } - } - - // the lines intersect - double r = numerator / denominator; - if (r < 0.0 || r > 1.0) { - return false; - } - - double s = (deltaACy * deltaBAx - deltaACx * deltaBAy) / denominator; - if (s < 0.0 || s > 1.0) { - return false; - } - - //Point PointF = Point ((float) (_point1._x + r * deltaBAx), (float) (_point1._y + r * deltaBAy)); - //cout<< l.toString() << " intersects with " << toString() <<endl; - //cout<<" at point " << PointF.toString()<<endl; - return true; + return this->IntersectionWith(l._point1, l._point2); } Line Line::enlarge(double d) const diff --git a/src/geometry/Line.h b/src/geometry/Line.h index bf4505edae5f78a2bc893f375dcb07f3fd4584c7..8467c13cf6b476046793916d05922e970bd4f580 100644 --- a/src/geometry/Line.h +++ b/src/geometry/Line.h @@ -164,7 +164,8 @@ public: * @return true if both segments intersect */ bool IntersectionWith(const Line& l) const; // check two segments for intersections - + bool IntersectionWith(const Point& p1, const Point&p2) const; + /** * @return the distance squared between the first point and the intersection * point with line l. This is exactly the same function diff --git a/src/geometry/Room.cpp b/src/geometry/Room.cpp index edf3e8af65e0f5452e8e0ae957ca3705fa07e8c0..340156c844ac9af327af991d2d9c48036999f49d 100644 --- a/src/geometry/Room.cpp +++ b/src/geometry/Room.cpp @@ -31,6 +31,7 @@ #include "../IO/OutputHandler.h" #include <sstream> +#include <memory> using namespace std; @@ -45,7 +46,6 @@ Room::Room() _egressTime=0; _caption = "no room caption"; _zPos = -1.0; - _subRooms = vector<SubRoom* > (); _outputFile=NULL; } @@ -54,7 +54,6 @@ Room::Room(const Room& orig) _id = orig.GetID(); _caption = orig.GetCaption(); _zPos = orig.GetZPos(); - _subRooms = orig.GetAllSubRooms(); _state=orig.GetState(); _egressTime=orig.GetEgressTime(); _outputFile=orig.GetOutputHandler(); @@ -62,8 +61,8 @@ Room::Room(const Room& orig) Room::~Room() { - for (unsigned int i = 0; i < _subRooms.size(); i++) - delete _subRooms[i]; + //for (unsigned int i = 0; i < _subRooms.size(); i++) + //delete _subRooms[i]; } /************************************************************* @@ -84,16 +83,6 @@ void Room::SetZPos(double z) _zPos = z; } -void Room::SetSubRoom(SubRoom* subroom, int index) -{ - if ((index >= 0) && (index < GetNumberOfSubRooms())) { - _subRooms[index] = subroom; - } else { - Log->Write("ERROR: Wrong Index in Room::SetSubRoom()"); - exit(0); - } -} - void Room::SetState(RoomState state) { _state=state; @@ -134,22 +123,20 @@ int Room::GetNumberOfSubRooms() const return _subRooms.size(); } -const vector<SubRoom*>& Room::GetAllSubRooms() const +const std::map<int, std::unique_ptr<SubRoom>>& Room::GetAllSubRooms() const { return _subRooms; } SubRoom* Room::GetSubRoom(int index) const { - if ((index >= 0) && (index < (int) _subRooms.size())) - return _subRooms[index]; - else { - char tmp[CLENGTH]; - sprintf(tmp,"ERROR: Room::GetSubRoom() No subroom id [%d] present in room id [%d] ",index,_id); - Log->Write(tmp); - return NULL; - //exit(EXIT_FAILURE); + //todo: the check is done in _subRooms.at(index); + if(_subRooms.count(index)==0) + { + Log->Write("ERROR: Room::GetSubRoom() No subroom id [%d] present in room id [%d] ",index,_id); + return nullptr; } + return _subRooms.at(index).get(); } @@ -169,7 +156,8 @@ const RoomState& Room::GetState() const ************************************************************/ void Room::AddSubRoom(SubRoom* r) { - _subRooms.push_back(r); + //_subRooms.push_back(r); + _subRooms[r->GetSubRoomID()]=std::unique_ptr<SubRoom>(r); } /************************************************************* diff --git a/src/geometry/Room.h b/src/geometry/Room.h index dc92e55a1521d1673998f3f9c1504ac096f45b26..2ffa5b97fc2e0060958a8e200d53eab106243b1d 100644 --- a/src/geometry/Room.h +++ b/src/geometry/Room.h @@ -30,6 +30,7 @@ #include <string> #include <algorithm> +#include <memory> #include "../general/Macros.h" //forward declarations @@ -52,8 +53,7 @@ private: /// room elevation double _zPos; /// all subrooms/partitions of the room - /// TODO: Change the type to (unorder)map <int, SubRoom*> - std::vector<SubRoom*> _subRooms; + std::map<int, std::unique_ptr<SubRoom>> _subRooms; /// all transitions ids std::vector<int> _transitionsIDs; /// needed if the trajectories for this room are to be write in a special way @@ -82,11 +82,6 @@ public: */ void SetZPos(double z); - /** - * Add a SubRoom at the given index - */ - void SetSubRoom(SubRoom* subroom, int index); - /** * Set/Get the state of the room as defined in the macro.h file */ @@ -125,7 +120,7 @@ public: /** * @return a vector containing all subrooms */ - const std::vector<SubRoom*>& GetAllSubRooms() const; + const std::map<int, std::unique_ptr<SubRoom>>& GetAllSubRooms() const; /** * @return a vector containing all transitions Ids diff --git a/src/geometry/SubRoom.cpp b/src/geometry/SubRoom.cpp index 830f9728380de0986c65df5082143ed2ea3053b3..e0f4a32ecc6521c77c57454f3e73d61378440be9 100644 --- a/src/geometry/SubRoom.cpp +++ b/src/geometry/SubRoom.cpp @@ -263,7 +263,6 @@ void SubRoom::CalculateArea() Point SubRoom::GetCentroid() const { - double px=0,py=0; double signedArea = 0.0; double x0 = 0.0; // Current vertex X @@ -272,6 +271,7 @@ Point SubRoom::GetCentroid() const double y1 = 0.0; // Next vertex Y double a = 0.0; // Partial signed area + if(_poly.size()==0) return Point(0,0); // For all vertices except last unsigned int i=0; for (i=0; i<_poly.size()-1; ++i) { @@ -376,39 +376,32 @@ bool SubRoom::IsVisible(const Point& p1, const Point& p2, bool considerHlines) // printf("\t\tEnter ped_is_visible\n"); // generate certain connection lines // connecting p1 with p2 - Line cl = Line(p1,p2); - Line L1=cl, L2; + //Line cl(p1,p2); + //Line L2; bool temp = true; //check intersection with Walls - for(unsigned int i = 0; i < _walls.size(); i++) { - if(temp && cl.IntersectionWith(_walls[i])){ - L2 = _walls[i]; - // fprintf (stdout, "\t\t INTERSECTION WALL L1_P1(%.2f, %.2f), L1_P2(%.2f, %.2f), L2_P1(%.2f, %.2f) L2_P2(%.2f, %.2f)\n", L1.GetPoint1().GetX(),L1.GetPoint1().GetY(),L1.GetPoint2().GetX(),L1.GetPoint2().GetY(), L2.GetPoint1().GetX(),L2.GetPoint1().GetY(),L2.GetPoint2().GetX(),L2.GetPoint2().GetY()); - temp = false; - } - } + for(const auto& wall : _walls) { + if (wall.IntersectionWith(p1, p2)) { + return false; + } + } // printf("\t\t -- ped_is_visible; check obstacles\n"); //check intersection with obstacles - for(unsigned int i = 0; i < _obstacles.size(); i++) { - Obstacle * obs = _obstacles[i]; - for(unsigned int k = 0; k<obs->GetAllWalls().size(); k++) { - const Wall& w = obs->GetAllWalls()[k]; - if(temp && cl.IntersectionWith(w)){ - L2 = w; - // fprintf (stdout, "\t\t INTERSECTION OBS; L1_P1(%.2f, %.2f), L1_P2(%.2f, %.2f), L2_P1(%.2f, %.2f) L2_P2(%.2f, %.2f)\n", L1.GetPoint1().GetX(),L1.GetPoint1().GetY(),L1.GetPoint2().GetX(),L1.GetPoint2().GetY(), L2.GetPoint1().GetX(),L2.GetPoint1().GetY(),L2.GetPoint2().GetX(),L2.GetPoint2().GetY()); - temp = false; - - } - } - } + for ( const auto obstacle : _obstacles) { + for ( const auto& wall : obstacle->GetAllWalls()) { + if (wall.IntersectionWith(p1, p2)) { + return false; + } + } + } // check intersection with other hlines in room if(considerHlines) for(unsigned int i = 0; i < _hlines.size(); i++) { if(_hlines[i]->IsInLineSegment(p1)|| _hlines[i]->IsInLineSegment(p2)) continue; - if(temp && cl.IntersectionWith(*(Line*)_hlines[i])) + if(temp && (Line*)_hlines[i]->IntersectionWith(p1, p2)) temp = false; }