tg-sys 0.1.1

unsafe low-level bindings to the `tg` geometry library
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
#![allow(nonstandard_style)]

/// The base point type used for all geometries.
///
/// - See [`PointFuncs`]
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct tg_point {
    pub x: libc::c_double,
    pub y: libc::c_double,
}


/// The base segment type used in [`tg_line`] and [`tg_ring`] for joining two vertices.
///
/// - See [`SegmentFuncs`]`
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct tg_segment {
    pub a: tg_point,
    pub b: tg_point,
}

/// A rectangle defined by a minimum and maximum coordinates.
/// Returned by the [`tg_geom_rect()`][GeometryAccessors::tg_geom_rect], [`tg_ring_rect()`][RingFuncs::tg_ring_rect], and other `\*_rect()`
/// functions for getting a geometry's minumum bounding rectangle.
/// Also used internally for geometry indexing.
///
/// - See [`RectFuncs`]
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct tg_rect {
    pub min: tg_point,
    pub max: tg_point,
}

/// Geometry types.
///
/// All tg_geom are one of the following underlying types.
///
/// - See [`tg_geom_typeof()`][`GeometryAccessors::tg_geom_type_string`]`
/// - See [`tg_geom_type_string()`][`GeometryAccessors::tg_geom_type_string`]
/// - See [`GeometryAccessors`]
#[repr(C)]
pub enum tg_geom_type {
    ///< Point
    TG_POINT = 1,
    ///< LineString
    TG_LINESTRING = 2,
    ///< Polygon
    TG_POLYGON = 3,
    ///< MultiPoint, collection of points
    TG_MULTIPOINT = 4,
    ///< MultiLineString, collection of linestrings
    TG_MULTILINESTRING = 5,
    ///< MultiPolygon, collection of polygons
    TG_MULTIPOLYGON = 6,
    ///< GeometryCollection, collection of geometries
    TG_GEOMETRYCOLLECTION = 7,
}

/// Geometry indexing options.
///
/// Used for polygons, rings, and lines to make the point-in-polygon and
/// geometry intersection operations fast.
///
/// An index can also be used for efficiently traversing, searching, and
/// performing nearest-neighbor (kNN) queries on the segment using
/// [`tg_ring_index_*()`][RingFuncs] and [`tg_ring_nearest_segment()`][RingFuncs::tg_ring_nearest_segment] functions.
#[repr(C)]
pub enum tg_index {
    ///< default is TG_NATURAL or tg_env_set_default_index().
    TG_DEFAULT,
    ///< no indexing available, or disabled.
    TG_NONE,
    ///< indexing with natural ring order, for rings/lines
    TG_NATURAL,
    ///< indexing using segment striping, rings only
    TG_YSTRIPES,
}

/// 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.
///
/// ```c
/// 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.
///
/// ```c
/// struct tg_geom *geom = (struct tg_geom*)line; // Cast to a tg_geom
/// ```
///
/// - See [`LineFuncs`]
#[repr(C)]
pub struct tg_line {
    _unused: [u8; 0],
}

/// 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()`][RingFuncs::tg_ring_new] function.
///
/// ```c
/// 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_*()`][PolyFuncs] or [`tg_geom_*()`][RingFuncs] function.
///
/// ```c
/// 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`]
#[repr(C)]
pub struct tg_ring {
    _unused: [u8; 0],
}

/// A polygon consists of one exterior ring and zero or more holes.
///
/// **Creating**
///
/// To create a new polygon use the [`tg_poly_new()`][PolyFuncs::tg_poly_new] function.
///
/// ```c
/// 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_&ast;() function.
///
/// ```c
/// struct tg_geom *geom = (struct tg_geom*)poly; // Cast to a tg_geom
/// ```
///
/// - See [`PolyFuncs`]
#[repr(C)]
pub struct tg_poly {
    _unused: [u8; 0],
}

/// 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 [`GeometryConstructors`] or
/// [`GeometryParsing`] functions.
///
/// ```c
/// 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_&ast;()
/// function.
///
/// ```c
/// 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`]
#[repr(C)]
pub struct tg_geom {
    _unused: [u8; 0],
}

/// Geometry constructors
///
/// Functions for creating and freeing geometries.
pub mod GeometryConstructors {
    use crate::tg_geom;
    use crate::{tg_line, tg_point, tg_poly};

