tg_sys/lib.rs
1#![allow(nonstandard_style)]
2
3/// The base point type used for all geometries.
4///
5/// - See [`PointFuncs`]
6#[repr(C)]
7#[derive(Clone, Copy, Debug, PartialEq)]
8pub struct tg_point {
9 pub x: libc::c_double,
10 pub y: libc::c_double,
11}
12
13
14/// The base segment type used in [`tg_line`] and [`tg_ring`] for joining two vertices.
15///
16/// - See [`SegmentFuncs`]`
17#[repr(C)]
18#[derive(Clone, Copy, Debug, PartialEq)]
19pub struct tg_segment {
20 pub a: tg_point,
21 pub b: tg_point,
22}
23
24/// A rectangle defined by a minimum and maximum coordinates.
25/// Returned by the [`tg_geom_rect()`][GeometryAccessors::tg_geom_rect], [`tg_ring_rect()`][RingFuncs::tg_ring_rect], and other `\*_rect()`
26/// functions for getting a geometry's minumum bounding rectangle.
27/// Also used internally for geometry indexing.
28///
29/// - See [`RectFuncs`]
30#[repr(C)]
31#[derive(Clone, Copy, Debug, PartialEq)]
32pub struct tg_rect {
33 pub min: tg_point,
34 pub max: tg_point,
35}
36
37/// Geometry types.
38///
39/// All tg_geom are one of the following underlying types.
40///
41/// - See [`tg_geom_typeof()`][`GeometryAccessors::tg_geom_type_string`]`
42/// - See [`tg_geom_type_string()`][`GeometryAccessors::tg_geom_type_string`]
43/// - See [`GeometryAccessors`]
44#[repr(C)]
45pub enum tg_geom_type {
46 ///< Point
47 TG_POINT = 1,
48 ///< LineString
49 TG_LINESTRING = 2,
50 ///< Polygon
51 TG_POLYGON = 3,
52 ///< MultiPoint, collection of points
53 TG_MULTIPOINT = 4,
54 ///< MultiLineString, collection of linestrings
55 TG_MULTILINESTRING = 5,
56 ///< MultiPolygon, collection of polygons
57 TG_MULTIPOLYGON = 6,
58 ///< GeometryCollection, collection of geometries
59 TG_GEOMETRYCOLLECTION = 7,
60}
61
62/// Geometry indexing options.
63///
64/// Used for polygons, rings, and lines to make the point-in-polygon and
65/// geometry intersection operations fast.
66///
67/// An index can also be used for efficiently traversing, searching, and
68/// performing nearest-neighbor (kNN) queries on the segment using
69/// [`tg_ring_index_*()`][RingFuncs] and [`tg_ring_nearest_segment()`][RingFuncs::tg_ring_nearest_segment] functions.
70#[repr(C)]
71pub enum tg_index {
72 ///< default is TG_NATURAL or tg_env_set_default_index().
73 TG_DEFAULT,
74 ///< no indexing available, or disabled.
75 TG_NONE,
76 ///< indexing with natural ring order, for rings/lines
77 TG_NATURAL,
78 ///< indexing using segment striping, rings only
79 TG_YSTRIPES,
80}
81
82/// A line is a series of tg_segment that make up a linestring.
83///
84/// **Creating**
85///
86/// To create a new line use the tg_line_new() function.
87///
88/// ```c
89/// struct tg_line *line = tg_line_new(points, npoints);
90/// ```
91///
92/// **Upcasting**
93///
94/// A tg_line can always be safely upcasted to a [`tg_geom`]; allowing
95/// it to use any `tg_geom_*()` function.
96///
97/// ```c
98/// struct tg_geom *geom = (struct tg_geom*)line; // Cast to a tg_geom
99/// ```
100///
101/// - See [`LineFuncs`]
102#[repr(C)]
103pub struct tg_line {
104 _unused: [u8; 0],
105}
106
107/// A ring is series of [`tg_segment`] which creates a shape that does not
108/// self-intersect and is fully closed, where the start and end points are the
109/// exact same value.
110///
111/// **Creating**
112///
113/// To create a new ring use the [`tg_ring_new()`][RingFuncs::tg_ring_new] function.
114///
115/// ```c
116/// struct tg_ring *ring = tg_ring_new(points, npoints);
117/// ```
118///
119/// **Upcasting**
120///
121/// A tg_ring can always be safely upcasted to a [`tg_poly`] or [`tg_geom`]; allowing
122/// it to use any [`tg_poly_*()`][PolyFuncs] or [`tg_geom_*()`][RingFuncs] function.
123///
124/// ```c
125/// struct tg_poly *poly = (struct tg_poly*)ring; // Cast to a tg_poly
126/// struct tg_geom *geom = (struct tg_geom*)ring; // Cast to a tg_geom
127/// ```
128/// - See [`RingFuncs`]
129/// - See [`PolyFuncs`]
130#[repr(C)]
131pub struct tg_ring {
132 _unused: [u8; 0],
133}
134
135/// A polygon consists of one exterior ring and zero or more holes.
136///
137/// **Creating**
138///
139/// To create a new polygon use the [`tg_poly_new()`][PolyFuncs::tg_poly_new] function.
140///
141/// ```c
142/// struct tg_poly *poly = tg_poly_new(exterior, holes, nholes);
143/// ```
144///
145/// **Upcasting**
146///
147/// A tg_poly can always be safely upcasted to a [`tg_geom`]; allowing
148/// it to use any tg_geom_*() function.
149///
150/// ```c
151/// struct tg_geom *geom = (struct tg_geom*)poly; // Cast to a tg_geom
152/// ```
153///
154/// - See [`PolyFuncs`]
155#[repr(C)]
156pub struct tg_poly {
157 _unused: [u8; 0],
158}
159
160/// A geometry is the common generic type that can represent a Point,
161/// LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, or
162/// GeometryCollection.
163///
164/// For geometries that are derived from GeoJSON, they may have addtional
165/// attributes such as being a Feature or a FeatureCollection; or include
166/// extra json fields.
167///
168/// **Creating**
169///
170/// To create a new geometry use one of the [`GeometryConstructors`] or
171/// [`GeometryParsing`] functions.
172///
173/// ```c
174/// struct tg_geom *geom = tg_geom_new_point(point);
175/// struct tg_geom *geom = tg_geom_new_polygon(poly);
176/// struct tg_geom *geom = tg_parse_geojson(geojson);
177/// ```
178///
179/// **Upcasting**
180///
181/// Other types, specifically [`tg_line`], [`tg_ring`], and [`tg_poly`], can be safely
182/// upcasted to a tg_geom; allowing them to use any tg_geom_*()
183/// function.
184///
185/// ```c
186/// struct tg_geom *geom1 = (struct tg_geom*)line; // Cast to a LineString
187/// struct tg_geom *geom2 = (struct tg_geom*)ring; // Cast to a Polygon
188/// struct tg_geom *geom3 = (struct tg_geom*)poly; // Cast to a Polygon
189/// ```
190///
191/// - See [`GeometryConstructors`]
192/// - See [`GeometryAccessors`]
193/// - See [`GeometryPredicates`]
194/// - See [`GeometryParsing`]
195/// - See [`GeometryWriting`]
196#[repr(C)]
197pub struct tg_geom {
198 _unused: [u8; 0],
199}
200
201/// Geometry constructors
202///
203/// Functions for creating and freeing geometries.
204pub mod GeometryConstructors {
205 use crate::tg_geom;
206 use crate::{tg_line, tg_point, tg_poly};
207
208 #[link(name = "tg")]
209 extern "C" {
210 /// Creates a Point geometry
211 ///
212 /// Returns a newly allocated geometry, or NULL if system is out of
213 /// memory.
214 ///
215 /// # Safety:
216 ///
217 /// The caller is responsible for freeing with [`tg_geom_free()`][tg_geom_free].
218 pub fn tg_geom_new_point(
219 point: tg_point) -> *mut tg_geom;
220
221 /// Creates a LineString geometry.
222 ///
223 /// Caller retains ownership of the input pointer.
224 ///
225 /// Returns a newly allocated geometry or NULL if system is out of memory.
226 ///
227 /// # Safety
228 ///
229 /// The caller is responsible for freeing with [`tg_geom_free()`][tg_geom_free].
230 pub fn tg_geom_new_linestring(line: *const tg_line) -> *mut tg_geom;
231
232 /// Creates a Polygon geometry.
233 ///
234 /// Caller retains ownership of the input pointer.
235 ///
236 /// Returns a newly allocated geometry or NULL if system is out of memory.
237 ///
238 /// # Safety
239 ///
240 /// The caller is responsible for freeing with [`tg_geom_free()`][tg_geom_free].
241 pub fn tg_geom_new_polygon(poly: *const tg_poly) -> *mut tg_geom;
242
243 /// Creates a MultiPoint geometry.
244 ///
245 /// Returns a newly allocated geometry or NULL if system is out of memory.
246 ///
247 /// # Params
248 ///
249 /// - `points`: An array of points, caller retains ownership.
250 /// - `npoints`: The number of points in array
251 ///
252 /// # Safety
253 ///
254 /// The caller is responsible for freeing with [`tg_geom_free()`][tg_geom_free].
255 pub fn tg_geom_new_multipoint(
256 points: *const tg_point,
257 npoints: libc::c_int,
258 ) -> *mut tg_geom;
259
260
261 /// Creates a MultiLineString geometry.
262 ///
263 /// Returns a newly allocated geometry or NULL if system is out of memory.
264 ///
265 /// # Params
266 ///
267 /// - `lines`: An array of lines, caller retains ownership.
268 /// - `nlines`: The number of lines in array
269 ///
270 /// # Safety
271 ///
272 /// The caller is responsible for freeing with [`tg_geom_free()`][tg_geom_free].
273 pub fn tg_geom_new_multilinestring(
274 lines: *const *const tg_line,
275 nlines: libc::c_int,
276 ) -> *mut tg_geom;
277
278 /// Creates a MultiPolygon geometry.
279 ///
280 /// Returns a newly allocated geometry or NULL if system is out of memory.
281 ///
282 /// # Params
283 ///
284 /// - `polys`: An array of polygons, caller retains ownership.
285 /// - `npolys`: The number of polygons in array
286 ///
287 /// # Safety
288 ///
289 /// The caller is responsible for freeing with [`tg_geom_free()`][tg_geom_free].
290 pub fn tg_geom_new_multipolygon(
291 polys: *const *const tg_poly,
292 npolys: libc::c_int,
293 ) -> *mut tg_geom;
294
295 /// Creates a GeometryCollection geometry.
296 ///
297 /// Returns a newly allocated geometry or NULL if system is out of memory.
298 ///
299 /// # Params
300 ///
301 /// - `geoms`: An array of geometries, caller retains ownership.
302 /// - `ngeoms`: The number of geometries in array
303 ///
304 /// # Safety
305 ///
306 /// The caller is responsible for freeing with [`tg_geom_free()`][tg_geom_free].
307 pub fn tg_geom_new_geometrycollection(
308 geoms: *const *const tg_geom,
309 ngeoms: libc::c_int,
310 ) -> *mut tg_geom;
311
312 /// Clones a geometry
313 ///
314 /// Returns a duplicate of the provided geometry.
315 ///
316 /// This method of cloning uses implicit sharing through an atomic
317 /// reference counter.
318 ///
319 /// # Params
320 ///
321 /// - `geom`: Input geometry, caller retains ownership.
322 ///
323 /// # Safety
324 ///
325 /// The caller is responsible for freeing with [`tg_geom_free()`][tg_geom_free].
326 pub fn tg_geom_clone(geom: *const tg_geom) -> *mut tg_geom;
327
328 /// Copies a geometry
329 ///
330 /// Returns a duplicate of the provided geometry or NULL if out of memory
331 ///
332 /// This method performs a deep copy of the entire geometry to new memory.
333 ///
334 /// # Params
335 ///
336 /// - `geom`: Input geometry, caller retains ownership.
337 ///
338 /// # Safety
339 ///
340 /// The caller is responsible for freeing with tg_geom_free().
341 pub fn tg_geom_copy(geom: *const tg_geom) -> *mut tg_geom;
342
343 /// Releases the memory associated with a geometry.
344 /// # Params
345 ///
346 /// - `geom`: Input geometry
347 pub fn tg_geom_free(geom: *mut tg_geom);
348 }
349}
350
351/// Geometry accessors
352///
353/// Functions for accessing various information about geometries, such as
354/// getting the geometry type or extracting underlying components or
355/// coordinates.
356pub mod GeometryAccessors {
357 // done
358 use crate::{tg_geom, tg_geom_type, tg_line, tg_point, tg_poly, tg_rect};
359
360 extern "C" {
361 pub fn tg_geom_typeof(geom: *const tg_geom) -> tg_geom_type;
362 pub fn tg_geom_type_string(r#type: tg_geom_type) -> *const libc::c_char;
363 pub fn tg_geom_rect(geom: *const tg_geom) -> tg_rect;
364 pub fn tg_geom_is_feature(geom: *const tg_geom) -> bool;
365 pub fn tg_geom_is_featurecollection(geom: *const tg_geom) -> bool;
366 pub fn tg_geom_point(geom: *const tg_geom) -> tg_point;
367 pub fn tg_geom_line(geom: *const tg_geom) -> *const tg_line;
368 pub fn tg_geom_poly(geom: *const tg_geom) -> *const tg_poly;
369 pub fn tg_geom_num_points(geom: *const tg_geom) -> libc::c_int;
370 pub fn tg_geom_point_at(geom: *const tg_geom, index: libc::c_int) -> tg_point;
371 pub fn tg_geom_num_lines(geom: *const tg_geom) -> libc::c_int;
372 pub fn tg_geom_line_at(geom: *const tg_geom, index: libc::c_int) -> *const tg_line;
373 pub fn tg_geom_num_polys(geom: *const tg_geom) -> libc::c_int;
374 pub fn tg_geom_poly_at(geom: *const tg_geom, index: libc::c_int) -> *const tg_poly;
375 pub fn tg_geom_num_geometries(geom: *const tg_geom) -> libc::c_int;
376 pub fn tg_geom_geometry_at(geom: *const tg_geom, index: libc::c_int) -> *const tg_geom;
377 pub fn tg_geom_extra_json(geom: *const tg_geom) -> *const libc::c_char;
378 pub fn tg_geom_is_empty(geom: *const tg_geom) -> bool;
379 pub fn tg_geom_dims(geom: *const tg_geom) -> libc::c_int;
380 pub fn tg_geom_has_z(geom: *const tg_geom) -> bool;
381 pub fn tg_geom_has_m(geom: *const tg_geom) -> bool;
382 pub fn tg_geom_z(geom: *const tg_geom) -> libc::c_double;
383 pub fn tg_geom_m(geom: *const tg_geom) -> libc::c_double;
384 pub fn tg_geom_extra_coords(geom: *const tg_geom) -> *const libc::c_double;
385 pub fn tg_geom_num_extra_coords(geom: *const tg_geom) -> libc::c_int;
386 pub fn tg_geom_memsize(geom: *const tg_geom) -> libc::size_t;
387 pub fn tg_geom_search(
388 geom: *const tg_geom,
389 rect: tg_rect,
390 iter: extern "C" fn(
391 geom: *const tg_geom,
392 index: libc::c_int,
393 udata: *mut libc::c_void,
394 ) -> bool,
395 udata: *mut libc::c_void,
396 );
397 }
398}
399
400/// Geometry predicates
401///
402/// Functions for testing the spatial relations of two geometries.
403pub mod GeometryPredicates {
404 // done
405 use crate::tg_geom;
406 use crate::tg_rect;
407
408 #[link(name = "tg")]
409 extern "C" {
410 pub fn tg_geom_equals(a: *const tg_geom, b: *const tg_geom) -> bool;
411 pub fn tg_geom_intersects(a: *const tg_geom, b: *const tg_geom) -> bool;
412 pub fn tg_geom_disjoint(a: *const tg_geom, b: *const tg_geom) -> bool;
413 pub fn tg_geom_contains(a: *const tg_geom, b: *const tg_geom) -> bool;
414 pub fn tg_geom_within(a: *const tg_geom, b: *const tg_geom) -> bool;
415 pub fn tg_geom_covers(a: *const tg_geom, b: *const tg_geom) -> bool;
416 pub fn tg_geom_coveredby(a: *const tg_geom, b: *const tg_geom) -> bool;
417 pub fn tg_geom_intersects_rect(a: *const tg_geom, b: tg_rect) -> bool;
418 pub fn tg_geom_intersects_xy(
419 a: *const tg_geom,
420 x: libc::c_double,
421 y: libc::c_double,
422 ) -> bool;
423 }
424}
425
426/// Geometry parsing
427///
428/// Functions for parsing geometries from external data representations.
429/// It's recommended to use tg_geom_error() after parsing to check for errors.
430pub mod GeometryParsing {
431 // done
432
433 use crate::{tg_geom, tg_index};
434
435 #[link(name = "tg")]
436 extern "C" {
437 pub fn tg_parse_geojson(geojson: *const libc::c_char) -> *mut tg_geom;
438 pub fn tg_parse_geojsonn(geojson: *const libc::c_char, len: libc::size_t) -> *mut tg_geom;
439 pub fn tg_parse_geojson_ix(geojson: *const libc::c_char, ix: tg_index) -> *mut tg_geom;
440 pub fn tg_parse_geojsonn_ix(
441 geojson: *const libc::c_char,
442 len: libc::size_t,
443 ix: tg_index,
444 ) -> *mut tg_geom;
445 pub fn tg_parse_wkt(wkt: *const libc::c_char) -> *mut tg_geom;
446 pub fn tg_parse_wktn(wkt: *const libc::c_char, len: libc::size_t) -> *mut tg_geom;
447 pub fn tg_parse_wkt_ix(wkt: *const libc::c_char, ix: tg_index) -> *mut tg_geom;
448 pub fn tg_parse_wktn_ix(
449 wkt: *const libc::c_char,
450 len: libc::size_t,
451 ix: tg_index,
452 ) -> *mut tg_geom;
453 pub fn tg_parse_wkb(wkb: *const u8, len: libc::size_t) -> *mut tg_geom;
454 pub fn tg_parse_wkb_ix(wkb: *const u8, len: libc::size_t, ix: tg_index) -> *mut tg_geom;
455 pub fn tg_parse_hex(hex: *const libc::c_char) -> *mut tg_geom;
456 pub fn tg_parse_hexn(hex: *const libc::c_char, len: libc::size_t) -> *mut tg_geom;
457 pub fn tg_parse_hex_ix(hex: *const libc::c_char, ix: tg_index) -> *mut tg_geom;
458 pub fn tg_parse_hexn_ix(
459 hex: *const libc::c_char,
460 len: libc::size_t,
461 ix: tg_index,
462 ) -> *mut tg_geom;
463 pub fn tg_geom_error(geom: *const tg_geom) -> *const libc::c_char;
464 }
465}
466
467/// Geometry writing
468///
469/// Functions for writing geometries as external data representations.
470pub mod GeometryWriting {
471 // done
472
473 use crate::tg_geom;
474
475 extern "C" {
476
477 pub fn tg_geom_geojson(
478 geom: *const tg_geom,
479 dst: *mut libc::c_char,
480 n: libc::size_t,
481 ) -> libc::size_t;
482 pub fn tg_geom_wkt(
483 geom: *const tg_geom,
484 dst: *mut libc::c_char,
485 n: libc::size_t,
486 ) -> libc::size_t;
487 pub fn tg_geom_wkb(geom: *const tg_geom, dst: *mut u8, n: libc::size_t) -> libc::size_t;
488 pub fn tg_geom_hex(
489 geom: *const tg_geom,
490 dst: *mut libc::c_char,
491 n: libc::size_t,
492 ) -> libc::size_t;
493 }
494}
495
496/// Geometry with alternative dimensions
497///
498/// Functions for working with geometries that have more than two dimensions or
499/// are empty. The extra dimensional coordinates contained within these
500/// geometries are only carried along and serve no other purpose than to be
501/// available for when it's desired to export to an output representation such
502/// as GeoJSON, WKT, or WKB.
503pub mod GeometryConstructorsEx {
504 // done
505
506 use crate::{tg_geom, tg_line, tg_point, tg_poly};
507
508 extern "C" {
509
510 pub fn tg_geom_new_point_z(point: tg_point, z: libc::c_double) -> *mut tg_geom;
511 pub fn tg_geom_new_point_m(point: tg_point, m: libc::c_double) -> *mut tg_geom;
512 pub fn tg_geom_new_point_zm(
513 point: tg_point,
514 z: libc::c_double,
515 m: libc::c_double,
516 ) -> *mut tg_geom;
517 pub fn tg_geom_new_point_empty() -> *mut tg_geom;
518 pub fn tg_geom_new_linestring_z(
519 line: *const tg_line,
520 extra_coords: *const libc::c_double,
521 ncoords: libc::c_int,
522 ) -> *mut tg_geom;
523 pub fn tg_geom_new_linestring_m(
524 line: *const tg_line,
525 extra_coords: *const libc::c_double,
526 ncoords: libc::c_int,
527 ) -> *mut tg_geom;
528 pub fn tg_geom_new_linestring_zm(
529 line: *const tg_line,
530 extra_coords: *const libc::c_double,
531 ncoords: libc::c_int,
532 ) -> *mut tg_geom;
533 pub fn tg_geom_new_linestring_empty() -> *mut tg_geom;
534 pub fn tg_geom_new_polygon_z(
535 poly: *const tg_poly,
536 extra_coords: *const libc::c_double,
537 ncoords: libc::c_int,
538 ) -> *mut tg_geom;
539 pub fn tg_geom_new_polygon_m(
540 poly: *const tg_poly,
541 extra_coords: *const libc::c_double,
542 ncoords: libc::c_int,
543 ) -> *mut tg_geom;
544 pub fn tg_geom_new_polygon_zm(
545 poly: *const tg_poly,
546 extra_coords: *const libc::c_double,
547 ncoords: libc::c_int,
548 ) -> *mut tg_geom;
549 pub fn tg_geom_new_polygon_empty() -> *mut tg_geom;
550 pub fn tg_geom_new_multipoint_z(
551 points: *const tg_point,
552 npoints: libc::c_int,
553 extra_coords: *const libc::c_double,
554 ncoords: libc::c_int,
555 ) -> *mut tg_geom;
556 pub fn tg_geom_new_multipoint_m(
557 points: *const tg_point,
558 npoints: libc::c_int,
559 extra_coords: *const libc::c_double,
560 ncoords: libc::c_int,
561 ) -> *mut tg_geom;
562 pub fn tg_geom_new_multipoint_zm(
563 points: *const tg_point,
564 npoints: libc::c_int,
565 extra_coords: *const libc::c_double,
566 ncoords: libc::c_int,
567 ) -> *mut tg_geom;
568 pub fn tg_geom_new_multipoint_empty() -> *mut tg_geom;
569 pub fn tg_geom_new_multilinestring_z(
570 lines: *const *const tg_line,
571 nlines: libc::c_int,
572 extra_coords: *const libc::c_double,
573 ncoords: libc::c_int,
574 ) -> *mut tg_geom;
575 pub fn tg_geom_new_multilinestring_m(
576 lines: *const *const tg_line,
577 nlines: libc::c_int,
578 extra_coords: *const libc::c_double,
579 ncoords: libc::c_int,
580 ) -> *mut tg_geom;
581 pub fn tg_geom_new_multilinestring_zm(
582 lines: *const *const tg_line,
583 nlines: libc::c_int,
584 extra_coords: *const libc::c_double,
585 ncoords: libc::c_int,
586 ) -> *mut tg_geom;
587 pub fn tg_geom_new_multilinestring_empty() -> *mut tg_geom;
588 pub fn tg_geom_new_multipolygon_z(
589 polys: *const *const tg_poly,
590 npolys: libc::c_int,
591 extra_coords: *const libc::c_double,
592 ncoords: libc::c_int,
593 ) -> *mut tg_geom;
594 pub fn tg_geom_new_multipolygon_m(
595 polys: *const *const tg_poly,
596 npolys: libc::c_int,
597 extra_coords: *const libc::c_double,
598 ncoords: libc::c_int,
599 ) -> *mut tg_geom;
600 pub fn tg_geom_new_multipolygon_zm(
601 polys: *const *const tg_poly,
602 npolys: libc::c_int,
603 extra_coords: *const libc::c_double,
604 ncoords: libc::c_int,
605 ) -> *mut tg_geom;
606 pub fn tg_geom_new_multipolygon_empty() -> *mut tg_geom;
607 pub fn tg_geom_new_geometrycollection_empty() -> *mut tg_geom;
608 }
609}
610
611/// Point functions
612///
613/// Functions for working directly with the tg_point type.
614pub mod PointFuncs {
615 use crate::{tg_point, tg_rect};
616 #[link(name = "tg")]
617 extern "C" {
618 pub fn tg_point_rect(point: tg_point) -> tg_rect;
619 pub fn tg_point_intersects_rect(point: tg_point, rect: tg_rect) -> bool;
620 }
621}
622
623/// Segment functions
624///
625/// Functions for working directly with the tg_segment type.
626pub mod SegmentFuncs {
627 use crate::{tg_rect, tg_segment};
628 #[link(name = "tg")]
629 extern "C" {
630 pub fn tg_segment_rect(s: tg_segment) -> tg_rect;
631 pub fn tg_segment_intersects_segment(a: tg_segment, b: tg_segment) -> bool;
632 }
633}
634
635/// Rectangle functions
636///
637/// Functions for working directly with the tg_rect type.
638pub mod RectFuncs {
639 // done
640
641 use crate::{tg_point, tg_rect};
642
643 extern "C" {
644 pub fn tg_rect_expand(rect: tg_rect, other: tg_rect) -> tg_rect;
645 pub fn tg_rect_expand_point(rect: tg_rect, point: tg_point) -> tg_rect;
646 pub fn tg_rect_center(rect: tg_rect) -> tg_point;
647 pub fn tg_rect_intersects_rect(a: tg_rect, b: tg_rect) -> bool;
648 pub fn tg_rect_intersects_point(a: tg_rect, b: tg_point) -> bool;
649 }
650}
651
652/// Ring functions
653///
654/// Functions for working directly with the tg_ring type.
655///
656/// There are no direct spatial predicates for tg_ring.
657/// If you want to perform operations like "intersects" or "covers" then you
658/// must upcast the ring to a tg_geom, like such:
659///
660/// ```c
661/// tg_geom_intersects((struct tg_geom*)ring, geom);
662/// ```
663pub mod RingFuncs {
664 // done
665 use crate::{tg_index, tg_line, tg_point, tg_rect, tg_ring, tg_segment};
666 extern "C" {
667 pub fn tg_ring_new(points: *const tg_point, npoints: libc::c_int) -> *mut tg_ring;
668 pub fn tg_ring_new_ix(
669 points: *const tg_point,
670 npoints: libc::c_int,
671 ix: tg_index,
672 ) -> *mut tg_ring;
673 pub fn tg_ring_free(ring: *mut tg_ring);
674 pub fn tg_ring_clone(ring: *const tg_ring) -> *mut tg_ring;
675 pub fn tg_ring_copy(ring: *const tg_ring) -> *mut tg_ring;
676 pub fn tg_ring_memsize(ring: *const tg_ring) -> libc::size_t;
677 pub fn tg_ring_rect(ring: *const tg_ring) -> tg_rect;
678 pub fn tg_ring_num_points(ring: *const tg_ring) -> libc::c_int;
679 pub fn tg_ring_point_at(ring: *const tg_ring, index: libc::c_int) -> tg_point;
680 pub fn tg_ring_points(ring: *const tg_ring) -> *const tg_point;
681 pub fn tg_ring_num_segments(ring: *const tg_ring) -> libc::c_int;
682 pub fn tg_ring_segment_at(ring: *const tg_ring, index: libc::c_int) -> tg_segment;
683 pub fn tg_ring_convex(ring: *const tg_ring) -> bool;
684 pub fn tg_ring_clockwise(ring: *const tg_ring) -> bool;
685 pub fn tg_ring_index_spread(ring: *const tg_ring) -> libc::c_int;
686 pub fn tg_ring_index_num_levels(ring: *const tg_ring) -> libc::c_int;
687 pub fn tg_ring_index_level_num_rects(
688 ring: *const tg_ring,
689 levelidx: libc::c_int,
690 ) -> libc::c_int;
691 pub fn tg_ring_index_level_rect(
692 ring: *const tg_ring,
693 levelidx: libc::c_int,
694 rectidx: libc::c_int,
695 ) -> tg_rect;
696
697 /// Iterates over segments from nearest to farthest.
698 ///
699 /// This is a kNN operation.
700 ///
701 /// The caller must provide their own "rect_dist" and "seg_dist" callbacks to
702 /// do the actual distance calculations.
703 ///
704 /// @param ring Input ring
705 ///
706 /// @param rect_dist Callback that returns the distance to a tg_rect.
707 ///
708 /// @param seg_dist Callback that returns the distance to a tg_segment.
709 ///
710 /// @param iter Callback that returns each segment in the ring in order of
711 /// nearest to farthest. Caller must return true to continue to the next
712 /// segment, or return false to stop iterating.
713 ///
714 /// @param udata User-defined data
715 ///
716 /// @return True if operation succeeded, false if out of memory.
717 ///
718 /// @note Though not typical, this operation may need to allocate memory.
719 /// It's recommended to check the return value for success.
720 /// @note The `*more` argument is an optional ref-value that is used for
721 /// performing partial step-based or probability-based calculations. A detailed
722 /// description of its use is outside the scope of this document. Ignoring it
723 /// altogether is the preferred behavior.
724 pub fn tg_ring_nearest_segment(
725 ring: *const tg_ring,
726 rect_dist: extern "C" fn(
727 rect: tg_rect,
728 more: *mut libc::c_int,
729 udata: *mut libc::c_void,
730 ) -> libc::c_double,
731 seg_dist: extern "C" fn(
732 seg: tg_segment,
733 more: *mut libc::c_int,
734 udata: *mut libc::c_void,
735 ) -> libc::c_double,
736 iter: extern "C" fn(
737 seg: tg_segment,
738 dist: libc::c_double,
739 index: libc::c_int,
740 udata: *mut libc::c_void,
741 ) -> bool,
742 udata: *mut libc::c_void,
743 ) -> bool;
744
745 pub fn tg_ring_line_search(
746 a: *const tg_ring,
747 b: *const tg_line,
748 iter: extern "C" fn(
749 aseg: tg_segment,
750 aidx: libc::c_int,
751 bseg: tg_segment,
752 bidx: libc::c_int,
753 udata: *mut libc::c_void,
754 ) -> bool,
755 udata: *mut libc::c_void,
756 );
757 pub fn tg_ring_ring_search(
758 a: *const tg_ring,
759 b: *const tg_ring,
760 iter: extern "C" fn(
761 aseg: tg_segment,
762 aidx: libc::c_int,
763 bseg: tg_segment,
764 bidx: libc::c_int,
765 udata: *mut libc::c_void,
766 ) -> bool,
767 udata: *mut libc::c_void,
768 );
769 pub fn tg_ring_area(libc: *const tg_ring) -> libc::c_double;
770 pub fn tg_ring_perimeter(libc: *const tg_ring) -> libc::c_double;
771 }
772}
773
774/// Line functions
775///
776/// Functions for working directly with the tg_line type.
777///
778/// There are no direct spatial predicates for tg_line.
779/// If you want to perform operations like "intersects" or "covers" then you
780/// must upcast the line to a tg_geom, like such:
781///
782/// ```c
783/// tg_geom_intersects((struct tg_geom*)line, geom);
784/// ```
785pub mod LineFuncs {
786 // done
787
788 use crate::{tg_index, tg_line, tg_point, tg_rect, tg_segment};
789
790 extern "C" {
791
792 /// Creates a line from a series of points.
793 /// @param points Array of points
794 /// @param npoints Number of points in array
795 /// @return A newly allocated line
796 /// @return NULL if out of memory
797 /// @note A tg_line can be safely upcasted to a tg_geom. `(struct tg_geom*)line`
798 /// @note All lines with 32 or more points are automatically indexed.
799 pub fn tg_line_new(points: *const tg_point, npoints: libc::c_int) -> *mut tg_line;
800
801 /// Creates a line from a series of points using provided index option.
802 /// @param points Array of points
803 /// @param npoints Number of points in array
804 /// @param ix Indexing option, e.g. TG_NONE, TG_NATURAL, TG_YSTRIPES
805 /// @return A newly allocated line
806 /// @return NULL if out of memory
807 /// @note A tg_line can be safely upcasted to a tg_geom. `(struct tg_geom*)poly`
808 /// @see [tg_index](.#tg_index)
809 pub fn tg_line_new_ix(
810 points: *const tg_point,
811 npoints: libc::c_int,
812 ix: tg_index,
813 ) -> *mut tg_line;
814
815 /// Releases the memory associated with a line.
816 /// @param line Input line
817 pub fn tg_line_free(line: *mut tg_line);
818
819 /// Clones a line
820 /// @param line Input line, caller retains ownership.
821 /// @return A duplicate of the provided line.
822 /// @note The caller is responsible for freeing with tg_line_free().
823 /// @note This method of cloning uses implicit sharing through an atomic
824 /// reference counter.
825 pub fn tg_line_clone(line: *const tg_line) -> *mut tg_line;
826
827 /// Copies a line
828 /// @param line Input line, caller retains ownership.
829 /// @return A duplicate of the provided line.
830 /// @return NULL if out of memory
831 /// @note The caller is responsible for freeing with tg_line_free().
832 /// @note This method performs a deep copy of the entire geometry to new memory.
833 pub fn tg_line_copy(line: *const tg_line) -> *mut tg_line;
834
835 /// Returns the allocation size of the line.
836 /// @param line Input line
837 /// @return Size of line in bytes
838 pub fn tg_line_memsize(line: *const tg_line) -> libc::size_t;
839
840 /// Returns the minimum bounding rectangle of a line.
841 pub fn tg_line_rect(line: *const tg_line) -> tg_rect;
842
843 /// Returns the number of points.
844 /// @param line Input line
845 /// @return Number of points
846 /// @see tg_line_point_at()
847 pub fn tg_line_num_points(line: *const tg_line) -> libc::c_int;
848
849 /// Returns the underlying point array of a line.
850 /// @param line Input line
851 /// @return Array or points
852 /// @see tg_line_num_points()
853 /// @see LineFuncs
854 pub fn tg_line_points(line: *const tg_line) -> *const tg_point;
855
856 /// Returns the point at index.
857 /// @param line Input line
858 /// @param index Index of point
859 /// @return The point at index
860 /// @note This function performs bounds checking. Use tg_line_points() for
861 /// direct access to the points.
862 /// @see tg_line_num_points()
863 pub fn tg_line_point_at(line: *const tg_line, index: libc::c_int) -> tg_point;
864
865 /// Returns the number of segments.
866 /// @param line Input line
867 /// @return Number of segments
868 /// @see tg_line_segment_at()
869 /// @see LineFuncs
870 pub fn tg_line_num_segments(line: *const tg_line) -> libc::c_int;
871
872 /// Returns the segment at index.
873 /// @param line Input line
874 /// @param index Index of segment
875 /// @return The segment at index
876 /// @see tg_line_num_segments()
877 pub fn tg_line_segment_at(line: *const tg_line, index: libc::c_int) -> tg_segment;
878
879 /// Returns true if winding order is clockwise.
880 /// @param line Input line
881 /// @return True if clockwise
882 /// @return False if counter-clockwise
883 pub fn tg_line_clockwise(line: *const tg_line) -> bool;
884
885 /// Returns the indexing spread for a line.
886 ///
887 /// The "spread" is the number of segments or rectangles that are grouped
888 /// together to produce a unioned rectangle that is stored at a higher level.
889 ///
890 /// For a tree based structure, this would be the number of items per node.
891 ///
892 /// @param line Input line
893 /// @return The spread, default is 16
894 /// @return Zero if line has no indexing
895 /// @see tg_line_index_num_levels()
896 /// @see tg_line_index_level_num_rects()
897 /// @see tg_line_index_level_rect()
898 pub fn tg_line_index_spread(line: *const tg_line) -> libc::c_int;
899
900 /// Returns the number of levels.
901 /// @param line Input line
902 /// @return The number of levels
903 /// @return Zero if line has no indexing
904 /// @see tg_line_index_spread()
905 /// @see tg_line_index_level_num_rects()
906 /// @see tg_line_index_level_rect()
907 pub fn tg_line_index_num_levels(line: *const tg_line) -> libc::c_int;
908
909 /// Returns the number of rectangles at level.
910 /// @param line Input line
911 /// @param levelidx The index of level
912 /// @return The number of index levels
913 /// @return Zero if line has no indexing or levelidx is out of bounds.
914 /// @see tg_line_index_spread()
915 /// @see tg_line_index_num_levels()
916 /// @see tg_line_index_level_rect()
917 pub fn tg_line_index_level_num_rects(
918 line: *const tg_line,
919 levelidx: libc::c_int,
920 ) -> libc::c_int;
921
922 /// Returns a specific level rectangle.
923 /// @param line Input line
924 /// @param levelidx The index of level
925 /// @param rectidx The index of rectangle
926 /// @return The rectangle
927 /// @return Empty rectangle if line has no indexing, or levelidx or rectidx
928 /// is out of bounds.
929 /// @see tg_line_index_spread()
930 /// @see tg_line_index_num_levels()
931 /// @see tg_line_index_level_num_rects()
932 pub fn tg_line_index_level_rect(
933 line: *const tg_line,
934 levelidx: libc::c_int,
935 rectidx: libc::c_int,
936 ) -> tg_rect;
937
938 /// Iterates over segments from nearest to farthest.
939 /// @see [`tg_ring_nearest_segment()`][crate::RingFuncs::tg_ring_nearest_segment]`, which shares the same interface, for a
940 /// detailed description.
941 pub fn tg_line_nearest_segment(
942 line: *const tg_line,
943 rect_dist: extern "C" fn(
944 rect: tg_rect,
945 more: *mut libc::c_int,
946 udata: *mut libc::c_void,
947 ) -> libc::c_double,
948 seg_dist: extern "C" fn(
949 seg: tg_segment,
950 more: *mut libc::c_int,
951 udata: *mut libc::c_void,
952 ) -> libc::c_double,
953 iter: extern "C" fn(
954 seg: tg_segment,
955 dist: libc::c_double,
956 index: libc::c_int,
957 udata: *mut libc::c_void,
958 ) -> bool,
959 udata: *mut libc::c_void,
960 ) -> bool;
961
962 /// Iterates over all segments in line A that intersect with segments in line B.
963 /// @note This efficently uses the indexes of each geometry, if available.
964 pub fn tg_line_line_search(
965 a: *const tg_line,
966 b: *const tg_line,
967 iter: extern "C" fn(
968 aseg: tg_segment,
969 aidx: libc::c_int,
970 bseg: tg_segment,
971 bidx: libc::c_int,
972 udata: *mut libc::c_void,
973 ) -> bool,
974 udata: *mut libc::c_void,
975 );
976
977 /// Calculate the length of a line.
978 pub fn tg_line_length(line: *const tg_line) -> libc::c_double;
979 }
980}
981
982/// Polygon functions
983///
984/// Functions for working directly with the tg_poly type.
985///
986/// There are no direct spatial predicates for tg_poly.
987/// If you want to perform operations like "intersects" or "covers" then you
988/// must upcast the poly to a tg_geom, like such:
989///
990/// ```c
991/// tg_geom_intersects((struct tg_geom*)poly, geom);
992/// ```
993pub mod PolyFuncs {
994 // done
995
996 use crate::{tg_poly, tg_rect, tg_ring};
997
998 extern "C" {
999 pub fn tg_poly_new(
1000 exterior: *const tg_ring,
1001 holes: *const *const tg_ring,
1002 nholes: libc::c_int,
1003 ) -> *mut tg_poly;
1004 pub fn tg_poly_free(poly: *mut tg_poly);
1005 pub fn tg_poly_clone(poly: *const tg_poly) -> *mut tg_poly;
1006 pub fn tg_poly_copy(poly: *const tg_poly) -> *mut tg_poly;
1007 pub fn tg_poly_memsize(poly: *const tg_poly) -> libc::size_t;
1008 pub fn tg_poly_exterior(poly: *const tg_poly) -> *const tg_ring;
1009 pub fn tg_poly_num_holes(poly: *const tg_poly) -> libc::c_int;
1010 pub fn tg_poly_hole_at(poly: *const tg_poly, index: libc::c_int) -> *const tg_ring;
1011 pub fn tg_poly_rect(poly: *const tg_poly) -> tg_rect;
1012 pub fn tg_poly_clockwise(poly: *const tg_poly) -> bool;
1013 }
1014}
1015
1016/// @defgroup GlobalFuncs Global environment
1017/// Functions for optionally setting the behavior of the TG environment.
1018/// These, if desired, should be called only once at program start up and prior
1019/// to calling any other tg_*() functions.
1020pub mod GlobalFuncs {
1021 // done
1022
1023 use crate::tg_index;
1024 extern "C" {
1025
1026 pub fn tg_env_set_allocator(
1027 malloc: extern "C" fn(size: libc::size_t) -> *mut libc::c_void,
1028 realloc: extern "C" fn(
1029 alloc: *mut libc::c_void,
1030 size: libc::size_t,
1031 ) -> *mut libc::c_void,
1032 free: extern "C" fn(),
1033 );
1034 pub fn tg_env_set_index(ix: tg_index);
1035 pub fn tg_env_set_index_spread(spread: libc::c_int);
1036 }
1037}
1038
1039// Code from tg.h follows //
1040////////////////////////////
1041
1042/*
1043
1044// https://github.com/tidwall/tg
1045//
1046// Copyright 2023 Joshua J Baker. All rights reserved.
1047// Use of this source code is governed by a license
1048// that can be found in the LICENSE file.
1049
1050#ifndef TG_H
1051#define TG_H
1052
1053#include <stdbool.h>
1054#include <stdint.h>
1055#include <stddef.h>
1056
1057/// [SNIP]
1058
1059#endif // TG_H
1060
1061*/