PedDistributor.cpp 21.8 KB
Newer Older
1
/**
Weichen's avatar
Weichen committed
2 3
 * \file        PedDistributor.cpp
 * \date        Oct 12, 2010
4 5
 * \version     v0.7
 * \copyright   <2009-2015> Forschungszentrum Jülich GmbH. All rights reserved.
Ulrich Kemloh's avatar
Ulrich Kemloh committed
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
 **/
27 28


Mohcine Chraibi's avatar
Mohcine Chraibi committed
29
#include <cmath>
Ulrich Kemloh's avatar
Ulrich Kemloh committed
30
#include "PedDistributor.h"
Ulrich Kemloh's avatar
Ulrich Kemloh committed
31
#include "../pedestrian/Pedestrian.h"
Ulrich Kemloh's avatar
Ulrich Kemloh committed
32
#include "../geometry/SubRoom.h"
GrgrLmml's avatar
GrgrLmml committed
33 34
#include "../IO/PedDistributionParser.h"

Mohcine Chraibi's avatar
Mohcine Chraibi committed
35 36 37 38 39 40 41 42
#include <boost/filesystem.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/foreach.hpp>
#include <boost/range/combine.hpp>

using namespace std;
namespace fs = boost::filesystem;
using namespace boost::lambda;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
43 44 45 46
/************************************************************
 PedDistributor
 ************************************************************/

GrgrLmml's avatar
GrgrLmml committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
PedDistributor::PedDistributor(const Configuration *configuration) : _configuration(configuration) {
    _start_dis = vector<std::shared_ptr<StartDistribution> >();
    _start_dis_sub = vector<std::shared_ptr<StartDistribution> >();
    _start_dis_sources = vector<std::shared_ptr<AgentsSource> >();
    PedDistributionLoader * parser;

#ifdef _JPS_AS_A_SERVICE
    if(_configuration->GetRunAsService()) {
        parser = new PedDistributionFromProtobufLoader(_configuration);

    } else
#endif
    {
        parser = new PedDistributionParser(_configuration);
    }

    parser->LoadPedDistribution(_start_dis,_start_dis_sub,_start_dis_sources);
64
    delete parser;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
65 66 67
}


GrgrLmml's avatar
GrgrLmml committed
68
PedDistributor::~PedDistributor() {
69 70 71 72 73 74 75 76 77 78 79
//     for (unsigned int i = 0; i < _start_dis.size(); i++)
//     {
//          delete _start_dis[i];
//     }
//     for (unsigned int i = 0; i < _start_dis_sub.size(); i++)
//     {
//          delete _start_dis_sub[i];
//     }

//     _start_dis_sub.clear();
//     _start_dis.clear();
80 81
}

GrgrLmml's avatar
GrgrLmml committed
82 83
const vector<std::shared_ptr<AgentsSource> > &PedDistributor::GetAgentsSources() const {
    return _start_dis_sources;
84 85
}

