fj_kernel/operations/build/
cycle.rs

1use fj_math::Point;
2use itertools::Itertools;
3
4use crate::{
5    objects::{Cycle, HalfEdge},
6    operations::{BuildHalfEdge, Insert},
7    services::Services,
8};
9
10/// Build a [`Cycle`]
11pub trait BuildCycle {
12    /// Build an empty cycle
13    fn empty() -> Cycle {
14        Cycle::new([])
15    }
16
17    /// Build a polygon
18    fn polygon<P, Ps>(points: Ps, services: &mut Services) -> Cycle
19    where
20        P: Into<Point<2>>,
21        Ps: IntoIterator<Item = P>,
22        Ps::IntoIter: Clone + ExactSizeIterator,
23    {
24        let half_edges = points
25            .into_iter()
26            .map(Into::into)
27            .circular_tuple_windows()
28            .map(|(start, end)| {
29                HalfEdge::line_segment([start, end], None, services)
30                    .insert(services)
31            });
32
33        Cycle::new(half_edges)
34    }
35}
36
37impl BuildCycle for Cycle {}