use crate::GeometryError;
use crate::types3d::coord3d::check_coords3d;
use crate::types3d::{Coord3d, CoordSeq3d};
#[derive(Clone, Debug, PartialEq)]
pub struct LinearRing3d {
coords: CoordSeq3d,
}
impl LinearRing3d {
pub fn new(coords: CoordSeq3d) -> Result<Self, GeometryError> {
let coords = if coords.len() >= 2 && coords[0] == coords[coords.len() - 1] {
CoordSeq3d::new(coords.as_slice()[..coords.len() - 1].to_vec())
} else {
coords
};
Self::from_distinct(coords)
}
pub(crate) fn from_distinct(coords: CoordSeq3d) -> Result<Self, GeometryError> {
check_coords3d(&coords)?;
if coords.len() < 3 {
return Err(GeometryError::InsufficientVertices {
required: 3,
found: coords.len(),
});
}
let n = coords.len();
if coords[n - 1] == coords[0] {
return Err(GeometryError::ConsecutiveDuplicateVertices { index: n - 1 });
}
Ok(Self { coords })
}
pub fn coords(&self) -> &CoordSeq3d {
&self.coords
}
pub fn num_vertices(&self) -> usize {
self.coords.len()
}
pub fn edges(&self) -> impl Iterator<Item = (Coord3d, Coord3d)> + Clone + '_ {
let n = self.coords.len();
(0..n).map(move |i| (self.coords[i], self.coords[(i + 1) % n]))
}
pub fn newell_normal(&self) -> [f64; 3] {
let n = self.coords.len();
let (mut nx, mut ny, mut nz) = (0.0_f64, 0.0_f64, 0.0_f64);
for i in 0..n {
let a = self.coords[i];
let b = self.coords[(i + 1) % n];
nx += (a.y - b.y) * (a.z + b.z);
ny += (a.z - b.z) * (a.x + b.x);
nz += (a.x - b.x) * (a.y + b.y);
}
[nx, ny, nz]
}
}
#[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::*;
fn seq(pts: &[(f64, f64, f64)]) -> CoordSeq3d {
CoordSeq3d::new(pts.iter().map(|&(x, y, z)| Coord3d::new(x, y, z)).collect())
}
fn square_ccw() -> CoordSeq3d {
seq(&[
(0.0, 0.0, 0.0),
(1.0, 0.0, 0.0),
(1.0, 1.0, 0.0),
(0.0, 1.0, 0.0),
])
}
#[test]
fn accepts_ogc_format_strips_closing_vertex() {
let ogc = seq(&[
(0.0, 0.0, 0.0),
(1.0, 0.0, 0.0),
(1.0, 1.0, 0.0),
(0.0, 1.0, 0.0),
(0.0, 0.0, 0.0),
]);
let ring = LinearRing3d::new(ogc).unwrap();
assert_eq!(ring.num_vertices(), 4);
assert_eq!(ring.coords()[0], Coord3d::new(0.0, 0.0, 0.0));
assert_eq!(ring.coords()[3], Coord3d::new(0.0, 1.0, 0.0));
}
#[test]
fn closing_vertex_strip_requires_equal_z() {
let pts = seq(&[
(0.0, 0.0, 0.0),
(1.0, 0.0, 0.0),
(1.0, 1.0, 0.0),
(0.0, 0.0, 1.0),
]);
let ring = LinearRing3d::new(pts).unwrap();
assert_eq!(ring.num_vertices(), 4);
}
#[test]
fn rejects_fewer_than_three_distinct_vertices() {
let err = LinearRing3d::new(seq(&[(0.0, 0.0, 0.0), (1.0, 0.0, 0.0)])).unwrap_err();
assert_eq!(
err,
GeometryError::InsufficientVertices {
required: 3,
found: 2
}
);
}
#[test]
fn rejects_fewer_than_three_after_ogc_strip() {
let err = LinearRing3d::new(seq(&[(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (0.0, 0.0, 0.0)]))
.unwrap_err();
assert_eq!(
err,
GeometryError::InsufficientVertices {
required: 3,
found: 2
}
);
}
#[test]
fn rejects_consecutive_duplicate_in_middle() {
let err = LinearRing3d::new(seq(&[
(0.0, 0.0, 0.0),
(1.0, 0.0, 0.0),
(1.0, 0.0, 0.0),
(1.0, 1.0, 0.0),
]))
.unwrap_err();
assert_eq!(
err,
GeometryError::ConsecutiveDuplicateVertices { index: 1 }
);
}
#[test]
fn rejects_non_finite_z() {
let err = LinearRing3d::new(seq(&[
(0.0, 0.0, 0.0),
(1.0, 0.0, f64::INFINITY),
(1.0, 1.0, 0.0),
]))
.unwrap_err();
assert_eq!(
err,
GeometryError::NonFiniteCoordinate {
coord_index: 1,
axis: 2
}
);
}
#[test]
fn edges_yields_n_edges_including_closing() {
let ring = LinearRing3d::new(square_ccw()).unwrap();
let edges: Vec<_> = ring.edges().collect();
assert_eq!(edges.len(), 4);
assert_eq!(
edges[3],
(Coord3d::new(0.0, 1.0, 0.0), Coord3d::new(0.0, 0.0, 0.0))
);
}
#[test]
fn newell_normal_of_ccw_square_is_plus_z() {
let ring = LinearRing3d::new(square_ccw()).unwrap();
assert_eq!(ring.newell_normal(), [0.0, 0.0, 2.0]);
}
#[test]
fn newell_normal_of_vertical_ring_has_zero_z() {
let ring = LinearRing3d::new(seq(&[
(0.0, 0.0, 0.0),
(1.0, 0.0, 0.0),
(1.0, 0.0, 1.0),
(0.0, 0.0, 1.0),
]))
.unwrap();
let n = ring.newell_normal();
assert_eq!(n[2], 0.0);
assert_eq!(n[1].abs(), 2.0);
}
}