s2rst 0.4.0

A Rust port of Google's S2 spherical geometry library — points, regions, shapes, and a hierarchical cell index on the sphere.
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
// 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
//   - Java: google/s2-geometry-library-java

// S2PolylineLayer: assembles edges into a single S2Polyline.

use crate::s2::polyline::Polyline;

use super::graph::Graph;
use super::graph::{
    DegenerateEdges, DuplicateEdges, EdgeType, GraphOptions, LabelFetcher, PolylineType,
    SiblingPairs,
};
use super::id_set_lexicon::IdSetLexicon;
use super::layer::Layer;
use super::{LabelSetId, S2Error};

/// Per-edge label set IDs for a polyline.
///
/// `label_set_ids[j]` gives the `LabelSetId` for edge `j` of the polyline
/// (the edge from vertex `j` to vertex `j+1`). Decode individual labels via
/// the accompanying `IdSetLexicon`.
pub type LabelSetIds = Vec<LabelSetId>;

/// Options for `S2PolylineLayer`.
#[derive(Clone, Debug, PartialEq)]
pub struct Options {
    /// Whether edges are directed or undirected.
    pub edge_type: EdgeType,
    /// If true, the layer verifies that the output polyline is valid
    /// (unit-length vertices, no adjacent identical/antipodal vertices).
    /// Any validation errors are reported via the `S2Error` passed to `build()`.
    ///
    /// C++: `S2PolylineLayer::Options::validate()`
    pub validate: bool,
}

impl Default for Options {
    fn default() -> Self {
        Options {
            edge_type: EdgeType::Directed,
            validate: false,
        }
    }
}

/// A layer that assembles edges into a single polyline.
///
/// If the edges cannot be assembled into a single connected polyline,
/// only the first polyline is kept.
#[derive(Debug)]
pub struct S2PolylineLayer {
    polyline: Option<Polyline>,
    options: Options,
    label_set_ids: Option<LabelSetIds>,
    label_set_lexicon: Option<IdSetLexicon>,
    track_labels: bool,
    /// Legacy shared output for backward-compatible test code.
    #[cfg(test)]
    legacy_output: Option<std::rc::Rc<std::cell::RefCell<Polyline>>>,
}

impl S2PolylineLayer {
    /// Creates a new layer with default options.
    pub fn new() -> Self {
        S2PolylineLayer {
            polyline: None,
            options: Options::default(),
            label_set_ids: None,
            label_set_lexicon: None,
            track_labels: false,
            #[cfg(test)]
            legacy_output: None,
        }
    }

    /// Creates a new layer with the given options.
    pub fn with_options(options: Options) -> Self {
        S2PolylineLayer {
            polyline: None,
            options,
            label_set_ids: None,
            label_set_lexicon: None,
            track_labels: false,
            #[cfg(test)]
            legacy_output: None,
        }
    }

    /// Creates a new layer that also collects per-edge label sets.
    pub fn with_labels(options: Options) -> Self {
        S2PolylineLayer {
            polyline: None,
            options,
            label_set_ids: None,
            label_set_lexicon: None,
            track_labels: true,
            #[cfg(test)]
            legacy_output: None,
        }
    }

    /// Consumes this layer and returns the built polyline.
    ///
    /// # Panics
    ///
    /// Panics if `build()` was not called or returned an error.
    #[expect(
        clippy::expect_used,
        reason = "panics are documented; caller must call build() first"
    )]
    pub fn into_output(self) -> Polyline {
        self.polyline
            .expect("S2PolylineLayer::build() was not called")
    }

    /// Returns a reference to the built polyline.
    pub fn output(&self) -> Option<&Polyline> {
        self.polyline.as_ref()
    }

    /// Takes the built polyline out, leaving `None` in its place.
    pub fn take_output(&mut self) -> Option<Polyline> {
        self.polyline.take()
    }

    /// Returns the per-edge label set IDs (if label tracking was enabled).
    pub fn label_set_ids(&self) -> Option<&LabelSetIds> {
        self.label_set_ids.as_ref()
    }

    /// Returns the label set lexicon (if label tracking was enabled).
    pub fn label_set_lexicon(&self) -> Option<&IdSetLexicon> {
        self.label_set_lexicon.as_ref()
    }
}

