simulator.cpp 7.51 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
2
#include "simulator.h"

3
Simulator::Simulator() : globalTime_(0.0f), timeStep_(0.2f)
Pierre Kraemer's avatar
Pierre Kraemer committed
4
{
Pierre Kraemer's avatar
Pierre Kraemer committed
5
	srand(10);
Pierre Kraemer's avatar
Pierre Kraemer committed
6
7
	envMap_.init();
	std::cout << "setup scenario" << std::endl;
Thomas's avatar
Thomas committed
8
9
//	importAgents("myAgents.pos");
	setupScenario(700);
10
//	addPathsToAgents();
11
12
13
14

	unsigned nbAgents = agents_.size();

	for(unsigned int i = 0; i < nbAgents; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
15
16
17
18
	{
		agents_[i]->updateObstacleNeighbors();
		agents_[i]->updateAgentNeighbors();
	}
19
20
21
22
23

//	tc1 = new ThreadUpdateInfo(agents_, 0, nbAgents / 4);
//	tc2 = new ThreadUpdateInfo(agents_, nbAgents / 4, nbAgents / 2);
//	tc3 = new ThreadUpdateInfo(agents_, nbAgents / 2, nbAgents * 3 / 4);
//	tc4 = new ThreadUpdateInfo(agents_, nbAgents * 3 / 4, nbAgents);
Pierre Kraemer's avatar
Pierre Kraemer committed
24
25
26
27
28
29
30
31
32
33
34
35
}

Simulator::~Simulator()
{
	for (unsigned int i = 0; i < agents_.size(); ++i)
		delete agents_[i];
}

void Simulator::doStep()
{
	envMap_.clearUpdateCandidates();

Pierre Kraemer's avatar
Pierre Kraemer committed
36
	envMap_.map.setCurrentLevel(0);
37

Thomas's avatar
Thomas committed
38
39
40
41
42
43
44
	for (unsigned int i = 0; i < agents_.size(); ++i)
	{
		agents_[i]->updateObstacleNeighbors();
		agents_[i]->updateAgentNeighbors();
		agents_[i]->computePrefVelocity();
		agents_[i]->computeNewVelocity();
	}
45

Thomas's avatar
Thomas committed
46
47
48
49
//	ThreadUpdateInfo tc1(agents_,0,agents_.size()/2);
//	ThreadUpdateInfo tc2(agents_,agents_.size()/2,agents_.size());
//	boost::thread thread1(&ThreadUpdateInfo::run, &tc1);
//	boost::thread thread2(&ThreadUpdateInfo::run, &tc2);
50
51
52
53
54
55
56
57

//	std::vector<Dart> oldDarts;
//	oldDarts.reserve(agents_.size());
//	for (unsigned int i = 0; i < agents_.size(); ++i)
//	{
//		oldDarts.push_back(agents_[i]->part_.d);
//	}

Thomas's avatar
Thomas committed
58
59
//	thread1.join();
//	thread2.join();
60

Pierre Kraemer's avatar
Pierre Kraemer committed
61
	envMap_.map.setCurrentLevel(envMap_.map.getMaxLevel());
Pierre Kraemer's avatar
Pierre Kraemer committed
62
63
64
65

	for (unsigned int i = 0; i < agents_.size(); ++i)
	{
		Dart oldFace = agents_[i]->part_.d;
Pierre Kraemer's avatar
Pierre Kraemer committed
66
67
68

		if(!envMap_.map.isDartValid(agents_[i]->part_.d))
			std::cout << "  AGENT PART INVALID DART before update" << std::endl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
69
		agents_[i]->update();
Pierre Kraemer's avatar
Pierre Kraemer committed
70
71
72
		if(!envMap_.map.isDartValid(agents_[i]->part_.d))
			std::cout << "  AGENT PART INVALID DART after update" << std::endl ;

Pierre Kraemer's avatar
Pierre Kraemer committed
73
74
75
76
//		if(envMap_.map.getEmbedding(oldFace, FACE_ORBIT) != envMap_.map.getEmbedding(agents_[i]->part_.d, FACE_ORBIT))
		if(agents_[i]->part_.crossCell != CGoGN::Algo::MovingObjects::NO_CROSS)
//		switch(agents_[i]->part_.crossCell)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
77
//			case CGoGN::Algo::MovingObjects::CROSS_EDGE :
Pierre Kraemer's avatar
Pierre Kraemer committed
78
79
80
//				envMap_.agentChangeFaceThroughEdge(agents_[i]);
//				envMap_.addCoarsenCandidate(oldFace);
//				envMap_.addRefineCandidate(agents_[i]->part_.d);
Pierre Kraemer's avatar
Pierre Kraemer committed
81
82
83
//				break;
//			case CGoGN::Algo::MovingObjects::CROSS_OTHER :
				envMap_.agentChangeFace(agents_[i], oldFace);
84
//				envMap_.agentChangeFace(agents_[i], oldDarts[i]);
Pierre Kraemer's avatar
Pierre Kraemer committed
85
//				break;
Pierre Kraemer's avatar
Pierre Kraemer committed
86
87
		}

Pierre Kraemer's avatar
Pierre Kraemer committed
88
89
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
90
	envMap_.updateMap();
Pierre Kraemer's avatar
Pierre Kraemer committed
91
92

	globalTime_ += timeStep_;
Pierre Kraemer's avatar
Pierre Kraemer committed
93

Pierre Kraemer's avatar
Pierre Kraemer committed
94
	if(int(globalTime_ / timeStep_) % 2000 == 0)
Pierre Kraemer's avatar
Pierre Kraemer committed
95
	{
Pierre Kraemer's avatar
bla    
Pierre Kraemer committed
96
		std::cout << "swap goals (globalTime -> " << globalTime_ << ")" << std::endl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
97
98
		swapAgentsGoals();
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
99
100
}

101
void Simulator::setupScenario(unsigned int nbMaxAgent)
Pierre Kraemer's avatar
Pierre Kraemer committed
102
103
104
105
106
107
{
	/*
	* Add agents, specifying their start position, and store their goals on the
	* opposite side of the environment.
	*/
	Dart d = envMap_.map.begin();
Thomas's avatar
Thomas committed
108
	CellMarker filled(envMap_.map,FACE_ORBIT);
Pierre Kraemer's avatar
Pierre Kraemer committed
109

Pierre Kraemer's avatar
Pierre Kraemer committed
110
111
	unsigned int nbx = 10;
	unsigned int nby = 10;
112

Pierre Kraemer's avatar
Pierre Kraemer committed
113
	unsigned int bMax = nbMaxAgent / (nbx*nby);
114
115

	for (unsigned int i = 0; i < bMax && d != envMap_.map.end(); ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
116
117
118
119
120
121
122
123
	{
		bool found = false;
		VEC3 pos;
		Dart dCell;
		while(!found && d != envMap_.map.end())
		{
			if(!filled.isMarked(d) && !envMap_.buildingMark.isMarked(d))
			{
Thomas's avatar
Thomas committed
124
				filled.mark(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
125
				pos = Algo::Geometry::faceCentroid<PFP>(envMap_.map, d, envMap_.position);
126
				pos[2] = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
				dCell = d;
				found = true;
			}
			envMap_.map.next(d);
		}

		if(found)
		{
			float ecart = 3.0f;
			VEC3 posinit = VEC3(pos[0] - (float(nbx)/2.0f * ecart), pos[1] - (float(nby)/2.0f * ecart), pos[2]);
			for(unsigned int curx = 0; curx < nbx; ++curx)
			{			
				for(unsigned int cury = 0; cury < nby; ++cury)
				{
					VEC3 posagent = posinit + VEC3(ecart * curx, ecart * cury, 0.0f);
					agents_.push_back(new Agent(this, posagent, dCell));
					agents_.back()->goals_.push_back(posagent);
					agents_.back()->goals_.push_back(-1.0f * posagent);
Pierre Kraemer's avatar
Pierre Kraemer committed
145
					agents_.back()->curGoal_ = 1;
Pierre Kraemer's avatar
Pierre Kraemer committed
146
147
148
149
150
151
152
153
154
				}
			}
		}
	}
	std::cout << "nb agents : " << agents_.size() << std::endl;

	swapAgentsGoals();

	for(unsigned int i = 0; i < agents_.size(); ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
155
		envMap_.pushAgentInCells(agents_[i], agents_[i]->part_.d);
Pierre Kraemer's avatar
Pierre Kraemer committed
156
}
157

Pierre Kraemer's avatar
Pierre Kraemer committed
158
159
void Simulator::addPathsToAgents()
{
Thomas's avatar
Thomas committed
160
	//city
Pierre Kraemer's avatar
Pierre Kraemer committed
161
	unsigned int dartDistForPath = 50;
Pierre Kraemer's avatar
Pierre Kraemer committed
162
163
164
	for(unsigned int i = 0; i < agents_.size(); ++i)
	{
		agents_[i]->goals_.clear();
165

Pierre Kraemer's avatar
Pierre Kraemer committed
166
167
		Dart dStart = agents_[i]->part_.d;
		Dart dStop = dStart;
168
		for(unsigned int j = 0; envMap_.buildingMark.isMarked(dStop) || j < dartDistForPath+rand()*20 || envMap_.map.sameFace(dStop,dStart); ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
169
		{
Thomas's avatar
Thomas committed
170
			envMap_.map.next(dStop);
Pierre Kraemer's avatar
Pierre Kraemer committed
171
172
173
			if(dStop == envMap_.map.end())
				dStop = envMap_.map.begin();
		}
174
175
176

		std::vector<Dart> path = CGoGN::PathFinder::pathFindAStar<PFP>(envMap_.map, envMap_.position, dStart, dStop, envMap_.buildingMark);

Pierre Kraemer's avatar
Pierre Kraemer committed
177
		for(std::vector<Dart>::iterator it = path.begin(); it != path.end(); ++it)
178
179
180
181
182
		{
			VEC3 dest = Algo::Geometry::faceCentroid<PFP>(envMap_.map, *it, envMap_.position);
			dest[2]=0;
			agents_[i]->goals_.push_back(dest);
		}
183

Pierre Kraemer's avatar
Pierre Kraemer committed
184
		Dart dStop2 = dStop;
185
		for(unsigned int j = 0; envMap_.buildingMark.isMarked(dStop2) || j < dartDistForPath+rand()*20 || envMap_.map.sameFace(dStop,dStop2) || envMap_.map.sameFace(dStop2,dStart); ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
186
		{
Thomas's avatar
Thomas committed
187
			envMap_.map.next(dStop2);
Pierre Kraemer's avatar
Pierre Kraemer committed
188
189
190
			if(dStop2 == envMap_.map.end())
				dStop2 = envMap_.map.begin();
		}
191
192
193

		path = CGoGN::PathFinder::pathFindAStar<PFP>(envMap_.map, envMap_.position, dStop, dStop2, envMap_.buildingMark);

Pierre Kraemer's avatar
Pierre Kraemer committed
194
		for(std::vector<Dart>::iterator it = path.begin(); it!=path.end();++it)
195
196
197
198
199
		{
			VEC3 dest = Algo::Geometry::faceCentroid<PFP>(envMap_.map, *it, envMap_.position);
			dest[2]=0;
			agents_[i]->goals_.push_back(dest);
		}
200
201
202

		path = CGoGN::PathFinder::pathFindAStar<PFP>(envMap_.map, envMap_.position, dStop2, dStart, envMap_.buildingMark);

Pierre Kraemer's avatar
Pierre Kraemer committed
203
		for(std::vector<Dart>::iterator it = path.begin(); it!=path.end();++it)
204
205
206
207
208
		{
			VEC3 dest = Algo::Geometry::faceCentroid<PFP>(envMap_.map, *it, envMap_.position);
			dest[2]=0;
			agents_[i]->goals_.push_back(dest);
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
209
210
	}
}
211

Pierre Kraemer's avatar
Pierre Kraemer committed
212
213
214
215
216
217
218
219
220
221
222
bool Simulator::importAgents(std::string filename)
{
	std::ifstream myfile(filename.c_str(), std::ios::in);

	if (!myfile.good())
	{
		std::cerr << "(export) Unable to open file " << filename << std::endl;
		return false;
	}

	std::string line,token;
Pierre Kraemer's avatar
Pierre Kraemer committed
223
224
	while ( myfile.good() )
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
225
226
		getline(myfile,line);

Pierre Kraemer's avatar
Pierre Kraemer committed
227
228
		if(line.size() > 1)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
229
230
231
			std::istringstream iss(line);
			float x,y,z;

Pierre Kraemer's avatar
Pierre Kraemer committed
232
233
234
235
236
			iss >> x;
			iss >> y;
			iss >> z;

			VEC3 pos(x, y, z);
Pierre Kraemer's avatar
Pierre Kraemer committed
237
238
239
240
241
			Dart dCell = envMap_.getBelongingCell(pos);

			agents_.push_back(new Agent(this, pos, dCell));
			agents_.back()->goals_.push_back(pos);
			agents_.back()->goals_.push_back(-1.0f * pos);
Pierre Kraemer's avatar
Pierre Kraemer committed
242
			agents_.back()->curGoal_ = 1;
Pierre Kraemer's avatar
Pierre Kraemer committed
243
244
245
		}
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
246
247
	swapAgentsGoals();

Pierre Kraemer's avatar
Pierre Kraemer committed
248
	for (unsigned int i = 0; i < agents_.size(); ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
249
		envMap_.pushAgentInCells(agents_[i], agents_[i]->part_.d);
Pierre Kraemer's avatar
Pierre Kraemer committed
250
251
252
253
254
255
256
257

	myfile.close();
	return true;
}

bool Simulator::exportAgents(std::string filename)
{
	std::ofstream out(filename.c_str(), std::ios::out);
258
259
	if (!out.good())
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
260
261
262
263
		std::cerr << "(export) Unable to open file " << filename << std::endl;
		return false;
	}

264
	for (unsigned int i = 0; i < agents_.size(); ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
265
266
267
268
269
270
271
272
		out << agents_[i]->part_.m_position << std::endl;

	out.close();
	return true;
}

void Simulator::swapAgentsGoals()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
273
274
275
276
277
	unsigned int nbAgents = agents_.size();
	for(unsigned int i = 0; i < nbAgents; ++i)
	{
		unsigned int r = rand() % nbAgents;

Pierre Kraemer's avatar
Pierre Kraemer committed
278
		for(unsigned int nbg = 0; nbg < agents_[i]->goals_.size(); ++nbg)
Pierre Kraemer's avatar
Pierre Kraemer committed
279
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
280
281
282
			VEC3 g = agents_[i]->goals_[nbg];
			agents_[i]->goals_[nbg] = agents_[r]->goals_[nbg];
			agents_[r]->goals_[nbg] = g;
283
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
284
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
285
}
286