BREP_boolean 0.24.0

Booleans and healing of the BREP kernel: imprint, fragment, boolean assembly, sewing and face merging.
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! SELF-TOUCH SPLIT — a face whose loops touch at an edge INTERIOR.
//!
//! A face's outer loop and one of its holes (or one loop and itself) can
//! touch at a point that is not a vertex of either edge: the 2026-09-07
//! report's r = 6 through-hole is tangent to both r = 4 fillet setbacks on
//! the top face, so the hole circle kisses the setback edges at (16, 10) and
//! (10, 16) with no vertex there.  Such a face is a pinched region; the
//! fragment stage's 2D arrangement resolves the pinch by carving the touched-
//! off corner as its own region — which only assembles when the touch point is
//! a vertex of BOTH touching edges, so every run through a chain is a
//! complete run and every fragment references shared sub-edges.  Without the
//! vertex the closed hole chain is walked in two partial runs and
//! `build_loop` refuses ("chain N fragmented into an incomplete run").  The
//! centre-plane document of the same day only executed because the imprint
//! happened to split the hole edge at those very points.
//!
//! Mint that vertex here, on the operand: split BOTH touching edges at their
//! closest approach when it lies within the contact band and interior to
//! each edge.  `apply_edge_splits` claims one vertex for the two coincident
//! split points and rewrites every coedge on the edge, so the hole's cylinder
//! wall — the face on the other side of the hole edge — carries the same
//! sub-edges by construction.  A face whose loops do not come within the band
//! of each other is untouched.

pub use super::*;
use crate::{KernelRefusal, KernelStage, OrRefuse};

/// Samples of an edge (interval count) for the coarse closest-approach scan.
const SCAN_SAMPLES: usize = 16;
/// Golden-section iterations refining the closest approach inside the
/// bracket around the best scan sample (0.618^48 ≈ 1e-10 of the bracket).
const REFINE_ITERATIONS: usize = 48;
/// Only a pair whose best scan sample comes within this fraction of the
/// pair's combined length is refined. A curve departs from its scan samples
/// by at most ~2% of its length between them (a full circle's chord sag at
/// 16 samples is 1.9% of the radius, 0.3% of the length), so a touch can
/// never hide behind the gate; a pair that is merely near in the bounding
/// boxes (most non-adjacent edge pairs of a curved face) costs the scan alone.
const REFINE_GATE: f64 = 0.05;

/// Contact band of the self-touch test: the imprint's contact band
/// (`tolerance × 100`) floored by the weld radius and by `2e-6 × scale`, which
/// stands in for the fragment arrangement's own touch radius (`1e-6 × the
/// face's uv-domain diagonal`) on the kernel's length-parameterised carriers,
/// where a face's domain diagonal stays within about √2 of the operand's
/// extent. Two loops the arrangement will fuse must be fused here as well, or
/// the arrangement still walks the partial runs.
pub fn self_touch_band(tolerance: f64, scale: f64) -> f64 {
    (tolerance * 100.0).max(WELD_FLOOR).max(2e-6 * scale)
}

struct EdgeScan<'a> {
    edge: &'a EdgeRecord,
    curve: NurbsCurve,
    minimum: Vec3,
    maximum: Vec3,
}

fn edge_scan<'a>(edge: &'a EdgeRecord, curve: NurbsCurve) -> EdgeScan<'a> {
    let mut minimum = Vec3::new(f64::INFINITY, f64::INFINITY, f64::INFINITY);
    let mut maximum = Vec3::new(f64::NEG_INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY);
    for control in &curve.control_points {
        if control.w.abs() <= 1e-300 {
            continue;
        }
        let point = Vec3::new(control.x / control.w, control.y / control.w, control.z / control.w);
        minimum = Vec3::new(minimum.x.min(point.x), minimum.y.min(point.y), minimum.z.min(point.z));
        maximum = Vec3::new(maximum.x.max(point.x), maximum.y.max(point.y), maximum.z.max(point.z));
    }
    EdgeScan {
        edge,
        curve,
        minimum,
        maximum,
    }
}

