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

7
8
#include "Algo/Modelisation/triangulation.h"

Pierre Kraemer's avatar
Pierre Kraemer committed
9
//float MovingObstacle::neighborDistSq_ = 5.0f * 5.0f;
pitiot's avatar
maj    
pitiot committed
10

pitiot's avatar
pitiot committed
11
float MovingObstacle::maxSpeed_ = 2.0f;
pitiot's avatar
pitiot committed
12
13
float MovingObstacle::neighborDist_ = 10.0f ;
float MovingObstacle::neighborDistSq_ = neighborDist_ * neighborDist_ ;
pitiot's avatar
maj    
pitiot committed
14
15
16
17
float MovingObstacle::timeHorizonObst_ = 10.0f;
unsigned int MovingObstacle::maxNeighbors_ = 20;
float MovingObstacle::detectionFixedObst = 50;

pitiot's avatar
pitiot committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void  MovingObstacle::addGeneralCell ( Dart d)
{
	bool added = false;
	std::vector< std::pair<Dart,int> >::iterator it =general_belonging.begin();
	while (it != general_belonging.end() )
	{
		if(sim_->envMap_.map.sameFace((*it).first,d))
		{
			(*it).second ++;
			added= true;
			break;
		}
		else ++it;

	};

	if (!added)
	{
		general_belonging.push_back(std::make_pair(d,1));
	}
}
Thomas Jund's avatar
Thomas Jund committed
39

pitiot's avatar
pitiot committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
bool  MovingObstacle::removeGeneralCell (Dart d)
{
	std::vector< std::pair<Dart,int> >::iterator it =general_belonging.begin();
	while (it != general_belonging.end() )
	{
		if(sim_->envMap_.map.sameFace((*it).first,d))
		{
			if(--((*it).second)==0)
			{
				*it = general_belonging.back() ;
				general_belonging.pop_back() ;
			}
			return true;
		}
		else ++it;

	};
	return false;
}

pitiot's avatar
maj    
pitiot committed
60
61
bool MovingObstacle::is_inside(VEC3 p)
{
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//	return false;
	VEC3 vec, norm, p1,p2;


	for (unsigned int i = 0; i < nbVertices; i++)
	{
		 p1 = getPosition(i);
		 p2 = getPosition((i+1)%nbVertices);

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

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

	return true;
pitiot's avatar
maj    
pitiot committed
81
82
}

pitiot's avatar
pitiot committed
83
float get_angle(VEC3 v1, VEC3 v2) //renvoie l'angle entre [- pi ; Pi] du v2 à v1
pitiot's avatar
maj    
pitiot committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
{
	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)
pitiot's avatar
pitiot committed
100
101
102
103
104
105
106
		flo = - flo;

	if(flo >M_PI)
		flo= flo-2*M_PI;
	if (flo<(-M_PI))
		flo=flo+2*M_PI;

pitiot's avatar
maj    
pitiot committed
107
108
109
	return flo;
}

pitiot's avatar
pitiot committed
110
VEC3 rotate(VEC3 pos1, VEC3 center, float angle) // renvoie le déplacement necessaire depuis pos1 pour effectuer la rotation centrée en center d'angle angle
pitiot's avatar
maj    
pitiot committed
111
112
113
114
115
116
117
118
119
{
	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;
}

pitiot's avatar
pitiot committed
120
MovingObstacle::MovingObstacle(Simulator* sim, int ind, std::vector<VEC3> pos, std::vector<VEC3> goals, bool rigid, bool spin,int curGoal, Dart dInside, ArticulatedObstacle * art, int indParent) :
121
		nbVertices(pos.size()),
122
		center(0),
pitiot's avatar
maj    
pitiot committed
123
		index(ind),
124
		goals_(goals),
pitiot's avatar
pitiot committed
125
		curGoal_(curGoal),
126
127
128
129
130
		velocity_factor(0.8f),
		color1(1.0f),
		color2(1.0f),
		color3(1.0f),
		seen(false),
pitiot's avatar
maj    
pitiot committed
131
		newVelocity_(0),
pitiot's avatar
merging    
pitiot committed
132
		sim_(sim),
133
134
		rigid_(rigid),
		spinning(spin),
pitiot's avatar
merging    
pitiot committed
135
		parent(art),
pitiot's avatar
blah    
pitiot committed
136
		mm_(NULL),
Thomas Jund's avatar
Thomas Jund committed
137
		ag_(NULL),
138
139
		index_parent(indParent),
		gravity_dist(0)