GrgrLmml's avatar
GrgrLmml committed
86 87 88 89
bool PedDistributor::Distribute(Building *building) const {
    Log->Write("INFO: \tInit Distribute");
    int nPeds_is = 0;
    int nPeds_expected = 0;
90

91

GrgrLmml's avatar
GrgrLmml committed
92 93 94 95 96 97 98 99 100
    // store the position in a map since we are not computing for all rooms/subrooms.
    std::map<int, std::map<int, vector<Point> > > allFreePos;

    //collect the available positions for that subroom
    for (const auto &dist: _start_dis_sub) {
        nPeds_expected += dist->GetAgentsNumber();
        int roomID = dist->GetRoomId();
        Room *r = building->GetRoom(roomID);
        if (!r) return false;
101

GrgrLmml's avatar
GrgrLmml committed
102 103 104
        int subroomID = dist->GetSubroomID();
        SubRoom *sr = r->GetSubRoom(subroomID);
        if (!sr) return false;
105

GrgrLmml's avatar
GrgrLmml committed
106 107 108 109
        auto &allFreePosRoom = allFreePos[roomID];
        // the positions were already computed
        if (allFreePosRoom.count(subroomID) > 0)
            continue;
Mohcine Chraibi's avatar
Mohcine Chraibi committed
110
        // check if we should read positions from some file
Mohcine Chraibi's avatar
Mohcine Chraibi committed
111
        bool fromDirectory = false;
Mohcine Chraibi's avatar
Mohcine Chraibi committed
112 113
        if(dist->GetPositionsDir().length()){
              string directory = dist->GetPositionsDir();
Mohcine Chraibi's avatar
Mohcine Chraibi committed
114
              string unit = dist->GetUnitTraj();
Mohcine Chraibi's avatar
Mohcine Chraibi committed
115 116 117
              fs::path the_path(directory);
              if(fs::exists(directory) && fs::is_directory(directory)){
                    fs::directory_iterator it(the_path), eod;
Mohcine Chraibi's avatar
Mohcine Chraibi committed
118 119
                    BOOST_FOREACH(fs::path const &p, std::make_pair(it, eod))
                    {
Mohcine Chraibi's avatar
Mohcine Chraibi committed
120 121 122 123
                          if(fs::is_regular_file(p))
                          {
                               std::string basename = fs::basename(p);
                               std::string extention = fs::extension(p);
Mohcine Chraibi's avatar
Mohcine Chraibi committed
124
                               auto tmpPositions = GetPositionsFromFile(p.string(), dist->GetAgentsNumber(), unit);
125 126
                               //check if positions are
                               //empty. May happen if file
127
                               //is misformed.
128
                               if(tmpPositions.empty()){
Mohcine Chraibi's avatar
Mohcine Chraibi committed
129 130 131
                                              Log->Write("ERROR: \tproblems with file <%s%s>.", basename.c_str(), extention.c_str());
                                              return false; //maybe just ignore?
                               }
132 133 134 135 136
                               else
                                    allFreePosRoom[subroomID] = tmpPositions;
                               fromDirectory = true;
                               Log->Write("INFO: \tDistributing %d pedestrians using file <%s%s>", dist->GetAgentsNumber(), basename.c_str(), extention.c_str());
                               break; //leave BOOST_FOREEACH
Mohcine Chraibi's avatar
Mohcine Chraibi committed
137
                          }//regular file
Mohcine Chraibi's avatar
Mohcine Chraibi committed
138
                    } // for files
139
                    if (fromDirectory == false){
140
                         Log->Write("ERROR: \tDistributing pedestrians using file is not successful.");
141
                         return false;
142
                    }
Mohcine Chraibi's avatar
Mohcine Chraibi committed
143 144
              }// check if directory
        }//if we have a directoy
145

Mohcine Chraibi's avatar
Mohcine Chraibi committed
146 147
        //------------------------------------- pack in function ------------
        else{
148

Mohcine Chraibi's avatar
Mohcine Chraibi committed
149 150 151 152 153
             auto possibleSubroomPositions = PedDistributor::PossiblePositions(*sr);
             shuffle(possibleSubroomPositions.begin(), possibleSubroomPositions.end(), dist->GetGenerator());
             allFreePosRoom[subroomID] = possibleSubroomPositions;
        }
    } // for sub_dis
GrgrLmml's avatar
GrgrLmml committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

    //collect the available positions for that room
    for (const auto &dist: _start_dis) {
        int roomID = dist->GetRoomId();
        Room *r = building->GetRoom(roomID);
        if (!r) return false;

        nPeds_expected += dist->GetAgentsNumber();
        //compute all subrooms since no specific one is given
        for (const auto &it_sr: r->GetAllSubRooms()) {
            int subroomID = it_sr.second->GetSubRoomID();
            auto &allFreePosRoom = allFreePos[roomID];
            // the positions were already computed
            if (allFreePosRoom.count(subroomID) > 0)
                continue;
Mohcine Chraibi's avatar
Mohcine Chraibi committed
169
            auto possibleSubroomPositions = PedDistributor::PossiblePositions(*it_sr.second);
170
            shuffle(possibleSubroomPositions.begin(), possibleSubroomPositions.end(), dist->GetGenerator());
Mohcine Chraibi's avatar
Mohcine Chraibi committed
171
            allFreePosRoom[subroomID] = possibleSubroomPositions;
GrgrLmml's avatar
GrgrLmml committed
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
        }
    }


    // now proceed to the distribution
    int pid = 1; // the pedID is being increased throughout...

    for (const auto &dist: _start_dis_sub) {
        int room_id = dist->GetRoomId();
        Room *r = building->GetRoom(room_id);
        if (!r) continue;
        int roomID = r->GetID();
        int subroomID = dist->GetSubroomID();
        int N = dist->GetAgentsNumber();
        SubRoom *sr = r->GetSubRoom(subroomID);
        if (!sr) continue;

        if (N < 0) {
            Log->Write("ERROR: \t negative  number of pedestrians!");
            return false;
        }

        auto &allpos = allFreePos[roomID][subroomID];

        int max_pos = allpos.size();
        if (max_pos < N) {
            Log->Write("ERROR: \tCannot distribute %d agents in Room %d . Maximum allowed: %d\n",
                       N, roomID, allpos.size());
            return false;
        }
202
        if (N==0) continue;
GrgrLmml's avatar
GrgrLmml committed
203 204 205 206

        // Distributing
        Log->Write("INFO: \tDistributing %d Agents in Room/Subrom [%d/%d]! Maximum allowed: %d", N, roomID, subroomID,
                   max_pos);
207
        DistributeInSubRoom(N, allpos, &pid, dist.get(), building);
GrgrLmml's avatar
GrgrLmml committed
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
        Log->Write("\t...Done");
        nPeds_is += N;
    }

    // then continue the distribution according to the rooms
    for (const auto &dist: _start_dis) {
        int room_id = dist->GetRoomId();
        Room *r = building->GetRoom(room_id);
        if (!r) continue;
        int N = dist->GetAgentsNumber();
        if (N < 0) {
            Log->Write("ERROR: \t negative or null number of pedestrians! Ignoring");
            continue;
        }

        double sum_area = 0;
        int max_pos = 0;
        double ppm; // pedestrians per square meter
        int ges_anz = 0;
        vector<int> max_anz = vector<int>();
        vector<int> akt_anz = vector<int>();

        auto &allFreePosInRoom = allFreePos[room_id];
        //FIXME: wont work if the subrooms ids are not continous, consider using map
        for (int is = 0; is < r->GetNumberOfSubRooms(); is++) {
            SubRoom *sr = r->GetSubRoom(is);
            double area = sr->GetArea();
            sum_area += area;
            int anz = allFreePosInRoom[is].size();
            max_anz.push_back(anz);
            max_pos += anz;
        }
        if (max_pos < N) {
            Log->Write("ERROR: \t Distribution of %d pedestrians in Room %d not possible! Maximum allowed: %d\n",
                       N, r->GetID(), max_pos);
            return false;
        }
        ppm = N / sum_area;
        // Anzahl der Personen pro SubRoom bestimmen
        //FIXME: wont work if the subrooms ids are not continous, consider using map
        for (int is = 0; is < r->GetNumberOfSubRooms(); is++) {
            SubRoom *sr = r->GetSubRoom(is);
            int anz = sr->GetArea() * ppm + 0.5; // wird absichtlich gerundet
            while (anz > max_anz[is]) {
                anz--;
            }
            akt_anz.push_back(anz);
            ges_anz += anz;
        }
        // Falls N noch nicht ganz erreicht, von vorne jeweils eine Person dazu
        int j = 0;
        while (ges_anz < N) {
            if (akt_anz[j] < max_anz[j]) {
                akt_anz[j] = akt_anz[j] + 1;
                ges_anz++;
            }
            j = (j + 1) % max_anz.size();
        }
        j = 0;
        while (ges_anz > N) {
            if (akt_anz[j] > 0) {
                akt_anz[j] = akt_anz[j] - 1;
                ges_anz--;
            }
            j = (j + 1) % max_anz.size();
        }
        // distributing
        for (unsigned int is = 0; is < akt_anz.size(); is++) {
            SubRoom *sr = r->GetSubRoom(is);
            //workaround, the subroom will be changing at each run.
            //so the last subroom ID is not necessarily the 'real' one
            // might conflicts with sources
            dist->SetSubroomID(sr->GetSubRoomID());
            //dist->SetSubroomUID(sr->GetSubRoomUID())
            if (akt_anz[is] > 0) {
283
                DistributeInSubRoom(akt_anz[is], allFreePosInRoom[is], &pid, dist.get(), building);
GrgrLmml's avatar
GrgrLmml committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
            }
        }
        nPeds_is += N;
    }

    //now populate the sources
    vector<Point> emptyPositions;
    for (const auto &source: _start_dis_sources) {
        for (const auto &dist: _start_dis_sub) {
            if (source->GetGroupId() == dist->GetGroupId()) {
                source->SetStartDistribution(dist);
                for (int i = 0; i < source->GetMaxAgents(); i++) {
                    //source->AddToPool(dist->GenerateAgent(building, &pid,emptyPositions));
                    //nPeds_is++;
                }
            }
        }

        for (const auto &dist: _start_dis) {
            if (source->GetGroupId() == dist->GetGroupId()) {
                source->SetStartDistribution(dist);
                for (int i = 0; i < source->GetMaxAgents(); i++) {
                    //source->AddToPool(dist->GenerateAgent(building, &pid,emptyPositions));
                    //nPeds_is++;
                }
            }
        }
    }
312

GrgrLmml's avatar
GrgrLmml committed
313 314 315
    if (nPeds_is != nPeds_expected) {
        Log->Write("ERROR:\t only [%d] agents could be distributed out of [%d] requested", nPeds_is, nPeds_expected);
    }
316

GrgrLmml's avatar
GrgrLmml committed
317
    return (nPeds_is == nPeds_expected);
318 319
}

