-
Notifications
You must be signed in to change notification settings - Fork 1
/
newray.cpp
1072 lines (855 loc) · 40.4 KB
/
newray.cpp
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 <vector>
#include "math.h"
#include "map.h"
#include "newray.h"
# define PI 3.14159265358979323846 /* pi */
NewRay::NewRay()
{
}
NewRay::~NewRay()
{
}
int NewRay::isCandidate(const dummy::Map *map, long i,long j)
{
int candidate = 0;
long r = i;
long s = j;
long minR = r - 1, maxR = r + 1, minS = s -1, maxS = s + 1;
if(minR < 0) minR = 0;
if(minS < 0) minS = 0;
if(maxR > map->getPathPlanningNumRows()) maxR = map->getPathPlanningNumRows();
if(maxS > map->getPathPlanningNumCols()) maxS = map->getPathPlanningNumCols();
for(r = minR; r <= maxR; ++r)
{
for(s = minS; s <= maxS; ++s)
{
if (map->getPathPlanningGridValue(r, s) == 0) candidate = 1;
}
}
return candidate;
}
int NewRay::isCandidate2(const dummy::Map *map, long i, long j)
{
int candidate = 0;
long r = i;
long s = j;
long minR = r - 1, maxR = r + 1, minS = s -1, maxS = s + 1;
if(minR < 0) minR = 0;
if(minS < 0) minS = 0;
if(maxR > map->getPathPlanningNumRows()) maxR = map->getPathPlanningNumRows();
if(maxS > map->getPathPlanningNumCols()) maxS = map->getPathPlanningNumCols();
for(r = minR; r <= maxR; ++r)
{
for(s = minS; s <= maxS; ++s)
{
for(int rg = r*gridToPathGridScale; rg < r*gridToPathGridScale + gridToPathGridScale; ++rg)
{
for(int sg = s*gridToPathGridScale; sg < s*gridToPathGridScale + gridToPathGridScale; ++sg)
{
if (map->getGridValue(rg, sg) == 0) candidate = 1;
}
}
}
}
return candidate;
}
void NewRay::findCandidatePositions(dummy::Map *map, long posX, long posY, int orientation, double FOV, int range)
{
NewRay::numGridRows = map->getNumGridRows();
NewRay::numPathPlanningGridCols = map->getPathPlanningNumCols();
NewRay::numPathPlanningGridRows = map->getPathPlanningNumRows();
NewRay::gridToPathGridScale = map->getGridToPathGridScale();
//set the correct FOV orientation
double startingPhi = orientation*PI/180 - FOV/2;
double endingPhi = orientation*PI/180 + FOV/2;
int add2pi = 0;
if(startingPhi <= 0)
{
add2pi = 1;
startingPhi = 2*PI + startingPhi;
endingPhi = 2*PI + endingPhi;
}
if(endingPhi > 2*PI) add2pi = 1;
//std::cout << std::endl << "StartingPhi: " << startingPhi << " EndingPhi: " << endingPhi <<std::endl;
//select the portion of map to be scanned
long minI = posX - range;
long maxI = posX + range;
long minJ = posY - range;
long maxJ = posY + range;
if(minI < 0) minI = 0;
if(minJ < 0) minJ = 0;
if(maxI > map->getPathPlanningNumRows()) maxI = map->getPathPlanningNumRows();
if(maxJ > map->getPathPlanningNumCols()) maxJ = map->getPathPlanningNumCols();
//scan the cells in the selected portion of the map
for(long i = minI; i <= maxI; ++i)
{
for(long j = minJ; j <=maxJ; ++j)
{
double distance = sqrt((i - posX)*(i - posX) + (j - posY)*(j - posY));
//cout << map->getGridValue(i, j) << " : " << distance << " : " <<range << endl;
//if a cell is a candidate one and within range of the robot, generate the ray connecting the robot cell and the free cell
if(map->getPathPlanningGridValue(i, j) == 2 && distance <= range)
{
if(NewRay::isCandidate(map, i, j) == 1)
{
double curX = posX; //starting position of the ray
double curY = posY;
double robotX = posX; //position of the robot
double robotY = posY;
double convertedI = NewRay::convertPointPP(i);
double convertedRX = NewRay::convertPointPP(robotX);
double slope = atan2(NewRay::convertPointPP(i) - NewRay::convertPointPP(robotX), j - robotY); //calculate the slope of the ray with atan2
if(slope <= 0 && add2pi == 0) slope = slope + 2*PI;
if(add2pi == 1) slope = 2*PI + slope; //needed in case of FOV spanning from negative to positive angle values
//std::cout << std::endl << "StartingPhi: " << startingPhi << " EndingPhi: " << endingPhi <<std::endl;
if(slope >= startingPhi && slope <= endingPhi) //only cast the ray if it is inside the FOV of the robot
{
//raycounter++;
//std::cout << "Inside loop, slope: " << slope << " Cell: " << j << " " << i << std::endl;
int hit = 0; //set to 1 when obstacle is hit by ray or when the cell is reached in order to stop the ray
double u = 0; //current position along the ray
while(hit == 0) //scan the map along the ray until an ostacle is found or the considered cell is reached
{
//convert the position on the ray to cell coordinates to check the grid
curY = robotY + 0.5 + u*cos(slope);
curX = robotX + 0.5 - u*sin(slope);
//not needed, but left anyway
if(curX < 0 || curX > map->getPathPlanningNumRows() || curY < 0 || curY > map->getPathPlanningNumCols()) hit = 1;
if(map->getPathPlanningGridValue((long)curX, (long)curY) == 1)
{
hit = 1; //hit set to 1 if an obstacle is found
//std::cout << "HIT! cell: " << j << " " << i << " Hit point: " << curY << " " << curX << std::endl;
}
if((long)curX == i && (long)curY == j) //if the free cell is reached, save it as edge point and stop the ray.
{
std::pair<long,long> temp = std::make_pair(i, j);
NewRay::edgePoints.push_back(temp);
//std::cout << "Cell scanned: " << (int)curY << " " << (int)curX << std::endl;
hit = 1;
}
u += 0.2; //move forward along the ray
}
}
}
}
}
}
}
void NewRay::findCandidatePositions2(dummy::Map *map, long posX, long posY, int orientation, double FOV, int range)
{
NewRay::numGridRows = map->getNumGridRows();
NewRay::numPathPlanningGridCols = map->getPathPlanningNumCols();
NewRay::numPathPlanningGridRows = map->getPathPlanningNumRows();
NewRay::gridToPathGridScale = map->getGridToPathGridScale();
//set the correct FOV orientation
double startingPhi = orientation*PI/180 - FOV/2;
double endingPhi = orientation*PI/180 + FOV/2;
int add2pi = 0;
if(startingPhi <= 0)
{
add2pi = 1;
startingPhi = 2*PI + startingPhi;
endingPhi = 2*PI + endingPhi;
}
if(endingPhi > 2*PI) add2pi = 1;
//std::cout << std::endl << "StartingPhi: " << startingPhi << " EndingPhi: " << endingPhi <<std::endl;
//select the portion of map to be scanned
long minI = posX - range;
long maxI = posX + range;
long minJ = posY - range;
long maxJ = posY + range;
if(minI < 0) minI = 0;
if(minJ < 0) minJ = 0;
if(maxI > map->getPathPlanningNumRows()) maxI = map->getPathPlanningNumRows();
if(maxJ > map->getPathPlanningNumCols()) maxJ = map->getPathPlanningNumCols();
//scan the cells in the selected portion of the map
for(long i = minI; i <= maxI; ++i)
{
for(long j = minJ; j <=maxJ; ++j)
{
double distance = sqrt((i - posX)*(i - posX) + (j - posY)*(j - posY));
//cout << map->getGridValue(i, j) << " : " << distance << " : " <<range << endl;
//if a cell is a candidate one and within range of the robot, generate the ray connecting the robot cell and the free cell
if(map->getPathPlanningGridValue(i, j) == 2 && distance <= range)
{
if(NewRay::isCandidate2(map, i, j) == 1)
{
double curX = posX; //starting position of the ray
double curY = posY;
double robotX = posX; //position of the robot
double robotY = posY;
double convertedI = NewRay::convertPointPP(i);
double convertedRX = NewRay::convertPointPP(robotX);
double slope = atan2(NewRay::convertPointPP(i) - NewRay::convertPointPP(robotX), j - robotY); //calculate the slope of the ray with atan2
if(slope <= 0 && add2pi == 0) slope = slope + 2*PI;
if(add2pi == 1) slope = 2*PI + slope; //needed in case of FOV spanning from negative to positive angle values
//std::cout << std::endl << "StartingPhi: " << startingPhi << " EndingPhi: " << endingPhi <<std::endl;
if(slope >= startingPhi && slope <= endingPhi) //only cast the ray if it is inside the FOV of the robot
{
//raycounter++;
//std::cout << "Inside loop, slope: " << slope << " Cell: " << j << " " << i << std::endl;
int hit = 0; //set to 1 when obstacle is hit by ray or when the cell is reached in order to stop the ray
double u = 0; //current position along the ray
while(hit == 0) //scan the map along the ray until an ostacle is found or the considered cell is reached
{
//convert the position on the ray to cell coordinates to check the grid
curY = robotY + 0.5 + u*cos(slope);
curX = robotX + 0.5 - u*sin(slope);
//not needed, but left anyway
if(curX < 0 || curX > map->getPathPlanningNumRows() || curY < 0 || curY > map->getPathPlanningNumCols()) hit = 1;
if(map->getPathPlanningGridValue((long)curX, (long)curY) == 1)
{
hit = 1; //hit set to 1 if an obstacle is found
//std::cout << "HIT! cell: " << j << " " << i << " Hit point: " << curY << " " << curX << std::endl;
}
if((long)curX == i && (long)curY == j) //if the free cell is reached, save it as edge point and stop the ray.
{
std::pair<long,long> temp = std::make_pair(i, j);
NewRay::edgePoints.push_back(temp);
//std::cout << "Cell scanned: " << (int)curY << " " << (int)curX << std::endl;
hit = 1;
}
u += 0.2; //move forward along the ray
}
}
}
}
}
}
}
vector< std::pair<long,long> > NewRay::getCandidatePositions()
{
return NewRay::edgePoints;
}
void NewRay::emptyCandidatePositions()
{
NewRay::edgePoints.clear();
}
std::pair<double,double> NewRay::getSensingTime(const dummy::Map *map, long posX,long posY, int orientation, double FOV, int range)
{
NewRay::numGridRows = map->getNumGridRows();
setGridToPathGridScale(map->getGridToPathGridScale());
double minPhi = 0; //slope of the first ray required
double maxPhi = 0; //slope of the last ray required
int phiFound = 0; //set to 1 if at least a cell can be scanned
//set the correct FOV orientation
double startingPhi = orientation*PI/180 - FOV/2;
double endingPhi = orientation*PI/180 + FOV/2;
int add2pi = 0;
if(startingPhi <= 0)
{
add2pi = 1;
startingPhi = 2*PI + startingPhi;
endingPhi = 2*PI + endingPhi;
}
if(endingPhi > 2*PI) add2pi = 1;
//std::cout << std::endl << "StartingPhi: " << startingPhi << " EndingPhi: " << endingPhi <<std::endl;
//select the portion of map to be scanned
long minI = posX*gridToPathGridScale + gridToPathGridScale/2 - range*gridToPathGridScale;
long maxI = posX*gridToPathGridScale + gridToPathGridScale/2 + range*gridToPathGridScale;
long minJ = posY*gridToPathGridScale + gridToPathGridScale/2 - range*gridToPathGridScale;
long maxJ = posY*gridToPathGridScale + gridToPathGridScale/2 + range*gridToPathGridScale;
if(minI < 0) minI = 0;
if(minJ < 0) minJ = 0;
if(maxI > map->getNumGridRows()) maxI = map->getNumGridRows();
if(maxJ > map->getNumGridCols()) maxJ = map->getNumGridCols();
//scan the cells in the selected portion of the map
for(long i = minI; i <= maxI; ++i)
{
for(long j = minJ; j <=maxJ; ++j)
{
double distance = sqrt((i - posX*gridToPathGridScale)*(i - posX*gridToPathGridScale) + (j - posY*gridToPathGridScale)*(j - posY*gridToPathGridScale));
//if a cell is free and within range of the robot, generate the ray connecting the robot cell and the free cell
if(map->getGridValue(i, j) == 0 && distance <= range*gridToPathGridScale)
{
double curX = posX*gridToPathGridScale + gridToPathGridScale/2; //starting position of the ray
double curY = posY*gridToPathGridScale + gridToPathGridScale/2;
double robotX = posX*gridToPathGridScale + gridToPathGridScale/2; //position of the robot
double robotY = posY*gridToPathGridScale + gridToPathGridScale/2;
double convertedI = NewRay::convertPoint(i);
double convertedRX = NewRay::convertPoint(robotX);
double slope = atan2(NewRay::convertPoint(i) - NewRay::convertPoint(robotX), j - robotY); //calculate the slope of the ray with atan2
if(slope <= 0 && add2pi == 0) slope = slope + 2*PI;
if(add2pi == 1) slope = 2*PI + slope; //needed in case of FOV spanning from negative to positive angle values
//std::cout << std::endl << "StartingPhi: " << startingPhi << " EndingPhi: " << endingPhi <<std::endl;
if(slope >= startingPhi && slope <= endingPhi) //only cast the ray if it is inside the FOV of the robot
{
//std::cout << "Inside loop, slope: " << slope << " Cell: " << j << " " << i << std::endl;
int hit = 0; //set to 1 when obstacle is hit by ray or when the cell is reached in order to stop the ray
double u = 0; //current position along the ray
while(hit == 0) //scan the map along the ray until an ostacle is found or the considered cell is reached
{
//convert the position on the ray to cell coordinates to check the grid
curY = robotY + 0.5 + u*cos(slope);
curX = robotX + 0.5 - u*sin(slope);
//not needed, but left anyway
if(curX < 0 || curX > map->getNumGridRows() || curY < 0 || curY > map->getNumGridCols()) hit = 1;
if(map->getGridValue((long)curX, (long)curY) == 1)
{
hit = 1; //hit set to 1 if an obstacle is found
//std::cout << "HIT! cell: " << j << " " << i << " Hit point: " << curY << " " << curX << std::endl;
}
if((long)curX == i && (long)curY == j) //free cell reached, check if the slope is a candidate for first or last ray
{
if(phiFound == 0) //enters if it is the first free cell found
{
phiFound = 1;
minPhi = slope;
maxPhi = slope;
}
if(phiFound == 1)
{
if(slope < minPhi) minPhi = slope;
if(slope > maxPhi) maxPhi = slope;
}
hit = 1;
}
u += 0.2; //move forward along the ray
}
}
}
}
}
double value; //FOV to return
/*
if(phiFound == 0) return -1; //return -1 if no free cells can be scanned
else //return the correct FOV (ALWAYS CENTERED ON THE ORIENTATION)
{
if(minPhi - startingPhi <= endingPhi - maxPhi) value = (endingPhi - startingPhi - 2*(minPhi - startingPhi));
else value = (endingPhi - startingPhi - 2*(endingPhi - maxPhi));
}
//std::cout << "startingPhi " << startingPhi << " endingPhi " << endingPhi << " minPhi " << minPhi << " maxPhi " << maxPhi << std::endl;
return value;
*/
// return sensingTime;
std::pair<double, double> angles;
angles.first = minPhi;
angles.second = maxPhi;
return angles;
}
int NewRay::performSensingOperationEllipse(dummy::Map *map, long posX,
long posY, int posOri,
double firstAngle, double lastAngle,
long a_pcell, long b_pcell,
bool debug) {
/*
dummy::Map map,
long posX robot coordinate x position in planning cells, focal point
long posY robot coordinate y position in planning cells, focal point
int posOri robot orientation in degrees
double firstAngle FOV starting angle in radians
double lastAngle FOV end angle in radians
long a_pcell #ellipse radius on the x-axis in planning cells
long b_pcell #ellipse radius on the y-axis in planning cells
*/
if (debug) {
printf("..................................................................."
". \n");
printf("Input data: \n");
printf("\t - Robot pose %lu, %lu (pCell) heading (%d) (degs) \n", posX,
posY, posOri);
printf("\t - FoV: %3.3f and %3.3f (rads) \n", firstAngle, lastAngle);
printf("\t - Mayor radius a: %lu (pCell) \n", a_pcell);
printf("\t - Minor radius b: %lu (pCell) \n", b_pcell);
}
NewRay::numGridRows = map->getNumGridRows();
setGridToPathGridScale(map->getGridToPathGridScale());
int counter = 0;
// set the correct FOV orientation
double orientation = posOri * PI / 180.0;
double startingPhi = firstAngle;
double endingPhi = lastAngle;
int add2pi = 0;
if (startingPhi <= 0) {
add2pi = 1;
startingPhi = 2 * PI + startingPhi;
endingPhi = 2 * PI + endingPhi;
}
if (endingPhi > 2 * PI)
add2pi = 1;
// data check
if ((a_pcell == 0) || (b_pcell == 0)) {
std::cout << "INVALID ELLIPSE RADIUS!!!" << std::endl;
return 0;
}
if (a_pcell < b_pcell) {
long tmp;
tmp = a_pcell;
a_pcell = b_pcell;
b_pcell = tmp;
std::cout << "minor radius smaller than mayor one!" << std::endl;
}
// get some relevant points of the ellipse in cell units
long c_pcell = sqrt((a_pcell * a_pcell) - (b_pcell * b_pcell));
double a_cell = a_pcell * gridToPathGridScale + gridToPathGridScale / 2;
double b_cell = b_pcell * gridToPathGridScale + gridToPathGridScale / 2;
double c_cell = c_pcell * gridToPathGridScale + gridToPathGridScale / 2;
if (debug) {
printf("\t - Centre to focus dist c: %lu (pCell) \n", c_pcell);
printf("\n");
}
// first focal point of the ellipse: robot pose
double x_f1_cell = posX * gridToPathGridScale + gridToPathGridScale / 2;
double y_f1_cell = posY * gridToPathGridScale + gridToPathGridScale / 2;
double robotX = x_f1_cell;
double robotY = y_f1_cell;
// center of the ellipse: to get ranges
double x_0_cell = x_f1_cell + (c_cell * cos(orientation));
double y_0_cell = y_f1_cell + (c_cell * sin(orientation));
// second focal point of the ellipse: to get distances
double x_f2_cell = x_0_cell + (c_cell * cos(orientation));
double y_f2_cell = y_0_cell + (c_cell * sin(orientation));
if (debug) {
printf("Ellipse params in nav cell scale: \n");
printf("\t - Mayor radius a: %3.1f (nCell) \n", a_cell);
printf("\t - Minor radius b: %3.1f (nCell) \n", b_cell);
printf("\t - Centre to focus dist c: %3.1f (nCell) \n", c_cell);
printf("\n");
printf("\t - Centre %3.1f, %3.1f (nCell)\n", x_0_cell, y_0_cell);
printf("\t - Focal point 1 %3.1f, %3.1f (nCell) (robot pose)\n", x_f1_cell,
y_f1_cell);
printf("\t - Focal point 2 %3.1f, %3.1f (nCell) (robot pose)\n", x_f2_cell,
y_f2_cell);
printf("\n");
}
// select the portion of map to be scanned
// no matter the orientation of the ellipse, a is the mayor radius
long minI = std::floor(x_0_cell - a_cell);
long maxI = std::floor(x_0_cell + a_cell);
long minJ = std::floor(y_0_cell - a_cell);
long maxJ = std::floor(y_0_cell + a_cell);
if (minI < 0)
minI = 0;
if (minJ < 0)
minJ = 0;
if (maxI > map->getNumGridRows())
maxI = map->getNumGridRows();
if (maxJ > map->getNumGridCols())
maxJ = map->getNumGridCols();
if (debug) {
printf("Update bounding box in nav cell scale: \n");
printf("\t - minI,minJ %lu, %lu (nCell) \n", minI, minJ);
printf("\t - maxI,maxJ %lu, %lu (nCell) \n", maxI, maxJ);
printf("\n");
}
// scan the cells in the selected portion of the map
for (long i = minI; i <= maxI; ++i) {
for (long j = minJ; j <= maxJ; ++j) {
// in an ellipse, sum of distance to focal points is constant
// double px = i
// double py =
double d1 = sqrt(pow(i - x_f1_cell, 2) + pow(j - y_f1_cell, 2));
double d2 = sqrt(pow(i - x_f2_cell, 2) + pow(j - y_f2_cell, 2));
bool isInside = (d1 + d2 <= (2 * a_cell));
if (debug) {
printf("Dists from %lu, %lu (nCell) to Focal points \n", i, j);
printf("\t to F1 == (%3.1f) (nCell) \n", d1);
printf("\t to F2 == (%3.1f) (nCell) \n", d2);
if (isInside) {
printf("\t Inside! \n");
} else {
printf("\t Outside! \n");
}
printf("\n");
}
// if a cell is free and within range of the robot, generate the ray
// connecting the robot cell and the free cell
if (map->getGridValue(i, j) == 0 && isInside) {
double curX = robotX; // starting position of the ray
double curY = robotY;
double convertedI = NewRay::convertPoint(i);
double convertedRX = NewRay::convertPoint(robotX);
double slope =
atan2(convertedI - convertedRX,
j - robotY); // calculate the slope of the ray with atan2
if (slope <= 0 && add2pi == 0)
slope = slope + 2 * PI;
if (add2pi == 1)
slope = 2 * PI + slope; // needed in case of FOV spanning from
// negative to positive angle values
// std::cout << std::endl << "StartingPhi: " << startingPhi << "
// EndingPhi: " << endingPhi <<std::endl;
if (slope >= startingPhi &&
slope <= endingPhi) // only cast the ray if it is inside the FOV of
// the robot
{
// raycounter++;
// std::cout << "Inside loop, slope: " << slope << " Cell: " << j <<
// " " << i << std::endl;
int hit = 0; // set to 1 when obstacle is hit by ray or when the cell
// is reached in order to stop the ray
double u = 0; // current position along the ray
while (hit == 0) // scan the map along the ray until an ostacle is
// found or the considered cell is reached
{
// convert the position on the ray to cell coordinates to check the
// grid
curY = robotY + 0.5 + u * cos(slope);
curX = robotX + 0.5 - u * sin(slope);
// not needed, but left anyway
if (curX < 0 || curX > map->getNumGridRows() || curY < 0 ||
curY > map->getNumGridCols())
hit = 1;
if (map->getGridValue((long)curX, (long)curY) == 1) {
hit = 1; // hit set to 1 if an obstacle is found
// std::cout << "HIT! cell: " << j << " " << i << " Hit point: "
// << curY << " " << curX << std::endl;
}
if ((long)curX == i &&
(long)curY == j) // if the free cell is reached, set its value
// to 2 and stop the ray
{
map->setGridValue(2, i, j);
counter++;
// std::cout << "Cell scanned: " << (int)curY << " " << (int)curX
// << std::endl;
hit = 1;
}
u += 0.2; // move forward along the ray
}
}
}
}
}
if (debug) {
printf("..................................................................."
". \n\n\n");
}
return counter;
}
int NewRay::performSensingOperation(dummy::Map *map, long posX, long posY, int orientation, double FOV, int range, double firstAngle, double lastAngle)
{
NewRay::numGridRows = map->getNumGridRows();
setGridToPathGridScale(map->getGridToPathGridScale());
int counter = 0;
//set the correct FOV orientation
double startingPhi = firstAngle; //orientation*PI/180 - FOV/2;
double endingPhi = lastAngle; //orientation*PI/180 + FOV/2;
int add2pi = 0;
if(startingPhi <= 0)
{
add2pi = 1;
startingPhi = 2*PI + startingPhi;
endingPhi = 2*PI + endingPhi;
}
if(endingPhi > 2*PI) add2pi = 1;
//std::cout << std::endl << "StartingPhi: " << startingPhi << " EndingPhi: " << endingPhi <<std::endl;
//select the portion of map to be scanned
long minI = posX*gridToPathGridScale + gridToPathGridScale/2 - range*gridToPathGridScale;
long maxI = posX*gridToPathGridScale + gridToPathGridScale/2 + range*gridToPathGridScale;
long minJ = posY*gridToPathGridScale + gridToPathGridScale/2 - range*gridToPathGridScale;
long maxJ = posY*gridToPathGridScale + gridToPathGridScale/2 + range*gridToPathGridScale;
if(minI < 0) minI = 0;
if(minJ < 0) minJ = 0;
if(maxI > map->getNumGridRows()) maxI = map->getNumGridRows();
if(maxJ > map->getNumGridCols()) maxJ = map->getNumGridCols();
//scan the cells in the selected portion of the map
for(long i = minI; i <= maxI; ++i)
{
for(long j = minJ; j <=maxJ; ++j)
{
double distance = sqrt((i - posX*gridToPathGridScale)*(i - posX*gridToPathGridScale) + (j - posY*gridToPathGridScale)*(j - posY*gridToPathGridScale));
//if a cell is free and within range of the robot, generate the ray connecting the robot cell and the free cell
if(map->getGridValue(i, j) == 0 && distance <= range*gridToPathGridScale)
{
double curX = posX*gridToPathGridScale + gridToPathGridScale/2; //starting position of the ray
double curY = posY*gridToPathGridScale + gridToPathGridScale/2;
double robotX = posX*gridToPathGridScale + gridToPathGridScale/2; //position of the robot
double robotY = posY*gridToPathGridScale + gridToPathGridScale/2;
double convertedI = NewRay::convertPoint(i);
double convertedRX = NewRay::convertPoint(robotX);
double slope = atan2(NewRay::convertPoint(i) - NewRay::convertPoint(robotX), j - robotY); //calculate the slope of the ray with atan2
if(slope <= 0 && add2pi == 0) slope = slope + 2*PI;
if(add2pi == 1) slope = 2*PI + slope; //needed in case of FOV spanning from negative to positive angle values
//std::cout << std::endl << "StartingPhi: " << startingPhi << " EndingPhi: " << endingPhi <<std::endl;
if(slope >= startingPhi && slope <= endingPhi) //only cast the ray if it is inside the FOV of the robot
{
//raycounter++;
//std::cout << "Inside loop, slope: " << slope << " Cell: " << j << " " << i << std::endl;
int hit = 0; //set to 1 when obstacle is hit by ray or when the cell is reached in order to stop the ray
double u = 0; //current position along the ray
while(hit == 0) //scan the map along the ray until an ostacle is found or the considered cell is reached
{
//convert the position on the ray to cell coordinates to check the grid
curY = robotY + 0.5 + u*cos(slope);
curX = robotX + 0.5 - u*sin(slope);
//not needed, but left anyway
if(curX < 0 || curX > map->getNumGridRows() || curY < 0 || curY > map->getNumGridCols()) hit = 1;
if(map->getGridValue((long)curX, (long)curY) == 1)
{
hit = 1; //hit set to 1 if an obstacle is found
//std::cout << "HIT! cell: " << j << " " << i << " Hit point: " << curY << " " << curX << std::endl;
}
if((long)curX == i && (long)curY == j) //if the free cell is reached, set its value to 2 and stop the ray
{
map->setGridValue(2, i, j);
counter++;
//std::cout << "Cell scanned: " << (int)curY << " " << (int)curX << std::endl;
hit = 1;
}
u += 0.2; //move forward along the ray
}
}
}
}
}
return counter;
}
long NewRay::convertPoint(long y)
{
return (NewRay::numGridRows - 1 - y);
}
long NewRay::convertPointPP(long y)
{
return (NewRay::numPathPlanningGridRows - 1 - y);
}
int NewRay::getInformationGain(const dummy::Map *map, long posX, long posY, int orientation, double FOV, int range)
{
//int raycounter = 0;
setGridToPathGridScale(map->getGridToPathGridScale());
int counter = 0; //count number of free cells that can be seen
NewRay::numGridRows = map->getNumGridRows();
//set the correct FOV orientation
double startingPhi = orientation*PI/180 - FOV/2;
double endingPhi = orientation*PI/180 + FOV/2;
int add2pi = 0;
if(startingPhi <= 0)
{
add2pi = 1;
startingPhi = 2*PI + startingPhi;
endingPhi = 2*PI + endingPhi;
}
if(endingPhi > 2*PI) add2pi = 1;
//std::cout << std::endl << "StartingPhi: " << startingPhi << " EndingPhi: " << endingPhi <<std::endl;
//select the portion of map to be scanned
long minI = posX*gridToPathGridScale + gridToPathGridScale/2 - range*gridToPathGridScale;
long maxI = posX*gridToPathGridScale + gridToPathGridScale/2 + range*gridToPathGridScale;
long minJ = posY*gridToPathGridScale + gridToPathGridScale/2 - range*gridToPathGridScale;
long maxJ = posY*gridToPathGridScale + gridToPathGridScale/2 + range*gridToPathGridScale;
if(minI < 0) minI = 0;
if(minJ < 0) minJ = 0;
if(maxI > map->getNumGridRows()) maxI = map->getNumGridRows();
if(maxJ > map->getNumGridCols()) maxJ = map->getNumGridCols();
//scan the cells in the selected portion of the map
for(long i = minI; i <= maxI; ++i)
{
for(long j = minJ; j <=maxJ; ++j)
{
double distance = sqrt((i - posX*gridToPathGridScale)*(i - posX*gridToPathGridScale) + (j - posY*gridToPathGridScale)*(j - posY*gridToPathGridScale));
//if a cell is free and within range of the robot, generate the ray connecting the robot cell and the free cell
if(map->getGridValue(i, j) == 0 && distance <= range*gridToPathGridScale)
{
double curX = posX*gridToPathGridScale + gridToPathGridScale/2; //starting position of the ray
double curY = posY*gridToPathGridScale + gridToPathGridScale/2;
double robotX = posX*gridToPathGridScale + gridToPathGridScale/2; //position of the robot
double robotY = posY*gridToPathGridScale + gridToPathGridScale/2;
double convertedI = NewRay::convertPoint(i);
double convertedRX = NewRay::convertPoint(robotX);
double slope = atan2(NewRay::convertPoint(i) - NewRay::convertPoint(robotX), j - robotY); //calculate the slope of the ray with atan2
if(slope <= 0 && add2pi == 0) slope = slope + 2*PI;
if(add2pi == 1) slope = 2*PI + slope; //needed in case of FOV spanning from negative to positive angle values
//std::cout << std::endl << "StartingPhi: " << startingPhi << " EndingPhi: " << endingPhi <<std::endl;
if(slope >= startingPhi && slope <= endingPhi) //only cast the ray if it is inside the FOV of the robot
{
//raycounter++;
//std::cout << "Inside loop, slope: " << slope << " Cell: " << j << " " << i << std::endl;
int hit = 0; //set to 1 when obstacle is hit by ray or when the cell is reached in order to stop the ray
double u = 0; //current position along the ray
while(hit == 0) //scan the map along the ray until an ostacle is found or the considered cell is reached
{
//convert the position on the ray to cell coordinates to check the grid
curY = robotY + 0.5 + u*cos(slope);
curX = robotX + 0.5 - u*sin(slope);
//not needed, but left anyway
if(curX < 0 || curX > map->getNumGridRows() || curY < 0 || curY > map->getNumGridCols())
{
hit = 1;
//break;
}
if(map->getGridValue((long)curX, (long)curY) == 1)
{
hit = 1; //hit set to 1 if an obstacle is found
//std::cout << "HIT! cell: " << j << " " << i << " Hit point: " << curY << " " << curX << std::endl;
}
if((long)curX == i && (long)curY == j) //if the free cell is reached, increase counter and stop the ray.
{
++counter;
//std::cout << "Cell scanned: " << (int)curY << " " << (int)curX << std::endl;
hit = 1;
}
u += 0.2; //move forward along the ray
}
}
}
}
}
//std::cout << "Number of rays: " << raycounter << std::endl;
return counter; //return the number of free cells
// return this->informationGain;
}
int NewRay::getRFIDGain(const dummy::Map *map, long posX, long posY, int orientation, double FOV, int range)
{
//int raycounter = 0;
setGridToPathGridScale(map->getGridToPathGridScale());
int counter = 0; //count number of free cells that can be seen
NewRay::numGridRows = map->getNumGridRows();
//set the correct FOV orientation
double startingPhi = orientation*PI/180 - FOV/2;
double endingPhi = orientation*PI/180 + FOV/2;
int add2pi = 0;
if(startingPhi <= 0)
{
add2pi = 1;
startingPhi = 2*PI + startingPhi;
endingPhi = 2*PI + endingPhi;
}
if(endingPhi > 2*PI) add2pi = 1;
//std::cout << std::endl << "StartingPhi: " << startingPhi << " EndingPhi: " << endingPhi <<std::endl;
//select the portion of map to be scanned
long minI = posX*gridToPathGridScale + gridToPathGridScale/2 - range*gridToPathGridScale;
long maxI = posX*gridToPathGridScale + gridToPathGridScale/2 + range*gridToPathGridScale;
long minJ = posY*gridToPathGridScale + gridToPathGridScale/2 - range*gridToPathGridScale;
long maxJ = posY*gridToPathGridScale + gridToPathGridScale/2 + range*gridToPathGridScale;
if(minI < 0) minI = 0;
if(minJ < 0) minJ = 0;
if(maxI > map->getNumGridRows()) maxI = map->getNumGridRows();
if(maxJ > map->getNumGridCols()) maxJ = map->getNumGridCols();
//scan the cells in the selected portion of the map
for(long i = minI; i <= maxI; ++i)
{
for(long j = minJ; j <=maxJ; ++j)
{
double distance = sqrt((i - posX*gridToPathGridScale)*(i - posX*gridToPathGridScale) + (j - posY*gridToPathGridScale)*(j - posY*gridToPathGridScale));
//if a cell is free and within range of the robot, generate the ray connecting the robot cell and the free cell
if(map->getRFIDGridValue(i, j) > 0 && distance <= range*gridToPathGridScale)
{
double curX = posX*gridToPathGridScale + gridToPathGridScale/2; //starting position of the ray
double curY = posY*gridToPathGridScale + gridToPathGridScale/2;
double robotX = posX*gridToPathGridScale + gridToPathGridScale/2; //position of the robot
double robotY = posY*gridToPathGridScale + gridToPathGridScale/2;
double convertedI = NewRay::convertPoint(i);
double convertedRX = NewRay::convertPoint(robotX);
double slope = atan2(NewRay::convertPoint(i) - NewRay::convertPoint(robotX), j - robotY); //calculate the slope of the ray with atan2
if(slope <= 0 && add2pi == 0) slope = slope + 2*PI;
if(add2pi == 1) slope = 2*PI + slope; //needed in case of FOV spanning from negative to positive angle values
//std::cout << std::endl << "StartingPhi: " << startingPhi << " EndingPhi: " << endingPhi <<std::endl;
if(slope >= startingPhi && slope <= endingPhi) //only cast the ray if it is inside the FOV of the robot
{
//raycounter++;
//std::cout << "Inside loop, slope: " << slope << " Cell: " << j << " " << i << std::endl;
int hit = 0; //set to 1 when obstacle is hit by ray or when the cell is reached in order to stop the ray
double u = 0; //current position along the ray
while(hit == 0) //scan the map along the ray until an ostacle is found or the considered cell is reached
{
//convert the position on the ray to cell coordinates to check the grid
curY = robotY + 0.5 + u*cos(slope);
curX = robotX + 0.5 - u*sin(slope);
//not needed, but left anyway
if(curX < 0 || curX > map->getNumGridRows() || curY < 0 || curY > map->getNumGridCols())
{
hit = 1;
//break;
}
if(map->getGridValue((long)curX, (long)curY) == 1)
{
hit = 1; //hit set to 1 if an obstacle is found
//std::cout << "HIT! cell: " << j << " " << i << " Hit point: " << curY << " " << curX << std::endl;
}
if((long)curX == i && (long)curY == j) //if the free cell is reached, increase counter and stop the ray.
{
++counter;
//std::cout << "Cell scanned: " << (int)curY << " " << (int)curX << std::endl;
hit = 1;
}
u += 0.2; //move forward along the ray
}
}
}
}
}
//std::cout << "Number of rays: " << raycounter << std::endl;
return counter; //return the number of free cells
// return this->informationGain;
}
int NewRay::setGridToPathGridScale(int value)
{
gridToPathGridScale = value;
}
void NewRay::performRFIDSensingOperation(dummy::Map *map, long posX, long posY, int orientation, double FOV, int range, double power, double firstAngle, double lastAngle)
{
cout << "[" << posX << "," << posY << "]" << endl;
NewRay::numGridRows = map->getNumGridRows();
setGridToPathGridScale(map->getGridToPathGridScale());
int counter = 0;
//set the correct FOV orientation
double startingPhi = firstAngle; //orientation*PI/180 - FOV/2;
double endingPhi = lastAngle; //orientation*PI/180 + FOV/2;
int add2pi = 0;
if(startingPhi <= 0)
{
add2pi = 1;
startingPhi = 2*PI + startingPhi;
endingPhi = 2*PI + endingPhi;
}
if(endingPhi > 2*PI) add2pi = 1;
//std::cout << std::endl << "StartingPhi: " << startingPhi << " EndingPhi: " << endingPhi <<std::endl;
//select the portion of map to be scanned
long minI = posX*gridToPathGridScale + gridToPathGridScale/2 - range*gridToPathGridScale;