1use crate::{
4 attributes::{AttributeStorage, UnknownAttributeStorage},
5 cmap::{CMap3, DartIdType, NULL_DART_ID, SewError},
6 geometry::CoordsFloat,
7 stm::{Transaction, TransactionClosureResult, abort, try_or_coerce},
8};
9
10impl<T: CoordsFloat> CMap3<T> {
12 #[allow(clippy::too_many_lines)]
13 pub(crate) fn two_sew(
15 &self,
16 trans: &mut Transaction,
17 ld: DartIdType,
18 rd: DartIdType,
19 ) -> TransactionClosureResult<(), SewError> {
20 let b1ld = self.betas[(1, ld)].read(trans)?;
21 let b1rd = self.betas[(1, rd)].read(trans)?;
22 match (b1ld == NULL_DART_ID, b1rd == NULL_DART_ID) {
24 (true, true) => {
26 let eid_l = self.edge_id_transac(trans, ld)?;
27 let eid_r = self.edge_id_transac(trans, b1rd)?;
28 try_or_coerce!(self.betas.two_link_core(trans, ld, rd), SewError);
29 let eid_new = self.edge_id_transac(trans, ld)?;
30 try_or_coerce!(
31 self.attributes
32 .merge_edge_attributes(trans, eid_new, eid_l, eid_r),
33 SewError
34 );
35 }
36 (true, false) => {
38 let eid_l = self.edge_id_transac(trans, ld)?;
40 let eid_r = self.edge_id_transac(trans, b1rd)?;
41 let vid_l = self.vertex_id_transac(trans, ld)?;
42 let vid_b1r = self.vertex_id_transac(trans, b1rd)?;
43 try_or_coerce!(self.betas.two_link_core(trans, ld, rd), SewError);
45 let vid_l_new = self.vertex_id_transac(trans, ld)?;
47 let eid_new = self.edge_id_transac(trans, ld)?;
48 try_or_coerce!(
49 self.vertices.merge(trans, vid_l_new, vid_l, vid_b1r),
50 SewError
51 );
52 try_or_coerce!(
53 self.attributes
54 .merge_vertex_attributes(trans, vid_l_new, vid_l, vid_b1r),
55 SewError
56 );
57 try_or_coerce!(
58 self.attributes
59 .merge_edge_attributes(trans, eid_new, eid_l, eid_r),
60 SewError
61 );
62 }
63 (false, true) => {
65 let eid_l = self.edge_id_transac(trans, ld)?;
67 let eid_r = self.edge_id_transac(trans, b1rd)?;
68 let vid_b1l = self.vertex_id_transac(trans, b1ld)?;
69 let vid_r = self.vertex_id_transac(trans, rd)?;
70 try_or_coerce!(self.betas.two_link_core(trans, ld, rd), SewError);
72 let vid_r_new = self.vertex_id_transac(trans, rd)?;
74 let eid_new = self.edge_id_transac(trans, ld)?;
75 try_or_coerce!(
76 self.vertices.merge(trans, vid_r_new, vid_b1l, vid_r),
77 SewError
78 );
79 try_or_coerce!(
80 self.attributes
81 .merge_vertex_attributes(trans, vid_r_new, vid_b1l, vid_r),
82 SewError
83 );
84 try_or_coerce!(
85 self.attributes
86 .merge_edge_attributes(trans, eid_new, eid_l, eid_r),
87 SewError
88 );
89 }
90 (false, false) => {
92 let eid_l = self.edge_id_transac(trans, ld)?;
94 let eid_r = self.edge_id_transac(trans, b1rd)?;
95 let vid_l = self.vertex_id_transac(trans, ld)?;
97 let vid_b1r = self.vertex_id_transac(trans, b1rd)?;
98 let vid_b1l = self.vertex_id_transac(trans, b1ld)?;
100 let vid_r = self.vertex_id_transac(trans, rd)?;
101
102 if let (
104 Ok(Some(l_vertex)),
106 Ok(Some(b1r_vertex)),
107 Ok(Some(b1l_vertex)),
109 Ok(Some(r_vertex)),
110 ) = (
111 self.vertices.read(trans, vid_l),
113 self.vertices.read(trans, vid_b1r),
114 self.vertices.read(trans, vid_b1l),
116 self.vertices.read(trans, vid_r),
117 ) {
118 let lhs_vector = b1l_vertex - l_vertex;
119 let rhs_vector = b1r_vertex - r_vertex;
120 if lhs_vector.dot(&rhs_vector) >= T::zero() {
124 abort(SewError::BadGeometry(2, ld, rd))?;
125 }
126 }
127
128 try_or_coerce!(self.betas.two_link_core(trans, ld, rd), SewError);
130 let vid_l_new = self.vertex_id_transac(trans, ld)?;
132 let vid_r_new = self.vertex_id_transac(trans, rd)?;
133 let eid_new = self.edge_id_transac(trans, ld)?;
134 try_or_coerce!(
135 self.vertices.merge(trans, vid_l_new, vid_l, vid_b1r),
136 SewError
137 );
138 try_or_coerce!(
139 self.vertices.merge(trans, vid_r_new, vid_b1l, vid_r),
140 SewError
141 );
142 try_or_coerce!(
143 self.attributes
144 .merge_vertex_attributes(trans, vid_l_new, vid_l, vid_b1r),
145 SewError
146 );
147 try_or_coerce!(
148 self.attributes
149 .merge_vertex_attributes(trans, vid_r_new, vid_b1l, vid_r),
150 SewError
151 );
152 try_or_coerce!(
153 self.attributes
154 .merge_edge_attributes(trans, eid_new, eid_l, eid_r),
155 SewError
156 );
157 }
158 }
159 Ok(())
160 }
161
162 pub(crate) fn two_unsew(
164 &self,
165 trans: &mut Transaction,
166 ld: DartIdType,
167 ) -> TransactionClosureResult<(), SewError> {
168 let rd = self.betas[(2, ld)].read(trans)?;
169 let b1ld = self.betas[(1, ld)].read(trans)?;
170 let b1rd = self.betas[(1, rd)].read(trans)?;
171 match (b1ld == NULL_DART_ID, b1rd == NULL_DART_ID) {
173 (true, true) => {
174 let eid_old = self.edge_id_transac(trans, ld)?;
176 try_or_coerce!(self.betas.two_unlink_core(trans, ld), SewError);
178 try_or_coerce!(
181 self.attributes
182 .split_edge_attributes(trans, ld, rd, eid_old),
183 SewError
184 );
185 }
186 (true, false) => {
187 let eid_old = self.edge_id_transac(trans, ld)?;
189 let vid_l = self.vertex_id_transac(trans, ld)?;
190 try_or_coerce!(self.betas.two_unlink_core(trans, ld), SewError);
192 try_or_coerce!(
195 self.attributes
196 .split_edge_attributes(trans, ld, rd, eid_old),
197 SewError
198 );
199 let (vid_l_newl, vid_l_newr) = (
200 self.vertex_id_transac(trans, ld)?,
201 self.vertex_id_transac(trans, b1rd)?,
202 );
203 try_or_coerce!(
204 self.vertices.split(trans, vid_l_newl, vid_l_newr, vid_l),
205 SewError
206 );
207 try_or_coerce!(
208 self.attributes
209 .split_vertex_attributes(trans, vid_l_newl, vid_l_newr, vid_l),
210 SewError
211 );
212 }
213 (false, true) => {
214 let eid_old = self.edge_id_transac(trans, ld)?;
216 let vid_r = self.vertex_id_transac(trans, rd)?;
217 try_or_coerce!(self.betas.two_unlink_core(trans, ld), SewError);
219 try_or_coerce!(
222 self.attributes
223 .split_edge_attributes(trans, ld, rd, eid_old),
224 SewError
225 );
226 let (vid_r_newl, vid_r_newr) = (
227 self.vertex_id_transac(trans, b1ld)?,
228 self.vertex_id_transac(trans, rd)?,
229 );
230 try_or_coerce!(
231 self.vertices.split(trans, vid_r_newl, vid_r_newr, vid_r),
232 SewError
233 );
234 try_or_coerce!(
235 self.attributes
236 .split_vertex_attributes(trans, vid_r_newl, vid_r_newr, vid_r),
237 SewError
238 );
239 }
240 (false, false) => {
241 let eid_old = self.edge_id_transac(trans, ld)?;
243 let vid_l = self.vertex_id_transac(trans, ld)?;
244 let vid_r = self.vertex_id_transac(trans, rd)?;
245 try_or_coerce!(self.betas.two_unlink_core(trans, ld), SewError);
247 try_or_coerce!(
250 self.attributes
251 .split_edge_attributes(trans, ld, rd, eid_old),
252 SewError
253 );
254 let (vid_l_newl, vid_l_newr) = (
255 self.vertex_id_transac(trans, ld)?,
256 self.vertex_id_transac(trans, b1rd)?,
257 );
258 let (vid_r_newl, vid_r_newr) = (
259 self.vertex_id_transac(trans, b1ld)?,
260 self.vertex_id_transac(trans, rd)?,
261 );
262 try_or_coerce!(
263 self.vertices.split(trans, vid_l_newl, vid_l_newr, vid_l),
264 SewError
265 );
266 try_or_coerce!(
267 self.vertices.split(trans, vid_r_newl, vid_r_newr, vid_r),
268 SewError
269 );
270 try_or_coerce!(
271 self.attributes
272 .split_vertex_attributes(trans, vid_l_newl, vid_l_newr, vid_l),
273 SewError
274 );
275 try_or_coerce!(
276 self.attributes
277 .split_vertex_attributes(trans, vid_r_newl, vid_r_newr, vid_r),
278 SewError
279 );
280 }
281 }
282 Ok(())
283 }
284}