    #[link(name = "tg")]
    extern "C" {
        /// Creates a Point geometry
        ///
        /// Returns a newly allocated geometry, or NULL if system is out of
        /// memory.
        ///
        /// # Safety:
        ///
        /// The caller is responsible for freeing with [`tg_geom_free()`][tg_geom_free].
        pub fn tg_geom_new_point(
            point: tg_point) -> *mut tg_geom;

        /// Creates a LineString geometry.
        ///
        /// Caller retains ownership of the input pointer.
        ///
        /// Returns a newly allocated geometry or NULL if system is out of memory.
        ///
        /// # Safety
        ///
        /// The caller is responsible for freeing with [`tg_geom_free()`][tg_geom_free].
        pub fn tg_geom_new_linestring(line: *const tg_line) -> *mut tg_geom;

        /// Creates a Polygon geometry.
        ///
        /// Caller retains ownership of the input pointer.
        ///
        /// Returns a newly allocated geometry or NULL if system is out of memory.
        ///
        /// # Safety
        ///
        /// The caller is responsible for freeing with [`tg_geom_free()`][tg_geom_free].
        pub fn tg_geom_new_polygon(poly: *const tg_poly) -> *mut tg_geom;

        /// Creates a MultiPoint geometry.
        ///
        /// Returns a newly allocated geometry or NULL if system is out of memory.
        ///
        /// # Params
        ///
        /// - `points`: An array of points, caller retains ownership.
        /// - `npoints`: The number of points in array
        ///
        /// # Safety
        ///
        /// The caller is responsible for freeing with [`tg_geom_free()`][tg_geom_free].
        pub fn tg_geom_new_multipoint(
            points: *const tg_point,
            npoints: libc::c_int,
        ) -> *mut tg_geom;


        /// Creates a MultiLineString geometry.
        ///
        /// Returns a newly allocated geometry or NULL if system is out of memory.
        ///
        /// # Params
        ///
        /// - `lines`: An array of lines, caller retains ownership.
        /// - `nlines`: The number of lines in array
        ///
        /// # Safety
        ///
        /// The caller is responsible for freeing with [`tg_geom_free()`][tg_geom_free].
        pub fn tg_geom_new_multilinestring(
            lines: *const *const tg_line,
            nlines: libc::c_int,
        ) -> *mut tg_geom;

        /// Creates a MultiPolygon geometry.
        ///
        /// Returns a newly allocated geometry or NULL if system is out of memory.
        ///
        /// # Params
        ///
        /// - `polys`: An array of polygons, caller retains ownership.
        /// - `npolys`: The number of polygons in array
        ///
        /// # Safety
        ///
        /// The caller is responsible for freeing with [`tg_geom_free()`][tg_geom_free].
        pub fn tg_geom_new_multipolygon(
            polys: *const *const tg_poly,
            npolys: libc::c_int,
        ) -> *mut tg_geom;

        /// Creates a GeometryCollection geometry.
        ///
        /// Returns a newly allocated geometry or NULL if system is out of memory.
        ///
        /// # Params
        ///
        /// - `geoms`: An array of geometries, caller retains ownership.
        /// - `ngeoms`: The number of geometries in array
        ///
        /// # Safety
        ///
        /// The caller is responsible for freeing with [`tg_geom_free()`][tg_geom_free].
        pub fn tg_geom_new_geometrycollection(
            geoms: *const *const tg_geom,
            ngeoms: libc::c_int,
        ) -> *mut tg_geom;

        /// Clones a geometry
        ///
        /// Returns a duplicate of the provided geometry.
        ///
        /// This method of cloning uses implicit sharing through an atomic
        /// reference counter.
        ///
        /// # Params
        ///
        /// - `geom`: Input geometry, caller retains ownership.
        ///
        /// # Safety
        ///
        /// The caller is responsible for freeing with [`tg_geom_free()`][tg_geom_free].
        pub fn tg_geom_clone(geom: *const tg_geom) -> *mut tg_geom;

        /// Copies a geometry
        ///
        /// Returns a duplicate of the provided geometry or NULL if out of memory
        ///
        /// This method performs a deep copy of the entire geometry to new memory.
        ///
        /// # Params
        ///
        /// - `geom`: Input geometry, caller retains ownership.
        ///
        /// # Safety
        ///
        /// The caller is responsible for freeing with tg_geom_free().
        pub fn tg_geom_copy(geom: *const tg_geom) -> *mut tg_geom;

        /// Releases the memory associated with a geometry.
        /// # Params
        ///
        /// - `geom`: Input geometry
        pub fn tg_geom_free(geom: *mut tg_geom);
    }
}

/// Geometry accessors
///
/// Functions for accessing various information about geometries, such as
/// getting the geometry type or extracting underlying components or
/// coordinates.
pub mod GeometryAccessors {
    // done
    use crate::{tg_geom, tg_geom_type, tg_line, tg_point, tg_poly, tg_rect};

