s2rst 0.3.2

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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
// 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

//! Find furthest edges between geometries using an `S2ShapeIndex`.
//!
//! This is the dual of [`closest_edge_query`](super::closest_edge_query): it finds edges that maximize
//! distance rather than minimize it.
//!
//! Corresponds to C++ `s2furthest_edge_query.h`.

#![expect(
    clippy::cast_sign_loss,
    reason = "max_results/EdgeId (i32) used as Vec indices"
)]
#![expect(
    clippy::cast_possible_truncation,
    reason = "max_results/EdgeId (i32) -> usize for Vec sizing"
)]
#![expect(
    clippy::cast_possible_wrap,
    reason = "usize -> i32 for max_results/EdgeId — always in range"
)]
use std::ops::ControlFlow;

use crate::s1::ChordAngle;
use crate::s2::distance_target::DistanceTarget;
use crate::s2::edge_distances;
use crate::s2::shape::{Edge, ShapeId};
use crate::s2::shape_index::ShapeIndex;
use crate::s2::{Cap, Cell, Point};

/// A result from a furthest edge query.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Result {
    /// The distance from the target to this edge.
    pub distance: ChordAngle,
    /// The shape ID in the index.
    pub shape_id: ShapeId,
    /// The edge ID within the shape, or -1 for interior.
    pub edge_id: i32,
}

impl Result {
    /// Returns an empty result (no edge found).
    pub fn empty() -> Self {
        Result {
            distance: ChordAngle::NEGATIVE,
            shape_id: ShapeId(-1),
            edge_id: -1,
        }
    }

    /// Returns true if this result represents a polygon interior.
    pub fn is_interior(&self) -> bool {
        self.shape_id >= 0 && self.edge_id < 0
    }

    /// Returns true if no edge was found.
    pub fn is_empty(&self) -> bool {
        self.shape_id < 0
    }
}

impl PartialEq for Result {
    fn eq(&self, other: &Self) -> bool {
        self.distance == other.distance
            && self.shape_id == other.shape_id
            && self.edge_id == other.edge_id
    }
}

impl Eq for Result {}

impl PartialOrd for Result {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Result {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        // Reverse: furthest first.
        other
            .distance
            .length2()
            .partial_cmp(&self.distance.length2())
            .unwrap_or(std::cmp::Ordering::Equal)
            .then_with(|| self.shape_id.cmp(&other.shape_id))
            .then_with(|| self.edge_id.cmp(&other.edge_id))
    }
}

/// Options for a furthest edge query.
#[derive(Clone, Debug, PartialEq)]
pub struct Options {
    /// Maximum number of results to return (default: `i32::MAX`).
    pub max_results: i32,
    /// Minimum distance for edges to be included (default: zero).
    pub min_distance: ChordAngle,
    /// Maximum error allowed (trades accuracy for speed).
    pub max_error: ChordAngle,
    /// Whether to include polygon interiors.
    pub include_interiors: bool,
    /// Force brute-force search.
    pub use_brute_force: bool,
}

impl Default for Options {
    fn default() -> Self {
        Options {
            max_results: i32::MAX,
            // C++ default: S2MaxDistance::Infinity() which maps to
            // S1ChordAngle::Negative(), meaning no minimum distance limit.
            min_distance: ChordAngle::NEGATIVE,
            max_error: ChordAngle::ZERO,
            include_interiors: true,
            use_brute_force: false,
        }
    }
}

impl Options {
    /// Sets `min_distance` so that edges at exactly `limit` are also returned.
    /// Equivalent to `min_distance = limit.predecessor()`.
    pub fn inclusive_min_distance(&mut self, limit: ChordAngle) {
        self.min_distance = limit.predecessor();
    }

    /// Sets `min_distance` so that all edges whose true distance is ≥ `limit`
    /// are returned, accounting for the maximum error in distance computation.
    pub fn conservative_min_distance(&mut self, limit: ChordAngle) {
        self.min_distance = limit
            .plus_error(-edge_distances::update_min_distance_max_error(limit))
            .predecessor();
    }
}

/// The target geometry to measure distance from.
/// The target geometry to measure distance from (furthest-edge variant).
///
/// Extends [`DistanceTarget`] with
/// methods specific to furthest-edge queries.
pub trait Target: DistanceTarget {
    /// Updates `dist_limit` if the distance from `p` to the target is greater.
    /// Returns `(new_dist, true)` if updated, or `(dist_limit, false)`.
    fn update_distance_to_point(&self, p: Point, dist_limit: ChordAngle) -> (ChordAngle, bool);

    /// Updates `dist_limit` if the distance from edge (v0, v1) to the target
    /// is greater. Returns `(new_dist, true)` if updated.
    fn update_distance_to_edge(
        &self,
        v0: Point,
        v1: Point,
        dist_limit: ChordAngle,
    ) -> (ChordAngle, bool);

    /// Updates `dist_limit` if the distance from the cell to the target is
    /// greater. Returns `(new_dist, true)` if updated.
    fn update_distance_to_cell(&self, cell: &Cell, dist_limit: ChordAngle) -> (ChordAngle, bool);

    /// Visits shapes in `index` whose interior contains the target (or its
    /// antipode, for furthest queries). For furthest-edge queries, polygons
    /// containing the **antipode** of the target are visited, since those
    /// represent distance π (the maximum possible).
    ///
    /// Corresponds to C++ `S2MaxDistanceTarget::VisitContainingShapeIds`.
    fn visit_containing_shapes(
        &self,
        _index: &ShapeIndex,
        _visitor: &mut dyn FnMut(ShapeId) -> ControlFlow<()>,
    ) {
        // Default: do nothing. Subtypes override.
    }