320

Ulrich Kemloh's avatar
Ulrich Kemloh committed
321
vector<Point> PedDistributor::PositionsOnFixX(double min_x, double max_x, double min_y, double max_y,
GrgrLmml's avatar
GrgrLmml committed
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
                                              const SubRoom &r, double bufx, double bufy, double dy) {
    vector<Point> positions;
    double x = (max_x + min_x) * 0.5;
    double y = min_y;

    while (y < max_y) {
        Point pos = Point(x, y);
        // Abstand zu allen Wänden prüfen
        bool ok = true;
        for (auto &&w: r.GetAllWalls()) {
            if (w.DistTo(pos) < max(bufx, bufy) || !r.IsInSubRoom(pos)) {
                ok = false;
                break; // Punkt ist zu nah an einer Wand oder nicht im Raum => ungültig
            }
        }

        if (ok) {
            //check all transitions
            bool tooNear = false;
            for (unsigned int t = 0; t < r.GetAllTransitions().size(); t++) {
                if (r.GetTransition(t)->DistTo(pos) < J_EPS_GOAL) {
                    //too close
                    tooNear = true;
                    break;
                }
            }

            for (unsigned int c = 0; c < r.GetAllCrossings().size(); c++) {
                if (r.GetCrossing(c)->DistTo(pos) < J_EPS_GOAL) {
                    //too close
                    tooNear = true;
                    break;
                }
            }
            if (tooNear == false) positions.push_back(pos);
        }
        y += dy;
    }
    return positions;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
361 362 363 364

}