    extern "C" {
        pub fn tg_geom_typeof(geom: *const tg_geom) -> tg_geom_type;
        pub fn tg_geom_type_string(r#type: tg_geom_type) -> *const libc::c_char;
        pub fn tg_geom_rect(geom: *const tg_geom) -> tg_rect;
        pub fn tg_geom_is_feature(geom: *const tg_geom) -> bool;
        pub fn tg_geom_is_featurecollection(geom: *const tg_geom) -> bool;
        pub fn tg_geom_point(geom: *const tg_geom) -> tg_point;
        pub fn tg_geom_line(geom: *const tg_geom) -> *const tg_line;
        pub fn tg_geom_poly(geom: *const tg_geom) -> *const tg_poly;
        pub fn tg_geom_num_points(geom: *const tg_geom) -> libc::c_int;
        pub fn tg_geom_point_at(geom: *const tg_geom, index: libc::c_int) -> tg_point;
        pub fn tg_geom_num_lines(geom: *const tg_geom) -> libc::c_int;
        pub fn tg_geom_line_at(geom: *const tg_geom, index: libc::c_int) -> *const tg_line;
        pub fn tg_geom_num_polys(geom: *const tg_geom) -> libc::c_int;
        pub fn tg_geom_poly_at(geom: *const tg_geom, index: libc::c_int) -> *const tg_poly;
        pub fn tg_geom_num_geometries(geom: *const tg_geom) -> libc::c_int;
        pub fn tg_geom_geometry_at(geom: *const tg_geom, index: libc::c_int) -> *const tg_geom;
        pub fn tg_geom_extra_json(geom: *const tg_geom) -> *const libc::c_char;
        pub fn tg_geom_is_empty(geom: *const tg_geom) -> bool;
        pub fn tg_geom_dims(geom: *const tg_geom) -> libc::c_int;
        pub fn tg_geom_has_z(geom: *const tg_geom) -> bool;
        pub fn tg_geom_has_m(geom: *const tg_geom) -> bool;
        pub fn tg_geom_z(geom: *const tg_geom) -> libc::c_double;
        pub fn tg_geom_m(geom: *const tg_geom) -> libc::c_double;
        pub fn tg_geom_extra_coords(geom: *const tg_geom) -> *const libc::c_double;
        pub fn tg_geom_num_extra_coords(geom: *const tg_geom) -> libc::c_int;
        pub fn tg_geom_memsize(geom: *const tg_geom) -> libc::size_t;
        pub fn tg_geom_search(
            geom: *const tg_geom,
            rect: tg_rect,
            iter: extern "C" fn(
                geom: *const tg_geom,
                index: libc::c_int,
                udata: *mut libc::c_void,
            ) -> bool,
            udata: *mut libc::c_void,
        );
    }
}

/// Geometry predicates
///
/// Functions for testing the spatial relations of two geometries.
pub mod GeometryPredicates {
    // done
    use crate::tg_geom;
    use crate::tg_rect;

    #[link(name = "tg")]
    extern "C" {
        pub fn tg_geom_equals(a: *const tg_geom, b: *const tg_geom) -> bool;
        pub fn tg_geom_intersects(a: *const tg_geom, b: *const tg_geom) -> bool;
        pub fn tg_geom_disjoint(a: *const tg_geom, b: *const tg_geom) -> bool;
        pub fn tg_geom_contains(a: *const tg_geom, b: *const tg_geom) -> bool;
        pub fn tg_geom_within(a: *const tg_geom, b: *const tg_geom) -> bool;
        pub fn tg_geom_covers(a: *const tg_geom, b: *const tg_geom) -> bool;
        pub fn tg_geom_coveredby(a: *const tg_geom, b: *const tg_geom) -> bool;
        pub fn tg_geom_intersects_rect(a: *const tg_geom, b: tg_rect) -> bool;
        pub fn tg_geom_intersects_xy(
            a: *const tg_geom,
            x: libc::c_double,
            y: libc::c_double,
        ) -> bool;
    }
}

/// Geometry parsing
///
/// Functions for parsing geometries from external data representations.
/// It's recommended to use tg_geom_error() after parsing to check for errors.
pub mod GeometryParsing {
    // done

    use crate::{tg_geom, tg_index};

