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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
// 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

//! Discrete Hausdorff distance between two geometries.
//!
//! [`S2HausdorffDistanceQuery`] computes the discrete (vertex-based) Hausdorff
//! distance between two [`ShapeIndex`] geometries — both directed and undirected.
//!
//! The discrete directed Hausdorff distance from A to B is defined as the maximum,
//! over all vertices of A, of the closest edge distance from the vertex to B.
//!
//! Corresponds to C++ `s2hausdorff_distance_query.h/cc`.

#![expect(clippy::cast_sign_loss, reason = "EdgeId (i32) used as Vec indices")]
#![expect(
    clippy::cast_possible_truncation,
    reason = "EdgeId (i32) -> usize for Vec indexing"
)]
#![expect(
    clippy::cast_possible_wrap,
    reason = "usize -> i32 for EdgeId — always in range"
)]
use crate::s1::ChordAngle;
use crate::s2::Point;
use crate::s2::edge_query::{ClosestEdgeQuery, EdgeQueryOptions, PointTarget};
use crate::s2::predicates;
use crate::s2::shape_index::ShapeIndex;

/// Options for the Hausdorff distance query.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct HausdorffOptions {
    /// Whether to include polygon interiors when computing distances.
    /// When true (default), points inside a polygon have zero distance to it.
    pub include_interiors: bool,
}

impl Default for HausdorffOptions {
    fn default() -> Self {
        HausdorffOptions {
            include_interiors: true,
        }
    }
}

/// Result of a directed Hausdorff distance query.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DirectedResult {
    /// The directed Hausdorff distance.
    pub distance: ChordAngle,
    /// The point on the target geometry where the distance is achieved.
    pub target_point: Point,
}

/// Result of an undirected Hausdorff distance query.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct HausdorffResult {
    /// The directed result from target to source.
    pub target_to_source: DirectedResult,
    /// The directed result from source to target.
    pub source_to_target: DirectedResult,
}

impl HausdorffResult {
    /// Returns the undirected Hausdorff distance (max of both directions).
    pub fn distance(&self) -> ChordAngle {
        let a = self.target_to_source.distance;
        let b = self.source_to_target.distance;
        if a > b { a } else { b }
    }
}

/// Computes discrete Hausdorff distances between two [`ShapeIndex`] geometries.
///
/// # Examples
///
/// ```
/// use s2rst::s2::hausdorff_distance_query::S2HausdorffDistanceQuery;
/// use s2rst::s2::shape_index::ShapeIndex;
/// use s2rst::s2::lax_polyline::LaxPolyline;
/// use s2rst::s2::LatLng;
///
/// let mut a = ShapeIndex::new();
/// a.add(Box::new(LaxPolyline::new(vec![
///     LatLng::from_degrees(0.0, 0.0).to_point(),
///     LatLng::from_degrees(1.0, 0.0).to_point(),
/// ])));
/// a.build();
///
/// let mut b = ShapeIndex::new();
/// b.add(Box::new(LaxPolyline::new(vec![
///     LatLng::from_degrees(0.0, 1.0).to_point(),
///     LatLng::from_degrees(1.0, 1.0).to_point(),
/// ])));
/// b.build();
///
/// let query = S2HausdorffDistanceQuery::new();
/// let result = query.get_result(&a, &b);
/// assert!(result.is_some());
/// assert!(result.unwrap().distance().to_angle().degrees() > 0.9);
/// ```
#[derive(Debug, Default)]
pub struct S2HausdorffDistanceQuery {
    options: HausdorffOptions,
}

impl S2HausdorffDistanceQuery {
    /// Creates a new query with default options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a new query with the given options.
    pub fn with_options(options: HausdorffOptions) -> Self {
        S2HausdorffDistanceQuery { options }
    }

    /// Returns a reference to the options.
    pub fn options(&self) -> &HausdorffOptions {
        &self.options
    }

    /// Returns a mutable reference to the options.
    pub fn options_mut(&mut self) -> &mut HausdorffOptions {
        &mut self.options
    }

