-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtg.c
15932 lines (14930 loc) · 523 KB
/
tg.c
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
// https://github.com/tidwall/tg
//
// Copyright 2023 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
#include <math.h>
#include <float.h>
#include <stdarg.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
/******************************************************************************
Implementation Notes:
- Right-hand-rule is not required for polygons.
- The "properties" GeoJSON field is not required for Features.
- All polygons with at least 32 points are indexed automatically (TG_NATURAL).
Developer notes:
- This tg.c source file includes the entire tg library. (amalgamation)
- All dependencies are embedded between the BEGIN/END tags.
- Do not edit a dependency directly in this file. Instead edit the file in
the deps directory and then run deps/embed.sh to replace out its code in
this file.
*******************************************************************************/
#ifdef TG_STATIC
#define TG_EXTERN static
#endif
#ifndef TG_EXTERN
#define TG_EXTERN
#endif
struct tg_point {
double x;
double y;
};
struct tg_segment {
struct tg_point a;
struct tg_point b;
};
struct tg_rect {
struct tg_point min;
struct tg_point max;
};
struct tg_line; ///< Find the description in the tg.c file.
struct tg_ring; ///< Find the description in the tg.c file.
struct tg_poly; ///< Find the description in the tg.c file.
struct tg_geom; ///< Find the description in the tg.c file.
enum tg_geom_type {
TG_POINT = 1, ///< Point
TG_LINESTRING = 2, ///< LineString
TG_POLYGON = 3, ///< Polygon
TG_MULTIPOINT = 4, ///< MultiPoint, collection of points
TG_MULTILINESTRING = 5, ///< MultiLineString, collection of linestrings
TG_MULTIPOLYGON = 6, ///< MultiPolygon, collection of polygons
TG_GEOMETRYCOLLECTION = 7, ///< GeometryCollection, collection of geometries
};
enum tg_index {
TG_DEFAULT, ///< default is TG_NATURAL or tg_env_set_default_index().
TG_NONE, ///< no indexing available, or disabled.
TG_NATURAL, ///< indexing with natural ring order, for rings/lines
TG_YSTRIPES, ///< indexing using segment striping, rings only
};
enum tg_raycast_result {
TG_OUT, // point is above, below, or to the right of the segment
TG_IN, // point is to the left (and inside the vertical bounds)
TG_ON, // point is on the segment
};
// Public methods. These should also be in the tg.h file
TG_EXTERN struct tg_geom *tg_geom_new_point(struct tg_point point);
TG_EXTERN struct tg_geom *tg_geom_new_linestring(const struct tg_line *line);
TG_EXTERN struct tg_geom *tg_geom_new_polygon(const struct tg_poly *poly);
TG_EXTERN struct tg_geom *tg_geom_new_multipoint(const struct tg_point *points,
int npoints);
TG_EXTERN struct tg_geom *tg_geom_new_multilinestring(
const struct tg_line *const lines[], int nlines);
TG_EXTERN struct tg_geom *tg_geom_new_multipolygon(
const struct tg_poly *const polys[], int npolys);
TG_EXTERN struct tg_geom *tg_geom_new_geometrycollection(
const struct tg_geom *const geoms[], int ngeoms);
TG_EXTERN struct tg_geom *tg_geom_new_error(const char *errmsg);
TG_EXTERN struct tg_geom *tg_geom_clone(const struct tg_geom *geom);
TG_EXTERN struct tg_geom *tg_geom_copy(const struct tg_geom *geom);
TG_EXTERN void tg_geom_free(struct tg_geom *geom);
TG_EXTERN enum tg_geom_type tg_geom_typeof(const struct tg_geom *geom);
TG_EXTERN const char *tg_geom_type_string(enum tg_geom_type type);
TG_EXTERN struct tg_rect tg_geom_rect(const struct tg_geom *geom);
TG_EXTERN bool tg_geom_is_feature(const struct tg_geom *geom);
TG_EXTERN bool tg_geom_is_featurecollection(const struct tg_geom *geom);
TG_EXTERN struct tg_point tg_geom_point(const struct tg_geom *geom);
TG_EXTERN const struct tg_line *tg_geom_line(const struct tg_geom *geom);
TG_EXTERN const struct tg_poly *tg_geom_poly(const struct tg_geom *geom);
TG_EXTERN int tg_geom_num_points(const struct tg_geom *geom);
TG_EXTERN struct tg_point tg_geom_point_at(const struct tg_geom *geom,
int index);
TG_EXTERN int tg_geom_num_lines(const struct tg_geom *geom);
TG_EXTERN const struct tg_line *tg_geom_line_at(const struct tg_geom *geom,
int index);
TG_EXTERN int tg_geom_num_polys(const struct tg_geom *geom);
TG_EXTERN const struct tg_poly *tg_geom_poly_at(const struct tg_geom *geom,
int index);
TG_EXTERN int tg_geom_num_geometries(const struct tg_geom *geom);
TG_EXTERN const struct tg_geom *tg_geom_geometry_at(const struct tg_geom *geom,
int index);
TG_EXTERN const char *tg_geom_extra_json(const struct tg_geom *geom);
TG_EXTERN bool tg_geom_is_empty(const struct tg_geom *geom);
TG_EXTERN int tg_geom_dims(const struct tg_geom *geom);
TG_EXTERN bool tg_geom_has_z(const struct tg_geom *geom);
TG_EXTERN bool tg_geom_has_m(const struct tg_geom *geom);
TG_EXTERN double tg_geom_z(const struct tg_geom *geom);
TG_EXTERN double tg_geom_m(const struct tg_geom *geom);
TG_EXTERN const double *tg_geom_extra_coords(const struct tg_geom *geom);
TG_EXTERN int tg_geom_num_extra_coords(const struct tg_geom *geom);
TG_EXTERN size_t tg_geom_memsize(const struct tg_geom *geom);
TG_EXTERN void tg_geom_search(const struct tg_geom *geom, struct tg_rect rect,
bool (*iter)(const struct tg_geom *geom, int index, void *udata),
void *udata);
TG_EXTERN int tg_geom_fullrect(const struct tg_geom *geom, double min[4],
double max[4]);
TG_EXTERN bool tg_geom_equals(const struct tg_geom *a, const struct tg_geom *b);
TG_EXTERN bool tg_geom_intersects(const struct tg_geom *a,
const struct tg_geom *b);
TG_EXTERN bool tg_geom_disjoint(const struct tg_geom *a,
const struct tg_geom *b);
TG_EXTERN bool tg_geom_contains(const struct tg_geom *a,
const struct tg_geom *b);
TG_EXTERN bool tg_geom_within(const struct tg_geom *a, const struct tg_geom *b);
TG_EXTERN bool tg_geom_covers(const struct tg_geom *a, const struct tg_geom *b);
TG_EXTERN bool tg_geom_coveredby(const struct tg_geom *a,
const struct tg_geom *b);
TG_EXTERN bool tg_geom_touches(const struct tg_geom *a,
const struct tg_geom *b);
TG_EXTERN bool tg_geom_intersects_rect(const struct tg_geom *a,
struct tg_rect b);
TG_EXTERN bool tg_geom_intersects_xy(const struct tg_geom *a, double x,
double y);
TG_EXTERN struct tg_geom *tg_parse_geojson(const char *geojson);
TG_EXTERN struct tg_geom *tg_parse_geojsonn(const char *geojson, size_t len);
TG_EXTERN struct tg_geom *tg_parse_geojson_ix(const char *geojson,
enum tg_index ix);
TG_EXTERN struct tg_geom *tg_parse_geojsonn_ix(const char *geojson, size_t len,
enum tg_index ix);
TG_EXTERN struct tg_geom *tg_parse_wkt(const char *wkt);
TG_EXTERN struct tg_geom *tg_parse_wktn(const char *wkt, size_t len);
TG_EXTERN struct tg_geom *tg_parse_wkt_ix(const char *wkt, enum tg_index ix);
TG_EXTERN struct tg_geom *tg_parse_wktn_ix(const char *wkt, size_t len,
enum tg_index ix);
TG_EXTERN struct tg_geom *tg_parse_wkb(const uint8_t *wkb, size_t len);
TG_EXTERN struct tg_geom *tg_parse_wkb_ix(const uint8_t *wkb, size_t len,
enum tg_index ix);
TG_EXTERN struct tg_geom *tg_parse_hex(const char *hex);
TG_EXTERN struct tg_geom *tg_parse_hexn(const char *hex, size_t len);
TG_EXTERN struct tg_geom *tg_parse_hex_ix(const char *hex, enum tg_index ix);
TG_EXTERN struct tg_geom *tg_parse_hexn_ix(const char *hex, size_t len,
enum tg_index ix);
TG_EXTERN struct tg_geom *tg_parse_geobin(const uint8_t *geobin, size_t len);
TG_EXTERN struct tg_geom *tg_parse_geobin_ix(const uint8_t *geobin, size_t len,
enum tg_index ix);
TG_EXTERN struct tg_geom *tg_parse(const void *data, size_t len);
TG_EXTERN struct tg_geom *tg_parse_ix(const void *data, size_t len,
enum tg_index ix);
TG_EXTERN const char *tg_geom_error(const struct tg_geom *geom);
TG_EXTERN int tg_geobin_fullrect(const uint8_t *geobin, size_t len,
double min[4], double max[4]);
TG_EXTERN struct tg_rect tg_geobin_rect(const uint8_t *geobin, size_t len);
TG_EXTERN struct tg_point tg_geobin_point(const uint8_t *geobin, size_t len);
TG_EXTERN size_t tg_geom_geojson(const struct tg_geom *geom, char *dst,
size_t n);
TG_EXTERN size_t tg_geom_wkt(const struct tg_geom *geom, char *dst, size_t n);
TG_EXTERN size_t tg_geom_wkb(const struct tg_geom *geom, uint8_t *dst,
size_t n);
TG_EXTERN size_t tg_geom_hex(const struct tg_geom *geom, char *dst, size_t n);
TG_EXTERN size_t tg_geom_geobin(const struct tg_geom *geom, uint8_t *dst,
size_t n);
TG_EXTERN struct tg_geom *tg_geom_new_point_z(struct tg_point point, double z);
TG_EXTERN struct tg_geom *tg_geom_new_point_m(struct tg_point point, double m);
TG_EXTERN struct tg_geom *tg_geom_new_point_zm(struct tg_point point, double z,
double m);
TG_EXTERN struct tg_geom *tg_geom_new_point_empty(void);
TG_EXTERN struct tg_geom *tg_geom_new_linestring_z(const struct tg_line *line,
const double *extra_coords, int ncoords);
TG_EXTERN struct tg_geom *tg_geom_new_linestring_m(const struct tg_line *line,
const double *extra_coords, int ncoords);
TG_EXTERN struct tg_geom *tg_geom_new_linestring_zm(const struct tg_line *line,
const double *extra_coords, int ncoords);
TG_EXTERN struct tg_geom *tg_geom_new_linestring_empty(void);
TG_EXTERN struct tg_geom *tg_geom_new_polygon_z(const struct tg_poly *poly,
const double *extra_coords, int ncoords);
TG_EXTERN struct tg_geom *tg_geom_new_polygon_m(const struct tg_poly *poly,
const double *extra_coords, int ncoords);
TG_EXTERN struct tg_geom *tg_geom_new_polygon_zm(const struct tg_poly *poly,
const double *extra_coords, int ncoords);
TG_EXTERN struct tg_geom *tg_geom_new_polygon_empty(void);
TG_EXTERN struct tg_geom *tg_geom_new_multipoint_z(
const struct tg_point *points, int npoints, const double *extra_coords,
int ncoords);
TG_EXTERN struct tg_geom *tg_geom_new_multipoint_m(
const struct tg_point *points, int npoints, const double *extra_coords,
int ncoords);
TG_EXTERN struct tg_geom *tg_geom_new_multipoint_zm(
const struct tg_point *points, int npoints, const double *extra_coords,
int ncoords);
TG_EXTERN struct tg_geom *tg_geom_new_multipoint_empty(void);
TG_EXTERN struct tg_geom *tg_geom_new_multilinestring_z(
const struct tg_line *const lines[], int nlines, const double *extra_coords,
int ncoords);
TG_EXTERN struct tg_geom *tg_geom_new_multilinestring_m(
const struct tg_line *const lines[], int nlines, const double *extra_coords,
int ncoords);
TG_EXTERN struct tg_geom *tg_geom_new_multilinestring_zm(
const struct tg_line *const lines[], int nlines, const double *extra_coords,
int ncoords);
TG_EXTERN struct tg_geom *tg_geom_new_multilinestring_empty(void);
TG_EXTERN struct tg_geom *tg_geom_new_multipolygon_z(
const struct tg_poly *const polys[], int npolys, const double *extra_coords,
int ncoords);
TG_EXTERN struct tg_geom *tg_geom_new_multipolygon_m(
const struct tg_poly *const polys[], int npolys, const double *extra_coords,
int ncoords);
TG_EXTERN struct tg_geom *tg_geom_new_multipolygon_zm(
const struct tg_poly *const polys[], int npolys, const double *extra_coords,
int ncoords);
TG_EXTERN struct tg_geom *tg_geom_new_multipolygon_empty(void);
TG_EXTERN struct tg_geom *tg_geom_new_geometrycollection_empty(void);
TG_EXTERN struct tg_rect tg_point_rect(struct tg_point point);
TG_EXTERN bool tg_point_intersects_rect(struct tg_point a, struct tg_rect b);
TG_EXTERN struct tg_rect tg_segment_rect(struct tg_segment s);
TG_EXTERN bool tg_segment_intersects_segment(struct tg_segment a,
struct tg_segment b);
TG_EXTERN struct tg_rect tg_rect_expand(struct tg_rect rect,
struct tg_rect other);
TG_EXTERN struct tg_rect tg_rect_expand_point(struct tg_rect rect,
struct tg_point point);
TG_EXTERN struct tg_point tg_rect_center(struct tg_rect rect);
TG_EXTERN bool tg_rect_intersects_rect(struct tg_rect a, struct tg_rect b);
TG_EXTERN bool tg_rect_intersects_point(struct tg_rect a, struct tg_point b);
TG_EXTERN struct tg_ring *tg_ring_new(const struct tg_point *points,
int npoints);
TG_EXTERN struct tg_ring *tg_ring_new_ix(const struct tg_point *points,
int npoints, enum tg_index ix);
TG_EXTERN void tg_ring_free(struct tg_ring *ring);
TG_EXTERN struct tg_ring *tg_ring_clone(const struct tg_ring *ring);
TG_EXTERN struct tg_ring *tg_ring_copy(const struct tg_ring *ring);
TG_EXTERN size_t tg_ring_memsize(const struct tg_ring *ring);
TG_EXTERN struct tg_rect tg_ring_rect(const struct tg_ring *ring);
TG_EXTERN int tg_ring_num_points(const struct tg_ring *ring);
TG_EXTERN struct tg_point tg_ring_point_at(const struct tg_ring *ring,
int index);
TG_EXTERN const struct tg_point *tg_ring_points(const struct tg_ring *ring);
TG_EXTERN int tg_ring_num_segments(const struct tg_ring *ring);
TG_EXTERN struct tg_segment tg_ring_segment_at(const struct tg_ring *ring,
int index);
TG_EXTERN bool tg_ring_convex(const struct tg_ring *ring);
TG_EXTERN bool tg_ring_clockwise(const struct tg_ring *ring);
TG_EXTERN int tg_ring_index_spread(const struct tg_ring *ring);
TG_EXTERN int tg_ring_index_num_levels(const struct tg_ring *ring);
TG_EXTERN int tg_ring_index_level_num_rects(const struct tg_ring *ring,
int levelidx);
TG_EXTERN struct tg_rect tg_ring_index_level_rect(const struct tg_ring *ring,
int levelidx, int rectidx);
TG_EXTERN bool tg_ring_nearest_segment(const struct tg_ring *ring,
double (*rect_dist)(struct tg_rect rect, int *more, void *udata),
double (*seg_dist)(struct tg_segment seg, int *more, void *udata),
bool (*iter)(struct tg_segment seg, double dist, int index, void *udata),
void *udata);
TG_EXTERN void tg_ring_line_search(const struct tg_ring *a,
const struct tg_line *b, bool (*iter)(struct tg_segment aseg, int aidx,
struct tg_segment bseg, int bidx, void *udata), void *udata);
TG_EXTERN void tg_ring_ring_search(const struct tg_ring *a,
const struct tg_ring *b, bool (*iter)(struct tg_segment aseg, int aidx,
struct tg_segment bseg, int bidx, void *udata), void *udata);
TG_EXTERN double tg_ring_area(const struct tg_ring *ring);
TG_EXTERN double tg_ring_perimeter(const struct tg_ring *ring);
TG_EXTERN struct tg_line *tg_line_new(const struct tg_point *points,
int npoints);
TG_EXTERN struct tg_line *tg_line_new_ix(const struct tg_point *points,
int npoints, enum tg_index ix);
TG_EXTERN void tg_line_free(struct tg_line *line);
TG_EXTERN struct tg_line *tg_line_clone(const struct tg_line *line);
TG_EXTERN struct tg_line *tg_line_copy(const struct tg_line *line);
TG_EXTERN size_t tg_line_memsize(const struct tg_line *line);
TG_EXTERN struct tg_rect tg_line_rect(const struct tg_line *line);
TG_EXTERN int tg_line_num_points(const struct tg_line *line);
TG_EXTERN const struct tg_point *tg_line_points(const struct tg_line *line);
TG_EXTERN struct tg_point tg_line_point_at(const struct tg_line *line,
int index);
TG_EXTERN int tg_line_num_segments(const struct tg_line *line);
TG_EXTERN struct tg_segment tg_line_segment_at(const struct tg_line *line,
int index);
TG_EXTERN bool tg_line_clockwise(const struct tg_line *line);
TG_EXTERN int tg_line_index_spread(const struct tg_line *line);
TG_EXTERN int tg_line_index_num_levels(const struct tg_line *line);
TG_EXTERN int tg_line_index_level_num_rects(const struct tg_line *line,
int levelidx);
TG_EXTERN struct tg_rect tg_line_index_level_rect(const struct tg_line *line,
int levelidx, int rectidx);
TG_EXTERN bool tg_line_nearest_segment(const struct tg_line *line,
double (*rect_dist)(struct tg_rect rect, int *more, void *udata),
double (*seg_dist)(struct tg_segment seg, int *more, void *udata),
bool (*iter)(struct tg_segment seg, double dist, int index, void *udata),
void *udata);
TG_EXTERN void tg_line_line_search(const struct tg_line *a,
const struct tg_line *b, bool (*iter)(struct tg_segment aseg, int aidx,
struct tg_segment bseg, int bidx, void *udata), void *udata);
TG_EXTERN double tg_line_length(const struct tg_line *line);
TG_EXTERN struct tg_poly *tg_poly_new(const struct tg_ring *exterior,
const struct tg_ring *const holes[], int nholes);
TG_EXTERN void tg_poly_free(struct tg_poly *poly);
TG_EXTERN struct tg_poly *tg_poly_clone(const struct tg_poly *poly);
TG_EXTERN struct tg_poly *tg_poly_copy(const struct tg_poly *poly);
TG_EXTERN size_t tg_poly_memsize(const struct tg_poly *poly);
TG_EXTERN const struct tg_ring *tg_poly_exterior(const struct tg_poly *poly);
TG_EXTERN int tg_poly_num_holes(const struct tg_poly *poly);
TG_EXTERN const struct tg_ring *tg_poly_hole_at(const struct tg_poly *poly,
int index);
TG_EXTERN struct tg_rect tg_poly_rect(const struct tg_poly *poly);
TG_EXTERN bool tg_poly_clockwise(const struct tg_poly *poly);
TG_EXTERN void tg_env_set_allocator(void *(*malloc)(size_t),
void *(*realloc)(void*, size_t), void (*free)(void*));
TG_EXTERN void tg_env_set_index(enum tg_index ix);
TG_EXTERN void tg_env_set_index_spread(int spread);
TG_EXTERN void tg_env_set_print_fixed_floats(bool print);
// Private methods. Not listed in the tg.h file.
TG_EXTERN bool tg_ring_empty(const struct tg_ring *ring);
TG_EXTERN bool tg_line_empty(const struct tg_line *line);
TG_EXTERN bool tg_poly_empty(const struct tg_poly *poly);
TG_EXTERN void tg_rect_search(struct tg_rect rect, struct tg_rect target,
bool(*iter)(struct tg_segment seg, int index, void *udata), void *udata);
TG_EXTERN void tg_ring_search(const struct tg_ring *ring, struct tg_rect rect,
bool(*iter)(struct tg_segment seg, int index, void *udata), void *udata);
TG_EXTERN void tg_line_search(const struct tg_line *ring, struct tg_rect rect,
bool(*iter)(struct tg_segment seg, int index, void *udata), void *udata);
TG_EXTERN void tg_geom_foreach(const struct tg_geom *geom, bool(*iter)(
const struct tg_geom *geom, void *udata), void *udata);
TG_EXTERN double tg_ring_polsby_popper_score(const struct tg_ring *ring);
TG_EXTERN double tg_line_polsby_popper_score(const struct tg_line *line);
TG_EXTERN int tg_rect_num_points(struct tg_rect rect);
TG_EXTERN struct tg_point tg_rect_point_at(struct tg_rect rect, int index);
TG_EXTERN int tg_rect_num_segments(struct tg_rect rect);
TG_EXTERN struct tg_segment tg_rect_segment_at(struct tg_rect rect, int index);
TG_EXTERN int tg_geom_de9im_dims(const struct tg_geom *geom);
TG_EXTERN bool tg_point_covers_point(struct tg_point a, struct tg_point b);
TG_EXTERN bool tg_point_covers_rect(struct tg_point a, struct tg_rect b);
TG_EXTERN bool tg_point_covers_line(struct tg_point a, const struct tg_line *b);
TG_EXTERN bool tg_point_covers_poly(struct tg_point a, const struct tg_poly *b);
TG_EXTERN bool tg_geom_covers_point(const struct tg_geom *a, struct tg_point b);
TG_EXTERN bool tg_geom_covers_xy(const struct tg_geom *a, double x, double y);
TG_EXTERN bool tg_segment_covers_segment(struct tg_segment a,
struct tg_segment b);
TG_EXTERN bool tg_segment_covers_point(struct tg_segment a, struct tg_point b);
TG_EXTERN bool tg_segment_covers_rect(struct tg_segment a, struct tg_rect b);
TG_EXTERN bool tg_rect_covers_point(struct tg_rect a, struct tg_point b);
TG_EXTERN bool tg_rect_covers_xy(struct tg_rect a, double x, double y);
TG_EXTERN bool tg_rect_covers_rect(struct tg_rect a, struct tg_rect b);
TG_EXTERN bool tg_rect_covers_line(struct tg_rect a, const struct tg_line *b);
TG_EXTERN bool tg_rect_covers_poly(struct tg_rect a, const struct tg_poly *b);
TG_EXTERN bool tg_line_covers_point(const struct tg_line *a, struct tg_point b);
TG_EXTERN bool tg_line_covers_rect(const struct tg_line *a, struct tg_rect b);
TG_EXTERN bool tg_line_covers_line(const struct tg_line *a,
const struct tg_line *b);
TG_EXTERN bool tg_line_covers_poly(const struct tg_line *a,
const struct tg_poly *b);
TG_EXTERN bool tg_line_intersects_point(const struct tg_line *a,
struct tg_point b);
TG_EXTERN bool tg_line_intersects_rect(const struct tg_line *a,
struct tg_rect b);
TG_EXTERN bool tg_line_intersects_line(const struct tg_line *a,
const struct tg_line *b);
TG_EXTERN bool tg_line_intersects_poly(const struct tg_line *a,
const struct tg_poly *b);
TG_EXTERN bool tg_point_intersects_point(struct tg_point a, struct tg_point b);
TG_EXTERN bool tg_point_intersects_rect(struct tg_point a, struct tg_rect b);
TG_EXTERN bool tg_point_intersects_line(struct tg_point a,
const struct tg_line *b);
TG_EXTERN bool tg_point_intersects_poly(struct tg_point a,
const struct tg_poly *b);
TG_EXTERN bool tg_rect_intersects_rect(struct tg_rect a, struct tg_rect b);
TG_EXTERN bool tg_rect_intersects_line(struct tg_rect a,
const struct tg_line *b);
TG_EXTERN bool tg_rect_intersects_poly(struct tg_rect a,
const struct tg_poly *b);
TG_EXTERN bool tg_segment_intersects_segment(struct tg_segment a,
struct tg_segment b);
TG_EXTERN bool tg_poly_covers_xy(const struct tg_poly *a, double x, double y);
TG_EXTERN bool tg_poly_touches_line(const struct tg_poly *a,
const struct tg_line *b);
TG_EXTERN bool tg_poly_coveredby_poly(const struct tg_poly *a,
const struct tg_poly *b);
TG_EXTERN bool tg_poly_covers_point(const struct tg_poly *a, struct tg_point b);
TG_EXTERN bool tg_poly_covers_rect(const struct tg_poly *a, struct tg_rect b);
TG_EXTERN bool tg_poly_covers_line(const struct tg_poly *a,
const struct tg_line *b);
TG_EXTERN bool tg_poly_covers_poly(const struct tg_poly *a,
const struct tg_poly *b);
TG_EXTERN bool tg_poly_intersects_point(const struct tg_poly *a,
struct tg_point b);
TG_EXTERN bool tg_poly_intersects_rect(const struct tg_poly *a,
struct tg_rect b);
TG_EXTERN bool tg_poly_intersects_line(const struct tg_poly *a,
const struct tg_line *b);
TG_EXTERN bool tg_poly_intersects_poly(const struct tg_poly *a,
const struct tg_poly *b);
TG_EXTERN bool tg_geom_intersects_point(const struct tg_geom *a,
struct tg_point b);
TG_EXTERN struct tg_segment tg_segment_move(struct tg_segment seg,
double delta_x, double delta_y);
TG_EXTERN struct tg_ring *tg_ring_move(const struct tg_ring *ring,
double delta_x, double delta_y);
TG_EXTERN bool tg_ring_intersects_segment(const struct tg_ring *ring,
struct tg_segment seg, bool allow_on_edge);
TG_EXTERN enum tg_raycast_result tg_raycast(struct tg_segment seg,
struct tg_point p);
TG_EXTERN void *tg_realloc(void *ptr, size_t nbytes);
TG_EXTERN void *tg_malloc(size_t nbytes);
TG_EXTERN void tg_free(void *ptr);
TG_EXTERN bool tg_point_touches_poly(struct tg_point point,
const struct tg_poly *poly);
TG_EXTERN bool tg_poly_contains_geom(struct tg_poly *a,
const struct tg_geom *b);
TG_EXTERN bool tg_poly_contains_line(const struct tg_poly *a,
const struct tg_line *b);
TG_EXTERN bool tg_poly_contains_point(const struct tg_poly *poly,
struct tg_point point);
TG_EXTERN bool tg_poly_contains_poly(const struct tg_poly *a,
const struct tg_poly *b);
TG_EXTERN struct tg_poly *tg_poly_move(const struct tg_poly *poly,
double delta_x, double delta_y);
TG_EXTERN bool tg_poly_touches_poly(const struct tg_poly *a,
const struct tg_poly *b);
TG_EXTERN double tg_rect_distance_rect(struct tg_rect a, struct tg_rect b);
TG_EXTERN struct tg_rect tg_rect_move(struct tg_rect rect, double delta_x,
double delta_y);
TG_EXTERN bool tg_ring_contains_line(const struct tg_ring *a,
const struct tg_line *b, bool allow_on_edge, bool respect_boundaries);
TG_EXTERN struct ring_result tg_ring_contains_point(const struct tg_ring *ring,
struct tg_point point, bool allow_on_edge);
TG_EXTERN double tg_point_distance_point(struct tg_point a, struct tg_point b);
TG_EXTERN double tg_point_distance_rect(struct tg_point p, struct tg_rect r);
TG_EXTERN double tg_point_distance_segment(struct tg_point p,
struct tg_segment s);
TG_EXTERN uint32_t tg_point_hilbert(struct tg_point point, struct tg_rect rect);
TG_EXTERN struct tg_point tg_point_move(struct tg_point point, double delta_x,
double delta_y);
TG_EXTERN bool tg_point_touches_geom(struct tg_point a,
const struct tg_geom *b);
TG_EXTERN bool tg_point_touches_line(struct tg_point point,
const struct tg_line *line);
TG_EXTERN struct tg_line *tg_line_move(const struct tg_line *line,
double delta_x, double delta_y);
TG_EXTERN bool tg_line_touches_geom(struct tg_line *a, const struct tg_geom *b);
TG_EXTERN bool tg_line_touches_line(const struct tg_line *a,
const struct tg_line *b);
TG_EXTERN bool tg_line_touches_point(const struct tg_line *line,
struct tg_point point);
TG_EXTERN bool tg_line_touches_poly(const struct tg_line *a,
const struct tg_poly *b);
TG_EXTERN bool tg_point_contains_geom(struct tg_point a,
const struct tg_geom *b);
TG_EXTERN bool tg_point_contains_line(struct tg_point point,
const struct tg_line *line);
TG_EXTERN void tg_geom_setnoheap(struct tg_geom *geom);
TG_EXTERN enum tg_index tg_index_extract_spread(enum tg_index ix, int *spread);
TG_EXTERN enum tg_index tg_index_with_spread(enum tg_index ix, int spread);
TG_EXTERN bool tg_line_contains_geom(struct tg_line *a,
const struct tg_geom *b);
TG_EXTERN bool tg_line_contains_line(const struct tg_line *line,
const struct tg_line *other);
TG_EXTERN bool tg_line_contains_point(const struct tg_line *line,
struct tg_point point);
TG_EXTERN bool tg_line_contains_poly(const struct tg_line *line,
const struct tg_poly *poly);
TG_EXTERN bool tg_point_contains_point(struct tg_point point,
struct tg_point other);
TG_EXTERN int tg_geom_multi_index_spread(const struct tg_geom *geom);
TG_EXTERN bool tg_geom_overlaps(const struct tg_geom *a,
const struct tg_geom *b);
TG_EXTERN bool tg_point_contains_poly(struct tg_point point,
const struct tg_poly *poly);
TG_EXTERN bool tg_point_touches_point(struct tg_point a, struct tg_point b);
TG_EXTERN bool tg_poly_touches_geom(struct tg_poly *a, const struct tg_geom *b);
TG_EXTERN bool tg_poly_touches_point(const struct tg_poly *poly,
struct tg_point point);
TG_EXTERN size_t tg_aligned_size(size_t size);
TG_EXTERN struct tg_ring *tg_circle_new_ix(struct tg_point center,
double radius, int steps, enum tg_index ix);
TG_EXTERN enum tg_index tg_env_get_default_index(void);
TG_EXTERN int tg_env_get_index_spread(void);
TG_EXTERN bool tg_geom_crosses(const struct tg_geom *a,
const struct tg_geom *b);
TG_EXTERN int tg_geom_multi_index_level_num_rects(const struct tg_geom *geom,
int levelidx);
TG_EXTERN struct tg_rect tg_geom_multi_index_level_rect(
const struct tg_geom *geom, int levelidx, int rectidx);
TG_EXTERN struct tg_ring *tg_circle_new(struct tg_point center, double radius,
int steps);
TG_EXTERN int tg_geom_multi_index_num_levels(const struct tg_geom *geom);
TG_EXTERN bool tg_ring_contains_ring(const struct tg_ring *a,
const struct tg_ring *b, bool allow_on_edge);
TG_EXTERN bool tg_ring_contains_segment(const struct tg_ring *ring,
struct tg_segment seg, bool allow_on_edge);
TG_EXTERN bool tg_ring_intersects_line(const struct tg_ring *ring,
const struct tg_line *line, bool allow_on_edge);
TG_EXTERN bool tg_ring_intersects_ring(const struct tg_ring *ring,
const struct tg_ring *other, bool allow_on_edge);
enum base {
BASE_POINT = 1, // tg_point
BASE_LINE = 2, // tg_line
BASE_RING = 3, // tg_ring
BASE_POLY = 4, // tg_poly
BASE_GEOM = 5, // tg_geom
};
enum flags {
HAS_Z = 1<<0, // Geometry has additional Z coordinates
HAS_M = 1<<1, // Geometry has additional M coordinates
IS_ERROR = 1<<2, // Geometry is a parse error. Falls back to POINT
IS_EMPTY = 1<<3, // Same as a GeoJSON null object (empty coords)
IS_FEATURE = 1<<4, // GeoJSON. Geometry is Feature
IS_FEATURE_COL = 1<<5, // GeoJSON. GeometryGollection is FeatureCollection
HAS_NULL_PROPS = 1<<6, // GeoJSON. 'Feature' with 'properties'=null
IS_UNLOCATED = 1<<7, // GeoJSON. 'Feature' with 'geometry'=null
};
// Optionally use non-atomic reference counting when TG_NOATOMICS is defined.
#ifdef TG_NOATOMICS
typedef int rc_t;
static void rc_init(rc_t *rc) {
*rc = 0;
}
static void rc_retain(rc_t *rc) {
*rc++;
}
static bool rc_release(rc_t *rc) {
*rc--;
return *rc == 1;
}
#else
#include <stdatomic.h>
/*
The relaxed/release/acquire pattern is based on:
http://boost.org/doc/libs/1_87_0/libs/atomic/doc/html/atomic/usage_examples.html
*/
typedef atomic_int rc_t;
static void rc_init(rc_t *rc) {
atomic_init(rc, 0);
}
static void rc_retain(rc_t *rc) {
atomic_fetch_add_explicit(rc, 1, __ATOMIC_RELAXED);
}
static bool rc_release(rc_t *rc) {
if (atomic_fetch_sub_explicit(rc, 1, __ATOMIC_RELEASE) == 1) {
atomic_thread_fence(__ATOMIC_ACQUIRE);
return true;
}
return false;
}
#endif
struct head {
rc_t rc;
bool noheap;
enum base base:4;
enum tg_geom_type type:4;
enum flags flags:8;
};
/// A ring is series of tg_segment which creates a shape that does not
/// self-intersect and is fully closed, where the start and end points are the
/// exact same value.
///
/// **Creating**
///
/// To create a new ring use the tg_ring_new() function.
///
/// ```
/// struct tg_ring *ring = tg_ring_new(points, npoints);
/// ```
///
/// **Upcasting**
///
/// A tg_ring can always be safely upcasted to a tg_poly or tg_geom; allowing
/// it to use any tg_poly_*() or tg_geom_*() function.
///
/// ```
/// struct tg_poly *poly = (struct tg_poly*)ring; // Cast to a tg_poly
/// struct tg_geom *geom = (struct tg_geom*)ring; // Cast to a tg_geom
/// ```
/// @see RingFuncs
/// @see PolyFuncs
struct tg_ring {
struct head head;
bool closed;
bool clockwise;
bool convex;
double area;
int npoints;
int nsegs;
struct tg_rect rect;
struct index *index;
struct ystripes *ystripes;
struct tg_point points[];
};
/// A line is a series of tg_segment that make up a linestring.
///
/// **Creating**
///
/// To create a new line use the tg_line_new() function.
///
/// ```
/// struct tg_line *line = tg_line_new(points, npoints);
/// ```
///
/// **Upcasting**
///
/// A tg_line can always be safely upcasted to a tg_geom; allowing
/// it to use any tg_geom_*() function.
///
/// ```
/// struct tg_geom *geom = (struct tg_geom*)line; // Cast to a tg_geom
/// ```
///
/// @see LineFuncs
struct tg_line { int _; };
/// A polygon consists of one exterior ring and zero or more holes.
///
/// **Creating**
///
/// To create a new polygon use the tg_poly_new() function.
///
/// ```
/// struct tg_poly *poly = tg_poly_new(exterior, holes, nholes);
/// ```
///
/// **Upcasting**
///
/// A tg_poly can always be safely upcasted to a tg_geom; allowing
/// it to use any tg_geom_*() function.
///
/// ```
/// struct tg_geom *geom = (struct tg_geom*)poly; // Cast to a tg_geom
/// ```
///
/// @see PolyFuncs
struct tg_poly {
struct head head;
struct tg_ring *exterior;
int nholes;
struct tg_ring **holes;
};
struct multi {
struct tg_geom **geoms;
int ngeoms;
struct tg_rect rect; // unioned rect child geometries
struct index *index; // geometry index, or NULL if not indexed
int *ixgeoms; // indexed geometries, or NULL if not indexed
};
/// A geometry is the common generic type that can represent a Point,
/// LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, or
/// GeometryCollection.
///
/// For geometries that are derived from GeoJSON, they may have addtional
/// attributes such as being a Feature or a FeatureCollection; or include
/// extra json fields.
///
/// **Creating**
///
/// To create a new geometry use one of the @ref GeometryConstructors or
/// @ref GeometryParsing functions.
///
/// ```
/// struct tg_geom *geom = tg_geom_new_point(point);
/// struct tg_geom *geom = tg_geom_new_polygon(poly);
/// struct tg_geom *geom = tg_parse_geojson(geojson);
/// ```
///
/// **Upcasting**
///
/// Other types, specifically tg_line, tg_ring, and tg_poly, can be safely
/// upcasted to a tg_geom; allowing them to use any tg_geom_*()
/// function.
///
/// ```
/// struct tg_geom *geom1 = (struct tg_geom*)line; // Cast to a LineString
/// struct tg_geom *geom2 = (struct tg_geom*)ring; // Cast to a Polygon
/// struct tg_geom *geom3 = (struct tg_geom*)poly; // Cast to a Polygon
/// ```
///
/// @see GeometryConstructors
/// @see GeometryAccessors
/// @see GeometryPredicates
/// @see GeometryParsing
/// @see GeometryWriting
struct tg_geom {
struct head head;
union {
struct tg_point point;
struct tg_line *line;
struct tg_poly *poly;
struct multi *multi;
};
union {
struct { // TG_POINT
double z;
double m;
};
struct { // !TG_POINT
double *coords; // extra dimensinal coordinates
int ncoords;
};
};
union {
char *xjson; // extra json fields, such as "id", "properties", etc.
char *error; // an error message, when flag IS_ERROR
};
};
struct boxed_point {
struct head head;
struct tg_point point;
};
#define todo(msg) { \
fprintf(stderr, "todo: %s, line: %d\n", (msg), __LINE__); \
exit(1); \
}
static_assert(sizeof(int) == 4 || sizeof(int) == 8, "invalid int size");
// Function attribute for noinline.
#if defined(__GNUC__)
#define __attr_noinline __attribute__((noinline))
#elif defined(_MSC_VER)
#define __attr_noinline __declspec(noinline)
#else
#define __attr_noinline
#endif
// Fast floating-point min and max for gcc and clang on arm64 and x64.
#if defined(__GNUC__) && defined(__aarch64__)
// arm64 already uses branchless operations for fmin and fmax
static inline double fmin0(double x, double y) {
return fmin(x, y);
}
static inline double fmax0(double x, double y) {
return fmax(x, y);
}
static inline float fminf0(float x, float y) {
return fminf(x, y);
}
static inline float fmaxf0(float x, float y) {
return fmaxf(x, y);
}
#elif defined(__GNUC__) && defined(__x86_64__)
// gcc/amd64 sometimes uses branching with fmin/fmax.
// This code use single a asm op instead.
// https://gcc.gnu.org/bugzilla//show_bug.cgi?id=94497
#pragma GCC diagnostic push
#ifdef __clang__
#pragma clang diagnostic ignored "-Wlanguage-extension-token"
#endif
static inline double fmin0(double x, double y) {
asm("minsd %1, %0" : "+x" (x) : "x" (y));
return x;
}
static inline double fmax0(double x, double y) {
asm("maxsd %1, %0" : "+x" (x) : "x" (y));
return x;
}
static inline float fminf0(float x, float y) {
asm("minss %1, %0" : "+x" (x) : "x" (y));
return x;
}
static inline float fmaxf0(float x, float y) {
asm("maxss %1, %0" : "+x" (x) : "x" (y));
return x;
}
#pragma GCC diagnostic pop
#else
// for everything else, let the compiler figure it out.
static inline double fmin0(double x, double y) {
return x < y ? x : y;
}
static inline double fmax0(double x, double y) {
return x > y ? x : y;
}
static inline float fminf0(double x, float y) {
return x < y ? x : y;
}
static inline float fmaxf0(float x, float y) {
return x > y ? x : y;
}
#endif
static inline double fclamp0(double f, double min, double max) {
return fmin0(fmax0(f, min), max);
}
static bool feq(double x, double y) {
return !((x < y) | (x > y));
}
static bool eq_zero(double x) {
return feq(x, 0);
}
static bool collinear(
double x1, double y1, // point 1
double x2, double y2, // point 2
double x3, double y3 // point 3
) {
bool x1x2 = feq(x1, x2);
bool x1x3 = feq(x1, x3);
bool x2x3 = feq(x2, x3);
bool y1y2 = feq(y1, y2);
bool y1y3 = feq(y1, y3);
bool y2y3 = feq(y2, y3);
if (x1x2) {
return x1x3;
}
if (y1y2) {
return y1y3;
}
if ((x1x2 & y1y2) | (x1x3 & y1y3) | (x2x3 & y2y3)) {
return true;
}
double cx1 = x3 - x1;
double cy1 = y3 - y1;
double cx2 = x2 - x1;
double cy2 = y2 - y1;
double s1 = cx1 * cy2;
double s2 = cy1 * cx2;
// Check if precision was lost.
double s3 = (s1 / cy2) - cx1;
double s4 = (s2 / cx2) - cy1;
if (s3 < 0) {
s1 = nexttoward(s1, -INFINITY);
} else if (s3 > 0) {
s1 = nexttoward(s1, +INFINITY);
}
if (s4 < 0) {
s2 = nexttoward(s2, -INFINITY);
} else if (s4 > 0) {
s2 = nexttoward(s2, +INFINITY);
}
return eq_zero(s1 - s2);
}
static double length(double x1, double y1, double x2, double y2) {
return sqrt((x1-x2) * (x1-x2) + (y1-y2) * (y1-y2));
}
#ifndef ludo
#define ludo
#define ludo1(i,f) f; i++;
#define ludo2(i,f) ludo1(i,f); ludo1(i,f);
#define ludo4(i,f) ludo2(i,f); ludo2(i,f);
#define ludo8(i,f) ludo4(i,f); ludo4(i,f);
#define ludo16(i,f) ludo8(i,f); ludo8(i,f);
#define ludo32(i,f) ludo16(i,f); ludo16(i,f);
#define ludo64(i,f) ludo32(i,f); ludo32(i,f);
#define for1(i,n,f) while(i+1<=(n)) { ludo1(i,f); }
#define for2(i,n,f) while(i+2<=(n)) { ludo2(i,f); } for1(i,n,f);
#define for4(i,n,f) while(i+4<=(n)) { ludo4(i,f); } for1(i,n,f);
#define for8(i,n,f) while(i+8<=(n)) { ludo8(i,f); } for1(i,n,f);
#define for16(i,n,f) while(i+16<=(n)) { ludo16(i,f); } for1(i,n,f);
#define for32(i,n,f) while(i+32<=(n)) { ludo32(i,f); } for1(i,n,f);
#define for64(i,n,f) while(i+64<=(n)) { ludo64(i,f); } for1(i,n,f);
#endif
static size_t grow_cap(size_t cap, size_t init_cap) {
return cap == 0 ? init_cap : cap < 1000 ? cap * 2 : cap * 1.25;
}
#define print_segment(s) { \
printf("(%f %f,%f %f)", (s).a.x, (s).a.y, (s).b.x, (s).b.y); \
}
#define print_geom(g) { \
size_t sz = tg_geom_wkt(g, 0, 0); \
char *wkt = tg_malloc(sz+1); \
assert(wkt); \
tg_geom_wkt(g, wkt, sz+1); \
printf("%s\n", wkt); \
free(wkt); \
}
/////////////////////
// global behaviors
/////////////////////
// Global variables shared by all TG functions.
static void *(*_malloc)(size_t) = NULL;
static void *(*_realloc)(void*, size_t) = NULL;
static void (*_free)(void*) = NULL;
static enum tg_index default_index = TG_NATURAL;
static int default_index_spread = 16;
static bool print_fixed_floats = false;
/// Set the floating point printing to be fixed size.
/// By default floating points are printed using their smallest textual
/// representation. Such as 800000 is converted to "8e5". This is ideal when
/// both accuracy and size are desired. But there may be times when only
/// fixed epresentations are wanted, in that case set this param to true.
void tg_env_set_print_fixed_floats(bool print) {
print_fixed_floats = print;
}
/// Allow for configuring a custom allocator.
///
/// This overrides the built-in malloc, realloc, and free functions for all
/// TG operations.
/// @warning This function, if needed, should be called **only once** at
/// program start up and prior to calling any other tg_*() function.
/// @see GlobalFuncs
void tg_env_set_allocator(
void *(*malloc)(size_t),
void *(*realloc)(void*, size_t),
void (*free)(void*))
{
_malloc = malloc;
_realloc = realloc;
_free = free;
}
void *tg_malloc(size_t nbytes) {
return (_malloc?_malloc:malloc)(nbytes);
}
void *tg_realloc(void *ptr, size_t nbytes) {
return (_realloc?_realloc:realloc)(ptr, nbytes);
}
void tg_free(void *ptr) {
(_free?_free:free)(ptr);
}
/// Set the geometry indexing default.
///
/// This is a global override to the indexing for all yet-to-be created
/// geometries.
/// @warning This function, if needed, should be called **only once** at
/// program start up and prior to calling any other tg_*() function.
/// @see [tg_index](.#tg_index)
/// @see tg_env_set_index()
/// @see GlobalFuncs
void tg_env_set_index(enum tg_index ix) {
switch (ix) {
case TG_NONE:
case TG_NATURAL:
case TG_YSTRIPES:
// only change for none, natural, and ystripes
default_index = ix;
break;
default:
// no change
break;
}
}
/// Get the current geometry indexing default.
/// @see [tg_index](.#tg_index)
/// @see tg_env_set_index()
/// @see GlobalFuncs
enum tg_index tg_env_get_default_index(void) {
return default_index;
}
/// Set the default index spread.
///
/// The "spread" is how many rectangles are grouped together on an indexed