    /// Maximum index size for which brute force is faster than an indexed
    /// search. The default is 100; subtypes override with values tuned
    /// to their geometry.
    fn max_brute_force_index_size(&self) -> i32 {
        100
    }
}

/// Target: find furthest edges from a point.
#[derive(Debug)]
pub struct PointTarget {
    point: Point,
}

impl PointTarget {
    /// Creates a target from a point.
    pub fn new(point: Point) -> Self {
        PointTarget { point }
    }
}

impl DistanceTarget for PointTarget {
    fn cap_bound(&self) -> Cap {
        Cap::from_point(self.point)
    }
}

impl Target for PointTarget {
    fn max_brute_force_index_size(&self) -> i32 {
        // C++: break-even ~100/400/600 for point cloud/fractal/regular.
        300
    }

    fn visit_containing_shapes(
        &self,
        index: &ShapeIndex,
        visitor: &mut dyn FnMut(ShapeId) -> ControlFlow<()>,
    ) {
        // For furthest-edge queries, visit polygons whose interior contains
        // the antipode of the target point. Matches C++
        // S2MaxDistancePointTarget::VisitContainingShapeIds.
        use crate::s2::contains_point_query::ContainsPointQuery;
        let mut query = ContainsPointQuery::new(
            index,
            crate::s2::contains_point_query::VertexModel::SemiOpen,
        );
        for shape_id in query.containing_shape_ids(-self.point) {
            if visitor(shape_id).is_break() {
                break;
            }
        }
    }
    fn update_distance_to_point(&self, p: Point, max_dist: ChordAngle) -> (ChordAngle, bool) {
        let dist = p.chord_angle(self.point);
        if dist > max_dist {
            (dist, true)
        } else {
            (max_dist, false)
        }
    }
    fn update_distance_to_edge(
        &self,
        v0: Point,
        v1: Point,
        max_dist: ChordAngle,
    ) -> (ChordAngle, bool) {
        edge_distances::update_max_distance(self.point, v0, v1, max_dist)
    }
    fn update_distance_to_cell(&self, cell: &Cell, max_dist: ChordAngle) -> (ChordAngle, bool) {
        let dist = cell.max_distance_to_point(self.point);
        if dist > max_dist {
            (dist, true)
        } else {
            (max_dist, false)
        }
    }
}

/// Target: find furthest edges from an edge.
#[derive(Debug)]
pub struct EdgeTarget {
    a: Point,
    b: Point,
}

impl EdgeTarget {
    /// Creates a target from an edge.
    pub fn new(a: Point, b: Point) -> Self {
        EdgeTarget { a, b }
    }
}

impl DistanceTarget for EdgeTarget {
    fn cap_bound(&self) -> Cap {
        Cap::from_point(Point((self.a.0 + self.b.0).normalize()))
            .expanded(self.a.chord_angle(self.b).to_angle() * 0.5)
    }
}

impl Target for EdgeTarget {
    fn max_brute_force_index_size(&self) -> i32 {
        // C++: break-even ~80/100/230 for point cloud/fractal/regular.
        110
    }

    fn visit_containing_shapes(
        &self,
        index: &ShapeIndex,
        visitor: &mut dyn FnMut(ShapeId) -> ControlFlow<()>,
    ) {
        // Only need to test one endpoint. If the tested vertex antipode is
        // not contained, the full edge antipode is not contained; if it is
        // contained, the edge at least intersects the polygon.
        // Matches C++ S2MaxDistanceEdgeTarget::VisitContainingShapeIds.
        use crate::s2::contains_point_query::ContainsPointQuery;
        let mut query = ContainsPointQuery::new(
            index,
            crate::s2::contains_point_query::VertexModel::SemiOpen,
        );
        for shape_id in query.containing_shape_ids(-self.a) {
            if visitor(shape_id).is_break() {
                break;
            }
        }
    }
    fn update_distance_to_point(&self, p: Point, max_dist: ChordAngle) -> (ChordAngle, bool) {
        edge_distances::update_max_distance(p, self.a, self.b, max_dist)
    }
    fn update_distance_to_edge(
        &self,
        v0: Point,
        v1: Point,
        max_dist: ChordAngle,
    ) -> (ChordAngle, bool) {
        // Delegates to update_edge_pair_max_distance which checks for
        // antipodal crossing (max distance = π) before falling back to
        // the four vertex-to-edge distance computations.
        edge_distances::update_edge_pair_max_distance(self.a, self.b, v0, v1, max_dist)
    }
    fn update_distance_to_cell(&self, cell: &Cell, max_dist: ChordAngle) -> (ChordAngle, bool) {
        let dist = cell.max_distance_to_edge(self.a, self.b);
        if dist > max_dist {
            (dist, true)
        } else {
            (max_dist, false)
        }
    }
}

/// Target: find furthest edges from a cell.
#[derive(Debug)]
pub struct CellTarget {
    cell: Cell,
}

impl CellTarget {
    /// Creates a target from a cell.
    pub fn new(cell: Cell) -> Self {
        CellTarget { cell }
    }
}

impl DistanceTarget for CellTarget {
    fn cap_bound(&self) -> Cap {
        self.cell.cap_bound()
    }
}

