Add PolyChain

This commit is contained in:
Hanno Braun 2022-04-09 11:52:42 +02:00
parent 2c0ab648ba
commit 56d681d201
2 changed files with 61 additions and 0 deletions

View File

@ -30,6 +30,7 @@
mod aabb;
mod coordinates;
mod point;
mod poly_chain;
mod scalar;
mod segment;
mod transform;
@ -40,6 +41,7 @@ pub use self::{
aabb::Aabb,
coordinates::{Uv, Xyz, T},
point::Point,
poly_chain::PolyChain,
scalar::Scalar,
segment::Segment,
transform::Transform,

59
fj-math/src/poly_chain.rs Normal file
View File

@ -0,0 +1,59 @@
use crate::{Point, Segment};
/// A polygonal chain
///
/// The dimensionality of the polygonal chain is defined by the const generic
/// `D` parameter.
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[repr(C)]
pub struct PolyChain<const D: usize> {
points: Vec<Point<D>>,
}
impl<const D: usize> PolyChain<D> {
/// Construct a polygonal chain from a number of points
pub fn from_points(
points: impl IntoIterator<Item = impl Into<Point<D>>>,
) -> Self {
let points = points.into_iter().map(Into::into).collect();
Self { points }
}
/// Access the segments of the polygonal chain
pub fn segments(&self) -> Vec<Segment<D>> {
let mut segments = Vec::new();
for points in self.points.windows(2) {
// Can't panic, as we passed `2` to `windows`. Can be cleaned up,
// once `array_windows` is stable.
let points = [points[0], points[1]];
let segment = Segment::from_points(points);
segments.push(segment);
}
segments
}
/// Close the polygonal chain
///
/// Adds the first point of the chain as the last, closing the chain. This
/// method does not check whether the `PolyChain` is already closed.
pub fn close(mut self) -> Self {
if let Some(&point) = self.points.first() {
self.points.push(point);
}
self
}
}
impl<P, Ps, const D: usize> From<Ps> for PolyChain<D>
where
P: Into<Point<D>>,
Ps: IntoIterator<Item = P>,
{
fn from(points: Ps) -> Self {
Self::from_points(points)
}
}