mirror of
https://github.com/hannobraun/Fornjot
synced 2025-11-07 23:50:01 +00:00
Add PolyChain
This commit is contained in:
parent
2c0ab648ba
commit
56d681d201
@ -30,6 +30,7 @@
|
|||||||
mod aabb;
|
mod aabb;
|
||||||
mod coordinates;
|
mod coordinates;
|
||||||
mod point;
|
mod point;
|
||||||
|
mod poly_chain;
|
||||||
mod scalar;
|
mod scalar;
|
||||||
mod segment;
|
mod segment;
|
||||||
mod transform;
|
mod transform;
|
||||||
@ -40,6 +41,7 @@ pub use self::{
|
|||||||
aabb::Aabb,
|
aabb::Aabb,
|
||||||
coordinates::{Uv, Xyz, T},
|
coordinates::{Uv, Xyz, T},
|
||||||
point::Point,
|
point::Point,
|
||||||
|
poly_chain::PolyChain,
|
||||||
scalar::Scalar,
|
scalar::Scalar,
|
||||||
segment::Segment,
|
segment::Segment,
|
||||||
transform::Transform,
|
transform::Transform,
|
||||||
|
|||||||
59
fj-math/src/poly_chain.rs
Normal file
59
fj-math/src/poly_chain.rs
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user