    #[link(name = "tg")]
    extern "C" {
        pub fn tg_parse_geojson(geojson: *const libc::c_char) -> *mut tg_geom;
        pub fn tg_parse_geojsonn(geojson: *const libc::c_char, len: libc::size_t) -> *mut tg_geom;
        pub fn tg_parse_geojson_ix(geojson: *const libc::c_char, ix: tg_index) -> *mut tg_geom;
        pub fn tg_parse_geojsonn_ix(
            geojson: *const libc::c_char,
            len: libc::size_t,
            ix: tg_index,
        ) -> *mut tg_geom;
        pub fn tg_parse_wkt(wkt: *const libc::c_char) -> *mut tg_geom;
        pub fn tg_parse_wktn(wkt: *const libc::c_char, len: libc::size_t) -> *mut tg_geom;
        pub fn tg_parse_wkt_ix(wkt: *const libc::c_char, ix: tg_index) -> *mut tg_geom;
        pub fn tg_parse_wktn_ix(
            wkt: *const libc::c_char,
            len: libc::size_t,
            ix: tg_index,
        ) -> *mut tg_geom;
        pub fn tg_parse_wkb(wkb: *const u8, len: libc::size_t) -> *mut tg_geom;
        pub fn tg_parse_wkb_ix(wkb: *const u8, len: libc::size_t, ix: tg_index) -> *mut tg_geom;
        pub fn tg_parse_hex(hex: *const libc::c_char) -> *mut tg_geom;
        pub fn tg_parse_hexn(hex: *const libc::c_char, len: libc::size_t) -> *mut tg_geom;
        pub fn tg_parse_hex_ix(hex: *const libc::c_char, ix: tg_index) -> *mut tg_geom;
        pub fn tg_parse_hexn_ix(
            hex: *const libc::c_char,
            len: libc::size_t,
            ix: tg_index,
        ) -> *mut tg_geom;
        pub fn tg_geom_error(geom: *const tg_geom) -> *const libc::c_char;
    }
}

/// Geometry writing
///
/// Functions for writing geometries as external data representations.
pub mod GeometryWriting {
    // done

    use crate::tg_geom;

    extern "C" {

        pub fn tg_geom_geojson(
            geom: *const tg_geom,
            dst: *mut libc::c_char,
            n: libc::size_t,
        ) -> libc::size_t;
        pub fn tg_geom_wkt(
            geom: *const tg_geom,
            dst: *mut libc::c_char,
            n: libc::size_t,
        ) -> libc::size_t;
        pub fn tg_geom_wkb(geom: *const tg_geom, dst: *mut u8, n: libc::size_t) -> libc::size_t;
        pub fn tg_geom_hex(
            geom: *const tg_geom,
            dst: *mut libc::c_char,
            n: libc::size_t,
        ) -> libc::size_t;
    }
}

/// Geometry with alternative dimensions
///
/// Functions for working with geometries that have more than two dimensions or
/// are empty. The extra dimensional coordinates contained within these
/// geometries are only carried along and serve no other purpose than to be
/// available for when it's desired to export to an output representation such
/// as GeoJSON, WKT, or WKB.
pub mod GeometryConstructorsEx {
    // done

    use crate::{tg_geom, tg_line, tg_point, tg_poly};

