BREP_kernel 0.2.0

A boundary representation (BREP) geometry kernel for building CAD applications.
Documentation
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use super::*;

/// Normalize fragment orientation before tracing the open boundary. Selection
/// can combine independently generated carrier fragments whose `same_sense`
/// flags are individually correct but whose shared coedges use the same
/// direction. Propagate a consistent orientation over every connected shell.
pub(crate) fn orient_open_solid_faces(solid: &mut BrepSolid) -> Result<(), String> {
    let faces = solid
        .shells
        .iter()
        .flat_map(|shell| &shell.faces)
        .collect::<Vec<_>>();
    let mut edge_uses = HashMap::<u64, Vec<(usize, bool)>>::default();
    for (face_index, face) in faces.iter().enumerate() {
        for coedge in face
            .loops
            .iter()
            .flat_map(|loop_record| &loop_record.coedges)
        {
            edge_uses
                .entry(coedge.edge_id)
                .or_default()
                .push((face_index, coedge.forward));
        }
    }
    let mut adjacency = vec![Vec::<(usize, bool)>::new(); faces.len()];
    // Deterministic constraint order: on a CONSISTENT shell the flip
    // assignment is order-independent, but on a frustrated one (a genuinely
    // broken open shell headed for an honest refusal) the conflict-tolerant
    // BFS keeps whichever spanning constraints it saw first — HashMap order
    // would make the refusal MODE run-to-run nondeterministic.
    let mut ordered_edge_ids = edge_uses.keys().copied().collect::<Vec<_>>();
    ordered_edge_ids.sort_unstable();
    for uses in ordered_edge_ids
        .iter()
        .map(|edge_id| &edge_uses[edge_id])
        .filter(|uses| uses.len() == 2)
    {
        let (first_face, first_forward) = uses[0];
        let (second_face, second_forward) = uses[1];
        if first_face == second_face {
            continue;
        }
        // Equal current directions require exactly one face flip.
        let different_flip = first_forward == second_forward;
        adjacency[first_face].push((second_face, different_flip));
        adjacency[second_face].push((first_face, different_flip));
    }
    let mut flips = vec![None; faces.len()];
    for root in 0..faces.len() {
        if flips[root].is_some() {
            continue;
        }
        flips[root] = Some(false);
        let mut pending = vec![root];
        while let Some(face_index) = pending.pop() {
            let face_flip = flips[face_index].unwrap();
            for &(neighbor, different_flip) in &adjacency[face_index] {
                let required = face_flip ^ different_flip;
                if let Some(existing) = flips[neighbor] {
                    if existing != required {
                        // Redundant constraints can conflict while duplicate
                        // or open boundaries still exist. Keep the spanning
                        // assignment already established; final manifold
                        // validation remains authoritative.
                        continue;
                    }
                } else {
                    flips[neighbor] = Some(required);
                    pending.push(neighbor);
                }
            }
        }
    }
    drop(faces);
    for (face_index, face) in solid
        .shells
        .iter_mut()
        .flat_map(|shell| &mut shell.faces)
        .enumerate()
    {
        if flips[face_index] != Some(true) {
            continue;
        }
        face.same_sense = !face.same_sense;
        for loop_record in &mut face.loops {
            loop_record.coedges.reverse();
            for coedge in &mut loop_record.coedges {
                coedge.forward = !coedge.forward;
                coedge.pcurve = coedge.pcurve.reversed()?;
            }
        }
    }
    Ok(())
}