vector<Point>PedDistributor::PositionsOnFixY(double min_x, double max_x, double min_y, double max_y,
GrgrLmml's avatar
GrgrLmml committed
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
                                             const SubRoom &r, double bufx, double bufy, double dx) {
    vector<Point> positions;
    double y = (max_y + min_y) * 0.5;
    double x = min_x;

    while (x < max_x) {
        Point pos = Point(x, y);
        // check distance to wall
        bool ok = true;
        for (auto &&w: r.GetAllWalls()) {
            if (w.DistTo(pos) < max(bufx, bufy) || !r.IsInSubRoom(pos)) {
                ok = false;
                break; // Punkt ist zu nah an einer Wand oder nicht im Raum => ungültig
            }
        }

        if (ok) {

            //check all transitions
            bool tooNear = false;
            for (unsigned int t = 0; t < r.GetAllTransitions().size(); t++) {
                if (r.GetTransition(t)->DistTo(pos) < J_EPS_GOAL) {
                    //too close
                    tooNear = true;
                    break;
                }
            }

            for (unsigned int c = 0; c < r.GetAllCrossings().size(); c++) {
                if (r.GetCrossing(c)->DistTo(pos) < J_EPS_GOAL) {
                    //too close
                    tooNear = true;
                    break;
                }
            }
            if (tooNear == false) positions.push_back(pos);
        }
        x += dx;
    }
    return positions;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
405 406
}

Mohcine Chraibi's avatar
Mohcine Chraibi committed
407
// format: id fr x y
408

