-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
745 lines (676 loc) · 24.7 KB
/
Copy pathmain.cpp
File metadata and controls
745 lines (676 loc) · 24.7 KB
1
2
3
4
5
6
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
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
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
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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
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
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
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
597
598
599
600
601
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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
#include <iostream>
#include <math.h>
#include <vector>
#include <string>
#include <sstream>
#include <list>
#include <algorithm>
using namespace std;
void rotation(double x, double y, double xo, double yo, double theta, double &xr, double &yr)
{
//rotate x,y around xo,yo by theta (rad)
xr = cos(theta) * (x - xo) - sin(theta) * (y - yo) + xo;
yr = sin(theta) * (x - xo) + cos(theta) * (y - yo) + yo;
}
enum MOUVEMENT
{
AVANCE = 0,
RECULE = 1,
AVANT_DROIT = 2,
AVANT_GAUCHE = 3,
ARRIERE_DROIT = 4,
ARRIERE_GAUCHE = 5,
STARTING = 6
};
string MOUVEMENTS_TEXTE[7] = {
"AVANCE",
"RECULE",
"AVANT_DROIT",
"AVANT_GAUCHE",
"ARRIERE_DROIT",
"ARRIERE_GAUCHE",
"STARTING",
};
const double epsilonMetres = 0.05;
const double epsilonRad = 0.08726646259971647;
class PointAngle
{
public:
double x;
double y;
double angleRad;
double distanceParcourue;
double estimation;
MOUVEMENT mouvement;
PointAngle *pere;
PointAngle() : x(0), y(0), angleRad(0), distanceParcourue(0), estimation(0), pere(NULL), mouvement(STARTING)
{
}
PointAngle(double _x, double _y, double _angleRad) : x(_x), y(_y), angleRad(_angleRad), distanceParcourue(0), estimation(0), pere(NULL), mouvement(STARTING)
{
}
PointAngle(double _x, double _y, double _angleRad,
double _distanceParcourue, MOUVEMENT _mouvement, PointAngle *_pere)
: x(_x), angleRad(0.f), y(_y), distanceParcourue(_distanceParcourue), estimation(0.f), mouvement(_mouvement), pere(_pere)
{
if (_angleRad <= -(M_PI))
{
angleRad = _angleRad + 2.0 * M_PI;
}
else if (_angleRad > M_PI)
{
angleRad = _angleRad - 2.0 * M_PI;
}
else
{
angleRad = _angleRad;
}
}
void vecteurAngle(double &xr, double &yr)
{
xr = cos(angleRad);
yr = sin(angleRad);
}
void vecteurAngleDroite(double &xr, double &yr)
{
double xr1, yr1, tmp;
vecteurAngle(xr1, yr1);
xr = yr1;
yr = -xr1;
}
void vecteurAngleGauche(double &xr, double &yr)
{
double xr1, yr1;
vecteurAngle(xr1, yr1);
xr = -yr1;
yr = xr1;
}
PointAngle *avance(double pas)
{
double xr, yr;
vecteurAngle(xr, yr);
double xFils = x + xr * pas;
double yFils = y + yr * pas;
return new PointAngle(xFils, yFils, angleRad, distanceParcourue + pas, AVANCE, this);
}
PointAngle *recule(double pas)
{
double xr, yr;
vecteurAngle(xr, yr);
double xFils = x - xr * pas;
double yFils = y - yr * pas;
return new PointAngle(xFils, yFils, angleRad, distanceParcourue + pas, RECULE, this);
}
PointAngle *avantDroit(double pas, double rayonBraquage)
{
double xr, yr, angleRotation = -pas / rayonBraquage; // vers ma droite => sens horaire => anti sens trigo
vecteurAngleDroite(xr, yr);
double xCentre = x + rayonBraquage * xr;
double yCentre = y + rayonBraquage * yr;
double xPos, yPos;
rotation(x, y, xCentre, yCentre, angleRotation, xPos, yPos);
return new PointAngle(xPos, yPos, angleRad + angleRotation, distanceParcourue + pas, AVANT_DROIT, this);
}
PointAngle *avantGauche(double pas, double rayonBraquage)
{
double xr, yr, angleRotation = pas / rayonBraquage; // vers ma droite => sens horaire => anti sens trigo
vecteurAngleGauche(xr, yr);
double xCentre = x + rayonBraquage * xr;
double yCentre = y + rayonBraquage * yr;
double xPos, yPos;
rotation(x, y, xCentre, yCentre, angleRotation, xPos, yPos);
return new PointAngle(xPos, yPos, angleRad + angleRotation, distanceParcourue + pas, AVANT_GAUCHE, this);
}
PointAngle *arriereDroit(double pas, double rayonBraquage)
{
double xr, yr, angleRotation = pas / rayonBraquage; // vers ma droite => sens horaire => anti sens trigo
vecteurAngleDroite(xr, yr);
double xCentre = x + rayonBraquage * xr;
double yCentre = y + rayonBraquage * yr;
double xPos, yPos;
rotation(x, y, xCentre, yCentre, angleRotation, xPos, yPos);
return new PointAngle(xPos, yPos, angleRad + angleRotation, distanceParcourue + pas, ARRIERE_DROIT, this);
}
PointAngle *arriereGauche(double pas, double rayonBraquage)
{
double xr, yr, angleRotation = -pas / rayonBraquage; // vers ma droite => sens horaire => anti sens trigo
vecteurAngleGauche(xr, yr);
double xCentre = x + rayonBraquage * xr;
double yCentre = y + rayonBraquage * yr;
double xPos, yPos;
rotation(x, y, xCentre, yCentre, angleRotation, xPos, yPos);
return new PointAngle(xPos, yPos, angleRad + angleRotation, distanceParcourue + pas, ARRIERE_GAUCHE, this);
}
PointAngle *bouge(MOUVEMENT mouvement, double pas, double rayonBraquage, unsigned int nombreDeFois = 1)
{
PointAngle *resultat;
if (mouvement == AVANCE)
resultat = avance(pas);
else if (mouvement == RECULE)
resultat = recule(pas);
else if (mouvement == AVANT_DROIT)
resultat = avantDroit(pas, rayonBraquage);
else if (mouvement == AVANT_GAUCHE)
resultat = avantGauche(pas, rayonBraquage);
else if (mouvement == ARRIERE_DROIT)
resultat = arriereDroit(pas, rayonBraquage);
else if (mouvement == ARRIERE_GAUCHE)
resultat = arriereGauche(pas, rayonBraquage);
else
throw "mouvement inconnu";
if (nombreDeFois == 1)
return resultat;
else
{
while (nombreDeFois != 1)
{
nombreDeFois--;
PointAngle *oldResultat = resultat;
resultat = resultat->bouge(mouvement, pas, rayonBraquage);
delete oldResultat;
}
return resultat;
}
}
bool tresProche(PointAngle p2)
{
return abs(x - p2.x) < epsilonMetres && abs(y - p2.y) < epsilonMetres && abs(angleRad - p2.angleRad) < epsilonRad;
}
};
ostream &operator<<(ostream &strm, const PointAngle &pa)
{
return strm << "PointAngle(x = " << pa.x << ", y = " << pa.y << ", angle = " << (pa.angleRad * 180.0 / M_PI)
<< ", dist = " << pa.distanceParcourue << " )";
}
string resumerManoeuvres(PointAngle *pa)
{
vector<PointAngle *> chaineDesPeres;
PointAngle *ancetre = pa;
while (ancetre->pere != NULL)
{
chaineDesPeres.push_back(ancetre);
ancetre = ancetre->pere;
}
// les vieux d'abord !
reverse(chaineDesPeres.begin(), chaineDesPeres.end());
// list des manoeuvres avec (manoeuvre, distanceParcourue)
vector< pair<MOUVEMENT, double> > chaineMouvements { pair<MOUVEMENT, double>(chaineDesPeres[0]->mouvement, chaineDesPeres[0]->distanceParcourue)};
double ancienneDistanceParcourue = chaineDesPeres[0]->distanceParcourue;
for (unsigned int i = 1; i < chaineDesPeres.size(); i++)
{
double pas = chaineDesPeres[i]->distanceParcourue - ancienneDistanceParcourue;
ancienneDistanceParcourue = chaineDesPeres[i]->distanceParcourue;
if (chaineMouvements.back().first == chaineDesPeres[i]->mouvement)
{
chaineMouvements.back().second = round((chaineMouvements.back().second + pas) * 1000.0) / 1000.0;
}
else
{
chaineMouvements.push_back(pair<MOUVEMENT, double>(chaineDesPeres[i]->mouvement, round(pas * 1000.0) / 1000.0));
}
}
string res = "";
for (unsigned int i = 0; i < chaineMouvements.size(); i++)
{
std::ostringstream strs;
strs << "(" << MOUVEMENTS_TEXTE[chaineMouvements[i].first] << ", " << chaineMouvements[i].second << ")";
if (i != chaineMouvements.size() - 1)
{
strs << ",";
}
res += strs.str();
}
return res;
}
void testMouvements()
{
cout << "TEST MOUVEMENTS" << endl;
MOUVEMENT mouvements[6] = {
AVANCE,
RECULE,
AVANT_DROIT,
AVANT_GAUCHE,
ARRIERE_DROIT,
ARRIERE_GAUCHE,
};
const double pas = 0.3;
const double rayonBraquage = 1.90;
const unsigned int nbrMouvements = 25;
PointAngle depart;
for (unsigned int i = 0; i < 6; i++)
{
PointAngle *resultat = depart.bouge(mouvements[i], pas, rayonBraquage, nbrMouvements);
cout << *resultat << " manoeuvres : " << resumerManoeuvres(resultat) << endl;
delete resultat;
}
}
double diffAngle(double a1, double a2)
{
double a = a2 - a1;
if (a > M_PI)
a -= 2.f * M_PI;
else if (a <= -M_PI)
a += 2.f * M_PI;
return abs(a);
}
bool butAtteint(PointAngle pointAngle, PointAngle butPointAngle, const double toleranceXY, const double toleranceAngle)
{
return abs(pointAngle.x - butPointAngle.x) < toleranceXY &&
abs(pointAngle.y - butPointAngle.y) < toleranceXY &&
diffAngle(pointAngle.angleRad, butPointAngle.angleRad) < toleranceAngle;
}
double distanceCarre(const PointAngle &pa1, const PointAngle &pa2)
{
return pow(pa2.x - pa1.x, 2.0) + pow(pa2.y - pa1.y, 2.0);
}
double distance(const PointAngle &pa1, const PointAngle &pa2)
{
return sqrt(distanceCarre(pa1, pa2));
}
double heuristiqueAngle(const PointAngle &pointAngle, const PointAngle &butPointAngle, const double rayonBraquage)
{
return rayonBraquage * diffAngle(pointAngle.angleRad, butPointAngle.angleRad);
}
double heuristique(const PointAngle &pointAngle, const PointAngle &butPointAngle, const double rayonBraquage, const double toleranceAngle = 0.0)
{
return max(distance(pointAngle, butPointAngle), heuristiqueAngle(pointAngle, butPointAngle, rayonBraquage) - toleranceAngle);
}
void transformeButVersRefBraquage(const PointAngle &pointAngle, const PointAngle &butPointAngle, double rayonBraquage, double &xOut, double &yOut)
{
xOut = butPointAngle.x;
yOut = butPointAngle.y;
// translation
xOut -= pointAngle.x;
yOut -= pointAngle.y;
// scale
xOut /= rayonBraquage;
yOut /= rayonBraquage;
// rotation
// butPointAngleAngle = butPointAngleAngle % (math.pi /2) # faux je pense. Les symétries en angle, pas important pour l'instant
// double butPointAngleAngle = butPointAngle.angleRad - pointAngle.angleRad;
rotation(xOut, yOut, 0, 0, -pointAngle.angleRad, xOut, yOut);
}
void calculMouvementsInaccessible()
{
}
double heuristiqueMeilleureDistanceInaccessible(const PointAngle &pointAngle, const PointAngle &butPointAngle, double rayonBraquage, double toleranceAngle = 0)
{
double butX, butY, distanceTotale;
transformeButVersRefBraquage(pointAngle, butPointAngle, rayonBraquage, butX, butY);
// symétrie avant / arriere
butX = abs(butX);
// gauche / droite
butY = abs(butY);
bool inaccessible = (butX * butX + (butY - 1.0) * (butY - 1.0)) < 1.0; //dans le cercle rayon de braquage
if (inaccessible)
{
// dans le cercle, on peut calculer les manoeuvres.
// addition d'une manoeuvre où on recule, une où on avance. dist1, dist2
// on utilise forcément le côté "vert", donc une rotation
// C => centre du cercle du bas, (0, -1)
// l = vecteur C => But
double lY = butY + 1.0;
double l = sqrt(butX * butX + lY * lY);
double angleL = atan2(lY, butX) - M_PI / 2.0;
// because reference is y axis, not x axis
// al kashi : c² = b² + a² - 2ab*gamma (gamma1 = angle F-C-But)
// => gamma1 = acos( (a²+b²-c²)/2ab)
// or b = 2, a = 1, c = l
// on obtient gamma1 = acos( (l²+3)/4l)
double gamma1 = acos((l * l + 3.0) / (4.0 * l));
double dist1 = gamma1 + angleL; // normalement : - angleL. test avec +.
// on réapplique Al kashi avec l'autre angle
// a=1, b = 2, c=l, on obtient
// acos( (5-l²) / 4)
double gamma2 = acos((5.0 - l * l) / 4.0);
distanceTotale = dist1 + gamma2;
}
else
{
double longueurL = sqrt(butX * butX + (butY - 1.0) * (butY - 1.0));
double longueurSegment = sqrt(longueurL * longueurL - 1.0);
double angleL = atan2(butY - 1.0, butX);
double angleTriangle = acos(1.0 / longueurL);
double angle = angleL - angleTriangle + (M_PI / 2.0);
distanceTotale = angle + longueurSegment;
}
// correction scaling
distanceTotale *= rayonBraquage;
return max(heuristiqueAngle(pointAngle, butPointAngle, rayonBraquage) - toleranceAngle, distanceTotale);
}
// LA FONCTION POUR XAVIER
void chercherMeilleureManoeuvreSansAngle(PointAngle &pointAngle, const PointAngle &butPointAngle, bool prefererArriere, double rayonBraquage, MOUVEMENT *mouvement1, double *outDist1, MOUVEMENT *mouvement2, double *outDist2, double *outAngleArrivee)
{
double butX, butY;
transformeButVersRefBraquage(pointAngle, butPointAngle, rayonBraquage, butX, butY);
// symétrie gauche / droite avant / arriere
bool inaccessible = (butX * butX + (abs(butY) - 1.0) * (abs(butY) - 1.0)) < 1.0; //dans le cercle rayon de braquage
bool symetrieX = butX < 0, symetrieY = butY < 0;
if (inaccessible)
{
if (symetrieY)
{
if (symetrieX && !prefererArriere)
{
*mouvement1 = AVANT_GAUCHE;
*mouvement2 = ARRIERE_DROIT;
}
else
{
*mouvement1 = ARRIERE_GAUCHE;
*mouvement2 = AVANT_DROIT;
}
}
else
{
if (symetrieX && !prefererArriere)
{
*mouvement1 = AVANT_DROIT;
*mouvement2 = ARRIERE_GAUCHE;
}
else
{
*mouvement1 = ARRIERE_DROIT;
*mouvement2 = AVANT_GAUCHE;
}
}
}
else
{
if (symetrieX)
{
if (symetrieY)
*mouvement1 = ARRIERE_DROIT;
else
*mouvement1 = ARRIERE_GAUCHE;
*mouvement2 = RECULE;
}
else
{
if (symetrieY)
*mouvement1 = AVANT_DROIT;
else
*mouvement1 = AVANT_GAUCHE;
*mouvement2 = AVANCE;
}
}
butY = abs(butY);
if (!inaccessible || !prefererArriere)
{
butX = abs(butX);
}
if (inaccessible)
{
//symétrie gauche / droite
butY = abs(butY);
// dans le cercle, on peut calculer les manoeuvres.
// addition d'une manoeuvre où on recule, une où on avance. dist1, dist2
// on utilise forcément le côté "vert", donc une rotation
// C => centre du cercle du bas, (0, -1)
// l = vecteur C => But
double lY = butY + 1.0;
double l = sqrt(butX * butX + lY * lY);
double angleL = atan2(lY, butX) - M_PI / 2.0;
// because reference is y axis, not x axis
// al kashi : c² = b² + a² - 2ab*gamma (gamma1 = angle F-C-But)
// => gamma1 = acos( (a²+b²-c²)/2ab)
// or b = 2, a = 1, c = l
// on obtient gamma1 = acos( (l²+3)/4l)
double gamma1 = acos((l * l + 3.0) / (4.0 * l));
// on réapplique Al kashi avec l'autre angle
// a=1, b = 2, c=l, on obtient
// acos( (5-l²) / 4)
double gamma2 = acos((5.0 - l * l) / 4.0);
*outDist1 = gamma1 + angleL;
*outDist2 = gamma2;
*outAngleArrivee = gamma1 + gamma2 + angleL;
}
else // accessible
{
double longueurL = sqrt(butX * butX + (butY - 1.0) * (butY - 1.0));
double longueurSegment = sqrt(longueurL * longueurL - 1.0);
double angleL = atan2(butY - 1.0, butX);
double angleTriangle = acos(1.0 / longueurL);
double angle = angleL - angleTriangle + (M_PI / 2.0);
*outDist1 = angle;
*outDist2 = longueurSegment;
*outAngleArrivee = angle;
}
// correction scaling
*outDist1 *= rayonBraquage;
*outDist2 *= rayonBraquage;
//symetries
if (symetrieY)
*outAngleArrivee = -(*outAngleArrivee);
if (symetrieX && !(inaccessible && prefererArriere))
*outAngleArrivee = -(*outAngleArrivee);
}
PointAngle *prendreMeilleurCandidat(list<PointAngle *> &pointsAChercher, const PointAngle &butPointAngle, double rayonBraquage, double toleranceAngle)
{
double meilleureEstimation = 100000000, estimation = 0;
list<PointAngle *>::iterator meilleurCandidatIterator = pointsAChercher.begin();
int i = 0;
for (auto pointIterator = pointsAChercher.begin(); pointIterator != pointsAChercher.end(); pointIterator++)
{
estimation = 0;
if ((*pointIterator)->estimation != 0)
{
estimation = (*pointIterator)->estimation;
}
else
{
estimation = (*pointIterator)->distanceParcourue + heuristiqueMeilleureDistanceInaccessible(**pointIterator, butPointAngle, rayonBraquage, toleranceAngle);
if ((*pointIterator)->pere != NULL)
{
estimation = max(estimation, (*pointIterator)->pere->estimation);
}
(*pointIterator)->estimation = estimation;
}
//cout << **pointIterator << " with estimation " << estimation << endl;
if (estimation < meilleureEstimation)
{
//meilleurCandidatIterator = list<PointAngle*>::iterator(pointIterator);
meilleurCandidatIterator = pointIterator;
meilleureEstimation = estimation;
//cout << estimation <<endl;
i++;
}
}
//cout << "i : " << i <<endl;
//cout << "size : " << pointsAChercher.size() << endl;
PointAngle *resultat = *meilleurCandidatIterator;
pointsAChercher.erase(meilleurCandidatIterator);
//cout << " resultat : " << *resultat << " with estimation " << estimation << endl;
return resultat;
}
void nettoyerListes(list<PointAngle *> l1, list<PointAngle *> l2)
{
for (auto i = l1.begin(); i != l1.end(); i++)
{
delete *i;
}
for (auto j = l2.begin(); j != l2.end(); j++)
{
delete *j;
}
}
bool chercherMeilleureManoeuvre(
const PointAngle &butPointAngle,
const double pas = 0.3,
const double rayonBraquage = 1.90,
const double toleranceXY = 0.36,
const double toleranceAngle = (20.0 / 180.0 * M_PI),
const int limite = -1)
{
unsigned int iterations = 0;
list<PointAngle *> pointsAChercher;
pointsAChercher.push_back(new PointAngle());
list<PointAngle *> pointsDejaCherches;
bool abandon = false;
while (limite == -1 || iterations < limite)
{
PointAngle *candidat = prendreMeilleurCandidat(pointsAChercher, butPointAngle, rayonBraquage, toleranceAngle);
//cout << *candidat << endl;
if (iterations % 100 == 0)
{
cout << iterations << " ";
}
for (auto mouvement = 0; mouvement < 6; mouvement++)
{
abandon = false;
PointAngle *successeur = candidat->bouge(MOUVEMENT(mouvement), pas, rayonBraquage);
if (butAtteint(*successeur, butPointAngle, toleranceXY, toleranceAngle))
{
cout << "but atteint en " << iterations << " iterations" << endl;
cout << *successeur << endl;
cout << resumerManoeuvres(successeur) << endl;
nettoyerListes(pointsAChercher, pointsDejaCherches);
return true;
}
else
{
for (auto it = pointsDejaCherches.begin(); it != pointsDejaCherches.end(); ++it)
{
if ((*it)->tresProche(*successeur) && (*it)->distanceParcourue < successeur->distanceParcourue)
{
//cout << "abandon deja cherche" << endl;
abandon = true;
break;
}
}
if (!abandon)
{
for (auto it = pointsAChercher.begin(); it != pointsAChercher.end(); ++it)
{
if ((*it)->tresProche(*successeur) && (*it)->distanceParcourue < successeur->distanceParcourue)
{
//cout << "abandon a chercher" << endl;
abandon = true;
break;
}
}
if (!abandon)
{
//cout << "ajout" << endl;
pointsAChercher.push_back(successeur);
}
}
}
}
pointsDejaCherches.push_back(candidat);
iterations++;
}
cout << "limite atteinte " << endl;
nettoyerListes(pointsAChercher, pointsDejaCherches);
return false;
}
//Fonctions de test
void testRotation()
{
double xr = 0;
double yr = 0;
rotation(0, 0, 0, 1, M_PI, xr, yr);
cout << "testRotation : " << xr << ", " << yr << " ok" << endl;
}
void testHeuristiqueInaccessible()
{
PointAngle start = PointAngle(0, 0, 0);
double epsilon = 0.0000000001;
bool test1 = heuristiqueMeilleureDistanceInaccessible(start, PointAngle(0, 1, 0), 1.0) - (acos(7.0 / 8.0) + acos(1.0 / 4.0)) < epsilon;
bool test2 = heuristiqueMeilleureDistanceInaccessible(start, PointAngle(0, 2, 0), 1.0) - (acos(-1.0) + acos(1.0)) < epsilon;
;
bool test3 = heuristiqueMeilleureDistanceInaccessible(start, PointAngle(0, 0, 0), 1.0) - 0.0 < epsilon;
cout << "quand l = 2 " << test1 << endl;
cout << "quand l = 3 " << test2 << endl;
cout << "quand l = 1 " << test3 << endl;
}
void testMeilleureManoeuvreSansAngleXYPreferer(double x, double y, bool prefererArriere)
{
cout << "point a tester : " << x << ", " << y << (prefererArriere ? " arriere" : "") << endl;
double rayonBraquage = 1.90;
PointAngle depart = PointAngle(0, 0, 0);
PointAngle arrivee = PointAngle(x, y, 0);
double distance1, distance2, angleArrivee;
MOUVEMENT mouvement1, mouvement2;
chercherMeilleureManoeuvreSansAngle(depart, arrivee, prefererArriere, rayonBraquage, &mouvement1, &distance1, &mouvement2, &distance2, &angleArrivee);
//Application du mouvement
PointAngle *apresMouvement1 = depart.bouge(mouvement1, distance1, rayonBraquage);
PointAngle *apresMouvement2 = apresMouvement1->bouge(mouvement2, distance2, rayonBraquage);
double epsilon = 0.000001;
if (abs(apresMouvement2->x - x) < epsilon && abs(apresMouvement2->y - y) < epsilon)
{
cout << "ok position" << endl;
}
else
{
cout << "FAIL position" << endl;
cout << apresMouvement2->x << " vs. " << x << endl;
cout << apresMouvement2->y << " vs. " << y << endl;
}
if (abs(apresMouvement2->angleRad - angleArrivee) < epsilon)
{
cout << "ok angle" << endl;
}
delete apresMouvement1;
delete apresMouvement2;
}
void testMeilleureManoeuvreSansAngle()
{
double tests[8][2] = {
//accessibles
{4, 1},
{-4, 1},
{4, -1},
{-4, -1},
//inaccessibles
{0.1, 0.5},
{-0.1, 0.5},
{0.1, -0.5},
{-0.1, -0.5}};
for (unsigned int i = 0; i < 8; i++)
{
testMeilleureManoeuvreSansAngleXYPreferer(tests[i][0], tests[i][1], true);
testMeilleureManoeuvreSansAngleXYPreferer(tests[i][0], tests[i][1], false);
}
}
int main()
{
//testRotation();
//testMouvements();
//testHeuristiqueInaccessible();
//testMeilleureManoeuvreSansAngle();
cout << " 1 : calculer manoeuvre avec angle (algo de recherche), 2 : calculer manoeuvre sans angle" << endl;
int l = 2;
cin >> l;
cout << (l == 1 ? "manoeuvre avec angle (algo de recherche) !" : "manoeuvre sans angle" ) << endl;
double x, y;
cout << " x ? " << endl;
cin >> x;
cout << " y ? " << endl;
cin >> y;
if (l == 1)
{
double angle;
cout << "angle en deg ? " << endl;
cin >> angle;
angle = angle * M_PI / 180.0;
cout << "calcul en cours..." << endl;
chercherMeilleureManoeuvre(PointAngle(x, y, angle));
}
else
{
bool prefererArriere = true;
cout << " preferer en arriere ? 0/1" << endl;
cin >> prefererArriere;
double distance1, distance2, angleArrivee;
MOUVEMENT mouvement1, mouvement2;
PointAngle depart(0, 0, 0);
chercherMeilleureManoeuvreSansAngle(depart, PointAngle(x, y, 0), prefererArriere, 1.90, &mouvement1, &distance1, &mouvement2, &distance2, &angleArrivee);
cout << "mouvement 1 : " << MOUVEMENTS_TEXTE[mouvement1] << " avec distance " << distance1 << endl;
cout << "mouvement 2 : " << MOUVEMENTS_TEXTE[mouvement2] << " avec distance " << distance2 << endl;
cout << "arrivee avec un angle de " << (angleArrivee * 180.0 / M_PI) << " deg" <<endl;
}
double wait;
cin >> wait;
return 0;
}