impl Target for CellTarget {
    fn visit_containing_shapes(
        &self,
        index: &ShapeIndex,
        visitor: &mut dyn FnMut(ShapeId) -> ControlFlow<()>,
    ) {
        // Matches C++ S2MaxDistanceCellTarget::VisitContainingShapeIds.
        use crate::s2::contains_point_query::ContainsPointQuery;
        let mut query = ContainsPointQuery::new(
            index,
            crate::s2::contains_point_query::VertexModel::SemiOpen,
        );
        for shape_id in query.containing_shape_ids(-self.cell.center()) {
            if visitor(shape_id).is_break() {
                break;
            }
        }
    }
    fn update_distance_to_point(&self, p: Point, max_dist: ChordAngle) -> (ChordAngle, bool) {
        let dist = self.cell.max_distance_to_point(p);
        if dist > max_dist {
            (dist, true)
        } else {
            (max_dist, false)
        }
    }
    fn update_distance_to_edge(
        &self,
        v0: Point,
        v1: Point,
        max_dist: ChordAngle,
    ) -> (ChordAngle, bool) {
        let dist = self.cell.max_distance_to_edge(v0, v1);
        if dist > max_dist {
            (dist, true)
        } else {
            (max_dist, false)
        }
    }
    fn update_distance_to_cell(&self, cell: &Cell, max_dist: ChordAngle) -> (ChordAngle, bool) {
        let dist = self.cell.max_distance_to_cell(*cell);
        if dist > max_dist {
            (dist, true)
        } else {
            (max_dist, false)
        }
    }
}

/// Target: find furthest edges from any edge in another `ShapeIndex`.
#[derive(Debug)]
pub struct ShapeIndexTarget<'a> {
    index: &'a ShapeIndex,
    /// Whether to include polygon interiors in the target.
    pub include_interiors: bool,
    /// Whether the internal query should use brute force.
    pub use_brute_force: bool,
}

impl<'a> ShapeIndexTarget<'a> {
    /// Creates a target from a `ShapeIndex`.
    pub fn new(index: &'a ShapeIndex) -> Self {
        ShapeIndexTarget {
            index,
            include_interiors: true,
            use_brute_force: false,
        }
    }

    /// Helper: find the furthest edge in `self.index` from a given inner target,
    /// then check if that distance exceeds `max_dist`.
    fn update_max_distance_inner(
        &self,
        inner_target: &dyn Target,
        max_dist: ChordAngle,
    ) -> (ChordAngle, bool) {
        let query = FurthestEdgeQuery::new(self.index);
        let opts = Options {
            max_results: 1,
            min_distance: max_dist,
            include_interiors: self.include_interiors,
            use_brute_force: self.use_brute_force,
            ..Options::default()
        };
        let result = query.find_furthest_edge_with_options(inner_target, &opts);
        if result.is_empty() {
            (max_dist, false)
        } else {
            (result.distance, true)
        }
    }
}

impl DistanceTarget for ShapeIndexTarget<'_> {
    fn cap_bound(&self) -> Cap {
        use crate::s2::region::Region;
        use crate::s2::shape_index_region::ShapeIndexRegion;
        let region = ShapeIndexRegion::new(self.index);
        region.cap_bound()
    }
    fn set_max_error(&mut self, _max_error: ChordAngle) -> bool {
        true
    }
}

impl Target for ShapeIndexTarget<'_> {
    fn max_brute_force_index_size(&self) -> i32 {
        // C++: break-even ~30/100/130 for point cloud/fractal/regular.
        70
    }

    fn visit_containing_shapes(
        &self,
        query_index: &ShapeIndex,
        visitor: &mut dyn FnMut(ShapeId) -> ControlFlow<()>,
    ) {
        // For each shape in the target index, test chain start vertices
        // (one per connected component). For shapes with no edges, use
        // the reference point if contained. Matches C++
        // S2MaxDistanceShapeIndexTarget::VisitContainingShapeIds.
        for shape_id in (0..self.index.num_shape_ids() as i32).map(ShapeId) {
            let Some(shape) = self.index.shape(shape_id) else {
                continue;
            };
            let num_chains = shape.num_chains();
            let mut tested_point = false;
            for c in 0..num_chains {
                let chain = shape.chain(c);
                if chain.length == 0 {
                    continue;
                }
                tested_point = true;
                let v0 = shape.chain_edge(c, 0).v0;
                let pt = PointTarget::new(v0);
                pt.visit_containing_shapes(query_index, visitor);
            }
            if !tested_point {
                // Handle full polygons with no edges.
                let ref_pt = shape.reference_point();
                if !ref_pt.contained {
                    continue;
                }
                let pt = PointTarget::new(ref_pt.point);
                pt.visit_containing_shapes(query_index, visitor);
            }
        }
    }
    fn update_distance_to_point(&self, p: Point, max_dist: ChordAngle) -> (ChordAngle, bool) {
        let inner = PointTarget::new(p);
        self.update_max_distance_inner(&inner, max_dist)
    }
    fn update_distance_to_edge(
        &self,
        v0: Point,
        v1: Point,
        max_dist: ChordAngle,
    ) -> (ChordAngle, bool) {
        let inner = EdgeTarget::new(v0, v1);
        self.update_max_distance_inner(&inner, max_dist)
    }
    fn update_distance_to_cell(&self, cell: &Cell, max_dist: ChordAngle) -> (ChordAngle, bool) {
        let inner = CellTarget::new(*cell);
        self.update_max_distance_inner(&inner, max_dist)
    }
}

/// Query to find furthest edges in a `ShapeIndex` from a given target.
#[derive(Debug)]
pub struct FurthestEdgeQuery<'a> {
    index: &'a ShapeIndex,
}

impl<'a> FurthestEdgeQuery<'a> {
    /// Creates a new furthest edge query for the given index.
    pub fn new(index: &'a ShapeIndex) -> Self {
        FurthestEdgeQuery { index }
    }

    /// Returns the furthest edge from the target, or an empty result.
    pub fn find_furthest_edge(&self, target: &dyn Target) -> Result {
        let opts = Options {
            max_results: 1,
            ..Options::default()
        };
        self.find_furthest_edge_with_options(target, &opts)
    }