Mohcine Chraibi's avatar
Mohcine Chraibi committed
409 410 411 412
const vector<Point>  PedDistributor::GetPositionsFromFile(std::string filename, int n, std::string unit) const{
      float m2cm = 1.0;
      if(unit == "cm")
            m2cm = 100.0;
413

Mohcine Chraibi's avatar
Mohcine Chraibi committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
      std::ifstream infile(filename);
      // read all data from file in xpos, ypos, ids and frames
      // @todo: need to read z too
      std::vector<double> xpos;
      std::vector<double> ypos;
      std::vector<int> ids;
      std::vector<int> frames;
      // here we push_back only the first (x,y) of every id.
      std::vector<Point> positions;
      std::vector<int> first_ids;
      if (infile.good()){
           std::string sLine;
            std::vector<std::string> strs;
            float x, y;
            int id, fr;
            while(getline(infile, sLine))
            {
                  if ( sLine[0] != '#' && !(sLine.empty()) )
Mohcine Chraibi's avatar
Mohcine Chraibi committed
432
                  {
Mohcine Chraibi's avatar
Mohcine Chraibi committed
433 434
                        boost::split(strs, sLine, boost::is_any_of("\t "));
                        id = atoi(strs[0].c_str());
Mohcine Chraibi's avatar
Mohcine Chraibi committed
435
                        fr = atoi(strs[1].c_str());
Mohcine Chraibi's avatar
Mohcine Chraibi committed
436 437
                        x =  atof(strs[2].c_str())/m2cm;
                        y = atof(strs[3].c_str())/m2cm;
Mohcine Chraibi's avatar
Mohcine Chraibi committed
438
                        // @todo: check for z component. Some data don't have. Some do.
Mohcine Chraibi's avatar
Mohcine Chraibi committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
                        xpos.push_back(x);
                        ypos.push_back(y);
                        ids.push_back(id);
                        frames.push_back(fr);
                  }
            }
      }
      infile.close();
      // now extract the first coordinates
      for (auto tup : boost::combine(ids, frames, xpos, ypos)) {
            float x, y;
            int id, fr;
            boost::tie(id, fr, x, y) = tup;

            auto it = std::find(first_ids.begin(), first_ids.end(), id);

            if (it == first_ids.end()) { // <id> is not yet in first_ids
                  Point pos(x, y);
                  positions.push_back(pos);
                  first_ids.push_back(id);
Mohcine Chraibi's avatar
Mohcine Chraibi committed
459
            }
Mohcine Chraibi's avatar
Mohcine Chraibi committed
460 461 462 463 464 465
            else
                 continue;
      }
      if(first_ids.size() != (unsigned)n){
           Log->Write("ERROR: \tGetPositionsFromFile: number of peds <%d> does not match number of peds from file <%d>",
                      n, first_ids.size());
466

Mohcine Chraibi's avatar
Mohcine Chraibi committed
467 468
           positions.clear();
      }
469
      else
470
           Log->Write("INFO: \tGetPositionsFromFile: number of peds <%d> in file. To simulate <%d>", first_ids.size(), n);
Mohcine Chraibi's avatar
Mohcine Chraibi committed
471 472 473
      return positions;
}

474

