forked from Heistergand/fanfields2
-
Notifications
You must be signed in to change notification settings - Fork 3
/
iitc_plugin_fanfields2.user.js
1333 lines (1104 loc) · 50.9 KB
/
iitc_plugin_fanfields2.user.js
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
// ==UserScript==
// @id fanfields@heistergand
// @name IITC plugin: Fan Fields 2
// @author Heistergand
// @category Layer
// @version 2.2.0
// @description Generate a link plan to create the maximum number fields from a group of portals. Enable from the layer chooser.
// @include https://intel.ingress.com/*
// @match https://intel.ingress.com/*
// @grant none
// @downloadURL https://github.com/bryane50/fanfields2/raw/master/iitc_plugin_fanfields2.user.js
// @updateURL https://github.com/bryane50/fanfields2/raw/master/iitc_plugin_fanfields2.meta.js
// ==/UserScript==
/*
Forked from Heistergand, with contributions from Seth10 and bryane50
*/
/*
Version History:
2.2.0
Added Single/Multi Anchor mode.
Incoming control disabled.
2.1.10
Bug fix: Move leaflet related init into setup()
2.1.9.1 (zysfryar)
Fixed blank in header for compatibility with IITC-CE Button.
2.1.9 (bryane50)
Fix for missing constants in leaflet verion 1.6.0.
2.1.8 (bryane50)
Added starting portal advance button to select among the list of
perimeter portals.
2.1.7 (bryane50)
Removed marker and random selection of starting point portal. Replaced
with use of first outer hull portal. This ensures maximum fields will
be generated.
2.1.5 (Seth10)
FIX: Minor syntax issue affecting potentially more strict runtimes
2.1.4 (Seth10)
FIX: Make the clockwise button change its label to "Counterclockwise" when toggled
2.1.3 (Heistergand)
FIX: added id tags to menu button elements, ...just because.
2.1.2
FIX: Minor issues
2.1.1
FIX: changed List export format to display as a table
2.1.0
NEW: Added save to DrawTools functionality
NEW: Added fanfield statistics
FIX: Changed some menu texts
VER: Increased Minor Version due to DrawTools Milestone
2.0.9
NEW: Added the number of outgoing links to the simple list export
2.0.8
NEW: Toggle the direction of the star-links (Inbound/Outbound) and calculate number of SBUL
FIX: Despite crosslinks, respecting the current intel did not handle done links
2.0.7
FIX: Sorting of the portals was not accurate for far distance anchors when the angle was too equal.
NEW: Added option to respect current intel and not crossing lines.
2.0.6
FIX: Plan messed up on multiple polygons.
2.0.5
FIX: fan links abandoned when Marker was outside the polygon
BUG: Issue found where plan messes up when using more than one polygon (fixed in 2.0.6)
2.0.4
NEW: Added Lock/Unlock button to freeze the plan and prevent recalculation on any events.
NEW: Added a simple text export (in a dialog box)
FIX: Several changes to the algorithm
BUG: Issue found where links are closing fields on top of portals that are
successors in the list once you got around the startportal
2.0.3
FIX: Counterclockwise did not work properly
NEW: Save as Bookmarks
2.0.2
NEW: Added Menu
NEW: Added counterclockwise option
FIX: Minor Bugfixes
2.0.1
NEW: Count keys to farm
NEW: Count total fields
NEW: Added labels to portals
FIX: Links were drawn in random order
FIX: Only fields to the center portal were drawn
Issues:
Lock function is not useful for layouts which require lower zoom levels due
to portal filtering. Locking portals at the high zooms which show all portals
(ie: no portal filters) would be more useful but requires a portal lock on a
per portal basis.
Todo:
Calculate distance to walk for the plan (crow / streets)
Calculate the most efficient possible plan based on ways to walk and keys to farm
Export to Arcs
Export to Tasks
Bookmarks saving works, but let it also save into a Bookmarks Folder
Calculate amount of possible rebuilds after flipping the center portal
Click on a link to flip it's direction
*/
function wrapper(plugin_info) {
// ensure plugin framework is there, even if iitc is not yet loaded
if(typeof window.plugin !== 'function') window.plugin = function() {};
// PLUGIN START ////////////////////////////////////////////////////////
// use own namespace for plugin
window.plugin.fanfields = function() {};
var thisplugin = window.plugin.fanfields;
// const values
// zoom level used for projecting points between latLng and pixel coordinates. may affect precision of triangulation
thisplugin.PROJECT_ZOOM = 16;
thisplugin.LABEL_WIDTH = 100;
thisplugin.LABEL_HEIGHT = 49;
// constants no longer present in leaflet 1.6.0
thisplugin.DEG_TO_RAD = Math.PI / 180;
thisplugin.RAD_TO_DEG = 180 / Math.PI;
thisplugin.labelLayers = {};
thisplugin.start = {guid:undefined, point : {}, index : undefined}; //undefined;
thisplugin.locations = [];
thisplugin.fanpoints = [];
thisplugin.sortedFanpoints = [];
thisplugin.sortedFanpoints[0] = [];
thisplugin.sfLinks = [];
thisplugin.hullPoints = [];
thisplugin.numSubFields = 1;
thisplugin.labels = [];
thisplugin.links = [];
thisplugin.linksLayerGroup = null;
thisplugin.fieldsLayerGroup = null;
thisplugin.numbersLayerGroup = null;
thisplugin.saveBookmarks = function() {
// loop thru portals and UN-Select them for bkmrks
var bkmrkData, list;
for (guid of Object.keys(thisplugin.fanpoints)) {
bkmrkData = window.plugin.bookmarks.findByGuid(guid);
if(bkmrkData) {
list = window.plugin.bookmarks.bkmrksObj.portals;
delete list[bkmrkData.id_folder].bkmrk[bkmrkData.id_bookmark];
$('.bkmrk#'+bkmrkData.id_bookmark + '').remove();
window.plugin.bookmarks.saveStorage();
window.plugin.bookmarks.updateStarPortal();
window.runHooks('pluginBkmrksEdit', {"target": "portal", "action": "remove", "folder": bkmrkData.id_folder, "id": bkmrkData.id_bookmark, "guid":guid});
console.log('BOOKMARKS via FANFIELDS: removed portal ('+bkmrkData.id_bookmark+' situated in '+bkmrkData.id_folder+' folder)');
}
};
// loop again: ordered(!) to add them as bookmarks
for (guid of Object.keys(thisplugin.fanpoints)) {
var p = window.portals[guid];
var ll = p.getLatLng();
plugin.bookmarks.addPortalBookmark(guid, ll.lat+','+ll.lng, p.options.data.title);
};
};
// cycle to next starting point on the convex hull list of portals
thisplugin.nextstartPoint = function() {
if (!thisplugin.is_locked) {
// *** startPoint handling is duplicated in updateLayer().
var i = thisplugin.start.index + 1;
if (i >= thisplugin.hullPoints.length) {
i = 0;
}
thisplugin.start.index = i;
thisplugin.start.guid = thisplugin.hullPoints[thisplugin.start.index][0];
thisplugin.start.point = this.fanpoints[thisplugin.start.guid];
// *** full behaviour of updateLayer is not needed here
// could split updateLayer into separate scanning and field creation sections
thisplugin.updateLayer();
}
};
thisplugin.generateTasks = function() {};
thisplugin.reset = function() {};
thisplugin.help = function() {
dialog({
html: '<p>Draw a polygon with Drawtools around a cluster of portals to be fielded. More than one polygon can be used if needed. You must remove existing polygons if they cover portals you do not want to use.'+
'<p>Use the Lock function to prevent the script from recalculating anything. This is useful if you have a large area and want to zoom into details.</p> '+
'<p><i>Move Start</i> changes the start portal to the next boundary portal.'+
'<i>Single Anchor/Multi Anchor</i> toggles between 1 anchor for all portals and multiple anchors to reduce the keys needed at the single anchor, but will increase keys required for other portals.'+
'<p>Export your fanfield portals to bookmarks to extend your possibilites to work with the information.</p>'+
'<p>To create the field plan follow the portal numbers from 1 to the highest where Start is at 0. At each portal link from '+
'the current portal to all linkable lower numbered portals in numeric order.'+
'For example: link 1->Start, link 2->Start, link 2->1, ...</p>'+
// To do : find out if impossible link issue still exists
/*'<p>There are some known issues you should be aware of:<br>This script uses a simple method to check for crosslinks. '+
'It may suggest links that are not possible in dense areas because <i>that last portal</i> is in the way. It means they have flipped order. '+
'If you\'re not sure, link to the center for both portals first and see what you can link. You\'ll get the same amount of fields, but need to farm other keys.</p>'+
*/
'',
id: 'plugin_fanfields_alert_help',
title: 'Fan Fields - Help',
width: 650,
closeOnEscape: true
});
};
// *** To do : Fix this to use multifield plan data
thisplugin.showStatistics = function() {
var text = "";
let nportals = Object.keys(thisplugin.fanpoints).length;
let nlinks = 0;
console.log('labels:', thisplugin.labels);
for (p of Object.keys(thisplugin.labels)) {
nlinks += thisplugin.labels[p].links;
}
text = "<table><tr><td>Total portals:</td><td>" + nportals.toString() + "</td><tr>" +
//"<tr><td>Total links / keys:</td><td>" + thisplugin.donelinks.length.toString() +"</td><tr>" +
"<tr><td>Total links:</td><td>" + nlinks.toString() +"</td><tr>" +
"<tr><td>Fields:</td><td>" + thisplugin.triangles.length.toString() +"</td><tr>" +
//"<tr><td>Build AP (links and fields):</td><td>" + (thisplugin.donelinks.length*313 + thisplugin.triangles.length*1250).toString() +"</td><tr>" +
"</table>";
dialog({
html: text,
id: 'plugin_fanfields_alert_statistics',
title: '== Fan Field Statistics == ',
// width: 500,
closeOnEscape: true
});
}
thisplugin.exportDrawtools = function() {
// todo: currently the link plan added to the DrawTools Layer. We need to replace existing
// drawn links and how about just exporting the json without saving it to the current draw?
// *** this exports geodesicPolyline; drawLink is using polyline
var alatlng, blatlng, layer;
thisplugin.sfLinks.forEach(function(sf){
sf.forEach(function(l) {
alatlng = [window.portals[l.a.guid]._latlng.lat, window.portals[l.a.guid]._latlng.lng];
blatlng = [window.portals[l.b.guid]._latlng.lat, window.portals[l.b.guid]._latlng.lng];
layer = L.geodesicPolyline([alatlng, blatlng], window.plugin.drawTools.lineOptions);
window.plugin.drawTools.drawnItems.addLayer(layer);
// remove save so the drawn items do not persist
//window.plugin.drawTools.save();
});
});
}
thisplugin.exportArcs = function() {
//todo...
}
thisplugin.exportTasks = function() {
//todo...
}
// To do : Add export refresh on updateLayer: On desktop the menu is active and can change the plan
// while text dialog is open, but the dialog does not update.
thisplugin.exportText = function() {
var text = "<table><thead><tr><th style='text-align:right'>Pos </th><th style='text-align:left'>Portal Name</th><th>Keys</th><th>Links</th></tr></thead><tbody>";
for (p of Object.keys(thisplugin.labels)) {
let portal = window.portals[p];
let title = "unknown title";
if (portal !== undefined) {
title = portal.options.data.title;
}
let index = thisplugin.labels[p].index.toString();
let keys = thisplugin.labels[p].keys.toString();
let links = thisplugin.labels[p].links.toString();
text+='<tr><td>' + index + '</td><td>'+ title + '</td><td>' + keys + '</td><td>' + links + '</td></tr>';
};
text+='</tbody></table>';
dialog({
html: text,
id: 'plugin_fanfields_alert_textExport',
title: 'Fan Fields',
width: 500,
closeOnEscape: true
});
};
thisplugin.respectCurrentLinks = false;
thisplugin.toggleRespectCurrentLinks = function() {
thisplugin.respectCurrentLinks = !thisplugin.respectCurrentLinks;
if (thisplugin.respectCurrentLinks) {
$('#plugin_fanfields_respectbtn').html('Respect Intel: ON');
} else {
$('#plugin_fanfields_respectbtn').html('Respect Intel: OFF');
}
thisplugin.delayedUpdateLayer(0.2);
};
thisplugin.is_locked = false;
thisplugin.lock = function() {
thisplugin.is_locked = !thisplugin.is_locked;
if (thisplugin.is_locked) {
$('#plugin_fanfields_lockbtn').html('Locked'); // 🔒
} else {
$('#plugin_fanfields_lockbtn').html('Unlocked'); // 🔓
}
};
thisplugin.is_clockwise = true;
thisplugin.toggleclockwise = function() {
thisplugin.is_clockwise = !thisplugin.is_clockwise;
var clockwiseSymbol="", clockwiseWord="";
if (thisplugin.is_clockwise)
//clockwiseSymbol = "↻", clockwiseWord = "Clockwise";
clockwiseSymbol = "↻", clockwiseWord = "CW";
else
//clockwiseSymbol = "↺", clockwiseWord = "Counterclockwise";
clockwiseSymbol = "↺", clockwiseWord = "CCW";
$('#plugin_fanfields_clckwsbtn').html(clockwiseWord+': ('+clockwiseSymbol+')');
thisplugin.delayedUpdateLayer(0.2);
};
thisplugin.multiField = false;
thisplugin.toggleMultiField = function() {
thisplugin.multiField = !thisplugin.multiField;
let text = "";
if (thisplugin.multiField) {
text = "Multi Anchor";
}
else {
text = "Single Anchor";
}
$('#plugin_fanfields_mfbtn').html(text);
thisplugin.delayedUpdateLayer(0.2);
};
thisplugin.starDirENUM = {CENTRALIZING:-1, RADIATING: 1};
thisplugin.stardirection = thisplugin.starDirENUM.CENTRALIZING;
thisplugin.toggleStarDirection = function() {
thisplugin.stardirection *= -1;
var html = "outbounding";
if (thisplugin.stardirection == thisplugin.starDirENUM.CENTRALIZING) {
html = "inbounding";
}
$('#plugin_fanfields_stardirbtn').html(html);
thisplugin.delayedUpdateLayer(0.2);
};
thisplugin.setupCSS = function() {
$("<style>").prop("type", "text/css").html('.plugin_fanfields_btn {margin-left:2px;margin-right:6px;}' +
'.plugin_fanfields{' +
'color: #FFFFBB;' +
'font-size: 11px;'+
'line-height: 13px;' +
'text-align: left;'+
'vertical-align: bottom;'+
'padding: 2px;' +
'padding-top: 15px;' +
'overflow: hidden;' +
'text-shadow: 1px 1px #000, 1px -1px #000, -1px 1px #000, -1px -1px #000, 0 0 5px #000;' +
'pointer-events: none;' +
'width: ' + thisplugin.LABEL_WIDTH + 'px;'+
'height: '+ thisplugin.LABEL_HEIGHT + 'px;'+
'border-left-color:red; border-left-style: dotted; border-left-width: thin;'+
// 'border-bottom-color:red; border-bottom-style: dashed; border-bottom-width: thin;'+
'}' +
'#plugin_fanfields_toolbox a.highlight { background-color:#ffce00; color:black; font-Weight:bold }'
).appendTo("head");
};
// find triangles formed in links list by testlink
//thisplugin.getThirds = function(list, a,b) {
thisplugin.getThirds = function(list, test) {
var i,k;
var linksOnA = [], linksOnB = [], result = [];
let a = test.a;
let b = test.b;
for (i in list) {
if ((list[i].a.point.equals(a.point) && list[i].b.point.equals(b.point)) || (list[i].a.point.equals(b.point) && list[i].b.point.equals(a.point))) {
// link in list equals tested link
// *** this should never happen, test link cannot match existing link
console.log('getThirds: link matches existing');
continue;
}
// find links in list that include point a
if (list[i].a.point.equals(a.point) || list[i].b.point.equals(a.point))
linksOnA.push(list[i]);
// find links in list that include point b
if (list[i].a.point.equals(b.point) || list[i].b.point.equals(b.point))
linksOnB.push(list[i]);
}
for (i in linksOnA) {
for (k in linksOnB) {
if (linksOnA[i].a.point.equals(linksOnB[k].a.point) || linksOnA[i].a.point.equals(linksOnB[k].b.point) )
result.push(linksOnA[i].a);
if (linksOnA[i].b.point.equals(linksOnB[k].a.point) || linksOnA[i].b.point.equals(linksOnB[k].b.point))
result.push(linksOnA[i].b);
}
}
return result;
};
thisplugin.linkExists = function(list, link) {
var i, result = false;
for (i in list) {
if (thisplugin.linksEqual(list[i],link)) {
result = true;
break;
}
}
return result;
};
thisplugin.linksEqual = function(link1,link2) {
var Aa, Ab, Ba, Bb;
//Aa = link1.a.equals(link2.a);
//Ab = link1.a.equals(link2.b);
//Ba = link1.b.equals(link2.a);
//Bb = link1.b.equals(link2.b);
Aa = link1.a.point.equals(link2.a);
Ab = link1.a.point.equals(link2.b);
Ba = link1.b.point.equals(link2.a);
Bb = link1.b.point.equals(link2.b);
if ((Aa || Ab) && (Ba || Bb)) {
return true;
}
};
thisplugin.intersects = function(link1, link2) {
/* Todo:
Change vars to meet original links
dGuid,dLatE6,dLngE6,oGuid,oLatE6,oLngE6
*/
var x1, y1, x2, y2, x3, y3, x4, y4;
//x1 = link1.a.x;
//y1 = link1.a.y;
//x2 = link1.b.x;
//y2 = link1.b.y;
//x3 = link2.a.x;
//y3 = link2.a.y;
//x4 = link2.b.x;
//y4 = link2.b.y;
x1 = link1.a.point.x;
y1 = link1.a.point.y;
x2 = link1.b.point.x;
y2 = link1.b.point.y;
x3 = link2.a.point.x;
y3 = link2.a.point.y;
x4 = link2.b.point.x;
y4 = link2.b.point.y;
var Aa, Ab, Ba, Bb;
//console.log('intersect link1.a:', link1.a);
//Aa = link1.a.equals(link2.a);
//Ab = link1.a.equals(link2.b);
//Ba = link1.b.equals(link2.a);
//Bb = link1.b.equals(link2.b);
Aa = link1.a.point.equals(link2.a.point);
Ab = link1.a.point.equals(link2.b.point);
Ba = link1.b.point.equals(link2.a.point);
Bb = link1.b.point.equals(link2.b.point);
if ( Aa || Ab || Ba || Bb) {
// intersection is at start, that's ok.
return false;
}
function sameSign(n1, n2) {
if (n1*n2 > 0) {
return true;
} else {
return false;
}
}
// debugger
var a1, a2, b1, b2, c1, c2;
var r1, r2 , r3, r4;
var denom, offset, num;
// Compute a1, b1, c1, where link joining points 1 and 2
// is "a1 x + b1 y + c1 = 0".
a1 = y2 - y1;
b1 = x1 - x2;
c1 = (x2 * y1) - (x1 * y2);
// Compute r3 and r4.
r3 = ((a1 * x3) + (b1 * y3) + c1);
r4 = ((a1 * x4) + (b1 * y4) + c1);
// Check signs of r3 and r4. If both point 3 and point 4 lie on
// same side of link 1, the link segments do not intersect.
if ((r3 !== 0) && (r4 !== 0) && (sameSign(r3, r4))){
return 0; //return that they do not intersect
}
// Compute a2, b2, c2
a2 = y4 - y3;
b2 = x3 - x4;
c2 = (x4 * y3) - (x3 * y4);
// Compute r1 and r2
r1 = (a2 * x1) + (b2 * y1) + c2;
r2 = (a2 * x2) + (b2 * y2) + c2;
// Check signs of r1 and r2. If both point 1 and point 2 lie
// on same side of second link segment, the link segments do
// not intersect.
if ((r1 !== 0) && (r2 !== 0) && (sameSign(r1, r2))){
return 0; //return that they do not intersect
}
//link segments intersect: compute intersection point.
denom = (a1 * b2) - (a2 * b1);
if (denom === 0) {
return 1; //collinear
}
// links_intersect
return 1; //links intersect, return true
};
thisplugin.removeLabel = function(guid) {
var previousLayer = thisplugin.labelLayers[guid];
if(previousLayer) {
thisplugin.numbersLayerGroup.removeLayer(previousLayer);
delete thisplugin.labelLayers[guid];
}
};
thisplugin.addLabel = function(guid, latLng, labelText) {
if (!window.map.hasLayer(thisplugin.numbersLayerGroup)) return;
var previousLayer = thisplugin.labelLayers[guid];
if(previousLayer) {
//Number of Portal may have changed, so we delete the old value.
thisplugin.numbersLayerGroup.removeLayer(previousLayer);
delete thisplugin.labelLayers[guid];
}
var label = L.marker(latLng, {
icon: L.divIcon({
className: 'plugin_fanfields',
iconAnchor: [0 ,0],
iconSize: [thisplugin.LABEL_WIDTH,thisplugin.LABEL_HEIGHT],
html: labelText
}),
guid: guid
});
thisplugin.labelLayers[guid] = label;
label.addTo(thisplugin.numbersLayerGroup);
};
thisplugin.clearAllPortalLabels = function() {
for (var guid in thisplugin.labelLayers) {
thisplugin.removeLabel(guid);
}
};
// angle of line a,b
// adjust for base angle c if present
thisplugin.getAngle = function(a, b, c) {
var angle;
angle = Math.atan2(b.y-a.y, b.x-a.x) * thisplugin.RAD_TO_DEG;
if (c != undefined) {
angle = angle - c;
if (angle < 0) {
angle = angle + 360;
}
}
return angle;
}
// angle between line a,b and line a,c
thisplugin.getAngle2 = function(a, b, c) {
var angle;
var ax, bx, ay, cy;
if (b === c) {
return 0;
}
ax = b.x - a.x;
ay = b.y - a.y;
bx = c.x - a.x;
by = c.y - a.y;
var num = ax*bx + ay*by;
var den = Math.sqrt(ax*ax + ay*ay) * Math.sqrt(bx*bx + by*by);
// to do: to reduce math needed, determine if this can be
// reduced to return(num/den)
// for sorting purposes, conversion to degrees is not needed
// cos(angle) decreases over 0..180, but is monotonic
// so -1*(num/den) should work
//angle = Math.acos(num / den) * thisplugin.RAD_TO_DEG;
angle = Math.acos(num / den);
return angle;
}
// find points in polygon
thisplugin.filterPolygon = function (points, polygon) {
var result = [];
var guid,i,j,ax,ay,bx,by,la,lb,cos,alpha,det;
for (guid in points) {
var asum = 0;
for (i = 0, j = polygon.length-1; i < polygon.length; j = i, ++i) {
ax = polygon[i].x - points[guid].x;
ay = polygon[i].y - points[guid].y;
bx = polygon[j].x - points[guid].x;
by = polygon[j].y - points[guid].y;
la = Math.sqrt(ax*ax + ay*ay);
lb = Math.sqrt(bx*bx + by*by);
if (Math.abs(la) < 0.1 || Math.abs(lb) < 0.1 ) { // the point is a vertex of the polygon
break;
}
cos = (ax*bx+ay*by)/la/lb;
if (cos < -1)
cos = -1;
if (cos > 1)
cos = 1;
alpha = Math.acos(cos);
det = ax*by-ay*bx;
if (Math.abs(det) < 0.1 && Math.abs(alpha - Math.PI) < 0.1) // the point is on a rib of the polygon
break;
if (det >= 0)
asum += alpha;
else
asum -= alpha;
}
if (i == polygon.length && Math.round(asum / Math.PI / 2) % 2 === 0)
continue;
result[guid] = points[guid];
}
return result;
};
thisplugin.n = 0;
thisplugin.triangles = [];
thisplugin.donelinks = [];
thisplugin.sfLinks = [];
thisplugin.updateLayer = function() {
var a,b,c;
var fanlinks = [], donelinks = [], maplinks = [];
var triangles = [];
var directiontest;
var centerOutgoings = 0;
var centerSbul = 0;
var pa,i,pb,k,ll,p;
var guid;
var polygon,intersection;
var fp_index, fp;
thisplugin.locations = [];
thisplugin.fanpoints = [];
thisplugin.links = [];
if (!window.map.hasLayer(thisplugin.linksLayerGroup) &&
!window.map.hasLayer(thisplugin.fieldsLayerGroup) &&
!window.map.hasLayer(thisplugin.numbersLayerGroup))
return;
thisplugin.linksLayerGroup.clearLayers();
thisplugin.fieldsLayerGroup.clearLayers();
thisplugin.numbersLayerGroup.clearLayers();
var ctrl = [$('.leaflet-control-layers-selector + span:contains("Fanfields links")').parent(),
$('.leaflet-control-layers-selector + span:contains("Fanfields fields")').parent(),
$('.leaflet-control-layers-selector + span:contains("Fanfields numbers")').parent()];
function drawLabel(guid, label) {
let p = thisplugin.fanpoints[guid];
let labelText = "";
if (label.index === 0) {
labelText = "Start<br>";
}
else {
labelText = label.index.toString() + "<br>";
}
labelText += "Keys: " + label.keys.toString() + "<br>";
labelText += "Out: " + label.links.toString();
let latlng = map.unproject(p, thisplugin.PROJECT_ZOOM);
thisplugin.addLabel(guid, latlng, labelText);
}
function drawLink(a, b, style) {
var alatlng = map.unproject(a, thisplugin.PROJECT_ZOOM);
var blatlng = map.unproject(b, thisplugin.PROJECT_ZOOM);
var poly = L.polyline([alatlng, blatlng], style);
poly.addTo(thisplugin.linksLayerGroup);
}
function drawField(a, b, c, style) {
var alatlng = map.unproject(a.point, thisplugin.PROJECT_ZOOM);
var blatlng = map.unproject(b.point, thisplugin.PROJECT_ZOOM);
var clatlng = map.unproject(c.point, thisplugin.PROJECT_ZOOM);
var poly = L.polygon([alatlng, blatlng, clatlng], style);
poly.addTo(thisplugin.fieldsLayerGroup);
}
// Get portal locations
$.each(window.portals, function(guid, portal) {
var ll = portal.getLatLng();
var p = map.project(ll, thisplugin.PROJECT_ZOOM);
thisplugin.locations[guid] = p;
});
thisplugin.intelLinks = {};
$.each(window.links, function(guid, link) {
var lls = link.getLatLngs();
var line = {a: {}, b: {} };
var a = lls[0], b = lls[1];
line.a = map.project(a, thisplugin.PROJECT_ZOOM);
line.b = map.project(b, thisplugin.PROJECT_ZOOM);
thisplugin.intelLinks[guid] = line;
});
// filter layers into array that only contains GeodesicPolygon
function findFanpoints(dtLayers,locations,filter) {
var polygon, dtLayer, result = [];
var i, filtered;
var fanLayer;
for( dtLayer in dtLayers) {
fanLayer = dtLayers[dtLayer];
if (!(fanLayer instanceof L.GeodesicPolygon)) {
continue;
}
ll = fanLayer.getLatLngs();
polygon = [];
for ( k = 0; k < ll.length; ++k) {
p = map.project(ll[k], thisplugin.PROJECT_ZOOM);
polygon.push(p);
}
filtered = filter(locations, polygon);
for (i in filtered) {
result[i] = filtered[i];
}
}
return result;
}
this.fanpoints = findFanpoints(plugin.drawTools.drawnItems._layers,
this.locations,
this.filterPolygon);
var npoints = Object.keys(this.fanpoints).length;
// no fields if npoints < 3
if (npoints < 3) {
//console.log('< 3 points found');
return;
}
// Find convex hull from fanpoints list of points
// Returns array of {guid: , point:{x:, y:}}
function convexHull(points) {
function cross(a, b, o) {
return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x)
}
// convert to array of [guid, point]
var pa = Object.entries(points).map(p => [p[0], p[1]]);
// sort by x then y if x the same
pa.sort(function(a, b) {
return a[1].x == b[1].x ? a[1].y - b[1].y : a[1].x - b[1].x;
});
var lower = [];
var i;
for (i = 0; i < pa.length; i++) {
while (lower.length >= 2 && cross(lower[lower.length - 2].point, lower[lower.length - 1].point, pa[i][1]) <= 0) {
lower.pop();
}
lower.push({guid:pa[i][0], point:pa[i][1]});
}
var upper = [];
for (i = pa.length - 1; i >= 0; i--) {
while (upper.length >= 2 && cross(upper[upper.length - 2].point, upper[upper.length - 1].point, pa[i][1]) <= 0) {
upper.pop();
}
upper.push({guid:pa[i][0], point:pa[i][1]});
}
upper.pop();
lower.pop();
return lower.concat(upper);
};
thisplugin.hullPoints = convexHull(this.fanpoints);
console.log('Found perimeter points :', thisplugin.hullPoints.length);
//console.log('hull points = ', thisplugin.hullPoints);
// Must have >= 3 hull points to proceed
if (thisplugin.hullPoints.length < 3)
return;
// Use currently selected index in outer hull as starting point
// *** Move this to start set/update handler
{
//console.log('update layer start=', thisplugin.start);
var index;
if (thisplugin.start.index == undefined) {
index = 0;
} else {
index = thisplugin.start.index;
}
if (index >= thisplugin.hullPoints.length) {
index = 0;
}
//console.log("next start index = ", index);
var guid = thisplugin.hullPoints[index].guid;
var point = thisplugin.fanpoints[guid];
thisplugin.start = {guid : guid, point : point, index : index};
//console.log('next start =', thisplugin.start);
}
// triangulate outer hull
// begin at start, zigzag across hullPoints
// *** move this to triangulate function
if (thisplugin.multiField) {
thisplugin.numSubFields = thisplugin.hullPoints.length - 2;
}
else {
thisplugin.numSubFields = 1;
}
var subfield_range = [...Array(thisplugin.numSubFields).keys()];
// truncate arrays to sub field count, required when new portal data changes the outer hull
thisplugin.sortedFanpoints.length = thisplugin.numSubFields;
thisplugin.sfLinks.length = thisplugin.numSubFields;
// arrays for each subfield
for (i of subfield_range) {
thisplugin.sortedFanpoints[i] = [];
thisplugin.sfLinks[i] = [];
}
var sfIndices = [];
var sfi = thisplugin.start.index;
var pmax = thisplugin.hullPoints.length;
var tri_dir = -1;
// calc last perimeter index
// dir selects between ccw(-1) and cw(1) advance
function sflast(p, dir, max) {
var n = p + dir * 1;
n = (max + n % max) % max;
return n;
}
// sfIndices : list of indices in hullPoints defining subfield boundary
// each list of 3 indices defines 1 subfield
// sfBoundary : list of 3 points which define subfield outer links
var sfBoundary = [];
for (sf of subfield_range) {
var p = thisplugin.hullPoints;
var indices = [];
if (sf === 0) {
indices = [sfi, (sfi+1) % pmax, (sfi+2) % pmax];
}
else {
indices = [sfIndices[sf-1][2], sfIndices[sf-1][0], sflast(sfIndices[sf-1][0], tri_dir, pmax)];
tri_dir *= -1;
}
sfIndices.push(indices);
sfBoundary.push([p[indices[0]], p[indices[1]], p[indices[2]]]);
}
// filter selected portals into sub fields
var sfFanpoints = [];
if (thisplugin.multiField) {
for (poly of sfBoundary) {
var poly_points = poly.map(function(p) {return p.point});
var sf_filtered = thisplugin.filterPolygon(thisplugin.fanpoints, poly_points);
sfFanpoints.push(sf_filtered);
}
} else {
sfFanpoints.push(thisplugin.fanpoints);
}
//console.log('sfFanpoints:', sfFanpoints);
// *** this is for multifield debug; could be merged into regular draw link
// outline subfields in thicker blue lines
/*
for (sf of sfBoundary) {
var points = sf.map(function(p) {return p.point});
for (i=0; i < 3; i++) {
drawLink(points[i], points[(i+1)%3], {
color: '#0000FF',
opacity: 1,
weight: 4,
clickable: false,
smoothFactor: 10,
//dashArray: [10, 5, 5, 5, 5, 5, 5, 5, "100%" ],
});
}
}
*/
// each sub field adds an array of points to sortedFanpoints
for (mfIdx = 0; mfIdx < thisplugin.numSubFields; mfIdx++) {
// base line of each subfield is first 2 boundary points
let base0 = sfBoundary[mfIdx][0].point;
let base1 = sfBoundary[mfIdx][1].point;
// create sortedFanpoints from all selected portals
thisplugin.sortedFanpoints[mfIdx] = [];
thisplugin.sfLinks[mfIdx] = [];
for (guid in sfFanpoints[mfIdx]) {
fp = sfFanpoints[mfIdx][guid];
let fp_angle;
let is_start;
let is_outer;
is_start = guid == sfBoundary[mfIdx][0].guid;
is_outer = (is_start || (guid == sfBoundary[mfIdx][1].guid) || (guid == sfBoundary[mfIdx][2].guid));
// force subfield anchor to start of sorted portals by setting its angle to -1
// (inner angles of triangles are 0..180 degrees)
if (is_start) {
fp_angle = -1;
} else {