    /// Computes the directed Hausdorff distance from `target` to `source`.
    ///
    /// Returns `None` if either index is empty (has no vertices).
    pub fn get_directed_result(
        &self,
        target: &ShapeIndex,
        source: &ShapeIndex,
    ) -> Option<DirectedResult> {
        let query = ClosestEdgeQuery::new(source);
        let opts = EdgeQueryOptions::default()
            .max_results(1)
            .include_interiors(self.options.include_interiors);

        let mut max_distance = ChordAngle::NEGATIVE;
        let mut target_point = Point::origin();
        let mut source_point = Point::origin();

        // Iterate over all vertices of the target index.
        for shape_id in 0..target.len() as i32 {
            let Some(shape) = target.shape(shape_id) else {
                continue;
            };

            for chain_id in 0..shape.num_chains() {
                let chain = shape.chain(chain_id);
                // Visit all vertices in this chain.
                // For a chain of length L, there are L+1 vertices (for polylines)
                // or L vertices (for loops). We gather them from edges.
                if chain.length == 0 {
                    continue;
                }

                // First vertex of first edge
                let first_edge = shape.chain_edge(chain_id, 0);
                self.update_max_distance(
                    first_edge.v0,
                    &query,
                    &opts,
                    &mut max_distance,
                    &mut target_point,
                    &mut source_point,
                );

                // v1 of each edge
                for offset in 0..chain.length {
                    let edge = shape.chain_edge(chain_id, offset);
                    self.update_max_distance(
                        edge.v1,
                        &query,
                        &opts,
                        &mut max_distance,
                        &mut target_point,
                        &mut source_point,
                    );
                }
            }
        }

        if max_distance.is_negative() {
            None
        } else {
            Some(DirectedResult {
                distance: max_distance,
                target_point,
            })
        }
    }

    /// Returns the directed Hausdorff distance, or `ChordAngle::INFINITY` if
    /// either index is empty.
    pub fn get_directed_distance(&self, target: &ShapeIndex, source: &ShapeIndex) -> ChordAngle {
        self.get_directed_result(target, source)
            .map_or(ChordAngle::INFINITY, |r| r.distance)
    }

    /// Returns whether the directed Hausdorff distance is less than `limit`.
    pub fn is_directed_distance_less(
        &self,
        target: &ShapeIndex,
        source: &ShapeIndex,
        distance_limit: ChordAngle,
    ) -> bool {
        let query = ClosestEdgeQuery::new(source);
        let opts = EdgeQueryOptions::default()
            .max_results(1)
            .include_interiors(self.options.include_interiors);

        let mut max_distance = ChordAngle::NEGATIVE;
        let mut target_point = Point::origin();
        let mut source_point = Point::origin();

        for shape_id in 0..target.len() as i32 {
            let Some(shape) = target.shape(shape_id) else {
                continue;
            };

            for chain_id in 0..shape.num_chains() {
                let chain = shape.chain(chain_id);
                if chain.length == 0 {
                    continue;
                }

                let first_edge = shape.chain_edge(chain_id, 0);
                self.update_max_distance(
                    first_edge.v0,
                    &query,
                    &opts,
                    &mut max_distance,
                    &mut target_point,
                    &mut source_point,
                );
                if max_distance > distance_limit {
                    return false;
                }

                for offset in 0..chain.length {
                    let edge = shape.chain_edge(chain_id, offset);
                    self.update_max_distance(
                        edge.v1,
                        &query,
                        &opts,
                        &mut max_distance,
                        &mut target_point,
                        &mut source_point,
                    );
                    if max_distance > distance_limit {
                        return false;
                    }
                }
            }
        }

        !max_distance.is_negative()
    }

    /// Computes the undirected Hausdorff distance between `target` and `source`.
    ///
    /// Returns `None` if either index is empty.
    pub fn get_result(&self, target: &ShapeIndex, source: &ShapeIndex) -> Option<HausdorffResult> {
        let t2s = self.get_directed_result(target, source)?;
        let s2t = self.get_directed_result(source, target)?;
        Some(HausdorffResult {
            target_to_source: t2s,
            source_to_target: s2t,
        })
    }

