use std::ops::Index;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Coord3d {
pub x: f64,
pub y: f64,
pub z: f64,
}
impl Coord3d {
#[inline]
pub fn new(x: f64, y: f64, z: f64) -> Self {
Self { x, y, z }
}
#[inline]
pub(crate) fn to_2d(self) -> crate::types::Coord2d {
crate::types::Coord2d::new(self.x, self.y)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct CoordSeq3d(Vec<Coord3d>);
impl CoordSeq3d {
pub fn new(coords: Vec<Coord3d>) -> Self {
Self(coords)
}
#[inline]
pub fn len(&self) -> usize {
self.0.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[inline]
pub fn as_slice(&self) -> &[Coord3d] {
&self.0
}
#[inline]
pub fn iter(&self) -> impl Iterator<Item = &Coord3d> {
self.0.iter()
}
}
impl Index<usize> for CoordSeq3d {
type Output = Coord3d;
#[inline]
fn index(&self, i: usize) -> &Self::Output {
&self.0[i]
}
}
pub(super) fn check_coords3d(coords: &CoordSeq3d) -> Result<(), crate::GeometryError> {
for (i, c) in coords.iter().enumerate() {
for (axis, v) in [(0_u8, c.x), (1, c.y), (2, c.z)] {
if !v.is_finite() {
return Err(crate::GeometryError::NonFiniteCoordinate {
coord_index: i,
axis,
});
}
}
}
for i in 0..coords.len().saturating_sub(1) {
if coords[i] == coords[i + 1] {
return Err(crate::GeometryError::ConsecutiveDuplicateVertices { index: i });
}
}
Ok(())
}
#[cfg(test)]
#[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::*;
#[test]
fn coord3d_new_round_trips() {
let c = Coord3d::new(1.0, 2.0, 3.0);
assert_eq!((c.x, c.y, c.z), (1.0, 2.0, 3.0));
}
#[test]
fn to_2d_drops_z() {
let c = Coord3d::new(1.0, 2.0, 99.0);
assert_eq!(c.to_2d(), crate::types::Coord2d::new(1.0, 2.0));
}
#[test]
fn coord_seq3d_accessors() {
let a = Coord3d::new(0.0, 0.0, 0.0);
let b = Coord3d::new(1.0, 2.0, 3.0);
let seq = CoordSeq3d::new(vec![a, b]);
assert_eq!(seq.len(), 2);
assert!(!seq.is_empty());
assert_eq!(seq.as_slice(), &[a, b]);
assert_eq!(seq[1], b);
assert_eq!(seq.iter().count(), 2);
assert!(CoordSeq3d::new(vec![]).is_empty());
}
#[test]
fn check_coords3d_reports_each_axis() {
for (axis, bad) in [
(0_u8, Coord3d::new(f64::NAN, 0.0, 0.0)),
(1, Coord3d::new(0.0, f64::INFINITY, 0.0)),
(2, Coord3d::new(0.0, 0.0, f64::NEG_INFINITY)),
] {
let seq = CoordSeq3d::new(vec![Coord3d::new(1.0, 1.0, 1.0), bad]);
assert_eq!(
check_coords3d(&seq),
Err(crate::GeometryError::NonFiniteCoordinate {
coord_index: 1,
axis
})
);
}
}
#[test]
fn check_coords3d_duplicate_requires_all_three_components() {
let a = Coord3d::new(1.0, 1.0, 1.0);
let same_xy = Coord3d::new(1.0, 1.0, 2.0);
assert!(check_coords3d(&CoordSeq3d::new(vec![a, same_xy])).is_ok());
assert_eq!(
check_coords3d(&CoordSeq3d::new(vec![a, a])),
Err(crate::GeometryError::ConsecutiveDuplicateVertices { index: 0 })
);
}
}