/// Re-analyticize a fitted-polyline rim as the EXACT v-isoline of its owner
/// surface, rewriting the owner's rim coedge to the straight parameter-space
/// line pinned through its loop neighbours. Returns the updated edge record
/// and the owner's new `forward` flag, or `None` (with the solid untouched)
/// when the rim is not a circle riding an owner isoline — callers keep the
/// faithful polyline in that case. The carrier polylines' chord sag is the
/// dominant wall-volume error this removes.
pub(super) fn upgrade_rim_to_owner_isoline(
    solid: &mut BrepSolid,
    edge_id: u64,
    owner_shell: usize,
    owner_face: usize,
    tolerance: f64,
) -> Result<Option<(EdgeRecord, bool)>, String> {
    let Some(edge) = solid.edges.iter().find(|edge| edge.id == edge_id).cloned() else {
        return Ok(None);
    };
    if fit_rim_circle(&edge, tolerance)?.is_none() {
        return Ok(None);
    }
    let owner = &solid.shells[owner_shell].faces[owner_face];
    let Some((prev_end, next_start)) = owner
        .loops
        .iter()
        .find(|loop_record| {
            loop_record
                .coedges
                .iter()
                .any(|coedge| coedge.edge_id == edge_id)
        })
        .and_then(|loop_record| {
            let count = loop_record.coedges.len();
            let index = loop_record
                .coedges
                .iter()
                .position(|coedge| coedge.edge_id == edge_id)?;
            let prev = &loop_record.coedges[(index + count - 1) % count];
            let next = &loop_record.coedges[(index + 1) % count];
            let prev_domain = prev.pcurve.domain().ok()?;
            let next_domain = next.pcurve.domain().ok()?;
            Some((
                prev.pcurve.evaluate(prev_domain[1]).ok()?,
                next.pcurve.evaluate(next_domain[0]).ok()?,
            ))
        })
    else {
        return Ok(None);
    };
    if (prev_end.y - next_start.y).abs() > tolerance.max(1e-4) {
        return Ok(None);
    }
    let Ok(iso) = owner.surface.iso_curve_v(prev_end.y) else {
        return Ok(None);
    };
    let [iso_t0, iso_t1] = iso.domain()?;
    // The isoline must actually be this rim (same closed locus).
    let mut worst = 0.0f64;
    for sample in 0..=32 {
        let fraction = sample as f64 / 32.0;
        let on_iso = iso.evaluate(iso_t0 + (iso_t1 - iso_t0) * fraction)?;
        worst = worst.max(crate::project_point_to_curve(&edge.curve, on_iso)?.distance);
    }
    if worst > tolerance.max(1e-3) {
        return Ok(None);
    }
    let owner_pcurve = crate::make_line(
        Vec3::new(prev_end.x, prev_end.y, 0.0),
        Vec3::new(next_start.x, next_start.y, 0.0),
    )?;
    let owner_forward_new = isoline_forward(&owner_pcurve, &owner.surface, &iso)?;
    let updated = EdgeRecord {
        curve: iso,
        t0: iso_t0,
        t1: iso_t1,
        ..edge.clone()
    };
    let owner = &mut solid.shells[owner_shell].faces[owner_face];
    for coedge in owner
        .loops
        .iter_mut()
        .flat_map(|loop_record| &mut loop_record.coedges)
        .filter(|coedge| coedge.edge_id == edge_id)
    {
        coedge.pcurve = owner_pcurve.clone();
        coedge.forward = owner_forward_new;
    }
    if let Some(record) = solid.edges.iter_mut().find(|record| record.id == edge_id) {
        record.curve = updated.curve.clone();
        record.t0 = updated.t0;
        record.t1 = updated.t1;
    }
    Ok(Some((updated, owner_forward_new)))
}