    /// Returns the undirected Hausdorff distance, or `ChordAngle::INFINITY` if
    /// either index is empty.
    pub fn get_distance(&self, target: &ShapeIndex, source: &ShapeIndex) -> ChordAngle {
        self.get_result(target, source)
            .map_or(ChordAngle::INFINITY, |r| r.distance())
    }

    /// Returns whether the undirected Hausdorff distance is less than `limit`.
    pub fn is_distance_less(
        &self,
        target: &ShapeIndex,
        source: &ShapeIndex,
        distance_limit: ChordAngle,
    ) -> bool {
        self.is_directed_distance_less(target, source, distance_limit)
            && self.is_directed_distance_less(source, target, distance_limit)
    }

    /// Internal: updates `max_distance` if the closest edge distance from `point`
    /// to the source index exceeds the current max.
    #[expect(clippy::unused_self, reason = "matches C++ method signature")]
    fn update_max_distance(
        &self,
        point: Point,
        query: &ClosestEdgeQuery,
        opts: &EdgeQueryOptions,
        max_distance: &mut ChordAngle,
        target_point: &mut Point,
        source_point: &mut Point,
    ) {
        // Optimization: skip if point is closer to last source_point than current max.
        if !max_distance.is_negative()
            && predicates::compare_distance(point, *source_point, *max_distance) <= 0
        {
            return;
        }

        let target = PointTarget::new(point);
        let results = query.find_edges(&target, opts);
        if let Some(result) = results.first()
            && *max_distance < result.distance
        {
            *max_distance = result.distance;
            *target_point = point;
            // Project point onto the closest edge to get source_point.
            if result.edge_id >= 0
                && let Some(shape) = query.index().shape(result.shape_id)
            {
                let edge = shape.edge(result.edge_id as usize);
                *source_point = crate::s2::edge_distances::project(point, edge.v0, edge.v1);
            } else {
                // Interior result — source_point is the point itself.
                *source_point = point;
            }
        }
    }
}

#[cfg(test)]
#[expect(
    clippy::field_reassign_with_default,
    reason = "clearer than a single struct literal with many fields"
)]
mod tests {
    use super::*;
    use crate::s2::LatLng;
    use crate::s2::lax_polyline::LaxPolyline;
    use crate::s2::point_vector::PointVector;
    use crate::s2::text_format;

    fn p(lat: f64, lng: f64) -> Point {
        LatLng::from_degrees(lat, lng).to_point()
    }

    fn make_index_with_polyline(points: Vec<Point>) -> ShapeIndex {
        let mut index = ShapeIndex::new();
        index.add(Box::new(LaxPolyline::new(points)));
        index.build();
        index
    }

    fn make_index_with_points(points: Vec<Point>) -> ShapeIndex {
        let mut index = ShapeIndex::new();
        index.add(Box::new(PointVector::new(points)));
        index.build();
        index
    }

    #[test]
    fn test_result_constructors() {
        let point1 = p(3.0, 4.0);
        let point2 = p(5.0, 6.0);
        let dist1 = ChordAngle::from_degrees(5.0);
        let dist2 = ChordAngle::from_degrees(5.0);

        let dr1 = DirectedResult {
            distance: dist1,
            target_point: point1,
        };
        let dr2 = DirectedResult {
            distance: dist2,
            target_point: point2,
        };
        let result = HausdorffResult {
            target_to_source: dr1.clone(),
            source_to_target: dr2.clone(),
        };

        assert_eq!(dr1.target_point, point1);
        assert_eq!(dr1.distance, dist1);
        assert_eq!(result.target_to_source.target_point, point1);
        assert_eq!(result.source_to_target.target_point, point2);
        assert_eq!(result.distance(), dist2);
    }

    #[test]
    fn test_options() {
        let default_opts = HausdorffOptions::default();
        assert!(default_opts.include_interiors);

        let mut opts = HausdorffOptions::default();
        opts.include_interiors = false;
        assert!(!opts.include_interiors);
    }

    #[test]
    fn test_query_options_accessors() {
        let mut query = S2HausdorffDistanceQuery::new();
        assert!(query.options().include_interiors);

        query.options_mut().include_interiors = false;
        assert!(!query.options().include_interiors);
    }

