mod area;
pub(crate) mod basic;
#[cfg(test)]
mod basic_tests;
mod concave_hull;
#[cfg(test)]
mod concave_hull_tests;
pub mod coverage;
pub mod coverage_simplify;
#[cfg(test)]
mod coverage_simplify_tests;
mod delaunay;
#[cfg(test)]
mod delaunay_tests;
pub mod distance;
pub mod frechet;
#[cfg(test)]
mod frechet_tests;
pub mod hausdorff;
#[cfg(test)]
mod hausdorff_tests;
mod hull;
mod length;
mod linework;
mod measure;
pub mod pinch;
mod shared;
mod simple;
pub mod simplify;
pub mod simplify_topology;
pub mod simplify_vw;
#[cfg(test)]
mod simplify_vw_tests;
pub mod validate;
pub use area::{area, signed_area, signed_area_ring};
pub use basic::{Reversible, boundary, normalize, reverse, unique_points};
pub use concave_hull::{ConcaveHullParameter, concave_hull};
pub use coverage::{
CoverageIssue, Polygonized, coverage_union, coverage_validate, coverage_validate_with_gap,
polygonize_noded,
};
pub use coverage_simplify::{coverage_simplify, coverage_simplify_inner};
pub use delaunay::{Triangulation, delaunay};
pub use distance::{distance, dwithin};
pub use frechet::{frechet_distance, frechet_within};
pub use hausdorff::{hausdorff_distance, hausdorff_within};
pub use hull::convex_hull;
pub use linework::{line_merge, line_merge_directed, line_sequence, split_by_point};
pub use shared::{SharedPaths, shared_paths};
#[cfg(feature = "3d")]
pub(crate) use area::certified_twice_area_edges;
pub use length::{edge_length, length, perimeter, ring_perimeter};
pub use measure::{collection_area, collection_length, polygon_length};
pub use pinch::split_pinches;
pub use simple::is_simple;
pub use simplify_topology::{
TopologySimplifiable, simplify_preserve_topology, simplify_preserve_topology_unvouched,
};
pub use simplify_vw::{Simplifiable, simplify_vw};
pub use validate::{
LocatedIssue, ValidityIssue, is_valid, validate, validate_line, validate_multipolygon,
validity_issues,
};
#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(
clippy::float_cmp,
clippy::cast_possible_truncation,
clippy::cast_precision_loss,
clippy::cast_possible_wrap,
clippy::range_plus_one,
clippy::no_effect_underscore_binding
)]
mod tests {
use super::*;
use crate::GeometryError;
use crate::types::{Coord2d, CoordSeq, LineString, LinearRing, Polygon};
fn seq(pts: &[(f64, f64)]) -> CoordSeq {
CoordSeq::new(pts.iter().map(|&(x, y)| Coord2d::new(x, y)).collect())
}
fn ring(pts: &[(f64, f64)]) -> LinearRing {
LinearRing::new(seq(pts)).unwrap()
}
fn poly(pts: &[(f64, f64)]) -> Polygon {
Polygon::new_simple(ring(pts)).unwrap()
}
const UNIT_CCW: [(f64, f64); 4] = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)];
const UNIT_CW: [(f64, f64); 4] = [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)];
#[test]
fn signed_area_of_a_ccw_unit_square_is_one() {
assert_eq!(signed_area(&poly(&UNIT_CCW)), 1.0);
assert_eq!(signed_area_ring(&ring(&UNIT_CCW)), 1.0);
}
#[test]
fn signed_area_of_a_cw_unit_square_is_minus_one() {
assert_eq!(signed_area_ring(&ring(&UNIT_CW)), -1.0);
}
#[test]
fn area_is_one_regardless_of_winding() {
assert_eq!(area(&poly(&UNIT_CCW)), 1.0);
}
#[test]
fn length_of_a_unit_segment_is_one() {
let l = LineString::new(seq(&[(0.0, 0.0), (1.0, 0.0)])).unwrap();
assert_eq!(length(&l), 1.0);
}
#[test]
fn length_and_perimeter_of_known_shapes() {
let l = LineString::new(seq(&[(0.0, 0.0), (3.0, 4.0), (3.0, 10.0)])).unwrap();
assert_eq!(length(&l), 11.0);
assert_eq!(perimeter(&poly(&UNIT_CCW)), 4.0);
assert_eq!(ring_perimeter(&ring(&UNIT_CW)), 4.0);
}
#[test]
fn a_hole_that_exceeds_the_exterior_is_rejected_at_construction() {
let big_hole = ring(&[(-5.0, -5.0), (-5.0, 5.0), (5.0, 5.0), (5.0, -5.0)]);
assert_eq!(
Polygon::new(ring(&UNIT_CCW), vec![big_hole]).unwrap_err(),
GeometryError::HoleNotInsideExterior { hole_index: 0 }
);
}
#[test]
fn area_far_from_the_origin_is_exact_where_the_plain_sum_is_not() {
let o = 1e8;
let pts = [(o, o), (o + 1.0, o), (o + 1.0, o + 1.0), (o, o + 1.0)];
let r = ring(&pts);
assert_eq!(signed_area_ring(&r), 1.0);
let mut plain = 0.0_f64;
for (a, b) in r.edges() {
plain += a.x * b.y;
plain -= b.x * a.y;
}
assert_ne!(
plain * 0.5,
1.0,
"the plain shoelace is expected to lose this"
);
assert_eq!(r.signed_area(), 1.0, "LinearRing::signed_area is exact");
}
}