    extern "C" {

        pub fn tg_geom_new_point_z(point: tg_point, z: libc::c_double) -> *mut tg_geom;
        pub fn tg_geom_new_point_m(point: tg_point, m: libc::c_double) -> *mut tg_geom;
        pub fn tg_geom_new_point_zm(
            point: tg_point,
            z: libc::c_double,
            m: libc::c_double,
        ) -> *mut tg_geom;
        pub fn tg_geom_new_point_empty() -> *mut tg_geom;
        pub fn tg_geom_new_linestring_z(
            line: *const tg_line,
            extra_coords: *const libc::c_double,
            ncoords: libc::c_int,
        ) -> *mut tg_geom;
        pub fn tg_geom_new_linestring_m(
            line: *const tg_line,
            extra_coords: *const libc::c_double,
            ncoords: libc::c_int,
        ) -> *mut tg_geom;
        pub fn tg_geom_new_linestring_zm(
            line: *const tg_line,
            extra_coords: *const libc::c_double,
            ncoords: libc::c_int,
        ) -> *mut tg_geom;
        pub fn tg_geom_new_linestring_empty() -> *mut tg_geom;
        pub fn tg_geom_new_polygon_z(
            poly: *const tg_poly,
            extra_coords: *const libc::c_double,
            ncoords: libc::c_int,
        ) -> *mut tg_geom;
        pub fn tg_geom_new_polygon_m(
            poly: *const tg_poly,
            extra_coords: *const libc::c_double,
            ncoords: libc::c_int,
        ) -> *mut tg_geom;
        pub fn tg_geom_new_polygon_zm(
            poly: *const tg_poly,
            extra_coords: *const libc::c_double,
            ncoords: libc::c_int,
        ) -> *mut tg_geom;
        pub fn tg_geom_new_polygon_empty() -> *mut tg_geom;
        pub fn tg_geom_new_multipoint_z(
            points: *const tg_point,
            npoints: libc::c_int,
            extra_coords: *const libc::c_double,
            ncoords: libc::c_int,
        ) -> *mut tg_geom;
        pub fn tg_geom_new_multipoint_m(
            points: *const tg_point,
            npoints: libc::c_int,
            extra_coords: *const libc::c_double,
            ncoords: libc::c_int,
        ) -> *mut tg_geom;
        pub fn tg_geom_new_multipoint_zm(
            points: *const tg_point,
            npoints: libc::c_int,
            extra_coords: *const libc::c_double,
            ncoords: libc::c_int,
        ) -> *mut tg_geom;
        pub fn tg_geom_new_multipoint_empty() -> *mut tg_geom;
        pub fn tg_geom_new_multilinestring_z(
            lines: *const *const tg_line,
            nlines: libc::c_int,
            extra_coords: *const libc::c_double,
            ncoords: libc::c_int,
        ) -> *mut tg_geom;
        pub fn tg_geom_new_multilinestring_m(
            lines: *const *const tg_line,
            nlines: libc::c_int,
            extra_coords: *const libc::c_double,
            ncoords: libc::c_int,
        ) -> *mut tg_geom;
        pub fn tg_geom_new_multilinestring_zm(
            lines: *const *const tg_line,
            nlines: libc::c_int,
            extra_coords: *const libc::c_double,
            ncoords: libc::c_int,
        ) -> *mut tg_geom;
        pub fn tg_geom_new_multilinestring_empty() -> *mut tg_geom;
        pub fn tg_geom_new_multipolygon_z(
            polys: *const *const tg_poly,
            npolys: libc::c_int,
            extra_coords: *const libc::c_double,
            ncoords: libc::c_int,
        ) -> *mut tg_geom;
        pub fn tg_geom_new_multipolygon_m(
            polys: *const *const tg_poly,
            npolys: libc::c_int,
            extra_coords: *const libc::c_double,
            ncoords: libc::c_int,
        ) -> *mut tg_geom;
        pub fn tg_geom_new_multipolygon_zm(
            polys: *const *const tg_poly,
            npolys: libc::c_int,
            extra_coords: *const libc::c_double,
            ncoords: libc::c_int,
        ) -> *mut tg_geom;
        pub fn tg_geom_new_multipolygon_empty() -> *mut tg_geom;
        pub fn tg_geom_new_geometrycollection_empty() -> *mut tg_geom;
    }
}

/// Point functions
///
/// Functions for working directly with the tg_point type.
pub mod PointFuncs {
    use crate::{tg_point, tg_rect};
    #[link(name = "tg")]
    extern "C" {
        pub fn tg_point_rect(point: tg_point) -> tg_rect;
        pub fn tg_point_intersects_rect(point: tg_point, rect: tg_rect) -> bool;
    }
}

/// Segment functions
///
/// Functions for working directly with the tg_segment type.
pub mod SegmentFuncs {
    use crate::{tg_rect, tg_segment};
    #[link(name = "tg")]
    extern "C" {
        pub fn tg_segment_rect(s: tg_segment) -> tg_rect;
        pub fn tg_segment_intersects_segment(a: tg_segment, b: tg_segment) -> bool;
    }
}

/// Rectangle functions
///
/// Functions for working directly with the tg_rect type.
pub mod RectFuncs {
    // done

    use crate::{tg_point, tg_rect};

    extern "C" {
        pub fn tg_rect_expand(rect: tg_rect, other: tg_rect) -> tg_rect;
        pub fn tg_rect_expand_point(rect: tg_rect, point: tg_point) -> tg_rect;
        pub fn tg_rect_center(rect: tg_rect) -> tg_point;
        pub fn tg_rect_intersects_rect(a: tg_rect, b: tg_rect) -> bool;
        pub fn tg_rect_intersects_point(a: tg_rect, b: tg_point) -> bool;
    }
}

/// Ring functions
///
/// Functions for working directly with the tg_ring type.
///
/// There are no direct spatial predicates for tg_ring.
/// If you want to perform operations like "intersects" or "covers" then you
/// must upcast the ring to a tg_geom, like such:
///
/// ```c
/// tg_geom_intersects((struct tg_geom*)ring, geom);
/// ```
pub mod RingFuncs {
    // done
    use crate::{tg_index, tg_line, tg_point, tg_rect, tg_ring, tg_segment};
    extern "C" {
        pub fn tg_ring_new(points: *const tg_point, npoints: libc::c_int) -> *mut tg_ring;
        pub fn tg_ring_new_ix(
            points: *const tg_point,
            npoints: libc::c_int,
            ix: tg_index,
        ) -> *mut tg_ring;
        pub fn tg_ring_free(ring: *mut tg_ring);
        pub fn tg_ring_clone(ring: *const tg_ring) -> *mut tg_ring;
        pub fn tg_ring_copy(ring: *const tg_ring) -> *mut tg_ring;
        pub fn tg_ring_memsize(ring: *const tg_ring) -> libc::size_t;
        pub fn tg_ring_rect(ring: *const tg_ring) -> tg_rect;
        pub fn tg_ring_num_points(ring: *const tg_ring) -> libc::c_int;
        pub fn tg_ring_point_at(ring: *const tg_ring, index: libc::c_int) -> tg_point;
        pub fn tg_ring_points(ring: *const tg_ring) -> *const tg_point;
        pub fn tg_ring_num_segments(ring: *const tg_ring) -> libc::c_int;
        pub fn tg_ring_segment_at(ring: *const tg_ring, index: libc::c_int) -> tg_segment;
        pub fn tg_ring_convex(ring: *const tg_ring) -> bool;
        pub fn tg_ring_clockwise(ring: *const tg_ring) -> bool;
        pub fn tg_ring_index_spread(ring: *const tg_ring) -> libc::c_int;
        pub fn tg_ring_index_num_levels(ring: *const tg_ring) -> libc::c_int;
        pub fn tg_ring_index_level_num_rects(
            ring: *const tg_ring,
            levelidx: libc::c_int,
        ) -> libc::c_int;
        pub fn tg_ring_index_level_rect(
            ring: *const tg_ring,
            levelidx: libc::c_int,
            rectidx: libc::c_int,
        ) -> tg_rect;

        /// Iterates over segments from nearest to farthest.
        ///
        /// This is a kNN operation.
        ///
        /// The caller must provide their own "rect_dist" and "seg_dist" callbacks to
        /// do the actual distance calculations.
        ///
        /// @param ring Input ring
        ///
        /// @param rect_dist Callback that returns the distance to a tg_rect.
        ///
        /// @param seg_dist Callback that returns the distance to a tg_segment.
        ///
        /// @param iter Callback that returns each segment in the ring in order of
        /// nearest to farthest. Caller must return true to continue to the next
        /// segment, or return false to stop iterating.
        ///
        /// @param udata User-defined data
        ///
        /// @return True if operation succeeded, false if out of memory.
        ///
        /// @note Though not typical, this operation may need to allocate memory.
        /// It's recommended to check the return value for success.
        /// @note The `*more` argument is an optional ref-value that is used for
        /// performing partial step-based or probability-based calculations. A detailed
        /// description of its use is outside the scope of this document. Ignoring it
        /// altogether is the preferred behavior.
        pub fn tg_ring_nearest_segment(
            ring: *const tg_ring,
            rect_dist: extern "C" fn(
                rect: tg_rect,
                more: *mut libc::c_int,
                udata: *mut libc::c_void,
            ) -> libc::c_double,
            seg_dist: extern "C" fn(
                seg: tg_segment,
                more: *mut libc::c_int,
                udata: *mut libc::c_void,
            ) -> libc::c_double,
            iter: extern "C" fn(
                seg: tg_segment,
                dist: libc::c_double,
                index: libc::c_int,
                udata: *mut libc::c_void,
            ) -> bool,
            udata: *mut libc::c_void,
        ) -> bool;

        pub fn tg_ring_line_search(
            a: *const tg_ring,
            b: *const tg_line,
            iter: extern "C" fn(
                aseg: tg_segment,
                aidx: libc::c_int,
                bseg: tg_segment,
                bidx: libc::c_int,
                udata: *mut libc::c_void,
            ) -> bool,
            udata: *mut libc::c_void,
        );
        pub fn tg_ring_ring_search(
            a: *const tg_ring,
            b: *const tg_ring,
            iter: extern "C" fn(
                aseg: tg_segment,
                aidx: libc::c_int,
                bseg: tg_segment,
                bidx: libc::c_int,
                udata: *mut libc::c_void,
            ) -> bool,
            udata: *mut libc::c_void,
        );
        pub fn tg_ring_area(libc: *const tg_ring) -> libc::c_double;
        pub fn tg_ring_perimeter(libc: *const tg_ring) -> libc::c_double;
    }
}

/// Line functions
///
/// Functions for working directly with the tg_line type.
///
/// There are no direct spatial predicates for tg_line.
/// If you want to perform operations like "intersects" or "covers" then you
/// must upcast the line to a tg_geom, like such:
///
/// ```c
/// tg_geom_intersects((struct tg_geom*)line, geom);
/// ```
pub mod LineFuncs {
    // done

    use crate::{tg_index, tg_line, tg_point, tg_rect, tg_segment};