    #[test]
    fn test_simple_polyline_queries() {
        let a0 = text_format::parse_points("0:0, 0:1, 0:1.5");
        let a1 = text_format::parse_points("0:2, 0:1.5, -10:1");
        let b0 = text_format::parse_points("1:0, 1:1, 3:2");

        let empty_index = ShapeIndex::new();

        // Shape index a has 2 polylines: a0 and a1.
        let mut a = ShapeIndex::new();
        a.add(Box::new(LaxPolyline::new(a0.clone())));
        a.add(Box::new(LaxPolyline::new(a1.clone())));
        a.build();

        // Shape index b has 1 polyline: b0.
        let mut b = ShapeIndex::new();
        b.add(Box::new(LaxPolyline::new(b0.clone())));
        b.build();

        let query = S2HausdorffDistanceQuery::new();

        // Empty index tests
        assert!(query.get_directed_result(&empty_index, &a).is_none());
        assert!(query.get_directed_result(&a, &empty_index).is_none());
        assert!(query.get_directed_distance(&a, &empty_index).is_infinity());
        assert!(!query.is_directed_distance_less(
            &empty_index,
            &a,
            ChordAngle::from_degrees(360.0)
        ));
        assert!(!query.is_directed_distance_less(
            &a,
            &empty_index,
            ChordAngle::from_degrees(360.0)
        ));

        // Directed distances
        let expected_a_to_b = a1[2].chord_angle(b0[1]);
        let expected_b_to_a = b0[2].chord_angle(a1[0]);

        let dir_a_to_b = query.get_directed_result(&a, &b);
        let dir_b_to_a = query.get_directed_result(&b, &a);

        assert!(dir_a_to_b.is_some());
        assert!(dir_b_to_a.is_some());

        let dir_a_to_b = dir_a_to_b.unwrap();
        let dir_b_to_a = dir_b_to_a.unwrap();

        assert!((dir_a_to_b.distance.degrees() - expected_a_to_b.degrees()).abs() < 1e-10);
        assert!((dir_b_to_a.distance.degrees() - expected_b_to_a.degrees()).abs() < 1e-10);

        let dir_a_to_b_dist = query.get_directed_distance(&a, &b);
        assert!((dir_a_to_b_dist.degrees() - expected_a_to_b.degrees()).abs() < 1e-10);

        // IsDirectedDistanceLess
        assert!(query.is_directed_distance_less(
            &a,
            &b,
            dir_a_to_b_dist + ChordAngle::from_degrees(1.0)
        ));
        assert!(!query.is_directed_distance_less(
            &a,
            &b,
            dir_a_to_b_dist - ChordAngle::from_degrees(1.0)
        ));

        // Undirected tests
        let a_to_b = query.get_result(&a, &b);
        let b_to_a = query.get_result(&b, &a);
        let bb = query.get_result(&b, &b);

        assert!(a_to_b.is_some());
        assert!(b_to_a.is_some());
        assert!(bb.is_some());

        let a_to_b = a_to_b.unwrap();
        let b_to_a_r = b_to_a.unwrap();
        let bb = bb.unwrap();

        assert!((a_to_b.distance().degrees() - b_to_a_r.distance().degrees()).abs() < 1e-10);
        assert!(bb.distance().degrees() < 1e-10);

        let b_to_a_dist = query.get_distance(&b, &a);
        assert!((b_to_a_dist.degrees() - b_to_a_r.distance().degrees()).abs() < 1e-10);

        // IsDistanceLess
        let larger = dir_a_to_b
            .distance
            .radians()
            .max(dir_b_to_a.distance.radians());
        let smaller = dir_a_to_b
            .distance
            .radians()
            .min(dir_b_to_a.distance.radians());
        let average = f64::midpoint(larger, smaller);

        assert!(query.is_distance_less(&a, &b, ChordAngle::from_radians(larger + 0.001)));
        assert!(!query.is_distance_less(&a, &b, ChordAngle::from_radians(average)));
        assert!(!query.is_distance_less(&a, &b, ChordAngle::from_radians(smaller - 0.001)));
        assert!(query.is_distance_less(&b, &b, ChordAngle::from_degrees(0.0)));
    }