    /// Returns the furthest edge from the target with the given options.
    /// Returns the furthest edge with the given options.
    /// For best performance, set `options.max_results = 1`.
    pub fn find_furthest_edge_with_options(
        &self,
        target: &dyn Target,
        options: &Options,
    ) -> Result {
        let results = self.find_furthest_edges(target, options);
        match results.into_iter().next() {
            Some(r) => r,
            None => Result::empty(),
        }
    }

    /// Returns the furthest edges from the target (up to `options.max_results`).
    pub fn find_furthest_edges(&self, target: &dyn Target, options: &Options) -> Vec<Result> {
        debug_assert!(options.max_results >= 1, "max_results must be >= 1");
        debug_assert!(
            target.max_brute_force_index_size() >= 0,
            "max_brute_force_index_size must be >= 0"
        );

        let mut results = Vec::new();

        let mut distance_limit = options.min_distance;

        // Check polygon interiors if requested. For furthest queries,
        // polygons containing the antipode of the target have distance π
        // (STRAIGHT). Matches C++ include_interiors handling.
        if options.include_interiors {
            let max_results = options.max_results as usize;
            let mut shape_ids = Vec::new();
            target.visit_containing_shapes(self.index, &mut |shape_id| {
                shape_ids.push(shape_id);
                if shape_ids.len() < max_results {
                    ControlFlow::Continue(())
                } else {
                    ControlFlow::Break(())
                }
            });
            for shape_id in shape_ids {
                // Only add if STRAIGHT exceeds the current distance limit.
                if ChordAngle::STRAIGHT > distance_limit {
                    self.add_result(
                        Result {
                            distance: ChordAngle::STRAIGHT,
                            shape_id,
                            edge_id: -1,
                        },
                        options,
                        &mut distance_limit,
                        &mut results,
                    );
                }
            }
            if distance_limit == ChordAngle::STRAIGHT {
                // Can't do better than π; no need to search edges.
                results.sort_unstable();
                results.dedup_by(|a, b| a.shape_id == b.shape_id && a.edge_id == b.edge_id);
                if results.len() > options.max_results as usize {
                    results.truncate(options.max_results as usize);
                }
                return results;
            }
        }

        // Always use brute force for furthest queries (the cell-based
        // optimization doesn't apply as directly for max-distance).
        self.find_furthest_edges_brute_force(target, options, &mut distance_limit, &mut results);

        // Sort (furthest first) and deduplicate.
        results.sort_unstable();
        results.dedup_by(|a, b| a.shape_id == b.shape_id && a.edge_id == b.edge_id);
        if results.len() > options.max_results as usize {
            results.truncate(options.max_results as usize);
        }
        results
    }

    /// Returns the distance from the target to the furthest edge.
    pub fn get_distance(&self, target: &dyn Target) -> ChordAngle {
        self.find_furthest_edge(target).distance
    }

    /// Returns true if the max distance to the target is greater than `limit`.
    pub fn is_distance_greater(&self, target: &dyn Target, limit: ChordAngle) -> bool {
        let opts = Options {
            max_results: 1,
            min_distance: limit,
            max_error: ChordAngle::STRAIGHT,
            ..Options::default()
        };
        let result = self.find_furthest_edge_with_options(target, &opts);
        !result.is_empty()
    }

    /// Returns true if the max distance to the target is greater than or
    /// equal to `limit`.
    pub fn is_distance_greater_or_equal(&self, target: &dyn Target, limit: ChordAngle) -> bool {
        let mut opts = Options {
            max_results: 1,
            max_error: ChordAngle::STRAIGHT,
            ..Options::default()
        };
        opts.inclusive_min_distance(limit);
        let result = self.find_furthest_edge_with_options(target, &opts);
        !result.is_empty()
    }

    /// Like [`is_distance_greater_or_equal`](Self::is_distance_greater_or_equal)
    /// but `limit` is decreased by the maximum error in distance computation,
    /// ensuring all truly-beyond-limit edges are found.
    pub fn is_conservative_distance_greater_or_equal(
        &self,
        target: &dyn Target,
        limit: ChordAngle,
    ) -> bool {
        let mut opts = Options {
            max_results: 1,
            max_error: ChordAngle::STRAIGHT,
            ..Options::default()
        };
        opts.conservative_min_distance(limit);
        let result = self.find_furthest_edge_with_options(target, &opts);
        !result.is_empty()
    }

    /// Returns the edge corresponding to the given result.
    pub fn get_edge(&self, result: &Result) -> Option<Edge> {
        if result.is_empty() || result.is_interior() {
            return None;
        }
        self.index
            .shape(result.shape_id)
            .map(|shape| shape.edge(result.edge_id as usize))
    }

    /// Brute force: check every edge in the index.
    fn find_furthest_edges_brute_force(
        &self,
        target: &dyn Target,
        options: &Options,
        distance_limit: &mut ChordAngle,
        results: &mut Vec<Result>,
    ) {
        for shape_id in (0..self.index.num_shape_ids() as i32).map(ShapeId) {
            let Some(shape) = self.index.shape(shape_id) else {
                continue;
            };
            for edge_id in 0..shape.num_edges() {
                let edge = shape.edge(edge_id);
                let (dist, updated) =
                    target.update_distance_to_edge(edge.v0, edge.v1, *distance_limit);
                if updated {
                    self.add_result(
                        Result {
                            distance: dist,
                            shape_id,
                            edge_id: edge_id as i32,
                        },
                        options,
                        distance_limit,
                        results,
                    );
                }
            }
        }
    }

