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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2026 Torgeir Børresen <tb@starkad.no>
// Rust port of Google's S2 Geometry library — a derivative work, modified from
// the upstream Apache-2.0 source(s) below (Copyright Google Inc.). See LICENSE.
// - C++: google/s2geometry
// - Go: golang/geo
// - Java: google/s2-geometry-library-java
//! Abstract interface for geometric regions on the unit sphere.
//!
//! Corresponds to C++ `S2Region`, Go `s2.Region`, Java `S2Region`.
use crate::s2::{Cap, Cell, CellId, Point, Rect};
/// A geometric region on the unit sphere.
///
/// All types implementing `Region` must provide bounding methods
/// (`cap_bound`, `rect_bound`, `cell_union_bound`) and containment /
/// intersection tests against cells and points.
///
/// # Examples
///
/// ```
/// use s2rst::s1::Angle;
/// use s2rst::s2::{Cap, LatLng, Point, Region};
///
/// // Cap implements Region.
/// let center = LatLng::from_degrees(48.8566, 2.3522).to_point();
/// let cap = Cap::from_center_angle(center, Angle::from_degrees(1.0));
///
/// // Bounding rectangle and bounding cap.
/// let rect = cap.rect_bound();
/// assert!(!rect.is_empty());
/// let bound = cap.cap_bound();
/// assert!(bound.contains_point(center));
///
/// // Cell union bound for spatial indexing.
/// let cells = cap.cell_union_bound();
/// assert!(!cells.is_empty());
/// ```
pub trait Region: std::fmt::Debug {
/// Returns a bounding spherical cap for this region.
fn cap_bound(&self) -> Cap;
/// Returns a bounding latitude-longitude rectangle for this region.
fn rect_bound(&self) -> Rect;
/// Returns a small set of cells that cover this region. The output is
/// not sorted.
fn cell_union_bound(&self) -> Vec<CellId>;
/// Reports whether this region completely contains the given cell.
fn contains_cell(&self, cell: &Cell) -> bool;
/// Reports whether this region intersects the given cell.
fn intersects_cell(&self, cell: &Cell) -> bool;
/// Reports whether this region contains the given point.
fn contains_point(&self, p: &Point) -> bool;
}
// --- impl Region for Cap ---
impl Region for Cap {
fn cap_bound(&self) -> Cap {
Cap::cap_bound(*self)
}
fn rect_bound(&self) -> Rect {
Cap::rect_bound(*self)
}
fn cell_union_bound(&self) -> Vec<CellId> {
Cap::cell_union_bound(*self)
}
fn contains_cell(&self, cell: &Cell) -> bool {
// A cap contains a cell if it contains the cell's bounding cap.
// This is conservative: it may return false even if the cell is contained.
self.contains(cell.cap_bound())
}
fn intersects_cell(&self, cell: &Cell) -> bool {
// A cap intersects a cell if it intersects the cell's bounding cap.
// This is conservative for the negative case.
self.intersects(cell.cap_bound())
}
fn contains_point(&self, p: &Point) -> bool {
Cap::contains_point(*self, *p)
}
}
// --- impl Region for Rect ---
impl Region for Rect {
fn cap_bound(&self) -> Cap {
Rect::cap_bound(*self)
}
fn rect_bound(&self) -> Rect {
*self
}
fn cell_union_bound(&self) -> Vec<CellId> {
Rect::cell_union_bound(*self)
}
fn contains_cell(&self, cell: &Cell) -> bool {
self.contains(cell.rect_bound())
}
fn intersects_cell(&self, cell: &Cell) -> bool {
// Conservative: check if rect intersects cell's rect bound.
self.intersects(cell.rect_bound())
}
fn contains_point(&self, p: &Point) -> bool {
Rect::contains_point(*self, *p)
}
}
// --- impl Region for Cell ---
impl Region for Cell {
fn cap_bound(&self) -> Cap {
Cell::cap_bound(*self)
}
fn rect_bound(&self) -> Rect {
Cell::rect_bound(*self)
}
fn cell_union_bound(&self) -> Vec<CellId> {
Cell::cell_union_bound(*self)
}
fn contains_cell(&self, cell: &Cell) -> bool {
Cell::contains_cell(*self, *cell)
}
fn intersects_cell(&self, cell: &Cell) -> bool {
Cell::intersects_cell(*self, *cell)
}
fn contains_point(&self, p: &Point) -> bool {
Cell::contains_point(*self, *p)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cap_region() {
let cap = Cap::from_point(Point::from_coords(1.0, 0.0, 0.0));
let r: &dyn Region = ∩
assert!(!r.cap_bound().is_empty());
assert!(!r.rect_bound().is_empty());
assert!(r.contains_point(&Point::from_coords(1.0, 0.0, 0.0)));
}
#[test]
fn test_rect_region() {
let rect = Rect::full();
let r: &dyn Region = ▭
assert!(r.cap_bound().is_full());
assert!(r.rect_bound().is_full());
assert!(r.contains_point(&Point::from_coords(1.0, 0.0, 0.0)));
}
#[test]
fn test_cell_region() {
let cell = Cell::from_cell_id(CellId::from_face(0));
let r: &dyn Region = &cell;
assert!(!r.cap_bound().is_empty());
assert!(!r.rect_bound().is_empty());
assert!(r.contains_point(&Point::from_coords(1.0, 0.0, 0.0)));
}
#[test]
fn test_dyn_region() {
// Ensure dyn Region is object-safe.
let regions: Vec<Box<dyn Region>> = vec![
Box::new(Cap::full()),
Box::new(Rect::full()),
Box::new(Cell::from_cell_id(CellId::from_face(0))),
];
for region in ®ions {
assert!(!region.cap_bound().is_empty());
}
}
}