-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Grid.cs
1014 lines (827 loc) · 38.9 KB
/
Grid.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MissionPlanner.Utilities;
namespace MissionPlanner.Utilities
{
public class Grid
{
const double rad2deg = (180 / Math.PI);
const double deg2rad = (1.0 / rad2deg);
public struct linelatlng
{
// start of line
public utmpos p1;
// end of line
public utmpos p2;
// used as a base for grid along line (initial setout)
public utmpos basepnt;
}
public enum StartPosition
{
Home = 0,
BottomLeft = 1,
TopLeft = 2,
BottomRight = 3,
TopRight = 4,
Point = 5,
}
public static PointLatLngAlt StartPointLatLngAlt = PointLatLngAlt.Zero;
static void addtomap(linelatlng pos)
{
}
static void addtomap(utmpos pos, string tag)
{
}
public static async Task<List<PointLatLngAlt>> CreateCorridorAsync(List<PointLatLngAlt> polygon, double altitude,
double distance,
double spacing, double angle, double overshoot1, double overshoot2, StartPosition startpos, bool shutter,
float minLaneSeparation, double width, float leadin = 0)
{
return await Task.Run(() => CreateCorridor(polygon, altitude, distance, spacing, angle, overshoot1, overshoot2,
startpos, shutter, minLaneSeparation, width, leadin)).ConfigureAwait(false);
}
public static List<PointLatLngAlt> CreateCorridor(List<PointLatLngAlt> polygon, double altitude, double distance,
double spacing, double angle, double overshoot1, double overshoot2, StartPosition startpos, bool shutter,
float minLaneSeparation, double width, float leadin = 0)
{
if (spacing < 4 && spacing != 0)
spacing = 4;
if (distance < 0.1)
distance = 0.1;
if (polygon.Count == 0)
return new List<PointLatLngAlt>();
List<PointLatLngAlt> ans = new List<PointLatLngAlt>();
// utm zone distance calcs will be done in
int utmzone = polygon[0].GetUTMZone();
// utm position list
List<utmpos> utmpositions = utmpos.ToList(PointLatLngAlt.ToUTM(utmzone, polygon), utmzone);
var lanes = (width / distance);
var start = (int)((lanes / 2) * -1);
var end = start * -1;
for (int lane = start; lane <= end; lane++)
{
// correct side of the line we are on because of list reversal
int multi = 1;
if ((lane - start) % 2 == 1)
multi = -1;
if (startpos != StartPosition.Home)
utmpositions.Reverse();
GenerateOffsetPath(utmpositions, distance * multi * lane, spacing, utmzone)
.ForEach(pnt => { ans.Add(pnt); });
if (startpos == StartPosition.Home)
utmpositions.Reverse();
}
// set the altitude on all points
ans.ForEach(plla => { plla.Alt = altitude; });
return ans;
}
private static List<utmpos> GenerateOffsetPath(List<utmpos> utmpositions, double distance, double spacing, int utmzone)
{
List<utmpos> ans = new List<utmpos>();
utmpos oldpos = utmpos.Zero;
for (int a = 0; a < utmpositions.Count - 2; a++)
{
var prevCenter = utmpositions[a];
var currCenter = utmpositions[a + 1];
var nextCenter = utmpositions[a + 2];
var l1bearing = prevCenter.GetBearing(currCenter);
var l2bearing = currCenter.GetBearing(nextCenter);
var l1prev = newpos(prevCenter, l1bearing + 90, distance);
var l1curr = newpos(currCenter, l1bearing + 90, distance);
var l2curr = newpos(currCenter, l2bearing + 90, distance);
var l2next = newpos(nextCenter, l2bearing + 90, distance);
var l1l2center = FindLineIntersectionExtension(l1prev, l1curr, l2curr, l2next);
//start
if (a == 0)
{
// add start
l1prev.Tag = "S";
ans.Add(l1prev);
// add start/trigger
l1prev.Tag = "SM";
ans.Add(l1prev);
oldpos = l1prev;
}
//spacing
if (spacing > 0)
{
for (int d = (int)((oldpos.GetDistance(l1l2center)) % spacing);
d < (oldpos.GetDistance(l1l2center));
d += (int)spacing)
{
double ax = oldpos.x;
double ay = oldpos.y;
newpos(ref ax, ref ay, l1bearing, d);
var utmpos2 = new utmpos(ax, ay, utmzone) { Tag = "M" };
ans.Add(utmpos2);
}
}
//end of leg
l1l2center.Tag = "S";
ans.Add(l1l2center);
oldpos = l1l2center;
// last leg
if ((a + 3) == utmpositions.Count)
{
if (spacing > 0)
{
for (int d = (int)((l1l2center.GetDistance(l2next)) % spacing);
d < (l1l2center.GetDistance(l2next));
d += (int)spacing)
{
double ax = l1l2center.x;
double ay = l1l2center.y;
newpos(ref ax, ref ay, l2bearing, d);
var utmpos2 = new utmpos(ax, ay, utmzone) { Tag = "M" };
ans.Add(utmpos2);
}
}
l2next.Tag = "ME";
ans.Add(l2next);
l2next.Tag = "E";
ans.Add(l2next);
}
}
return ans;
}
public static async Task<List<PointLatLngAlt>> CreateRotaryAsync(List<PointLatLngAlt> polygon, double altitude, double distance, double spacing, double angle, double overshoot1, double overshoot2, StartPosition startpos, bool shutter, float minLaneSeparation, float leadin, PointLatLngAlt HomeLocation, int clockwise_laps, bool match_spiral_perimeter, int laps)
{
return await Task.Run((() => CreateRotary(polygon, altitude, distance, spacing, angle, overshoot1, overshoot2,
startpos, shutter, minLaneSeparation, leadin, HomeLocation, clockwise_laps, match_spiral_perimeter, laps))).ConfigureAwait(false);
}
public static List<PointLatLngAlt> CreateRotary(List<PointLatLngAlt> polygon, double altitude, double distance, double spacing, double angle, double overshoot1, double overshoot2, StartPosition startpos, bool shutter, float minLaneSeparation, float leadin, PointLatLngAlt HomeLocation, int clockwise_laps, bool match_spiral_perimeter, int laps)
{
spacing = 0;
if (distance < 0.1)
distance = 0.1;
if (polygon.Count == 0)
return new List<PointLatLngAlt>();
List<utmpos> ans = new List<utmpos>();
// utm zone distance calcs will be done in
int utmzone = polygon[0].GetUTMZone();
// utm position list
List<utmpos> utmpositions = utmpos.ToList(PointLatLngAlt.ToUTM(utmzone, polygon), utmzone);
// get mins/maxs of coverage area
Rect area = getPolyMinMax(utmpositions);
var maxlane = laps;// (Centroid(utmpositions).GetDistance(utmpositions[0]) / distance);
// pick start positon based on initial point rectangle
utmpos startposutm;
switch (startpos)
{
default:
case StartPosition.Home:
startposutm = new utmpos(HomeLocation);
break;
case StartPosition.BottomLeft:
startposutm = new utmpos(area.Left, area.Bottom, utmzone);
break;
case StartPosition.BottomRight:
startposutm = new utmpos(area.Right, area.Bottom, utmzone);
break;
case StartPosition.TopLeft:
startposutm = new utmpos(area.Left, area.Top, utmzone);
break;
case StartPosition.TopRight:
startposutm = new utmpos(area.Right, area.Top, utmzone);
break;
case StartPosition.Point:
startposutm = new utmpos(StartPointLatLngAlt);
break;
}
// find the closes polygon point based from our startpos selection
startposutm = findClosestPoint(startposutm, utmpositions);
ClipperLib.ClipperOffset clipperOffset = new ClipperLib.ClipperOffset();
clipperOffset.AddPath(utmpositions.Select(a => { return new ClipperLib.IntPoint(a.x * 1000.0, a.y * 1000.0); }).ToList(), ClipperLib.JoinType.jtMiter, ClipperLib.EndType.etClosedPolygon);
for (int lane = 0; lane < maxlane; lane++)
{
List<utmpos> ans1 = new List<utmpos>();
ClipperLib.PolyTree tree = new ClipperLib.PolyTree();
clipperOffset.Execute(ref tree, (Int64)(distance * 1000.0 * -lane));
if (tree.ChildCount == 0)
break;
if (lane < clockwise_laps || clockwise_laps < 0)
{
ClipperLib.Clipper.ReversePaths(ClipperLib.Clipper.PolyTreeToPaths(tree));
}
foreach (var treeChild in tree.Childs)
{
ans1 = treeChild.Contour.Select(a => new utmpos(a.X / 1000.0, a.Y / 1000.0, utmzone))
.ToList();
// rotate points so the start point is close to the previous
{
startposutm = findClosestPoint(startposutm, ans1);
var startidx = ans1.IndexOf(startposutm);
var firsthalf = ans1.GetRange(startidx, ans1.Count - startidx);
var secondhalf = ans1.GetRange(0, startidx);
ans1 = firsthalf;
ans1.AddRange(secondhalf);
}
if (lane == 0 && clockwise_laps != 1 && match_spiral_perimeter)
{
ans1.Insert(0, ans1.Last<utmpos>()); // start at the last point of the first calculated lap
// to make a closed polygon on the first trip around
}
if (lane == clockwise_laps - 1)
{
ans1.Add(ans1.First<utmpos>()); // revisit the first waypoint on this lap to cleanly exit the CW pattern
}
if (ans.Count() > 2)
{
var start1 = ans[ans.Count() - 1];
var end1 = ans[ans.Count() - 2];
var start2 = ans1[0];
var end2 = ans1[ans1.Count() - 1];
}
ans.AddRange(ans1);
}
}
// set the altitude on all points
return ans.Select(plla => { var a = plla.ToLLA(); a.Alt = altitude; a.Tag = "S"; return a; }).ToList();
}
static utmpos Centroid(List<utmpos> poly)
{
double x = 0;
double y = 0;
double parts = poly.Count;
poly.ForEach(a =>
{
x += (a.x / parts);
y += (a.y / parts);
});
return new utmpos(x, y, poly[0].zone);
}
public static async Task<List<PointLatLngAlt>> CreateGridAsync(List<PointLatLngAlt> polygon, double altitude,
double distance, double spacing, double angle, double overshoot1, double overshoot2, StartPosition startpos,
bool shutter, float minLaneSeparation, float leadin1,float leadin2, PointLatLngAlt HomeLocation, bool useextendedendpoint = true)
{
return await Task.Run((() => CreateGrid(polygon, altitude, distance, spacing, angle, overshoot1, overshoot2,
startpos, shutter, minLaneSeparation, leadin1, leadin2, HomeLocation, useextendedendpoint)))
.ConfigureAwait(false);
}
/// <summary>
///
/// </summary>
/// <param name="polygon">the polygon outside edge</param>
/// <param name="altitude">flight altitude</param>
/// <param name="distance">distance between lines</param>
/// <param name="spacing">space between triggers</param>
/// <param name="angle">angle of the lines</param>
/// <param name="overshoot1"></param>
/// <param name="overshoot2"></param>
/// <param name="startpos"></param>
/// <param name="shutter"></param>
/// <param name="minLaneSeparation"></param>
/// <param name="leadin1"></param>
/// <param name="leadin2"></param>
/// <param name="HomeLocation"></param>
/// <param name="useextendedendpoint">use the leadout point to find the next closes line</param>
/// <returns></returns>
public static List<PointLatLngAlt> CreateGrid(List<PointLatLngAlt> polygon, double altitude, double distance, double spacing, double angle, double overshoot1, double overshoot2, StartPosition startpos, bool shutter, float minLaneSeparation, float leadin1, float leadin2, PointLatLngAlt HomeLocation, bool useextendedendpoint = true)
{
//DoDebug();
if (spacing < 0.1 && spacing != 0)
spacing = 0.1;
if (distance < 0.1)
distance = 0.1;
if (polygon.Count == 0)
return new List<PointLatLngAlt>();
// Make a non round number in case of corner cases
if (minLaneSeparation != 0)
minLaneSeparation += 0.5F;
// Lane Separation in meters
double minLaneSeparationINMeters = minLaneSeparation * distance;
List<PointLatLngAlt> ans = new List<PointLatLngAlt>();
// utm zone distance calcs will be done in
int utmzone = polygon[0].GetUTMZone();
// utm position list
List<utmpos> utmpositions = utmpos.ToList(PointLatLngAlt.ToUTM(utmzone, polygon), utmzone);
// close the loop if its not already
if (utmpositions[0] != utmpositions[utmpositions.Count - 1])
utmpositions.Add(utmpositions[0]); // make a full loop
// get mins/maxs of coverage area
Rect area = getPolyMinMax(utmpositions);
// get initial grid
// used to determine the size of the outer grid area
double diagdist = area.DiagDistance();
// somewhere to store out generated lines
List<linelatlng> grid = new List<linelatlng>();
// number of lines we need
int lines = 0;
// get start point middle
double x = area.MidWidth;
double y = area.MidHeight;
addtomap(new utmpos(x, y, utmzone), "Base");
// get left extent
double xb1 = x;
double yb1 = y;
// to the left
newpos(ref xb1, ref yb1, angle - 90, diagdist / 2 + distance);
// backwards
newpos(ref xb1, ref yb1, angle + 180, diagdist / 2 + distance);
utmpos left = new utmpos(xb1, yb1, utmzone);
addtomap(left, "left");
// get right extent
double xb2 = x;
double yb2 = y;
// to the right
newpos(ref xb2, ref yb2, angle + 90, diagdist / 2 + distance);
// backwards
newpos(ref xb2, ref yb2, angle + 180, diagdist / 2 + distance);
utmpos right = new utmpos(xb2, yb2, utmzone);
addtomap(right, "right");
// set start point to left hand side
x = xb1;
y = yb1;
// draw the outergrid, this is a grid that cover the entire area of the rectangle plus more.
while (lines < ((diagdist + distance * 2) / distance))
{
// copy the start point to generate the end point
double nx = x;
double ny = y;
newpos(ref nx, ref ny, angle, diagdist + distance * 2);
linelatlng line = new linelatlng();
line.p1 = new utmpos(x, y, utmzone);
line.p2 = new utmpos(nx, ny, utmzone);
line.basepnt = new utmpos(x, y, utmzone);
grid.Add(line);
// addtomap(line);
newpos(ref x, ref y, angle + 90, distance);
lines++;
}
// find intersections with our polygon
// store lines that dont have any intersections
List<linelatlng> remove = new List<linelatlng>();
int gridno = grid.Count;
// cycle through our grid
for (int a = 0; a < gridno; a++)
{
double closestdistance = double.MaxValue;
double farestdistance = double.MinValue;
utmpos closestpoint = utmpos.Zero;
utmpos farestpoint = utmpos.Zero;
// somewhere to store our intersections
List<utmpos> matchs = new List<utmpos>();
int b = -1;
int crosses = 0;
utmpos newutmpos = utmpos.Zero;
foreach (utmpos pnt in utmpositions)
{
b++;
if (b == 0)
{
continue;
}
newutmpos = FindLineIntersection(utmpositions[b - 1], utmpositions[b], grid[a].p1, grid[a].p2);
if (!newutmpos.IsZero)
{
crosses++;
matchs.Add(newutmpos);
if (closestdistance > grid[a].p1.GetDistance(newutmpos))
{
closestpoint.y = newutmpos.y;
closestpoint.x = newutmpos.x;
closestpoint.zone = newutmpos.zone;
closestdistance = grid[a].p1.GetDistance(newutmpos);
}
if (farestdistance < grid[a].p1.GetDistance(newutmpos))
{
farestpoint.y = newutmpos.y;
farestpoint.x = newutmpos.x;
farestpoint.zone = newutmpos.zone;
farestdistance = grid[a].p1.GetDistance(newutmpos);
}
}
}
if (crosses == 0) // outside our polygon
{
if (!PointInPolygon(grid[a].p1, utmpositions) && !PointInPolygon(grid[a].p2, utmpositions))
remove.Add(grid[a]);
}
else if (crosses == 1) // bad - shouldnt happen
{
}
else if (crosses == 2) // simple start and finish
{
linelatlng line = grid[a];
line.p1 = closestpoint;
line.p2 = farestpoint;
grid[a] = line;
}
else // multiple intersections
{
linelatlng line = grid[a];
remove.Add(line);
while (matchs.Count > 1)
{
linelatlng newline = new linelatlng();
closestpoint = findClosestPoint(closestpoint, matchs);
newline.p1 = closestpoint;
matchs.Remove(closestpoint);
closestpoint = findClosestPoint(closestpoint, matchs);
newline.p2 = closestpoint;
matchs.Remove(closestpoint);
newline.basepnt = line.basepnt;
grid.Add(newline);
}
}
}
// cleanup and keep only lines that pass though our polygon
foreach (linelatlng line in remove)
{
grid.Remove(line);
}
// debug
foreach (linelatlng line in grid)
{
addtomap(line);
}
if (grid.Count == 0)
return ans;
// pick start positon based on initial point rectangle
utmpos startposutm;
switch (startpos)
{
default:
case StartPosition.Home:
startposutm = new utmpos(HomeLocation);
break;
case StartPosition.BottomLeft:
startposutm = new utmpos(area.Left, area.Bottom, utmzone);
break;
case StartPosition.BottomRight:
startposutm = new utmpos(area.Right, area.Bottom, utmzone);
break;
case StartPosition.TopLeft:
startposutm = new utmpos(area.Left, area.Top, utmzone);
break;
case StartPosition.TopRight:
startposutm = new utmpos(area.Right, area.Top, utmzone);
break;
case StartPosition.Point:
startposutm = new utmpos(StartPointLatLngAlt);
break;
}
// find the closes polygon point based from our startpos selection
startposutm = findClosestPoint(startposutm, utmpositions);
// find closest line point to startpos
linelatlng closest = findClosestLine(startposutm, grid, 0 /*Lane separation does not apply to starting point*/, angle);
utmpos lastpnt;
// get the closes point from the line we picked
if (closest.p1.GetDistance(startposutm) < closest.p2.GetDistance(startposutm))
{
lastpnt = closest.p1;
}
else
{
lastpnt = closest.p2;
}
// S = start
// E = end
// ME = middle end
// SM = start middle
while (grid.Count > 0)
{
// for each line, check which end of the line is the next closest
if (closest.p1.GetDistance(lastpnt) < closest.p2.GetDistance(lastpnt))
{
utmpos newstart = newpos(closest.p1, angle, -leadin1);
newstart.Tag = "S";
addtomap(newstart, "S");
ans.Add(newstart);
if (leadin1 < 0)
{
var p2 = new utmpos(newstart) { Tag = "SM" };
addtomap(p2, "SM");
ans.Add(p2);
}
else
{
closest.p1.Tag = "SM";
addtomap(closest.p1, "SM");
ans.Add(closest.p1);
}
if (spacing > 0)
{
for (double d = (spacing - ((closest.basepnt.GetDistance(closest.p1)) % spacing));
d < (closest.p1.GetDistance(closest.p2));
d += spacing)
{
double ax = closest.p1.x;
double ay = closest.p1.y;
newpos(ref ax, ref ay, angle, d);
var utmpos1 = new utmpos(ax, ay, utmzone) { Tag = "M" };
addtomap(utmpos1, "M");
ans.Add(utmpos1);
}
}
utmpos newend = newpos(closest.p2, angle, overshoot1);
if (overshoot1 < 0)
{
var p2 = new utmpos(newend) { Tag = "ME" };
addtomap(p2, "ME");
ans.Add(p2);
}
else
{
closest.p2.Tag = "ME";
addtomap(closest.p2, "ME");
ans.Add(closest.p2);
}
newend.Tag = "E";
addtomap(newend, "E");
ans.Add(newend);
lastpnt = closest.p2;
grid.Remove(closest);
if (grid.Count == 0)
break;
if(useextendedendpoint)
closest = findClosestLine(newend, grid, minLaneSeparationINMeters, angle);
else
closest = findClosestLine(closest.p2, grid, minLaneSeparationINMeters, angle);
}
else
{
utmpos newstart = newpos(closest.p2, angle, leadin2);
newstart.Tag = "S";
addtomap(newstart, "S");
ans.Add(newstart);
if (leadin2 < 0)
{
var p2 = new utmpos(newstart) { Tag = "SM" };
addtomap(p2, "SM");
ans.Add(p2);
}
else
{
closest.p2.Tag = "SM";
addtomap(closest.p2, "SM");
ans.Add(closest.p2);
}
if (spacing > 0)
{
for (double d = ((closest.basepnt.GetDistance(closest.p2)) % spacing);
d < (closest.p1.GetDistance(closest.p2));
d += spacing)
{
double ax = closest.p2.x;
double ay = closest.p2.y;
newpos(ref ax, ref ay, angle, -d);
var utmpos2 = new utmpos(ax, ay, utmzone) { Tag = "M" };
addtomap(utmpos2, "M");
ans.Add(utmpos2);
}
}
utmpos newend = newpos(closest.p1, angle, -overshoot2);
if (overshoot2 < 0)
{
var p2 = new utmpos(newend) { Tag = "ME" };
addtomap(p2, "ME");
ans.Add(p2);
}
else
{
closest.p1.Tag = "ME";
addtomap(closest.p1, "ME");
ans.Add(closest.p1);
}
newend.Tag = "E";
addtomap(newend, "E");
ans.Add(newend);
lastpnt = closest.p1;
grid.Remove(closest);
if (grid.Count == 0)
break;
if(useextendedendpoint)
closest = findClosestLine(newend, grid, minLaneSeparationINMeters, angle);
else
closest = findClosestLine(closest.p1, grid, minLaneSeparationINMeters, angle);
}
}
// set the altitude on all points
ans.ForEach(plla => { plla.Alt = altitude; });
return ans;
}
static Rect getPolyMinMax(List<utmpos> utmpos)
{
if (utmpos.Count == 0)
return new Rect();
double minx, miny, maxx, maxy;
minx = maxx = utmpos[0].x;
miny = maxy = utmpos[0].y;
foreach (utmpos pnt in utmpos)
{
minx = Math.Min(minx, pnt.x);
maxx = Math.Max(maxx, pnt.x);
miny = Math.Min(miny, pnt.y);
maxy = Math.Max(maxy, pnt.y);
}
return new Rect(minx, maxy, maxx - minx, miny - maxy);
}
// polar to rectangular
static void newpos(ref double x, ref double y, double bearing, double distance)
{
double degN = 90 - bearing;
if (degN < 0)
degN += 360;
x = x + distance * Math.Cos(degN * deg2rad);
y = y + distance * Math.Sin(degN * deg2rad);
}
// polar to rectangular
static utmpos newpos(utmpos input, double bearing, double distance)
{
double degN = 90 - bearing;
if (degN < 0)
degN += 360;
double x = input.x + distance * Math.Cos(degN * deg2rad);
double y = input.y + distance * Math.Sin(degN * deg2rad);
return new utmpos(x, y, input.zone);
}
/// <summary>
/// from http://stackoverflow.com/questions/1119451/how-to-tell-if-a-line-intersects-a-polygon-in-c
/// </summary>
/// <param name="start1"></param>
/// <param name="end1"></param>
/// <param name="start2"></param>
/// <param name="end2"></param>
/// <returns></returns>
public static utmpos FindLineIntersection(utmpos start1, utmpos end1, utmpos start2, utmpos end2)
{
double denom = ((end1.x - start1.x) * (end2.y - start2.y)) - ((end1.y - start1.y) * (end2.x - start2.x));
// AB & CD are parallel
if (denom == 0)
return utmpos.Zero;
double numer = ((start1.y - start2.y) * (end2.x - start2.x)) - ((start1.x - start2.x) * (end2.y - start2.y));
double r = numer / denom;
double numer2 = ((start1.y - start2.y) * (end1.x - start1.x)) - ((start1.x - start2.x) * (end1.y - start1.y));
double s = numer2 / denom;
if ((r < 0 || r > 1) || (s < 0 || s > 1))
return utmpos.Zero;
// Find intersection point
utmpos result = new utmpos();
result.x = start1.x + (r * (end1.x - start1.x));
result.y = start1.y + (r * (end1.y - start1.y));
result.zone = start1.zone;
return result;
}
/// <summary>
/// from http://stackoverflow.com/questions/1119451/how-to-tell-if-a-line-intersects-a-polygon-in-c
/// </summary>
/// <param name="start1"></param>
/// <param name="end1"></param>
/// <param name="start2"></param>
/// <param name="end2"></param>
/// <returns></returns>
public static utmpos FindLineIntersectionExtension(utmpos start1, utmpos end1, utmpos start2, utmpos end2)
{
double denom = ((end1.x - start1.x) * (end2.y - start2.y)) - ((end1.y - start1.y) * (end2.x - start2.x));
// AB & CD are parallel
if (denom == 0)
return utmpos.Zero;
double numer = ((start1.y - start2.y) * (end2.x - start2.x)) -
((start1.x - start2.x) * (end2.y - start2.y));
double r = numer / denom;
double numer2 = ((start1.y - start2.y) * (end1.x - start1.x)) -
((start1.x - start2.x) * (end1.y - start1.y));
double s = numer2 / denom;
if ((r < 0 || r > 1) || (s < 0 || s > 1))
{
// line intersection is outside our lines.
}
// Find intersection point
utmpos result = new utmpos();
result.x = start1.x + (r * (end1.x - start1.x));
result.y = start1.y + (r * (end1.y - start1.y));
result.zone = start1.zone;
return result;
}
static utmpos findClosestPoint(utmpos start, List<utmpos> list)
{
utmpos answer = utmpos.Zero;
double currentbest = double.MaxValue;
foreach (utmpos pnt in list)
{
double dist1 = start.GetDistance(pnt);
if (dist1 < currentbest)
{
answer = pnt;
currentbest = dist1;
}
}
return answer;
}
// Add an angle while normalizing output in the range 0...360
static double AddAngle(double angle, double degrees)
{
angle += degrees;
angle = angle % 360;
while (angle < 0)
{
angle += 360;
}
return angle;
}
static linelatlng findClosestLine(utmpos start, List<linelatlng> list, double minDistance, double angle)
{
if (minDistance == 0)
{
linelatlng answer = list[0];
double shortest = double.MaxValue;
foreach (linelatlng line in list)
{
double ans1 = start.GetDistance(line.p1);
double ans2 = start.GetDistance(line.p2);
utmpos shorterpnt = ans1 < ans2 ? line.p1 : line.p2;
if (shortest > start.GetDistance(shorterpnt))
{
answer = line;
shortest = start.GetDistance(shorterpnt);
}
}
return answer;
}
// By now, just add 5.000 km to our lines so they are long enough to allow intersection
double METERS_TO_EXTEND = 5000;
double perperndicularOrientation = AddAngle(angle, 90);
// Calculation of a perpendicular line to the grid lines containing the "start" point
/*
* --------------------------------------|------------------------------------------
* --------------------------------------|------------------------------------------
* -------------------------------------start---------------------------------------
* --------------------------------------|------------------------------------------
* --------------------------------------|------------------------------------------
* --------------------------------------|------------------------------------------
* --------------------------------------|------------------------------------------
* --------------------------------------|------------------------------------------
*/
utmpos start_perpendicular_line = newpos(start, perperndicularOrientation, -METERS_TO_EXTEND);
utmpos stop_perpendicular_line = newpos(start, perperndicularOrientation, METERS_TO_EXTEND);
// Store one intersection point per grid line
Dictionary<utmpos, linelatlng> intersectedPoints = new Dictionary<utmpos, linelatlng>();
// lets order distances from every intersected point per line with the "start" point
Dictionary<double, utmpos> ordered_min_to_max = new Dictionary<double, utmpos>();
foreach (linelatlng line in list)
{
// Calculate intersection point
utmpos p = FindLineIntersectionExtension(line.p1, line.p2, start_perpendicular_line, stop_perpendicular_line);
// Store it
intersectedPoints[p] = line;
// Calculate distances between interesected point and "start" (i.e. line and start)
double distance_p = start.GetDistance(p);
if (!ordered_min_to_max.ContainsKey(distance_p))
ordered_min_to_max.Add(distance_p, p);
}
// Acquire keys and sort them.
List<double> ordered_keys = ordered_min_to_max.Keys.ToList();
ordered_keys.Sort();
// Lets select a line that is the closest to "start" point but "mindistance" away at least.
// If we have only one line, return that line whatever the minDistance says
double key = double.MaxValue;
int i = 0;
while (key == double.MaxValue && i < ordered_keys.Count)
{
if (ordered_keys[i] >= minDistance)
key = ordered_keys[i];
i++;
}
// If no line is selected (because all of them are closer than minDistance, then get the farest one
if (key == double.MaxValue)
key = ordered_keys[ordered_keys.Count - 1];
var filteredlist = intersectedPoints.Where(a => a.Key.GetDistance(start) >= key);
return findClosestLine(start, filteredlist.Select(a => a.Value).ToList(), 0, angle);
}
static bool PointInPolygon(utmpos p, List<utmpos> poly)
{
utmpos p1, p2;
bool inside = false;
if (poly.Count < 3)
{
return inside;
}
utmpos oldPoint = new utmpos(poly[poly.Count - 1]);
for (int i = 0; i < poly.Count; i++)
{
utmpos newPoint = new utmpos(poly[i]);
if (newPoint.y > oldPoint.y)
{
p1 = oldPoint;
p2 = newPoint;
}
else
{
p1 = newPoint;
p2 = oldPoint;
}