/// Weld an offset skin's rim that the wall/cap fragmentation left orphaned
/// into the coplanar PLANAR wall face that contains it, as an inner (hole)
/// loop.
///
/// The offset pipeline builds a curved retained face's rim as a CLOSED circle
/// edge (`offset_face_carrier` copies the source topology).  When that rim
/// stays in the plane of an opening cap — the retained side face is
/// perpendicular to the opening, as for a cylinder or a vertical-walled prism
/// or a straight through-hole — the imprint of the offset carrier against the
/// opening plane is a boundary-grazing intersection whose pcurve on the plane
/// collapses to a degenerate straight segment (`fragment::sample_chain` then
/// emits only its two coincident endpoints).  The wall disk is therefore never
/// split into an annulus, and the offset rim dangles as a one-use closed edge
/// (the historical "rim pairs with inconsistent representations — closed
/// circle on one side, open fitted chain on the other — cannot weld" note in
/// GOLOVANOV-PLAN.md phase 5).
///
/// Rather than re-run that fragile SSI, sew the two representations directly:
/// the orphaned rim IS exactly the hole the wall needs.  Add it as the wall's
/// inner loop with the opposite coedge use, which makes the rim two-use
/// (watertight) and turns the full disk into the correct annulus.  Only planar
/// containing faces and rims that actually lie in that plane qualify, so a cone
/// (whose offset rim leaves the opening plane) is untouched and still degrades
/// through the normal validation path.
pub(super) fn weld_coplanar_orphan_rims(solid: &mut BrepSolid, tolerance: f64) -> Result<usize, String> {
    let mut use_counts = HashMap::<u64, usize>::default();
    for coedge in solid
        .shells
        .iter()
        .flat_map(|shell| &shell.faces)
        .flat_map(|face| &face.loops)
        .flat_map(|loop_record| &loop_record.coedges)
    {
        *use_counts.entry(coedge.edge_id).or_default() += 1;
    }
    let orphan_ids = solid
        .edges
        .iter()
        .filter(|edge| {
            use_counts.get(&edge.id).copied().unwrap_or(0) == 1
                && edge.start_vertex_id == edge.end_vertex_id
        })
        .map(|edge| edge.id)
        .collect::<Vec<_>>();
    let mut welded = 0usize;
    let mut shell_unions: Vec<(usize, usize)> = Vec::new();
    for edge_id in orphan_ids {
        let edge = solid
            .edges
            .iter()
            .find(|edge| edge.id == edge_id)
            .cloned()
            .ok_or_else(|| "offset_shell: orphan rim vanished".to_string())?;
        // Reject true point placeholders (poles): a rim must sweep measurably
        // away from its seam vertex.
        let anchor = edge.curve.evaluate(edge.t0)?;
        let sweeps = [0.25, 0.5, 0.75].into_iter().any(|fraction| {
            edge.curve
                .evaluate(edge.t0 + (edge.t1 - edge.t0) * fraction)
                .map(|point| point.sub(anchor).length() > 10.0 * tolerance)
                .unwrap_or(false)
        });
        if !sweeps {
            continue;
        }
        // The single existing use fixes the manifold direction: the wall must
        // trace the shared rim the opposite way.
        let (owner_shell, owner_face, owner_forward) = solid
            .shells
            .iter()
            .enumerate()
            .find_map(|(shell_index, shell)| {
                shell
                    .faces
                    .iter()
                    .enumerate()
                    .find_map(|(face_index, face)| {
                        face.loops
                            .iter()
                            .flat_map(|loop_record| &loop_record.coedges)
                            .find(|coedge| coedge.edge_id == edge_id)
                            .map(|coedge| (shell_index, face_index, coedge.forward))
                    })
            })
            .ok_or_else(|| "offset_shell: orphan rim has no use".to_string())?;
        let mut wall_forward = !owner_forward;
        let rim_mid = edge.curve.evaluate((edge.t0 + edge.t1) * 0.5)?;
        // Locate a planar face that both carries the rim and strictly contains
        // it — the opening cap the rim should hole.
        let mut target: Option<(usize, usize)> = None;
        'search: for (shell_index, shell) in solid.shells.iter().enumerate() {
            for (face_index, face) in shell.faces.iter().enumerate() {
                if !face.surface.is_affine().unwrap_or(false) {
                    continue;
                }
                if face
                    .loops
                    .iter()
                    .flat_map(|loop_record| &loop_record.coedges)
                    .any(|coedge| coedge.edge_id == edge_id)
                {
                    continue;
                }
                if !edge_on_surface(&edge, &face.surface, tolerance)? {
                    continue;
                }
                let projection = project_point_to_surface(&face.surface, rim_mid)?;
                let uv = Vec2 {
                    x: projection.u,
                    y: projection.v,
                };
                if projection.distance > tolerance
                    || parameter_point_in_face(face, uv, tolerance)? == PolygonClass::Outside
                {
                    continue;
                }
                target = Some((shell_index, face_index));
                break 'search;
            }
        }
        let Some((shell_index, face_index)) = target else {
            continue;
        };
        // Exactness upgrade (best effort, full fallback): when the rim is a
        // circle that runs along a v-isoline of its owner surface, swap the
        // carrier's fitted polyline for the owner's EXACT isoline and pin the
        // owner pcurve to the straight parameter-space line through its loop
        // neighbours — the same re-analyticization the ruled weld performs.
        // The polyline's chord sag is the only thing standing between a
        // cylinder-wall shell and an exact annulus wall volume. Non-circular
        // rims (a prism hole's rectangle) keep the faithful polyline weld.
        let edge =
            match upgrade_rim_to_owner_isoline(solid, edge_id, owner_shell, owner_face, tolerance)?
            {
                Some((updated, owner_forward_new)) => {
                    wall_forward = !owner_forward_new;
                    updated
                }
                None => edge,
            };
        let Some(pcurve) = boundary_pcurve(
            &edge,
            wall_forward,
            &solid.shells[shell_index].faces[face_index].surface,
            tolerance,
        )?
        else {
            continue;
        };
        let next_loop_id = solid.shells[shell_index].faces[face_index]
            .loops
            .iter()
            .map(|loop_record| loop_record.id)
            .max()
            .unwrap_or(0)
            + 1;
        let next_coedge_id = solid
            .shells
            .iter()
            .flat_map(|shell| &shell.faces)
            .flat_map(|face| &face.loops)
            .flat_map(|loop_record| &loop_record.coedges)
            .map(|coedge| coedge.id)
            .max()
            .unwrap_or(0)
            + 1;
        solid.shells[shell_index].faces[face_index]
            .loops
            .push(LoopRecord {
                id: next_loop_id,
                coedges: vec![CoedgeRecord {
                    id: next_coedge_id,
                    edge_id,
                    forward: wall_forward,
                    pcurve,
                }],
            });
        // The rim is now shared real topology, not a face-local placeholder:
        // clear the historical degenerate marking the assembler left on the
        // unwelded closed edge so Euler/degenerate-use validation see the 1-cell.
        if let Some(record) = solid.edges.iter_mut().find(|record| record.id == edge_id) {
            record.degenerate = false;
        }
        if owner_shell != shell_index {
            shell_unions.push((owner_shell, shell_index));
        }
        welded += 1;
    }
    // A welded rim joins the offset skin to its opening cap; if they lived in
    // separate assembled shell containers, they are now one connected shell.
    if !shell_unions.is_empty() {
        let mut parent = (0..solid.shells.len()).collect::<Vec<_>>();
        fn root(parent: &mut [usize], index: usize) -> usize {
            if parent[index] != index {
                parent[index] = root(parent, parent[index]);
            }
            parent[index]
        }
        for (first, second) in shell_unions {
            let first_root = root(&mut parent, first);
            let second_root = root(&mut parent, second);
            if first_root != second_root {
                parent[second_root] = first_root;
            }
        }
        let original = std::mem::take(&mut solid.shells);
        let mut merged = Vec::<ShellRecord>::new();
        let mut group_of = HashMap::<usize, usize>::default();
        for (index, shell) in original.into_iter().enumerate() {
            let group = root(&mut parent, index);
            if let Some(target) = group_of.get(&group).copied() {
                merged[target].faces.extend(shell.faces);
            } else {
                group_of.insert(group, merged.len());
                merged.push(shell);
            }
        }
        solid.shells = merged;
    }
    Ok(welded)
}

fn flip_face_orientation(face: &mut FaceRecord) -> Result<(), String> {
    face.same_sense = !face.same_sense;
    for loop_record in &mut face.loops {
        loop_record.coedges.reverse();
        for coedge in &mut loop_record.coedges {
            coedge.forward = !coedge.forward;
            coedge.pcurve = coedge.pcurve.reversed()?;
        }
    }
    Ok(())
}

pub(crate) fn flip_shell_faces(shell: &mut ShellRecord) -> Result<(), String> {
    for face in &mut shell.faces {
        flip_face_orientation(face)?;
    }
    Ok(())
}

pub(crate) fn flip_all_faces(solid: &mut BrepSolid) -> Result<(), String> {
    for shell in &mut solid.shells {
        flip_shell_faces(shell)?;
    }
    Ok(())
}