From 1e37bff5e5b56e19ee78d689b3cc293369877007 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tobias=20Schr=C3=B6dter?= <t.schroedter@fz-juelich.de>
Date: Wed, 6 Feb 2019 18:50:17 +0100
Subject: [PATCH] WaitingAreas now can also be assigned to Transition, ped in
 wa wait till transition is open

---
 IO/GeoFileParser.cpp                      |  4 +-
 geometry/Building.cpp                     |  2 +-
 geometry/Crossing.cpp                     | 16 +++----
 geometry/Crossing.h                       | 26 ++++++------
 geometry/Goal.cpp                         |  3 +-
 geometry/Hline.h                          |  2 +-
 geometry/WaitingArea.cpp                  | 52 +++++++++++++++--------
 geometry/WaitingArea.h                    | 19 +++++++--
 routing/ff_router_trips/ffRouterTrips.cpp |  4 +-
 9 files changed, 79 insertions(+), 49 deletions(-)

diff --git a/IO/GeoFileParser.cpp b/IO/GeoFileParser.cpp
index acba6362..c73c37e5 100644
--- a/IO/GeoFileParser.cpp
+++ b/IO/GeoFileParser.cpp
@@ -419,6 +419,8 @@ bool GeoFileParser::LoadRoutingInfo(Building* building)
                int min_peds = xmltoi(e->Attribute("min_peds"), -1);
                int max_peds = xmltoi(e->Attribute("max_peds"), -1);
                int waiting_time = xmltoi(e->Attribute("waiting_time"), -1);
+               int transition_id = xmltoi(e->Attribute("transition_id"), -1);
+
                std::string caption = xmltoa(e->Attribute("caption"), "-1");
 
                Goal* wa = new WaitingArea();
@@ -430,7 +432,7 @@ bool GeoFileParser::LoadRoutingInfo(Building* building)
                waitingArea->setMinNumPed(min_peds);
                waitingArea->setMaxNumPed(max_peds);
                waitingArea->setWaitingTime(waiting_time);
-
+               waitingArea->setTransitionID(transition_id);
                std::map<int, double> nextGoals;
 
                trips.addGoal(wa->GetId());
diff --git a/geometry/Building.cpp b/geometry/Building.cpp
index ecc219f1..9c778d81 100644
--- a/geometry/Building.cpp
+++ b/geometry/Building.cpp
@@ -821,7 +821,7 @@ Room* Building::GetRoom(string caption) const
 bool Building::AddCrossing(Crossing* line)
 {
          int IDRoom = line->GetRoom1()->GetID();
-         int IDLine = line->GetID();
+         int IDLine = line->GetUniqueID();
          int IDCrossing = 1000 * IDRoom + IDLine;
          if (_crossings.count(IDCrossing) != 0)
          {
diff --git a/geometry/Crossing.cpp b/geometry/Crossing.cpp
index 60161117..436d9703 100644
--- a/geometry/Crossing.cpp
+++ b/geometry/Crossing.cpp
@@ -42,10 +42,10 @@ Crossing::~Crossing()
 {
 }
 
-void Crossing::SetID(int ID)
-{
-     _id = ID;
-}
+//void Crossing::SetID(int ID)
+//{
+//     _id = ID;
+//}
 
 //void Crossing::SetSubRoom2(SubRoom* r2)
 //{
@@ -54,10 +54,10 @@ void Crossing::SetID(int ID)
 
 // Getter-Funktionen
 
-int Crossing::GetID() const
-{
-     return _id;
-}
+//int Crossing::GetID() const
+//{
+//     return _id;
+//}
 
 //SubRoom* Crossing::GetSubRoom2() const
 //{
diff --git a/geometry/Crossing.h b/geometry/Crossing.h
index d758528c..4f9702f6 100644
--- a/geometry/Crossing.h
+++ b/geometry/Crossing.h
@@ -38,8 +38,8 @@ class SubRoom;
 
 class Crossing : public Hline {
 private:
-     /// TODO ? unique between crossings and transitions ?
-     int _id;
+//     /// TODO ? unique between crossings and transitions ?
+//     int _id;
      /// only one room needed, since a crossing only separates 2 subrooms
      //Room* _room1;
      //SubRoom* _subRoom1;
@@ -64,17 +64,17 @@ public:
       */
      virtual ~Crossing();
 
-     /**
-      * Set/Get the Id of the crossing
-      * TODO: do you really want to shadow ?
-      */
-     void SetID(int ID);
-
-     /**
-      * Set/Get the Id of the crossing
-      * TODO: do you really want to shadow ?
-      */
-     int GetID () const;
+//     /**
+//      * Set/Get the Id of the crossing
+//      * TODO: do you really want to shadow ?
+//      */
+//     void SetID(int ID);
+//
+//     /**
+//      * Set/Get the Id of the crossing
+//      * TODO: do you really want to shadow ?
+//      */
+//     int GetID () const;
 
      /**
       * Set/Get the second subroom
diff --git a/geometry/Goal.cpp b/geometry/Goal.cpp
index d31216e1..8d31a9e5 100644
--- a/geometry/Goal.cpp
+++ b/geometry/Goal.cpp
@@ -41,7 +41,7 @@ Goal::Goal()
      _isFinalGoal=0;
      _walls = vector<Wall > ();
      _poly = vector<Point > ();
-     _crossing = Crossing();
+//     _crossing = Crossing();
 }
 
 Goal::~Goal()
@@ -72,6 +72,7 @@ int Goal::GetId() const
 void Goal::SetId(int id)
 {
      _id = id;
+     _crossing.SetID(id);
 }
 
 const vector<Point>& Goal::GetPolygon() const
diff --git a/geometry/Hline.h b/geometry/Hline.h
index 50036103..b764ca5d 100644
--- a/geometry/Hline.h
+++ b/geometry/Hline.h
@@ -38,7 +38,7 @@ class Room;
 
 class Hline: public NavLine {
 
-private:
+protected:
      int _id;
      std::string _caption;
 
diff --git a/geometry/WaitingArea.cpp b/geometry/WaitingArea.cpp
index 8bb312e0..47f15a15 100644
--- a/geometry/WaitingArea.cpp
+++ b/geometry/WaitingArea.cpp
@@ -3,6 +3,7 @@
 //
 
 #include "WaitingArea.h"
+#include "Building.h"
 
 int WaitingArea::getMaxNumPed() const
 {
@@ -99,13 +100,10 @@ void WaitingArea::setWaitingTime(double waitingTime)
      WaitingArea::waitingTime = waitingTime;
 }
 
-std::vector<Line> WaitingArea::getAllLines()
-{
-}
-
 int WaitingArea::GetNextGoal()
 {
-     double random = std::rand()/RAND_MAX;
+     std::srand( (unsigned)time( NULL ) );
+     double random = ((double) std::rand() / (RAND_MAX));
      double cumProb = 0.;
 
      for (auto& nextGoal : nextGoals){
@@ -114,17 +112,16 @@ int WaitingArea::GetNextGoal()
                return nextGoal.first;
           }
      }
-
 }
 
-void WaitingArea::addPed()
+void WaitingArea::addPed(int ped)
 {
-     numPed++;
+     pedInside.insert(ped);
 }
 
-void WaitingArea::removePed()
+void WaitingArea::removePed(int ped)
 {
-     numPed--;
+     pedInside.erase(ped);
 }
 
 void WaitingArea::startTimer(double time)
@@ -133,22 +130,41 @@ void WaitingArea::startTimer(double time)
      std::cout << "Timer started at " << startTime << std::endl;
 }
 
-bool WaitingArea::isWaiting(double time)
+bool WaitingArea::isWaiting(double time, const Building* building)
 {
-     if ((numPed >= minNumPed) && (startTime < 0. )){
+     Transition* trans;
+     if (transitionID >= 0){
+          trans = building->GetTransition(transitionID);
+     }
+
+     if ((pedInside.size() >= minNumPed) && (startTime < 0. )){
           startTimer(time);
      }
 
-     if ((time < startTime + waitingTime) || (startTime < 0. )){
-          std::cout << "Waiting ..." << std::endl;
-          return true;
-     }else{
-          std::cout << "Waiting over!" << std::endl;
+     if ((time > startTime + waitingTime) && (startTime > 0. ) && (transitionID < 0)){
+          return false;
+     }
+
+     if ((startTime > 0. ) && (time > startTime + waitingTime) && (trans->IsOpen())){
+          std::cout << "Waiting ended" << std::endl;
           return false;
      }
+
+     std::cout << "Waiting ..." << std::endl;
+     return true;
 }
 
 int WaitingArea::getNumPed()
 {
-     return numPed;
+     return pedInside.size();
+}
+
+int WaitingArea::getTransitionID() const
+{
+     return transitionID;
+}
+
+void WaitingArea::setTransitionID(int transitionID)
+{
+     WaitingArea::transitionID = transitionID;
 }
diff --git a/geometry/WaitingArea.h b/geometry/WaitingArea.h
index a584c4eb..255b93ae 100644
--- a/geometry/WaitingArea.h
+++ b/geometry/WaitingArea.h
@@ -12,6 +12,7 @@
 #include <map>
 #include <stdio.h>
 
+class Building;
 class WaitingArea : public Goal{
 
 protected:
@@ -30,7 +31,12 @@ protected:
 
     double startTime = -1;
 
-    int numPed = 0;
+    int transitionID = -1;
+public:
+    int getTransitionID() const;
+
+    void setTransitionID(int transitionID);
+
 protected:
 
     /**
@@ -43,6 +49,11 @@ protected:
      */
     std::map<int, double> nextGoals;
 
+    /**
+     * Set of pedestrians who are currently in waiting area
+     */
+     std::set<int> pedInside;
+
 public:
 
     int getMaxNumPed() const;
@@ -71,11 +82,11 @@ public:
 
     int GetNextGoal();
 
-    void addPed();
-    void removePed();
+    void addPed(int ped);
+    void removePed(int ped);
     void startTimer(double time);
     int  getNumPed();
-    bool isWaiting(double time);
+    bool isWaiting(double time, const Building* building);
 
 private:
     void updateProbabilities();
diff --git a/routing/ff_router_trips/ffRouterTrips.cpp b/routing/ff_router_trips/ffRouterTrips.cpp
index d09a1c59..853d5295 100644
--- a/routing/ff_router_trips/ffRouterTrips.cpp
+++ b/routing/ff_router_trips/ffRouterTrips.cpp
@@ -497,9 +497,9 @@ int FFRouterTrips::FindExit(Pedestrian* ped)
                //take the current time from the pedestrian
                double t = Pedestrian::GetGlobalTime();
 
-               wa->addPed();
+               wa->addPed(ped->GetID());
 
-               if (!wa->isWaiting(t)){
+               if (!wa->isWaiting(t, _building)){
                     ped->SetFinalDestination(wa->GetNextGoal());
                }
           }
-- 
GitLab