moving_obstacle.cpp 13.2 KB
Newer Older
pitiot's avatar
maj    
pitiot committed
1
2
3
4
5
#include "moving_obstacle.h"
#include "obstacle.h"
#include "agent.h"
#include "simulator.h"

Pierre Kraemer's avatar
Pierre Kraemer committed
6
//float MovingObstacle::neighborDistSq_ = 5.0f * 5.0f;
pitiot's avatar
maj    
pitiot committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

float MovingObstacle::maxSpeed_ = 1.0f;

float MovingObstacle::timeHorizonObst_ = 10.0f;
unsigned int MovingObstacle::maxNeighbors_ = 20;
float MovingObstacle::detectionFixedObst = 50;

bool MovingObstacle::is_inside(VEC3 p)
{
	VEC3 vec, norm;
	Obstacle * obst;

	for (unsigned int i = 0; i < nbVertices; i++)
	{
		obst = this->obstacles_[i];

		vec = VEC3(obst->p2 - obst->p1);
		norm[0] = vec[1];
		norm[1] = -vec[0];

		vec = VEC3(obst->p2 -p);
		if (vec*norm < 0)
			return false;
	}

	return true;
}

float get_angle(VEC3 v1, VEC3 v2)
{
	float flo = 0.0f;
	float nb = std::sqrt(v1.norm2() * v2.norm2());

	if (nb != 0)
	{
		nb = (v1*v2) / nb;

		if (nb > 1)
			nb = 1;
		else if (nb < -1)
			nb = -1;
		flo = std::acos(nb);
	}

	if ((v1[0] * v2[1] - v2[0] * v1[1]) > 0)
		flo = 2.0f * M_PI - flo;
	return flo;
}

VEC3 rotate(VEC3 pos1, VEC3 center, float angle)
{
	VEC3 pos2;
	PFP::REAL x = pos1[0] - center[0];
	PFP::REAL y = pos1[1] - center[1];
	pos2[0] = x * cos(angle) - y * sin(angle) - x;
	pos2[1] = x * sin(angle) + y * cos(angle) - y;
	return pos2;
}

MovingObstacle::MovingObstacle(Simulator* sim, int ind, std::vector<VEC3> pos, VEC3 goal, float rota) :
		index(ind),
		newVelocity_(0),
		sim_(sim)
{
	assert(pos.size() > 2);

	movingObstNeighbors_.reserve(maxNeighbors_);

	rot = rota;
	VEC3 sum = 0;
	nb_agents_voisins = 0;
	nb_register_cells = 0;
	float pos_max = 0;

	velocity_factor = 0.8f;
	nbVertices = pos.size();

	parts_ = new CGoGN::Algo::MovingObjects::ParticleCell2D<PFP>*[nbVertices];
	obstacles_ = new Obstacle*[nbVertices];
	belonging_cells = new std::list<Dart>[nbVertices];
	neighbor_cells = new std::list<Dart>[nbVertices];

	for (unsigned int i = 0; i < nbVertices; ++i)
	{
		sum += pos[i];

		//		CGoGNout << "Au début : Obstacle "<< o << " position :" << pos[i] << CGoGNendl;
		Dart d = sim_->envMap_.getBelongingCell(pos[i]);
		CGoGN::Algo::MovingObjects::ParticleCell2D<PFP>* part =
						new CGoGN::Algo::MovingObjects::ParticleCell2D<PFP>(sim_->envMap_.map, d, pos[i], sim_->envMap_.position);
		parts_[i] = part;
	}

	for (unsigned int i = 0; i < nbVertices; ++i)
	{
		Obstacle* o = new Obstacle(parts_[i]->getPosition(),
								   parts_[(i + 1) % nbVertices]->getPosition(),
								   parts_[(i - 1 + nbVertices) % nbVertices]->getPosition(),
								   parts_[(i + 2) % nbVertices]->getPosition(), this, i);
		obstacles_[i] = o;

		register_(o, parts_[i]->d, i);
	}


	center = sum / nbVertices;
	for (unsigned int i = 0; i < nbVertices; ++i)
	{
		float it = (center - pos[i]).norm();
		if (it > pos_max)
			pos_max = it;
	}
	gravity_dist = pos_max + 5.0f;

	obstacle_range = 15.0f * 15.0f;
	make_half_turn = get_angle(finalGoal - center, (parts_[0]->getPosition() + parts_[1]->getPosition()) / 2 - center) * nbVertices;
pitiot's avatar
pitiot committed
123
124
125
126

	goals_.push_back(center);
	goals_.push_back(goal);
	curGoal_ = 0;
pitiot's avatar
maj    
pitiot committed
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
193
194
195
196
197
}