pitiot's avatar
maj    
pitiot committed
140
141
142
{
	assert(pos.size() > 2);

143
144
	if(dInside==NIL)
		dInside = sim_->envMap_.getBelongingCell(pos[0]);
pitiot's avatar
maj    
pitiot committed
145

146
147
148
#ifdef TWO_AND_HALF_DIM
	parts_ = new CGoGN::Algo::Surface::MovingObjects::ParticleCell2DAndHalf<PFP>*[nbVertices];
#else
pitiot's avatar
pitiot committed
149
	parts_ = new CGoGN::Algo::Surface::MovingObjects::ParticleCell2D<PFP>*[nbVertices];
150
151
#endif

pitiot's avatar
maj    
pitiot committed
152
	obstacles_ = new Obstacle*[nbVertices];
153
154
	belonging_cells = new std::vector<Dart>[nbVertices];
	neighbor_cells = new std::vector<Dart>[nbVertices];
155
156
157
158

	position = map.addAttribute<VEC3, VERTEX>("position") ;
	normal = map.addAttribute<VEC3, VERTEX>("normal") ;
	deformation = map.addAttribute<VEC3, VERTEX>("deformation") ;
159
160
161
162

	if(!rigid_)
	{
		velocity = map.addAttribute<VEC3, VERTEX>("velocity") ;
Thomas Jund's avatar
Thomas Jund committed
163
		forces = map.addAttribute<VEC3, VERTEX>("force") ;
164
		edgeLength = map.addAttribute<float, EDGE>("edgeLength") ;
Thomas Jund's avatar
Thomas Jund committed
165
		vertexAngle = map.addAttribute<float, DART>("vertexAngle") ;
166
	}
167

168
	for (unsigned int i = 0; i < nbVertices; ++i)
169
170
171
172
	{
		Dart d = sim_->envMap_.getBelongingCell(pos[i]);
#ifdef TWO_AND_HALF_DIM
		CGoGN::Algo::Surface::MovingObjects::ParticleCell2DAndHalf<PFP>* part = new CGoGN::Algo::Surface::MovingObjects::ParticleCell2DAndHalf<PFP>(sim_->envMap_.map, d, pos[i], sim_->envMap_.position);
173

174
175
176
177
178
179
180
181
182
#else
		CGoGN::Algo::Surface::MovingObjects::ParticleCell2D<PFP>* part = new CGoGN::Algo::Surface::MovingObjects::ParticleCell2D<PFP>(sim_->envMap_.map, d, pos[i], sim_->envMap_.position);
#endif
		parts_[i] = part;
		center += pos[i];

		if(i==0)
			dDir = d;
	}
183
184
185

		center /= nbVertices;
		front=(pos[0] + pos[1]) / 2;
Thomas Jund's avatar
merge    
Thomas Jund committed
186

187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
		// M appartient à l'ellipse ssi MF1 + MF2 = sum_dist_foci est une constante
		// où F1 et F2 sont les deux foyers.
	//	length = (pos[0]-pos[1]).norm();
	//	width  = (pos[1]-pos[2]).norm();
	//	sum_dist_foci_rest = 2*(length + width*(sqrt(2)-0.5));

//		if (spinning && parent==NULL) //départ face à la cible en cas d'obstacles pouvant effectuer des rotations
//		{
//
//			angle = get_angle(goals_[curGoal_] - center,front  - center);
////			std::cout<<" angle : "<< angle;
//
//			for (unsigned int i = 0; i < nbVertices; ++i)
//			{
////				std::cout<<" || pos[i] avant : "<< pos [i];
//				pos[i]+=rotate(pos[i], center, angle);
////				std::cout<<" || pos[i] APRES : "<< pos [i]<<std::endl;
//
//				parts_[i]->move(pos[i]);
//
//
//			}
//			angle=0;
//			front=(pos[0] + pos[1]) / 2;
//		}
Thomas Jund's avatar
merge    
Thomas Jund committed
212

213
214
	groundFace = map.newFace(nbVertices);

pitiot's avatar
maj    
pitiot committed
215
216
	for (unsigned int i = 0; i < nbVertices; ++i)
	{
217
218
219
220
221
		float distance=(center-pos[i]).norm();
		if(distance>gravity_dist)
		{
			gravity_dist=distance;
		}
Thomas Jund's avatar
Thomas Jund committed
222
		Dart d = i;
223
224
		position[d] = pos[i];
		deformation[d] = VEC3(0);
225
226

		if(!rigid_)
Thomas Jund's avatar
Thomas Jund committed
227
		{
228
			velocity[d] = VEC3(0);
Thomas Jund's avatar
Thomas Jund committed
229
230
			forces[d] = VEC3(0);
		}
231

232

pitiot's avatar
pitiot committed
233
	}
234

235
236
	//extrude face to build a cage
	// compute edgeLength for mass-spring
Thomas Jund's avatar
Thomas Jund committed
237
	Algo::Surface::Modelisation::extrudeFace<PFP>(map, position, groundFace, -10.0f) ;
238
//	Algo::Surface::Modelisation::extrudeFace<PFP>(map, position, groundFace, 0.0f) ;
239
240
241
	map.fillHole(groundFace);
	groundFace = map.phi2(groundFace);

242
243
	if(!rigid_)
	{
244
		Algo::Surface::Modelisation::EarTriangulation<PFP> et(map);
245
246
247
248
249
250
251
		et.triangule();

		TraversorE<PFP::MAP> tE(map);
		for(Dart d = tE.begin() ; d != tE.end() ; d = tE.next())
		{
			edgeLength[d] = VEC3(position[map.phi1(d)]-position[d]).norm();
		}
Thomas Jund's avatar
Thomas Jund committed
252

Thomas Jund's avatar
Thomas Jund committed
253
254
255
		DartMarker treated(map);
		for(Dart d = map.begin() ; d != map.end() ; map.next(d))
		{
256
			if(!map.isBoundaryMarked2(d) && !treated.isMarked(d))
Thomas Jund's avatar
Thomas Jund committed
257
258
			{
				treated.mark(d);
259
				vertexAngle[d] = Algo::Surface::Geometry::angle<PFP>(map,map.phi_1(d),map.phi1(d),position);
Thomas Jund's avatar
Thomas Jund committed
260
261
262
			}
		}

263
		map.enableQuickTraversal<VERTEX>();
264

Thomas Jund's avatar
Thomas Jund committed
265
		dDir=dInside;
pitiot's avatar
pitiot committed
266
	}
267

pitiot's avatar
pitiot committed
268

269

pitiot's avatar
maj    
pitiot committed
270
271
	for (unsigned int i = 0; i < nbVertices; ++i)
	{
pitiot's avatar
pitiot committed
272
273
		Obstacle* o = new Obstacle(parts_[i]->getPosition(),
				parts_[(i + 1) % nbVertices]->getPosition(),
Thomas Jund's avatar
merge    
Thomas Jund committed
274
				parts_[(i - 1 + nbVertices) % nbVertices]->getPosition(),
Thomas Jund's avatar
Thomas Jund committed
275
				parts_[(i + 2) % nbVertices]->getPosition(), i, (i+1)% nbVertices, this, i);
pitiot's avatar
maj    
pitiot committed
276
		obstacles_[i] = o;
pitiot's avatar
pitiot committed
277
//		CGoGNout<<" obstacle :"<< i << " num : "<< o<<CGoGNendl;
pitiot's avatar
pitiot committed
278
		sim_->envMap_.pushObstacleInCells(o);
pitiot's avatar
maj    
pitiot committed
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
void MovingObstacle::initGL()
{
#ifdef EXPORTING_BOXES

	m_render = new Algo::Render::GL2::MapRender();

	m_positionVBO = new Utils::VBO();

	// using simple shader with color
	m_shader = new Utils::ShaderSimpleColor();
	m_shader->setAttributePosition(m_positionVBO);
	m_shader->setColor(Geom::Vec4f(0.,1.,0.,0.));
//	m_shader->setAmbiant(Geom::Vec4f(0.,1.,0.,0.));
//	m_shader->setDiffuse(Geom::Vec4f(0.,1.,0.,0.));

	m_positionVBO->updateData(position) ;

	m_render->initPrimitives<PFP>(map, Algo::Render::GL2::LINES,false) ;
	m_render->initPrimitives<PFP>(map, Algo::Render::GL2::TRIANGLES,false) ;

//	registerShader(m_shader);
#endif
}

void MovingObstacle::draw()
{
#ifdef EXPORTING_BOXES
//	m_render->initPrimitives<PFP>(map, Algo::Render::GL2::LINES,false) ;
//	m_render->initPrimitives<PFP>(map, Algo::Render::GL2::TRIANGLES,false) ;
	m_positionVBO->updateData(position);
312
313
//	m_shader->setColor(Geom::Vec4f(movingObstacleNeighbors_.size()==0 ? 1.0f : 0,0.,0.,0.));
	m_shader->setColor(Geom::Vec4f(1.0f,0.,0.,0.0));
314
315
316
317
	m_render->draw(m_shader, Algo::Render::GL2::TRIANGLES);
	m_shader->setColor(Geom::Vec4f(0.,0.,0.,0.));
	m_render->draw(m_shader, Algo::Render::GL2::LINES);
#endif
pitiot's avatar
maj    
pitiot committed
318
319
}

320
321
VEC3 MovingObstacle::getDilatedPosition(unsigned int ind)
{
322
	Dart d(ind); //WARNING : works only for one face created at start !
Arash HABIBI's avatar
Arash HABIBI committed
323
324
//	return position[d]+deformation[d];
	return position[d];
325
326
327
328
}

VEC3 MovingObstacle::getPosition(unsigned int ind)
{
329
330
	Dart d(ind);
	return position[d];
331
332
}

pitiot's avatar
maj    
pitiot committed
333
334
335
336
bool MovingObstacle::test_opposition(VEC3 o, VEC3 p1, VEC3 p2)
{
	o.normalize();
	o *= -1;
337
338
339
	VEC3 vector(p1 - p2);
	vector.normalize();
	return (o - vector).norm2() < 0.1;
pitiot's avatar
maj    
pitiot committed
340
341
342
}

// TODO Check position
pitiot's avatar
pitiot committed
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
//void MovingObstacle::contournerBatiment()
//{
//	PFP::VEC3 toto;
//	PFP::VEC3 toto_norm;
//	//for particles in the "front" of the object (to modify)
//	for(int k =0;k<2;k++)
//	{
//
//		registering_part->get_memo(vertices[k]);
//		std::vector<Obstacle*>& obst = sim_->envMap_.obstvect[registering_part->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, vertices[k]);
//				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,front,center)) //// à changer ////////////
//					{
//						int co = rand() % 2 ;
//						if (toto[0]==0)
//						{
//							finalGoal[0]=front[0];
//							if (co == 0)
//								finalGoal[0]=sim_->envMap_.geometry.max()[1]-maxNeighbors_;
//							else
//								finalGoal[0]=sim_->envMap_.geometry.min()[1]+maxNeighbors_;
//						}
//						else
//						{
//							finalGoal[1]=front[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,front -center);
//						make_half_turn=angle*nbVertices;
//						break;
//					}
//				}
//			}
//		}
//	}
//}
pitiot's avatar
maj    
pitiot committed
397

pitiot's avatar
pitiot committed
398
void MovingObstacle::updateAgentNeighbors() // agents voisins avec distance au bord (distpointlinesq) de l'obstacle // a mettre en place si besoin
pitiot's avatar
maj    
pitiot committed
399
{
Thomas Jund's avatar
Thomas Jund committed
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
442
443
444
445
446
447
448
449
450
451
452
	//test for slugs : doesn't always work.. :)
//	if(!rigid_)
//	{
//		Dart d = registering_part->d;
//
//		std::list<MovingObstacle*> seen;
//		PFP::OBSTACLEVECT& obst = sim_->envMap_.obstvect[d] ;
//		PFP::OBSTACLEVECT& neighborObst = sim_->envMap_.neighborObstvect[d] ;
//
//		for (std::vector<Obstacle*>::iterator it = obst.begin(); it != obst.end(); ++it)
//		{
//
//			if ((*it)->mo!=NULL)
//			{
//				MovingObstacle* other = (*it)->mo;
//				if(other->velocity_ * velocity_<=0.0f) //stop when not in front of each other
//				{
//					if(velocity_*(other->registering_part->getPosition()-registering_part->getPosition())>0)
//					{
//						newVelocity_ = VEC3(newVelocity_[1], -newVelocity_[0], 0);
//						seen.push_back(other);
//					}
//				}
//				else if(velocity_*(other->registering_part->getPosition()-registering_part->getPosition())>0
//						&& (other->registering_part->getPosition()-registering_part->getPosition()).norm2()<2000.0f)
//					newVelocity_ *= 0.01f;
//			}
//		}
//
//		for (std::vector<Obstacle*>::iterator it = neighborObst.begin(); it != neighborObst.end(); ++it)
//		{
//
//			if ((*it)->mo!=NULL)
//			{
//				MovingObstacle* other = (*it)->mo;
//				if(std::find(seen.begin(),seen.end(), other)==seen.end())
//				{
//					if(other->velocity_ * velocity_<=0.0f
//							&& (other->registering_part->getPosition()-registering_part->getPosition()).norm2()<2000.0f)
//					{
//
//						if(velocity_*(other->registering_part->getPosition()-registering_part->getPosition())>0)
//						{
//							newVelocity_ = VEC3(newVelocity_[1], -newVelocity_[0], 0);
//						}
//					}
////					else if(velocity_*(other->registering_part->getPosition()-registering_part->getPosition())>0)
////						newVelocity_ *= 0.5f;
//				}
//			}
//		}
//	}

pitiot's avatar
pitiot committed
453
454
455
456
457
//	agentNeighbors_.clear() ;
//	Dart d;
//	float maxDist = 0.0f ;//distance max des agents eregistrés au centre
//	std::set::iterator agIt;//emplacement de l'agent en question
//	for(int i = 0;i< nbVertices;i++ )
pitiot's avatar
pitiot committed
458
//	{
pitiot's avatar
pitiot committed
459
460
461
462
463
464
//		registering_part->get_memo(vertices[i]);
//		d=registering_part->d;
//		const std::vector<Agent*>& agents = sim_->envMap_.agentvect[d] ;
//		const std::vector<Agent*>& neighborAgents = sim_->envMap_.neighborAgentvect[d] ;
//
//
pitiot's avatar
pitiot committed
465
//
pitiot's avatar
pitiot committed
466
467
468
469
//		for (std::vector<Agent*>::const_iterator it = agents.begin(); it != agents.end(); ++it)
//		{
//			if ((*it)->alive)
//			{
pitiot's avatar
pitiot committed
470
//
pitiot's avatar
pitiot committed
471
472
473
474
//				float distSq = (center - (*it)->getPosition()).norm2() ;
//				if ((agentNeighbors_.size() < maxNeighbors_ || distSq < maxDist)
//					&& distSq < neighborDistSq_)
//				{
pitiot's avatar
pitiot committed
475
//
pitiot's avatar
pitiot committed
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//					if (distSq > maxDist)
//					{
//						maxDist = distSq ;
//						agIt=(agentNeighbors_.insert(std::make_pair(distSq, *it))).first() ;
//					}
//					else
//					{
//						agentNeighbors_.insert(std::make_pair(distSq, *it))
//					}
//				}
//
//			}
//		}
//
//		for (std::vector<Agent*>::const_iterator it = neighborAgents.begin(); it != neighborAgents.end(); ++it)
//		{
//			if ((*it)->alive)
//			{
//				float distSq = (getPosition() - (*it)->getPosition()).norm2() ;
//				if ((agentNeighbors_.size() < maxNeighbors_ || distSq < maxDist)
//					&& distSq < neighborDistSq_)
//				{
//					if (distSq > maxDist)
//					{
//						maxDist = distSq ;
//						agIt=(agentNeighbors_.insert(std::make_pair(distSq, *it))).first() ;
//					}
//					else
//					{
//						agentNeighbors_.insert(std::make_pair(distSq, *it))
//					}
//				}
//			}
//
//		}
pitiot's avatar
pitiot committed
511
//	}
pitiot's avatar
pitiot committed
512
513
514
515
}

void MovingObstacle::updateObstacleNeighbors() // obstacles voisins , distance par rapport aux centres des segments// a mettre en place si besoin
{
Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
516
517
	obstacleNeighbors_.clear() ;
	movingObstacleNeighbors_.clear() ;
pitiot's avatar
pitiot committed
518
	for (std::vector< std::pair<Dart, int> >::iterator it2 = general_belonging.begin();it2 != general_belonging.end(); ++it2)
Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
519
	{
pitiot's avatar
pitiot committed
520
521
		std::vector<Obstacle*>& obst = sim_->envMap_.obstvect[(*it2).first] ;
		std::vector<Obstacle*>& neighborObst = sim_->envMap_.neighborObstvect[(*it2).first] ;
522
523
524
525
526
//		float maxDistObst = 0.0f ;
//		float maxDistMovingObst = 0.0f ;
		float distance_detection=2.5* gravity_dist;
		float distance_detectionSq=distance_detection*distance_detection;
		for(std::vector<Obstacle*>::const_iterator it = obst.begin() ; it != obst.end() ; ++it)
Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
527
		{
528
			if ((*it)->mo==NULL)
Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
529
			{
530
531
532
				float distSq = distSqPointLineSegment((*it)->p1, (*it)->p2, center) ;
				if (/*(obstacleNeighbors_.size() < maxMovingObstacles_|| distSq < maxDistObst)
						&&*/ distSq < distance_detectionSq)
Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
533
				{
534
535
536
537
538
539
540
541


//						if (distSq > maxDistObst)
//							maxDistObst = distSq ;
						obstacleNeighbors_.push_back(std::make_pair(distSq, *it)) ;



Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
542
543
				}
			}
544
545
546
547
548
549
550
551
552
553
554
555
556
			else
			{
				if((*it)->mo->index!=index)
				{
					float distSq = distSqPointLineSegment((*it)->p1, (*it)->p2, center) ;
					if (/*(movingObstacleNeighbors_.size() < maxMovingObstacles_ || distSq < maxDistMovingObst) &&*/
							 distSq < distance_detectionSq)
					{

//						if (distSq > maxDistMovingObst)
//							maxDistMovingObst = distSq ;
						movingObstacleNeighbors_.push_back(std::make_pair(distSq, *it)) ;

Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
557

558
559
560
561
562

					}

				}
			}
Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
563
		}
564
565

		for(std::vector<Obstacle*>::const_iterator it = neighborObst.begin() ; it != neighborObst.end() ; ++it)
Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
566
		{
567
			if ((*it)->mo==NULL)
Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
568
			{
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
				float distSq = distSqPointLineSegment((*it)->p1, (*it)->p2, center) ;
				if (/*(obstacleNeighbors_.size() < maxMovingObstacles_|| distSq < maxDistObst)
						&&*/ distSq < distance_detectionSq)
				{


//						if (distSq > maxDistObst)
//							maxDistObst = distSq ;
						obstacleNeighbors_.push_back(std::make_pair(distSq, *it)) ;



				}
			}
			else
			{
				if((*it)->mo->index!=index)
				{
					float distSq = distSqPointLineSegment((*it)->p1, (*it)->p2, center) ;
					if (/*(movingObstacleNeighbors_.size() < maxMovingObstacles_ || distSq < maxDistMovingObst) &&*/
							 distSq < distance_detectionSq)
					{

//						if (distSq > maxDistMovingObst)
//							maxDistMovingObst = distSq ;
						movingObstacleNeighbors_.push_back(std::make_pair(distSq, *it)) ;
					}
				}
Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
597
598
599
			}
		}
	}
pitiot's avatar
maj    
pitiot committed
600
601
}

Arash HABIBI's avatar
unidir    
Arash HABIBI committed
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
//-------------------------------------------------------------

int matrixInverse(VEC3 v1, VEC3 v2, VEC3 v3, VEC3 *inv1, VEC3 *inv2, VEC3 *inv3)
{
	int res = 0;
	float determinant = v1[0]*v2[1]*v3[2] + v2[0]*v3[1]*v1[2] + v3[0]*v1[1]*v2[2] - v3[0]*v2[1]*v1[2] - v2[0]*v1[1]*v3[2] - v1[0]*v3[1]*v2[2];

	if(determinant==0.0)
		res=1;

	else
	{
		float un_sur = 1.0 / determinant;

		*inv1 = VEC3( v2[1]*v3[2]-v3[1]*v2[2], v2[0]*v3[2]-v3[0]*v2[2], v2[0]*v3[1]-v3[0]*v2[1] );
		*inv2 = VEC3( v1[1]*v3[2]-v3[1]*v1[2], v1[0]*v3[2]-v3[0]*v1[2], v1[0]*v3[1]-v3[0]*v1[1] );
		*inv3 = VEC3( v1[1]*v2[2]-v2[1]*v1[2], v1[0]*v2[2]-v2[0]*v1[2], v1[0]*v2[1]-v2[0]*v1[1] );
		res=0;
		*inv1 = *inv1 * un_sur;
		*inv2 = *inv2 * un_sur;
		*inv3 = *inv3 * un_sur;
	}
	return res;
}

//-------------------------------------------------------------

float distToLine(VEC3 M, VEC3 P, VEC3 u)
{
	u.normalize();
	VEC3 MP = P - M;
	return(MP*(u^(MP^u)));
}

//-------------------------------------------------------------
// Première stratégie : appliquer à p1 et à p2 une force
// colinéaire à f, mais d'intensité différente. Dans ce cas,
// il est nécessaire de calculer la somme des moments et de
// l'annuler. *f1 = f*d2/(d1+d2) et *f2 = f*d1/(d1+d2).

void getResponse1(VEC3 f, VEC3 p, VEC3 p1, VEC3 p2, VEC3 *f1, VEC3 *f2)
{
	float d1  = distToLine(p1,p,f);
	float d2  = distToLine(p2,p,f);

	*f1 = f*(-d2/(d1+d2));
	*f2 = f*(-d1/(d1+d2));
}

//-------------------------------------------------------------
// Deuxième stratégie, les forces appliquées à p1 et p2
// doivent etre colinaires respectivement à pp1 et pp2.
// Donc la somme des moments est automatiquement nulle.
// Pour calculer les intensites, il faut décomposer f
// dans la base (pp1,pp2), ce qui donnera l'intensite de
// chaque force. Problème : on peut générer des forces
// d'étirement importantes.

void getResponse2(VEC3 f, VEC3 p, VEC3 p1, VEC3 p2, VEC3 *f1, VEC3 *f2)
{
	float d1  = (p1-p).norm();
	float d2  = (p2-p).norm();
	float d12 = (p2-p1).norm();

	VEC3 ortho = (p1-p) ^ (p2-p);
	if(ortho.norm()==0.0)
	{
		if(d12==0) // comprend le cas où d1==0 et d2==0
		{
			*f2 = (-0.5)*f;
			*f1 = (-0.5)*f;
		}
		else if(d1==0)
		{
			*f2 = VEC3(0,0,0);
			*f1 = -f;
			// meilleure idee : projeter f sur p1p2 -> f2 et f1 <- f-f2
		}
		else if(d2==0)
		{
			*f1 = VEC3(0,0,0);
			*f2 = -f;
			// meilleure idee : projeter f sur p1p2 -> f1 et f2 <- f-f1
		}
		else // trois points distincts, mais alignés
		{
			// A suivre
		}
	}
	// int res = matrixInverse(p1-p,p2-p,ortho, VEC3 *inv1, VEC3 *inv2, VEC3 *inv3);
	// inv1, inv2 et inv3 sont les composantes de la matrice inverse. L'image de
	// p1-p et p2-p par cette matrice donne la valeur des forces f1 et f2.
}


//-------------------------------------------------------------

void MovingObstacle::initForces()
{
	if(!rigid_)
		for (unsigned int i = 1; i < nbVertices; ++i)
		{
			//initialisation of forces
			forces[dd] = VEC3(0.0);
			map.next(dd);
		}

}

//-------------------------------------------------------------

pitiot's avatar
maj    
pitiot committed
713
714
void MovingObstacle::update()
{
pitiot's avatar
pitiot committed
715
	assert(sim_->envMap_.map.getCurrentLevel() == sim_->envMap_.map.getMaxLevel()) ;
pitiot's avatar
pitiot committed
716

pitiot's avatar
pitiot committed
717
	//pour les tests de détection///////////////
pitiot's avatar
merging    
pitiot committed
718
719
720
721
722
723
724
	if(!seen)
	{
		color1=1.0f;
		color2=1.0f;
		color3=1.0f;
	}
	seen=false;
Thomas Jund's avatar
merge    
Thomas Jund committed
725

pitiot's avatar
pitiot committed
726
	//////////////////////////
Thomas Jund's avatar
merge    
Thomas Jund committed
727

728
	PFP::VEC3 bary(0);
pitiot's avatar
maj    
pitiot committed
729
730

	Dart d;
pitiot's avatar
pitiot committed
731
732
	velocity_[0] = newVelocity_[0] * velocity_factor;
	velocity_[1] = newVelocity_[1] * velocity_factor;
Arash HABIBI's avatar
Arash HABIBI committed
733
734
	// velocity_[0] = 0.0;
	// velocity_[1] = 0.0;
pitiot's avatar
maj    
pitiot committed
735
736

	// MAJ des particules
pitiot's avatar
pitiot committed
737
	float abs_angle= angle > 0 ? 1 : -1;
pitiot's avatar
merge    
pitiot committed
738

pitiot's avatar
merging    
pitiot committed
739
	float rotor=0;
740

pitiot's avatar
merging    
pitiot committed
741
742
743
744
	if (index_parent==0)
		rotor = abs_angle*angle > 0.04f ? 0.04f : abs_angle*angle ;
	else
		rotor = abs_angle*angle ;
pitiot's avatar
merge    
pitiot committed
745

Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
746
747
	// masse ressort pour la limace

748
	if(!rigid_)
pitiot's avatar
maj    
pitiot committed
749
	{
750
751
		Dart d = groundFace;
		map.next(d);
pitiot's avatar
maj    
pitiot committed
752

Thomas Jund's avatar
Thomas Jund committed
753
754
755
756
		Dart dd =d;
		for (unsigned int i = 1; i < nbVertices; ++i)
		{
			//initialisation of forces with viscosity
Arash HABIBI's avatar
unidir    
Arash HABIBI committed
757
			forces[dd] += -0.2f*velocity[dd];
Arash HABIBI's avatar
Arash HABIBI committed
758
			// forces[dd] = VEC3(0.0);
Thomas Jund's avatar
Thomas Jund committed
759
760
761
			map.next(dd);
		}

762
		CellMarkerStore<EDGE> cm(map);
Arash HABIBI's avatar
Arash HABIBI committed
763
		CellMarkerStore<VERTEX> cmV(map);
Thomas Jund's avatar
Thomas Jund committed
764
		DartMarkerStore dm(map);
Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
765
		// ARASH : On parcourt les sommets de la grande face
766
767
768
		for (unsigned int i = 1; i < nbVertices; ++i)
		{
			Dart dd = d;
Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
769
770

			// ARASH : On parcourt les sous-faces triangulaire de la grande face
Arash HABIBI's avatar
Arash HABIBI committed
771

772
773
774
775
			do {
				if(!cm.isMarked(dd))
				{
					cm.mark(dd);
Thomas Jund's avatar
Thomas Jund committed
776
777
					VEC3 p1Next = position[map.phi1(dd)]+(velocity[map.phi1(dd)] * sim_->timeStep_);
					VEC3 p2Next = position[dd]+(velocity[dd] * sim_->timeStep_);
Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
778
					// p1Next et p2Next sont la position des extremites de l'arete.
Thomas Jund's avatar
Thomas Jund committed
779
					VEC3 v1 = (p1Next-p2Next);
780
781
782

					//stretch spring : /!\ max rigidity relative to the timestep used (unstable otherwise)
					float norm = v1.norm();
Thomas Jund's avatar
Thomas Jund committed
783
					float rigidity = 70.0f;
784

785
					float stretch = rigidity*(edgeLength[dd]-v1.norm());
786
787
					if(norm>0.0f)
					{
Arash HABIBI's avatar
Arash HABIBI committed
788
						VEC3 f = 1.5*stretch*(v1/norm);
789

790
791
792
793
						//apply force symmetrically
						forces[dd] -= f;
						forces[map.phi1(dd)] += f;
					}
Arash HABIBI's avatar
Arash HABIBI committed
794
795
				}

Thomas Jund's avatar
Thomas Jund committed
796
797
798
				dd = map.phi1(dd);
			} while(dd!=d);

Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
799
			// ARASH : les ressorts angulaires
Thomas Jund's avatar
Thomas Jund committed
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
			do {
				if(!dm.isMarked(dd))
				{
					dm.mark(dd);
//					VEC3 v1 = (position[map.phi1(dd)]-position[dd]);
					VEC3 p1Next = position[map.phi1(dd)]+(velocity[map.phi1(dd)] * sim_->timeStep_);
					VEC3 p2Next = position[dd]+(velocity[dd] * sim_->timeStep_);
					VEC3 v1 = (p1Next-p2Next);

					float norm = v1.norm();

					//bend spring: /!\ max rigidity relative to the timestep used (unstable otherwise)
					float restAngle = vertexAngle[dd];
					if(restAngle!=0.0f)
					{
Thomas Jund's avatar
Thomas Jund committed
815
						float angularRig = 70.0f;
Thomas Jund's avatar
Thomas Jund committed
816

817
						float curAngle = Algo::Surface::Geometry::angle<PFP>(map, map.phi_1(dd),map.phi1(dd),position);
818

Thomas Jund's avatar
Thomas Jund committed
819
820
						float angularStretch = angularRig*(restAngle-curAngle);

821
822
823
824
825
826
827
						if(norm>0.0f && curAngle==curAngle) //test to avoid NaN
						{
							VEC3 f = angularStretch*(v1/norm);

							forces[dd] += f;
							forces[map.phi1(dd)] -= f;
						}
Thomas Jund's avatar
Thomas Jund committed
828
					}
829
				}
pitiot's avatar
maj    
pitiot committed
830

831
832
				dd = map.phi1(dd);
			} while(dd!=d);
pitiot's avatar
maj    
pitiot committed
833

Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
834
835
836
837
			//-------------------------------------------------------------------------
			// ARASH : A présent on calcule les interactions avec les autres obstacles.

			do {
Arash HABIBI's avatar
Arash HABIBI committed
838
				if(!cmV.isMarked(dd))
Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
839
				{
Arash HABIBI's avatar
Arash HABIBI committed
840
					cmV.mark(dd);
Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
841

Arash HABIBI's avatar
comment    
Arash HABIBI committed
842
					VEC3 norm;
Arash HABIBI's avatar
Arash HABIBI committed
843
844
					double obst_stiffness = 5.0; // agent-obstacle interaction stiffness
					int obst_power = 2  ;           // the power to which elevate the agent-obstacle distance
Arash HABIBI's avatar
comment    
Arash HABIBI committed
845
846
847
848
849

					double obst_radius_infl;
					if(sim_->config==0)
						obst_radius_infl = 100.; // scenario 0
					else
Arash HABIBI's avatar
Arash HABIBI committed
850
						obst_radius_infl = 30.; // scenario 1 et 3
Arash HABIBI's avatar
comment    
Arash HABIBI committed
851
852
					float force_value;

Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
853
854
855
856
857
858
859
860
					VEC3 p = position[dd]+(velocity[dd] * sim_->timeStep_);

					for(std::vector<std::pair<float, Obstacle*> >::iterator it = movingObstacleNeighbors_.begin() ;
						it != movingObstacleNeighbors_.end() ; ++it)
					{
						Obstacle * obst = it->second;
						VEC3 p1=obst->p1 ;
						VEC3 p2=obst->p2 ;
Arash HABIBI's avatar
unidir    
Arash HABIBI committed
861
862
863
864
865
866
867
868
869
870
871
872
873

						MovingObstacle *mo = obst->mo;
						if(mo!=NULL)
						{
							// int N = mo->nbVertices;
							// int ind1 = obst->index;
							// int ind2 = (obst->index+1)%N;
							// fprintf(stderr,"%d %d [%d]\n",ind1,ind2,N);
							// cout << "-------------------" << mo->forces[ind1] << endl;
						}
						else
							fprintf(stderr,"NULL\n");

Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
874
875
876
877
878
879
880
881
						double longueur2 = (p1-p2).norm2();
						double rest_sum_of_dists = 2 * sqrt(obst_radius_infl*obst_radius_infl + longueur2/4);

						double d1 = (p-p1).norm();
						double d2 = (p-p2).norm();
						double sum_of_dists = d1+d2;
						if(sum_of_dists < rest_sum_of_dists)
						{
Arash HABIBI's avatar
comment    
Arash HABIBI committed
882
							double collision_softening_factor = pow(1-sum_of_dists/rest_sum_of_dists,obst_power);
Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
883
884
885
886
887
888
889
							force_value = obst_stiffness*collision_softening_factor*(rest_sum_of_dists - sum_of_dists);
							VEC3 v_obst = p2 - p1;
							VEC3 normal = VEC3(v_obst[1],-v_obst[0],0);
							// Ajouter une composante tangentielle
							normal += v_obst * ((d1-d2)/(5*sum_of_dists));
							// Le facteur 5 est là seulement pour diminuer l'influence de la composante tangentielle
							normal.normalize();
Arash HABIBI's avatar
comment    
Arash HABIBI committed
890

Arash HABIBI's avatar
Arash HABIBI committed
891
							// force_value *= 10;
Arash HABIBI's avatar
unidir    
Arash HABIBI committed
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
							VEC3 force_vector = force_value * normal;
							forces[dd] += force_vector;

							// obst->index ??? indice local de l'obstacle dans le mo
							// obst->mo : le moving_obstacle
							// obst->mo->nbVertices
							// obst->mo->forces[obst->index]
							// obst->mo->forces[obst->index+1%obst->mo->nbVertices]
							VEC3 force_vector1, force_vector2;
							getResponse1(force_vector,p,p1,p2,&force_vector1,&force_vector2);
							int indice1 = obst->index;
							int indice2 = (obst->index+1)%(obst->mo->nbVertices);
							// obst->mo->forces[indice1] += force_vector1;
							// obst->mo->forces[indice2] += force_vector2;
							VEC3 p11 = obst->mo->position[indice1];
							VEC3 p22 = obst->mo->position[indice2];
							// cerr << "p1=" << p1 << " p11=" << p11 << " p2=" << p2 << " p22=" << p22 << endl;
Arash HABIBI's avatar
03_2013    
Arash HABIBI committed
909
910
911
912
913
914
						}
					}
				}
				dd = map.phi1(dd);
			} while(dd!=d);

915
916
917
918
			map.next(d);
		}

		//guiding vertex = first vertex (set the displacement)
Thomas Jund's avatar
Thomas Jund committed
919
		forces[groundFace] = VEC3(0);
920
		velocity[groundFace] = velocity_;
Arash HABIBI's avatar
Arash HABIBI committed
921

922
923
924
		//apply force to each vertex
		d = groundFace;
		for (unsigned int i = 0; i < nbVertices; ++i)
pitiot's avatar
pitiot committed
925
		{
Thomas Jund's avatar
Thomas Jund committed
926
			velocity[d] += forces[d] * sim_->timeStep_;
927
928
929
930
			position[d] += (velocity[d] * sim_->timeStep_);
			position[map.phi<211>(d)] += (velocity[d] * sim_->timeStep_);
			bary += position[d];
			map.next(d);
pitiot's avatar
pitiot committed
931
		}
932
	}
pitiot's avatar
pitiot committed
933
934
	else
	{
935
936
937
938
939
940
941
942
943
	//	CGoGNout << "Obstacle "<< index << CGoGNendl;
//		CGoGNout << "vitesse : "<< velocity_ << CGoGNendl;
		//	on fait tourner l'obstacle
		Dart d = groundFace;
		for (unsigned int i = 0; i < nbVertices; ++i)
		{
			position[d] += rotate(position[d], center, abs_angle*rotor);
			position[d] += (velocity_ * sim_->timeStep_);

944
945
946
947
948
#ifdef EXPORTING_BOXES
			position[map.phi<211>(d)] += rotate(position[map.phi<211>(d)], center, abs_angle*rotor);
			position[map.phi<211>(d)] += (velocity_ * sim_->timeStep_);
#endif

949
950
951
//			position[map.phi<211>(d)] += (velocity_ * sim_->timeStep_);

			bary += position[d];
Thomas Jund's avatar
Thomas Jund committed
952
953
954
955

#ifdef TWO_AND_HALF_DIM
			std::cout << " dist supp " << (getDilatedPosition(i)-parts_[i]->getPosition()).norm() << std::endl;
#endif
pitiot's avatar
pitiot committed
956
			parts_[i]->move(getDilatedPosition(i));
Thomas Jund's avatar
Thomas Jund committed
957
958
959
960
961
#ifdef TWO_AND_HALF_DIM
			std::cout << " dist " << parts_[i]->getDistance() << std::endl;

			position[d] = parts_[i]->getPosition();
#endif
962
963
964
			map.next(d);
		}

pitiot's avatar
pitiot committed
965
		front=(position[0] + position[1]) / 2;
966
967
968
969
		if(angle >0)
			angle -= rotor;
		else
			angle += rotor;
pitiot's avatar
pitiot committed
970
	}
pitiot's avatar
maj    
pitiot committed
971

pitiot's avatar
pitiot committed
972
	center = bary / nbVertices;
pitiot's avatar
maj    
pitiot committed
973

974
975
	//computing deformation vector on vertices :
	//depending on the velocity, extend the perceived shape
976
	VEC3 vel = velocity_.normalized();
977
978
979

	d = groundFace;
	for (unsigned int i = 0; i < nbVertices; ++i)
980
	{
981
		VEC3 v(position[d] - center);
982
983
		VEC3 vN = v.normalized();
		float dot = vN * vel;
984
985
		deformation[d] = v * 2.0f * (dot+1.0f)/2.0f;
		map.next(d);
986
987
	}

Thomas Jund's avatar
Thomas Jund committed
988
989
//	if(!rigid_)
//		center = position[groundFace];
990

pitiot's avatar
maj    
pitiot committed
991
992
993
994
995
	// MAJ des obstacles
	for (unsigned int i = 0; i < nbVertices; ++i)
	{
		//		CGoGNout << "avant une etape : Obstacle "<< i << CGoGNendl;
		Obstacle* o = obstacles_[i];
996
997
		o->p1 = getDilatedPosition(i);
		o->p2 = getDilatedPosition((i+1) % nbVertices);
Thomas Jund's avatar
Thomas Jund committed
998
999

#ifndef POTENTIAL
1000
1001
		o->prevP = getDilatedPosition((i - 1 + nbVertices) % nbVertices);
		o->nextP = getDilatedPosition((i + 2 + nbVertices) % nbVertices);
Thomas Jund's avatar
Thomas Jund committed
1002
1003
#endif

pitiot's avatar
pitiot committed
1004
1005
		Dart d1 = parts_[i]->d;
		Dart d2 = parts_[(i+1)%nbVertices]->d;
Thomas Jund's avatar
Thomas Jund committed
1006

pitiot's avatar
pitiot committed
1007
		if(!((sim_->envMap_.map.sameFace(d1,d2))&& (parts_[i]->crossCell==CGoGN::Algo::Surface::MovingObjects::NO_CROSS && parts_[(i+1)%nbVertices]->crossCell==CGoGN::Algo::Surface::MovingObjects::NO_CROSS)))
pitiot's avatar
pitiot committed
1008
		{
Thomas Jund's avatar
Thomas Jund committed
1009
			sim_->envMap_.popAndPushObstacleInCells(o,i);
pitiot's avatar
pitiot committed
1010
		}
Thomas Jund's avatar
Thomas Jund committed
1011

pitiot's avatar
pitiot committed
1012
1013
1014
1015
		/////affichage des belonging cells
//		CGoGNout<< CGoGNendl;
//		CGoGNout << "Obstacle "<< i << ": ";
//		for(std::vector<Dart>::iterator ite = belonging_cells[i].begin();ite!=belonging_cells[i].end(); ++ite)
1016
//		{
pitiot's avatar
pitiot committed
1017
//			CGoGNout<<(*ite).index<<" ; ";
1018
//		}
pitiot's avatar
pitiot committed
1019
//		CGoGNout<< CGoGNendl;
pitiot's avatar
maj    
pitiot committed
1020
1021
		//		CGoGNout << "Apres une etape : Obstacle "<< i << CGoGNendl;
	}
pitiot's avatar
pitiot committed
1022
1023
1024
1025
1026
1027
1028
1029
	/////affichage du general_belonging
//	CGoGNout<< CGoGNendl;
//	CGoGNout << "General : ";
//	for(std::vector<std::pair<Dart , int> >::iterator ite = general_belonging.begin();ite!=general_belonging.end(); ++ite)
//	{
//		CGoGNout<<"< "<<(*ite).first.index<<" , "<<(*ite).second<<"> ; ";
//	}
//	CGoGNout<< CGoGNendl;
pitiot's avatar
maj    
pitiot committed
1030
1031
1032
	//
	//		envMap_->addMovingObstAsNeighbor(this,general_belonging,&(general_neighbors));
	//		CGoGNout << "nouvel update : "<< CGoGNendl;
1033
1034
	if(sim_->detect_agent_collision)
	{
pitiot's avatar
pitiot committed
1035
		for (std::vector< std::pair<Dart, int> >::iterator it2 = general_belonging.begin(); it2 != general_belonging.end(); ++ it2)
1036
		{
pitiot's avatar
pitiot committed
1037
			std::vector<Agent*> vector =sim_->envMap_.agentvect[(*it2).first];
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
			for(std::vector<Agent*>::iterator it=vector.begin();it!=vector.end(); ++it)
			{
				if (this->is_inside((*it)->part_.getPosition()))
				{
					(*it)->alive=false;
					(*it)->color1=0.0f;
					(*it)->color2=0.4f;
					(*it)->color3=0.0f;
					sim_->envMap_.popAgentInCells(*it,(*it)->part_.d);
				}
			}
pitiot's avatar
maj    
pitiot committed
1049

1050
1051
		}
	}
pitiot's avatar
maj    
pitiot committed
1052
1053
1054

}

Thomas Jund's avatar
Thomas Jund committed
1055

pitiot's avatar
pitiot committed
1056
std::vector<Dart> MovingObstacle::getMemoCross(const VEC3& pos, const VEC3& dest, Dart& d1, Dart& d2)
pitiot's avatar
maj    
pitiot committed
1057
{
1058
1059
1060
#ifdef TWO_AND_HALF_DIM
	CGoGN::Algo::Surface::MovingObjects::ParticleCell2DAndHalfMemo<PFP> * registering_part = new CGoGN::Algo::Surface::MovingObjects::ParticleCell2DAndHalfMemo<PFP>(sim_->envMap_.map, d1,pos,sim_->envMap_.position);
#else
pitiot's avatar
pitiot committed
1061
	CGoGN::Algo::Surface::MovingObjects::ParticleCell2DMemo<PFP> * registering_part = new CGoGN::Algo::Surface::MovingObjects::ParticleCell2DMemo<PFP>(sim_->envMap_.map, d1,pos,sim_->envMap_.position);
1062
#endif
pitiot's avatar
pitiot committed
1063
1064
	std::vector<Dart> result =(registering_part->move(dest));
	d2=registering_part->d;
1065
//	CGoGNout<<"d1 : "<< *d1<<"|| d2 : "<< *d2<<"|| start : "<< pos<<"|| stop : "<< dest<<CGoGNendl;
pitiot's avatar
pitiot committed
1066
	return result;
pitiot's avatar
maj    
pitiot committed
1067
1068
}

1069
void resetPartSubdiv(Obstacle* o)
pitiot's avatar
maj    
pitiot committed
1070
1071
{
	MovingObstacle * mo = o->mo;
1072

pitiot's avatar
maj    
pitiot committed
1073
1074
	if (mo != NULL)
	{
pitiot's avatar
pitiot committed
1075
		int n =o->index;
Arash HABIBI's avatar
Arash HABIBI committed
1076

pitiot's avatar
pitiot committed
1077
		VEC3 pos =mo->parts_[n]->getPosition();
pitiot's avatar
pitiot committed
1078
		mo->parts_[n]->CGoGN::Algo::MovingObjects::ParticleBase<PFP>::move(Algo::Surface::Geometry::faceCentroid<PFP>(mo->sim_->envMap_.map, mo->parts_[n]->d, mo->sim_->envMap_.position)) ;
Arash HABIBI's avatar
Arash HABIBI committed
1079

pitiot's avatar
pitiot committed
1080
1081
1082
1083
		mo->parts_[n]->setState(FACE) ;
		mo->parts_[n]->move(pos) ;

		mo->dDir = mo->parts_[0]->d;
pitiot's avatar
maj    
pitiot committed
1084
1085
1086
1087
1088
1089
	}
}

void resetObstPartInFace(Obstacle* o, Dart d1)
{
	MovingObstacle * mo = o->mo;
1090

Thomas Jund's avatar
Thomas Jund committed
1091
1092
	if (mo != NULL)
	{
pitiot's avatar
pitiot committed
1093
1094
		unsigned int n =o->index;
		VEC3 pos1 = mo->parts_[n]->getPosition();
Thomas Jund's avatar
Thomas Jund committed
1095
1096

		if (Algo::Surface::Geometry::isPointInConvexFace2D<PFP>(mo->sim_->envMap_.map, d1, mo->sim_->envMap_.position, pos1, true))
pitiot's avatar
pitiot committed
1097
			mo->parts_[n]->d = d1;
Thomas Jund's avatar
Thomas Jund committed
1098

pitiot's avatar
pitiot committed
1099
		if(n==0)
pitiot's avatar
pitiot committed
1100
			mo->dDir = mo->parts_[0]->d;
pitiot's avatar
maj    
pitiot committed
1101
1102
1103
	}
}

pitiot's avatar
pitiot committed
1104
void resetPart(Obstacle * o, Dart d1)
pitiot's avatar
maj    
pitiot committed
1105
{
pitiot's avatar
pitiot committed
1106
1107
1108
1109
	MovingObstacle * mo = o->mo;
	if(mo!=NULL)
	{
		int n =o->index;
pitiot's avatar
pitiot committed
1110
1111
1112

		if (mo->parts_[n]->d == mo->sim_->envMap_.map.phi1(d1))
			mo->parts_[n]->d = d1;
Thomas Jund's avatar
Thomas Jund committed
1113

pitiot's avatar
pitiot committed
1114
		if(n==0)
pitiot's avatar
pitiot committed
1115
			mo->dDir = mo->parts_[n]->d;
pitiot's avatar
pitiot committed
1116
	}
pitiot's avatar
maj    
pitiot committed
1117
1118
1119
1120
1121
1122
}

void displayMO(Obstacle * o)
{
	CGoGNout << "obstacle problematique : " << (o->mo->index) << CGoGNendl;
}
pitiot's avatar
pitiot committed
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////comportement à modifier///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



// TODO Check position
void MovingObstacle::computePrefVelocity() //calcul du vecteur optimal pour atteindre l'objectif // changer pour 2.5 ?
{
pitiot's avatar
merging    
pitiot committed
1133
	VEC3 goalVector;
Thomas Jund's avatar
Thomas Jund committed
1134
	if (index_parent<1) //not articulated
pitiot's avatar
pitiot committed
1135
	{
Thomas Jund's avatar
Thomas Jund committed
1136
		if(rigid_ || goals_.size()>1)
pitiot's avatar
merging    
pitiot committed
1137
1138
		{
			goalVector = goals_[curGoal_] - center ;
Thomas Jund's avatar
Thomas Jund committed
1139
	//		goalVector = goals_[curGoal_] - position[groundFace] ;
pitiot's avatar
merging    
pitiot committed
1140

Thomas Jund's avatar
Thomas Jund committed
1141
1142
			float goalDist2 = goalVector.norm2() ;

1143
			if (goalDist2 < 1000.0f)
Thomas Jund's avatar
Thomas Jund committed
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
			{
				curGoal_ = (curGoal_ + 1) % goals_.size() ;
				goalVector = goals_[curGoal_] - center ;
	//			goalVector = goals_[curGoal_] - position[groundFace] ;
				goalDist2 = goalVector.norm2() ;
			}

			if (goalDist2 > maxSpeed_)
			{
				goalVector.normalize() ;
				goalVector *= maxSpeed_;
			}
		}
		else
pitiot's avatar
merging    
pitiot committed
1158
		{
Thomas Jund's avatar
Thomas Jund committed
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
			if(ag_!=NULL)
			{
				goalVector = ag_->getPosition() - center ;
				if (goalVector.norm2() > maxSpeed_)
				{
					goalVector.normalize() ;
					goalVector *= maxSpeed_;
				}
			}
			else
Thomas Jund's avatar
Thomas Jund committed
1169
			{
Thomas Jund's avatar
Thomas Jund committed
1170
1171
	//			if(sim_->envMap_.pedWayMark.isMarked(sim_->envMap_.map.phi2(dDir)))
	//				dDir = sim_->envMap_.map.phi1(sim_->envMap_.map.phi1(dDir));
Thomas Jund's avatar
Thomas Jund committed
1172

Thomas Jund's avatar
Thomas Jund committed
1173
				VEC3 mid = (sim_->envMap_.position[dDir]+sim_->envMap_.position[sim_->envMap_.map.phi1(dDir)])*0.5f;
Thomas Jund's avatar
Thomas Jund committed
1174
1175
				goalVector = mid - position[0] ;

Thomas Jund's avatar
Thomas Jund committed
1176
1177
1178
1179
1180
1181
1182
				Dart dStart=dDir;
				VEC3 dir = center - position[0];
				while(goalVector.norm2()==0 || sim_->envMap_.pedWayMark.isMarked(sim_->envMap_.map.phi2(dDir)) || ((dir*goalVector)>0))
				{
	//				std::cout << "d " << dDir << std::endl;
	//				std::cout << "dir*goalVector " << dir*goalVector << std::endl;
	//				std::cout << "goal vec " << goalVector << std::endl;
Thomas Jund's avatar
Thomas Jund committed
1183

Thomas Jund's avatar
Thomas Jund committed
1184
1185
1186
					dDir = sim_->envMap_.map.phi_1(dDir);
					mid = (sim_->envMap_.position[dDir]+sim_->envMap_.position[sim_->envMap_.map.phi1(dDir)])*0.5f;
					goalVector = mid - position[0] ;
Thomas Jund's avatar
Thomas Jund committed
1187

Thomas Jund's avatar
Thomas Jund committed
1188
1189
1190
					if(dDir==dStart)
						break;
				}
Thomas Jund's avatar
Thomas Jund committed
1191

Thomas Jund's avatar
Thomas Jund committed
1192
1193
				if(sim_->envMap_.map.isBoundaryEdge(sim_->envMap_.map.phi2(dDir)))
					goalVector = VEC3(0);
Thomas Jund's avatar
Thomas Jund committed
1194

Thomas Jund's avatar
Thomas Jund committed
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
	//			goalVector = mid - center ;

	//			std::cout << "dDir " << dDir << " goalVec" << goalVector << std::endl;

	//			float goalDist2 = goalVector.norm2() ;
	//			if (goalDist2 > maxSpeed_)
				{
					goalVector.normalize() ;
					goalVector *= maxSpeed_*maxSpeed_;
				}
Thomas Jund's avatar
Thomas Jund committed
1205
			}
pitiot's avatar
merging    
pitiot committed
1206
1207
		}
	}
Thomas Jund's avatar
Thomas Jund committed
1208
	else //articulated
pitiot's avatar
pitiot committed
1209
	{
Thomas Jund's avatar
Thomas Jund committed
1210
		goalVector = (get_center(parent,index_parent-1) -center);
pitiot's avatar
merging    
pitiot committed
1211
1212
1213
1214
1215
1216
		float goalDist2 = goalVector.norm2() ;
		if (goalDist2 > maxSpeed_)
		{
			goalVector.normalize() ;
			goalVector *= maxSpeed_;
		}
pitiot's avatar
pitiot committed
1217
	}
1218
1219

	if (spinning)
Thomas Jund's avatar
Thomas Jund committed
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
	{
		float n_angle = get_angle(goalVector,front-center);

		float bornAngle = M_PI/64.0f;
		if(fabs(n_angle-angle)<bornAngle)
			angle = n_angle;
		else
		{
			if(n_angle-angle>=0.0f)
				angle += bornAngle;
			else
				angle -= bornAngle;
		}
	}
pitiot's avatar
merging    
pitiot committed
1234

pitiot's avatar
pitiot committed
1235
1236
1237
1238
1239
	prefVelocity_ = goalVector ;

}
void MovingObstacle::computeNewVelocity()  //comportement des obstacles en tenant compte de l'environnement.
{
pitiot's avatar
pitiot committed
1240
1241
1242
1243
1244
	if (spinning)
	{
		VEC3 forward = front-center;
		float goalDist2 = forward.norm2() ;
		float objective = prefVelocity_.norm2();
1245

pitiot's avatar
pitiot committed
1246
		if (goalDist2 > objective)
1247
1248
1249
1250
1251
1252
1253
1254
		{
			forward.normalize() ;
			forward *= objective;
		}

		if(index_parent==0 && angle>0.01f)
			forward/=10;

pitiot's avatar
pitiot committed
1255
1256
1257
1258
1259
1260
		newVelocity_=forward;
	}
	else
	{
		newVelocity_=prefVelocity_;
	}
pitiot's avatar
pitiot committed
1261
}
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326



PFP::REAL MovingObstacle::computeMVC(PFP::VEC3 p, Dart vertex)
{
	PFP::REAL r = (position[vertex]-p).norm();