fj_core/operations/sweep/
vertex.rs

1use crate::{
2    objects::{Curve, Vertex},
3    operations::insert::Insert,
4    storage::Handle,
5    Core,
6};
7
8use super::SweepCache;
9
10/// # Sweep a [`Vertex`]
11///
12/// See [module documentation] for more information.
13///
14/// [module documentation]: super
15pub trait SweepVertex: Sized {
16    /// # Sweep the vertex
17    ///
18    /// Returns the curve that the vertex was swept along, as well as a new
19    /// vertex to represent the point at the end of the sweep.
20    ///
21    ///
22    /// ## Comparison to Other Sweep Operations
23    ///
24    /// This method is a bit weird, compared to most other sweep operations, in
25    /// that it doesn't actually do any sweeping. That is because because both
26    /// [`Vertex`] and [`Curve`] do not define any geometry (please refer to
27    /// their respective documentation). Because of that, this method doesn't
28    /// even take the sweep path as an argument.
29    ///
30    /// The reason this code still exists as part of the sweep infrastructure,
31    /// is to make sure that sweeping the same vertex multiple times always
32    /// results in the same curve. This is also the reason that this trait is
33    /// only implemented for `Handle<Vertex>` and produces a `Handle<Curve>`.
34    fn sweep_vertex(
35        &self,
36        cache: &mut SweepCache,
37        core: &mut Core,
38    ) -> (Handle<Curve>, Handle<Vertex>);
39}
40
41impl SweepVertex for Handle<Vertex> {
42    fn sweep_vertex(
43        &self,
44        cache: &mut SweepCache,
45        core: &mut Core,
46    ) -> (Handle<Curve>, Handle<Vertex>) {
47        let curve = cache
48            .curves
49            .entry(self.id())
50            .or_insert_with(|| Curve::new().insert(core))
51            .clone();
52
53        let vertex = cache
54            .vertices
55            .entry(self.id())
56            .or_insert_with(|| Vertex::new().insert(core))
57            .clone();
58
59        (curve, vertex)
60    }
61}