bool MovingObstacle::test_opposition(VEC3 o, VEC3 p1, VEC3 p2)
{
	o.normalize();
	o *= -1;
	VEC3 vect(p1 - p2);
	vect.normalize();
	return (o - vect).norm2() < 0.1;
}

// TODO Check position
void MovingObstacle::updateFixedObstNeighbors()
{
	PFP::VEC3 toto;
	PFP::VEC3 toto_norm;
	//for particles in the "front" of the object (to modify)
	for(int k =0;k<3;k++)
	{
		if (k==2)
			k=nbVertices-1;

		std::vector<Obstacle*>& obst = sim_->envMap_.obstvect[parts_[k]->d];

		//search all obstacles around
		for(std::vector<Obstacle*>::const_iterator it = obst.begin(); it != obst.end(); ++it)
		{
			//only for fixed obstacles around
			if ((*it)->mo==NULL)
			{
				float distSq = distSqPointLineSegment((*it)->p1, (*it)->p2, parts_[0]->getPosition());
				if(distSq < detectionFixedObst*detectionFixedObst)
				{
					toto= (*it)->p1 -(*it)->p2;
					toto_norm=toto;
					toto_norm[0]=-toto[1];
					toto_norm[1]=toto[0];

					if(test_opposition(toto_norm,parts_[0]->getPosition(),center)) //// à changer ////////////
					{
						int co = rand() % 2 ;
						if (toto[0]==0)
						{
							finalGoal[0]=parts_[0]->getPosition()[0];
							if (co == 0)
								finalGoal[0]=sim_->envMap_.geometry.max()[1]-maxNeighbors_;
							else
								finalGoal[0]=sim_->envMap_.geometry.min()[1]+maxNeighbors_;
						}
						else
						{
							finalGoal[1]=parts_[0]->getPosition()[1];
							if (co == 0)
								finalGoal[0]=sim_->envMap_.geometry.max()[1]-maxNeighbors_;
							else
								finalGoal[0]=sim_->envMap_.geometry.min()[1]+maxNeighbors_;
						}

						float angle =get_angle(finalGoal-center,(parts_[0]->getPosition())-center);
						make_half_turn=angle*nbVertices;
						break;
					}
				}
			}
		}
	}
}

// TODO Check position
void MovingObstacle::computePrefVelocity()
{
pitiot's avatar
pitiot committed
198
199
	VEC3 goalVector = goals_[curGoal_] - center ;
	float goalDist2 = goalVector.norm2() ;
pitiot's avatar
maj    
pitiot committed
200

pitiot's avatar
pitiot committed
201
202
203
204
205
	if (goalDist2 < 2.0f)
	{
		curGoal_ = (curGoal_ + 1) % goals_.size() ;
		goalVector = goals_[curGoal_] - center ;
		goalDist2 = goalVector.norm2() ;
pitiot's avatar
maj    
pitiot committed
206
207
	}

pitiot's avatar
pitiot committed
208
209
210
211
212
	if (goalDist2 > maxSpeed_)
	{
		goalVector.normalize() ;
		goalVector *= maxSpeed_;
	}
pitiot's avatar
maj    
pitiot committed
213

pitiot's avatar
pitiot committed
214
	prefVelocity_ = goalVector ;
pitiot's avatar
maj    
pitiot committed
215

pitiot's avatar
pitiot committed
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
//	VEC3 goalVector = finalGoal - center;
//	int co=2;
//	float angle;
//	float goalDist2 = goalVector.norm2();
//	//	CGoGNout << "finalGoal "<< finalGoal << CGoGNendl;
//	//	CGoGNout << "dist 2 "<< goalDist2 << CGoGNendl;
//	if(goalDist2 < 2.0f)
//	{
////		co=rand() % 2 + 1;
//
//
//		if(co==1)
//			finalGoal[0]*=-1;
//		else
//			finalGoal[1]*=-1;
//
//		goalVector = finalGoal - center;
//		angle =get_angle(finalGoal-center,(parts_[0]->getPosition())-center);
//		make_half_turn=angle*nbVertices;
//	}
//
//	if (goalDist2 > 1.0f)
//		goalVector.normalize();
//
//	prefVelocity_=goalVector;
pitiot's avatar
maj    
pitiot committed
241
242
243
244
245
}