GrgrLmml's avatar
GrgrLmml committed
475 476 477 478 479 480 481 482 483 484 485 486
vector<Point>  PedDistributor::PossiblePositions(const SubRoom &r) {
    double uni = 0.7; // wenn ein Raum in x oder y -Richtung schmaler ist als 0.7 wird in der Mitte verteilt
    double bufx = 0.12;
    double bufy = 0.12;

    double amin = 0.18; // = GetAmin()->GetMean();
    double bmax = 0.25; // = GetBmax()->GetMean();

    double dx = amin + bufx;
    double dy = bmax + bufy;

    double max_buf = max(bufx, bufy);
487

GrgrLmml's avatar
GrgrLmml committed
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
    vector<double>::iterator min_x, max_x, min_y, max_y;
    const vector<Point> &poly = r.GetPolygon();
    vector<Point> all_positions;
    vector<double> xs;
    vector<double> ys;

    for (int p = 0; p < (int) poly.size(); ++p) {
        xs.push_back(poly[p]._x);
        ys.push_back(poly[p]._y);
    }

    min_x = min_element(xs.begin(), xs.end());
    max_x = max_element(xs.begin(), xs.end());
    min_y = min_element(ys.begin(), ys.end());
    max_y = max_element(ys.begin(), ys.end());

    if (*max_y - *min_y < uni) {
        all_positions = PositionsOnFixY(*min_x, *max_x, *min_y, *max_y, r, bufx, bufy, dx);
    } else if (*max_x - *min_x < uni) {
        all_positions = PositionsOnFixX(*min_x, *max_x, *min_y, *max_y, r, bufx, bufy, dy);
    } else {
        // create the grid
        double x = (*min_x);
        while (x < *max_x) {
            double y = (*min_y);
            while (y < *max_y) {
                y += dy;
                Point pos = Point(x, y);
                bool tooNear = false;

                //skip if the position is not in the polygon
                if (!r.IsInSubRoom(pos)) continue;

                // check the distance to all Walls
                for (const auto &w : r.GetAllWalls()) {
                    if (w.DistTo(pos) < max_buf) {
                        tooNear = true;
                        break; // too close
                    }
                }

                //check the distance to all transitions
                if (tooNear == true) continue;
                for (const auto &t : r.GetAllTransitions()) {
                    if (t->DistTo(pos) < max_buf) {
                        //too close
                        tooNear = true;
                        break;
536
                    }
GrgrLmml's avatar
GrgrLmml committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550
                }

                //  and check the distance to all crossings
                if (tooNear == true) continue;
                for (const auto &c : r.GetAllCrossings()) {
                    if (c->DistTo(pos) < max_buf) {
                        //too close
                        tooNear = true;
                        break;
                    }
                }

                // and finally the distance to all opened obstacles
                if (tooNear == true) continue;
551

GrgrLmml's avatar
GrgrLmml committed
552 553 554 555 556 557
                for (const auto &obst : r.GetAllObstacles()) {
                    for (const auto &wall : obst->GetAllWalls()) {
                        if (wall.DistTo(pos) < max_buf) {
                            tooNear = true;
                            break; // too close
                        }
558 559
                    }

GrgrLmml's avatar
GrgrLmml committed
560 561 562 563 564 565
                    //only continue if...
                    if (tooNear == true) continue;

                    if (obst->Contains(pos)) {
                        tooNear = true;
                        break; // too close
566
                    }
GrgrLmml's avatar
GrgrLmml committed
567
                }
568

GrgrLmml's avatar
GrgrLmml committed
569 570 571 572 573 574
                if (tooNear == false) all_positions.push_back(pos);
            }
            x += dx;
        }
    }
    return all_positions;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
575
}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
576

Ulrich Kemloh's avatar
Ulrich Kemloh committed
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
/* Verteilt N Fußgänger in SubRoom r
 * Algorithms:
 *   - Lege Gitter von min_x bis max_x mit Abstand dx und entsprechend min_y bis max_y mit
 *     Abstand dy
 *   - Prüfe alle so enstandenen Gitterpunkte, ob ihr Abstand zu Wänden und Übergängen aus
 *     reicht
 *   - Wenn nicht verwerfe Punkt, wenn ja merke Punkt in Vector positions
 *   - Wähle zufällig einen Punkt aus dem Vektor aus Position der einzelnen Fußgänger
 * Parameter:
 *   - r:       SubRoom in den verteilt wird
 *   - N:       Anzahl der Fußgänger für SubRoom r
 *   - pid:     fortlaufender Index der Fußgänger (wird auch wieder zurückgegeben, für den
 *              nächsten Aufruf)
 *   - routing: wird benötigt um die Zielline der Fußgänger zu initialisieren
 * */
592
void PedDistributor::DistributeInSubRoom(int nAgents, vector<Point> &positions, int *pid,
GrgrLmml's avatar
GrgrLmml committed
593
                                         StartDistribution *para, Building *building) const {
Mohcine Chraibi's avatar
Mohcine Chraibi committed
594 595 596 597 598 599 600 601


     std::vector<int> reserved_ids;
    for (const auto &source: _start_dis_sources) {
         if(source->GetAgentId() >=0)
              reserved_ids.push_back(source->GetAgentId());
    }

Ulrich Kemloh's avatar
Ulrich Kemloh committed
602
    // set the pedestrians
GrgrLmml's avatar
GrgrLmml committed
603
    for (int i = 0; i < nAgents; ++i) {
Mohcine Chraibi's avatar
Mohcine Chraibi committed
604 605 606 607 608
         // look for a not reserved id.
         while( std::find(reserved_ids.begin(), reserved_ids.end(), *pid) != reserved_ids.end() ){
              *pid += 1;
         }

GrgrLmml's avatar
GrgrLmml committed
609 610
        Pedestrian *ped = para->GenerateAgent(building, pid, positions);
        building->AddPedestrian(ped);
611
    }
612
}