Création d'un compte pour un collaborateur extérieur au laboratoire depuis l'intranet ICube : https://intranet.icube.unistra.fr/fr/labs/member/profile

simulator.cpp 12.1 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "simulator.h"
#include <cstdlib>

// #ifdef HAVE_CONFIG_H
// #include "config.h"
// #endif
// 
// #if HAVE_OPENMP || _OPENMP
// #include <omp.h>
// #endif

Simulator::Simulator() : agents_(), defaultAgent_(0), globalTime_(0.0f), timeStep_(1.0f)
{
// 	CGoGN::CityGenerator::generateGrid<PFP,PFP::TVEC3>(envMap.map,envMap.position,100,100,50.0f,envMap.closeMark);

	envMap.sideSize = 70.0f;
17
	CGoGN::CityGenerator::generateSmallCity<PFP,PFP::TVEC3>(envMap.map,envMap.position,envMap.closeMark,envMap.sideSize);
Pierre Kraemer's avatar
Pierre Kraemer committed
18

Thomas's avatar
Thomas committed
19
// 	CGoGN::CityGenerator::generateAbsolutSpiralOfDeath<PFP>(envMap.map,envMap.position,envMap.closeMark,2000,0.25,110,100);
20
//	CGoGN::CityGenerator::generateToboggan<PFP>(envMap.map,envMap.position,envMap.closeMark,100,0.25,200,10);
Pierre Kraemer's avatar
Pierre Kraemer committed
21

Pierre Kraemer's avatar
Pierre Kraemer committed
22
//	std::cout << "simplify" << std::endl;
23
// 	envMap.simplify();
24
	envMap.map.init();
Pierre Kraemer's avatar
Pierre Kraemer committed
25
	std::cout << "setup scenario" << std::endl;
26
27
 	setupScenario();
//	setupHelicoidScenario(1,50);
Pierre Kraemer's avatar
Pierre Kraemer committed
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

// 	Dart dStop=envMap.map.begin();
// 	for(unsigned int i=0;i<5000;++i) envMap.map.next(dStop);
// 	FunctorIsMarked fum(envMap.closeMark);
// 	path = CGoGN::PathFinder::pathFindAStar<PFP>(envMap.map,envMap.position,envMap.map.begin(),dStop,fum);
}

Simulator::Simulator(float timeStep, float neighborDist, size_t maxNeighbors, float timeHorizon, float timeHorizonObst, float radius, float maxSpeed, const VEC3& velocity) : agents_(), defaultAgent_(0), globalTime_(0.0f), timeStep_(timeStep)
{
	defaultAgent_ = new Agent(this,0);

	defaultAgent_->maxNeighbors_ = maxNeighbors;
	defaultAgent_->maxSpeed_ = maxSpeed;
	defaultAgent_->neighborDist_ = neighborDist;
	defaultAgent_->newVelocity_ = Agent::VEC_A(velocity[0],velocity[1],0);
	defaultAgent_->radius_ = radius;
	defaultAgent_->timeHorizon_ = timeHorizon;
	defaultAgent_->timeHorizonObst_ = timeHorizonObst;
	defaultAgent_->velocity_ = Agent::VEC_A(velocity[0],velocity[1],0);
}

Simulator::~Simulator()
{
	if (defaultAgent_ != 0) {
	delete defaultAgent_;
	}

	for (size_t i = 0; i < agents_.size(); ++i) {
	delete agents_[i];
	}
}

size_t Simulator::addAgent(VEC3 position)
{
	if (defaultAgent_ == 0) {
	return RVO_ERROR;
	}

	Agent* agent = new Agent(this, position,agents_.size());
	agents_.push_back(agent);

	return agents_.size() - 1;
}

size_t Simulator::addAgent(VEC3 position, Dart d)
{
	if (defaultAgent_ == 0) {
	return RVO_ERROR;
	}

	Agent* agent = new Agent(this, position,agents_.size(),d);
	agents_.push_back(agent);

	return agents_.size() - 1;
}

size_t Simulator::addAgent(VEC3 position, float neighborDist, size_t maxNeighbors, float timeHorizon, float timeHorizonObst, float radius, float maxSpeed, const VEC3& velocity)
{
	Agent* agent = new Agent(this, Agent::VEC_A(position[0],position[1],0), neighborDist, maxNeighbors, timeHorizon, timeHorizonObst, radius, Agent::VEC_A(velocity[0],velocity[1],0), maxSpeed,agents_.size());
	agents_.push_back(agent);

	return agents_.size() - 1;
}

