AgentsSourcesManager.cpp 3.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * AgentsSourcesManager.cpp
 *
 *  Created on: 14.04.2015
 *      Author: piccolo
 */

#include "AgentsSourcesManager.h"
#include "Pedestrian.h"
#include "AgentsQueue.h"
#include "AgentsSource.h"
12 13 14
#include "../voronoi/VoronoiDiagramGenerator.h"
#include "../geometry/Building.h"

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include <iostream>
#include <thread>
#include <chrono>

using namespace std;

AgentsSourcesManager::AgentsSourcesManager()
{

}

AgentsSourcesManager::~AgentsSourcesManager()
{
}

void AgentsSourcesManager::operator()(int value)
{
     //the loop is updated each second.
     //it might be better to use a timer
     bool finished=false;
35
     long updateFrequency=5;// 1 second
36 37
     do
     {
38 39 40 41 42

          int current_time = Pedestrian::GetGlobalTime();

          if ( (current_time != _lastUpdateTime) &&
                    ((current_time % updateFrequency) == 0))
43 44
          {

45 46 47 48
               finished=true;
               for (const auto& src: _sources)
               {
                    if(src->GetPoolSize())
49
                    {
50 51 52 53 54 55 56 57 58 59 60 61 62
                         vector<Pedestrian*> peds;
                         src->GenerateByFrequency(peds);
                         AgentsQueue::Add(peds);

                         ComputeBestPosition(src.get());

                         // compute the optimal position for insertion
                         for (auto&& ped: peds)
                         {
                              ped->SetPos(Point(15,15),true);
                         }
                         finished=false;
                         //cout<<"Agents generated: "<<peds.size()<<endl;
63
                    }
64
                    //src->Dump();
65
               }
66
               _lastUpdateTime = current_time;
67
          }
68 69
          //wait some time
          std::this_thread::sleep_for(std::chrono::milliseconds(10));
70 71 72
     }while (!finished);
}

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 111 112 113 114 115 116 117
void AgentsSourcesManager::ComputeBestPosition(AgentsSource* src)
{
     int roomID=0;
     int subroomID=0;

     //Get all pedestrians in that location
     vector<Pedestrian*> peds;
     _building->GetPedestrians(roomID,subroomID,peds);

     // compute the cells and cut with the bounds
     const int count=peds.size();
     float xValues[count];
     float yValues[count];

     for(int i=0;i<count;i++)
     {
          xValues[i]=peds[i]->GetPos()._x;
          yValues[i]=peds[i]->GetPos()._y;
     }

     VoronoiDiagramGenerator vdg;
     vdg.generateVoronoi(xValues,yValues,count, -100,100,-100,100,3);
     vdg.resetIterator();
     vdg.resetVerticesIterator();

     float x1,y1;

     printf("\n------vertices---------\n");
     while(vdg.getNextVertex(x1,y1))
     {
          printf("GOT Point (%f,%f)\n",x1,y1);
     }


     //exit(0);
     // float x1,y1,x2,y2;
     //while(vdg.getNext(x1,y1,x2,y2))
     //{
     //     printf("GOT Line (%f,%f)->(%f,%f)\n",x1,y1,x2, y2);
     //
     //}
     //compute the best position
     exit(0);
}

118 119 120 121 122 123 124 125 126
void AgentsSourcesManager::AddSource(std::shared_ptr<AgentsSource> src)
{
     _sources.push_back(src);
}

const std::vector<std::shared_ptr<AgentsSource> >& AgentsSourcesManager::GetSources() const
{
     return _sources;
}
127 128 129 130 131

void AgentsSourcesManager::SetBuilding(Building* building)
{
   _building=building;
}