    extern "C" {

        /// Creates a line from a series of points.
        /// @param points Array of points
        /// @param npoints Number of points in array
        /// @return A newly allocated line
        /// @return NULL if out of memory
        /// @note A tg_line can be safely upcasted to a tg_geom. `(struct tg_geom*)line`
        /// @note All lines with 32 or more points are automatically indexed.
        pub fn tg_line_new(points: *const tg_point, npoints: libc::c_int) -> *mut tg_line;

        /// Creates a line from a series of points using provided index option.
        /// @param points Array of points
        /// @param npoints Number of points in array
        /// @param ix Indexing option, e.g. TG_NONE, TG_NATURAL, TG_YSTRIPES
        /// @return A newly allocated line
        /// @return NULL if out of memory
        /// @note A tg_line can be safely upcasted to a tg_geom. `(struct tg_geom*)poly`
        /// @see [tg_index](.#tg_index)
        pub fn tg_line_new_ix(
            points: *const tg_point,
            npoints: libc::c_int,
            ix: tg_index,
        ) -> *mut tg_line;

        /// Releases the memory associated with a line.
        /// @param line Input line
        pub fn tg_line_free(line: *mut tg_line);

        /// Clones a line
        /// @param line Input line, caller retains ownership.
        /// @return A duplicate of the provided line.
        /// @note The caller is responsible for freeing with tg_line_free().
        /// @note This method of cloning uses implicit sharing through an atomic
        /// reference counter.
        pub fn tg_line_clone(line: *const tg_line) -> *mut tg_line;

        /// Copies a line
        /// @param line Input line, caller retains ownership.
        /// @return A duplicate of the provided line.
        /// @return NULL if out of memory
        /// @note The caller is responsible for freeing with tg_line_free().
        /// @note This method performs a deep copy of the entire geometry to new memory.
        pub fn tg_line_copy(line: *const tg_line) -> *mut tg_line;

        /// Returns the allocation size of the line.
        /// @param line Input line
        /// @return Size of line in bytes
        pub fn tg_line_memsize(line: *const tg_line) -> libc::size_t;

        /// Returns the minimum bounding rectangle of a line.
        pub fn tg_line_rect(line: *const tg_line) -> tg_rect;

        /// Returns the number of points.
        /// @param line Input line
        /// @return Number of points
        /// @see tg_line_point_at()
        pub fn tg_line_num_points(line: *const tg_line) -> libc::c_int;

        /// Returns the underlying point array of a line.
        /// @param line Input line
        /// @return Array or points
        /// @see tg_line_num_points()
        /// @see LineFuncs
        pub fn tg_line_points(line: *const tg_line) -> *const tg_point;

        /// Returns the point at index.
        /// @param line Input line
        /// @param index Index of point
        /// @return The point at index
        /// @note This function performs bounds checking. Use tg_line_points() for
        /// direct access to the points.
        /// @see tg_line_num_points()
        pub fn tg_line_point_at(line: *const tg_line, index: libc::c_int) -> tg_point;

        /// Returns the number of segments.
        /// @param line Input line
        /// @return Number of segments
        /// @see tg_line_segment_at()
        /// @see LineFuncs
        pub fn tg_line_num_segments(line: *const tg_line) -> libc::c_int;

        /// Returns the segment at index.
        /// @param line Input line
        /// @param index Index of segment
        /// @return The segment at index
        /// @see tg_line_num_segments()
        pub fn tg_line_segment_at(line: *const tg_line, index: libc::c_int) -> tg_segment;

        /// Returns true if winding order is clockwise.
        /// @param line Input line
        /// @return True if clockwise
        /// @return False if counter-clockwise
        pub fn tg_line_clockwise(line: *const tg_line) -> bool;

        /// Returns the indexing spread for a line.
        ///
        /// The "spread" is the number of segments or rectangles that are grouped
        /// together to produce a unioned rectangle that is stored at a higher level.
        ///
        /// For a tree based structure, this would be the number of items per node.
        ///
        /// @param line Input line
        /// @return The spread, default is 16
        /// @return Zero if line has no indexing
        /// @see tg_line_index_num_levels()
        /// @see tg_line_index_level_num_rects()
        /// @see tg_line_index_level_rect()
        pub fn tg_line_index_spread(line: *const tg_line) -> libc::c_int;

        /// Returns the number of levels.
        /// @param line Input line
        /// @return The number of levels
        /// @return Zero if line has no indexing
        /// @see tg_line_index_spread()
        /// @see tg_line_index_level_num_rects()
        /// @see tg_line_index_level_rect()
        pub fn tg_line_index_num_levels(line: *const tg_line) -> libc::c_int;

        /// Returns the number of rectangles at level.
        /// @param line Input line
        /// @param levelidx The index of level
        /// @return The number of index levels
        /// @return Zero if line has no indexing or levelidx is out of bounds.
        /// @see tg_line_index_spread()
        /// @see tg_line_index_num_levels()
        /// @see tg_line_index_level_rect()
        pub fn tg_line_index_level_num_rects(
            line: *const tg_line,
            levelidx: libc::c_int,
        ) -> libc::c_int;

        /// Returns a specific level rectangle.
        /// @param line Input line
        /// @param levelidx The index of level
        /// @param rectidx The index of rectangle
        /// @return The rectangle
        /// @return Empty rectangle if line has no indexing, or levelidx or rectidx
        /// is out of bounds.
        /// @see tg_line_index_spread()
        /// @see tg_line_index_num_levels()
        /// @see tg_line_index_level_num_rects()
        pub fn tg_line_index_level_rect(
            line: *const tg_line,
            levelidx: libc::c_int,
            rectidx: libc::c_int,
        ) -> tg_rect;

        /// Iterates over segments from nearest to farthest.
        /// @see [`tg_ring_nearest_segment()`][crate::RingFuncs::tg_ring_nearest_segment]`, which shares the same interface, for a
        /// detailed description.
        pub fn tg_line_nearest_segment(
            line: *const tg_line,
            rect_dist: extern "C" fn(
                rect: tg_rect,
                more: *mut libc::c_int,
                udata: *mut libc::c_void,
            ) -> libc::c_double,
            seg_dist: extern "C" fn(
                seg: tg_segment,
                more: *mut libc::c_int,
                udata: *mut libc::c_void,
            ) -> libc::c_double,
            iter: extern "C" fn(
                seg: tg_segment,
                dist: libc::c_double,
                index: libc::c_int,
                udata: *mut libc::c_void,
            ) -> bool,
            udata: *mut libc::c_void,
        ) -> bool;

        /// Iterates over all segments in line A that intersect with segments in line B.
        /// @note This efficently uses the indexes of each geometry, if available.
        pub fn tg_line_line_search(
            a: *const tg_line,
            b: *const tg_line,
            iter: extern "C" fn(
                aseg: tg_segment,
                aidx: libc::c_int,
                bseg: tg_segment,
                bidx: libc::c_int,
                udata: *mut libc::c_void,
            ) -> bool,
            udata: *mut libc::c_void,
        );

        /// Calculate the length of a line.
        pub fn tg_line_length(line: *const tg_line) -> libc::c_double;
    }
}

/// Polygon functions
///
/// Functions for working directly with the tg_poly type.
///
/// There are no direct spatial predicates for tg_poly.
/// If you want to perform operations like "intersects" or "covers" then you
/// must upcast the poly to a tg_geom, like such:
///
/// ```c
/// tg_geom_intersects((struct tg_geom*)poly, geom);
/// ```
pub mod PolyFuncs {
    // done

