boost_geometry 0.0.8

Rust port of Boost.Geometry — same design philosophy (concepts, tags, strategy-based dispatch), works with your own point/geometry types, re-exported as a single API surface.
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
//! Public-facade parity tests for Boost.Geometry policy headers.

use core::cmp::Ordering;

use boost_geometry::coords::Rational;
use boost_geometry::model::{Point as ModelPoint, Point2D, Polygon, Ring};
use boost_geometry::prelude::{
    Cartesian, Degree, Geographic, Radian, Spherical, ValidityFailure, ValidityOptions, is_valid,
    is_valid_with, validity_reason, validity_reason_with,
};
use boost_geometry::strategy::compare::{EqualTo, Greater, Less, LessExact};
use boost_geometry::trait_::PointMut as _;

type CartesianPoint = Point2D<f64, Cartesian>;
const LESS: Less = Less;
const LESS_EXACT: LessExact = LessExact;
const GREATER: Greater = Greater;
const EQUAL_TO: EqualTo = EqualTo;

/// `test/policies/compare.cpp:48-132` — the default policy compares all
/// coordinates lexicographically; dimension policies inspect one ordinate.
#[test]
fn cartesian_compare_matches_the_reference_matrix() {
    let p1 = CartesianPoint::new(3.0, 1.0);
    let p2 = CartesianPoint::new(3.0, 1.0);
    let p3 = CartesianPoint::new(1.0, 3.0);
    let p4 = CartesianPoint::new(5.0, 2.0);
    let p5 = CartesianPoint::new(3.0, 2.0);

    assert!(EQUAL_TO.apply(&p1, &p2));
    assert!(!EQUAL_TO.apply(&p1, &p3));
    assert!(LESS.apply(&p1, &p4));
    assert!(LESS.apply(&p1, &p5));
    assert!(LESS.apply(&p3, &p4));
    assert!(GREATER.apply(&p1, &p3));

    assert!(EqualTo::<0>.apply(&p1, &p5));
    assert!(!Less::<0>.apply(&p1, &p5));
    assert!(Greater::<0>.apply(&p1, &p3));

    assert!(!EqualTo::<1>.apply(&p1, &p5));
    assert!(Less::<1>.apply(&p1, &p3));
    assert!(Less::<1>.apply(&p1, &p5));
    assert!(Greater::<1>.apply(&p3, &p4));
}

/// `test/policies/compare.cpp:135-201` — the policies are suitable for
/// ascending, descending, and single-dimension sorting.
#[test]
fn cartesian_compare_sorts_like_the_reference_policy() {
    let mut points = [
        CartesianPoint::new(3.0, 1.0),
        CartesianPoint::new(2.0, 3.0),
        CartesianPoint::new(2.0, 2.0),
        CartesianPoint::new(1.0, 3.0),
    ];

    points.sort_by(|left, right| {
        if LESS.apply(left, right) {
            Ordering::Less
        } else if GREATER.apply(left, right) {
            Ordering::Greater
        } else {
            Ordering::Equal
        }
    });
    assert_eq!(
        points,
        [
            CartesianPoint::new(1.0, 3.0),
            CartesianPoint::new(2.0, 2.0),
            CartesianPoint::new(2.0, 3.0),
            CartesianPoint::new(3.0, 1.0),
        ]
    );

    points.sort_by(|left, right| {
        if GREATER.apply(left, right) {
            Ordering::Less
        } else if LESS.apply(left, right) {
            Ordering::Greater
        } else {
            Ordering::Equal
        }
    });
    assert_eq!(points[0], CartesianPoint::new(3.0, 1.0));

    points.sort_by(|left, right| {
        if Less::<1>.apply(left, right) {
            Ordering::Less
        } else if Greater::<1>.apply(left, right) {
            Ordering::Greater
        } else {
            Ordering::Equal
        }
    });
    assert_eq!(points[0], CartesianPoint::new(3.0, 1.0));
}