pub fn boxes_overlap(a: &EdgeScan<'_>, b: &EdgeScan<'_>, pad: f64) -> bool {
    a.minimum.x <= b.maximum.x + pad
        && b.minimum.x <= a.maximum.x + pad
        && a.minimum.y <= b.maximum.y + pad
        && b.minimum.y <= a.maximum.y + pad
        && a.minimum.z <= b.maximum.z + pad
        && b.minimum.z <= a.maximum.z + pad
}

fn shares_a_vertex(a: &EdgeRecord, b: &EdgeRecord) -> bool {
    a.start_vertex_id == b.start_vertex_id
        || a.start_vertex_id == b.end_vertex_id
        || a.end_vertex_id == b.start_vertex_id
        || a.end_vertex_id == b.end_vertex_id
}

/// Closest approach of `probe` to `target`: `(t on probe, u on target,
/// distance)`.  A coarse uniform scan of `probe` projected onto `target`,
/// then a golden-section refinement of the distance over the bracket around
/// the best sample.  Exact for the tangential kiss this exists for (the
/// distance is smooth and unimodal inside one scan bracket).
fn closest_approach(
    probe: &NurbsCurve,
    target: &NurbsCurve,
    refine_gate: f64,
) -> Result<(f64, f64, f64), KernelRefusal> {
    let [t0, t1] = probe.domain().or_refuse(KernelStage::Intersect, "domain")?;
    let distance_at = |t: f64| -> Result<(f64, f64), KernelRefusal> {
        let point = probe.evaluate(t).or_refuse(KernelStage::Intersect, "evaluate")?;
        let projection =
            project_point_to_curve(target, point).or_refuse(KernelStage::Intersect, "project_point_to_curve")?;
        Ok((projection.u, projection.distance))
    };
    let mut best_index = 0usize;
    let mut best: Option<(f64, f64, f64)> = None;
    for index in 0..=SCAN_SAMPLES {
        let t = t0 + (t1 - t0) * index as f64 / SCAN_SAMPLES as f64;
        let (u, distance) = distance_at(t)?;
        if best.is_none_or(|(_, _, current)| distance < current) {
            best = Some((t, u, distance));
            best_index = index;
        }
    }
    let Some(mut best) = best else {
        return Err(KernelRefusal::internal(KernelStage::Intersect, "imprint.self_touch", "closest approach of an empty scan"));
    };
    if best.2 > refine_gate {
        return Ok(best);
    }
    let step = (t1 - t0) / SCAN_SAMPLES as f64;
    let mut low = (t0 + step * best_index.saturating_sub(1) as f64).max(t0);
    let mut high = (t0 + step * (best_index + 1) as f64).min(t1);
    // Golden-section search on a unimodal bracket: keep the two interior
    // probes, drop the outer quarter on the worse side.
    let ratio = (5f64.sqrt() - 1.0) / 2.0;
    let mut inner_low = high - ratio * (high - low);
    let mut inner_high = low + ratio * (high - low);
    let mut at_low = distance_at(inner_low)?;
    let mut at_high = distance_at(inner_high)?;
    for _ in 0..REFINE_ITERATIONS {
        if at_low.1 < at_high.1 {
            high = inner_high;
            inner_high = inner_low;
            at_high = at_low;
            inner_low = high - ratio * (high - low);
            at_low = distance_at(inner_low)?;
        } else {
            low = inner_low;
            inner_low = inner_high;
            at_low = at_high;
            inner_high = low + ratio * (high - low);
            at_high = distance_at(inner_high)?;
        }
        if high - low <= 1e-14 * (t1 - t0).abs().max(1.0) {
            break;
        }
    }
    for (t, (u, distance)) in [(inner_low, at_low), (inner_high, at_high)] {
        if distance < best.2 {
            best = (t, u, distance);
        }
    }
    Ok(best)
}

