fj_core/operations/split/
edge.rs

1use fj_math::Point;
2
3use crate::{
4    objects::{HalfEdge, Shell},
5    operations::{
6        derive::DeriveFrom, geometry::UpdateHalfEdgeGeometry, insert::Insert,
7        replace::ReplaceHalfEdge, split::SplitHalfEdge, update::UpdateHalfEdge,
8    },
9    queries::SiblingOfHalfEdge,
10    storage::Handle,
11    Core,
12};
13
14/// Split a pair of [`HalfEdge`]s into two
15pub trait SplitEdge: Sized {
16    /// Split the provided [`HalfEdge`], as well as its sibling, into two
17    ///
18    /// # Panics
19    ///
20    /// Panics, if the provided half-edge is not a part of this shell.
21    #[must_use]
22    fn split_edge(
23        &self,
24        half_edge: &Handle<HalfEdge>,
25        point: impl Into<Point<1>>,
26        core: &mut Core,
27    ) -> (Self, [[Handle<HalfEdge>; 2]; 2]);
28}
29
30impl SplitEdge for Shell {
31    fn split_edge(
32        &self,
33        half_edge: &Handle<HalfEdge>,
34        point: impl Into<Point<1>>,
35        core: &mut Core,
36    ) -> (Self, [[Handle<HalfEdge>; 2]; 2]) {
37        let point = point.into();
38
39        let sibling = self
40            .get_sibling_of(half_edge)
41            .expect("Expected half-edge and its sibling to be part of shell");
42
43        let [half_edge_a, half_edge_b] = half_edge
44            .split_half_edge(point, core)
45            .map(|half_edge_part| {
46                half_edge_part.insert(core).derive_from(half_edge, core)
47            });
48
49        let siblings = {
50            let [sibling_a, sibling_b] = sibling.split_half_edge(point, core);
51            let sibling_b = sibling_b
52                .update_start_vertex(
53                    |_, _| half_edge_b.start_vertex().clone(),
54                    core,
55                )
56                .insert(core);
57            [sibling_a, sibling_b].map(|half_edge| {
58                half_edge.insert(core).derive_from(&sibling, core).set_path(
59                    core.layers.geometry.of_half_edge(&sibling).path,
60                    &mut core.layers.geometry,
61                )
62            })
63        };
64
65        let [half_edge_a, half_edge_b] =
66            [half_edge_a, half_edge_b].map(|half_edge_part| {
67                half_edge_part.set_path(
68                    core.layers.geometry.of_half_edge(half_edge).path,
69                    &mut core.layers.geometry,
70                )
71            });
72
73        let shell = self
74            .replace_half_edge(
75                half_edge,
76                [half_edge_a.clone(), half_edge_b.clone()],
77                core,
78            )
79            .into_inner()
80            .replace_half_edge(&sibling, siblings.clone(), core)
81            .into_inner();
82
83        (shell, [[half_edge_a, half_edge_b], siblings])
84    }
85}