/// `policies/compare.hpp:35-73` — ordinary comparison treats values within
/// Boost's epsilon as equal while `less_exact` does not.
#[test]
fn exact_and_epsilon_less_policies_are_distinct() {
    let left = CartesianPoint::new(1.0, 0.0);
    let right = CartesianPoint::new(1.0 + f64::EPSILON, 0.0);

    assert!(EQUAL_TO.apply(&left, &right));
    assert!(!LESS.apply(&left, &right));
    assert!(LESS_EXACT.apply(&left, &right));
}

/// `test/policies/compare.cpp:241-250` — integer and floating coordinate
/// models use the same public policy, including mixed-scalar comparisons.
#[test]
fn cartesian_compare_accepts_integer_and_mixed_scalars() {
    let integer = Point2D::<i32, Cartesian>::new(3, 1);
    let wider_integer = Point2D::<i64, Cartesian>::new(3, 2);
    let floating = CartesianPoint::new(4.0, 0.0);

    assert!(LESS.apply(&integer, &wider_integer));
    assert!(LESS.apply(&integer, &floating));
    assert!(GREATER.apply(&floating, &wider_integer));
}

/// `test/policies/compare.cpp:204-238` and
/// `strategies/spherical/compare.hpp:96-164` — the antimeridian sorts after
/// ordinary longitudes, its two spellings compare equal on longitude, and
/// degree/radian inputs compare in a shared unit.
#[test]
fn spherical_and_geographic_compare_handle_angular_coordinates() {
    type SphericalPoint = Point2D<f64, Spherical<Degree>>;
    let mut points = [
        SphericalPoint::new(180.0, 70.56),
        SphericalPoint::new(179.73, 71.56),
        SphericalPoint::new(177.47, 71.23),
        SphericalPoint::new(-178.78, 72.78),
        SphericalPoint::new(-180.0, 73.12),
    ];
    points.sort_by(|left, right| {
        if LESS.apply(left, right) {
            Ordering::Less
        } else if GREATER.apply(left, right) {
            Ordering::Greater
        } else {
            Ordering::Equal
        }
    });
    assert_eq!((points[0].x(), points[0].y()), (-178.78, 72.78));
    assert_eq!((points[3].x(), points[3].y()), (180.0, 70.56));
    assert_eq!((points[4].x(), points[4].y()), (-180.0, 73.12));
    assert!(EqualTo::<0>.apply(
        &SphericalPoint::new(180.0, 0.0),
        &SphericalPoint::new(-180.0, 10.0),
    ));

    let degrees = Point2D::<f64, Geographic<Degree>>::new(180.0, 45.0);
    let radians = Point2D::<f64, Geographic<Radian>>::new(
        core::f64::consts::PI,
        core::f64::consts::FRAC_PI_4,
    );
    assert!(EQUAL_TO.apply(&degrees, &radians));
}

