1use crate::attributes::{AttributeStorage, UnknownAttributeStorage};
2use crate::cmap::{CMap2, DartIdType, EdgeIdType, NULL_DART_ID, SewError};
3use crate::geometry::CoordsFloat;
4use crate::stm::{Transaction, TransactionClosureResult, abort, try_or_coerce};
5
6#[doc(hidden)]
7impl<T: CoordsFloat> CMap2<T> {
9 #[allow(clippy::too_many_lines)]
11 pub(super) fn two_sew(
12 &self,
13 trans: &mut Transaction,
14 lhs_dart_id: DartIdType,
15 rhs_dart_id: DartIdType,
16 ) -> TransactionClosureResult<(), SewError> {
17 let b1lhs_dart_id = self.betas[(1, lhs_dart_id)].read(trans)?;
18 let b1rhs_dart_id = self.betas[(1, rhs_dart_id)].read(trans)?;
19 match (b1lhs_dart_id == NULL_DART_ID, b1rhs_dart_id == NULL_DART_ID) {
21 (true, true) => {
23 try_or_coerce!(
24 self.betas.two_link_core(trans, lhs_dart_id, rhs_dart_id),
25 SewError
26 );
27 let eid_new = self.edge_id_transac(trans, lhs_dart_id)?;
28 try_or_coerce!(
29 self.attributes.merge_edge_attributes(
30 trans,
31 eid_new,
32 lhs_dart_id as EdgeIdType,
33 rhs_dart_id as EdgeIdType,
34 ),
35 SewError
36 );
37 }
38 (true, false) => {
40 let lhs_eid_old = self.edge_id_transac(trans, lhs_dart_id)?;
42 let rhs_eid_old = self.edge_id_transac(trans, b1rhs_dart_id)?;
43 let lhs_vid_old = self.vertex_id_transac(trans, lhs_dart_id)?;
44 let b1rhs_vid_old = self.vertex_id_transac(trans, b1rhs_dart_id)?;
45 try_or_coerce!(
47 self.betas.two_link_core(trans, lhs_dart_id, rhs_dart_id),
48 SewError
49 );
50 let lhs_vid_new = self.vertex_id_transac(trans, lhs_dart_id)?;
52 let eid_new = self.edge_id_transac(trans, lhs_dart_id)?;
53 try_or_coerce!(
54 self.vertices
55 .merge(trans, lhs_vid_new, lhs_vid_old, b1rhs_vid_old),
56 SewError
57 );
58 try_or_coerce!(
59 self.attributes.merge_vertex_attributes(
60 trans,
61 lhs_vid_new,
62 lhs_vid_old,
63 b1rhs_vid_old,
64 ),
65 SewError
66 );
67 try_or_coerce!(
68 self.attributes
69 .merge_edge_attributes(trans, eid_new, lhs_eid_old, rhs_eid_old,),
70 SewError
71 );
72 }
73 (false, true) => {
75 let lhs_eid_old = self.edge_id_transac(trans, lhs_dart_id)?;
77 let rhs_eid_old = self.edge_id_transac(trans, b1rhs_dart_id)?;
78 let b1lhs_vid_old = self.vertex_id_transac(trans, b1lhs_dart_id)?;
79 let rhs_vid_old = self.vertex_id_transac(trans, rhs_dart_id)?;
80 try_or_coerce!(
82 self.betas.two_link_core(trans, lhs_dart_id, rhs_dart_id),
83 SewError
84 );
85 let rhs_vid_new = self.vertex_id_transac(trans, rhs_dart_id)?;
87 let eid_new = self.edge_id_transac(trans, lhs_dart_id)?;
88 try_or_coerce!(
89 self.vertices
90 .merge(trans, rhs_vid_new, b1lhs_vid_old, rhs_vid_old),
91 SewError
92 );
93 try_or_coerce!(
94 self.attributes.merge_vertex_attributes(
95 trans,
96 rhs_vid_new,
97 b1lhs_vid_old,
98 rhs_vid_old,
99 ),
100 SewError
101 );
102 try_or_coerce!(
103 self.attributes
104 .merge_edge_attributes(trans, eid_new, lhs_eid_old, rhs_eid_old,),
105 SewError
106 );
107 }
108 (false, false) => {
110 let lhs_eid_old = self.edge_id_transac(trans, lhs_dart_id)?;
112 let rhs_eid_old = self.edge_id_transac(trans, b1rhs_dart_id)?;
113 let lhs_vid_old = self.vertex_id_transac(trans, lhs_dart_id)?;
115 let b1rhs_vid_old = self.vertex_id_transac(trans, b1rhs_dart_id)?;
116 let b1lhs_vid_old = self.vertex_id_transac(trans, b1lhs_dart_id)?;
118 let rhs_vid_old = self.vertex_id_transac(trans, rhs_dart_id)?;
119
120 if let (
122 Ok(Some(l_vertex)), Ok(Some(b1r_vertex)), Ok(Some(b1l_vertex)), Ok(Some(r_vertex)), ) = (
127 self.vertices.read(trans, lhs_vid_old), self.vertices.read(trans, b1rhs_vid_old), self.vertices.read(trans, b1lhs_vid_old), self.vertices.read(trans, rhs_vid_old), ) {
132 let lhs_vector = b1l_vertex - l_vertex;
133 let rhs_vector = b1r_vertex - r_vertex;
134 if lhs_vector.dot(&rhs_vector) >= T::zero() {
138 abort(SewError::BadGeometry(2, lhs_dart_id, rhs_dart_id))?;
139 }
140 }
141
142 try_or_coerce!(
144 self.betas.two_link_core(trans, lhs_dart_id, rhs_dart_id),
145 SewError
146 );
147 let lhs_vid_new = self.vertex_id_transac(trans, lhs_dart_id)?;
149 let rhs_vid_new = self.vertex_id_transac(trans, rhs_dart_id)?;
150 let eid_new = self.edge_id_transac(trans, lhs_dart_id)?;
151 try_or_coerce!(
152 self.vertices
153 .merge(trans, lhs_vid_new, lhs_vid_old, b1rhs_vid_old),
154 SewError
155 );
156 try_or_coerce!(
157 self.vertices
158 .merge(trans, rhs_vid_new, b1lhs_vid_old, rhs_vid_old),
159 SewError
160 );
161 try_or_coerce!(
162 self.attributes.merge_vertex_attributes(
163 trans,
164 lhs_vid_new,
165 lhs_vid_old,
166 b1rhs_vid_old,
167 ),
168 SewError
169 );
170 try_or_coerce!(
171 self.attributes.merge_vertex_attributes(
172 trans,
173 rhs_vid_new,
174 b1lhs_vid_old,
175 rhs_vid_old,
176 ),
177 SewError
178 );
179 try_or_coerce!(
180 self.attributes
181 .merge_edge_attributes(trans, eid_new, lhs_eid_old, rhs_eid_old,),
182 SewError
183 );
184 }
185 }
186 Ok(())
187 }
188
189 #[allow(clippy::too_many_lines)]
191 pub(super) fn two_unsew(
192 &self,
193 trans: &mut Transaction,
194 lhs_dart_id: DartIdType,
195 ) -> TransactionClosureResult<(), SewError> {
196 let rhs_dart_id = self.betas[(2, lhs_dart_id)].read(trans)?;
197 let b1lhs_dart_id = self.betas[(1, lhs_dart_id)].read(trans)?;
198 let b1rhs_dart_id = self.betas[(1, rhs_dart_id)].read(trans)?;
199 match (b1lhs_dart_id == NULL_DART_ID, b1rhs_dart_id == NULL_DART_ID) {
201 (true, true) => {
202 let eid_old = self.edge_id_transac(trans, lhs_dart_id)?;
204 try_or_coerce!(self.betas.two_unlink_core(trans, lhs_dart_id), SewError);
206 try_or_coerce!(
209 self.attributes
210 .split_edge_attributes(trans, lhs_dart_id, rhs_dart_id, eid_old,),
211 SewError
212 );
213 }
214 (true, false) => {
215 let eid_old = self.edge_id_transac(trans, lhs_dart_id)?;
217 let lhs_vid_old = self.vertex_id_transac(trans, lhs_dart_id)?;
218 try_or_coerce!(self.betas.two_unlink_core(trans, lhs_dart_id), SewError);
220 try_or_coerce!(
223 self.attributes
224 .split_edge_attributes(trans, lhs_dart_id, rhs_dart_id, eid_old,),
225 SewError
226 );
227 let (new_lv_lhs, new_lv_rhs) = (
228 self.vertex_id_transac(trans, lhs_dart_id)?,
229 self.vertex_id_transac(trans, b1rhs_dart_id)?,
230 );
231 try_or_coerce!(
232 self.vertices
233 .split(trans, new_lv_lhs, new_lv_rhs, lhs_vid_old),
234 SewError
235 );
236 try_or_coerce!(
237 self.attributes.split_vertex_attributes(
238 trans,
239 new_lv_lhs,
240 new_lv_rhs,
241 lhs_vid_old,
242 ),
243 SewError
244 );
245 }
246 (false, true) => {
247 let eid_old = self.edge_id_transac(trans, lhs_dart_id)?;
249 let rhs_vid_old = self.vertex_id_transac(trans, rhs_dart_id)?;
250 try_or_coerce!(self.betas.two_unlink_core(trans, lhs_dart_id), SewError);
252 try_or_coerce!(
255 self.attributes
256 .split_edge_attributes(trans, lhs_dart_id, rhs_dart_id, eid_old,),
257 SewError
258 );
259 let (new_rv_lhs, new_rv_rhs) = (
260 self.vertex_id_transac(trans, b1lhs_dart_id)?,
261 self.vertex_id_transac(trans, rhs_dart_id)?,
262 );
263 try_or_coerce!(
264 self.vertices
265 .split(trans, new_rv_lhs, new_rv_rhs, rhs_vid_old),
266 SewError
267 );
268 try_or_coerce!(
269 self.attributes.split_vertex_attributes(
270 trans,
271 new_rv_lhs,
272 new_rv_rhs,
273 rhs_vid_old,
274 ),
275 SewError
276 );
277 }
278 (false, false) => {
279 let eid_old = self.edge_id_transac(trans, lhs_dart_id)?;
281 let lhs_vid_old = self.vertex_id_transac(trans, lhs_dart_id)?;
282 let rhs_vid_old = self.vertex_id_transac(trans, rhs_dart_id)?;
283 try_or_coerce!(self.betas.two_unlink_core(trans, lhs_dart_id), SewError);
285 try_or_coerce!(
288 self.attributes
289 .split_edge_attributes(trans, lhs_dart_id, rhs_dart_id, eid_old,),
290 SewError
291 );
292 let (new_lv_lhs, new_lv_rhs) = (
293 self.vertex_id_transac(trans, lhs_dart_id)?,
294 self.vertex_id_transac(trans, b1rhs_dart_id)?,
295 );
296 let (new_rv_lhs, new_rv_rhs) = (
297 self.vertex_id_transac(trans, b1lhs_dart_id)?,
298 self.vertex_id_transac(trans, rhs_dart_id)?,
299 );
300 try_or_coerce!(
301 self.vertices
302 .split(trans, new_lv_lhs, new_lv_rhs, lhs_vid_old),
303 SewError
304 );
305 try_or_coerce!(
306 self.attributes.split_vertex_attributes(
307 trans,
308 new_lv_lhs,
309 new_lv_rhs,
310 lhs_vid_old,
311 ),
312 SewError
313 );
314 try_or_coerce!(
315 self.vertices
316 .split(trans, new_rv_lhs, new_rv_rhs, rhs_vid_old),
317 SewError
318 );
319 try_or_coerce!(
320 self.attributes.split_vertex_attributes(
321 trans,
322 new_rv_lhs,
323 new_rv_rhs,
324 rhs_vid_old,
325 ),
326 SewError
327 );
328 }
329 }
330 Ok(())
331 }
332}