// TODO Check position
void MovingObstacle::update()
{
pitiot's avatar
pitiot committed
246
	assert(sim_->envMap_.map.getCurrentLevel() == sim_->envMap_.map.getMaxLevel()) ;
pitiot's avatar
maj    
pitiot committed
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
//	general_belonging.clear();
	PFP::VEC3 bary = 0;

	Dart d;
	velocity_[0] = prefVelocity_[0] * velocity_factor;
	velocity_[1] = prefVelocity_[1] * velocity_factor;

	// MAJ des particules
	make_half_turn = 0;
	float rotor = make_half_turn > 0.01f * nbVertices ?	0.01f : make_half_turn / nbVertices;

	//	CGoGNout << "début ++ update "<< CGoGNendl;
	for (unsigned int i = 0; i < nbVertices; ++i)
	{
		//		CGoGNout << "update particule : "<< i << CGoGNendl;
		VEC3 target = parts_[i]->getPosition();

		//		Dart oldFace =  parts_[i]->d;
		if (make_half_turn == 0)
		{
			if (rot != 0)
			{
				target += rotate(parts_[i]->getPosition(), center, rot);
			}
			target += (velocity_ * sim_->timeStep_);

		}
		else
		{
			target += rotate(parts_[i]->getPosition(), center, rotor);
		}

		parts_[i]->move(target);
		bary += target;
	}

	make_half_turn -= rotor * nbVertices;

	if (make_half_turn == 0)
		center = bary / nbVertices;

	// MAJ des obstacles
	for (unsigned int i = 0; i < nbVertices; ++i)
	{
		//		CGoGNout << "avant une etape : Obstacle "<< i << CGoGNendl;
		Obstacle* o = obstacles_[i];
//		o->p1 = parts_[i]->getPosition();
//		o->p2 = parts_[(i + 1) % nbVertices]->getPosition();
//		o->prevP = parts_[(i - 1 + nbVertices) % nbVertices]->getPosition();
//		o->nextP = parts_[(i + 2) % nbVertices]->getPosition();
		d = parts_[i]->d;
		register_update(o, d, i);
		//
//		for (std::list<Dart>::iterator it = belonging_cells[i].begin(); it != belonging_cells[i].end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
301
//		{
pitiot's avatar
maj    
pitiot committed
302
//			general_belonging.push_back(*it);
Pierre Kraemer's avatar
Pierre Kraemer committed
303
//		}
pitiot's avatar
maj    
pitiot committed
304
305
306
307
308
309
310
311
312
313
314
		//		CGoGNout << "Apres une etape : Obstacle "<< i << CGoGNendl;
	}

	//
	//		envMap_->addMovingObstAsNeighbor(this,general_belonging,&(general_neighbors));
	//		CGoGNout << "nouvel update : "<< CGoGNendl;

//	for (std::list<Dart>::iterator it2 = general_belonging.begin(); it2 != general_belonging.end(); ++ it2)
//	{
//		std::vector<Agent*> vector =envMap_->agentvect[(*it2)];
//		for(std::vector<Agent*>::iterator it=vector.begin();it!=vector.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
315
//		{
pitiot's avatar
maj    
pitiot committed
316
//			if (this->is_inside((*it)->part_.m_position))
Pierre Kraemer's avatar
Pierre Kraemer committed
317
//			{
pitiot's avatar
maj    
pitiot committed
318
319
320
321
322
//				(*it)->alive=false;
//				(*it)->color1=0.0f;
//				(*it)->color2=0.4f;
//				(*it)->color3=0.0f;
//				envMap_->popAgentInCells(*it,(*it)->part_.d);
Pierre Kraemer's avatar
Pierre Kraemer committed
323
324
325
//			}
//		}
//
pitiot's avatar
maj    
pitiot committed
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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//	}

}