/// The public angular policies cover explicit latitude and higher-dimension
/// selection, pole equivalence, both antimeridian orderings, and every scalar
/// conversion supported by angular coordinate systems.
#[test]
fn angular_compare_covers_dimensions_and_scalar_conversions() {
    type SphericalPoint = Point2D<f64, Spherical<Degree>>;
    type SphericalPoint4 = ModelPoint<f64, 4, Spherical<Degree>>;

    let ordinary = SphericalPoint::new(20.0, 10.0);
    let antimeridian = SphericalPoint::new(180.0, 10.0);
    assert!(LESS.apply(&ordinary, &antimeridian));
    assert!(GREATER.apply(&antimeridian, &ordinary));
    assert!(LESS_EXACT.apply(&ordinary, &antimeridian));

    assert!(EqualTo::<0>.apply(
        &SphericalPoint::new(-30.0, 90.0),
        &SphericalPoint::new(70.0, 90.0),
    ));
    assert!(EqualTo::<0>.apply(
        &SphericalPoint::new(10.0, 0.0),
        &SphericalPoint::new(10.0, 20.0),
    ));
    assert!(Less::<1>.apply(
        &SphericalPoint::new(0.0, -10.0),
        &SphericalPoint::new(0.0, 20.0),
    ));
    assert!(Greater::<1>.apply(
        &SphericalPoint::new(0.0, 20.0),
        &SphericalPoint::new(0.0, -10.0),
    ));
    assert!(EqualTo::<1>.apply(
        &SphericalPoint::new(0.0, 20.0),
        &SphericalPoint::new(10.0, 20.0),
    ));

    let point4 = |longitude, latitude, z, m| {
        let mut point = SphericalPoint4::default();
        point.set::<0>(longitude);
        point.set::<1>(latitude);
        point.set::<2>(z);
        point.set::<3>(m);
        point
    };
    let lower = point4(10.0, 20.0, 30.0, 40.0);
    let higher_z = point4(10.0, 20.0, 31.0, 40.0);
    let higher_m = point4(10.0, 20.0, 30.0, 41.0);
    assert!(LESS.apply(&lower, &higher_z));
    assert!(EQUAL_TO.apply(&lower, &lower));
    assert!(Less::<2>.apply(&lower, &higher_z));
    assert!(Less::<3>.apply(&lower, &higher_m));
    assert!(EqualTo::<3>.apply(&lower, &higher_z));

    let integer = Point2D::<i32, Geographic<Degree>>::new(0, 0);
    let floating = Point2D::<f64, Geographic<Degree>>::new(1.0, 0.0);
    assert!(LESS.apply(&integer, &floating));
    assert!(GREATER.apply(&floating, &integer));

    let single = Point2D::<f32, Spherical<Degree>>::new(1.0, 2.0);
    let double = Point2D::<f64, Spherical<Degree>>::new(2.0, 2.0);
    assert!(LESS.apply(&single, &double));

    let rational = Point2D::<Rational<i64>, Spherical<Degree>>::new(
        Rational::from_integer(1),
        Rational::from_integer(2),
    );
    let rational_higher = Point2D::<Rational<i64>, Spherical<Degree>>::new(
        Rational::from_integer(2),
        Rational::from_integer(2),
    );
    assert!(LESS.apply(&rational, &rational_higher));
}

/// `test/policies/compare.cpp:241-250` exercises scalar-independent policy
/// dispatch. The Rust public policy additionally accepts every pair in its
/// integer, floating, and exact-rational comparison lattice.
#[test]
fn cartesian_compare_covers_the_public_scalar_lattice() {
    macro_rules! assert_less {
        ($left:expr, $right:expr) => {{
            let left = Point2D::<_, Cartesian>::new($left, $left);
            let right = Point2D::<_, Cartesian>::new($right, $right);
            assert!(LESS.apply(&left, &right));
        }};
    }

    assert_less!(1_i32, 2_i32);
    assert_less!(1_i64, 2_i64);
    assert_less!(1_i64, 2_i32);
    assert_less!(1_f32, 2_f64);
    assert_less!(1_f64, 2_f32);
    assert_less!(1_i32, 2_f32);
    assert_less!(1_f32, 2_i32);
    assert_less!(1_i32, 2_f64);
    assert_less!(1_f64, 2_i32);
    assert_less!(1_i64, 2_f32);
    assert_less!(1_f32, 2_i64);
    assert_less!(1_i64, 2_f64);
    assert_less!(1_f64, 2_i64);

    let q32_one = Rational::<i32>::from_integer(1);
    let q32_two = Rational::<i32>::from_integer(2);
    let q64_one = Rational::<i64>::from_integer(1);
    let q64_two = Rational::<i64>::from_integer(2);
    assert_less!(q32_one, q64_two);
    assert_less!(q64_one, q32_two);
    assert_less!(q32_one, 2_i32);
    assert_less!(1_i32, q32_two);
    assert_less!(q64_one, 2_i64);
    assert_less!(1_i64, q64_two);
    assert_less!(q32_one, 2_f32);
    assert_less!(1_f32, q32_two);
    assert_less!(q64_one, 2_f64);
    assert_less!(1_f64, q64_two);
}

fn duplicate_polygon() -> Polygon<CartesianPoint> {
    Polygon::new(Ring::from_vec(vec![
        CartesianPoint::new(0.0, 0.0),
        CartesianPoint::new(0.0, 2.0),
        CartesianPoint::new(0.0, 2.0),
        CartesianPoint::new(2.0, 2.0),
        CartesianPoint::new(2.0, 0.0),
        CartesianPoint::new(0.0, 0.0),
    ]))
}

