fj_core/operations/build/
cycle.rs

1use fj_math::{Point, Scalar};
2use itertools::Itertools;
3
4use crate::{
5    objects::{Cycle, HalfEdge},
6    operations::{build::BuildHalfEdge, update::UpdateCycle},
7    Core,
8};
9
10/// Build a [`Cycle`]
11///
12/// See [module-level documentation] for context.
13///
14/// [module-level documentation]: super
15pub trait BuildCycle {
16    /// Build an empty cycle
17    fn empty() -> Cycle {
18        Cycle::new([])
19    }
20
21    /// Build a circle
22    fn circle(
23        center: impl Into<Point<2>>,
24        radius: impl Into<Scalar>,
25        core: &mut Core,
26    ) -> Cycle {
27        let circle = HalfEdge::circle(center, radius, core);
28        Cycle::empty().add_half_edges([circle], core)
29    }
30
31    /// Build a polygon
32    fn polygon<P, Ps>(points: Ps, core: &mut Core) -> Cycle
33    where
34        P: Into<Point<2>>,
35        Ps: IntoIterator<Item = P>,
36        Ps::IntoIter: Clone + ExactSizeIterator,
37    {
38        let edges = points
39            .into_iter()
40            .map(Into::into)
41            .circular_tuple_windows()
42            .map(|(start, end)| {
43                HalfEdge::line_segment([start, end], None, core)
44            });
45
46        Cycle::new(edges)
47    }
48}
49
50impl BuildCycle for Cycle {}