Repository navigation
Expand file tree
/
Copy pathGeometry.cs
More file actions
1747 lines (1585 loc) · 54.1 KB
/
Copy pathGeometry.cs
File metadata and controls
1747 lines (1585 loc) · 54.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using weland.level;
namespace Weland;
public delegate bool PolygonFilter(Polygon p);
public delegate bool ObjectFilter(MapObject o);
public partial class Level
{
public static PolygonFilter Filter = p => true;
public static ObjectFilter ObjectFilter = o => true;
public static bool FilterPoints = false;
public static bool RememberDeletedSides = false;
public static LevelAttributes Attributes = new LevelAttributes();
public static int? VisibleLayer = null;
Dictionary<short, Side> ClockwiseOrphanedSides = [];
Dictionary<short, Side> CounterClockwiseOrphanedSides = [];
HashSet<short> LinesReflectingOrphanedSides = [];
public bool FilterLine(PolygonFilter f, Line line)
{
if (line.ClockwisePolygonOwner == -1)
{
if (line.CounterclockwisePolygonOwner == -1)
{
return true;
}
else
{
return f(Polygons[line.CounterclockwisePolygonOwner]);
}
}
else
{
if (line.CounterclockwisePolygonOwner == -1)
{
return f(Polygons[line.ClockwisePolygonOwner]);
}
else
{
return f(Polygons[line.ClockwisePolygonOwner]) || f(Polygons[line.CounterclockwisePolygonOwner]);
}
}
}
public bool FilterPoint(PolygonFilter f, short index)
{
if (!FilterPoints) return true;
var polygonSet = EndpointPolygons[index];
foreach (var polygon in polygonSet)
{
if (f(polygon))
{
return true;
}
}
var lineSet = EndpointLines[index];
foreach (var line in lineSet)
{
if (FilterLine(f, line))
{
return true;
}
}
return false;
}
public int Distance(Point p0, Point p1)
{
return (int)Math.Round(Math.Sqrt(Math.Pow(p0.X - p1.X, 2) + Math.Pow(p0.Y - p1.Y, 2)));
}
double U(Point p, Line line)
{
var e0 = Endpoints[line.EndpointIndexes[0]];
var e1 = Endpoints[line.EndpointIndexes[1]];
return ((((double)p.X - e0.X) * ((double)e1.X - e0.X)) + (((double)p.Y - e0.Y) * ((double)e1.Y - e0.Y))) / (Math.Pow(e1.X - e0.X, 2) + Math.Pow(e1.Y - e0.Y, 2));
}
public Point ClosestPointOnLine(Point p, Line line)
{
var e0 = Endpoints[line.EndpointIndexes[0]];
var e1 = Endpoints[line.EndpointIndexes[1]];
var u = U(p, line);
Point point;
point.X = (short)Math.Round(e0.X + (u * (e1.X - e0.X)));
point.Y = (short)Math.Round(e0.Y + (u * (e1.Y - e0.Y)));
return point;
}
public int Distance(Point p, Line line)
{
var e0 = Endpoints[line.EndpointIndexes[0]];
var e1 = Endpoints[line.EndpointIndexes[1]];
var u = U(p, line);
if (u > 1.0)
{
return Distance(p, e1);
}
else if (u < 0.0)
{
return Distance(p, e0);
}
else
{
var i = new Point
{
X = (short)Math.Round(e0.X + (u * (e1.X - e0.X))),
Y = (short)Math.Round(e0.Y + (u * (e1.Y - e0.Y)))
};
return Distance(p, i);
}
}
public double Dot(Point p0, Point p1)
{
return (p0.X * p1.X) + (p0.Y * p1.Y);
}
public double Cross(Point p0, Point p1)
{
return (p0.Y * p1.X) - (p0.X * p1.Y);
}
public short GetClosestAnnotation(Point p)
{
var min = int.MaxValue;
short closest_annotation = -1;
for (short i = 0; i < Annotations.Count; ++i)
{
var a = Annotations[i];
var distance = Distance(p, new Point(a.X, a.Y));
if (distance < min)
{
closest_annotation = i;
min = distance;
}
}
return closest_annotation;
}
public short GetClosestPoint(Point p)
{
var min = int.MaxValue;
short closest_point = -1;
for (short i = 0; i < Endpoints.Count; ++i)
{
if (FilterPoint(Filter, i))
{
var distance = Distance(p, Endpoints[i]);
if (distance < min)
{
closest_point = i;
min = distance;
}
}
}
return closest_point;
}
public short GetClosestObject(Point p)
{
var min = int.MaxValue;
short closest_object = -1;
for (short i = 0; i < Objects.Count; ++i)
{
if (Filter(Polygons[Objects[i].PolygonIndex]) && ObjectFilter(Objects[i]))
{
var distance = Distance(p, new Point(Objects[i].X, Objects[i].Y));
if (distance < min)
{
closest_object = i;
min = distance;
}
}
}
return closest_object;
}
public short GetClosestLine(Point p)
{
var min = int.MaxValue;
short closest_line = -1;
for (short i = 0; i < Lines.Count; ++i)
{
if (FilterLine(Filter, Lines[i]))
{
var distance = Distance(p, Lines[i]);
if (distance < min)
{
closest_line = i;
min = distance;
}
}
}
return closest_line;
}
public short GetEnclosingPolygon(Point p)
{
for (var i = 0; i < Polygons.Count; ++i)
{
if (Filter(Polygons[i]))
{
var points = GetPointRingFromLineRing(new List<short>(Polygons[i].LineIndexes).GetRange(0, Polygons[i].VertexCount));
if (PointLoopEnclosesPoint(p, points))
{
return (short)i;
}
}
}
return -1;
}
public List<short> GetEnclosingPolygons(Point p)
{
var polygons = new List<short>();
for (var i = 0; i < Polygons.Count; ++i)
{
if (Filter(Polygons[i]))
{
var points = GetPointRingFromLineRing(new List<short>(Polygons[i].LineIndexes).GetRange(0, Polygons[i].VertexCount));
if (PointLoopEnclosesPoint(p, points))
{
polygons.Add((short)i);
}
}
}
return polygons;
}
public short NewPoint(short X, short Y)
{
var p = new Point
{
X = X,
Y = Y
};
Endpoints.Add(p);
EndpointPolygons.Add([]);
EndpointLines.Add([]);
return (short)(Endpoints.Count - 1);
}
public short NewLine(short p1, short p2)
{
var line = new Line();
line.EndpointIndexes[0] = p1;
line.EndpointIndexes[1] = p2;
EndpointLines[p1].Add(line);
EndpointLines[p2].Add(line);
Lines.Add(line);
return (short)(Lines.Count - 1);
}
public short NewObject(Point p, short polygon_index)
{
var o = new MapObject
{
X = p.X,
Y = p.Y,
PolygonIndex = polygon_index
};
Objects.Add(o);
return (short)(Objects.Count - 1);
}
// split line, return a new point index at X, Y
public short SplitLine(short index, short X, short Y)
{
var line = Lines[index];
var e0 = line.EndpointIndexes[0];
var e1 = line.EndpointIndexes[1];
var p = NewPoint(X, Y);
var l1 = NewLine(e0, p);
var l2 = NewLine(p, e1);
if (line.ClockwisePolygonOwner != -1)
{
var polygon = Polygons[line.ClockwisePolygonOwner];
for (var i = 0; i < Polygon.MaxVertexCount; ++i)
{
if (polygon.LineIndexes[i] == index)
{
for (var j = Polygon.MaxVertexCount - 2; j >= i; --j)
{
polygon.LineIndexes[j + 1] = polygon.LineIndexes[j];
polygon.AdjacentPolygonIndexes[j + 1] = polygon.AdjacentPolygonIndexes[j];
polygon.EndpointIndexes[j + 1] = polygon.EndpointIndexes[j];
}
polygon.LineIndexes[i] = l1;
polygon.LineIndexes[i + 1] = l2;
if (polygon.EndpointIndexes[i] == e0)
{
polygon.EndpointIndexes[i + 1] = p;
}
else
{
polygon.EndpointIndexes[i] = p;
}
EndpointPolygons[p].Add(polygon);
polygon.VertexCount++;
UpdatePolygonConcavity(polygon);
if (line.ClockwisePolygonSideIndex != -1)
{
// clone it
var side = Sides[line.ClockwisePolygonSideIndex];
var stream = new MemoryStream();
var writer = new BinaryWriterBE(stream);
side.Save(writer);
stream.Seek(0, SeekOrigin.Begin);
var s1 = new Side();
var reader = new BinaryReaderBE(stream);
s1.Load(reader);
s1.LineIndex = l1;
Lines[l1].ClockwisePolygonSideIndex = (short)Sides.Count;
Sides.Add(s1);
stream.Seek(0, SeekOrigin.Begin);
var s2 = new Side();
s2.Load(reader);
s2.LineIndex = l2;
var l1_length = (short)Distance(Endpoints[e0], Endpoints[p]);
if (!s2.Primary.Texture.IsEmpty())
{
s2.Primary.X += l1_length;
}
if (!s2.Secondary.Texture.IsEmpty())
{
s2.Secondary.X += l1_length;
}
if (!s2.Transparent.Texture.IsEmpty())
{
s2.Transparent.X += l1_length;
}
Lines[l2].ClockwisePolygonSideIndex = (short)Sides.Count;
Sides.Add(s2);
}
break;
}
}
Lines[l1].ClockwisePolygonOwner = line.ClockwisePolygonOwner;
Lines[l2].ClockwisePolygonOwner = line.ClockwisePolygonOwner;
}
if (line.CounterclockwisePolygonOwner != -1)
{
var polygon = Polygons[line.CounterclockwisePolygonOwner];
for (var i = 0; i < Polygon.MaxVertexCount; ++i)
{
if (polygon.LineIndexes[i] == index)
{
for (var j = Polygon.MaxVertexCount - 2; j >= i; --j)
{
polygon.LineIndexes[j + 1] = polygon.LineIndexes[j];
polygon.AdjacentPolygonIndexes[j + 1] = polygon.AdjacentPolygonIndexes[j];
polygon.EndpointIndexes[j + 1] = polygon.EndpointIndexes[j];
}
polygon.LineIndexes[i] = l2;
polygon.LineIndexes[i + 1] = l1;
if (polygon.EndpointIndexes[i] == e0)
{
polygon.EndpointIndexes[i] = p;
}
else
{
polygon.EndpointIndexes[i + 1] = p;
}
polygon.VertexCount++;
UpdatePolygonConcavity(polygon);
if (line.CounterclockwisePolygonSideIndex != -1)
{
// clone it
var side = Sides[line.CounterclockwisePolygonSideIndex];
var stream = new MemoryStream();
var writer = new BinaryWriterBE(stream);
side.Save(writer);
stream.Seek(0, SeekOrigin.Begin);
var s1 = new Side();
var reader = new BinaryReaderBE(stream);
s1.Load(reader);
s1.LineIndex = l1;
Lines[l1].CounterclockwisePolygonSideIndex = (short)Sides.Count;
Sides.Add(s1);
stream.Seek(0, SeekOrigin.Begin);
var s2 = new Side();
s2.Load(reader);
s2.LineIndex = l2;
var l1_length = (short)Distance(Endpoints[e0], Endpoints[p]);
if (!s2.Primary.Texture.IsEmpty())
{
s2.Primary.X += l1_length;
}
if (!s2.Secondary.Texture.IsEmpty())
{
s2.Secondary.X += l1_length;
}
if (!s2.Transparent.Texture.IsEmpty())
{
s2.Transparent.X += l1_length;
}
Lines[l2].CounterclockwisePolygonSideIndex = (short)Sides.Count;
Sides.Add(s2);
}
break;
}
}
Lines[l1].CounterclockwisePolygonOwner = line.CounterclockwisePolygonOwner;
Lines[l2].CounterclockwisePolygonOwner = line.CounterclockwisePolygonOwner;
if (Lines[l1].ClockwisePolygonOwner != -1)
{
Lines[l1].Transparent = true;
}
if (Lines[l2].ClockwisePolygonOwner != -1)
{
Lines[l2].Transparent = true;
}
}
if (line.ClockwisePolygonSideIndex != -1)
{
DeleteSide(line.ClockwisePolygonSideIndex);
}
if (line.CounterclockwisePolygonSideIndex != -1)
{
DeleteSide(line.CounterclockwisePolygonSideIndex);
}
DeleteLineIndex(index);
return p;
}
// returns all line indices due east of X, Y sorted by x intersect
SortedDictionary<double, short> GetFillCandidateLines(short X, short Y)
{
SortedDictionary<double, short> candidates = [];
for (var i = 0; i < Lines.Count; ++i)
{
if (FilterLine(Filter, Lines[i]))
{
var p0 = Endpoints[Lines[i].EndpointIndexes[0]];
var p1 = Endpoints[Lines[i].EndpointIndexes[1]];
if (p1.Y == p0.Y)
{
continue;
}
var u = (double)(Y - p0.Y) / (p1.Y - p0.Y);
var x_intersect = p0.X + (u * (p1.X - p0.X));
if (u >= 0.0 && u <= 1.0 && x_intersect > X)
{
// note that if two lines miraculously share the
// same X intercept, one will be lost--oh well,
// click somewhere else
candidates[x_intersect] = (short)i;
}
}
}
return candidates;
}
// get all lines attached to endpoint
public List<short> FindEndpointLines(short index)
{
List<short> lines = [];
for (var i = 0; i < Lines.Count; ++i)
{
var line = Lines[i];
if (FilterLine(Filter, line) && (line.EndpointIndexes[0] == index ||
line.EndpointIndexes[1] == index))
{
lines.Add((short)i);
}
}
return lines;
}
// get all polygons that have this endpoint
public List<short> FindEndpointPolygons(short index)
{
List<short> polygons = [];
for (var i = 0; i < Polygons.Count; ++i)
{
for (var j = 0; j < Polygons[i].VertexCount; ++j)
{
var line = Lines[Polygons[i].LineIndexes[j]];
if (line.EndpointIndexes[0] == index ||
line.EndpointIndexes[1] == index)
{
polygons.Add((short)i);
break;
}
}
}
return polygons;
}
Point Diff(Point p0, Point p1)
{
return new Point((short)(p0.X - p1.X), (short)(p0.Y - p1.Y));
}
double Norm(Point p)
{
return Math.Sqrt(Math.Pow(p.X, 2) + Math.Pow(p.Y, 2));
}
bool BuildLoop(short target, short prev, short current, int depth, short starter, List<short> list)
{
if (depth >= 8)
{
return false;
}
else if (current == target)
{
int other_index = Lines[list[0]].EndpointIndexes[0];
if (other_index == current)
other_index = Lines[list[0]].EndpointIndexes[1];
var cross = Cross(Diff(Endpoints[prev], Endpoints[current]), Diff(Endpoints[other_index], Endpoints[current]));
if (cross >= 0.0)
{
return true;
}
else
{
return false;
}
}
var neighbors = FindEndpointLines(current);
neighbors.Remove(starter);
if (neighbors.Count == 0)
{
return false;
}
// C# needs a multimap :(
SortedDictionary<double, List<short>> dotProducts = [];
foreach (var i in neighbors)
{
if (Lines[i].EndpointIndexes[0] == prev ||
Lines[i].EndpointIndexes[1] == prev)
{
continue;
}
var p0 = Endpoints[Lines[i].EndpointIndexes[0]];
var p1 = Endpoints[Lines[i].EndpointIndexes[1]];
double dp;
if (Lines[i].EndpointIndexes[0] == current)
{
dp = Dot(Diff(Endpoints[prev], Endpoints[current]), Diff(p1, Endpoints[current])) / Norm(Diff(p1, Endpoints[current]));
}
else
{
dp = Dot(Diff(Endpoints[prev], Endpoints[current]), Diff(p0, Endpoints[current])) / Norm(Diff(p0, Endpoints[current]));
}
if (!dotProducts.ContainsKey(dp))
{
dotProducts[dp] = [];
}
dotProducts[dp].Add(i);
}
short firstNeighbor = -1;
foreach (var kvp in dotProducts)
{
foreach (var i in kvp.Value)
{
double cross;
var p0 = Endpoints[Lines[i].EndpointIndexes[0]];
var p1 = Endpoints[Lines[i].EndpointIndexes[1]];
if (Lines[i].EndpointIndexes[0] == current)
{
cross = Cross(Diff(Endpoints[prev], Endpoints[current]), Diff(p1, Endpoints[current]));
}
else
{
cross = Cross(Diff(Endpoints[prev], Endpoints[current]), Diff(p0, Endpoints[current]));
}
if (cross >= 0.0)
{
firstNeighbor = i;
}
}
}
if (firstNeighbor == -1)
{
return false;
}
short tightest;
if (Lines[firstNeighbor].EndpointIndexes[0] == current)
{
tightest = Lines[firstNeighbor].EndpointIndexes[1];
}
else
{
tightest = Lines[firstNeighbor].EndpointIndexes[0];
}
list.Add(firstNeighbor);
return BuildLoop(target, current, tightest, depth + 1, starter, list);
}
List<short> GetPointRingFromLineRing(List<short> lines)
{
List<short> points =
[
// find the first point
Lines[lines[0]].EndpointIndexes[0] == Lines[lines[1]].EndpointIndexes[0] ||
Lines[lines[0]].EndpointIndexes[0] == Lines[lines[1]].EndpointIndexes[1] ? Lines[lines[0]].EndpointIndexes[0] : Lines[lines[0]].EndpointIndexes[1],
];
for (var i = 1; i < lines.Count; ++i)
{
if (points[points.Count - 1] == Lines[lines[i]].EndpointIndexes[0])
{
points.Add(Lines[lines[i]].EndpointIndexes[1]);
}
else
{
points.Add(Lines[lines[i]].EndpointIndexes[0]);
}
}
return points;
}
double Angle(int X0, int Y0, int X1, int Y1)
{
// computes the angle from the line from the origin to p0,
// and the line from the origin to p1, and returns a value
// in the range [-pi,pi]
var dtheta = (Math.Atan2(Y0, X0) - Math.Atan2(Y1, X1)) % (Math.PI * 2);
if (dtheta > Math.PI)
return dtheta - (Math.PI * 2);
else if (dtheta < -Math.PI)
return dtheta + (Math.PI * 2);
else
return dtheta;
}
bool PointLoopEnclosesPoint(Point p, List<short> points)
{
double angle_sum = 0;
for (var i = 0; i < points.Count; ++i)
{
var p0 = Endpoints[points[i]];
var p1 = Endpoints[points[(i + 1) % points.Count]];
var angle = Angle(p0.X - p.X, p0.Y - p.Y, p1.X - p.X, p1.Y - p.Y);
angle_sum += angle;
}
return !(Math.Abs(angle_sum) < Math.PI);
}
List<short> SelectLineLoop(short X, short Y)
{
var candidates = GetFillCandidateLines(X, Y);
foreach (var kvp in candidates)
{
var line = Lines[kvp.Value];
var p0 = line.EndpointIndexes[0];
var p1 = line.EndpointIndexes[1];
if (Endpoints[p0].Y > Endpoints[p1].Y)
{
p0 = line.EndpointIndexes[1];
p1 = line.EndpointIndexes[0];
}
List<short> lines = [kvp.Value];
if (BuildLoop(p0, p0, p1, 0, kvp.Value, lines))
{
// make sure the points enclose X, Y!
var points = GetPointRingFromLineRing(lines);
if (PointLoopEnclosesPoint(new Point(X, Y), points))
{
return lines;
}
}
}
return [];
}
public bool FillPolygon(short X, short Y)
{
if (GetEnclosingPolygon(new Point(X, Y)) != -1)
{
return false;
}
var loop = SelectLineLoop(X, Y);
if (loop.Count == 0)
{
return false;
}
foreach (var i in loop)
{
if (Lines[i].ClockwisePolygonOwner != -1 &&
Lines[i].CounterclockwisePolygonOwner != -1)
{
return false;
}
}
var polygon = new Polygon
{
VertexCount = (ushort)loop.Count
};
loop.CopyTo(polygon.LineIndexes);
var points = GetPointRingFromLineRing(loop);
points.CopyTo(polygon.EndpointIndexes);
foreach (var i in points)
{
EndpointPolygons[i].Add(polygon);
}
var index = (short)Polygons.Count;
Polygons.Add(polygon);
Polygon adjacent = null;
for (var i = 0; i < loop.Count; ++i)
{
var line = Lines[loop[i]];
if (line.EndpointIndexes[1] == polygon.EndpointIndexes[i])
{
line.ClockwisePolygonOwner = index;
if (line.CounterclockwisePolygonOwner != -1 && adjacent == null)
{
adjacent = Polygons[line.CounterclockwisePolygonOwner];
}
if (RememberDeletedSides && ClockwiseOrphanedSides.ContainsKey(loop[i]))
{
var side = ClockwiseOrphanedSides[loop[i]];
line.ClockwisePolygonSideIndex = (short)Sides.Count;
side.LineIndex = loop[i];
side.PolygonIndex = index;
Sides.Add(side);
FixSideType(side);
ClockwiseOrphanedSides.Remove(loop[i]);
}
}
else
{
line.CounterclockwisePolygonOwner = index;
if (line.ClockwisePolygonOwner != -1 && adjacent == null)
{
adjacent = Polygons[line.ClockwisePolygonOwner];
}
if (RememberDeletedSides && CounterClockwiseOrphanedSides.ContainsKey(loop[i]))
{
var side = CounterClockwiseOrphanedSides[loop[i]];
line.CounterclockwisePolygonSideIndex = (short)Sides.Count;
side.LineIndex = loop[i];
side.PolygonIndex = index;
Sides.Add(side);
FixSideType(side);
CounterClockwiseOrphanedSides.Remove(loop[i]);
}
}
if (line.ClockwisePolygonOwner != -1 &&
line.CounterclockwisePolygonOwner != -1)
{
line.Flags = LineFlags.Transparent;
// empty the side opposite
if (line.ClockwisePolygonOwner == index)
{
if (line.CounterclockwisePolygonSideIndex != -1)
{
if (RememberDeletedSides && LinesReflectingOrphanedSides.Contains(loop[i]))
{
LinesReflectingOrphanedSides.Remove(loop[i]);
}
else
{
Sides[line.CounterclockwisePolygonSideIndex].Clear();
}
}
}
else
{
if (line.ClockwisePolygonSideIndex != -1)
{
if (RememberDeletedSides && LinesReflectingOrphanedSides.Contains(loop[i]))
{
LinesReflectingOrphanedSides.Remove(loop[i]);
}
else
{
Sides[line.ClockwisePolygonSideIndex].Clear();
}
}
}
}
}
if (adjacent != null)
{
// copy some settings from it
polygon.FloorHeight = adjacent.FloorHeight;
polygon.CeilingHeight = adjacent.CeilingHeight;
polygon.FloorTexture = adjacent.FloorTexture;
polygon.CeilingTexture = adjacent.CeilingTexture;
polygon.FloorTransferMode = adjacent.FloorTransferMode;
polygon.CeilingTransferMode = adjacent.CeilingTransferMode;
polygon.FloorLight = adjacent.FloorLight;
polygon.CeilingLight = adjacent.CeilingLight;
}
UpdatePolygonConcavity(polygon);
return true;
}
public void UpdatePolygonConcavity(Polygon polygon)
{
List<short> lines = [];
for (var i = 0; i < polygon.VertexCount; ++i)
{
lines.Add(polygon.LineIndexes[i]);
}
double positive = 0;
var points = GetPointRingFromLineRing(lines);
for (var i = 0; i < points.Count; ++i)
{
var p0 = Endpoints[points[i]];
var p1 = Endpoints[points[(i + 1) % points.Count]];
var p2 = Endpoints[points[(i + 2) % points.Count]];
var cross = Cross(Diff(p1, p0), Diff(p2, p1));
if (positive == 0)
{
positive = cross;
}
else if (cross != 0)
{
if ((positive > 0) != (cross > 0))
{
polygon.Concave = true;
return;
}
}
}
polygon.Concave = false;
}
public Point PolygonCenter(Polygon polygon)
{
double X = 0;
double Y = 0;
for (var i = 0; i < polygon.VertexCount; ++i)
{
X += Endpoints[polygon.EndpointIndexes[i]].X;
Y += Endpoints[polygon.EndpointIndexes[i]].Y;
}
return new Point((short)(X / polygon.VertexCount), (short)(Y / polygon.VertexCount));
}
public void DeleteObject(short index)
{
var obj = Objects[index];
if (obj.Type == ObjectType.Monster && MonsterPlacement[obj.Index].InitialCount > 0)
{
MonsterPlacement[obj.Index].InitialCount--;
}
else if (obj.Type == ObjectType.Item && ItemPlacement[obj.Index].InitialCount > 0)
{
ItemPlacement[obj.Index].InitialCount--;
}
Objects.RemoveAt(index);
}
public void DeletePlatform(short index)
{
Platforms.RemoveAt(index);
foreach (var polygon in Polygons)
{
if (polygon.Type == PolygonType.Platform)
{
if (polygon.Permutation > index)
{
--polygon.Permutation;
}
}
}
}
public void TogglePolygonLayer(short index, int layer)
{
if (Attributes.PolygonLayers.ContainsKey(index))
{
if (Attributes.PolygonLayers[index].Contains(layer))
{
Attributes.PolygonLayers[index].Remove(layer);
}
else
{
Attributes.PolygonLayers[index].Add(layer);
}
}
else
{
Attributes.PolygonLayers[index] = [layer];
}
}
public void ClearPolygonLayers(short index)
{
if (Attributes.PolygonLayers.ContainsKey(index))
{
Attributes.PolygonLayers.Remove(index);
}
}
public void DeletePolygon(short index)
{
var polygonRef = Polygons[index];
short platformIndex = -1;
if (Polygons[index].Type == PolygonType.Platform)
{
platformIndex = Polygons[index].Permutation;
}
Polygons.RemoveAt(index);
for (short line_index = 0; line_index < Lines.Count; ++line_index)
{
var line = Lines[line_index];
if (line.ClockwisePolygonOwner > index)
{
--line.ClockwisePolygonOwner;
}
else if (line.ClockwisePolygonOwner == index)
{
line.ClockwisePolygonOwner = -1;
if (RememberDeletedSides)
{
if (LinesReflectingOrphanedSides.Contains(line_index))
{
LinesReflectingOrphanedSides.Remove(line_index);
}
else
{
LinesReflectingOrphanedSides.Add(line_index);
}
}
line.Flags = LineFlags.Solid;
}
if (line.CounterclockwisePolygonOwner > index)
{
--line.CounterclockwisePolygonOwner;
}
else if (line.CounterclockwisePolygonOwner == index)