    /// Adds a result, potentially updating the distance limit.
    ///
    /// For `max_results == 1`, keeps only the single furthest edge.
    /// For bounded results, maintains a sorted set and prunes the worst.
    #[expect(clippy::unused_self, reason = "matches C++ method signature")]
    fn add_result(
        &self,
        result: Result,
        options: &Options,
        distance_limit: &mut ChordAngle,
        results: &mut Vec<Result>,
    ) {
        if options.max_results == 1 {
            // Singleton strategy: the new result is always better (it passed
            // the distance_limit check) so just replace.
            if results.is_empty() {
                results.push(result);
            } else {
                results[0] = result;
            }
            let best_dist = results[0].distance;
            // For furthest queries with max_error, the limit *increases*
            // (we can skip edges that can't beat best - error).
            *distance_limit = if options.max_error > ChordAngle::ZERO {
                ChordAngle::from_length2(
                    (best_dist.length2() + options.max_error.length2())
                        .min(ChordAngle::STRAIGHT.length2()),
                )
            } else {
                best_dist
            };
        } else {
            results.push(result);
            if results.len() as i32 >= options.max_results {
                // Sort (furthest first) and truncate.
                results.sort_unstable();
                if results.len() > options.max_results as usize {
                    results.truncate(options.max_results as usize);
                }
                if let Some(last) = results.last() {
                    // Raise limit to worst result's distance.
                    *distance_limit = last.distance;
                }
            }
        }
    }
}

