Commit 6766950e authored by Mohcine Chraibi's avatar Mohcine Chraibi

🆕 Distribute source agents on a fixed position

parent 55fbde57
Pipeline #12992 passed with stages
in 6 minutes and 11 seconds
...@@ -172,6 +172,8 @@ bool PedDistributionParser::LoadPedDistribution(vector<std::shared_ptr<StartDist ...@@ -172,6 +172,8 @@ bool PedDistributionParser::LoadPedDistribution(vector<std::shared_ptr<StartDist
string str_greedy = xmltoa(e->Attribute("greedy"), "false"); string str_greedy = xmltoa(e->Attribute("greedy"), "false");
double time = xmltof(e->Attribute("time"), 0); double time = xmltof(e->Attribute("time"), 0);
int agent_id = xmltoi(e->Attribute("agent_id"), -1); int agent_id = xmltoi(e->Attribute("agent_id"), -1);
float startx = xmltof(e->Attribute("startX"), std::numeric_limits<float>::quiet_NaN());
float starty = xmltof(e->Attribute("startY"), std::numeric_limits<float>::quiet_NaN());
bool greedy = (str_greedy == "true")?true:false; bool greedy = (str_greedy == "true")?true:false;
if (agent_id >= 0){ if (agent_id >= 0){
agents_max = 1; agents_max = 1;
...@@ -179,7 +181,7 @@ bool PedDistributionParser::LoadPedDistribution(vector<std::shared_ptr<StartDist ...@@ -179,7 +181,7 @@ bool PedDistributionParser::LoadPedDistribution(vector<std::shared_ptr<StartDist
} }
auto source = std::shared_ptr<AgentsSource>( auto source = std::shared_ptr<AgentsSource>(
new AgentsSource(id, caption, agents_max, group_id, new AgentsSource(id, caption, agents_max, group_id,
frequency, greedy, time, agent_id)); frequency, greedy, time, agent_id, startx, starty));
startDisSources.push_back(source); startDisSources.push_back(source);
Log->Write("INFO:\tSource with id %d will be parsed (greedy = %d)!", id, greedy); Log->Write("INFO:\tSource with id %d will be parsed (greedy = %d)!", id, greedy);
...@@ -188,9 +190,3 @@ bool PedDistributionParser::LoadPedDistribution(vector<std::shared_ptr<StartDist ...@@ -188,9 +190,3 @@ bool PedDistributionParser::LoadPedDistribution(vector<std::shared_ptr<StartDist
Log->Write("INFO: \t...Done"); Log->Write("INFO: \t...Done");
return true; return true;
} }
...@@ -30,8 +30,9 @@ ...@@ -30,8 +30,9 @@
#include "AgentsSource.h" #include "AgentsSource.h"
#include "Pedestrian.h" #include "Pedestrian.h"
AgentsSource::AgentsSource(int id, const std::string& caption,int max_agents,int group_id,int frequency, bool greedy, double time, int agent_id): AgentsSource::AgentsSource(int id, const std::string& caption,int max_agents,int group_id,int frequency, bool greedy, double time, int agent_id, float startx, float starty):
_id(id), _frequency(frequency), _maxAgents(max_agents), _groupID(group_id), _caption(caption), _greedy(greedy), _agent_id(agent_id), _time(time) _id(id), _frequency(frequency), _maxAgents(max_agents), _groupID(group_id), _caption(caption), _greedy(greedy), _agent_id(agent_id), _time(time), _startx(startx), _starty(starty)
{ {
_agentsGenerated=0; _agentsGenerated=0;
_boundaries[0] = 0; _boundaries[0] = 0;
...@@ -145,6 +146,18 @@ int AgentsSource::GetMaxAgents() const ...@@ -145,6 +146,18 @@ int AgentsSource::GetMaxAgents() const
return _maxAgents; return _maxAgents;
} }
float AgentsSource::GetStartX() const
{
return _startx;
}
float AgentsSource::GetStartY() const
{
return _starty;
}
void AgentsSource::SetStartDistribution(std::shared_ptr<StartDistribution> startDistribution) void AgentsSource::SetStartDistribution(std::shared_ptr<StartDistribution> startDistribution)
{ {
_startDistribution=startDistribution; _startDistribution=startDistribution;
...@@ -167,10 +180,10 @@ void AgentsSource::GenerateAgents(std::vector<Pedestrian*>& peds, int count, Bui ...@@ -167,10 +180,10 @@ void AgentsSource::GenerateAgents(std::vector<Pedestrian*>& peds, int count, Bui
// for (const auto &source: _start_dis_sources) // for (const auto &source: _start_dis_sources)
// if(source->GetAgentId() >= 0) // if(source->GetAgentId() >= 0)
// reserved_ids.push_back(source->GetAgentId()); // reserved_ids.push_back(source->GetAgentId());
// while( std::find(reserved_ids.begin(), reserved_ids.end(), pid) != reserved_ids.end() ){ // while( std::find(reserved_ids.begin(), reserved_ids.end(), pid) != reserved_ids.end() ){
// std::cout << "\n\nSOURCE SORRY " << pid << " is reserved!\n"; // std::cout << "\n\nSOURCE SORRY " << pid << " is reserved!\n";
// pid += 1; // pid += 1;
// } // }
// } // }
......
...@@ -49,7 +49,7 @@ public: ...@@ -49,7 +49,7 @@ public:
/** /**
* Constructor * Constructor
*/ */
AgentsSource(int id, const std::string& caption,int max_agents,int group_id,int frequency, bool greedy, double time, int agent_id); AgentsSource(int id, const std::string& caption,int max_agents,int group_id,int frequency, bool greedy, double time, int agent_id, float startx, float starty);
/** /**
* Destructor * Destructor
...@@ -111,6 +111,8 @@ public: ...@@ -111,6 +111,8 @@ public:
int GetGroupId() const; int GetGroupId() const;
int GetId() const; int GetId() const;
int GetAgentId() const; int GetAgentId() const;
float GetStartX() const;
float GetStartY() const;
double GetPlanTime() const; double GetPlanTime() const;
int GetMaxAgents() const; int GetMaxAgents() const;
bool Greedy() const; bool Greedy() const;
...@@ -129,10 +131,11 @@ private: ...@@ -129,10 +131,11 @@ private:
double _boundaries[4];// = { 0, 0, 0, 0 }; double _boundaries[4];// = { 0, 0, 0, 0 };
int _agent_id; int _agent_id;
double _time; double _time;
float _startx;
float _starty;
std::vector<Pedestrian*> _agents; std::vector<Pedestrian*> _agents;
std::shared_ptr<StartDistribution> _startDistribution; std::shared_ptr<StartDistribution> _startDistribution;
}; };
#endif /* AGENTSOURCE_H_ */ #endif /* AGENTSOURCE_H_ */
...@@ -122,14 +122,19 @@ bool AgentsSourcesManager::ProcessAllSources() const ...@@ -122,14 +122,19 @@ bool AgentsSourcesManager::ProcessAllSources() const
src->RemoveAgentsFromPool(peds, src->GetFrequency()); src->RemoveAgentsFromPool(peds, src->GetFrequency());
source_peds.reserve(source_peds.size() + peds.size()); source_peds.reserve(source_peds.size() + peds.size());
Log->Write("> INFO:\tSource %d generating %d agents (%d remaining)\n",src->GetId(),peds.size(),src->GetPoolSize()); Log->Write("\nINFO:\tSource %d generating %d agents (%d remaining)\n",src->GetId(),peds.size(),src->GetPoolSize());
printf("INFO:\tSource %d generating %d agents (%d remaining)\n",src->GetId(), peds.size(),src->GetPoolSize()); printf("\nINFO:\tSource %d generating %lu agents (%d remaining)\n",src->GetId(), peds.size(),src->GetPoolSize());
//ComputeBestPositionRandom(src.get(), peds); //ComputeBestPositionRandom(src.get(), peds);
//todo: here every pedestrian needs an exitline //todo: here every pedestrian needs an exitline
if(!std::isnan(src->GetStartX()) && !std::isnan(src->GetStartY()))
if( !ComputeBestPositionVoronoiBoost(src.get(), peds, _building, source_peds) ) {
Log->Write("WARNING:\tThere was no place for some pedestrians"); printf("INFO:\tSet source agent on fixed position (%.2f, %.2f)", src->GetStartX(), src->GetStartY());
InitFixedPosition(src.get(), peds);
}
else
if( !ComputeBestPositionVoronoiBoost(src.get(), peds, _building, source_peds) )
Log->Write("WARNING:\tThere was no place for some pedestrians");
source_peds.insert(source_peds.end(), peds.begin(), peds.end()); source_peds.insert(source_peds.end(), peds.begin(), peds.end());
/* std::cout << KRED << ">> Add to queue " << peds.size() << "\n" << RESET; */ /* std::cout << KRED << ">> Add to queue " << peds.size() << "\n" << RESET; */
...@@ -155,30 +160,29 @@ bool AgentsSourcesManager::ProcessAllSources() const ...@@ -155,30 +160,29 @@ bool AgentsSourcesManager::ProcessAllSources() const
// std::cout<< KBLU << "BUL: agentssourcesManager: " << pp->GetPos()._x << ", " << pp->GetPos()._y << RESET << std::endl; // std::cout<< KBLU << "BUL: agentssourcesManager: " << pp->GetPos()._x << ", " << pp->GetPos()._y << RESET << std::endl;
// //
/* std::cout << "========================\n"; */ /* std::cout << "========================\n"; */
return empty; return empty;
} }
//4 agents frequency, just for an example
void AgentsSourcesManager::ComputeBestPositionDummy(AgentsSource* src,
vector<Pedestrian*>& peds)const
{
peds[0]->SetPos( Point(10,5.5) );
peds[1]->SetPos( Point(10,4.9) );
peds[2]->SetPos( Point(10,4.3) );
peds[3]->SetPos( Point(10,3.7) );
/*peds[0]->SetPos( Point(10,5.4) );
peds[1]->SetPos( Point(10,4.6) );
peds[2]->SetPos( Point(10,3.8) );*/
for(auto&& ped : peds) void AgentsSourcesManager::InitFixedPosition(AgentsSource* src,
{ vector<Pedestrian*>& peds)const
Point v = (ped->GetExitLine()->ShortestPoint(ped->GetPos())- ped->GetPos()).Normalized(); {
double speed=ped->GetV0Norm(); for(auto&& ped : peds)
v=v*speed; {
ped->SetV(v); Point v;
if (ped->GetExitLine()) {
v = (ped->GetExitLine()->ShortestPoint(ped->GetPos())- ped->GetPos()).Normalized();
} else {
const NavLine L = Line(Point(0, 0), Point(1,1));
ped->SetExitLine(&L); // set dummy line
v = Point(0., 0.);
} }
double speed=ped->GetEllipse().GetV0();
v=v*speed;
ped->SetV(v);
ped->SetPos( Point(src->GetStartX(), src->GetStartY()) );
} }
}
void AgentsSourcesManager::ComputeBestPositionCompleteRandom(AgentsSource* src, void AgentsSourcesManager::ComputeBestPositionCompleteRandom(AgentsSource* src,
vector<Pedestrian*>& peds)const vector<Pedestrian*>& peds)const
......
...@@ -137,7 +137,7 @@ private: ...@@ -137,7 +137,7 @@ private:
void ComputeBestPositionVoronoi(AgentsSource* src, Pedestrian* agent) const; void ComputeBestPositionVoronoi(AgentsSource* src, Pedestrian* agent) const;
void ComputeBestPositionDummy(AgentsSource* src, std::vector<Pedestrian*>& peds) const; void InitFixedPosition(AgentsSource* src, std::vector<Pedestrian*>& peds) const;
/** /**
* Position incoming pedestrians completely random * Position incoming pedestrians completely random
......
...@@ -124,7 +124,7 @@ bool PedDistributor::Distribute(Building *building) const { ...@@ -124,7 +124,7 @@ bool PedDistributor::Distribute(Building *building) const {
auto tmpPositions = GetPositionsFromFile(p.string(), dist->GetAgentsNumber(), unit); auto tmpPositions = GetPositionsFromFile(p.string(), dist->GetAgentsNumber(), unit);
//check if positions are //check if positions are
//empty. May happen if file //empty. May happen if file
//is misformed. //is misformed.
if(tmpPositions.empty()){ if(tmpPositions.empty()){
Log->Write("ERROR: \tproblems with file <%s%s>.", basename.c_str(), extention.c_str()); Log->Write("ERROR: \tproblems with file <%s%s>.", basename.c_str(), extention.c_str());
return false; //maybe just ignore? return false; //maybe just ignore?
...@@ -136,12 +136,13 @@ bool PedDistributor::Distribute(Building *building) const { ...@@ -136,12 +136,13 @@ bool PedDistributor::Distribute(Building *building) const {
break; //leave BOOST_FOREEACH break; //leave BOOST_FOREEACH
}//regular file }//regular file
} // for files } // for files
if (fromDirectory == false){ if (fromDirectory == false){
Log->Write("ERROR: \tDistributing pedestrians using file is not successful."); Log->Write("ERROR: \tDistributing pedestrians using file is not successful.");
return false; return false;
} }
}// check if directory }// check if directory
}//if we have a directoy }//if we have a directoy
//------------------------------------- pack in function ------------ //------------------------------------- pack in function ------------
else{ else{
...@@ -409,7 +410,7 @@ const vector<Point> PedDistributor::GetPositionsFromFile(std::string filename, ...@@ -409,7 +410,7 @@ const vector<Point> PedDistributor::GetPositionsFromFile(std::string filename,
float m2cm = 1.0; float m2cm = 1.0;
if(unit == "cm") if(unit == "cm")
m2cm = 100.0; m2cm = 100.0;
std::ifstream infile(filename); std::ifstream infile(filename);
// read all data from file in xpos, ypos, ids and frames // read all data from file in xpos, ypos, ids and frames
// @todo: need to read z too // @todo: need to read z too
...@@ -462,10 +463,11 @@ const vector<Point> PedDistributor::GetPositionsFromFile(std::string filename, ...@@ -462,10 +463,11 @@ const vector<Point> PedDistributor::GetPositionsFromFile(std::string filename,
if(first_ids.size() != (unsigned)n){ if(first_ids.size() != (unsigned)n){
Log->Write("ERROR: \tGetPositionsFromFile: number of peds <%d> does not match number of peds from file <%d>", Log->Write("ERROR: \tGetPositionsFromFile: number of peds <%d> does not match number of peds from file <%d>",
n, first_ids.size()); n, first_ids.size());
positions.clear(); positions.clear();
} }
else else
Log->Write("ERROR: \tGetPositionsFromFile: number of peds <%d> in file. To simulate <%d>", first_ids.size(), n); Log->Write("INFO: \tGetPositionsFromFile: number of peds <%d> in file. To simulate <%d>", first_ids.size(), n);
return positions; return positions;
} }
...@@ -608,7 +610,3 @@ void PedDistributor::DistributeInSubRoom(int nAgents, vector<Point> &positions, ...@@ -608,7 +610,3 @@ void PedDistributor::DistributeInSubRoom(int nAgents, vector<Point> &positions,
building->AddPedestrian(ped); building->AddPedestrian(ped);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment