fj_core/operations/update/
cycle.rs

1use crate::{
2    objects::{Cycle, HalfEdge},
3    operations::{derive::DeriveFrom, insert::Insert},
4    storage::Handle,
5    Core,
6};
7
8/// Update a [`Cycle`]
9pub trait UpdateCycle {
10    /// Add edges to the cycle
11    #[must_use]
12    fn add_half_edges<T>(
13        &self,
14        half_edges: impl IntoIterator<Item = T>,
15        core: &mut Core,
16    ) -> Self
17    where
18        T: Insert<Inserted = Handle<HalfEdge>>;
19
20    /// Update an edge of the cycle
21    ///
22    /// # Panics
23    ///
24    /// Panics, if the object can't be found.
25    ///
26    /// Panics, if the update results in multiple handles referencing the same object.
27    #[must_use]
28    fn update_half_edge<T, R>(
29        &self,
30        handle: &Handle<HalfEdge>,
31        update: impl FnOnce(&Handle<HalfEdge>, &mut Core) -> R,
32        core: &mut Core,
33    ) -> Self
34    where
35        T: Insert<Inserted = Handle<HalfEdge>>,
36        R: IntoIterator<Item = T>;
37}
38
39impl UpdateCycle for Cycle {
40    fn add_half_edges<T>(
41        &self,
42        half_edges: impl IntoIterator<Item = T>,
43        core: &mut Core,
44    ) -> Self
45    where
46        T: Insert<Inserted = Handle<HalfEdge>>,
47    {
48        let half_edges = half_edges
49            .into_iter()
50            .map(|half_edge| half_edge.insert(core));
51        let half_edges = self.half_edges().iter().cloned().chain(half_edges);
52        Cycle::new(half_edges)
53    }
54
55    fn update_half_edge<T, R>(
56        &self,
57        handle: &Handle<HalfEdge>,
58        update: impl FnOnce(&Handle<HalfEdge>, &mut Core) -> R,
59        core: &mut Core,
60    ) -> Self
61    where
62        T: Insert<Inserted = Handle<HalfEdge>>,
63        R: IntoIterator<Item = T>,
64    {
65        let edges = self
66            .half_edges()
67            .replace(
68                handle,
69                update(handle, core).into_iter().map(|object| {
70                    object.insert(core).derive_from(handle, core)
71                }),
72            )
73            .expect("Half-edge not found");
74        Cycle::new(edges)
75    }
76}