This repository has been archived by the owner on May 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathMPE_fastpoly2tri.h
2201 lines (1966 loc) · 64.1 KB
/
MPE_fastpoly2tri.h
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
/* fast-poly2tri - v1.0
Rewrite of the poly2tri library (https://github.com/jhasse/poly2tri)
by Unspongeful (@unspongeful).
Based on the Sweep-line, Constrained Delauney Triangulation (CDT) See: Domiter,
V. and Zalik, B.(2008)'Sweep-line algorithm for constrained Delaunay
triangulation', International Journal of Geographical Information Science
"FlipScan" Constrained Edge Algorithm by Thomas hln, [email protected]
This library's focus is speed. No validation is performed, other than
basic debug-time assert.
Passing malformed data will result in a crash, _you must_ pre-validate
the data.
NOTE: Unlike unlike poly2tri's original implementation I removed
the ability to add Steiner points explicitly, but they can be added
by manipulating the internal state of the context.
////////////////////////////////
To include in your project:
#define MPE_POLY2TRI_IMPLEMENTATION
before you include this file in *one* C or C++ file to create the implementation.
// i.e. it should look like this:
#include ...
#define MPE_POLY2TRI_IMPLEMENTATION
#include "MPE_fastpoly2tri.h"
On multiple compilation unit builds you must also redefine
`insternal_static` to extern.
////////////////////////////////
Usage:
// The maximum number of points you expect to need
// This value is used by the library to calculate
// working memory required
uint32_t MaxPointCount = 100;
// Request how much memory (in bytes) you should
// allocate for the library
size_t MemoryRequired = MPE_PolyMemoryRequired(MaxPointCount);
// Allocate a void* memory block of size MemoryRequired
// IMPORTANT: The memory must be zero initialized
void* Memory = calloc(MemoryRequired, 1);
// Initialize the poly context by passing the memory pointer,
// and max number of points from before
MPEPolyContext PolyContext;
if (MPE_PolyInitContext(&PolyContext, Memory, MaxPointCount))
{
// Populate the points of the polyline for the shape
// you want to triangulate using one of the following
// two methods:
// Option A - One point at a time
for(...)
{
MPEPolyPoint* Point = MPE_PolyPushPoint(&PolyContext);
Point->X = ...;
Point->Y = ...;
}
// Option B - memcpy
// This option requires your point data structure to
// correspond to MPEPolyPoint. See below.
uint32_t PointCount = ...;
MPEPolyPoint* FirstPoint = MPE_PolyPushPointArray(&PolyContext, PointCount);
memcpy(FirstPoint, YOUR_POINT_DATA, sizeof(MPEPolyPoint)*PointCount);
// IMPORTANT: Both push functions perform no validation other
// than an assert for bounds checking. You must make sure your
// point data is correct:
// - Duplicate points are not supported
// - Bounds checking is not implemented other than debug asserts
// Add the polyline for the edge. This will consume all points added so far.
MPE_PolyAddEdge(&PolyContext);
// If you want to add holes to the shape you can do:
if (AddHoles)
{
MPEPolyPoint* Hole = MPE_PolyPushPointArray(&PolyContext, 4);
Hole[0].X = 325; Hole[0].Y = 437;
Hole[1].X = 320; Hole[1].Y = 423;
Hole[2].X = 329; Hole[2].Y = 413;
Hole[3].X = 332; Hole[3].Y = 423;
MPE_PolyAddHole(&PolyContext);
}
// Triangulate the shape
MPE_PolyTriangulate(&PolyContext);
// The resulting triangles can be used like so
for (uxx TriangleIndex = 0; TriangleIndex < PolyContext.TriangleCount; ++TriangleIndex)
{
MPEPolyTriangle* Triangle = PolyContext.Triangles[TriangleIndex];
MPEPolyPoint* PointA = Triangle->Points[0];
MPEPolyPoint* PointB = Triangle->Points[1];
MPEPolyPoint* PointC = Triangle->Points[2];
}
// You may want to copy the resulting triangle soup into
// your own data structures if you plan on using them often
// as the Points and Triangles from the PolyContext are not
// compact in memory.
////////////////////////////////
Configuration defines:
#define MPE_POLY2TRI_USE_DOUBLE
To use double precision floating point for calculations
#define MPE_POLY2TRI_USE_CUSTOM_SORT
To use the a custom merge sort implementation. Enabling this option will
require more working memory.
////////////////////////////////
Standard library overrides
#define MPE_MemorySet, MPE_MemoryCopy
To avoid including string.h
#define internal_static
Defaults to static, but you can change it if not using a unity-style build.
LICENSE - MIT (same as original license)
*/
#ifndef MPE_POLY2TRI_HEADER
#define MPE_POLY2TRI_HEADER
//SECTION: Engine define overrides
#include <math.h> // fabs fabsf
#ifndef MPE_MemorySet
#include <string.h> //memset
#define MPE_MemorySet memset
#define MPE_MemoryCopy memcpy
#endif
#ifndef MPE_Assert
#include <assert.h>
#define MPE_Assert assert
#endif
#ifndef uxx
#include <stdint.h> // uint32_t
typedef uint8_t u8;
typedef int32_t i32;
typedef int32_t b32;
typedef uint32_t u32;
typedef float f32;
typedef double f64;
typedef size_t uxx;
typedef ssize_t bxx;
typedef intptr_t imm;
typedef uintptr_t umm;
#endif
#ifdef MPE_POLY2TRI_USE_DOUBLE
typedef double poly_float;
#else
typedef float poly_float;
#endif
#ifndef internal_static
#define internal_static static
#endif
#ifndef MPE_INLINE
#define MPE_INLINE inline
#endif
#ifdef __cplusplus
extern "C" {
#endif
/////////////////////
typedef struct MPEPolyAllocator
{
u8* Memory;
umm Size;
umm Used;
} MPEPolyAllocator;
typedef struct MPEPolyPoint
{
struct MPEPolyEdge* FirstEdge;
poly_float X;
poly_float Y;
} MPEPolyPoint;
typedef struct MPEPolyTriangle
{
struct MPEPolyTriangle* Neighbors[3];
MPEPolyPoint* Points[3];
uxx Flags;
} MPEPolyTriangle;
typedef struct MPEPolyNode
{
struct MPEPolyNode* Next;
struct MPEPolyNode* Prev;
MPEPolyPoint* Point;
MPEPolyTriangle* Triangle;
poly_float Value;
#ifndef MPE_POLY2TRI_USE_DOUBLE
u32 _PADDING;
#endif
} MPEPolyNode;
typedef struct MPEPolyBasin
{
MPEPolyNode* LeftNode;
MPEPolyNode* BottomNode;
MPEPolyNode* RightNode;
poly_float Width;
#ifdef MPE_POLY2TRI_USE_DOUBLE
bxx LeftHighest;
#else
b32 LeftHighest;
#endif
} MPEPolyBasin;
typedef struct MPEPolyEdge
{
MPEPolyPoint* P;
MPEPolyPoint* Q;
struct MPEPolyEdge* Next;
} MPEPolyEdge;
typedef struct MPEEdgeEvent
{
MPEPolyEdge* ConstrainedEdge;
bxx Right;
} MPEEdgeEvent;
typedef struct MPEPolyContext
{
void* UserData;
void* Memory;
MPEPolyAllocator Allocator;
MPEPolyBasin Basin;
MPEEdgeEvent EdgeEvent;
MPEPolyNode* HeadNode;
MPEPolyNode* TailNode;
MPEPolyNode* SearchNode;
MPEPolyTriangle* TrianglePool;
MPEPolyTriangle** Triangles;
MPEPolyTriangle** TempTriangles;
MPEPolyPoint** Points;
MPEPolyPoint* PointsPool;
MPEPolyNode* Nodes;
MPEPolyPoint* HeadPoint;
MPEPolyPoint* TailPoint;
u32 MaxPointCount;
u32 PointPoolCount;
u32 PointCount;
u32 TrianglePoolCount;
u32 TriangleCount;
u32 NodeCount;
b32 Valid;
u32 _PADDING;
} MPEPolyContext;
/////////////////////
// PUBLIC API
internal_static
umm MPE_PolyMemoryRequired(u32 MaxPointCount);
internal_static
b32 MPE_PolyInitContext(MPEPolyContext* PolyContext, void* Memory, u32 MaxPointCount);
internal_static
MPEPolyPoint* MPE_PolyPushPoint(MPEPolyContext* PolyContext);
internal_static
MPEPolyPoint* MPE_PolyPushPointArray(MPEPolyContext* PolyContext, u32 Count);
internal_static
void MPE_PolyAddEdge(MPEPolyContext* PolyContext);
internal_static
void MPE_PolyTriangulate(MPEPolyContext* PolyContext);
//SECTION: Required overrides
#ifdef __cplusplus
}
#endif
#endif // MPE_POLY2TRI_HEADER
#ifdef MPE_POLY2TRI_IMPLEMENTATION
//////////////////////
// IMPLEMENTATION
#define MPE_POLY2TRI_EPSILON (poly_float)1e-12f
#define MPE_POLY2TRI_POINT_COUNT_EPSILON 16
#ifndef MPE_InvalidCodePath
#define MPE_InvalidCodePath MPE_Assert(0)
#endif
#ifndef MPE_TRUE
#define MPE_FALSE 0
#define MPE_TRUE 1
#endif
#ifndef MPE_Assert
#error "Please #define MPE_Assert to assert from <assert.h> or custom implementation"
#endif
#ifndef MPE_MemorySet
#error "Please #define MPE_MemorySet to memset from <string.h> or custom implementation"
#endif
#define MPE_PolyPush(Allocator, Type) ((Type*)MPE_PolyPushRaw(&Allocator, sizeof(Type)))
#define MPE_PolyPushArray(Allocator, Type, Count) ((Type*)MPE_PolyPushRaw(&Allocator, sizeof(Type) * Count))
internal_static
u8* MPE_PolyPushRaw(MPEPolyAllocator* Allocator, umm Size);
typedef struct MPEPolyEdges
{
u8 ConstrainedCW;
u8 ConstrainedCCW;
u8 DelaunayCW;
u8 DelaunayCCW;
} MPEPolyEdges;
enum MPEPolyOrientation
{
MPEPolyOrientation_CCW,
MPEPolyOrientation_CW,
MPEPolyOrientation_Collinear
};
enum MPEPolyTriFlag
{
MPEPolyTriFlag_DelaunayEdge0 = 1 << 0,
MPEPolyTriFlag_DelaunayEdge1 = 1 << 1,
MPEPolyTriFlag_DelaunayEdge2 = 1 << 2,
MPEPolyTriFlag_ConstrainedEdge0 = 1 << 3,
MPEPolyTriFlag_ConstrainedEdge1 = 1 << 4,
MPEPolyTriFlag_ConstrainedEdge2 = 1 << 5,
MPEPolyTriFlag_IsInterior = 1 << 30,
MPEPolyTriFlag_DelaunayEdgeMask = MPEPolyTriFlag_DelaunayEdge0 | MPEPolyTriFlag_DelaunayEdge1 | MPEPolyTriFlag_DelaunayEdge2,
MPEPolyTriFlag_ConstrainedEdgeMask = MPEPolyTriFlag_ConstrainedEdge0 | MPEPolyTriFlag_ConstrainedEdge1 | MPEPolyTriFlag_ConstrainedEdge2
};
static u32 MPEPolyEdgeLUT[] = {0, 1, 2, 0, 1, 2, 3};
internal_static
u8* MPE_PolyPushRaw(MPEPolyAllocator* Allocator, umm Size)
{
u8* Result = 0;
if (Allocator->Used + Size <= Allocator->Size)
{
Result = Allocator->Memory + Allocator->Used;
Allocator->Used += Size;
}
else
{
MPE_InvalidCodePath;
}
return Result;
}
/*
Formula to calculate signed area
Positive if CCW
Negative if CW
0 if collinear
A[P1,P2,P3] = (x1*y2 - y1*x2) + (x2*y3 - y2*x3) + (x3*y1 - y3*x1)
= (x1-x3)*(y2-y3) - (y1-y3)*(x2-x3)
*/
internal_static
uxx MPE_PolyOrient2D(MPEPolyPoint* PointA, MPEPolyPoint* PointB, MPEPolyPoint* PointC)
{
uxx Result;
MPE_Assert(PointA != PointB && PointA != PointC && PointB != PointC);
poly_float DeltaLeft = (PointA->X - PointC->X) * (PointB->Y - PointC->Y);
poly_float DeltaRight = (PointA->Y - PointC->Y) * (PointB->X - PointC->X);
poly_float Value = DeltaLeft - DeltaRight;
if (Value > -MPE_POLY2TRI_EPSILON && Value < MPE_POLY2TRI_EPSILON)
{
Result = MPEPolyOrientation_Collinear;
}
else if (Value > 0)
{
Result = MPEPolyOrientation_CCW;
}
else
{
Result = MPEPolyOrientation_CW;
}
return Result;
}
internal_static
bxx MPE_PolyInScanArea(MPEPolyPoint* PointA, MPEPolyPoint* PointB, MPEPolyPoint* PointC, MPEPolyPoint* PointD)
{
MPE_Assert(PointA != PointB && PointA != PointC && PointA != PointD &&
PointB != PointC && PointB != PointD &&
PointC != PointD);
poly_float oadb = (PointA->X - PointB->X) * (PointD->Y - PointB->Y) - (PointD->X - PointB->X) * (PointA->Y - PointB->Y);
bxx Result;
if (oadb >= -MPE_POLY2TRI_EPSILON)
{
Result = MPE_FALSE;
}
else
{
poly_float oadc = (PointA->X - PointC->X) * (PointD->Y - PointC->Y) - (PointD->X - PointC->X) * (PointA->Y - PointC->Y);
if (oadc <= MPE_POLY2TRI_EPSILON)
{
Result = MPE_FALSE;
}
else
{
Result = MPE_TRUE;
}
}
return Result;
}
internal_static MPE_INLINE
bxx MPE_PolyContainsPoint(MPEPolyTriangle* Triangle, MPEPolyPoint* Point)
{
MPE_Assert(Point);
bxx Result = Point == Triangle->Points[0] || Point == Triangle->Points[1] || Point == Triangle->Points[2];
return Result;
}
internal_static MPE_INLINE
bxx MPE_PolyContainsPoints(MPEPolyTriangle* Triangle, MPEPolyPoint* Point, MPEPolyPoint* OtherPoint)
{
bxx Result = MPE_PolyContainsPoint(Triangle, Point) && MPE_PolyContainsPoint(Triangle, OtherPoint);
return Result;
}
internal_static
void MPE_PolySwapNeighbors(MPEPolyTriangle* Triangle, MPEPolyTriangle* OldNeighbor, MPEPolyTriangle* NewNeighbor)
{
for(u32 NeighborIndex = 0;; ++NeighborIndex)
{
MPE_Assert(NeighborIndex < 3);
if (Triangle->Neighbors[NeighborIndex] == OldNeighbor)
{
Triangle->Neighbors[NeighborIndex] = NewNeighbor;
break;
}
}
}
internal_static
void MPE_PolyMarkNeighborTri(MPEPolyTriangle* Triangle, MPEPolyTriangle* Neighbor)
{
for (u32 EdgeIndex = 0; EdgeIndex < 3; ++EdgeIndex)
{
u32 NextEdgeIndex = MPEPolyEdgeLUT[EdgeIndex+1];
for (u32 NeighborIndex = 0; NeighborIndex < 3; ++NeighborIndex)
{
u32 NextNeighborIndex = MPEPolyEdgeLUT[NeighborIndex+1];
if ((Triangle->Points[EdgeIndex] == Neighbor->Points[NeighborIndex] &&
Triangle->Points[NextEdgeIndex] == Neighbor->Points[NextNeighborIndex]) ||
(Triangle->Points[EdgeIndex] == Neighbor->Points[NextNeighborIndex] &&
Triangle->Points[NextEdgeIndex] == Neighbor->Points[NeighborIndex]))
{
Triangle->Neighbors[MPEPolyEdgeLUT[NextEdgeIndex+1]] = Neighbor;
Neighbor->Neighbors[MPEPolyEdgeLUT[NextNeighborIndex+1]] = Triangle;
return;
}
}
}
}
internal_static MPE_INLINE
void MPE_ClearDelunayEdges(MPEPolyTriangle* Triangle)
{
Triangle->Flags &= ~(uxx)MPEPolyTriFlag_DelaunayEdgeMask;
}
internal_static
MPEPolyPoint* MPE_PointCW(MPEPolyTriangle* Triangle, MPEPolyPoint* Point, i32* PointIndex)
{
MPEPolyPoint* Result;
if (Point == Triangle->Points[0])
{
Result = Triangle->Points[2];
*PointIndex = 2;
}
else if (Point == Triangle->Points[1])
{
Result = Triangle->Points[0];
*PointIndex = 0;
}
else if (Point == Triangle->Points[2])
{
Result = Triangle->Points[1];
*PointIndex = 1;
}
else
{
MPE_InvalidCodePath;
Result = 0;
*PointIndex = -1;
}
return Result;
}
internal_static
MPEPolyTriangle* MPE_NeighborCCW(MPEPolyTriangle* Triangle, MPEPolyPoint* Point)
{
MPEPolyTriangle* Result;
if (Point == Triangle->Points[0])
{
Result = Triangle->Neighbors[2];
}
else if (Point == Triangle->Points[1])
{
Result = Triangle->Neighbors[0];
}
else
{
MPE_Assert(Point == Triangle->Points[2]);
Result = Triangle->Neighbors[1];
}
return Result;
}
#define FLAG(Tri, Name) (Tri->Flags & MPEPolyTriFlag_##Name) == MPEPolyTriFlag_## Name
internal_static
void MPE_SetAdjacentEdges(MPEPolyTriangle* Triangle, MPEPolyPoint* Point, MPEPolyEdges Edges, bxx Clockwise)
{
uxx Mask;
uxx NewFlags;
if (Point == Triangle->Points[0])
{
if (Clockwise)
{
Mask = MPEPolyTriFlag_ConstrainedEdge1 | MPEPolyTriFlag_DelaunayEdge1;
NewFlags = (Edges.ConstrainedCW & MPEPolyTriFlag_ConstrainedEdge1) |
(Edges.DelaunayCW & MPEPolyTriFlag_DelaunayEdge1);
}
else
{
Mask = MPEPolyTriFlag_ConstrainedEdge2 | MPEPolyTriFlag_DelaunayEdge2;
NewFlags = (Edges.ConstrainedCCW & MPEPolyTriFlag_ConstrainedEdge2) |
(Edges.DelaunayCCW & MPEPolyTriFlag_DelaunayEdge2);
}
}
else if (Point == Triangle->Points[1])
{
if (Clockwise)
{
Mask = MPEPolyTriFlag_ConstrainedEdge2 | MPEPolyTriFlag_DelaunayEdge2;
NewFlags = (Edges.ConstrainedCW & MPEPolyTriFlag_ConstrainedEdge2) |
(Edges.DelaunayCW & MPEPolyTriFlag_DelaunayEdge2);
}
else
{
Mask = MPEPolyTriFlag_ConstrainedEdge0 | MPEPolyTriFlag_DelaunayEdge0;
NewFlags = (Edges.ConstrainedCCW & MPEPolyTriFlag_ConstrainedEdge0) |
(Edges.DelaunayCCW & MPEPolyTriFlag_DelaunayEdge0);
}
}
else
{
if (Clockwise)
{
Mask = MPEPolyTriFlag_ConstrainedEdge0 | MPEPolyTriFlag_DelaunayEdge0;
NewFlags = (Edges.ConstrainedCW & MPEPolyTriFlag_ConstrainedEdge0) |
(Edges.DelaunayCW & MPEPolyTriFlag_DelaunayEdge0);
}
else
{
Mask = MPEPolyTriFlag_ConstrainedEdge1 | MPEPolyTriFlag_DelaunayEdge1;
NewFlags = (Edges.ConstrainedCCW & MPEPolyTriFlag_ConstrainedEdge1) |
(Edges.DelaunayCCW & MPEPolyTriFlag_DelaunayEdge1);
}
}
Triangle->Flags = (Triangle->Flags & ~Mask) | NewFlags;
}
internal_static
uxx MPE_GetConstrainedEdgeCW(MPEPolyTriangle* Triangle, MPEPolyPoint* Point)
{
uxx Result;
if (Point == Triangle->Points[0])
{
Result = Triangle->Flags & MPEPolyTriFlag_ConstrainedEdge1;
}
else if (Point == Triangle->Points[1])
{
Result = Triangle->Flags & MPEPolyTriFlag_ConstrainedEdge2;
}
else
{
MPE_Assert(Point == Triangle->Points[2]);
Result = Triangle->Flags & MPEPolyTriFlag_ConstrainedEdge0;
}
return Result;
}
internal_static MPE_INLINE
MPEPolyTriangle* MPE_PolyNeighborAcross(MPEPolyTriangle* Triangle, MPEPolyPoint* OppositePoint)
{
MPEPolyTriangle* Result;
if (OppositePoint == Triangle->Points[0])
{
Result = Triangle->Neighbors[0];
}
else if (OppositePoint == Triangle->Points[1])
{
Result = Triangle->Neighbors[1];
}
else
{
MPE_Assert(OppositePoint == Triangle->Points[2]);
Result = Triangle->Neighbors[2];
}
return Result;
}
internal_static MPE_INLINE
void MPE_PolyLegalizePoint(MPEPolyTriangle* Triangle, MPEPolyPoint* Point)
{
Triangle->Points[1] = Triangle->Points[0];
Triangle->Points[0] = Triangle->Points[2];
Triangle->Points[2] = Point;
}
internal_static
i32 MPE_PolyPointIndex(MPEPolyTriangle* Triangle, MPEPolyPoint* Point)
{
i32 Result;
if (Point == Triangle->Points[0])
{
Result = 0;
}
else if (Point == Triangle->Points[1])
{
Result = 1;
}
else if (Point == Triangle->Points[2])
{
Result = 2;
}
else
{
Result = -1;
MPE_InvalidCodePath;
}
return Result;
}
internal_static
i32 MPE_EdgeIndex(MPEPolyTriangle* Triangle, MPEPolyPoint* P1, MPEPolyPoint* P2)
{
i32 Result = -1;
if (Triangle->Points[0] == P1)
{
if (Triangle->Points[1] == P2)
{
Result = 2;
}
else if (Triangle->Points[2] == P2)
{
Result = 1;
}
}
else if (Triangle->Points[1] == P1)
{
if (Triangle->Points[2] == P2)
{
Result = 0;
}
else if (Triangle->Points[0] == P2)
{
Result = 2;
}
}
else if (Triangle->Points[2] == P1)
{
if (Triangle->Points[0] == P2)
{
Result = 1;
}
else if (Triangle->Points[1] == P2)
{
Result = 0;
}
}
return Result;
}
internal_static MPE_INLINE
void MPE_PolyMarkConstrainedEdgeIndex(MPEPolyTriangle* Triangle, i32 Index)
{
MPE_Assert(Index >= 0);
Triangle->Flags |= (uxx)MPEPolyTriFlag_ConstrainedEdge0 << (u32)Index;
}
internal_static MPE_INLINE
void MPE_PolyMarkDelaunayEdge(MPEPolyTriangle* Triangle, i32 Index)
{
MPE_Assert(Index >= 0);
Triangle->Flags |= (uxx)MPEPolyTriFlag_DelaunayEdge0 << (u32)Index;
}
internal_static
void MPE_PolyMarkConstrainedEdgePoints(MPEPolyTriangle* Triangle, MPEPolyPoint* Point, MPEPolyPoint* OtherPoint)
{
if ((OtherPoint == Triangle->Points[0] && Point == Triangle->Points[1]) ||
(OtherPoint == Triangle->Points[1] && Point == Triangle->Points[0]))
{
MPE_PolyMarkConstrainedEdgeIndex(Triangle, 2);
}
else if ((OtherPoint == Triangle->Points[0] && Point == Triangle->Points[2]) ||
(OtherPoint == Triangle->Points[2] && Point == Triangle->Points[0]))
{
MPE_PolyMarkConstrainedEdgeIndex(Triangle, 1);
}
else if ((OtherPoint == Triangle->Points[1] && Point == Triangle->Points[2]) ||
(OtherPoint == Triangle->Points[2] && Point == Triangle->Points[1]))
{
MPE_PolyMarkConstrainedEdgeIndex(Triangle, 0);
}
}
internal_static MPE_INLINE
void MPE_PolyMarkConstrainedEdge(MPEPolyTriangle* Triangle, MPEPolyEdge* Edge)
{
MPE_PolyMarkConstrainedEdgePoints(Triangle, Edge->P, Edge->Q);
}
internal_static MPE_INLINE
MPEPolyNode* MPE_PolyNode(MPEPolyContext* PolyContext, MPEPolyPoint* Point, MPEPolyTriangle* Triangle)
{
MPEPolyNode* Result = PolyContext->Nodes + PolyContext->NodeCount++;
Result->Point = Point;
Result->Triangle = Triangle;
Result->Value = Point->X;
return Result;
}
internal_static
MPEPolyPoint* MPE_PolyPushPoint(MPEPolyContext* PolyContext)
{
MPE_Assert(PolyContext->MaxPointCount > PolyContext->PointPoolCount);
MPEPolyPoint* Result = PolyContext->PointsPool + PolyContext->PointPoolCount++;
return Result;
}
internal_static
MPEPolyPoint* MPE_PolyPushPointArray(MPEPolyContext* PolyContext, u32 Count)
{
MPE_Assert(PolyContext->MaxPointCount > (PolyContext->PointPoolCount+Count));
MPEPolyPoint* Result = PolyContext->PointsPool + PolyContext->PointPoolCount;
PolyContext->PointPoolCount += Count;
return Result;
}
internal_static MPE_INLINE
MPEPolyTriangle* MPE_PushTriangle(MPEPolyContext* PolyContext, MPEPolyPoint* A, MPEPolyPoint* B, MPEPolyPoint* C)
{
MPEPolyTriangle* Result = PolyContext->TrianglePool + PolyContext->TrianglePoolCount++;
Result->Points[0] = A;
Result->Points[1] = B;
Result->Points[2] = C;
return Result;
}
internal_static MPE_INLINE
MPEPolyNode* MPE_LocatePoint(MPEPolyContext* PolyContext, MPEPolyPoint* Point)
{
poly_float PX = Point->X;
MPEPolyNode* Node = PolyContext->SearchNode;
poly_float NX = Node->Point->X;
if (PX < NX)
{
while ((Node = Node->Prev))
{
if (Point == Node->Point)
{
break;
}
}
}
else if ((PX - NX) < MPE_POLY2TRI_EPSILON)
{
if (Point != Node->Point)
{
// We might have two nodes with same X Value for a short time
if (Point == Node->Prev->Point)
{
Node = Node->Prev;
}
else
{
MPE_Assert(Point == Node->Next->Point);
Node = Node->Next;
}
}
}
else
{
while ((Node = Node->Next))
{
if (Point == Node->Point)
{
break;
}
}
}
if (Node)
{
PolyContext->SearchNode = Node;
}
return Node;
}
internal_static
bxx MPE_IsEdgeSideOfTriangle(MPEPolyTriangle* Triangle, MPEPolyPoint* EdgeP, MPEPolyPoint* EdgeQ)
{
i32 EdgeIndex = MPE_EdgeIndex(Triangle, EdgeP, EdgeQ);
bxx Result;
if (EdgeIndex != -1)
{
MPE_PolyMarkConstrainedEdgeIndex(Triangle, EdgeIndex);
MPEPolyTriangle* NeighborTriangle = Triangle->Neighbors[EdgeIndex];
if (NeighborTriangle)
{
MPE_PolyMarkConstrainedEdgePoints(NeighborTriangle, EdgeP, EdgeQ);
}
Result = MPE_TRUE;
}
else
{
Result = MPE_FALSE;
}
return Result;
}
internal_static
bxx MPE_AngleExceeds90Degrees(MPEPolyPoint* Origin, MPEPolyPoint* PointA, MPEPolyPoint* PointB)
{
poly_float AX = PointA->X - Origin->X;
poly_float AY = PointA->Y - Origin->Y;
poly_float BX = PointB->X - Origin->X;
poly_float BY = PointB->Y - Origin->Y;
poly_float DotProduct = AX * BX + AY * BY;
bxx Result = DotProduct < 0;
return Result;
}
internal_static
bxx MPE_AngleExceedsPlus90DegreesOrIsNegative(MPEPolyPoint* Origin, MPEPolyPoint* PointA, MPEPolyPoint* PointB)
{
poly_float AX = PointA->X - Origin->X;
poly_float AY = PointA->Y - Origin->Y;
poly_float BX = PointB->X - Origin->X;
poly_float BY = PointB->Y - Origin->Y;
poly_float DotProduct = AX * BX + AY * BY;
poly_float Direction = AX * BY - AY * BX;
bxx Result = DotProduct < 0 || Direction < 0;
return Result;
}
internal_static
bxx MPE_LargeHole_DontFill(MPEPolyNode* Node)
{
bxx Result;
MPEPolyNode* NextNode = Node->Next;
MPEPolyNode* PrevNode = Node->Prev;
if (!MPE_AngleExceeds90Degrees(Node->Point, NextNode->Point, PrevNode->Point))
{
Result = MPE_FALSE;
}
else
{
// Check additional points on front.
MPEPolyNode* Next2Node = NextNode->Next;
// "..Plus.." because only want angles on same side as MPEPolyPoint being added.
if (Next2Node && !MPE_AngleExceedsPlus90DegreesOrIsNegative(Node->Point, Next2Node->Point, PrevNode->Point))
{
Result = MPE_FALSE;
}
else
{
MPEPolyNode* Prev2Node = PrevNode->Prev;
// "..Plus.." because only want angles on same side as MPEPolyPoint being added.
if (Prev2Node && !MPE_AngleExceedsPlus90DegreesOrIsNegative(Node->Point, NextNode->Point, Prev2Node->Point))
{
Result = MPE_FALSE;
}
else
{
Result = MPE_TRUE;
}
}
}
return Result;
}
internal_static MPE_INLINE
bxx MPE_PolyShouldFillBasin(MPEPolyNode* Node)
{
poly_float AX = Node->Point->X;
poly_float AY = Node->Point->Y;
poly_float BX = Node->Next->Next->Point->X;
poly_float BY = Node->Next->Next->Point->Y;
poly_float DotProduct = AX * BX + AY * BY;
poly_float Direction = AX * BY - AY * BX;
bxx Result = DotProduct < 0 || Direction > 0;
return Result;
}
/**
* Requirement:
* 1. a,b and c form a triangle.
* 2. a and d is know to be on opposite side of bc
* a
* +
* / \
* / \
* b/ \c
* +-------+
* / d \
* / \
* Fact: d has to be in area B to have a chance to be inside the circle formed by
* a,b and c
* d is outside B if MPE_PolyOrient2D(a,b,d) or MPE_PolyOrient2D(c,a,d) is CW
* This preknowledge gives us a way to optimize the incircle test
*/
internal_static
bxx MPE_PolyInCircle(MPEPolyPoint* PointA, MPEPolyPoint* PointB, MPEPolyPoint* pc, MPEPolyPoint* pd)
{
bxx Result;
poly_float adx = PointA->X - pd->X;
poly_float ady = PointA->Y - pd->Y;
poly_float bdx = PointB->X - pd->X;
poly_float bdy = PointB->Y - pd->Y;
poly_float adxbdy = adx * bdy;
poly_float bdxady = bdx * ady;
poly_float oabd = adxbdy - bdxady;
if (oabd <= 0)
{
Result = MPE_FALSE;
}
else
{
poly_float cdx = pc->X - pd->X;
poly_float cdy = pc->Y - pd->Y;
poly_float cdxady = cdx * ady;
poly_float adxcdy = adx * cdy;
poly_float ocad = cdxady - adxcdy;
if (ocad <= 0)
{
Result = MPE_FALSE;
}
else
{
poly_float bdxcdy = bdx * cdy;
poly_float cdxbdy = cdx * bdy;