impl Default for S2PolylineLayer {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
impl S2PolylineLayer {
    /// Legacy constructor for test backward compatibility.
    pub fn new_legacy(output: std::rc::Rc<std::cell::RefCell<Polyline>>) -> Self {
        let mut s = Self::new();
        s.legacy_output = Some(output);
        s
    }

    /// Legacy constructor with options for test backward compatibility.
    pub fn with_options_legacy(
        output: std::rc::Rc<std::cell::RefCell<Polyline>>,
        options: Options,
    ) -> Self {
        let mut s = Self::with_options(options);
        s.legacy_output = Some(output);
        s
    }

    /// Syncs output to legacy Rc<RefCell> if present.
    fn sync_legacy(&self) {
        if let (Some(output), Some(legacy)) = (&self.polyline, &self.legacy_output) {
            *legacy.borrow_mut() = output.clone();
        }
    }
}

impl Layer for S2PolylineLayer {
    fn graph_options(&self) -> GraphOptions {
        GraphOptions::new(
            self.options.edge_type,
            DegenerateEdges::Discard,
            DuplicateEdges::Keep,
            SiblingPairs::Keep,
        )
    }

    fn build(&mut self, graph: &Graph, error: &mut S2Error) {
        if graph.num_edges() == 0 {
            self.polyline = Some(Polyline::new(vec![]));
            #[cfg(test)]
            self.sync_legacy();
            return;
        }

        let edge_polylines = graph.get_polylines(PolylineType::Walk);

        if edge_polylines.len() != 1 {
            *error = S2Error {
                code: super::S2ErrorCode::BuilderEdgesDoNotFormPolyline,
                message: "Input edges cannot be assembled into polyline".to_string(),
            };
            #[cfg(test)]
            self.sync_legacy();
            return;
        }

        let edge_path = &edge_polylines[0];
        let mut vertices = Vec::with_capacity(edge_path.len() + 1);
        vertices.push(graph.vertex(graph.edge(edge_path[0]).0));
        for &eid in edge_path {
            let (_, v1) = graph.edge(eid);
            vertices.push(graph.vertex(v1));
        }

        // Collect labels if requested.
        if self.track_labels {
            let fetcher = LabelFetcher::new(graph, self.options.edge_type);
            let mut lex = self.label_set_lexicon.take().unwrap_or_default();
            let mut ids = Vec::with_capacity(edge_path.len());
            for &eid in edge_path {
                let labels = fetcher.fetch(graph, eid);
                ids.push(lex.add_set(&labels));
            }
            self.label_set_ids = Some(ids);
            self.label_set_lexicon = Some(lex);
        }

        let polyline = Polyline::new(vertices);
        if self.options.validate
            && let Some(e) = polyline.find_validation_error()
        {
            *error = e;
        }
        self.polyline = Some(polyline);

        // Sync to legacy Rc<RefCell> output if present (test backward compat).
        #[cfg(test)]
        self.sync_legacy();
    }

    fn into_any(self: Box<Self>) -> Box<dyn std::any::Any> {
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::s2::Point;
    use crate::s2::text_format::{make_polyline, polyline_to_string};

    fn test_s2_polyline_with_edge_type(
        input_strs: &[&str],
        expected_str: &str,
        edge_type: EdgeType,
        builder_opts: super::super::Options,
    ) {
        use super::super::S2Builder;

        let mut builder = S2Builder::new(builder_opts);
        let opts = Options {
            edge_type,
            ..Options::default()
        };
        builder.start_layer(Box::new(S2PolylineLayer::with_options(opts)));

        for &s in input_strs {
            let polyline = make_polyline(s);
            builder.add_polyline(&polyline);
        }

        let mut layers = builder.build().expect("build failed");
        let layer = layers
            .remove(0)
            .into_any()
            .downcast::<S2PolylineLayer>()
            .expect("wrong layer type");
        let output = layer.into_output();

        let expected = make_polyline(expected_str);
        assert_eq!(
            polyline_to_string(&expected),
            polyline_to_string(&output),
            "edge_type={edge_type:?}, input={input_strs:?}"
        );
    }

    fn test_s2_polyline(input_strs: &[&str], expected_str: &str) {
        test_s2_polyline_with_edge_type(
            input_strs,
            expected_str,
            EdgeType::Directed,
            super::super::Options::default(),
        );
    }

    fn test_s2_polyline_unchanged(input_str: &str) {
        test_s2_polyline(&[input_str], input_str);
    }

    // ─── Tests ──────────────────────────────────────────────────────────

    #[test]
    fn test_polyline_layer_no_edges() {
        test_s2_polyline_unchanged("");
    }

    #[test]
    fn test_polyline_layer_one_edge() {
        test_s2_polyline_unchanged("3:4, 1:1");
        test_s2_polyline_unchanged("1:1, 3:4");
    }

    #[test]
    fn test_polyline_layer_straight_line_with_backtracking() {
        test_s2_polyline_unchanged("0:0, 1:0, 2:0, 3:0, 2:0, 1:0, 2:0, 3:0, 4:0");
    }

    #[test]
    fn test_polyline_layer_early_walk_termination_1() {
        use super::super::snap::IntLatLngSnapFunction;

        let builder_opts = super::super::Options {
            snap_function: Box::new(IntLatLngSnapFunction::new(2)),
            ..super::super::Options::default()
        };
        test_s2_polyline_with_edge_type(
            &["0:0, 0:2, 0:1"],
            "0:0, 0:1, 0:2, 0:1",
            EdgeType::Directed,
            builder_opts,
        );
    }

    #[test]
    fn test_polyline_layer_early_walk_termination_2() {
        test_s2_polyline(&["0:0, 0:1", "0:2, 0:1", "0:1, 0:2"], "0:0, 0:1, 0:2, 0:1");
    }

    #[test]
    fn test_polyline_layer_simple_loop() {
        test_s2_polyline_unchanged("0:0, 0:5, 5:5, 5:0, 0:0");
    }

    #[test]
    fn test_polyline_layer_many_loops() {
        test_s2_polyline_unchanged(
            "0:0, 2:2, 2:4, 2:2, 2:4, 4:4, 4:2, 2:2, 4:4, 4:2, 2:2, 2:0, \
             2:2, 2:0, 4:0, 2:2, 4:2, 2:2, 0:2, 0:4, 2:2, 0:4, 0:2, 2:2, \
             0:4, 2:2, 0:2, 2:2, 0:0, 0:2, 2:2, 0:0",
        );
    }

    #[test]
    fn test_polyline_layer_unordered_loops() {
        test_s2_polyline(
            &[
                "3:3, 3:2, 2:2, 2:3, 3:3",
                "1:0, 0:0, 0:1, 1:1, 1:0",
                "3:1, 3:0, 2:0, 2:1, 3:1",
                "1:3, 1:2, 0:2, 0:1, 1:3",
                "1:1, 1:2, 2:2, 2:1, 1:1",
            ],
            "3:3, 3:2, 2:2, 2:1, 3:1, 3:0, 2:0, 2:1, 1:1, 1:0, 0:0, 0:1, \
             1:1, 1:2, 0:2, 0:1, 1:3, 1:2, 2:2, 2:3, 3:3",
        );
    }

    #[test]
    fn test_polyline_layer_split_edges() {
        use super::super::snap::IntLatLngSnapFunction;

        let builder_opts = super::super::Options {
            snap_function: Box::new(IntLatLngSnapFunction::new(7)),
            split_crossing_edges: true,
            ..super::super::Options::default()
        };
        test_s2_polyline_with_edge_type(
            &["0:10, 0:0, 1:0, -1:2, 1:4, -1:6, 1:8, -1:10, -5:0, 0:0, 0:10"],
            "0:10, 0:9, 0:7, 0:5, 0:3, 0:1, 0:0, 1:0, 0:1, -1:2, 0:3, 1:4, \
             0:5, -1:6, 0:7, 1:8, 0:9, -1:10, -5:0, 0:0, 0:1, 0:3, 0:5, 0:7, \
             0:9, 0:10",
            EdgeType::Directed,
            builder_opts,
        );
    }

    #[test]
    fn test_polyline_layer_validate_ok() {
        // A valid polyline should not produce a validation error.
        let mut builder = super::super::S2Builder::new(super::super::Options::default());
        let opts = Options {
            validate: true,
            ..Options::default()
        };
        builder.start_layer(Box::new(S2PolylineLayer::with_options(opts)));
        builder.add_polyline(&make_polyline("0:0, 1:1, 2:2"));
        let mut layers = builder.build().expect("build failed");
        let layer = layers
            .remove(0)
            .into_any()
            .downcast::<S2PolylineLayer>()
            .expect("wrong layer type");
        let output = layer.into_output();
        assert_eq!(
            polyline_to_string(&output),
            polyline_to_string(&make_polyline("0:0, 1:1, 2:2"))
        );
    }

    #[test]
    fn test_polyline_layer_invalid_polyline() {
        // Antipodal vertices with validate: true should fail.
        // C++: S2PolylineLayer::InvalidPolyline
        let mut builder = super::super::S2Builder::new(super::super::Options::default());
        let opts = Options {
            validate: true,
            ..Options::default()
        };
        builder.start_layer(Box::new(S2PolylineLayer::with_options(opts)));
        let p0 = Point::from_coords(1.0, 0.0, 0.0);
        let p1 = Point::from_coords(-1.0, 0.0, 0.0);
        builder.add_edge(p0, p1);
        let result = builder.build();
        assert!(
            result.is_err(),
            "expected build to fail for antipodal vertices"
        );
        let err = result.unwrap_err();
        assert_eq!(err.code, super::super::S2ErrorCode::AntipodalVertices);
    }

    #[test]
    fn test_polyline_layer_simple_edge_labels() {
        // C++: S2PolylineLayer::SimpleEdgeLabels
        use super::super::S2Builder;

        let mut builder = S2Builder::new(super::super::Options::default());
        let opts = Options {
            edge_type: EdgeType::Undirected,
            ..Options::default()
        };
        builder.start_layer(Box::new(S2PolylineLayer::with_labels(opts)));
        builder.set_label(5);
        builder.add_polyline(&make_polyline("0:0, 0:1, 0:2"));
        builder.push_label(7);
        builder.add_polyline(&make_polyline("0:3, 0:2"));
        builder.clear_labels();
        builder.add_polyline(&make_polyline("0:3, 0:4, 0:5"));
        builder.set_label(11);
        builder.add_polyline(&make_polyline("0:6, 0:5"));

        let mut layers = builder.build().expect("build failed");
        let layer = layers
            .remove(0)
            .into_any()
            .downcast::<S2PolylineLayer>()
            .expect("wrong layer type");

        let expected: Vec<Vec<i32>> = vec![vec![5], vec![5], vec![5, 7], vec![], vec![], vec![11]];
        let ids = layer.label_set_ids().expect("labels should be present");
        let lex = layer
            .label_set_lexicon()
            .expect("lexicon should be present");
        assert_eq!(expected.len(), ids.len());
        for (i, exp) in expected.iter().enumerate() {
            let labels = lex.id_set(ids[i]);
            assert_eq!(exp, &labels, "edge {i}: expected {exp:?}, got {labels:?}");
        }
    }
}