thread_local! {
    /// [`face_self_touches`]' splits per face, as (index into the face's
    /// non-degenerate edges, parameter), keyed by everything the scan reads:
    /// the band's bits and, per edge in order, its degenerate flag, its two
    /// vertices renumbered by first appearance (which pairs share a vertex,
    /// never the ids) and its subcurve's content. The fillet's cutter
    /// composition hands the same faces, under new ids, to imprint after
    /// imprint (0.77 s of F9 on the sprocket report 1d179b4a).
    static TOUCH_MEMO: std::cell::RefCell<HashMap<[u64; 2], Vec<(usize, f64)>>> = std::cell::RefCell::new(HashMap::default());
    static TOUCH_SCANS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
    /// [`closest_approach`]'s reading per edge pair, keyed by the two
    /// subcurves' content in scan order and the refine gate's bits (all the
    /// reading depends on). A boolean that adds one hole to a face of many
    /// holes changes the face's key, so the face is scanned again, yet every
    /// pair of its old edges reads what it read before.
    static PAIR_MEMO: std::cell::RefCell<HashMap<[u64; 2], (f64, f64, f64)>> = std::cell::RefCell::new(HashMap::default());
    static PAIR_READS: std::cell::Cell<[usize; 2]> = const { std::cell::Cell::new([0; 2]) };
}

/// `BREP_SELF_TOUCH_PAIR_MEMO` (default on, `0` off): serve a repeated edge pair's
/// closest approach from [`PAIR_MEMO`]. Off with the face memo's hatches.
fn pair_memo_on() -> bool {
    switch!("BREP_SELF_TOUCH_PAIR_MEMO", true)
}

/// Pair readings (served, read) since the thread started.
pub fn pair_reads() -> [usize; 2] {
    PAIR_READS.with(|reads| reads.get())
}

/// The content key of one subcurve (see [`PAIR_MEMO`]).
fn curve_key(curve: &NurbsCurve) -> [u64; 2] {
    let mut lane = crate::mass_properties::TwoLane::new();
    crate::mass_properties::hash_curve(&mut lane, curve);
    lane.finish()
}

/// Drop every memoised self-touch scan (`reading_memo::clear`).
pub fn clear_touch_memo() {
    TOUCH_MEMO.with(|memo| memo.borrow_mut().clear());
    PAIR_MEMO.with(|memo| memo.borrow_mut().clear());
}

/// Faces scanned (memo misses and unmemoised scans) since the thread started.
pub fn touch_scans() -> usize {
    TOUCH_SCANS.with(|scans| scans.get())
}