#[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::coords::Level;

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

    fn make_polyline_index(vertices: Vec<Point>) -> ShapeIndex {
        let mut index = ShapeIndex::new();
        index.add(Box::new(crate::s2::polyline::Polyline::new(vertices)));
        index.build();
        index
    }

    #[test]
    fn test_no_edges() {
        let index = ShapeIndex::new();
        let query = FurthestEdgeQuery::new(&index);
        let target = PointTarget::new(p(0.0, 0.0));
        let result = query.find_furthest_edge(&target);
        assert!(result.is_empty());
    }

    #[test]
    fn test_furthest_edge_simple() {
        // Polyline along the equator from 0 to 10 degrees.
        let index = make_polyline_index(vec![p(0.0, 0.0), p(0.0, 10.0)]);
        let query = FurthestEdgeQuery::new(&index);
        let target = PointTarget::new(p(0.0, 5.0));
        let result = query.find_furthest_edge(&target);

        assert!(!result.is_empty());
        // The furthest point on the edge from (0,5) is one of the endpoints
        // (0,0) or (0,10), both ~5 degrees away.
        let dist_degrees = result.distance.to_angle().degrees();
        assert!(
            (dist_degrees - 5.0).abs() < 0.5,
            "distance = {dist_degrees} degrees, expected ~5.0"
        );
    }

    #[test]
    fn test_furthest_from_antipodal() {
        // Polyline near the equator. Query from near the antipode.
        let index = make_polyline_index(vec![p(0.0, 0.0), p(0.0, 10.0)]);
        let query = FurthestEdgeQuery::new(&index);
        let target = PointTarget::new(p(0.0, -175.0));
        let result = query.find_furthest_edge(&target);

        assert!(!result.is_empty());
        // Distance should be close to 180 degrees.
        let dist_degrees = result.distance.to_angle().degrees();
        assert!(
            dist_degrees > 170.0,
            "distance = {dist_degrees} degrees, expected > 170"
        );
    }

    #[test]
    fn test_get_distance() {
        let index = make_polyline_index(vec![p(0.0, 0.0), p(0.0, 10.0)]);
        let query = FurthestEdgeQuery::new(&index);
        let target = PointTarget::new(p(0.0, 5.0));
        let dist = query.get_distance(&target);
        // Max distance ~5 degrees (to either endpoint).
        assert!(dist.to_angle().degrees() > 4.0);
        assert!(dist.to_angle().degrees() < 6.0);
    }

    #[test]
    fn test_is_distance_greater() {
        let index = make_polyline_index(vec![p(0.0, 0.0), p(0.0, 10.0)]);
        let query = FurthestEdgeQuery::new(&index);
        let target = PointTarget::new(p(0.0, 5.0));
        // Max distance is ~5 degrees.
        assert!(
            query.is_distance_greater(&target, ChordAngle::from_angle(Angle::from_degrees(3.0)))
        );
        assert!(
            !query.is_distance_greater(&target, ChordAngle::from_angle(Angle::from_degrees(10.0)))
        );
    }

    #[test]
    fn test_multiple_shapes() {
        let mut index = ShapeIndex::new();
        // Polyline near the equator.
        index.add(Box::new(crate::s2::polyline::Polyline::new(vec![
            p(0.0, 0.0),
            p(0.0, 5.0),
        ])));
        // Polyline far from the query point.
        index.add(Box::new(crate::s2::polyline::Polyline::new(vec![
            p(0.0, 170.0),
            p(0.0, 175.0),
        ])));
        index.build();

        let query = FurthestEdgeQuery::new(&index);
        let target = PointTarget::new(p(0.0, -5.0));
        let result = query.find_furthest_edge(&target);

        // The second polyline is furthest.
        assert!(!result.is_empty());
        assert_eq!(result.shape_id, 1);
    }

    #[test]
    fn test_edge_target() {
        let index = make_polyline_index(vec![p(0.0, 0.0), p(0.0, 10.0)]);
        let query = FurthestEdgeQuery::new(&index);
        let target = EdgeTarget::new(p(0.0, -5.0), p(0.0, -10.0));
        let result = query.find_furthest_edge(&target);

        assert!(!result.is_empty());
        // Furthest distance: from endpoint (0,-10) to (0,10) = 20 degrees.
        let dist_degrees = result.distance.to_angle().degrees();
        assert!(
            dist_degrees > 15.0,
            "distance = {dist_degrees} degrees, expected ~20"
        );
    }

    #[test]
    fn test_top_k_results() {
        let mut index = ShapeIndex::new();
        for i in 0..5 {
            let lng = f64::from(i) * 30.0;
            index.add(Box::new(crate::s2::polyline::Polyline::new(vec![
                p(0.0, lng),
                p(0.0, lng + 5.0),
            ])));
        }
        index.build();

        let query = FurthestEdgeQuery::new(&index);
        let target = PointTarget::new(p(0.0, -5.0));
        let mut opts = Options::default();
        opts.max_results = 3;
        let results = query.find_furthest_edges(&target, &opts);

        assert_eq!(results.len(), 3);
        // Results should be ordered furthest first.
        assert!(results[0].distance >= results[1].distance);
        assert!(results[1].distance >= results[2].distance);
    }

    #[test]
    fn test_is_distance_greater_or_equal() {
        let index = make_polyline_index(vec![p(0.0, 0.0), p(0.0, 10.0)]);
        let query = FurthestEdgeQuery::new(&index);
        let target = PointTarget::new(p(0.0, 180.0)); // antipode

        // Furthest distance is ~170-180 degrees. >= 160° → true.
        assert!(query.is_distance_greater_or_equal(&target, ChordAngle::from_degrees(160.0)));
        // >= INFINITY → false (nothing has distance INFINITY).
        assert!(!query.is_distance_greater_or_equal(&target, ChordAngle::INFINITY));
    }

    #[test]
    fn test_is_conservative_distance_greater_or_equal() {
        let index = make_polyline_index(vec![p(0.0, 0.0), p(0.0, 10.0)]);
        let query = FurthestEdgeQuery::new(&index);
        let target = PointTarget::new(p(0.0, 180.0));

        assert!(
            query.is_conservative_distance_greater_or_equal(
                &target,
                ChordAngle::from_degrees(160.0)
            )
        );
    }

    #[test]
    fn test_inclusive_min_distance() {
        let mut opts = Options::default();
        opts.inclusive_min_distance(ChordAngle::from_degrees(5.0));
        // inclusive = limit.predecessor()
        assert!(opts.min_distance < ChordAngle::from_degrees(5.0));
    }

    #[test]
    fn test_conservative_min_distance() {
        let mut opts = Options::default();
        opts.conservative_min_distance(ChordAngle::from_degrees(5.0));
        // Conservative should be <= inclusive.
        let mut inclusive = Options::default();
        inclusive.inclusive_min_distance(ChordAngle::from_degrees(5.0));
        assert!(opts.min_distance <= inclusive.min_distance);
    }

    #[test]
    fn test_shape_index_target() {
        // Query index: polyline along the equator.
        let mut query_index = ShapeIndex::new();
        query_index.add(Box::new(crate::s2::polyline::Polyline::new(vec![
            p(0.0, 0.0),
            p(0.0, 10.0),
        ])));
        query_index.build();

        // Target index: polyline near the north pole.
        let mut target_index = ShapeIndex::new();
        target_index.add(Box::new(crate::s2::polyline::Polyline::new(vec![
            p(89.0, 0.0),
            p(89.0, 10.0),
        ])));
        target_index.build();

        let query = FurthestEdgeQuery::new(&query_index);
        let target = ShapeIndexTarget::new(&target_index);
        let result = query.find_furthest_edge(&target);

        assert!(!result.is_empty());
        let dist_degrees = result.distance.to_angle().degrees();
        assert!(
            dist_degrees > 85.0,
            "distance = {dist_degrees} degrees, expected >85"
        );
    }

    #[test]
    fn test_set_max_error_on_target() {
        let mut target = PointTarget::new(p(1.0, 2.0));
        assert!(!target.set_max_error(ChordAngle::from_degrees(1.0)));
    }

    #[test]
    fn test_include_interiors_antipodal() {
        // A polygon containing the antipode of the target should produce
        // an interior result at distance STRAIGHT (π).
        use crate::s2::lax_polygon::LaxPolygon;
        use crate::s2::shape_index::ShapeIndex;

        // Create a large polygon covering most of the sphere.
        let mut index = ShapeIndex::new();
        // A full polygon contains everything, including the antipode.
        index.add(Box::new(LaxPolygon::full()));
        index.build();

        let query = FurthestEdgeQuery::new(&index);
        let target = PointTarget::new(p(0.0, 0.0));
        let opts = Options {
            max_results: i32::MAX,
            include_interiors: true,
            ..Options::default()
        };
        let results = query.find_furthest_edges(&target, &opts);

        // Should find the interior at distance STRAIGHT.
        let interior_results: Vec<_> = results.iter().filter(|r| r.is_interior()).collect();
        assert!(
            !interior_results.is_empty(),
            "should find interior result for full polygon"
        );
        assert_eq!(interior_results[0].distance, ChordAngle::STRAIGHT);
    }

    #[test]
    fn test_include_interiors_false() {
        // With include_interiors=false, no interior results should appear.
        use crate::s2::lax_polygon::LaxPolygon;
        use crate::s2::shape_index::ShapeIndex;

        let mut index = ShapeIndex::new();
        index.add(Box::new(LaxPolygon::full()));
        index.build();

        let query = FurthestEdgeQuery::new(&index);
        let target = PointTarget::new(p(0.0, 0.0));
        let opts = Options {
            max_results: i32::MAX,
            include_interiors: false,
            ..Options::default()
        };
        let results = query.find_furthest_edges(&target, &opts);
        assert!(
            results.iter().all(|r| !r.is_interior()),
            "no interior results with include_interiors=false"
        );
    }

    // ═══════════════════════════════════════════════════════════════════
    // Randomized brute-force correctness tests — ported from C++
    // s2furthest_edge_query_test.cc (CircleEdges / FractalEdges /
    // PointCloudEdges).
    // ═══════════════════════════════════════════════════════════════════

    use crate::s1::Angle;
    use crate::s2::cap::Cap;
    use crate::s2::cell::Cell;
    use crate::s2::cell_id::CellId;
    use crate::s2::coords::MAX_CELL_LEVEL;
    use crate::s2::fractal::S2Fractal;
    use crate::s2::metric;
    use crate::s2::point_vector::PointVector;
    use crate::s2::testing::{frame_at, random_point, sample_point_from_cap};
    use rand::Rng;
    use rand::SeedableRng;
    use rand::rngs::StdRng;

    trait ShapeIndexFactory {
        fn add_edges(&self, cap: &Cap, num_edges: usize, index: &mut ShapeIndex, rng: &mut StdRng);
    }

    struct RegularLoopFactory;
    impl ShapeIndexFactory for RegularLoopFactory {
        fn add_edges(
            &self,
            cap: &Cap,
            num_edges: usize,
            index: &mut ShapeIndex,
            _rng: &mut StdRng,
        ) {
            index.add(Box::new(crate::s2::Loop::make_regular(
                cap.center(),
                cap.angle_radius(),
                num_edges,
            )));
        }
    }

    struct FractalLoopFactory;
    impl ShapeIndexFactory for FractalLoopFactory {
        fn add_edges(&self, cap: &Cap, num_edges: usize, index: &mut ShapeIndex, rng: &mut StdRng) {
            let seed = rng.r#gen::<u64>();
            let mut fractal = S2Fractal::new(seed);
            fractal.level_for_approx_max_edges(num_edges as i32);
            let frame = frame_at(rng, cap.center());
            let frame_mat =
                crate::r3::matrix::Matrix3x3::from_cols(frame.0.0, frame.1.0, frame.2.0);
            let loop_ = fractal.make_loop(&frame_mat, cap.angle_radius());
            index.add(Box::new(loop_));
        }
    }

    struct PointCloudFactory;
    impl ShapeIndexFactory for PointCloudFactory {
        fn add_edges(&self, cap: &Cap, num_edges: usize, index: &mut ShapeIndex, rng: &mut StdRng) {
            let mut points = Vec::with_capacity(num_edges);
            for _ in 0..num_edges {
                points.push(sample_point_from_cap(rng, cap));
            }
            index.add(Box::new(PointVector::new(points)));
        }
    }

    fn count_edges(index: &ShapeIndex) -> usize {
        let mut total = 0;
        for id in 0..index.num_shape_ids() {
            if let Some(shape) = index.shape(id as i32) {
                total += shape.num_edges();
            }
        }
        total
    }

    fn log_uniform(rng: &mut StdRng, lo: f64, hi: f64) -> f64 {
        let v: f64 = rng.gen_range(lo.log2()..hi.log2());
        v.exp2()
    }

    /// Verify that furthest edge query results satisfy the search criteria.
    fn get_furthest_edges(
        target: &dyn Target,
        query: &FurthestEdgeQuery<'_>,
        options: &Options,
    ) -> Vec<Result> {
        let results = query.find_furthest_edges(target, options);
        assert!(
            results.len() <= options.max_results as usize,
            "too many results: {} > {}",
            results.len(),
            options.max_results
        );
        if options.min_distance == ChordAngle::NEGATIVE {
            let min_expected = (options.max_results as usize).min(count_edges(query.index));
            if options.include_interiors {
                assert!(results.len() >= min_expected);
            } else {
                assert_eq!(
                    results.len(),
                    min_expected,
                    "expected {min_expected} results"
                );
            }
        }
        for r in &results {
            assert!(
                r.distance >= options.min_distance,
                "result distance {:?} < min_distance {:?}",
                r.distance,
                options.min_distance
            );
        }
        results
    }

    /// Compare brute-force vs default (also brute-force for furthest queries,
    /// but exercises the full code path including dedup and sort).
    fn test_find_furthest_edges(
        target: &dyn Target,
        query: &FurthestEdgeQuery<'_>,
        options: &Options,
    ) {
        let mut bf_opts = options.clone();
        bf_opts.use_brute_force = true;
        let expected = get_furthest_edges(target, query, &bf_opts);

        let mut opt_opts = options.clone();
        opt_opts.use_brute_force = false;
        let actual = get_furthest_edges(target, query, &opt_opts);

        // Check that expected and actual agree on the maximum distance.
        // (Full CheckDistanceResults is overkill since both paths are brute-force.)
        if !expected.is_empty() && !actual.is_empty() {
            let diff = (expected[0].distance.length2() - actual[0].distance.length2()).abs();
            assert!(
                diff < 1e-12,
                "furthest distance mismatch: expected {:?}, actual {:?}",
                expected[0].distance,
                actual[0].distance,
            );
        }

        if expected.is_empty() {
            return;
        }

        // Verify GetDistance and IsDistanceGreater consistency.
        // Use options that match the original search (especially
        // include_interiors) to avoid spurious interior results.
        let max_error = options.max_error;
        let expected_distance = expected[0].distance;

        let get_dist_opts = Options {
            max_results: 1,
            include_interiors: options.include_interiors,
            ..Options::default()
        };
        let got = query
            .find_furthest_edge_with_options(target, &get_dist_opts)
            .distance;
        assert!(
            got >= expected_distance - max_error,
            "GetDistance {got:?} < expected {expected_distance:?} - error {max_error:?}",
        );

        let check_opts = Options {
            max_results: 1,
            min_distance: expected_distance + max_error,
            max_error: ChordAngle::STRAIGHT,
            include_interiors: options.include_interiors,
            ..Options::default()
        };
        let check_result = query.find_furthest_edge_with_options(target, &check_opts);
        assert!(
            check_result.is_empty(),
            "IsDistanceGreater should be false above max + error"
        );
    }

    fn test_cap_radius() -> Angle {
        Angle::from_radians(10.0 / 6371.01)
    }

    fn test_with_furthest_index_factory(
        factory: &dyn ShapeIndexFactory,
        num_indexes: usize,
        num_edges: usize,
        num_queries: usize,
        seed: u64,
    ) {
        let mut rng = StdRng::seed_from_u64(seed);

        let mut index_caps = Vec::with_capacity(num_indexes);
        let mut indexes = Vec::with_capacity(num_indexes);
        for _ in 0..num_indexes {
            let center = random_point(&mut rng);
            let cap = Cap::from_center_angle(center, test_cap_radius());
            let mut index = ShapeIndex::new();
            factory.add_edges(&cap, num_edges, &mut index, &mut rng);
            index.build();
            index_caps.push(cap);
            indexes.push(index);
        }

        for _ in 0..num_queries {
            let i_index = rng.gen_range(0..num_indexes);
            let index_cap = &index_caps[i_index];
            let query_radius = 2.0 * index_cap.angle_radius().radians();

            // Exercise the opposite-hemisphere code 1/5 of the time.
            let antipodal: f64 = if rng.gen_range(0..5) == 0 { -1.0 } else { 1.0 };
            let query_center = Point(index_cap.center().0 * antipodal);
            let query_cap = Cap::from_center_angle(query_center, Angle::from_radians(query_radius));

            let mut opts = Options::default();
            if rng.gen_range(0..5) != 0 {
                opts.max_results = rng.gen_range(1..11);
            }
            if rng.gen_range(0..3) != 0 {
                let frac: f64 = rng.gen_range(0.0..1.0);
                opts.min_distance =
                    ChordAngle::from_angle(Angle::from_radians(frac * query_radius));
            }
            if rng.gen_range(0..2) == 0 {
                let e = log_uniform(&mut rng, 1e-4, 1.0) * query_radius;
                opts.max_error = ChordAngle::from_angle(Angle::from_radians(e));
            }
            opts.include_interiors = rng.gen_range(0..2) == 0;

            let query = FurthestEdgeQuery::new(&indexes[i_index]);
            let target_type = rng.gen_range(0..4);

            match target_type {
                0 => {
                    let point = sample_point_from_cap(&mut rng, &query_cap);
                    let target = PointTarget::new(point);
                    test_find_furthest_edges(&target, &query, &opts);
                }
                1 => {
                    let a = sample_point_from_cap(&mut rng, &query_cap);
                    let edge_radius = log_uniform(&mut rng, 1e-4, 1.0) * query_radius;
                    let b_cap = Cap::from_center_angle(a, Angle::from_radians(edge_radius));
                    let b = sample_point_from_cap(&mut rng, &b_cap);
                    let target = EdgeTarget::new(a, b);
                    test_find_furthest_edges(&target, &query, &opts);
                }
                2 => {
                    let min_level = metric::MAX_DIAG.max_level(query_radius);
                    let level = Level::new(rng.gen_range(min_level.as_u8()..=MAX_CELL_LEVEL));
                    let a = sample_point_from_cap(&mut rng, &query_cap);
                    let cell = Cell::from(CellId::from_point(&a).parent_at_level(level));
                    let target = CellTarget::new(cell);
                    test_find_furthest_edges(&target, &query, &opts);
                }
                3 => {
                    let j_index = rng.gen_range(0..num_indexes);
                    let target = ShapeIndexTarget::new(&indexes[j_index]);
                    test_find_furthest_edges(&target, &query, &opts);
                }
                _ => unreachable!(),
            }
        }
    }

    const FURTHEST_NUM_INDEXES: usize = 50;
    const FURTHEST_NUM_EDGES: usize = 100;
    const FURTHEST_NUM_QUERIES: usize = 200;

    #[test]
    fn test_furthest_circle_edges() {
        test_with_furthest_index_factory(
            &RegularLoopFactory,
            FURTHEST_NUM_INDEXES,
            FURTHEST_NUM_EDGES,
            FURTHEST_NUM_QUERIES,
            0xfeed_c1c1,
        );
    }

    #[test]
    fn test_furthest_fractal_edges() {
        test_with_furthest_index_factory(
            &FractalLoopFactory,
            FURTHEST_NUM_INDEXES,
            FURTHEST_NUM_EDGES,
            FURTHEST_NUM_QUERIES,
            0xfeed_f4ac,
        );
    }

    #[test]
    fn test_furthest_point_cloud_edges() {
        test_with_furthest_index_factory(
            &PointCloudFactory,
            FURTHEST_NUM_INDEXES,
            FURTHEST_NUM_EDGES,
            FURTHEST_NUM_QUERIES,
            0xfeed_9c1d,
        );
    }

    #[cfg(feature = "serde")]
    #[test]
    fn test_serde_result_roundtrip() {
        let r = Result {
            distance: ChordAngle::from_degrees(120.0),
            shape_id: ShapeId(1),
            edge_id: 3,
        };
        let json = serde_json::to_string(&r).unwrap();
        let back: Result = serde_json::from_str(&json).unwrap();
        assert_eq!(r.distance, back.distance);
        assert_eq!(r.shape_id, back.shape_id);
        assert_eq!(r.edge_id, back.edge_id);
    }
}