void Simulator::doStep()
{
// 	envMap.map.check();
	if(globalTime_==0) {

	}

99
	for (unsigned int i = 0; i < agents_.size(); ++i) {
Pierre Kraemer's avatar
Pierre Kraemer committed
100
101
102
103
		envMap.linkAgentWithCells(agents_[i]);
	}

// #pragma omp parallel for
104
	for (unsigned int i = 0; i < agents_.size(); ++i) {
Pierre Kraemer's avatar
Pierre Kraemer committed
105
106
107
108
109
		agents_[i]->computeNewVelocity();
	}


// #pragma omp parallel for
110
	for (unsigned int i = 0; i < agents_.size(); ++i) {
Pierre Kraemer's avatar
Pierre Kraemer committed
111
112
113
114
115
116
		envMap.popAgentInCell(agents_[i],agents_[i]->part->d);
		agents_[i]->update();
		envMap.pushAgentInCell(agents_[i],agents_[i]->part->d);
		agents_[i]->treated=false;
	}

117
	envMap.updateMap();
Pierre Kraemer's avatar
Pierre Kraemer committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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

	globalTime_ += timeStep_;
}

VEC3 Simulator::getAgentPrefVelocity(size_t agentNo) const
{
	return  VEC3(agents_[agentNo]->prefVelocity_[0],agents_[agentNo]->prefVelocity_[1],0);
}

void Simulator::setAgentDefaults(float neighborDist, size_t maxNeighbors, float timeHorizon, float timeHorizonObst, float radius, float maxSpeed, const VEC3& velocity)
{
	if (defaultAgent_ == 0) {
	defaultAgent_ = new Agent(this,0);
	}
	
	defaultAgent_->maxNeighbors_ = maxNeighbors;
	defaultAgent_->maxSpeed_ = maxSpeed;
	defaultAgent_->neighborDist_ = neighborDist;
	defaultAgent_->newVelocity_ = Agent::VEC_A(velocity[0],velocity[1],0);
	defaultAgent_->radius_ = radius;
	defaultAgent_->timeHorizon_ = timeHorizon;
	defaultAgent_->timeHorizonObst_ = timeHorizonObst;
	defaultAgent_->velocity_ = Agent::VEC_A(velocity[0],velocity[1],0);
}

void Simulator::setAgentPrefVelocity(size_t agentNo, const VEC3& prefVelocity)
{
	agents_[agentNo]->prefVelocity_ = Agent::VEC_A(prefVelocity[0],prefVelocity[1],0);
}

void Simulator::setTimeStep(float timeStep)
{
	timeStep_ = timeStep;
}

/** Added functions */

std::vector<PFP::VEC3> Simulator::getAgentsPosition()
{
	std::vector<PFP::VEC3> v;
	for (size_t i = 0; i < agents_.size() ; ++i) {
		VEC3 rvo_v = agents_[i]->position_;
		v.push_back(PFP::VEC3(rvo_v[0],rvo_v[1],0));
	}

	return v;
}

void Simulator::setPreferredVelocities(PFP::VEC3 posToReach)
{
  /* 
   * Set the preferred velocity to be a vector of unit magnitude (speed) in the
   * direction of the goal.
   */
// 	#pragma omp parallel for
    for (int i = 0; i < static_cast<int>(agents_.size()); ++i) {
		VEC3 goalVector = goals[i] - agents_[i]->position_;

		if (absSq(goalVector) > 1.0f) {
		goalVector = normalize(goalVector);
		}

		this->setAgentPrefVelocity(i, goalVector);
		
		/*
		* Perturb a little to avoid deadlocks due to perfect symmetry.
		*/
		float angle = std::rand() * 2.0f * M_PI / RAND_MAX;
		float dist = std::rand() * 0.0001f / RAND_MAX;
		
		this->setAgentPrefVelocity(i, this->getAgentPrefVelocity(i) +
								 VEC3(dist*std::cos(angle), dist*std::sin(angle),0.0f));
  }
}

