Data structure ready for train arrivals

parent 2580b20f
......@@ -912,6 +912,7 @@ std::shared_ptr<TrainTimeTable> GeoFileParser::parseTrainTimeTableNode(TiXmlElem
int id = xmltoi(e->Attribute("id"), -1);
std::string type = xmltoa(e->Attribute("type"), "-1");
int room_id = xmltoi(e->Attribute("room_id"), -1);
int subroom_id = xmltoi(e->Attribute("subroom_id"), -1);
int platform_id = xmltoi(e->Attribute("platform_id"), -1);
float track_start_x = xmltof(e->Attribute("track_start_x"), -1);
float track_start_y = xmltof(e->Attribute("track_start_y"), -1);
......@@ -929,6 +930,7 @@ std::shared_ptr<TrainTimeTable> GeoFileParser::parseTrainTimeTableNode(TiXmlElem
Log->Write("INFO:\t id: %d", id);
Log->Write("INFO:\t type: %s", type.c_str());
Log->Write("INFO:\t room_id: %d", room_id);
Log->Write("INFO:\t subroom_id: %d", subroom_id);
Log->Write("INFO:\t platform_id: %d", platform_id);
Log->Write("INFO:\t track_start: [%.2f, %.2f]", track_start_x, track_start_y);
Log->Write("INFO:\t track_end: [%.2f, %.2f]", track_end_x, track_end_y);
......@@ -943,6 +945,7 @@ std::shared_ptr<TrainTimeTable> GeoFileParser::parseTrainTimeTableNode(TiXmlElem
id,
type,
room_id,
subroom_id,
arrival_time,
departure_time,
track_start,
......
......@@ -655,17 +655,23 @@ double Simulation::RunBody(double maxSimTime)
// here open transition that should be closed
// TODO fix, opens door everytime...
bool trainHere = false;
std::string train = "";
std::string trainType = "";
Point trackStart, trackEnd;
auto now = Pedestrian::GetGlobalTime();
for(auto && tab: TrainTimeTables)
{
if( (now>=tab.second->tin) && (now<=tab.second->tout) )
{
trainHere = true;
train = tab.second->type;
trainType = tab.second->type;
trackStart = tab.second->pstart;
trackEnd = tab.second->pend;
continue;
}
}
if(trainHere)
correctGeometry(_building, trainType, trackStart, trackEnd);
for (auto& itr: _building->GetAllTransitions())
{
Transition* Trans = itr.second;
......@@ -682,38 +688,39 @@ double Simulation::RunBody(double maxSimTime)
}// normal transition
}
// ------ train
if(trainHere) // track?
{
auto doors = TrainTypes[train]->doors;
for(auto door: doors)
{
auto tp1 = door.GetPoint1();
auto tp2 = door.GetPoint2();
if(Trans->IsInLineSegment(tp1) && Trans->IsInLineSegment(tp2))
{
Trans->SetMaxDoorUsage(TrainTypes[train]->nmax);
//todo: SetOutflowRate() to sum of doors of train
Trans->Open();
}
}
}
else
{
for(auto tt: TrainTypes)
{
auto doors = tt.second->doors;
for(auto door: doors)
{
auto tp1 = door.GetPoint1();
auto tp2 = door.GetPoint2();
if(Trans->IsInLineSegment(tp1) && Trans->IsInLineSegment(tp2))
//todo: If only we knew that Trans is a track
{
Trans->TempClose();
}
}
}
}
// if(trainHere) // track?
// {
// coorect_geometry(_building, train)
// auto doors = TrainTypes[train]->doors;
// for(auto door: doors)
// {
// auto tp1 = door.GetPoint1();
// auto tp2 = door.GetPoint2();
// if(Trans->IsInLineSegment(tp1) && Trans->IsInLineSegment(tp2))
// {
// Trans->SetMaxDoorUsage(TrainTypes[train]->nmax);
// //todo: SetOutflowRate() to sum of doors of train
// Trans->Open();
// }
// }
// }
// else
// {
// for(auto tt: TrainTypes)
// {
// auto doors = tt.second->doors;
// for(auto door: doors)
// {
// auto tp1 = door.GetPoint1();
// auto tp2 = door.GetPoint2();
// if(Trans->IsInLineSegment(tp1) && Trans->IsInLineSegment(tp2))
// //todo: If only we knew that Trans is a track
// {
// Trans->TempClose();
// }
// }
// }
// }
}
if(frameNr % 1000 == 0)
{
......@@ -723,7 +730,76 @@ double Simulation::RunBody(double maxSimTime)
}// while time
return t;
}
// | |
// *-------------* <---door
// | |
// | |
// | |
// | |
// | |
//*-----x-------------x--------* <- wall
// | |
bool Simulation::correctGeometry(std::shared_ptr<Building> building, std::string trainType, Point TrackStart, Point TrackEnd)
{
std::cout << "enter with train " << trainType.c_str() << "\n";
int subroomId = -1;
//auto platforms = building->GetPlatforms();
auto mytrack = building->GetTrackWalls(TrackStart, TrackEnd, subroomId);
if(mytrack.empty())
return false;
std::cout << "subroom: " << subroomId << "\n";
auto train = building->GetTrainTypes().at(trainType);
auto doors = train->doors;
// std::vector<std::pair<PointWall, pointWall > >
auto pws = building->GetIntersectionPoints(doors, mytrack);
if(pws.empty())
std::cout << "simulation::correctGeometry: pws are empty\n";
// debugging
std::cout << "------\n";
for(auto pw: pws)
{
auto pw1 = pw.first;
auto pw2 = pw.second;
auto p1 = pw1.first;
auto w1 = pw1.second;
auto p2 = pw2.first;
auto w2 = pw2.second;
std::cout << "p1 " << p1.toString() << ", wall: " << w1.toString() << "\n";
std::cout << "p2 " << p2.toString() << ", wall: " << w2.toString() << "\n";
std::cout << "------\n";
// case 1
Point P;
if(w1.ShareCommonPointWith(w2, P))
{
// add AP and BP: walls
// remove walls w1 and w2
// p1 p2 door
}
else if(w1 == w2)
{
// use function we have in correct geometry
}
else // disjoint
{
// find points on w1 and w2 between p1 and p2
// (A, B)
// remove all walls connected to A
// move A forwards
// remove walls... until B is found
// remove walls w1 and w2
}
}
std::cout << "------\n";
getc(stdin);
return true;
}
void Simulation::RunFooter()
{
// writing the footer
......
......@@ -188,6 +188,8 @@ public:
void UpdateDoorticks() const;
int GetMaxSimTime() const;
bool correctGeometry(std::shared_ptr<Building> building, std::string trainType, Point TrackStart, Point TrackEnd);
int _countTraj=0; // count number of TXT trajectories to produce
double _maxFileSize; // in MB
};
......
......@@ -366,7 +366,23 @@ bool Building::InitGeometry()
InitInsideGoals();
InitPlatforms();
//---
for (auto platform: _platforms)
{
std::cout << "\n platform " << platform.first << ", "<< platform.second->id << "\n";
std::cout << "\t rid " << platform.second->rid << "\n";
auto tracks = platform.second->tracks;
for(auto track: tracks)
{
std::cout << "\t track " << track.first << "\n";
auto walls = track.second;
for(auto wall: walls)
{
std::cout << "\t\t wall: " << wall.GetType() << ". " << wall.GetPoint1().toString() << " | " << wall.GetPoint2().toString() << "\n";
}
}
}
Log->Write("INFO: \tInit Geometry successful!!!\n");
// for (auto& transItr : _transitions){
......@@ -380,6 +396,106 @@ bool Building::InitGeometry()
return true;
}
const std::vector<Wall> Building::GetTrackWalls(Point TrackStart, Point TrackEnd, int & subroomId) const
{
bool trackFound = false;
int track_id = -1;
int platform_id = -1;
std::vector<Wall> mytrack;
for(auto platform: _platforms)
{
platform_id = platform.second->id;
auto tracks = platform.second->tracks;
for(auto track: tracks)
{
track_id=track.first;
int commonPoints = 0;
std::cout << "\t track " << track.first << "\n";
auto walls = track.second;
for(auto wall: walls)
{
Point P1 = wall.GetPoint1();
Point P2 = wall.GetPoint2();
//std::cout << "\t\t wall: " << wall.GetType() << ". " << wall.GetPoint1().toString() << " | " << wall.GetPoint2().toString() << "\n";
if (P1 == TrackStart)
commonPoints++;
if(P1 == TrackEnd)
commonPoints++;
if(P2 == TrackStart)
commonPoints++;
if(P2 == TrackEnd)
commonPoints++;
}
if(commonPoints == 2 )
{
trackFound = true;
break;
}
} // tracks
if (trackFound) break;
} // plattforms
if(trackFound)
{
subroomId = _platforms.at(platform_id)->sid;
mytrack = _platforms.at(platform_id)->tracks[track_id];
std::cout << "track has walls: " << mytrack.size() << "\n";
std::cout << "platform " << platform_id << " track " << track_id << "\n";
}
else
{
std::cout << "could not find any track! \n";
}
return mytrack;
}
const std::vector<std::pair<PointWall, PointWall > > Building::GetIntersectionPoints(const std::vector<Transition> doors, const std::vector<Wall> mytrack) const
{
const int scaleFactor = 1000; // very long orthogonal walls to train's doors
std::vector<std::pair<PointWall, PointWall > > pws;
// every door has two points.
// for every point -> get pair<P, W>
// collect pairs of pairs
for(auto door: doors)
{
PointWall pw1, pw2;
int nintersections = 0;
auto n = door.NormalVec();
auto p11 = door.GetPoint1() + n*scaleFactor;
auto p12 = door.GetPoint1() - n*scaleFactor;
auto p21 = door.GetPoint2() + n*scaleFactor;
auto p22 = door.GetPoint2() - n*scaleFactor;
auto normalWall1 = Wall(p11, p12);
auto normalWall2 = Wall(p21, p22);
/* std::cout << "normal wall 1: " << normalWall1.toString() << "\n"; */
/* std::cout << "normal wall 2: " << normalWall2.toString() << "\n"; */
for(auto twall: mytrack)
{
/* std::cout << " twall " << twall.toString() << "\n"; */
Point interPoint1, interPoint2;
auto res = normalWall1.IntersectionWith(twall, interPoint1);
auto res2 = normalWall2.IntersectionWith(twall, interPoint2);
if(res == 1)
{
/* std::cout << "intersection at :" << interPoint1.toString() << "\n"; */
pw1 = std::make_pair(interPoint1, twall);
nintersections++;
}
if(res2 == 1)
{
pw2 = std::make_pair(interPoint2, twall);
nintersections++;
/* std::cout << "intersection at :" << interPoint2.toString() << "\n"; */
}
} // tracks
std::cout << "door: " << door.toString() << ", intersections: " << nintersections << "\n";
if(nintersections == 2)
pws.push_back(std::make_pair(pw1, pw2));
}// doors
return pws;
}
bool Building::InitPlatforms()
{
int num_platform = -1;
......@@ -389,6 +505,7 @@ bool Building::InitPlatforms()
for (auto& subRoomItr : room->GetAllSubRooms())
{
SubRoom* subRoom = subRoomItr.second.get();
int subroom_id = subRoom->GetSubRoomID();
if(subRoom->GetType() != "Platform" ) continue;
std::map<int, std::vector<Wall> > tracks;
num_platform++;
......@@ -409,9 +526,10 @@ bool Building::InitPlatforms()
std::shared_ptr<Platform> p = std::make_shared<Platform>(
Platform{
num_platform,
room->GetID(),
tracks,
});
room->GetID(),
subroom_id,
tracks,
});
AddPlatform(p);
}//subroom
......
......@@ -45,10 +45,17 @@
#include "Goal.h"
#include "../general/Configuration.h"
typedef std::pair<Point, Wall> PointWall;
// train schedules: Trains get deleted and added.
//std::vector<Wall> TempAddedWalls;
//std::vector<Wall> TempRemovedWalls;
struct Platform
{
int id;
int rid;
int sid;
std::map<int, std::vector<Wall> > tracks;
};
......@@ -57,6 +64,7 @@ struct TrainTimeTable
int id;
std::string type;
int rid; // room id
int sid; // subroom id
double tin; // arrival time
double tout; //leaving time
Point pstart; // track start
......@@ -238,13 +246,17 @@ public:
const std::map<int, Hline*>& GetAllHlines() const;
const std::map<int, Goal*>& GetAllGoals() const;
// --------------- Trains interface
const std::map<std::string, std::shared_ptr<TrainType> >& GetTrainTypes() const;
const std::map<int, std::shared_ptr<TrainTimeTable> >& GetTrainTimeTables() const;
const std::map<int, std::shared_ptr<Platform> >& GetPlatforms() const;
const std::vector<Wall> GetTrackWalls(Point TrackStart, Point TrackEnd, int & subroomId) const;
const std::vector<std::pair<PointWall, PointWall > > GetIntersectionPoints(const std::vector<Transition> doors, const std::vector<Wall>) const;
// ------------------------------------
bool AddCrossing(Crossing* line);
bool AddTransition(Transition* line);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment