ffRouter.cpp 28.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/**
 * \file        ffRouter.h
 * \date        Feb 19, 2016
 * \version     v0.8
 * \copyright   <2016-2022> Forschungszentrum Jülich GmbH. All rights reserved.
 *
 * \section License
 * This file is part of JuPedSim.
 *
 * JuPedSim is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * 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.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with JuPedSim. If not, see <http://www.gnu.org/licenses/>.
 *
 * \section Description
 * This router is an update of the former Router.{cpp, h} - Global-, Quickest
 * Router System. In the __former__ version, a graph was created with doors and
 * hlines as nodes and the distances of (doors, hlines), connected with a line-
 * of-sight, was used as edge-costs. If there was no line-of-sight, there was no
 * connecting edge. On the resulting graph, the Floyd-Warshall algorithm was
 * used to find any paths. In the "quickest-___" variants, the edge cost was not
 * determined by the distance, but by the distance multiplied by a speed-
 * estimate, to find the path with minimum travel times. This whole construct
 * worked pretty well, but dependend on hlines to create paths with line-of-
 * sights to the next target (hline/door).
 *
 * In the ffRouter, we want to overcome hlines by using floor fields to
 * determine the distances. A line of sight is not required any more. We hope to
 * reduce the graph complexity and the preparation-needs for new geometries.
 *
 * To find a counterpart for the "quickest-____" router, we can either use
 * __special__ floor fields, that respect the travel time in the input-speed map,
 * or take the distance-floor field and multiply it by a speed-estimate (analog
 * to the former construct.
 *
 * We will derive from the <Router> class to fit the interface.
 *
 **/

#include <cfloat>
#include <algorithm>
#include "ffRouter.h"
//#include "FloorfieldViaFM.h"
//#include "../../geometry/Building.h"

int FFRouter::_cnt = 0;

FFRouter::FFRouter()
{

}

FFRouter::FFRouter(int id, RoutingStrategy s, bool hasSpecificGoals, Configuration* config):Router(id,s) {
     _config = config;
     _building = nullptr;
     _hasSpecificGoals = hasSpecificGoals;
     _globalFF = nullptr;
     _targetWithinSubroom = true; //depending on exit_strat 8 => false, depending on exit_strat 9 => true;
     _targetWithinSubroom = (_config->get_exit_strat() == 9);
     if (s == ROUTING_FF_QUICKEST) {
          _mode = quickest;
          _recalc_interval = _config->get_recalc_interval();
     } else if (s == ROUTING_FF_LOCAL_SHORTEST) {
          _mode = local_shortest;
          _localShortestSafedPeds.clear();
          _localShortestSafedPeds.reserve(500);
     } else if (s == ROUTING_FF_GLOBAL_SHORTEST) {
          _mode = global_shortest;
     }
}

//FFRouter::FFRouter(const Building* const building)

FFRouter::~FFRouter()
{
     if (_globalFF) {
          delete _globalFF;
     }
     //delete localffs
     std::map<int, UnivFFviaFM*>::reverse_iterator delIter;
     for (delIter = _locffviafm.rbegin();
          delIter != _locffviafm.rend();
          ++delIter) {
          delete (*delIter).second;
     }
}

bool FFRouter::Init(Building* building)
{
     _building = building;
     if (_hasSpecificGoals) {
          std::vector<int> goalIDs;
          goalIDs.clear();
          //get global field to manage goals (which are not in a subroom)
          _globalFF = new FloorfieldViaFM(building, 0.25, 0.25, 0.0, false, true);
          for (auto &itrGoal : building->GetAllGoals()) {
               _globalFF->createMapEntryInLineToGoalID(itrGoal.first);
               goalIDs.emplace_back(itrGoal.first);
          }
          _goalToLineUIDmap = _globalFF->getGoalToLineUIDmap();
          _goalToLineUIDmap2 = _globalFF->getGoalToLineUIDmap2();
          _goalToLineUIDmap3 = _globalFF->getGoalToLineUIDmap3();
Mohcine Chraibi's avatar
Mohcine Chraibi committed
111
          _globalFF->writeGoalFF("goal.vtk", goalIDs);
112 113 114 115 116 117 118 119 120 121 122
     }
     //get all door UIDs
     _allDoorUIDs.clear();
     _TransByUID.clear();
     _ExitsByUID.clear();
     _CroTrByUID.clear();
     auto& allTrans = building->GetAllTransitions();
     auto& allCross = building->GetAllCrossings();
     std::vector<std::pair<int, int>> roomAndCroTrVector;
     roomAndCroTrVector.clear();
     for (auto& pair:allTrans) {
123 124
          //TODO if (pair.second->IsOpen()) {
          if (!pair.second->IsClose()) {
125 126 127 128 129 130 131 132 133 134 135 136
               _allDoorUIDs.emplace_back(pair.second->GetUniqueID());
               _CroTrByUID.insert(std::make_pair(pair.second->GetUniqueID(), pair.second));
               if (pair.second->IsExit()) {
                    _ExitsByUID.insert(std::make_pair(pair.second->GetUniqueID(), pair.second));
               }
               Room* room1 = pair.second->GetRoom1();
               if (room1) roomAndCroTrVector.emplace_back(std::make_pair(room1->GetID(), pair.second->GetUniqueID()));
               Room* room2 = pair.second->GetRoom2();
               if (room2) roomAndCroTrVector.emplace_back(std::make_pair(room2->GetID(), pair.second->GetUniqueID()));
          }
     }
     for (auto& pair:allCross) {
137 138
          //TODO if (pair.second->IsOpen()) {
          if (!pair.second->IsClose()) {
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 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
               _allDoorUIDs.emplace_back(pair.second->GetUniqueID());
               _CroTrByUID.insert(std::make_pair(pair.second->GetUniqueID(), pair.second));
               Room* room1 = pair.second->GetRoom1();
               if (room1) roomAndCroTrVector.emplace_back(std::make_pair(room1->GetID(), pair.second->GetUniqueID()));
          }
     }
     //make unique
     std::sort(_allDoorUIDs.begin(), _allDoorUIDs.end());
     _allDoorUIDs.erase( std::unique(_allDoorUIDs.begin(),_allDoorUIDs.end()), _allDoorUIDs.end());

     //cleanse maps
     _distMatrix.clear();
     _pathsMatrix.clear();

     //init, yet no distances, only create map entries
     for(auto& id1 : _allDoorUIDs) {
          for(auto& id2 : _allDoorUIDs){
               std::pair<int, int> key   = std::make_pair(id1, id2);
               double              value = (id1 == id2)? 0.0 : DBL_MAX;
               //distMatrix[i][j] = 0,   if i==j
               //distMatrix[i][j] = max, else
               _distMatrix.insert(std::make_pair( key , value));
               //pathsMatrix[i][j] = i or j ? (follow wiki:path_reconstruction, it should be j)
               _pathsMatrix.insert(std::make_pair( key , id2 ));
               //_subroomMatrix.insert(std::make_pair(key, nullptr));
          }
     }

     //prepare all room-floor-fields-objects (one room = one instance)
     _locffviafm.clear();
     //type of allRooms: const std::map<int, std::unique_ptr<Room> >&
     const std::map<int, std::shared_ptr<Room> >& allRooms = _building->GetAllRooms();


     for (unsigned int i = 0; i < allRooms.size(); ++i) {

#ifdef DEBUG
          std::cerr << "Creating Floorfield for Room: " << pair.first << std::endl;
#endif

          auto pairRoomIt = allRooms.begin();
          std::advance(pairRoomIt, i);
          UnivFFviaFM *locffptr = nullptr;
          locffptr = new UnivFFviaFM(pairRoomIt->second.get(), building, 0.125, 0.0, false);

          locffptr->setUser(DISTANCE_MEASUREMENTS_ONLY);
          locffptr->setMode(CENTERPOINT);
          locffptr->setSpeedMode(FF_HOMO_SPEED);
          locffptr->addAllTargetsParallel();
          //locffptr->writeFF("UnivFF"+std::to_string(pairRoomIt->first)+".vtk", locffptr->getKnownDoorUIDs());
          Log->Write("INFO: \tAdding distances in Room %d to matrix", (*pairRoomIt).first);
//#pragma omp critical(_locffviafm)
          _locffviafm.insert(std::make_pair((*pairRoomIt).first, locffptr));
     }


     // nowait, because the parallel region ends directly afterwards
//#pragma omp for nowait
     //@todo: @ar.graf: it would be easier to browse thru doors of each field directly after "addAllTargetsParallel" as
     //                 we do only want doors of same subroom anyway. BUT the router would have to switch from room-scope
     //                 to subroom-scope. Nevertheless, we could omit the room info (used to acces correct field), if we
     //                 do it like in "ReInit()".
     for (unsigned int i = 0; i < roomAndCroTrVector.size(); ++i) {
          auto rctIt = roomAndCroTrVector.begin();
          std::advance(rctIt, i);

          ////loop over upper triangular matrice (i,j) and write to (j,i) as well
          for (auto otherDoor : roomAndCroTrVector) {
               if (otherDoor.first != rctIt->first) continue; // we only want doors with one room in common
               if (otherDoor.second <= rctIt->second) continue; // calculate every path only once
               // if we exclude otherDoor.second == rctIt->second, the program loops forever

               //if the door is closed, then don't calc distances
               //if (!_CroTrByUID.at(*otherDoor)->IsOpen()) {
               //     continue;
               //}

               // if the two doors are not within the same subroom, do not consider (ar.graf)
               // should fix problems of oscillation caused by doorgaps in the distancegraph
               int thisUID1 = (_CroTrByUID.at(rctIt->second)->GetSubRoom1()) ? _CroTrByUID.at(rctIt->second)->GetSubRoom1()->GetUID() : -10 ;
               int thisUID2 = (_CroTrByUID.at(rctIt->second)->GetSubRoom2()) ? _CroTrByUID.at(rctIt->second)->GetSubRoom2()->GetUID() : -20 ;
               int otherUID1 = (_CroTrByUID.at(otherDoor.second)->GetSubRoom1()) ? _CroTrByUID.at(otherDoor.second)->GetSubRoom1()->GetUID() : -30 ;
               int otherUID2 = (_CroTrByUID.at(otherDoor.second)->GetSubRoom2()) ? _CroTrByUID.at(otherDoor.second)->GetSubRoom2()->GetUID() : -40 ;

               if (
                         (thisUID1 != otherUID1) &&
                         (thisUID1 != otherUID2) &&
                         (thisUID2 != otherUID1) &&
                         (thisUID2 != otherUID2)      ) {
                    continue;
               }

               UnivFFviaFM* locffptr = _locffviafm[rctIt->first];
               double tempDistance = locffptr->getDistanceBetweenDoors(rctIt->second, otherDoor.second);

               if (tempDistance < locffptr->getGrid()->Gethx()) {
                    Log->Write("WARNING:\tIgnoring distance of doors %d and %d because it is too small: %f",rctIt->second, otherDoor.second, tempDistance);
                    //Log->Write("^^^^^^^^\tIf there are scattered subrooms, which are not connected, this is ok.");
                    continue;
               }
//
               std::pair<int, int> key_ij = std::make_pair(otherDoor.second, rctIt->second);
               std::pair<int, int> key_ji = std::make_pair(rctIt->second, otherDoor.second);


#pragma omp critical(_distMatrix)
               if (_distMatrix.at(key_ij) > tempDistance) {
                    _distMatrix.erase(key_ij);
                    _distMatrix.erase(key_ji);
                    _distMatrix.insert(std::make_pair(key_ij, tempDistance));
                    _distMatrix.insert(std::make_pair(key_ji, tempDistance));
               }
          } // otherDoor
     } // roomAndCroTrVector

     if (_config->get_has_directional_escalators()) {
         _directionalEscalatorsUID.clear();
         _penaltyList.clear();
         for (auto room : building->GetAllRooms()) {
             for (auto subroom : room.second->GetAllSubRooms()) {
                 if ((subroom.second->GetType() == "escalator_up") || (subroom.second->GetType() == "escalator_down")) {
                     _directionalEscalatorsUID.emplace_back(subroom.second->GetUID());
                 }
             }
         }
         for (int subUID : _directionalEscalatorsUID) {
             Escalator* escalator = (Escalator*) building->GetSubRoomByUID(subUID);
             std::vector<int> lineUIDs = escalator->GetAllGoalIDs();
             assert(lineUIDs.size() == 2);
             if (escalator->IsEscalatorUp()) {
                 if (_CroTrByUID[lineUIDs[0]]->IsInLineSegment(escalator->GetUp())) {
                     _penaltyList.emplace_back(std::make_pair(lineUIDs[0], lineUIDs[1]));
                 } else {
                     _penaltyList.emplace_back(std::make_pair(lineUIDs[1], lineUIDs[0]));
                 }
             } else { //IsEscalatorDown
                 if (_CroTrByUID[lineUIDs[0]]->IsInLineSegment(escalator->GetUp())) {
                     _penaltyList.emplace_back(std::make_pair(lineUIDs[1], lineUIDs[0]));
                 } else {
                     _penaltyList.emplace_back(std::make_pair(lineUIDs[0], lineUIDs[1]));
                 }
             }
         }
         for (auto key : _penaltyList) {
             _distMatrix.erase(key);
             _distMatrix.insert(std::make_pair(key, DBL_MAX));
         }
     }

     FloydWarshall();

     //debug output in file
//     _locffviafm[4]->writeFF("ffTreppe.vtk", _allDoorUIDs);

     //int roomTest = (*(_locffviafm.begin())).first;
     //int transTest = (building->GetRoom(roomTest)->GetAllTransitionsIDs())[0];
     //auto test = _CroTrByUID.at(1253);

     if (_config->get_write_VTK_files()) {
          for (unsigned int i = 0; i < _locffviafm.size(); ++i) {
               auto iter = _locffviafm.begin();
               std::advance(iter, i);
               int roomNr = iter->first;
Mohcine Chraibi's avatar
Mohcine Chraibi committed
302
               iter->second->writeFF("ffrouterRoom_" + std::to_string(roomNr) + "_t_"+ std::to_string(Pedestrian::GetGlobalTime()) + ".vtk", _allDoorUIDs);
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
          }
     }

//     std::ofstream matrixfile;
//     matrixfile.open("Matrix.txt");
//
//     for (auto mapItem : _distMatrix) {
//          matrixfile << mapItem.first.first << " to " << mapItem.first.second << " : " << mapItem.second << "\t via \t" << _pathsMatrix[mapItem.first];
//          matrixfile << "\t" << _CroTrByUID.at(mapItem.first.first)->GetID() << " to " << _CroTrByUID.at(mapItem.first.second)->GetID() << "\t via \t";
//          matrixfile << _CroTrByUID.at(_pathsMatrix[mapItem.first])->GetID() << std::endl;
//     }
//     matrixfile.close();
     Log->Write("INFO: \tFF Router Init done.");
     return true;
}

bool FFRouter::ReInit()
{
     //cleanse maps
     _distMatrix.clear();
     _pathsMatrix.clear();

     //init, yet no distances, only create map entries
     for(auto& id1 : _allDoorUIDs) {
          for(auto& id2 : _allDoorUIDs){
               std::pair<int, int> key   = std::make_pair(id1, id2);
               double              value = (id1 == id2)? 0.0 : DBL_MAX;
               //distMatrix[i][j] = 0,   if i==j
               //distMatrix[i][j] = max, else
               _distMatrix.insert(std::make_pair( key , value));
               //pathsMatrix[i][j] = i or j ? (follow wiki:path_reconstruction, it should be j)
               _pathsMatrix.insert(std::make_pair( key , id2 ));
          }
     }

     for (auto floorfield : _locffviafm) {
          floorfield.second->setSpeedMode(FF_PED_SPEED);
          //@todo: ar.graf: create a list of local ped-ptr instead of giving all peds-ptr
          floorfield.second->createPedSpeed(_building->GetAllPedestrians().data(), _building->GetAllPedestrians().size(), _mode, 1.);
          floorfield.second->recreateAllForQuickest();
          std::vector<int> allDoors(floorfield.second->getKnownDoorUIDs());
          for (auto firstDoor : allDoors) {
               for (auto secondDoor : allDoors) {
                    if (secondDoor <= firstDoor) continue; // calculate every path only once
                    // if the two doors are not within the same subroom, do not consider (ar.graf)
                    // should fix problems of oscillation caused by doorgaps in the distancegraph
                    int thisUID1 = (_CroTrByUID.at(firstDoor)->GetSubRoom1()) ? _CroTrByUID.at(firstDoor)->GetSubRoom1()->GetUID() : -10 ;
                    int thisUID2 = (_CroTrByUID.at(firstDoor)->GetSubRoom2()) ? _CroTrByUID.at(firstDoor)->GetSubRoom2()->GetUID() : -20 ;
                    int otherUID1 = (_CroTrByUID.at(secondDoor)->GetSubRoom1()) ? _CroTrByUID.at(secondDoor)->GetSubRoom1()->GetUID() : -30 ;
                    int otherUID2 = (_CroTrByUID.at(secondDoor)->GetSubRoom2()) ? _CroTrByUID.at(secondDoor)->GetSubRoom2()->GetUID() : -40 ;

                    if (
                              (thisUID1 != otherUID1) &&
                              (thisUID1 != otherUID2) &&
                              (thisUID2 != otherUID1) &&
                              (thisUID2 != otherUID2)      ) {
                         continue;
                    }

                    //double tempDistance = floorfield.second->getCostToDestination(firstDoor, _CroTrByUID.at(secondDoor)->GetCentre());
                    double tempDistance = floorfield.second->getDistanceBetweenDoors(firstDoor, secondDoor);
                    if (tempDistance < floorfield.second->getGrid()->Gethx()) {
                         Log->Write("WARNING:\tDistance of doors %d and %d is too small: %f",firstDoor, secondDoor, tempDistance);
                         //Log->Write("^^^^^^^^\tIf there are scattered subrooms, which are not connected, this is ok.");
                         continue;
                    }
                    std::pair<int, int> key_ij = std::make_pair(secondDoor, firstDoor);
                    std::pair<int, int> key_ji = std::make_pair(firstDoor, secondDoor);
                    if (_distMatrix.at(key_ij) > tempDistance) {
                         _distMatrix.erase(key_ij);
                         _distMatrix.erase(key_ji);
                         _distMatrix.insert(std::make_pair(key_ij, tempDistance));
                         _distMatrix.insert(std::make_pair(key_ji, tempDistance));
                    }
               } //secondDoor(s)
          } //firstDoor(s)
     } //allRooms

    if (_config->get_has_directional_escalators()) {
        _directionalEscalatorsUID.clear();
        _penaltyList.clear();
        for (auto room : _building->GetAllRooms()) {
            for (auto subroom : room.second->GetAllSubRooms()) {
                if ((subroom.second->GetType() == "escalator_up") || (subroom.second->GetType() == "escalator_down")) {
                    _directionalEscalatorsUID.emplace_back(subroom.second->GetUID());
                }
            }
        }
        for (int subUID : _directionalEscalatorsUID) {
            Escalator* escalator = (Escalator*) _building->GetSubRoomByUID(subUID);
            std::vector<int> lineUIDs = escalator->GetAllGoalIDs();
            assert(lineUIDs.size() == 2);
            if (escalator->IsEscalatorUp()) {
                if (_CroTrByUID[lineUIDs[0]]->IsInLineSegment(escalator->GetUp())) {
                    _penaltyList.emplace_back(std::make_pair(lineUIDs[0], lineUIDs[1]));
                } else {
                    _penaltyList.emplace_back(std::make_pair(lineUIDs[1], lineUIDs[0]));
                }
            } else { //IsEscalatorDown
                if (_CroTrByUID[lineUIDs[0]]->IsInLineSegment(escalator->GetUp())) {
                    _penaltyList.emplace_back(std::make_pair(lineUIDs[1], lineUIDs[0]));
                } else {
                    _penaltyList.emplace_back(std::make_pair(lineUIDs[0], lineUIDs[1]));
                }
            }
        }
        for (auto key : _penaltyList) {
            _distMatrix.erase(key);
            _distMatrix.insert(std::make_pair(key, DBL_MAX));
        }
    }

     FloydWarshall();
     _plzReInit = false;
     return true;
}



int FFRouter::FindExit(Pedestrian* p)
{
//     if (_mode == local_shortest) {
//          if ((_locffviafm.at(p->GetRoomID())->getGrid()->includesPoint(p->GetPos())) &&
//              (p->GetSubRoomUID() != _locffviafm.at(p->GetRoomID())->getSubroomUIDAt(p->GetPos()))) {
//               //pedestrian is still in the room, but changed subroom
//               _localShortestSafedPeds.emplace_back(p->GetID());
//          }
//
//          //if needed: quickest-mechanic part 2 of 2
//          if (!(_locffviafm.at(p->GetRoomID())->getGrid()->includesPoint(p->GetPos()))) {
//               //pedestrian left the room and therefore changed subroom
//               _localShortestSafedPeds.emplace_back(p->GetID());
//          }
//     }
     if (_mode == quickest) {
          if (p->GetGlobalTime() > _recalc_interval && _building->GetRoom(p->GetRoomID())->GetSubRoom(p->GetSubRoomID())->IsInSubRoom(p)
              && _locffviafm[p->GetRoomID()]->getCostToDestination(p->GetExitIndex(), p->GetPos()) > 3.0
                    && p->GetExitIndex() != -1) {
               //delay possible
               if ((int) p->GetGlobalTime() % 10 != p->GetID() % 10) {
                    return p->GetExitIndex();     //stay with old target
               }
          }
          //new version: recalc densityspeed every x seconds
          if ((p->GetGlobalTime() > _timeToRecalc) && (p->GetGlobalTime() > Pedestrian::GetMinPremovementTime() + _recalc_interval)) {
               _plzReInit = true;
          }
     }
     double minDist = DBL_MAX;
     int bestDoor = -1;

     int goalID = p->GetFinalDestination();
     std::vector<int> validFinalDoor; //UIDs of doors
     validFinalDoor.clear();
     if (goalID == -1) {
          for (auto& pairDoor : _ExitsByUID) {
               //we add the all exits,
               validFinalDoor.emplace_back(pairDoor.first); //UID
          }
     } else {  //only one specific goal, goalToLineUIDmap gets
               //populated in Init()
          if ((_goalToLineUIDmap.count(goalID) == 0) || (_goalToLineUIDmap[goalID] == -1)) {
               Log->Write("ERROR: \t ffRouter: unknown/unreachable goalID: %d in FindExit(Ped)",goalID);
          } else {
               validFinalDoor.emplace_back(_goalToLineUIDmap.at(goalID));
          }
     }

     std::vector<int> DoorUIDsOfRoom;
     DoorUIDsOfRoom.clear();
     if (_building->GetRoom(p->GetRoomID())->GetSubRoom(p->GetSubRoomID())->IsInSubRoom(p->GetPos())) {
          //ped is in the subroom, according to its member attribs
     } else {
          bool located = false;
          SubRoom* oldSubRoom = _building->GetRoom(p->GetRoomID())->GetSubRoom(p->GetSubRoomID());
          for (auto& room : _building->GetAllRooms()) {
               if (located) {break;}
               for (auto& subroom : room.second->GetAllSubRooms()) {
                    if (subroom.second->IsInSubRoom(p->GetPos()) && subroom.second->IsDirectlyConnectedWith(oldSubRoom)) {
                         //maybe room on wrong floor
                         p->SetRoomID(room.second->GetID(), room.second->GetCaption());
                         p->SetSubRoomID(subroom.second->GetSubRoomID());
                         p->SetSubRoomUID(subroom.second->GetUID());
                         located = true;
                         break;
                    }
               }
          }
          if (!located) { //ped is outside
               return -1;
          }
     }
     DoorUIDsOfRoom.clear();
     if (!_targetWithinSubroom) {
          //candidates of current room (ID) (provided by Room)
          for (auto transUID : _building->GetRoom(p->GetRoomID())->GetAllTransitionsIDs()) {
499 500
               // TODO if ((_CroTrByUID.count(transUID) != 0) && (_CroTrByUID[transUID]->IsOpen())) {
               if ((_CroTrByUID.count(transUID) != 0) && (!_CroTrByUID[transUID]->IsClose())) {
501 502 503 504 505
                    DoorUIDsOfRoom.emplace_back(transUID);
               }
          }
          for (auto &subIPair : _building->GetRoom(p->GetRoomID())->GetAllSubRooms()) {
               for (auto &crossI : subIPair.second->GetAllCrossings()) {
506 507
                    // TODO if (crossI->IsOpen()) {
                    if (!crossI->IsClose()) {
508 509 510 511 512 513 514 515 516
                         DoorUIDsOfRoom.emplace_back(crossI->GetUniqueID());
                    }
               }
          }
     }
     else
     {
          //candidates of current subroom only
          for (auto &crossI : _building->GetRoom(p->GetRoomID())->GetSubRoom(p->GetSubRoomID())->GetAllCrossings()) {
517 518
               //TODO if (crossI->IsOpen()) {
               if (!crossI->IsClose()) {
519 520 521 522 523
                    DoorUIDsOfRoom.emplace_back(crossI->GetUniqueID());
               }
          }

          for (auto &transI : _building->GetRoom(p->GetRoomID())->GetSubRoom(p->GetSubRoomID())->GetAllTransitions()) {
524 525
               //TODO if (transI->IsClose()) {
               if (!transI->IsClose()) {
526 527 528 529 530 531 532 533 534 535 536 537
                    DoorUIDsOfRoom.emplace_back(transI->GetUniqueID());
               }
          }
     }

     int bestFinalDoor = -1; // to silence the compiler
     for(int finalDoor : validFinalDoor) {
          //with UIDs, we can ask for shortest path
          for (int doorUID : DoorUIDsOfRoom) {
               //double locDistToDoor = _locffviafm[p->GetRoomID()]->getCostToDestination(doorUID, p->GetPos(), _mode);
               double locDistToDoor = 0.;
               if (_targetWithinSubroom) {
538
                   locDistToDoor = _config->get_dirStrategy()->GetDistance2Target(p, doorUID);
539
               } else {
540
                   locDistToDoor = _config->get_dirStrategy()->GetDistance2Target(p, doorUID);
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
               }

               if (locDistToDoor < -J_EPS) {     //for old ff: //this can happen, if the point is not reachable and therefore has init val -7
                    continue;
               }
               std::pair<int, int> key = std::make_pair(doorUID, finalDoor);
               //auto subroomDoors = _building->GetSubRoomByUID(p->GetSubRoomUID())->GetAllGoalIDs();
               //only consider, if paths exists
               if (_pathsMatrix.count(key)==0) {
                    Log->Write("no key for %d %d", key.first, key.second);
                    continue;
               }

               if ((_distMatrix.count(key)!=0) && (_distMatrix.at(key) != DBL_MAX)) {
                    if ((_distMatrix.at(key) + locDistToDoor) < minDist) {
                         minDist = _distMatrix.at(key) + locDistToDoor;
                         bestDoor = key.first; //doorUID
                         //if (locDistToDoor == 0.) {
                         if (true) {
                             auto subroomDoors = _building->GetSubRoomByUID(p->GetSubRoomUID())->GetAllGoalIDs();
                             if (std::find(subroomDoors.begin(), subroomDoors.end(), _pathsMatrix[key]) != subroomDoors.end()) {
                                 bestDoor = _pathsMatrix[key]; //@todo: @ar.graf: check this hack
                             }
                         }
                         bestFinalDoor = key.second;
                    }
               }
          }
     }

     //at this point, bestDoor is either a crossing or a transition
     if ((!_targetWithinSubroom) && (_CroTrByUID.count(bestDoor) != 0)) {
          while (!_CroTrByUID[bestDoor]->IsTransition()) {
               std::pair<int, int> key = std::make_pair(bestDoor, bestFinalDoor);
               bestDoor = _pathsMatrix[key];
          }
     }

//#pragma omp critical(finalDoors)
//     _finalDoors.emplace(std::make_pair(p->GetID(), bestFinalDoor));

     if (_CroTrByUID.count(bestDoor)) {
          p->SetExitIndex(bestDoor);
          p->SetExitLine(_CroTrByUID.at(bestDoor));
     }
     return bestDoor; //-1 if no way was found, doorUID of best, if path found
}

void FFRouter::FloydWarshall()
{
     bool change = false;
     double savedDistance = 0.;
     int totalnum = _allDoorUIDs.size();
     for(int k = 0; k<totalnum; ++k) {
          for(int i = 0; i<totalnum; ++i) {
               for(int j= 0; j<totalnum; ++j) {
                    std::pair<int, int> key_ij = std::make_pair(_allDoorUIDs[i], _allDoorUIDs[j]);
                    std::pair<int, int> key_ik = std::make_pair(_allDoorUIDs[i], _allDoorUIDs[k]);
                    std::pair<int, int> key_kj = std::make_pair(_allDoorUIDs[k], _allDoorUIDs[j]);
                    if ((_distMatrix[key_ik] < DBL_MAX) && (_distMatrix[key_kj] < DBL_MAX) &&
                       (_distMatrix[key_ik] + _distMatrix[key_kj] < _distMatrix[key_ij]))
                    {
                         savedDistance = _distMatrix[key_ij] - _distMatrix[key_ik] - _distMatrix[key_kj];
                         _distMatrix.erase(key_ij);
                         _distMatrix.insert(std::make_pair(key_ij, _distMatrix[key_ik] + _distMatrix[key_kj]));
                         _pathsMatrix.erase(key_ij);
                         _pathsMatrix.insert(std::make_pair(key_ij, _pathsMatrix[key_ik]));
                         change = true;
                    }
               }
          }
     }
     if (change) {
          //Log->Write("Floyd nochmal!!! %f", savedDistance);
          FloydWarshall();
     } else {
          Log->Write("INFO:\t FloydWarshall done!");
     }
}

void FFRouter::SetMode(std::string s)
{
     if (s == "global_shortest"){
          _mode = global_shortest;
          return;
     }

     if (s == "quickest") {
          _mode = quickest;
          return;
     }

     _mode = global_shortest;
     return;
}

bool FFRouter::MustReInit() {
     return _plzReInit;
}

void FFRouter::SetRecalc(double t) {
     _timeToRecalc = t + _recalc_interval;
643 644 645 646
}

void FFRouter::Update(){
     this->ReInit();
647
}