void MovingObstacle::register_(Obstacle* o, Dart d, int n)
{
	VEC3 start = o->p1;
	VEC3 stop = o->p2;

	CGoGN::Algo::MovingObjects::ParticleCell2DMemo<PFP> * part =
			new CGoGN::Algo::MovingObjects::ParticleCell2DMemo<PFP>(sim_->envMap_.map, d, start, sim_->envMap_.position);

	part->move(stop);

	belonging_cells[n] = part->memo_cross;

	sim_->envMap_.addObstAsNeighbor(o, belonging_cells[n], &(neighbor_cells[n]));

	//	CGoGNout << "coucou je suis sorti de addNeighbor" << CGoGNendl;
	for (std::list<Dart>::iterator it = part->memo_cross.begin(); it != part->memo_cross.end(); ++it)
	{
		general_belonging.push_back(*it);
		sim_->envMap_.pushObstacleInCells(o, *it);
	}

	for (std::list<Dart>::iterator it = this->neighbor_cells[n].begin(); it != this->neighbor_cells[n].end(); ++it)
	{
		sim_->envMap_.pushObstNeighborInCells(o, *it);

	}
}

void register_pop(Obstacle* o, int n)
{
	MovingObstacle * mo = o->mo;
	if (mo != NULL)
	{
		for (std::list<Dart>::iterator it = mo->belonging_cells[n].begin();	it != mo->belonging_cells[n].end(); ++it)
		{
			mo->sim_->envMap_.popObstacleInCells(o, *it);
		}

		for (std::list<Dart>::iterator it = mo->neighbor_cells[n].begin(); it != mo->neighbor_cells[n].end(); ++it)
		{
			mo->sim_->envMap_.popObstNeighborInCells(o, *it);
		}
	}
}

void MovingObstacle::register_oneRingAdd(Obstacle * o, Dart d, int n)
{
	assert(sim_->envMap_.map.getCurrentLevel() == sim_->envMap_.map.getMaxLevel());
	assert(std::find(sim_->envMap_.obstvect[d].begin(), sim_->envMap_.obstvect[d].end(), o) == sim_->envMap_.obstvect[d].end());

	sim_->envMap_.pushObstacleInCells(o, d);

	belonging_cells[n].clear();
	belonging_cells[n].push_back(d);
	neighbor_cells[n].clear();

	Dart dd = d;
	do
	{
		Dart ddd = sim_->envMap_.map.alpha1(sim_->envMap_.map.alpha1(dd));
		while(ddd != dd)
		{
			sim_->envMap_.pushObstNeighborInCells(o, ddd);

			this->neighbor_cells[n].push_back(ddd);
			ddd = sim_->envMap_.map.alpha1(ddd);
		}
		dd = sim_->envMap_.map.phi1(dd);
	} while(dd != d);
}

void register_add(Obstacle* o, int n, const std::list<Dart>& memo_cross)
{
	MovingObstacle * mo = o->mo;

	mo->belonging_cells[n].clear();
	mo->belonging_cells[n] = memo_cross;

	for (std::list<Dart>::iterator it = mo->belonging_cells[n].begin();	it != mo->belonging_cells[n].end(); ++it)
	{
		//		 CGoGNout <<"obstacle"<< o->p1 << "new cell : "<< *it << CGoGNendl;
		mo->sim_->envMap_.pushObstacleInCells(o, *it);
	}

	mo->sim_->envMap_.addObstAsNeighbor(o, mo->belonging_cells[n], &(mo->neighbor_cells[n]));

	for (std::list<Dart>::iterator it = mo->neighbor_cells[n].begin(); it != mo->neighbor_cells[n].end(); ++it)
	{
		//		 CGoGNout <<"obstacle"<< o->p1 << "new cell : "<< *it << CGoGNendl;
		mo->sim_->envMap_.pushObstNeighborInCells(o, *it);

	}
}

std::list<Dart> MovingObstacle::getMemoCross(const Algo::MovingObjects::ParticleCell2D<PFP>* p1, const VEC3& dest)
{
	Dart d = p1->d;
	const VEC3& start = p1->getPosition();

	CGoGN::Algo::MovingObjects::ParticleCell2DMemo<PFP> part(sim_->envMap_.map, d, start, sim_->envMap_.position);
	part.move(dest);

	return part.memo_cross;
}

