Crossing.cpp 8.03 KB
Newer Older
1
/**
Weichen's avatar
Weichen committed
2 3
 * \file        Crossing.cpp
 * \date        Nov 16, 2010
4 5
 * \version     v0.7
 * \copyright   <2009-2015> Forschungszentrum Jülich GmbH. All rights reserved.
6
 *
Weichen's avatar
Weichen committed
7
 * \section License
8 9 10
 * This file is part of JuPedSim.
 *
 * JuPedSim is free software: you can redistribute it and/or modify
Weichen's avatar
Weichen committed
11
 * it under the terms of the GNU Lesser General Public License as published by
12 13 14 15 16 17 18 19
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * JuPedSim is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
Weichen's avatar
Weichen committed
20
 * You should have received a copy of the GNU Lesser General Public License
21 22
 * along with JuPedSim. If not, see <http://www.gnu.org/licenses/>.
 *
Weichen's avatar
Weichen committed
23
 * \section Description
24 25
 *
 *
Weichen's avatar
Weichen committed
26
 **/
Ulrich Kemloh's avatar
Ulrich Kemloh committed
27

28

Ulrich Kemloh's avatar
Ulrich Kemloh committed
29
#include "Crossing.h"
30 31
#include "SubRoom.h"

32
using namespace std;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
33 34


35 36 37
Crossing::Crossing()
{
     _id = -1;
38
     _doorUsage = 0;
39 40
     _maxDoorUsage = (std::numeric_limits<int>::max)(); //avoid name conflicts in windows winmindef.h
     _outflowRate = (std::numeric_limits<double>::max)();
41
     _lastPassingTime = 0;
42 43 44 45 46 47
     _lastFlowMeasurement = 0;
     _DT = 1;
     _DN = 1;
     _partialDoorUsage = 0;
     _closingTime = 0;

48
     _state = DoorState::OPEN;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
49 50
}

51 52
Crossing::~Crossing()
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
53 54
}

55 56 57 58
//void Crossing::SetID(int ID)
//{
//     _id = ID;
//}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
59

60 61 62 63
//void Crossing::SetSubRoom2(SubRoom* r2)
//{
//     _subRoom2 = r2;
//}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
64 65 66

// Getter-Funktionen

67 68 69 70
//int Crossing::GetID() const
//{
//     return _id;
//}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
71

72 73 74 75
//SubRoom* Crossing::GetSubRoom2() const
//{
//     return _subRoom2;
//}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
76 77 78
// Sonstiges


79 80 81
bool Crossing::IsExit() const
{
     return false;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
82 83 84
}


85 86
bool Crossing::IsOpen() const
{
87 88 89 90 91 92 93 94 95 96 97
     return _state == DoorState::OPEN;
}

bool Crossing::IsClose() const
{
     return _state == DoorState::CLOSE;
}

bool Crossing::IsTempClose() const
{
     return _state == DoorState::TEMP_CLOSE;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
98 99
}

100 101 102
bool Crossing::IsTransition() const
{
     return false;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
103 104
}

105 106
void Crossing::Close()
{
107 108 109 110 111 112
     _state = DoorState::CLOSE;
}

void Crossing::TempClose()
{
     _state = DoorState::TEMP_CLOSE;
113 114
}

115

116 117
void Crossing::Open()
{
118
     _state = DoorState::OPEN;
119 120
}

121 122 123
bool Crossing::IsInSubRoom(int subroomID) const
{
     bool r1, r2;
124
     if (_subRoom1 != nullptr)
125 126 127
          r1 = _subRoom1->GetSubRoomID() == subroomID;
     else
          r1 = false;
128
     if (_subRoom2 != nullptr)
129 130 131 132
          r2 = _subRoom2->GetSubRoomID() == subroomID;
     else
          r2 = false;
     return (r1 || r2);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
133 134 135 136 137
}

/* gibt den ANDEREN Subroom != subroomID zurück
 * roomID wird hier nicht benötigt, aber in Transition::GetOtherSubRoom()
 * (virtuelle Funktion) */
138 139 140 141 142 143 144 145 146
SubRoom* Crossing::GetOtherSubRoom(int roomID, int subroomID) const
{
     if (_subRoom1->GetSubRoomID() == subroomID)
          return _subRoom2;
     else if (_subRoom2->GetSubRoomID() == subroomID)
          return _subRoom1;
     else {
          Log->Write("WARMING: \tCrossing::GetOtherSubRoom No exit found "
                     "on the other side\n ID=%hd, roomID=%hd, subroomID=%hd\n",GetID(),roomID,subroomID);
147
          return nullptr;
148
     }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
149 150
}

151

Ulrich Kemloh's avatar
Ulrich Kemloh committed
152
// Ausgabe
153 154 155 156
void Crossing::WriteToErrorLog() const
{
     string s;
     char tmp[CLENGTH];
Oliver Schmidts's avatar
Oliver Schmidts committed
157
     sprintf(tmp, "\t\tCROSS: %d (%f, %f) -- (%f, %f)\n", GetID(), GetPoint1()._x,
158
               GetPoint1()._y, GetPoint2()._x, GetPoint2()._y);
159 160
     s.append(tmp);
     sprintf(tmp, "\t\t\t\tSubRoom: %d <-> SubRoom: %d\n", GetSubRoom1()->GetSubRoomID(),
161
               GetSubRoom2()->GetSubRoomID());
162 163
     s.append(tmp);
     Log->Write(s);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
164 165 166
}

// TraVisTo Ausgabe
167
string Crossing::GetDescription() const
168 169 170 171
{
     //return "";
     string geometry;
     char tmp[CLENGTH] = "";
Ulrich Kemloh's avatar
Ulrich Kemloh committed
172
     sprintf(tmp,"\t\t<crossing ID=\"%d\" color = \"250\" caption=\"%d_%d\">\n",GetUniqueID(),GetID(),GetUniqueID());
173 174 175
     geometry.append(tmp);
     //geometry.append("\t\t<door color=\"250\">\n");
     sprintf(tmp, "\t\t\t<point xPos=\"%.2f\" yPos=\"%.2f\" zPos=\"%.2f\" />\n",
176 177 178
               (GetPoint1()._x) * FAKTOR,
               (GetPoint1()._y) * FAKTOR,
               _subRoom1->GetElevation(GetPoint1())*FAKTOR);
179 180
     geometry.append(tmp);
     sprintf(tmp, "\t\t\t<point xPos=\"%.2f\" yPos=\"%.2f\" zPos=\"%.2f\" />\n",
181 182 183
               (GetPoint2()._x) * FAKTOR,
               (GetPoint2()._y) * FAKTOR,
               _subRoom1->GetElevation(GetPoint2())*FAKTOR);
184
     geometry.append(tmp);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
185
     geometry.append("\t\t</crossing>\n");
186
     return geometry;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
187
}
188 189 190

int Crossing::CommonSubroomWith(Crossing* other, SubRoom* &subroom) {
     int result = 0;
191
     if (_subRoom1 && (_subRoom1 == other->_subRoom1 || _subRoom1 == other->_subRoom2)) {
192 193 194
          ++result;
          subroom = _subRoom1;
     }
195
     if (_subRoom2 && (_subRoom2 == other->_subRoom1 || _subRoom2 == other->_subRoom2)) {
196 197 198 199 200
          ++result;
          subroom = _subRoom2;
     }
     return result;
}
201 202 203

void Crossing::IncreaseDoorUsage(int number, double time)
{
204

205 206 207
     _doorUsage += number;
     _lastPassingTime = time;
     _flowAtExit += to_string(time) + "  " + to_string(_doorUsage) + "\n";
208
}
209

210 211 212 213 214 215 216
void Crossing::IncreasePartialDoorUsage(int number)
{
     _partialDoorUsage += number;
}
void Crossing::ResetPartialDoorUsage()
{
     _partialDoorUsage = 0;
217 218 219 220
}

int Crossing::GetDoorUsage() const
{
221 222 223
     return _doorUsage;
}

224 225 226 227 228 229
int Crossing::GetPartialDoorUsage() const
{
     return _partialDoorUsage;
}


230 231 232 233 234 235 236 237 238
int Crossing::GetMaxDoorUsage() const
{
     return  _maxDoorUsage;
}


double Crossing::GetOutflowRate() const
{
     return _outflowRate;
239 240
}

241

242 243
double Crossing::GetLastPassingTime() const
{
244
     return _lastPassingTime;
245 246 247 248
}

const std::string & Crossing::GetFlowCurve() const
{
249 250 251 252 253 254 255 256 257 258 259 260
     return _flowAtExit;
}

void Crossing::SetOutflowRate(double outflow)
{
     _outflowRate = outflow;
}

void Crossing::SetMaxDoorUsage(int mdu)
{
     _maxDoorUsage = mdu;
}
261 262 263 264 265 266 267 268 269 270 271

double Crossing::GetClosingTime() const
{
     return _closingTime;
}

void Crossing::UpdateClosingTime(double dt)
{
     _closingTime -= dt;
}

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

double Crossing::GetDT()
{
     return _DT;
}

void Crossing::SetDT(double dt)
{
     _DT = dt;
}
int Crossing::GetDN()
{
     return _DN;
}

void Crossing::SetDN(int dn)
{
     _DN = dn;
}

// changes:
// - _lasFlowMeasurement
// - _closingTime
// - state of door (close/open)
// - _temporaryClosed (false if maxDoorUsage is reached)
void Crossing::regulateFlow(double time)
{
     double number = GetPartialDoorUsage();
     double T = time -  _lastFlowMeasurement;
     double flow =  number / T;
     if(flow >  _outflowRate)
     {
          // _outflowRate > flow (=number/deltaTime)
          // _outflowRate = number/(deltaTime + t1)
          // --> [1]
307
          //---------------------------
308
          _closingTime = number / _outflowRate -  T; //[1]
309 310 311
//           _temporaryClosed = true;
//          this->Close();
          this->TempClose();
312 313 314 315 316 317
          Log-> Write("INFO:\tClosing door %d. DoorUsage = %d (max = %d). Flow = %.2f (max =  %.2f) Time=%.2f", GetID(), GetDoorUsage(), GetMaxDoorUsage(), flow, _outflowRate, time);
     }

     // close the door is mdu is reached
     if(_doorUsage >= _maxDoorUsage)
     {
318 319
          Log-> Write("INFO:\tClosing door %d. DoorUsage = %d (>= %d). Time=%.2f", GetID(), GetDoorUsage(), GetMaxDoorUsage(), time);
          this->Close();
320
//          _temporaryClosed = false;
321 322 323 324
     }
     _lastFlowMeasurement = time +  _closingTime;
}

325 326
void Crossing::changeTemporaryState()
{
327 328 329 330 331 332 333 334
//       _temporaryClosed = false;
     _closingTime = 0;
     this->Open();
}

DoorState Crossing::GetState() const
{
     return _state;
335
}
336

337
void Crossing::SetState(DoorState state)
338
{
339
     Crossing::_state = state;
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
}

std::string Crossing::toString() const
{
     std::stringstream tmp;
//     tmp << _point1.toString() << "--" << _point2.toString();

     tmp << this->GetPoint1().toString() << "--" << this->GetPoint2().toString();
     switch (_state){
     case DoorState::OPEN:
          tmp << " open";
          break;
     case DoorState::CLOSE:
          tmp << " close";
          break;
     case DoorState::TEMP_CLOSE:
          tmp << " temp_close";
          break;
358 359 360
     case DoorState::Error:
          tmp << " Error";
          break;
361 362 363 364

     }
     return tmp.str();
}