-
Notifications
You must be signed in to change notification settings - Fork 7
/
jc_voronoi.h
1786 lines (1494 loc) · 52.6 KB
/
jc_voronoi.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
// Copyright (c) 2015-2023 Mathias Westerdahl
// For LICENSE (MIT), USAGE or HISTORY, see bottom of file
#ifndef JC_VORONOI_H
#define JC_VORONOI_H
#include <math.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <float.h>
#include <assert.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef JCV_REAL_TYPE
#define JCV_REAL_TYPE float
#endif
#ifndef JCV_REAL_TYPE_EPSILON
#define JCV_REAL_TYPE_EPSILON FLT_EPSILON
#endif
#ifndef JCV_ATAN2
#define JCV_ATAN2(_Y_, _X_) atan2f(_Y_, _X_)
#endif
#ifndef JCV_SQRT
#define JCV_SQRT(_X_) sqrtf(_X_)
#endif
#ifndef JCV_PI
#define JCV_PI 3.14159265358979323846264338327950288f
#endif
#ifndef JCV_FLT_MAX
#define JCV_FLT_MAX 3.402823466e+38F
#endif
#ifndef JCV_EDGE_INTERSECT_THRESHOLD
// Fix for Issue #40
#define JCV_EDGE_INTERSECT_THRESHOLD 1.0e-10F
#endif
typedef JCV_REAL_TYPE jcv_real;
typedef struct jcv_point_ jcv_point;
typedef struct jcv_rect_ jcv_rect;
typedef struct jcv_site_ jcv_site;
typedef struct jcv_edge_ jcv_edge;
typedef struct jcv_graphedge_ jcv_graphedge;
typedef struct jcv_delauney_edge_ jcv_delauney_edge;
typedef struct jcv_delauney_iter_ jcv_delauney_iter;
typedef struct jcv_diagram_ jcv_diagram;
typedef struct jcv_clipper_ jcv_clipper;
typedef struct jcv_context_internal_ jcv_context_internal;
/// Tests if a point is inside the final shape
typedef int (*jcv_clip_test_point_fn)(const jcv_clipper* clipper, const jcv_point p);
/** Given an edge, and the clipper, calculates the e->pos[0] and e->pos[1]
* Returns 0 if not successful
*/
typedef int (*jcv_clip_edge_fn)(const jcv_clipper* clipper, jcv_edge* e);
/** Given the clipper, the site and the last edge,
* closes any gaps in the polygon by adding new edges that follow the bounding shape
* The internal context is use when allocating new edges.
*/
typedef void (*jcv_clip_fillgap_fn)(const jcv_clipper* clipper, jcv_context_internal* allocator, jcv_site* s);
/**
* Uses malloc
* If a clipper is not supplied, a default box clipper will be used
* If rect is null, an automatic bounding box is calculated, with an extra padding of 10 units
* All points will be culled against the bounding rect, and all edges will be clipped against it.
*/
extern void jcv_diagram_generate( int num_points, const jcv_point* points, const jcv_rect* rect, const jcv_clipper* clipper, jcv_diagram* diagram );
typedef void* (*FJCVAllocFn)(void* userctx, size_t size);
typedef void (*FJCVFreeFn)(void* userctx, void* p);
// Same as above, but allows the client to use a custom allocator
extern void jcv_diagram_generate_useralloc( int num_points, const jcv_point* points, const jcv_rect* rect, const jcv_clipper* clipper, void* userallocctx, FJCVAllocFn allocfn, FJCVFreeFn freefn, jcv_diagram* diagram );
// Uses free (or the registered custom free function)
extern void jcv_diagram_free( jcv_diagram* diagram );
// Returns an array of sites, where each index is the same as the original input point array.
extern const jcv_site* jcv_diagram_get_sites( const jcv_diagram* diagram );
// Returns a linked list of all the voronoi edges
// excluding the ones that lie on the borders of the bounding box.
// For a full list of edges, you need to iterate over the sites, and their graph edges.
extern const jcv_edge* jcv_diagram_get_edges( const jcv_diagram* diagram );
// Iterates over a list of edges, skipping invalid edges (where p0==p1)
extern const jcv_edge* jcv_diagram_get_next_edge( const jcv_edge* edge );
// Creates an iterator over the delauney edges of a voronoi diagram
void jcv_delauney_begin( const jcv_diagram* diagram, jcv_delauney_iter* iter );
// Steps the iterator and returns the next edge
// Returns 0 when there are no more edges
int jcv_delauney_next( jcv_delauney_iter* iter, jcv_delauney_edge* next );
// For the default clipper
extern int jcv_boxshape_test(const jcv_clipper* clipper, const jcv_point p);
extern int jcv_boxshape_clip(const jcv_clipper* clipper, jcv_edge* e);
extern void jcv_boxshape_fillgaps(const jcv_clipper* clipper, jcv_context_internal* allocator, jcv_site* s);
#pragma pack(push, 1)
struct jcv_point_
{
jcv_real x;
jcv_real y;
};
struct jcv_graphedge_
{
struct jcv_graphedge_* next;
struct jcv_edge_* edge;
struct jcv_site_* neighbor;
jcv_point pos[2];
jcv_real angle;
};
struct jcv_site_
{
jcv_point p;
int index; // Index into the original list of points
jcv_graphedge* edges; // The half edges owned by the cell
};
// The coefficients a, b and c are from the general line equation: ax * by + c = 0
struct jcv_edge_
{
struct jcv_edge_* next;
jcv_site* sites[2];
jcv_point pos[2];
jcv_real a;
jcv_real b;
jcv_real c;
};
struct jcv_delauney_iter_
{
const jcv_edge* sentinel;
const jcv_edge* current;
};
struct jcv_delauney_edge_
{
const jcv_edge* edge; // The voronoi edge separating the two sites
const jcv_site* sites[2];
jcv_point pos[2]; // the positions of the two sites
};
struct jcv_rect_
{
jcv_point min;
jcv_point max;
};
struct jcv_clipper_
{
jcv_clip_test_point_fn test_fn;
jcv_clip_edge_fn clip_fn;
jcv_clip_fillgap_fn fill_fn;
jcv_point min; // The bounding rect min
jcv_point max; // The bounding rect max
void* ctx; // User defined context
};
struct jcv_diagram_
{
jcv_context_internal* internal;
int numsites;
jcv_point min;
jcv_point max;
};
#pragma pack(pop)
#ifdef __cplusplus
}
#endif
#endif // JC_VORONOI_H
#ifdef JC_VORONOI_IMPLEMENTATION
#undef JC_VORONOI_IMPLEMENTATION
#include <memory.h>
// INTERNAL FUNCTIONS
#if defined(_MSC_VER) && !defined(__cplusplus)
#define inline __inline
#endif
static const int JCV_DIRECTION_LEFT = 0;
static const int JCV_DIRECTION_RIGHT = 1;
static const jcv_real JCV_INVALID_VALUE = (jcv_real)-JCV_FLT_MAX;
// jcv_real
static inline jcv_real jcv_abs(jcv_real v) {
return (v < 0) ? -v : v;
}
static inline int jcv_real_eq(jcv_real a, jcv_real b)
{
return jcv_abs(a - b) < JCV_REAL_TYPE_EPSILON;
}
static inline jcv_real jcv_real_to_int(jcv_real v) {
return (sizeof(jcv_real) == 4) ? (jcv_real)(int)v : (jcv_real)(long long)v;
}
// Only used for calculating the initial bounding box
static inline jcv_real jcv_floor(jcv_real v) {
jcv_real i = jcv_real_to_int(v);
return (v < i) ? i - 1 : i;
}
// Only used for calculating the initial bounding box
static inline jcv_real jcv_ceil(jcv_real v) {
jcv_real i = jcv_real_to_int(v);
return (v > i) ? i + 1 : i;
}
static inline jcv_real jcv_min(jcv_real a, jcv_real b) {
return a < b ? a : b;
}
static inline jcv_real jcv_max(jcv_real a, jcv_real b) {
return a > b ? a : b;
}
// jcv_point
static inline int jcv_point_cmp(const void* p1, const void* p2)
{
const jcv_point* s1 = (const jcv_point*) p1;
const jcv_point* s2 = (const jcv_point*) p2;
return (s1->y != s2->y) ? (s1->y < s2->y ? -1 : 1) : (s1->x < s2->x ? -1 : 1);
}
static inline int jcv_point_less( const jcv_point* pt1, const jcv_point* pt2 )
{
return (pt1->y == pt2->y) ? (pt1->x < pt2->x) : pt1->y < pt2->y;
}
static inline int jcv_point_eq( const jcv_point* pt1, const jcv_point* pt2 )
{
return jcv_real_eq(pt1->y, pt2->y) && jcv_real_eq(pt1->x, pt2->x);
}
static inline int jcv_point_on_box_edge( const jcv_point* pt, const jcv_point* min, const jcv_point* max )
{
return pt->x == min->x || pt->y == min->y || pt->x == max->x || pt->y == max->y;
}
// corners
static const int JCV_EDGE_LEFT = 1;
static const int JCV_EDGE_RIGHT = 2;
static const int JCV_EDGE_BOTTOM = 4;
static const int JCV_EDGE_TOP = 8;
static const int JCV_CORNER_NONE = 0;
static const int JCV_CORNER_TOP_LEFT = 1;
static const int JCV_CORNER_BOTTOM_LEFT = 2;
static const int JCV_CORNER_BOTTOM_RIGHT = 3;
static const int JCV_CORNER_TOP_RIGHT = 4;
static inline int jcv_get_edge_flags( const jcv_point* pt, const jcv_point* min, const jcv_point* max )
{
int flags = 0;
if (pt->x == min->x) flags |= JCV_EDGE_LEFT;
else if (pt->x == max->x) flags |= JCV_EDGE_RIGHT;
if (pt->y == min->y) flags |= JCV_EDGE_BOTTOM;
else if (pt->y == max->y) flags |= JCV_EDGE_TOP;
return flags;
}
static inline int jcv_edge_flags_to_corner(int edge_flags)
{
#define TEST_FLAGS(_FLAGS, _RETVAL) if ( (_FLAGS) == edge_flags ) return _RETVAL
TEST_FLAGS(JCV_EDGE_TOP|JCV_EDGE_LEFT, JCV_CORNER_TOP_LEFT);
TEST_FLAGS(JCV_EDGE_TOP|JCV_EDGE_RIGHT, JCV_CORNER_TOP_RIGHT);
TEST_FLAGS(JCV_EDGE_BOTTOM|JCV_EDGE_LEFT, JCV_CORNER_BOTTOM_LEFT);
TEST_FLAGS(JCV_EDGE_BOTTOM|JCV_EDGE_RIGHT, JCV_CORNER_BOTTOM_RIGHT);
#undef TEST_FLAGS
return 0;
}
static inline int jcv_is_corner(int corner)
{
return corner != 0;
}
static inline int jcv_corner_rotate_90(int corner)
{
corner--;
corner = (corner+1)%4;
return corner + 1;
}
static inline jcv_point jcv_corner_to_point(int corner, const jcv_point* min, const jcv_point* max )
{
jcv_point p;
if (corner == JCV_CORNER_TOP_LEFT) { p.x = min->x; p.y = max->y; }
else if (corner == JCV_CORNER_TOP_RIGHT) { p.x = max->x; p.y = max->y; }
else if (corner == JCV_CORNER_BOTTOM_LEFT) { p.x = min->x; p.y = min->y; }
else if (corner == JCV_CORNER_BOTTOM_RIGHT) { p.x = max->x; p.y = min->y; }
else { p.x = JCV_INVALID_VALUE; p.y = JCV_INVALID_VALUE; }
return p;
}
static inline jcv_real jcv_point_dist_sq( const jcv_point* pt1, const jcv_point* pt2)
{
jcv_real diffx = pt1->x - pt2->x;
jcv_real diffy = pt1->y - pt2->y;
return diffx * diffx + diffy * diffy;
}
static inline jcv_real jcv_point_dist( const jcv_point* pt1, const jcv_point* pt2 )
{
return (jcv_real)(JCV_SQRT(jcv_point_dist_sq(pt1, pt2)));
}
// Structs
#pragma pack(push, 1)
typedef struct jcv_halfedge_
{
jcv_edge* edge;
struct jcv_halfedge_* left;
struct jcv_halfedge_* right;
jcv_point vertex;
jcv_real y;
int direction; // 0=left, 1=right
int pqpos;
} jcv_halfedge;
typedef struct jcv_memoryblock_
{
size_t sizefree;
struct jcv_memoryblock_* next;
char* memory;
} jcv_memoryblock;
typedef int (*FJCVPriorityQueuePrint)(const void* node, int pos);
typedef struct jcv_priorityqueue_
{
// Implements a binary heap
int maxnumitems;
int numitems;
void** items;
} jcv_priorityqueue;
struct jcv_context_internal_
{
void* mem;
jcv_edge* edges;
jcv_halfedge* beachline_start;
jcv_halfedge* beachline_end;
jcv_halfedge* last_inserted;
jcv_priorityqueue* eventqueue;
jcv_site* sites;
jcv_site* bottomsite;
int numsites;
int currentsite;
int _padding;
jcv_memoryblock* memblocks;
jcv_edge* edgepool;
jcv_halfedge* halfedgepool;
void** eventmem;
jcv_clipper clipper;
void* memctx; // Given by the user
FJCVAllocFn alloc;
FJCVFreeFn free;
jcv_rect rect;
};
#pragma pack(pop)
void jcv_diagram_free( jcv_diagram* d )
{
jcv_context_internal* internal = d->internal;
void* memctx = internal->memctx;
FJCVFreeFn freefn = internal->free;
while(internal->memblocks)
{
jcv_memoryblock* p = internal->memblocks;
internal->memblocks = internal->memblocks->next;
freefn( memctx, p );
}
freefn( memctx, internal->mem );
}
const jcv_site* jcv_diagram_get_sites( const jcv_diagram* diagram )
{
return diagram->internal->sites;
}
const jcv_edge* jcv_diagram_get_edges( const jcv_diagram* diagram )
{
jcv_edge e;
e.next = diagram->internal->edges;
return jcv_diagram_get_next_edge(&e);
}
const jcv_edge* jcv_diagram_get_next_edge( const jcv_edge* edge )
{
const jcv_edge* e = edge->next;
while (e != 0 && jcv_point_eq(&e->pos[0], &e->pos[1])) {
e = e->next;
}
return e;
}
void jcv_delauney_begin( const jcv_diagram* diagram, jcv_delauney_iter* iter )
{
iter->current = 0;
iter->sentinel = jcv_diagram_get_edges(diagram);
}
int jcv_delauney_next( jcv_delauney_iter* iter, jcv_delauney_edge* next )
{
if (iter->sentinel)
{
iter->current = iter->sentinel;
iter->sentinel = 0;
}
else {
// Note: If we use the raw edges, we still get a proper delauney triangulation
// However, the result looks less relevant to the cells contained within the bounding box
// E.g. some cells that look isolated from each other, suddenly still are connected,
// because they share an edge outside of the bounding box
iter->current = jcv_diagram_get_next_edge(iter->current);
}
while (iter->current && (iter->current->sites[0] == 0 || iter->current->sites[1] == 0))
{
iter->current = jcv_diagram_get_next_edge(iter->current);
}
if (!iter->current)
return 0;
next->edge = iter->current;
next->sites[0] = next->edge->sites[0];
next->sites[1] = next->edge->sites[1];
next->pos[0] = next->sites[0]->p;
next->pos[1] = next->sites[1]->p;
return 1;
}
static inline void* jcv_align(void* value, size_t alignment)
{
return (void*) (((uintptr_t) value + (alignment-1)) & ~(alignment-1));
}
static void* jcv_alloc(jcv_context_internal* internal, size_t size)
{
if( !internal->memblocks || internal->memblocks->sizefree < (size+sizeof(void*)) )
{
size_t blocksize = 16 * 1024;
jcv_memoryblock* block = (jcv_memoryblock*)internal->alloc( internal->memctx, blocksize );
size_t offset = sizeof(jcv_memoryblock);
block->sizefree = blocksize - offset;
block->next = internal->memblocks;
block->memory = ((char*)block) + offset;
internal->memblocks = block;
}
void* p_raw = internal->memblocks->memory;
void* p_aligned = jcv_align(p_raw, sizeof(void*));
size += (uintptr_t)p_aligned - (uintptr_t)p_raw;
internal->memblocks->memory += size;
internal->memblocks->sizefree -= size;
return p_aligned;
}
static jcv_edge* jcv_alloc_edge(jcv_context_internal* internal)
{
return (jcv_edge*)jcv_alloc(internal, sizeof(jcv_edge));
}
static jcv_halfedge* jcv_alloc_halfedge(jcv_context_internal* internal)
{
if( internal->halfedgepool )
{
jcv_halfedge* edge = internal->halfedgepool;
internal->halfedgepool = internal->halfedgepool->right;
return edge;
}
return (jcv_halfedge*)jcv_alloc(internal, sizeof(jcv_halfedge));
}
static jcv_graphedge* jcv_alloc_graphedge(jcv_context_internal* internal)
{
return (jcv_graphedge*)jcv_alloc(internal, sizeof(jcv_graphedge));
}
static void* jcv_alloc_fn(void* memctx, size_t size)
{
(void)memctx;
return malloc(size);
}
static void jcv_free_fn(void* memctx, void* p)
{
(void)memctx;
free(p);
}
// jcv_edge
static inline int jcv_is_valid(const jcv_point* p)
{
return (p->x != JCV_INVALID_VALUE || p->y != JCV_INVALID_VALUE) ? 1 : 0;
}
static void jcv_edge_create(jcv_edge* e, jcv_site* s1, jcv_site* s2)
{
e->next = 0;
e->sites[0] = s1;
e->sites[1] = s2;
e->pos[0].x = JCV_INVALID_VALUE;
e->pos[0].y = JCV_INVALID_VALUE;
e->pos[1].x = JCV_INVALID_VALUE;
e->pos[1].y = JCV_INVALID_VALUE;
// Create line equation between S1 and S2:
// jcv_real a = -1 * (s2->p.y - s1->p.y);
// jcv_real b = s2->p.x - s1->p.x;
// //jcv_real c = -1 * (s2->p.x - s1->p.x) * s1->p.y + (s2->p.y - s1->p.y) * s1->p.x;
//
// // create perpendicular line
// jcv_real pa = b;
// jcv_real pb = -a;
// //jcv_real pc = pa * s1->p.x + pb * s1->p.y;
//
// // Move to the mid point
// jcv_real mx = s1->p.x + dx * jcv_real(0.5);
// jcv_real my = s1->p.y + dy * jcv_real(0.5);
// jcv_real pc = ( pa * mx + pb * my );
jcv_real dx = s2->p.x - s1->p.x;
jcv_real dy = s2->p.y - s1->p.y;
int dx_is_larger = (dx*dx) > (dy*dy); // instead of fabs
// Simplify it, using dx and dy
e->c = dx * (s1->p.x + dx * (jcv_real)0.5) + dy * (s1->p.y + dy * (jcv_real)0.5);
if( dx_is_larger )
{
e->a = (jcv_real)1;
e->b = dy / dx;
e->c /= dx;
}
else
{
e->a = dx / dy;
e->b = (jcv_real)1;
e->c /= dy;
}
}
// CLIPPING
int jcv_boxshape_test(const jcv_clipper* clipper, const jcv_point p)
{
return p.x >= clipper->min.x && p.x <= clipper->max.x &&
p.y >= clipper->min.y && p.y <= clipper->max.y;
}
// The line equation: ax + by + c = 0
// see jcv_edge_create
int jcv_boxshape_clip(const jcv_clipper* clipper, jcv_edge* e)
{
jcv_real pxmin = clipper->min.x;
jcv_real pxmax = clipper->max.x;
jcv_real pymin = clipper->min.y;
jcv_real pymax = clipper->max.y;
jcv_real x1, y1, x2, y2;
jcv_point* s1;
jcv_point* s2;
if (e->a == (jcv_real)1 && e->b >= (jcv_real)0)
{
s1 = jcv_is_valid(&e->pos[1]) ? &e->pos[1] : 0;
s2 = jcv_is_valid(&e->pos[0]) ? &e->pos[0] : 0;
}
else
{
s1 = jcv_is_valid(&e->pos[0]) ? &e->pos[0] : 0;
s2 = jcv_is_valid(&e->pos[1]) ? &e->pos[1] : 0;
}
if (e->a == (jcv_real)1) // delta x is larger
{
y1 = pymin;
if (s1 != 0 && s1->y > pymin)
{
y1 = s1->y;
}
if( y1 > pymax )
{
y1 = pymax;
}
x1 = e->c - e->b * y1;
y2 = pymax;
if (s2 != 0 && s2->y < pymax)
y2 = s2->y;
if( y2 < pymin )
{
y2 = pymin;
}
x2 = (e->c) - (e->b) * y2;
// Never occurs according to lcov
// if( ((x1 > pxmax) & (x2 > pxmax)) | ((x1 < pxmin) & (x2 < pxmin)) )
// {
// return 0;
// }
if (x1 > pxmax)
{
x1 = pxmax;
y1 = (e->c - x1) / e->b;
}
else if (x1 < pxmin)
{
x1 = pxmin;
y1 = (e->c - x1) / e->b;
}
if (x2 > pxmax)
{
x2 = pxmax;
y2 = (e->c - x2) / e->b;
}
else if (x2 < pxmin)
{
x2 = pxmin;
y2 = (e->c - x2) / e->b;
}
}
else // delta y is larger
{
x1 = pxmin;
if( s1 != 0 && s1->x > pxmin )
x1 = s1->x;
if( x1 > pxmax )
{
x1 = pxmax;
}
y1 = e->c - e->a * x1;
x2 = pxmax;
if( s2 != 0 && s2->x < pxmax )
x2 = s2->x;
if( x2 < pxmin )
{
x2 = pxmin;
}
y2 = e->c - e->a * x2;
// Never occurs according to lcov
// if( ((y1 > pymax) & (y2 > pymax)) | ((y1 < pymin) & (y2 < pymin)) )
// {
// return 0;
// }
if( y1 > pymax )
{
y1 = pymax;
x1 = (e->c - y1) / e->a;
}
else if( y1 < pymin )
{
y1 = pymin;
x1 = (e->c - y1) / e->a;
}
if( y2 > pymax )
{
y2 = pymax;
x2 = (e->c - y2) / e->a;
}
else if( y2 < pymin )
{
y2 = pymin;
x2 = (e->c - y2) / e->a;
}
}
e->pos[0].x = x1;
e->pos[0].y = y1;
e->pos[1].x = x2;
e->pos[1].y = y2;
// If the two points are equal, the result is invalid
return (x1 == x2 && y1 == y2) ? 0 : 1;
}
// The line equation: ax + by + c = 0
// see jcv_edge_create
static int jcv_edge_clipline(jcv_context_internal* internal, jcv_edge* e)
{
return internal->clipper.clip_fn(&internal->clipper, e);
}
static jcv_edge* jcv_edge_new(jcv_context_internal* internal, jcv_site* s1, jcv_site* s2)
{
jcv_edge* e = jcv_alloc_edge(internal);
jcv_edge_create(e, s1, s2);
return e;
}
// jcv_halfedge
static void jcv_halfedge_link(jcv_halfedge* edge, jcv_halfedge* newedge)
{
newedge->left = edge;
newedge->right = edge->right;
edge->right->left = newedge;
edge->right = newedge;
}
static inline void jcv_halfedge_unlink(jcv_halfedge* he)
{
he->left->right = he->right;
he->right->left = he->left;
he->left = 0;
he->right = 0;
}
static inline jcv_halfedge* jcv_halfedge_new(jcv_context_internal* internal, jcv_edge* e, int direction)
{
jcv_halfedge* he = jcv_alloc_halfedge(internal);
he->edge = e;
he->left = 0;
he->right = 0;
he->direction = direction;
he->pqpos = 0;
// These are set outside
//he->y
//he->vertex
return he;
}
static void jcv_halfedge_delete(jcv_context_internal* internal, jcv_halfedge* he)
{
he->right = internal->halfedgepool;
internal->halfedgepool = he;
}
static inline jcv_site* jcv_halfedge_leftsite(const jcv_halfedge* he)
{
return he->edge->sites[he->direction];
}
static inline jcv_site* jcv_halfedge_rightsite(const jcv_halfedge* he)
{
return he->edge ? he->edge->sites[1 - he->direction] : 0;
}
static int jcv_halfedge_rightof(const jcv_halfedge* he, const jcv_point* p)
{
const jcv_edge* e = he->edge;
const jcv_site* topsite = e->sites[1];
int right_of_site = (p->x > topsite->p.x) ? 1 : 0;
if (right_of_site && he->direction == JCV_DIRECTION_LEFT)
return 1;
if (!right_of_site && he->direction == JCV_DIRECTION_RIGHT)
return 0;
jcv_real dxp, dyp, dxs, t1, t2, t3, yl;
int above;
if (e->a == (jcv_real)1)
{
dyp = p->y - topsite->p.y;
dxp = p->x - topsite->p.x;
int fast = 0;
if( (!right_of_site & (e->b < (jcv_real)0)) | (right_of_site & (e->b >= (jcv_real)0)) )
{
above = dyp >= e->b * dxp;
fast = above;
}
else
{
above = (p->x + p->y * e->b) > e->c;
if (e->b < (jcv_real)0)
above = !above;
if (!above)
fast = 1;
}
if (!fast)
{
dxs = topsite->p.x - e->sites[0]->p.x;
above = e->b * (dxp * dxp - dyp * dyp)
< dxs * dyp * ((jcv_real)1 + (jcv_real)2 * dxp / dxs + e->b * e->b);
if (e->b < (jcv_real)0)
above = !above;
}
}
else // e->b == 1
{
yl = e->c - e->a * p->x;
t1 = p->y - yl;
t2 = p->x - topsite->p.x;
t3 = yl - topsite->p.y;
above = t1 * t1 > (t2 * t2 + t3 * t3);
}
return (he->direction == JCV_DIRECTION_LEFT ? above : !above);
}
// Keeps the priority queue sorted with events sorted in ascending order
// Return 1 if the edges needs to be swapped
static inline int jcv_halfedge_compare( const jcv_halfedge* he1, const jcv_halfedge* he2 )
{
return (he1->y == he2->y) ? he1->vertex.x > he2->vertex.x : he1->y > he2->y;
}
static int jcv_halfedge_intersect(const jcv_halfedge* he1, const jcv_halfedge* he2, jcv_point* out)
{
const jcv_edge* e1 = he1->edge;
const jcv_edge* e2 = he2->edge;
jcv_real d = e1->a * e2->b - e1->b * e2->a;
if( ((jcv_real)-JCV_EDGE_INTERSECT_THRESHOLD < d && d < (jcv_real)JCV_EDGE_INTERSECT_THRESHOLD) )
{
return 0;
}
out->x = (e1->c * e2->b - e1->b * e2->c) / d;
out->y = (e1->a * e2->c - e1->c * e2->a) / d;
const jcv_edge* e;
const jcv_halfedge* he;
if( jcv_point_less( &e1->sites[1]->p, &e2->sites[1]->p) )
{
he = he1;
e = e1;
}
else
{
he = he2;
e = e2;
}
int right_of_site = out->x >= e->sites[1]->p.x;
if ((right_of_site && he->direction == JCV_DIRECTION_LEFT) || (!right_of_site && he->direction == JCV_DIRECTION_RIGHT))
{
return 0;
}
return 1;
}
// Priority queue
static int jcv_pq_moveup(jcv_priorityqueue* pq, int pos)
{
jcv_halfedge** items = (jcv_halfedge**)pq->items;
jcv_halfedge* node = items[pos];
for( int parent = (pos >> 1);
pos > 1 && jcv_halfedge_compare(items[parent], node);
pos = parent, parent = parent >> 1)
{
items[pos] = items[parent];
items[pos]->pqpos = pos;
}
node->pqpos = pos;
items[pos] = node;
return pos;
}
static int jcv_pq_maxchild(jcv_priorityqueue* pq, int pos)
{
int child = pos << 1;
if( child >= pq->numitems )
return 0;
jcv_halfedge** items = (jcv_halfedge**)pq->items;
if( (child + 1) < pq->numitems && jcv_halfedge_compare(items[child], items[child+1]) )
return child+1;
return child;
}
static int jcv_pq_movedown(jcv_priorityqueue* pq, int pos)
{
jcv_halfedge** items = (jcv_halfedge**)pq->items;
jcv_halfedge* node = items[pos];
int child = jcv_pq_maxchild(pq, pos);
while( child && jcv_halfedge_compare(node, items[child]) )
{
items[pos] = items[child];
items[pos]->pqpos = pos;
pos = child;
child = jcv_pq_maxchild(pq, pos);
}
items[pos] = node;
items[pos]->pqpos = pos;
return pos;
}
static void jcv_pq_create(jcv_priorityqueue* pq, int capacity, void** buffer)
{
pq->maxnumitems = capacity;
pq->numitems = 1;
pq->items = buffer;
}
static int jcv_pq_empty(jcv_priorityqueue* pq)
{
return pq->numitems == 1 ? 1 : 0;
}
static int jcv_pq_push(jcv_priorityqueue* pq, void* node)
{
assert(pq->numitems < pq->maxnumitems);
int n = pq->numitems++;
pq->items[n] = node;
return jcv_pq_moveup(pq, n);
}
static void* jcv_pq_pop(jcv_priorityqueue* pq)
{
void* node = pq->items[1];
pq->items[1] = pq->items[--pq->numitems];
jcv_pq_movedown(pq, 1);
return node;
}
static void* jcv_pq_top(jcv_priorityqueue* pq)
{
return pq->items[1];
}
static void jcv_pq_remove(jcv_priorityqueue* pq, jcv_halfedge* node)
{
if( pq->numitems == 1 )
return;
int pos = node->pqpos;
if( pos == 0 )
return;
jcv_halfedge** items = (jcv_halfedge**)pq->items;
items[pos] = items[--pq->numitems];
if( jcv_halfedge_compare( node, items[pos] ) )
jcv_pq_moveup( pq, pos );
else
jcv_pq_movedown( pq, pos );
node->pqpos = pos;
}
// internal functions
static inline jcv_site* jcv_nextsite(jcv_context_internal* internal)
{
return (internal->currentsite < internal->numsites) ? &internal->sites[internal->currentsite++] : 0;
}
static jcv_halfedge* jcv_get_edge_above_x(jcv_context_internal* internal, const jcv_point* p)
{
// Gets the arc on the beach line at the x coordinate (i.e. right above the new site event)
// A good guess it's close by (Can be optimized)
jcv_halfedge* he = internal->last_inserted;
if( !he )
{
if( p->x < (internal->rect.max.x - internal->rect.min.x) / 2 )
he = internal->beachline_start;
else
he = internal->beachline_end;
}
//
if( he == internal->beachline_start || (he != internal->beachline_end && jcv_halfedge_rightof(he, p)) )
{