/// The content key of one face's self-touch scan (see [`TOUCH_MEMO`]).
fn touch_key(edges: &[&EdgeRecord], scans: &[EdgeScan<'_>], band: f64) -> [u64; 2] {
    let mut lane = crate::mass_properties::TwoLane::new();
    lane.f64(band);
    lane.usize(edges.len());
    let mut numbering: HashMap<u64, usize> = HashMap::default();
    for edge in edges {
        lane.bool(edge.degenerate);
        for vertex in [edge.start_vertex_id, edge.end_vertex_id] {
            let next = numbering.len();
            lane.usize(*numbering.entry(vertex).or_insert(next));
        }
    }
    lane.usize(scans.len());
    for scan in scans {
        crate::mass_properties::hash_curve(&mut lane, &scan.curve);
    }
    lane.finish()
}

/// Every self-touch of ONE face's loops, as the `(edge, parameter)` splits
/// that mint each touch as a vertex on BOTH touching edges.  Shared by the
/// imprint builder's per-operand pass and the solid-level entry point below
/// so the two lanes can never scan on different terms.  Served from
/// [`TOUCH_MEMO`] when a face of the same content was scanned before; off
/// under `BREP_SELF_TOUCH_MEMO=0`, the reading memo's hatch
/// (`BREP_READING_MEMO=0`) and the scan's own trace (`BREP_DEBUG_SELF_TOUCH`).
pub fn face_self_touches<'a>(
    operand: u8,
    face_id: u64,
    edges: &[&'a EdgeRecord],
    band: f64,
    subcurve: &dyn Fn(&EdgeRecord) -> Result<NurbsCurve, KernelRefusal>,
) -> Result<Vec<(&'a EdgeRecord, f64)>, KernelRefusal> {
    if edges.len() < 2 {
        return Ok(Vec::new());
    }
    let debug = env_read!("BREP_DEBUG_SELF_TOUCH").is_some();
    let scans = edges
        .iter()
        .filter(|edge| !edge.degenerate)
        .map(|edge| Ok(edge_scan(edge, subcurve(edge)?)))
        .collect::<Result<Vec<_>, KernelRefusal>>()?;
    let memo = !debug
        && crate::reading_memo::memo_on()
        && env_read!("BREP_SELF_TOUCH_MEMO").as_deref() != Some("0");
    let key = memo.then(|| touch_key(edges, &scans, band));
    if let Some(key) = key {
        if let Some(found) = TOUCH_MEMO.with(|memo| memo.borrow().get(&key).cloned()) {
            crate::reading_memo::note(true);
            return Ok(found.into_iter().map(|(index, parameter)| (scans[index].edge, parameter)).collect());
        }
        crate::reading_memo::note(false);
    }
    TOUCH_SCANS.with(|count| count.set(count.get() + 1));
    let lengths = scans
        .iter()
        .map(|scan| curve_length_rough(&scan.curve))
        .collect::<Result<Vec<_>, KernelRefusal>>()?;
    let mut splits = Vec::new();
    let mut found: Vec<(usize, f64)> = Vec::new();
    // SPAN CULL (`BREP_SELF_TOUCH_SPAN_CULL`, default on, =0 restores;
    // 2026-11-03): a pair is kept only when `closest_approach` reads it within
    // `band`, and that reading is ATTAINED (a probe point's distance to its
    // projection on the target), so never below the curves' true distance.
    // When every knot-span box of one edge is apart from every span box of the
    // other by more than `band` on some axis, the true distance exceeds the
    // band and the pair cannot be kept: it is skipped unread. The whole-curve
    // boxes above admit pairs whose spans never come near (the orca's 8 pairs
    // at 8.4 ms each, citrus's 178 of 492; none of either within the band).
    let span_cull = switch!(live "BREP_SELF_TOUCH_SPAN_CULL", true);
    let mut spans: Vec<Option<Option<Vec<crate::spatial::Aabb>>>> = vec![None; scans.len()];
    let pair_memo = memo && pair_memo_on();
    let mut curve_keys: Vec<Option<[u64; 2]>> = vec![None; scans.len()];
    for (index, first) in scans.iter().enumerate() {
        for (other, second) in scans.iter().enumerate().skip(index + 1) {
            if shares_a_vertex(first.edge, second.edge) || !boxes_overlap(first, second, band) {
                continue;
            }
            if span_cull {
                for at in [index, other] {
                    if spans[at].is_none() {
                        spans[at] = Some(crate::curve_curve_intersection::span_boxes(&scans[at].curve).ok().flatten());
                    }
                }
                if let (Some(Some(a)), Some(Some(b))) = (&spans[index], &spans[other]) {
                    if !a.iter().any(|p| b.iter().any(|q| p.intersects_upper(*q, band))) {
                        continue;
                    }
                }
            }
            let refine_gate = (REFINE_GATE * (lengths[index] + lengths[other])).max(band);
            let pair_key = pair_memo.then(|| {
                let mut lane = crate::mass_properties::TwoLane::new();
                for at in [index, other] {
                    let [a, b] = *curve_keys[at].get_or_insert_with(|| curve_key(&scans[at].curve));
                    lane.u64(a);
                    lane.u64(b);
                }
                lane.f64(refine_gate);
                lane.finish()
            });
            let served = pair_key.and_then(|key| PAIR_MEMO.with(|memo| memo.borrow().get(&key).copied()));
            PAIR_READS.with(|reads| {
                let [hits, misses] = reads.get();
                reads.set(if served.is_some() { [hits + 1, misses] } else { [hits, misses + 1] });
            });
            let (t, u, distance) = match served {
                Some(reading) => reading,
                None => {
                    let reading = closest_approach(&first.curve, &second.curve, refine_gate)?;
                    if let Some(key) = pair_key {
                        PAIR_MEMO.with(|memo| {
                            let mut memo = memo.borrow_mut();
                            if memo.len() >= crate::reading_memo::capacity() {
                                memo.clear();
                            }
                            memo.insert(key, reading);
                        });
                    }
                    reading
                }
            };
            if distance > band {
                continue;
            }
            if debug {
                eprintln!(
                    "self-touch: operand {} face {} edges {} and {} touch at t={:.9} u={:.9} (gap {:.3e}, band {:.3e})",
                    operand, face_id, first.edge.id, second.edge.id, t, u, distance, band
                );
            }
            splits.push((first.edge, t));
            splits.push((second.edge, u));
            found.push((index, t));
            found.push((other, u));
        }
    }
    if let Some(key) = key {
        TOUCH_MEMO.with(|memo| {
            let mut memo = memo.borrow_mut();
            if memo.len() >= crate::reading_memo::capacity() {
                memo.clear();
            }
            memo.insert(key, found);
        });
    }
    Ok(splits)
}

/// The self-touch splits of a WHOLE SOLID, ready for `apply_edge_splits`.
///
/// `build_imprints` runs the same scan on the two operands it is already
/// pairing, which covers every boolean.  The offset shell fragments solids
/// that no imprint ever describes: its SOURCE goes through `fragment_solid`
/// with an EMPTY imprint, and its offset carriers — whose trims are copied
/// straight from the source faces — deliberately keep their boundaries
/// un-split.  A pinched face therefore reached the arrangement exactly as
/// modelled and `build_loop` refused ("chain N fragmented into an incomplete
/// run"); this is how that lane mints the same vertex.
///
/// Returns one record per split edge, parameters ascending.  An empty result
/// means no face self-touches, and the caller should leave the solid alone.
pub fn self_touch_edge_splits(
    solid: &BrepSolid,
    operand: u8,
    tolerance: f64,
    scale: f64,
) -> Result<Vec<EdgeSplitRecord>, KernelRefusal> {
    let band = self_touch_band(tolerance, scale);
    let edges_by_id = solid
        .edges
        .iter()
        .map(|edge| (edge.id, edge))
        .collect::<HashMap<_, _>>();
    let mut splits: HashMap<u64, Vec<f64>> = HashMap::default();
    for face in solid.shells.iter().flat_map(|shell| &shell.faces) {
        let mut seen = HashSet::default();
        let mut edges = Vec::new();
        for coedge in face
            .loops
            .iter()
            .flat_map(|loop_record| &loop_record.coedges)
        {
            if seen.insert(coedge.edge_id) {
                if let Some(edge) = edges_by_id.get(&coedge.edge_id) {
                    edges.push(*edge);
                }
            }
        }
        for (edge, parameter) in
            face_self_touches(operand, face.id, &edges, band, &edge_subcurve)?
        {
            push_edge_split(
                splits.entry(edge.id).or_default(),
                edge,
                parameter,
                tolerance,
                f64::INFINITY,
            )?;
        }
    }
    let mut records = splits
        .into_iter()
        .map(|(edge_id, mut parameters)| {
            parameters.sort_by(f64::total_cmp);
            EdgeSplitRecord {
                operand,
                edge_id,
                parameters,
            }
        })
        .collect::<Vec<_>>();
    records.sort_by_key(|record| record.edge_id);
    Ok(records)
}

impl ImprintBuilder<'_> {
    /// Split every pair of edges of one face that touch at an interior point
    /// of either (see the module doc).  Both edges receive the split, at the
    /// parameter of their closest approach; `add_edge_split`'s near-endpoint
    /// guard drops the half that already ends at a vertex there.
    pub(super) fn split_self_touching_loops(
        &mut self,
        faces: &[TaggedFace<'_>],
        face_edge_lists: &HashMap<FaceKey, Vec<&EdgeRecord>>,
        subcurve: &dyn Fn(u8, &EdgeRecord) -> Result<NurbsCurve, KernelRefusal>,
    ) -> Result<(), KernelRefusal> {
        let band = self_touch_band(self.tolerance, self.scale);
        for face in faces {
            let operand = face.operand;
            let touches = face_self_touches(
                operand,
                face.face.id,
                &face_edge_lists[&face.key()],
                band,
                &|edge| subcurve(operand, edge),
            )?;
            for (edge, parameter) in touches {
                self.add_edge_split(operand, edge, parameter)?;
            }
        }
        Ok(())
    }
}