    #[test]
    fn test_point_vector_shape_queries() {
        let a_points = text_format::parse_points("2:0, 0:1, 1:2, 0:3, 0:4");
        let b_points = text_format::parse_points("-1:2, -0.5:0.5, -0.5:3.5");

        let a = make_index_with_polyline(a_points.clone());
        let b = make_index_with_points(b_points.clone());

        let query = S2HausdorffDistanceQuery::new();

        let expected_a_to_b = a_points[0].chord_angle(b_points[1]);
        let expected_b_to_a = b_points[0].chord_angle(a_points[3]);
        let expected_a_b = if expected_a_to_b > expected_b_to_a {
            expected_a_to_b
        } else {
            expected_b_to_a
        };

        let dir_a_to_b = query.get_directed_result(&a, &b).unwrap();
        let dir_b_to_a = query.get_directed_result(&b, &a).unwrap();
        let undirected_a_b = query.get_distance(&a, &b);

        assert!(!undirected_a_b.is_infinity());
        assert!((undirected_a_b.degrees() - expected_a_b.degrees()).abs() < 1e-10);
        assert!((dir_a_to_b.distance.degrees() - expected_a_to_b.degrees()).abs() < 1e-10);
        assert_eq!(dir_a_to_b.target_point, a_points[0]);
        assert!((dir_b_to_a.distance.degrees() - expected_b_to_a.degrees()).abs() < 1e-10);
        assert_eq!(dir_b_to_a.target_point, b_points[0]);

        // IsDirectedDistanceLess
        assert!(query.is_directed_distance_less(
            &a,
            &b,
            ChordAngle::from_degrees(expected_a_to_b.degrees() + 0.01)
        ));
        assert!(query.is_directed_distance_less(
            &b,
            &a,
            ChordAngle::from_degrees(expected_b_to_a.degrees() + 0.01)
        ));
        assert!(!query.is_directed_distance_less(
            &a,
            &b,
            ChordAngle::from_degrees(expected_a_to_b.degrees() - 0.01)
        ));
        assert!(!query.is_directed_distance_less(
            &b,
            &a,
            ChordAngle::from_degrees(expected_b_to_a.degrees() - 0.01)
        ));

        // IsDistanceLess
        assert!(query.is_distance_less(
            &a,
            &b,
            ChordAngle::from_degrees(expected_a_b.degrees() + 0.01)
        ));
        assert!(!query.is_distance_less(
            &b,
            &a,
            ChordAngle::from_degrees(expected_b_to_a.degrees() - 0.01)
        ));
    }

    #[test]
    fn test_overlapping_polygons() {
        let epsilon = 3.0e-3;

        // Triangle with first two vertices inside quadrangle.
        let mut a = ShapeIndex::new();
        a.add(Box::new(text_format::make_lax_polygon("1:1, 1:2, 3.5:1.5")));
        a.build();

        // Quadrangle.
        let mut b = ShapeIndex::new();
        b.add(Box::new(text_format::make_lax_polygon(
            "0:0, 0:3, 3:3, 3:0",
        )));
        b.build();

        // Triangle fully inside quadrangle.
        let mut c = ShapeIndex::new();
        c.add(Box::new(text_format::make_lax_polygon("0:0, 0:2, 3:0")));
        c.build();

        // Query 1: exclude interiors
        let query1 = S2HausdorffDistanceQuery::with_options(HausdorffOptions {
            include_interiors: false,
        });

        let a_to_b_1 = query1.get_directed_result(&a, &b).unwrap();
        // Without interiors, the Hausdorff distance is from the vertex (1,2)
        // which is inside the quadrangle, to the nearest edge — about 1 degree.
        assert!((a_to_b_1.distance.degrees() - 1.0).abs() < epsilon);
        assert_eq!(a_to_b_1.target_point, p(1.0, 2.0));

        assert!(query1.is_directed_distance_less(&c, &b, ChordAngle::from_degrees(1.0 + epsilon)));

        // Query 2: include interiors
        let query2 = S2HausdorffDistanceQuery::with_options(HausdorffOptions {
            include_interiors: true,
        });

        let a_to_b_2 = query2.get_directed_result(&a, &b).unwrap();
        // With interiors, the two vertices inside the quadrangle have zero distance.
        // The max distance is from (3.5, 1.5) which is outside — about 0.5 degrees.
        assert!((a_to_b_2.distance.degrees() - 0.5).abs() < epsilon);
        assert_eq!(a_to_b_2.target_point, p(3.5, 1.5));

        // C is fully inside B, so directed distance should be ~0.
        assert!(query2.is_directed_distance_less(&c, &b, ChordAngle::from_degrees(epsilon)));
    }

