Skip to main content

Crate s2rst

Crate s2rst 

Source
Expand description

A library for spherical geometry on the unit sphere (S²).

This crate provides robust, flexible, and performant operations on spherical geometry. All geometry lives on the surface of the unit sphere in three-dimensional space, with points represented as unit-length vectors rather than latitude-longitude pairs. This representation avoids singularities at the poles, eliminates discontinuities at the antimeridian, and enables efficient computation with geodesic edges (shortest paths on the sphere).

§Key properties

  • Robustness: Core predicates use conservative error bounds with exact arithmetic fallback, guaranteeing topologically correct results.
  • Geodesic edges: All edges follow great circles, behaving consistently everywhere on the sphere without special-casing poles or meridians.
  • Hierarchical indexing: The sphere is decomposed into a hierarchy of cells (s2::CellId) ordered along a space-filling Hilbert curve, enabling fast spatial queries through s2::shape_index::ShapeIndex.
  • No unsafe code: The entire crate is built with #![forbid(unsafe_code)].

§Module hierarchy

The crate is organized into five modules corresponding to the mathematical spaces they operate in:

ModuleSpaceKey types
r1Real line (ℝ¹)r1::Interval
r2Euclidean plane (ℝ²)r2::Point, r2::Rect
r3Euclidean 3-space (ℝ³)r3::Vector, r3::PreciseVector, r3::ExactFloat
s1Unit circle (S¹)s1::Angle, s1::ChordAngle, s1::Interval
s2Unit sphere (S²)s2::Point, s2::CellId, s2::Cell, s2::Loop, s2::Polygon, and many more

Most users will work primarily with the types in s2.

§Core workflow

  1. Represent geometry using s2::Point, s2::Loop, s2::Polygon, or s2::polyline::Polyline.
  2. Index geometry in a ShapeIndex for fast spatial queries.
  3. Query using s2::closest_edge_query, s2::contains_point_query, s2::crossing_edge_query, or s2::boolean_operation.
  4. Cover regions with cells using s2::region_coverer::RegionCoverer for spatial indexing in databases.
  5. Build new geometry from raw edges with s2::builder::S2Builder, which handles vertex snapping and topological assembly.

§Quick example

use s2rst::s1::Angle;
use s2rst::s2::{Cap, LatLng, Point, Region};
use s2rst::s2::region_coverer::RegionCoverer;

// Define a spherical cap (disc) centered on Paris.
let center = LatLng::from_degrees(48.8566, 2.3522).to_point();
let cap = Cap::from_center_angle(center, Angle::from_degrees(0.5));

// Approximate the cap with a covering of S2 cells.
let coverer = RegionCoverer::new().max_level(14).max_cells(8);
let covering = coverer.covering(&cap);
assert!(!covering.is_empty());

// Every point inside the cap is contained by the covering.
assert!(covering.contains_point(center));

Modules§

geo_types_interop
Interoperability with the geo-types crate.
r1
One-dimensional geometry on the real line (ℝ¹).
r2
Two-dimensional Euclidean geometry (ℝ²).
r3
Three-dimensional Euclidean geometry and exact arithmetic (ℝ³).
s1
One-dimensional geometry on the unit circle (S¹).
s2
Spherical geometry on the unit sphere (S²).