    use crate::{tg_poly, tg_rect, tg_ring};

    extern "C" {
        pub fn tg_poly_new(
            exterior: *const tg_ring,
            holes: *const *const tg_ring,
            nholes: libc::c_int,
        ) -> *mut tg_poly;
        pub fn tg_poly_free(poly: *mut tg_poly);
        pub fn tg_poly_clone(poly: *const tg_poly) -> *mut tg_poly;
        pub fn tg_poly_copy(poly: *const tg_poly) -> *mut tg_poly;
        pub fn tg_poly_memsize(poly: *const tg_poly) -> libc::size_t;
        pub fn tg_poly_exterior(poly: *const tg_poly) -> *const tg_ring;
        pub fn tg_poly_num_holes(poly: *const tg_poly) -> libc::c_int;
        pub fn tg_poly_hole_at(poly: *const tg_poly, index: libc::c_int) -> *const tg_ring;
        pub fn tg_poly_rect(poly: *const tg_poly) -> tg_rect;
        pub fn tg_poly_clockwise(poly: *const tg_poly) -> bool;
    }
}

/// @defgroup GlobalFuncs Global environment
/// Functions for optionally setting the behavior of the TG environment.
/// These, if desired, should be called only once at program start up and prior
/// to calling any other tg_*() functions.
pub mod GlobalFuncs {
    // done

    use crate::tg_index;
    extern "C" {

        pub fn tg_env_set_allocator(
            malloc: extern "C" fn(size: libc::size_t) -> *mut libc::c_void,
            realloc: extern "C" fn(
                alloc: *mut libc::c_void,
                size: libc::size_t,
            ) -> *mut libc::c_void,
            free: extern "C" fn(),
        );
        pub fn tg_env_set_index(ix: tg_index);
        pub fn tg_env_set_index_spread(spread: libc::c_int);
    }
}

// Code from tg.h follows //
////////////////////////////

/*

// https://github.com/tidwall/tg
//
// Copyright 2023 Joshua J Baker. All rights reserved.
// Use of this source code is governed by a license
// that can be found in the LICENSE file.

#ifndef TG_H
#define TG_H

#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>

/// [SNIP]

#endif // TG_H

*/