Thomas's avatar
Thomas committed
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
void Simulator::setPreferredNextCellVelocities()
{
    for (int i = 0; i < static_cast<int>(agents_.size()); ++i) {
		Dart dNext = agents_[i]->part->d;
		while(envMap.closeMark.isMarked(envMap.map.phi2(dNext)) && envMap.map.phi1(dNext) != agents_[i]->part->d)
			dNext = envMap.map.phi1(dNext);

		VEC3 goalVector;
		if(envMap.closeMark.isMarked(envMap.map.phi2(dNext))) {
			goalVector = VEC3(0,0,0);
		}
		else {
// 			goalVector = Algo::Geometry::faceCentroid<PFP>(envMap.map,envMap.map.phi2(dNext),envMap.position);
			goalVector = (envMap.position[dNext]+envMap.position[envMap.map.phi2(dNext)])*0.5f;
			goalVector -= agents_[i]->position_;
			goalVector = normalize(goalVector);
		}

// 		goalVector *= 0.5f;
// 		goalVector += agents_[i]->prefVelocity_;
// 		goalVector *= 0.5f;

// 		if (absSq(goalVector) > 1.0f) {
// 			goalVector = normalize(goalVector);
// 		}

		this->setAgentPrefVelocity(i, goalVector);

		/*
		* Perturb a little to avoid deadlocks due to perfect symmetry.
		*/
		float angle = std::rand() * 2.0f * M_PI / RAND_MAX;
		float dist = std::rand() * 0.0001f / RAND_MAX;
		
		this->setAgentPrefVelocity(i, this->getAgentPrefVelocity(i) +
								 VEC3(dist*std::cos(angle), dist*std::sin(angle),0.0f));
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
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
302
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
void Simulator::setPreferredPathVelocities()
{
    for (int i = 0; i < static_cast<int>(agents_.size()); ++i) {
// 		std::cout << "i " << i << " pathPointer : " << pathPointer[i].first << " " << pathPointer[i].second << std::endl;
// 		std::cout << " point to reach : " << (*pathToFollow[pathPointer[i].first])[pathPointer[i].second] << std::endl;
		VEC3 goalVector((*pathToFollow[pathPointer[i].first])[pathPointer[i].second]);
		goalVector -= agents_[i]->position_;

		if (absSq(goalVector) > 100.0f) {
// 		goalVector = normalize(goalVector);
		}
		else {
			pathPointer[i].second = (pathPointer[i].second+1)%pathToFollow[pathPointer[i].first]->size();
			goalVector = (*pathToFollow[pathPointer[i].first])[pathPointer[i].second] - agents_[i]->position_;
// 			goalVector = normalize(goalVector);
		}

		this->setAgentPrefVelocity(i, goalVector);
		
		/*
		* Perturb a little to avoid deadlocks due to perfect symmetry.
		*/
		float angle = std::rand() * 2.0f * M_PI / RAND_MAX;
		float dist = std::rand() * 0.0001f / RAND_MAX;
		
		this->setAgentPrefVelocity(i, this->getAgentPrefVelocity(i) +
								 VEC3(dist*std::cos(angle), dist*std::sin(angle),0.0f));

  }
}

void Simulator::setupScenario()
{
  srand(0);
  /* Specify the global time step of the simulation. */
  setTimeStep(0.25f);
//   setTimeStep(1.0f);

  /* Specify the default parameters for agents that are subsequently added. */
//   setAgentDefaults(15.0f, 20, 10.0f, 10.0f, 1.5f, 2.0f);
  setAgentDefaults(15.0f, 10, 10.0f, 10.0f, 1.5f, 2.0f);

  /* 
   * Add agents, specifying their start position, and store their goals on the
   * opposite side of the environment.
   */
//    int i = 0;
	Dart d = envMap.map.begin();
	DartMarkerStore filled(envMap.map);

	bool found=false;

  for (int i = 0; i < 500 && d != envMap.map.end(); ++i) {
	found = false;
	VEC3 pos;
	Dart dCell;
	while(!found && d != envMap.map.end()) {
		VEC3 n = Algo::Geometry::triangleNormal<PFP>(envMap.map,d,envMap.position);
		if(!envMap.closeMark.isMarked(d) && !filled.isMarked(d) && envMap.position[d][2]==0.0f && n[2]==1.0f) {
			pos = envMap.faceCenter(d);
			dCell=d;
			found = true;
			filled.markOrbit(FACE_ORBIT,d);
			std::cout << "pos " << pos << std::endl;
		}
		envMap.map.next(d);
	}

	if(found) {
		float ecart=3.0f;
		addAgent(pos,dCell);
		addAgent(pos+VEC3(ecart,0,0),dCell);
		addAgent(pos+VEC3(-ecart,0,0),dCell);
		addAgent(pos+VEC3(ecart,ecart,0),dCell);
		addAgent(pos+VEC3(0,ecart,0),dCell);
		addAgent(pos+VEC3(0,-ecart,0),dCell);
		addAgent(pos+VEC3(-ecart,-ecart,0),dCell);
		addAgent(pos+VEC3(ecart,-ecart,0),dCell);
		addAgent(pos+VEC3(-ecart,ecart,0),dCell);
    	goals.push_back(-1.0f*pos);
    	goals.push_back(-1.0f*(pos+VEC3(ecart,0,0)));
		goals.push_back(-1.0f*(pos+VEC3(-ecart,0,0)));
		goals.push_back(-1.0f*(pos+VEC3(ecart,ecart,0)));
		goals.push_back(-1.0f*(pos+VEC3(0,ecart,0)));
		goals.push_back(-1.0f*(pos+VEC3(0,-ecart,0)));
		goals.push_back(-1.0f*(pos+VEC3(-ecart,-ecart,0)));
		goals.push_back(-1.0f*(pos+VEC3(ecart,-ecart,0)));
		goals.push_back(-1.0f*(pos+VEC3(-ecart,ecart,0)));
	}
  }

	std::cout << "nb of agents : " << goals.size() << std::endl;

	std::random_shuffle( goals.begin(), goals.end() );

	for (int i = 0; i < static_cast<int>(agents_.size()); ++i)
		envMap.pushAgentInCell(agents_[i],agents_[i]->part->d);

}

void Simulator::setupHelicoidScenario(float rMax,float rMin)
{
  srand(0);
  setTimeStep(0.25f);
  setAgentDefaults(15.0f, 10, 10.0f, 10.0f, 1.5f, 2.0f);

  /* 
   * Add agents, specifying their start position, and store their goals on the
   * opposite side of the environment.
   */
//    int i = 0;
	Dart d = envMap.map.begin();
	for(unsigned int i=0;i<50;++i)
		envMap.map.next(d);
	DartMarkerStore filled(envMap.map);

	bool found=false;
Thomas's avatar
Thomas committed
349
	int nbPack=2000;
Pierre Kraemer's avatar
Pierre Kraemer committed
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

  for (int i = 0; i < nbPack && d != envMap.map.end(); ++i) {
	found = false;
	VEC3 pos;
	Dart dCell;
	while(!found && d != envMap.map.end()) {
		if(!envMap.closeMark.isMarked(d) && !filled.isMarked(d)) {
			pos = envMap.faceCenter(d);
			if(i==nbPack/2)
				std::cout << "pos " << pos << std::endl;
			pos[2]=0.0f;
			dCell=d;
			found = true;
			filled.markOrbit(FACE_ORBIT,d);
		}
		envMap.map.next(d);
	}

	if(found) {
		float ecart=3.0f;
// 		addAgent(pos,dCell);
// 		addAgent(pos+VEC3(ecart,0,0),dCell);
// 		addAgent(pos+VEC3(-ecart,0,0),dCell);
// 		addAgent(pos+VEC3(ecart,ecart,0),dCell);
// 		addAgent(pos+VEC3(0,ecart,0),dCell);
// 		addAgent(pos+VEC3(0,-ecart,0),dCell);
// 		addAgent(pos+VEC3(-ecart,-ecart,0),dCell);
// 		addAgent(pos+VEC3(ecart,-ecart,0),dCell);
// 		addAgent(pos+VEC3(-ecart,ecart,0),dCell);

		addAgent(pos+VEC3(ecart,ecart,0),dCell);
		addAgent(pos+VEC3(-ecart,-ecart,0),dCell);
		addAgent(pos+VEC3(ecart,-ecart,0),dCell);
		addAgent(pos+VEC3(-ecart,ecart,0),dCell);

		unsigned int nbNewAgent =4;

		for(unsigned int j=0;j<nbNewAgent;++j) {
			pathPointer.push_back(std::make_pair<int,int>(i*nbNewAgent+j,0));
			pathToFollow.push_back(new std::vector<VEC3>());
			float coeff = VEC3(pos[0],pos[1],0).norm();
			float pas=16.0f;
			for(float p = 1.0f; p<=2.0f*pas ; p=p+1.0f) {
				if(i<nbPack/2)
					pathToFollow[i*nbNewAgent+j]->push_back(coeff*VEC3(sin(M_PI*p/pas),cos(M_PI*p/pas),0));
				else
					pathToFollow[i*nbNewAgent+j]->push_back(coeff*VEC3(cos(M_PI*p/pas),sin(M_PI*p/pas),0));
			}
		}
	}
  }

	std::cout << "nb of agents : " << agents_.size() << std::endl;

// 	std::random_shuffle( goals.begin(), goals.end() );

	for (int i = 0; i < static_cast<int>(agents_.size()); ++i)
		envMap.pushAgentInCell(agents_[i],agents_[i]->part->d);

}