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 throughs2::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:
| Module | Space | Key types |
|---|---|---|
r1 | Real line (ℝ¹) | r1::Interval |
r2 | Euclidean plane (ℝ²) | r2::Point, r2::Rect |
r3 | Euclidean 3-space (ℝ³) | r3::Vector, r3::PreciseVector, r3::ExactFloat |
s1 | Unit circle (S¹) | s1::Angle, s1::ChordAngle, s1::Interval |
s2 | Unit 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
- Represent geometry using
s2::Point,s2::Loop,s2::Polygon, ors2::polyline::Polyline. - Index geometry in a
ShapeIndexfor fast spatial queries. - Query using
s2::closest_edge_query,s2::contains_point_query,s2::crossing_edge_query, ors2::boolean_operation. - Cover regions with cells using
s2::region_coverer::RegionCovererfor spatial indexing in databases. - 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-typescrate. - 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²).