    #[test]
    fn test_whole_world() {
        let mut a = ShapeIndex::new();
        a.add(Box::new(PointVector::new(text_format::parse_points("1:1"))));
        a.build();

        let b = text_format::make_index("# # full");

        let query = S2HausdorffDistanceQuery::with_options(HausdorffOptions {
            include_interiors: true,
        });

        // Point to full polygon: directed distance should be 0.
        let a_to_b = query.get_directed_result(&a, &b);
        assert!(a_to_b.is_some());
        assert_eq!(a_to_b.unwrap().distance.degrees(), 0.0);

        // Full polygon to point: no vertices in the full polygon, returns None.
        let b_to_a = query.get_directed_result(&b, &a);
        assert!(b_to_a.is_none());

        // Undirected: should fail since one direction returns None.
        assert!(query.get_result(&b, &a).is_none());
        assert!(query.get_result(&a, &b).is_none());

        // IsDirectedDistanceLess
        assert!(query.is_directed_distance_less(&a, &b, ChordAngle::ZERO));
        assert!(!query.is_directed_distance_less(&b, &a, ChordAngle::INFINITY));
        assert!(!query.is_distance_less(&a, &b, ChordAngle::INFINITY));
    }

    #[test]
    fn test_whole_world_same_reference() {
        let a = text_format::make_index("# # full");
        let b = text_format::make_index("# # full");

        let query = S2HausdorffDistanceQuery::with_options(HausdorffOptions {
            include_interiors: true,
        });

        assert!(query.get_result(&a, &b).is_none());
        assert!(query.get_result(&a, &a).is_none());
        assert!(!query.is_distance_less(&a, &b, ChordAngle::INFINITY));
        assert!(!query.is_distance_less(&a, &a, ChordAngle::INFINITY));
    }

    #[cfg(feature = "serde")]
    #[test]
    fn test_serde_options_roundtrip() {
        let opts = HausdorffOptions {
            include_interiors: false,
        };
        let json = serde_json::to_string(&opts).unwrap();
        let back: HausdorffOptions = serde_json::from_str(&json).unwrap();
        assert_eq!(opts.include_interiors, back.include_interiors);
    }

    #[cfg(feature = "serde")]
    #[test]
    fn test_serde_directed_result_roundtrip() {
        let dr = DirectedResult {
            distance: ChordAngle::from_degrees(45.0),
            target_point: Point::from_coords(1.0, 0.0, 0.0),
        };
        let json = serde_json::to_string(&dr).unwrap();
        let back: DirectedResult = serde_json::from_str(&json).unwrap();
        assert_eq!(dr.distance, back.distance);
        assert_eq!(dr.target_point, back.target_point);
    }

    #[cfg(feature = "serde")]
    #[test]
    fn test_serde_hausdorff_result_roundtrip() {
        let p1 = Point::from_coords(1.0, 0.0, 0.0);
        let p2 = Point::from_coords(0.0, 1.0, 0.0);
        let hr = HausdorffResult {
            target_to_source: DirectedResult {
                distance: ChordAngle::from_degrees(30.0),
                target_point: p1,
            },
            source_to_target: DirectedResult {
                distance: ChordAngle::from_degrees(60.0),
                target_point: p2,
            },
        };
        let json = serde_json::to_string(&hr).unwrap();
        let back: HausdorffResult = serde_json::from_str(&json).unwrap();
        assert_eq!(hr.distance(), back.distance());
    }
}