/// `test/algorithms/is_valid_failure.cpp` and
/// `policies/is_valid/failing_reason_policy.hpp:32-63` — every public result
/// has the stable base reason used by Boost's reason policy.
#[test]
fn validity_failures_expose_reference_reason_messages() {
    let reference_reasons = [
        (ValidityFailure::FewPoints, "Geometry has too few points"),
        (
            ValidityFailure::WrongTopologicalDimension,
            "Geometry has wrong topological dimension",
        ),
        (ValidityFailure::Spikes, "Geometry has spikes"),
        (
            ValidityFailure::DuplicatePoints,
            "Geometry has duplicate (consecutive) points",
        ),
        (
            ValidityFailure::NotClosed,
            "Geometry is defined as closed but is open",
        ),
        (
            ValidityFailure::SelfIntersection,
            "Geometry has invalid self-intersections",
        ),
        (
            ValidityFailure::WrongOrientation,
            "Geometry has wrong orientation",
        ),
        (
            ValidityFailure::InteriorRingOutside,
            "Geometry has interior rings defined outside the outer boundary",
        ),
        (
            ValidityFailure::NestedInteriorRings,
            "Geometry has nested interior rings",
        ),
        (
            ValidityFailure::DisconnectedInterior,
            "Geometry has disconnected interior",
        ),
        (
            ValidityFailure::IntersectingInteriors,
            "Multi-polygon has intersecting interiors",
        ),
        (
            ValidityFailure::WrongCornerOrder,
            "Box has corners in wrong order",
        ),
        (
            ValidityFailure::InvalidCoordinate,
            "Geometry has point(s) with invalid coordinate(s)",
        ),
        (
            ValidityFailure::CoordinateOutOfRange,
            "Geometry has coordinate(s) outside the supported arithmetic range",
        ),
        (
            ValidityFailure::CollinearPointsOnFace,
            "Geometry has collinear points on a face",
        ),
        (
            ValidityFailure::NonCoplanarPointsOnFace,
            "Geometry has non-coplanar points on a face",
        ),
        (
            ValidityFailure::FewPointsOnFace,
            "Geometry has too few points on a face",
        ),
        (
            ValidityFailure::InconsistentOrientation,
            "Geometry has inconsistent surface orientation",
        ),
        (
            ValidityFailure::InvalidIntersection,
            "Geometry has invalid face intersections",
        ),
        (
            ValidityFailure::DisconnectedSurface,
            "Geometry has a disconnected surface",
        ),
    ];
    for (failure, reason) in reference_reasons {
        assert_eq!(failure.message(), reason);
        assert_eq!(failure.to_string(), reason);
    }

    let valid: Polygon<CartesianPoint> = Polygon::new(Ring::from_vec(vec![
        CartesianPoint::new(0.0, 0.0),
        CartesianPoint::new(0.0, 2.0),
        CartesianPoint::new(2.0, 2.0),
        CartesianPoint::new(2.0, 0.0),
        CartesianPoint::new(0.0, 0.0),
    ]));
    assert_eq!(validity_reason(&valid), "Geometry is valid");
    assert_eq!(
        validity_reason(&duplicate_polygon()),
        "Geometry has duplicate (consecutive) points"
    );
}

/// `policies/is_valid/default_policy.hpp:26-61` — Boost's default allows
/// consecutive duplicates. The existing strict Rust default stays intact,
/// and the Boost behavior is selected explicitly through the public facade.
#[test]
fn validity_options_preserve_strict_behavior_and_offer_boost_defaults() {
    let custom = ValidityOptions::new(true, false);
    assert!(custom.allows_duplicates());
    assert!(!custom.allows_spikes_for_linear());
    assert_eq!(ValidityOptions::default(), ValidityOptions::STRICT);

    let duplicate = duplicate_polygon();
    assert_eq!(is_valid(&duplicate), Err(ValidityFailure::DuplicatePoints));
    assert!(is_valid_with(&duplicate, ValidityOptions::BOOST_DEFAULT).is_ok());
    assert_eq!(
        validity_reason_with(&duplicate, ValidityOptions::BOOST_DEFAULT),
        "Geometry is valid"
    );
}