void MovingObstacle::register_update(Obstacle* o, Dart d, int n)
{
	CGoGN::Algo::MovingObjects::ParticleCell2D<PFP>* p1 = this->parts_[n];
	CGoGN::Algo::MovingObjects::ParticleCell2D<PFP>* p2 = this->parts_[(n+1)%nbVertices];

	std::list<Dart> memo;

pitiot's avatar
pitiot committed
442
//	bool modif=false;
pitiot's avatar
maj    
pitiot committed
443
444
445

//	if(p1->crossCell != CGoGN::Algo::MovingObjects::NO_CROSS
//		|| p2->crossCell != CGoGN::Algo::MovingObjects::NO_CROSS)
Pierre Kraemer's avatar
Pierre Kraemer committed
446
//	{
pitiot's avatar
maj    
pitiot committed
447
448
449
		memo = getMemoCross(p1,p2->getPosition());
//		memo.sort();
//		modif=true;
Pierre Kraemer's avatar
Pierre Kraemer committed
450
//	}
pitiot's avatar
maj    
pitiot committed
451
//	else if(!sim_->envMap_.map.sameFace(p1->d,p2->d))
Pierre Kraemer's avatar
Pierre Kraemer committed
452
//	{
pitiot's avatar
maj    
pitiot committed
453
454
455
456
457
//		memo = getMemoCross(p1,p2->getPosition());
//		memo.sort();
//	 	if(belonging_cells[n] != memo)
//			modif=true;
//	}
Pierre Kraemer's avatar
Pierre Kraemer committed
458
//
pitiot's avatar
maj    
pitiot committed
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//	if(modif)
	{
		register_pop(o, n);

		if(sim_->envMap_.map.sameFace(p1->d,p2->d))
		{
			register_oneRingAdd(o , p1->d , n);
		}
		else
		{
			register_add(o, n, memo);
		}
	}
}

void update_registration(Obstacle * o)
{
	MovingObstacle * mo = o->mo;
	if (mo != NULL)
	{
		int i = o->index;

		std::list<Dart> memo = mo->getMemoCross(mo->parts_[i] , mo->parts_[(i+1)%mo->nbVertices]->getPosition());
		register_add(o, i, memo);

//		mo->general_belonging.clear();
//		for (unsigned int j = 0; j < mo->nbVertices; j++)
Pierre Kraemer's avatar
Pierre Kraemer committed
486
//		{
pitiot's avatar
maj    
pitiot committed
487
488
489
490
//			for (std::list<Dart>::iterator it = mo->belonging_cells[j].begin();	it != mo->belonging_cells[j].end(); ++it)
//			{
//				mo->general_belonging.push_back(*it);
//			}
Pierre Kraemer's avatar
Pierre Kraemer committed
491
//		}
pitiot's avatar
maj    
pitiot committed
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
	}
}


void resetObstInFace(Obstacle* o)
{
	MovingObstacle * mo = o->mo;
	int ind = o->index;
	if (mo != NULL)
	{
		VEC3 pos = o->p1;
		mo->parts_[ind]->ParticleBase<PFP>::move(Algo::Geometry::faceCentroid<PFP>(mo->sim_->envMap_.map, mo->parts_[ind]->d,
			                                  mo->sim_->envMap_.position));
		mo->parts_[ind]->setState(FACE);
		mo->parts_[ind]->move(pos);
	}
}

void resetObstPartInFace(Obstacle* o, Dart d1)
{
	MovingObstacle * mo = o->mo;
	int ind = o->index;
	if (mo != NULL) {

		VEC3 pos1 = o->p1;
		if (Algo::Geometry::isPointInConvexFace2D < PFP
				> (mo->sim_->envMap_.map, d1, mo->sim_->envMap_.position, pos1, true)) {
			mo->parts_[ind]->d = d1;
		}

	}
}

pitiot's avatar
pitiot committed
525

pitiot's avatar
pitiot committed
526
void resetPart(MovingObstacle * mo,int index, Dart d1)
pitiot's avatar
maj    
pitiot committed
527
528
{

pitiot's avatar
pitiot committed
529
	int i = index;
pitiot's avatar
pitiot committed
530
531
	if (mo->parts_[i]->d == mo->sim_->envMap_.map.phi1(d1))
		mo->parts_[i]->d = d1;
pitiot's avatar
pitiot committed
532

pitiot's avatar
maj    
pitiot committed
533
534
535
536
537
538
}

void displayMO(Obstacle * o)
{
	CGoGNout << "obstacle problematique : " << (o->mo->index) << CGoGNendl;
}