-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot.cc
More file actions
1405 lines (1227 loc) · 39.3 KB
/
robot.cc
File metadata and controls
1405 lines (1227 loc) · 39.3 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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "robot.h"
bool RobotSpatial::check_inside_domain(RobotSpatial RS)
{
if ((abs(RS.getX()) + getR() > dmax) or (abs(RS.getY()) + getR() > dmax))
{
cout << message::spatial_robot_ouside(RS.getX(), RS.getY());
return true;
}
return false;
}
bool RobotSpatial::check_superpos_N_N(RobotNeutralisateur n1, RobotNeutralisateur n2)
{
if (collision_cercles(n1.getCircle(), n2.getCircle(), false))
{
cout << message::neutralizers_superposition(n1.getX(), n1.getY(),
n2.getX(), n2.getY());
return true;
}
return false;
}
bool RobotSpatial::check_superpos_R_R(RobotReparateur r1, RobotReparateur r2)
{
if (collision_cercles(r1.getCircle(), r2.getCircle(), false))
{
cout << message::repairers_superposition(r1.getX(), r1.getY(),
r2.getX(), r2.getY());
return true;
}
return false;
}
bool RobotSpatial::check_superpos_R_N(RobotReparateur r1, RobotNeutralisateur n1)
{
if (collision_cercles(r1.getCircle(), n1.getCircle(), false))
{
cout << message::repairer_neutralizer_superposition(r1.getX(), r1.getY(),
n1.getX(), n1.getY());
return true;
}
return false;
}
bool RobotSpatial::check_superpos_R_P(Robot R, Particule p)
{
if (collision_carre_cercle(p.getSquare(), R.getCircle(), false))
{
cout << message::particle_robot_superposition(p.getX(), p.getY(), p.getD(),
R.getX(), R.getY(), R.getR());
return true;
}
return false;
}
bool RobotSpatial::check_nbUpdate(RobotNeutralisateur n, int k_update_panne_in)
{
if (k_update_panne_in > getNbUpdate())
{
cout << message::invalid_k_update(n.getX(), n.getY(), k_update_panne_in,
getNbUpdate());
return true;
}
return false;
}
int RobotSpatial::getNbNp() const
{
int NbNp(0);
vector<RobotNeutralisateur> robotsneutralisateurs = getNeutralisateurs();
for (int i(0); i < getnbRs_initial(); i++)
{
if (robotsneutralisateurs[i].getPanne())
{
NbNp += 1;
}
}
return NbNp;
}
void RobotReparateur::draw_robot_repairer() const
{
draw_robot_repairer_(getCircle());
}
void RobotNeutralisateur::draw_robot_neutralizer_panne() const
{
draw_robot_neutralizer_panne_(getCircle(), getA());
}
void RobotNeutralisateur::draw_robot_neutralizer_service() const
{
draw_robot_neutralizer_service_(getCircle(), getA());
}
void RobotNeutralisateur::draw_robot_neutralizer_collistion() const
{
draw_robot_neutralizer_collistion_(getCircle(), getA());
}
void RobotSpatial::draw_robot_spatial()
{
draw_robot_spatial_(getCircle());
}
void RobotSpatial::clear_robot()
{
reparateurs.clear();
neutralisateurs.clear();
};
bool RobotNeutralisateur::isColliding(const vector<RobotNeutralisateur> &robots)
{
for (const auto &robot : robots)
{
// Skip checking collision with self
if (this == &robot)
continue;
if (collision_cercles(this->getCircle(), robot.getCircle(), true))
{
return true;
}
}
return false;
}
void RobotSpatial::destroy_neutraliseur(int i){
neutralisateurs.erase(neutralisateurs.begin() + i);
++nbNd;
--nbNs;
}
/*
* Rotates the robot to the desired angle using a PID controller.
*
* @param desiredAngle the desired angle in radians
*
* @return void
*
* @throws None
*/
void RobotNeutralisateur::rotateToAngle(double desiredAngle)
{
double dt = delta_t;
double da;
// Calculate angle difference using atan2
double angleDifference =
atan2(sin(desiredAngle - getA()), cos(desiredAngle - getA()));
// Calculate the rotation speed
double rotationSpeed = angleDifference > 0 ? vrot_max : -vrot_max;
da = rotationSpeed * dt;
if (abs(angleDifference - da) <= epsil_alignement)
{
a = desiredAngle;
}
else
{
a += da;
}
// Ensure the angle stays within -pi to pi
if (a > M_PI)
{
a -= 2 * M_PI;
}
else if (a < -M_PI)
{
a += 2 * M_PI;
}
}
/*
* Moves the RobotNeutralisateur forwards until collision is detected.
*
* @param None
*
* @return None
*
* @throws None
*/
void RobotNeutralisateur::moveForwards()
{
double dt = delta_t;
// Move the robot until collision
circle.centre.x += cos(a) * vtran_max * dt;
circle.centre.y += sin(a) * vtran_max * dt;
}
/*
* Moves the robot towards a target point.
*
* @param target the target point to move towards
*
* @return void
*
* @throws None
*/
void RobotNeutralisateur::moveTowardsTarget(const S2d &target)
{
// Calculate the distance to the target
double distance = sqrt(pow(target.x - getX(), 2) + pow(target.y - getY(), 2));
// Define a threshold distance for slowing down
// (for instance, 2 times the maximum possible translation)
double slowDownDistance = 2 * vtran_max * delta_t;
// Adjust the speed according to the distance to the target
double speed;
if (distance < slowDownDistance)
{
// If we're within the slow-down distance, linearly interpolate the speed
//between 0 (at the target location) and vtran_max (at the slow-down distance)
speed = (distance / slowDownDistance) * vtran_max;
}
else
{
// If we're farther than the slow-down distance, move at maximum speed
speed = vtran_max;
}
// Apply the movement
double dx = cos(getA()) * speed * delta_t;
double dy = sin(getA()) * speed * delta_t;
circle.centre.x += dx;
circle.centre.y += dy;
}
double RobotNeutralisateur::getDistance(double x1, double y1, double x2, double y2)
{
return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}
int RobotNeutralisateur::getSide(int corner, double x, double y, double left,
double right, double top, double bottom)
{
switch (corner)
{
case 0: // Top Left
return (y <= top) ? 0 : ((x >= left) ? 3 : 4);
case 1: // Top Right
return (y <= top) ? 2 : ((x <= right) ? 3 : 4);
case 2: // Bottom Left
return (y >= bottom) ? 0 : ((x >= left) ? 1 : 4);
case 3: // Bottom Right
return (y >= bottom) ? 2 : ((x <= right) ? 1 : 4);
default:
return -1; // This should never happen
}
}
/*
* Calculates the collision side of a particule with respect to a point in a 2D space.
*
* @param target the particule whose collision side will be calculated
* @param x the x coordinate of the point in the space
* @param y the y coordinate of the point in the space
*
* @return an integer indicating the side
* of the particule that collided with the point
*
* @throws None
*/
int RobotNeutralisateur::getCollisionSide(const Particule &target, double x, double y)
{
double left = target.getX() - target.getD() / 2;
double right = target.getX() + target.getD() / 2;
double top = target.getY() + target.getD() / 2;
double bottom = target.getY() - target.getD() / 2;
// Calculate distances to the four corners
double distToTopLeft = getDistance(x, y, left, top);
double distToTopRight = getDistance(x, y, right, top);
double distToBottomLeft = getDistance(x, y, left, bottom);
double distToBottomRight = getDistance(x, y, right, bottom);
// Initialize corner and minDist to top left and distToTopLeft
int corner = 0; // Top Left
double minDist = distToTopLeft;
// Check top right
if (distToTopRight < minDist)
{
corner = 1; // Top Right
minDist = distToTopRight;
}
// Check bottom left
if (distToBottomLeft < minDist)
{
corner = 2; // Bottom Left
minDist = distToBottomLeft;
}
// Check bottom right
if (distToBottomRight < minDist)
{
corner = 3; // Bottom Right
minDist = distToBottomRight;
}
// Return side based on closest corner
return getSide(corner, x, y, left, right, top, bottom);
}
/*
* Calculates the desired angle of the robot's neutralizer
arm to target the given particle.
*
* @param target the particle to target
*
* @return the desired angle in radians
*
* @throws None
*/
double RobotNeutralisateur::desiredAngle0(const Particule &target)
{
// Determine which side of the particle the robot is closest to
int side = getCollisionSide(target, circle.centre.x, circle.centre.y);
// Calculate the desired angle based on the side of collision
double desiredAngle;
switch (side)
{
case 0: // Left
desiredAngle = 0;
break;
case 1: // Bottom
desiredAngle = M_PI / 2;
break;
case 2: // Right
desiredAngle = M_PI;
break;
case 3: // Top
desiredAngle = -M_PI / 2;
break;
case 4: // Top-Left
desiredAngle = atan2(target.getY() - getY(), target.getX() - getX());
break;
default:
// This case should not happen as side will always be between 0 and 7
// However, in case an error occurs, we default to 0
//to prevent any undefined behavior
desiredAngle = 0;
break;
}
return desiredAngle;
}
/*
* Moves the robot to a type 0 particle.
*
* @param target The target type 0 particle.
* @return void
*/
void RobotNeutralisateur::move_to_type0(const Particule& target) {
// Calculate the angle between the robot and the particle's center
double angleToTarget = atan2(target.getY() - getY(), target.getX() - getX());
if ((abs(angleToTarget - getA()) >= epsil_alignement) and
(!collision_carre_cercle(target.getSquare(), getCircle(), true)))
{
// Rotate to face the target
rotateToAngle(angleToTarget);
}
else if (!collision_carre_cercle(target.getSquare(), getCircle(), true))
{
// move_to_type0 towards the target until collision
moveTowardsTarget(target.get_position());
}
else if (collision_carre_cercle(target.getSquare(), getCircle(), true))
{
// Align perpendicularly with the particle's side
rotateToAngle(desiredAngle0(target));
}
}
double RobotNeutralisateur::calculateTimeToTarget(const Particule &target)
{
// Calculate the angle between the robot and the particle's center
double desiredAngle = desiredAngle0(target);
// Distance between the robot and the target
double distanceToTarget = sqrt(pow(target.getX() - getX(), 2)
+ pow(target.getY() - getY(), 2));
// Calculate the time for each state
double timeToMoveToTarget = distanceToTarget / vtran_max;
double timeToAlignWithTarget = abs(atan2(sin(desiredAngle - getA()),
cos(desiredAngle - getA()))) / vrot_max;
// The total time is the sum of the times for each state
return timeToMoveToTarget + timeToAlignWithTarget;
}
int RobotSpatial::findNearestUnassignedRobot(const Particule &p,
const vector<bool> &robotAssigned)
{
double minTime = 1e30; // Large value for initialization
vector<RobotNeutralisateur> NeutraliseurTemp = neutralisateurs;
int index = -1;
for (int i = 0; i < getNbNs_initial(); ++i)
{
if (robotAssigned[i])
{
continue;
}
if (NeutraliseurTemp[i].getPanne()) {
continue;
}
if (NeutraliseurTemp[i].isColliding(NeutraliseurTemp))
{
continue;
}
double delta_t = NeutraliseurTemp[i].calculateTimeToTarget(p);
if (delta_t < minTime && !NeutraliseurTemp[i].isColliding(NeutraliseurTemp))
{
minTime = delta_t;
index = i;
}
}
return index;
}
/*
* Finds the index of the colliding particle, if any.
*
* @param robot The RobotNeutralisateur instance to check for collision.
* @param particles The vector of Particule instances to check for collision.
* @return The index of the colliding particle, or -1 if no collision.
*/
int RobotSpatial::findCollidingParticle(const RobotNeutralisateur &robot,
const std::vector<Particule> &particles)
{
for (size_t i = 0; i < particles.size(); ++i)
{
if (collision_carre_cercle(particles[i].getSquare(), robot.getCircle(), true))
{
return static_cast<int>(i);
}
}
return -1;
}
/*
* Manage movements of robots and particles.
*
* @param vector_particules vector of particules
*
* @return void
*
* @throws None
*/
void RobotSpatial::manage_movements(vector<Particule> &vector_particules)
{
std::vector<bool> robotAssigned(getNbNs_initial(), false);
std::vector<bool> particleAssigned(vector_particules.size(), false);
bool allAssigned = false;
while (!allAssigned)
{
allAssigned = true; // Assume all are assigned until proven otherwise
for (size_t i = 0; i < vector_particules.size(); i++)
{
if (particleAssigned[i])
{
continue; // Skip this particle if it's already assigned to a robot
}
int index = findNearestUnassignedRobot(vector_particules[i],
robotAssigned);
if (index != -1)
{
int collidingParticuleIndex =
findCollidingParticle(neutralisateurs[index], vector_particules);
if (collidingParticuleIndex != -1)
{
// If colliding, update target to the colliding particle
neutralisateurs[index].move_to_type0(vector_particules
[collidingParticuleIndex]);
particleAssigned[collidingParticuleIndex] = true;
// Mark this particle as assigned
}
else{
// If not colliding, move towards current target as usual
neutralisateurs[index].move_to_type0(vector_particules[i]);
particleAssigned[i] = true; // Mark this particle as assigned
}
robotAssigned[index] = true;
allAssigned = false; // We assigned a robot, so not all were assigned
}
}
}
}
/*
* Chooses a repair robot that has not been assigned yet and is closest to
* the target robot without colliding with it.
*
* @param robot the target robot
* @param reparateurAssigned a boolean vector indicating which repair robots
* have already been assigned
*
* @return the index of the chosen repair robot
*
* @throws none
*/
int RobotSpatial::chooseReparateur(const RobotNeutralisateur &robot,
const vector<bool> &reparateurAssigned)
{
double minTime = 1e30;
vector<RobotReparateur> ReparateurTemp = reparateurs;
int index = -1;
for (int i = 0; i < reparateurs.size(); ++i)
{
if (reparateurAssigned[i])
{
continue;
}
while (!collision_cercles(robot.getCircle(),
ReparateurTemp[i].getCircle(), true))
{
ReparateurTemp[i].move_reparateur(robot);
}
double timetotarget = ReparateurTemp[i].getReachTime();
if (timetotarget < minTime)
{
minTime = timetotarget;
index = i;
}
}
return index;
}
/*
* Move the RobotReparateur towards the RobotNeutralisateur target until collision.
*
* @param target The RobotNeutralisateur towards which the RobotReparateur should move.
*
* @throws None
*/
void RobotReparateur::move_reparateur(const RobotNeutralisateur &target)
{
double dt = delta_t;
// Move the robot until collision
double goal_a = atan2(target.getY() - getY(), target.getX() - getX());
circle.centre.x += cos(goal_a) * vtran_max * dt;
circle.centre.y += sin(goal_a) * vtran_max * dt;
reachtime += dt;
}
/*
* Finds the index of the colliding particle, if any.
*
* @param robot The RobotNeutralisateur instance to check for collision.
* @param particles The vector of Particule instances to check for collision.
* @return The index of the colliding particle, or -1 if no collision.
*/
bool RobotReparateur::repCheckCollision(const vector<RobotReparateur> &reparateurs,
const vector<RobotNeutralisateur> &neutralisateurs)
{
for (const auto &robot : reparateurs)
{
// Skip checking collision with self
if (this == &robot)
continue;
if (collision_cercles(this->getCircle(), robot.getCircle(), true))
{
return true;
}
}
for (const auto &robot : neutralisateurs)
{
// Skip checking collision with self
if (collision_cercles(this->getCircle(), robot.getCircle(), true))
{
return true;
}
}
return false;
}
/*
* Spawn a RobotReparateur if the current update count is divisible by modulo_update
* and there are still available repair robots. The robot is spawned at the location
* of this RobotSpatial instance, provided that it does not collide with any existing
* RobotReparateurs or neutralizers.
*
* @return void
*
* @throws None
*/
void RobotSpatial::spawnReparateur()
{
if (nbUpdate % modulo_update == 0 && nbRr > 0)
{
RobotReparateur reparateurTemp(getX(), getY());
if (nbUpdate != 0 && !(reparateurTemp.repCheckCollision(reparateurs,
neutralisateurs)))
{
reparateurs.push_back(reparateurTemp);
++nbRs_initial;
--nbRr;
}
}
}
/*
* Spawns a neutralizing robot with the same position as the RobotSpatial
* and the default radius if nbUpdate is a multiple of modulo_update and there
* are still robots to be spawned. The robot is added to the list of
* neutralizing robots if it does not collide with other robots and if there
* are enough particles in the environment. The number of initial neutralizing
* robots and the number of robots to be spawned are updated accordingly.
*
* @param vector_particules a vector of Particule objects representing the
* environment
*
* @throws None
*/
void RobotSpatial::spawnNeutralisateur(vector<Particule> vector_particules)
{
if (nbUpdate % modulo_update == 0 && nbNr > 0)
{
// Create a new neutralizing robot with the same position
//as the RobotSpatial and the default radius
RobotNeutralisateur robotTemp(getX(), getY(), 0, 0, 0, 0);
if (nbUpdate != 0 && vector_particules.size() >= nbNs_initial &&
!robotTemp.isColliding(neutralisateurs))
{
// Add the new robot to the list of neutralizing robots
neutralisateurs.push_back(robotTemp);
// Update the number of initial neutralizing robots
++nbNs_initial;
--nbNr;
}
}
}
/*
* Updates the state of the RobotSpatial object by moving neutralizer
robots towards the dock and removing them
* from the list of active neutralizers once they reach the dock.
*
* @param particulesTemp a vector of Particule objects representing
* the current state of the system
*
* @throws None
*/
void RobotSpatial::neutralisateurReturnDock(vector<Particule> particulesTemp)
{
Particule particuleTemp(getX(), getY(), 0);
vector<bool> backToDock(neutralisateurs.size(), false);
cercle Station = {get_position(), getR() - 2};
for (size_t i = 0; i < neutralisateurs.size(); ++i)
{
if (!backToDock[i])
{
if (neutralisateurs[i].getPanne())
{
continue;
}
if (neutralisateurs[i].isColliding(neutralisateurs))
{
continue;
}
if (!(neutralisateurs[i].getPanne()))
{
neutralisateurs[i].move_to_type0(particuleTemp);
}
if (collision_cercles(neutralisateurs[i].getCircle(), Station, true))
{
neutralisateurs.erase(neutralisateurs.begin() + i);
++nbNr;
--nbNs_initial;
backToDock[i] = true;
}
}
}
}
/*
* Returns reparator robot to the dock.
*
* @param None
*
* @return None
*
* @throws None
*/
void RobotSpatial::reparateurReturnDock()
{
RobotNeutralisateur robotTemp(getX(), getY(), 0, 0, 0, 0);
vector<bool> backToDock(reparateurs.size(), false);
bool collision = false;
cercle Station = {get_position(), getR() - 2};
for (size_t i = 0; i < reparateurs.size(); ++i)
{
if (!backToDock[i]){
for (size_t j = 0; j < neutralisateurs.size(); ++j)
{
if (collision_cercles(reparateurs[i].getCircle(),
neutralisateurs[j].getCircle(), true))
{
collision = true;
}
if (!collision)
{
reparateurs[i].move_reparateur(robotTemp);
}
}
if (collision_cercles(reparateurs[i].getCircle(), Station, false))
{
reparateurs.erase(reparateurs.begin() + i);
++nbRr;
--nbRs_initial;
backToDock[i] = true;
}
}
}
}
/*
* Manages the redistribution of repairers to neutralizers that need repair.
*
* @return void
*
* @throws None
*/
void RobotSpatial::manage_reparateurs()
{
std::vector<bool> reparateurAssigned(reparateurs.size(), false);
bool allAssigned = false;
while (!allAssigned)
{
allAssigned = true;
for (size_t i = 0; i < neutralisateurs.size(); i++)
{
if (!neutralisateurs[i].getPanne())
{
continue;
}
int index = chooseReparateur(neutralisateurs[i], reparateurAssigned);
if (index != -1)
{
if (collision_cercles(neutralisateurs[i].getCircle(),
reparateurs[index].getCircle(), true))
{
neutralisateurs[i].setPanne(false);
}
else if (!reparateurs[index].repCheckCollision(reparateurs,
neutralisateurs))
{
reparateurs[index].move_reparateur(neutralisateurs[i]);
}
reparateurAssigned[index] = true;
allAssigned = false;
}
}
}
}
/*
* Neutralize a specific malfunctioning robot by setting its "panne" value to true.
*
* @param index the index of the robot to be neutralized.
*
* @throws None.
*/
void RobotSpatial::neutraliseurPanne(int index)
{
neutralisateurs[index].setPanne(true);
neutralisateurs[index].setK_update_panne(nbUpdate);
}
/*
* Determines if the given particle is within the risk zone.
*
* @param p the particle to check
*
* @return true if the robot is within the risk zone, false otherwise
*
* @throws None
*/
bool RobotNeutralisateur::is_within_risk_zone(const Particule &p)
{
// Calculate the bounds of the risk zone around the particle
double particle_d = p.getD();
double risk_d = particle_d * risk_factor;
Particule p_risk = Particule(p.getX(), p.getY(), risk_d);
// Check if the robot is within the risk zone
if (collision_carre_cercle(p_risk.getSquare(), getCircle(), true))
{
return true;
}
else
{
return false;
}
}
/* END */
/*
// MOUVEMENT TYPE 1
S2d RobotNeutralisateur::point_outide_riskZone(const Particule &p)
{
S2d target;
// Calculate the bounds of the risk zone around the particle
double demi_d = p.getD() / 2;
double left = p.getX() - risk_factor * demi_d - getR();
double right = p.getX() + risk_factor * demi_d + getR();
double top = p.getY() + risk_factor * demi_d + getR();
double bottom = p.getY() - risk_factor * demi_d - getR();
double p_left = p.getX() - demi_d;
double p_right = p.getX() + demi_d;
double p_top = p.getY() + demi_d;
double p_bottom = p.getY() - demi_d;
// Get the current position of the robot
S2d pos = get_position();
// Scenario 1 - 4: Robot is in top, right, bottom, or left region
if (pos.y > top && pos.x >= p_left && pos.x <= p_right)
{ // Top
target.x = pos.x;
target.y = top;
}
else if (pos.x > right && pos.y <= p_top && pos.y >= p_bottom)
{ // Right
target.x = right;
target.y = pos.y;
}
else if (pos.y < bottom && pos.x >= p_left && pos.x <= p_right)
{ // Bottom
target.x = pos.x;
target.y = bottom;
}
else if (pos.x < left && pos.y <= p_top && pos.y >= p_bottom)
{ // Left
target.x = left;
target.y = pos.y;
}
// Scenario 5 - 8: Robot is in top-right, bottom-right
, bottom-left, or top-left region
else if (pos.y > p_top && pos.x > p_right)
{ // Top-right
if (pos.y - p_top >= pos.x - p_right)
{ // Top-right top
target.x = p_right;
target.y = top;
}
else
{ // Top-right right
target.x = right;
target.y = p_top;
}
}
else if (pos.y < p_bottom && pos.x > p_right)
{ // Bottom-right
if (p_bottom - pos.y >= pos.x - p_right)
{ // Bottom-right bottom
target.x = p_right;
target.y = bottom;
}
else
{ // Bottom-right right
target.x = right;
target.y = p_bottom;
}
}
else if (pos.y < p_bottom && pos.x < p_left)
{ // Bottom-left
if (p_bottom - pos.y >= p_left - pos.x)
{ // Bottom-left bottom
target.x = p_left;
target.y = bottom;
}
else
{ // Bottom-left left
target.x = left;
target.y = p_bottom;
}
}
else if (pos.y > p_top && pos.x < p_left)
{ // Top-left
if (pos.y - p_top >= p_left - pos.x)
{ // Top-left top
target.x = p_top;
target.y = top;
}
else
{ // Top-left left
target.x = left;
target.y = p_left;
}
}
return target;
}*/
/*
S2d RobotNeutralisateur::nearest_point_outside_risk_zone(const Particule& p,
const S2d& pt) {
// Initialize nearest point
S2d nearest;
// Calculate the bounds of the risk zone around the particle
double demi_d = p.getD()/2;
double left = p.getX() - risk_factor*demi_d - getR();
double right = p.getX() + risk_factor*demi_d + getR();
double top = p.getY() + risk_factor*demi_d + getR();
double bottom = p.getY() - risk_factor*demi_d - getR();
// Check which quadrant of the "cross" the point is in and calculate the
nearest point accordingly
if (pt.x <= p.getX() && pt.y > p.getY()) {
// Top-left quadrant: nearest point is on the top or left side
nearest.x = std::max(pt.x, left);
nearest.y = std::min(pt.y, top);
} else if (pt.x > p.getX() && pt.y >= p.getY()) {
// Top-right quadrant: nearest point is on the top or right side
nearest.x = std::min(pt.x, right);
nearest.y = std::min(pt.y, top);
} else if (pt.x <= p.getX() && pt.y < p.getY()) {
// Bottom-left quadrant: nearest point is on the bottom or left side
nearest.x = std::max(pt.x, left);
}
nearest.y = std::max(pt.y, bottom);
} else if(pt.x > p.getX() && pt.y < p.getY()) {
// Bottom-right quadrant: nearest point is on the bottom or right side