aboutsummaryrefslogtreecommitdiff
path: root/src/msev.rs
blob: 16d18e461e201c977ab16987cd697d597cc2c228 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Make Split Edge Vertex

use crate::*;

pub struct Msev<'brand, 'arena, V> {
    pub shell: ptr!(Shell),
    pub vertex: ptr!(Vertex),
    pub loops: [ptr!(Loop); 2],
    pub data: V,
}

impl<'brand, 'arena, V> Msev<'brand, 'arena, V> {
    pub fn new(shell: ptr!(Shell), vertex: ptr!(Vertex), loops: [ptr!(Loop); 2], data: V) -> Self {
        Self {
            shell,
            vertex,
            loops,
            data,
        }
    }
}

#[derive(Debug, Error)]
pub enum MsevError {
    #[error("vertex is not part of loop")]
    VertexLoopMismatch,
}

impl<'brand, 'arena, V> Operator<'brand, 'arena, V> for Msev<'brand, 'arena, V> {
    type Inverse = Ksev<'brand, 'arena, V>;
    type Error = MsevError;
    type Check = [ptr!(HalfEdge); 2];

    fn check(&self, dcel: &Dcel<'brand, 'arena, V>) -> Result<Self::Check, Self::Error> {
        use MsevError::*;

        let a = self
            .vertex
            .find_outgoing(self.loops[0], dcel)
            .ok_or(VertexLoopMismatch)?;
        let b = self
            .vertex
            .find_outgoing(self.loops[1], dcel)
            .ok_or(VertexLoopMismatch)?;

        Ok([a, b])
    }

    fn apply(
        self,
        dcel: &mut Dcel<'brand, 'arena, V>,
    ) -> Result<Self::Inverse, OperatorErr<Self, Self::Error>> {
        let [a2, b0] = try_check!(self, dcel);
        let a0 = a2.prev(dcel);
        let b2 = b0.prev(dcel);

        let (edge, [a1, b1]) = Edge::create(self.shell, dcel);
        let v = self.shell.add_new_vertex(self.data, dcel);

        a1.update_origin(self.vertex, dcel);
        b2.twin(dcel).update_origin(*v, dcel);
        b1.update_origin(*v, dcel);
        a2.update_origin(*v, dcel);

        dcel.follow(a0, a1);
        dcel.follow(a1, a2);

        dcel.follow(b2, b1);
        dcel.follow(b1, b0);

        a1.set_loop_(self.loops[0], dcel);
        b1.set_loop_(self.loops[1], dcel);

        Ok(Ksev {
            shell: self.shell,
            loops: self.loops,
            old_vertex: self.vertex,
            new_vertex: v,
            edge,
        })
    }
}

pub struct Ksev<'brand, 'arena, V> {
    pub shell: ptr!(Shell),
    pub loops: [ptr!(Loop); 2],
    pub old_vertex: ptr!(Vertex),
    pub new_vertex: own!(Vertex),
    pub edge: own!(Edge),
}

impl<'brand, 'arena, V> Ksev<'brand, 'arena, V> {
    pub fn new(
        shell: ptr!(Shell),
        loops: [ptr!(Loop); 2],
        old_vertex: ptr!(Vertex),
        new_vertex: own!(Vertex),
        edge: own!(Edge),
    ) -> Self {
        Self {
            shell,
            loops,
            old_vertex,
            new_vertex,
            edge,
        }
    }
}

#[derive(Debug, Error)]
pub enum KsevError {
    #[error("edge does not match vertices")]
    EdgeVertexMismatch,
    #[error("edge does not match loops")]
    EdgeLoopMismatch,
}

impl<'brand, 'arena, V> Operator<'brand, 'arena, V> for Ksev<'brand, 'arena, V> {
    type Inverse = Msev<'brand, 'arena, V>;
    type Error = KsevError;
    type Check = [ptr!(HalfEdge); 2];

    fn check(&self, dcel: &Dcel<'brand, 'arena, V>) -> Result<Self::Check, Self::Error> {
        use KsevError::*;

        let [mut a, mut b] = self.edge.lens(dcel).half_edges();
        if a.origin().eq(*self.new_vertex) {
            [a, b] = [b, a];
        }

        or_err(
            a.origin().eq(self.old_vertex) && b.origin().eq(*self.new_vertex),
            EdgeVertexMismatch,
        )?;

        or_err(
            (a.loop_().eq(self.loops[0]) && b.loop_().eq(self.loops[1]))
                || (a.loop_().eq(self.loops[1]) && b.loop_().eq(self.loops[0])),
            EdgeLoopMismatch,
        )?;

        Ok([a.item, b.item])
    }

    fn apply(
        self,
        dcel: &mut Dcel<'brand, 'arena, V>,
    ) -> Result<Self::Inverse, OperatorErr<Self, Self::Error>> {
        let [a1, b1] = try_check!(self, dcel);

        let Ksev {
            shell,
            old_vertex,
            new_vertex,
            edge,
            ..
        } = self;

        let loops = [a1.loop_(dcel), b1.loop_(dcel)];

        let a0 = a1.prev(dcel);
        let a2 = a1.next(dcel);

        let b0 = b1.next(dcel);
        let b2 = b1.prev(dcel);

        dcel.follow(a0, a2);
        dcel.follow(b2, b0);

        a2.update_origin(old_vertex, dcel);
        b2.twin(dcel).update_origin(old_vertex, dcel);

        loops[0].set_half_edges(a0, dcel);
        loops[1].set_half_edges(b0, dcel);

        shell.remove_edge(*edge, dcel);
        shell.remove_vertex(*new_vertex, dcel);

        edge.destroy(dcel);
        let data = new_vertex.destroy(dcel);

        Ok(Msev {
            shell,
            vertex: old_vertex,
            loops,
            data,
        })
    }
}