BREP_kernel 0.5.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
//! Flat-pattern (unfold) → 2D vector export (DXF R12 + SVG).
//!
//! A [`SheetTree`] laid out FLAT (every bend opened) is a set of coplanar 2D
//! regions: each flat's outline, the bend-allowance strip filling the gap between
//! a parent flat and its folded child, and every flat's hole loops. This module
//! walks the tree at `fold = 0` — the SAME geometry [`super::evaluate`] would
//! build at `fold = 0.0`, but as pure 2D polylines instead of an exact-BREP solid
//! — and serializes the result to DXF (R12 ASCII) and SVG.
//!
//! ## Why a bespoke 2D walk (not `folded_flat_placements` / `evaluate`)
//!
//! [`super::evaluate::folded_flat_placements`] is hardcoded to `fold = 1.0` (the
//! FOLDED layout — wrong for a flat pattern), and the fold-parametric frame math
//! (`build_bend` / `Frame`) is private to the `evaluate` module. Extracting the
//! outline from the `evaluate(tree, 0)` BREP solid is possible but heavy: that
//! solid keeps coplanar plate/band faces UN-merged, so its "top" is many faces
//! whose union boundary would have to be reconstructed. The 2D layout is far
//! simpler and is derived DIRECTLY from the tree, matching `build_bend`'s
//! `fold = 0` branch exactly:
//!   * outward in-plane direction of a CCW fold edge `a -> b` is `out = (eʸ, -eˣ)`,
//!   * the child flat's frame is `origin = a + out·allowance`, `u = out`, `v = ê`,
//!   * the allowance gap is `allowance` wide; the fold centerline runs down its
//!     middle, at the edge offset outward by `allowance / 2`.
//!
//! ## Output line categories / roles
//!
//! Every emitted line falls into one of three [`LoopRole`]s (DXF layer + colour +
//! linetype and SVG stroke follow from it):
//!   * [`LoopRole::Cut`] — the continuous OUTER cut profile of the flat blank:
//!     every outline edge that is not a fold PLUS the two SIDE edges of each bend
//!     region (`a -> a+out·A`, `b -> b+out·A`). Solid blue-green (layer `CUT`).
//!   * [`LoopRole::Fold`] — one bend CENTERLINE per fold, down the middle of the
//!     allowance gap. Red PHANTOM (layer `FOLD`).
//!   * [`LoopRole::BendInternal`] — the two TANGENT lines per bend (parent-side
//!     `a -> b`, child-side `a+out·A -> b+out·A`): the fold-zone boundaries where
//!     each flat meets the bend. Internal, NOT cut. Cyan DASHED (layer `TANGENT`).
//!
//! A flat's fold-carrying outline edge is therefore emitted as the dashed-cyan
//! parent tangent, never as a solid cut, and a child's seam edge (its attachment
//! to the parent bend) is suppressed from the cut profile too — so a 0° bend
//! (no gap) reads as one continuous blank with no line at the seam.
//!
//! Each non-degenerate bend also gets a short TEXT [`Annotation`] on layer `BEND`
//! stating the fold direction and angle (`"UP 90° R1"` / `"DOWN 45° R2"`), drawn
//! yellow at a span-scaled height. A drawn collar is not developable, so — exactly
//! as the `fold = 0` plate does — only its enlarged hole opening shows (no collar
//! wall region).

use super::tree::{Flat, HoleBendKind, SheetTree};
use crate::NurbsCurve;

/// A 2D segment (its two endpoints) in the common root-plane frame.
type Seg = ([f64; 2], [f64; 2]);

/// What KIND of line a polyline is. Drives the DXF layer / colour / linetype and
/// the SVG stroke style.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LoopRole {
    /// A material boundary that is physically cut: the outer outline, a hole, or
    /// the side edge of a bend region. Solid blue-green.
    Cut,
    /// A bend fold line — the centerline down the allowance gap between two flats.
    /// Red PHANTOM.
    Fold,
    /// An INTERNAL fold-zone boundary: the tangent line where a flat meets a bend
    /// (parent-side or child-side). Inside the part, not cut. Cyan DASHED.
    BendInternal,
}

/// One 2D loop (or open line) of the flat pattern, in the flat plane's own
/// coordinates (the root flat's local frame; +Y up). Closed loops are cut
/// outlines / holes; the open cut runs, tangent lines and fold centerlines reuse
/// this same type.
#[derive(Clone, Debug)]
pub struct Polyline {
    /// Vertices, head-to-tail (a closed loop's closing edge is implicit).
    pub points: Vec<[f64; 2]>,
    /// Closed cut loop vs open line (cut run / tangent / fold centerline).
    pub closed: bool,
    /// Which line category this is.
    pub role: LoopRole,
}

/// A short shop-floor bend label placed at a fold line's midpoint (fold
/// direction + angle, e.g. `"UP 90° R1"`).
#[derive(Clone, Debug)]
pub struct Annotation {
    /// Label anchor (the fold-line midpoint) in the flat plane's own
    /// coordinates (root frame, +Y up) — the SAME frame the [`Polyline`]s use.
    pub pos: [f64; 2],
    /// Text baseline orientation, degrees CCW from +X in the flat-plane frame,
    /// normalized to `(-90, 90]` so the label always reads upright. DXF (Y-up)
    /// consumes this directly as its `50` rotation; SVG (Y-down) negates it.
    pub rotation_deg: f64,
    /// The rendered label (uses a real `°` glyph; the DXF writer rewrites it to
    /// the AutoCAD `%%d` degree code for ANSI-clean AC1009 output).
    pub text: String,
}

/// The full flat-pattern export: cut loops / runs, tangent lines and fold
/// centerlines (all [`Polyline`]s) plus the per-bend [`Annotation`]s, in the one
/// common root-plane frame. Produced by [`build`]; consumed by [`to_dxf`] /
/// [`to_svg`].
#[derive(Clone, Debug)]
pub struct FlatPattern {
    /// Cut outlines / holes (closed), cut runs / tangent lines / fold centerlines
    /// (open), all coplanar.
    pub polylines: Vec<Polyline>,
    /// One bend label per non-degenerate fold.
    pub annotations: Vec<Annotation>,
}

/// A 2D rigid placement: local `(x, y)` maps to `o + x·u + y·v` (all root-plane
/// coordinates). Composes down the flat tree so every flat's outline lands in one
/// common plane.
#[derive(Clone, Copy)]
struct Placement {
    o: [f64; 2],
    u: [f64; 2],
    v: [f64; 2],
}

impl Placement {
    /// The root flat sits in its own local frame unchanged.
    fn identity() -> Self {
        Placement { o: [0.0, 0.0], u: [1.0, 0.0], v: [0.0, 1.0] }
    }
    /// Map a local point into the root plane.
    fn map(&self, p: [f64; 2]) -> [f64; 2] {
        [
            self.o[0] + p[0] * self.u[0] + p[1] * self.v[0],
            self.o[1] + p[0] * self.u[1] + p[1] * self.v[1],
        ]
    }
    /// Map a local DIRECTION (no translation) into the root plane.
    fn map_dir(&self, d: [f64; 2]) -> [f64; 2] {
        [
            d[0] * self.u[0] + d[1] * self.v[0],
            d[0] * self.u[1] + d[1] * self.v[1],
        ]
    }
}

/// The flat pattern of `tree`: the outer cut profile (outline runs + bend side
/// edges) on layer `CUT`, one fold CENTERLINE per bend on layer `FOLD`, the
/// fold-zone TANGENT lines on layer `TANGENT`, and one bend `Annotation` per
/// fold, all laid out coplanar (the `fold = 0` layout).
pub fn build(tree: &SheetTree) -> Result<FlatPattern, String> {
    if tree.thickness <= 0.0 {
        return Err(format!(
            "sheet-metal flat pattern: thickness must be positive, got {}",
            tree.thickness
        ));
    }
    let mut out = FlatPattern { polylines: Vec::new(), annotations: Vec::new() };
    walk(&tree.root, Placement::identity(), tree.thickness, None, &mut out)?;
    if out.polylines.is_empty() {
        return Err("sheet-metal flat pattern: tree produced no geometry".into());
    }
    Ok(out)
}

/// Emit `flat`'s cut profile + holes, then recurse across every bend (outline
/// flanges and straight hole-rim flaps), unfolding each child into the same
/// plane. `seam` — when `flat` is a child — is the root-coord segment along which
/// it attaches to its parent's bend; that edge is a fold-zone boundary (drawn by
/// the parent's `emit_bend` as the child-side tangent), so it is suppressed from
/// this flat's cut profile.
fn walk(
    flat: &Flat,
    place: Placement,
    thickness: f64,
    seam: Option<Seg>,
    out: &mut FlatPattern,
) -> Result<(), String> {
    let n = flat.outline.len();

    // 1. The outer cut profile. Every outline edge is a solid cut EXCEPT one that
    //    carries a bend (its tangent is drawn by `emit_bend`) or one that is this
    //    flat's seam to its parent (the parent's `emit_bend` draws it as the
    //    child-side tangent). Those are dropped here so they never render as a cut.
    emit_outline(flat, place, seam, out)?;

    // 2. Hole loops (outer + islands). A blind pocket's rim is emitted as a cut
    //    loop too — consistent with the `fold = 0` plate, which carries the same
    //    hole; a through/blind distinction is not drawn in v1.
    for hole in &flat.holes {
        out.polylines.push(loop_polyline(&hole.outer, place)?);
        for island in &hole.islands {
            out.polylines.push(loop_polyline(island, place)?);
        }
    }

    // 3 + 4. Bends: outline flanges (on `edges`) and straight hole-rim flaps.
    //        Each emits the two bend SIDE edges (cut), the two TANGENT lines and a
    //        fold CENTERLINE + annotation (unless a 0° bend, which has no
    //        allowance gap) and an unfolded child placement to recurse into. A
    //        drawn collar is not developable — its wall is skipped (its enlarged
    //        opening already shows as a hole loop above), matching the `fold = 0`
    //        plate.
    let mut children: Vec<(&Flat, Placement, Seg)> = Vec::new();
    for index in 0..n {
        let Some(bend) = flat.edges.get(index).and_then(|edge| edge.bend.as_ref()) else {
            continue;
        };
        let a = flat.outline[index];
        let b = flat.outline[(index + 1) % n];
        let allowance = bend.allowance(thickness);
        emit_bend(
            a,
            b,
            allowance,
            bend.angle_deg,
            bend.inside_radius,
            &bend.child,
            place,
            &mut children,
            out,
        );
    }
    for hole_bend in &flat.hole_bends {
        let HoleBendKind::Straight { child } = &hole_bend.kind else {
            continue; // a collar has no developable wall (see the note above)
        };
        let hole = flat.holes.get(hole_bend.hole).ok_or_else(|| {
            format!(
                "sheet-metal flat pattern: hole bend `{}` references missing hole {} on flat `{}`",
                hole_bend.id, hole_bend.hole, flat.id
            )
        })?;
        let curve = hole.outer.get(hole_bend.segment).ok_or_else(|| {
            format!(
                "sheet-metal flat pattern: hole bend `{}` references missing segment {} of hole {}",
                hole_bend.id, hole_bend.segment, hole_bend.hole
            )
        })?;
        let (mut a, mut b) = curve_endpoints_2d(curve)?;
        if hole_bend.reversed {
            std::mem::swap(&mut a, &mut b);
        }
        // Allowance for the transient hole-rim bend — same neutral-fiber formula
        // as `Bend::allowance` (tree.rs), without cloning the child subtree.
        let allowance = bend_allowance(
            hole_bend.angle_deg,
            hole_bend.inside_radius,
            hole_bend.k_factor,
            thickness,
        );
        emit_bend(
            a,
            b,
            allowance,
            hole_bend.angle_deg,
            hole_bend.inside_radius,
            child,
            place,
            &mut children,
            out,
        );
    }

    for (child, child_place, child_seam) in children {
        walk(child, child_place, thickness, Some(child_seam), out)?;
    }
    Ok(())
}

/// Emit `flat`'s outer cut profile. An outline edge that carries a bend or is the
/// flat's seam to its parent is a fold-zone boundary (drawn elsewhere as a
/// tangent), so it is NOT a cut. With no such edge the whole outline is one closed
/// cut loop; otherwise each maximal run of consecutive cut edges is emitted as an
/// open cut chain (the side edges from `emit_bend` bridge the runs into the full
/// closed profile).
fn emit_outline(
    flat: &Flat,
    place: Placement,
    seam: Option<Seg>,
    out: &mut FlatPattern,
) -> Result<(), String> {
    let n = flat.outline.len();
    if n == 0 {
        return Ok(());
    }

    // Which outline edges are fold-zone boundaries (dropped from the cut profile)?
    let suppressed: Vec<bool> = (0..n)
        .map(|i| {
            let is_bend = flat
                .edges
                .get(i)
                .and_then(|edge| edge.bend.as_ref())
                .is_some();
            let is_seam = seam.is_some_and(|s| {
                let pa = place.map(flat.outline[i]);
                let pb = place.map(flat.outline[(i + 1) % n]);
                seg_matches(pa, pb, s)
            });
            is_bend || is_seam
        })
        .collect();

    if suppressed.iter().all(|&drop| !drop) {
        // No fold-zone boundary — the whole outline is one closed cut loop.
        let outline = flat.densified_outline()?;
        out.polylines.push(Polyline {
            points: outline.iter().map(|p| place.map(*p)).collect(),
            closed: true,
            role: LoopRole::Cut,
        });
        return Ok(());
    }

    // Emit each maximal run of consecutive cut edges as an open chain. Start just
    // past the first suppressed edge so a run never wraps across a boundary.
    let start = suppressed.iter().position(|&drop| drop).unwrap();
    let mut run: Vec<[f64; 2]> = Vec::new();
    for step in 0..n {
        let i = (start + 1 + step) % n;
        if suppressed[i] {
            flush_run(&mut run, place, out);
        } else {
            let pts = edge_points(flat, i)?;
            if run.is_empty() {
                run.extend(pts);
            } else {
                // Drop the shared vertex where this edge meets the previous one.
                run.extend(pts.into_iter().skip(1));
            }
        }
    }
    flush_run(&mut run, place, out);
    Ok(())
}

/// Push the accumulated cut run (≥ 2 points) as an open cut polyline and reset it.
fn flush_run(run: &mut Vec<[f64; 2]>, place: Placement, out: &mut FlatPattern) {
    if run.len() >= 2 {
        out.polylines.push(Polyline {
            points: run.iter().map(|p| place.map(*p)).collect(),
            closed: false,
            role: LoopRole::Cut,
        });
    }
    run.clear();
}

/// The flat-local points of outline edge `i`, endpoints INCLUSIVE — a straight
/// edge is its two vertices; a curved edge (keyed in [`Flat::outline_curves`] by
/// its edge id) is 17 samples of its exact curve.
fn edge_points(flat: &Flat, i: usize) -> Result<Vec<[f64; 2]>, String> {
    let n = flat.outline.len();
    let start = flat.outline[i];
    let end = flat.outline[(i + 1) % n];
    match flat
        .edges
        .get(i)
        .and_then(|edge| flat.outline_curves.get(&edge.id))
    {
        Some(curve) => {
            let [t0, t1] = curve.domain()?;
            let mut pts = Vec::with_capacity(17);
            for step in 0..=16 {
                let t = t0 + (t1 - t0) * (step as f64) / 16.0;
                let p = curve.evaluate(t)?;
                pts.push([p.x, p.y]);
            }
            Ok(pts)
        }
        None => Ok(vec![start, end]),
    }
}

/// Whether the (root-coord) segment `pa -> pb` is the same segment as `seg`,
/// regardless of direction.
fn seg_matches(pa: [f64; 2], pb: [f64; 2], seg: Seg) -> bool {
    let (s0, s1) = seg;
    (near(pa, s0) && near(pb, s1)) || (near(pa, s1) && near(pb, s0))
}

/// Two 2D points within a sub-micron tolerance (sheet-metal features are ≥ mm).
fn near(p: [f64; 2], q: [f64; 2]) -> bool {
    (p[0] - q[0]).abs() < 1e-6 && (p[1] - q[1]).abs() < 1e-6
}

/// Emit a bend on the parent-local edge `a -> b`: the two SIDE edges (cut), the
/// two TANGENT lines (parent-side `a -> b` and child-side `a' -> b'`, internal),
/// the fold CENTERLINE + annotation, and push the child's unfolded placement.
/// Mirrors `build_bend`'s `fold = 0` branch: the child lies just past the
/// allowance gap (spacing = the neutral-fiber allowance `A`), so `a' = a + out·A`,
/// `b' = b + out·A`, and the centerline runs down the middle of the gap
/// (`out·A/2`). A 0° bend (no gap) emits none of those lines — the child seam is
/// suppressed on both flats, so the two read as one continuous blank.
fn emit_bend<'a>(
    a: [f64; 2],
    b: [f64; 2],
    allowance: f64,
    angle_deg: f64,
    inside_radius: f64,
    child: &'a Flat,
    place: Placement,
    children: &mut Vec<(&'a Flat, Placement, Seg)>,
    out: &mut FlatPattern,
) {
    let e = normalized([b[0] - a[0], b[1] - a[1]]);
    if e == [0.0, 0.0] {
        return; // degenerate fold edge — nothing to unfold
    }
    let out_dir = [e[1], -e[0]]; // in-plane outward = right of a -> b (CCW)
    let a2 = [a[0] + out_dir[0] * allowance, a[1] + out_dir[1] * allowance];
    let b2 = [b[0] + out_dir[0] * allowance, b[1] + out_dir[1] * allowance];

    if allowance > 1e-9 {
        // The two SIDE edges — the outer cut profile continuing through the bend.
        out.polylines.push(open_line(place.map(a), place.map(a2), LoopRole::Cut));
        out.polylines.push(open_line(place.map(b), place.map(b2), LoopRole::Cut));
        // The two TANGENT lines — the fold-zone boundaries where each flat meets
        // the bend (parent-side `a -> b`, child-side `a' -> b'`). Internal.
        out.polylines
            .push(open_line(place.map(a), place.map(b), LoopRole::BendInternal));
        out.polylines
            .push(open_line(place.map(a2), place.map(b2), LoopRole::BendInternal));
        // The fold CENTERLINE: the edge a -> b shifted outward by half the
        // allowance, i.e. down the middle of the parent/child gap.
        let half = allowance * 0.5;
        let c_a = [a[0] + out_dir[0] * half, a[1] + out_dir[1] * half];
        let c_b = [b[0] + out_dir[0] * half, b[1] + out_dir[1] * half];
        out.polylines
            .push(open_line(place.map(c_a), place.map(c_b), LoopRole::Fold));

        // The bend label at the centerline midpoint, oriented to read along the
        // fold. Direction (root frame) fixes the rotation; the sign of the bend
        // angle fixes UP vs DOWN.
        let mid = place.map([(c_a[0] + c_b[0]) * 0.5, (c_a[1] + c_b[1]) * 0.5]);
        let dir = place.map_dir(e);
        let rotation_deg = readable_angle(dir[1].atan2(dir[0]).to_degrees());
        out.annotations.push(Annotation {
            pos: mid,
            rotation_deg,
            text: bend_label(angle_deg, inside_radius),
        });
    }

    // The child attaches along its near edge `a' -> b'` (root coords). Suppress it
    // from the child's cut profile even for a 0° bend (a' == a, b' == b), so no
    // line is drawn at a flush seam.
    let seam = (place.map(a2), place.map(b2));
    children.push((
        child,
        Placement {
            o: place.map(a2),
            u: place.map_dir(out_dir),
            v: place.map_dir(e),
        },
        seam,
    ));
}

/// A two-point open polyline of the given role.
fn open_line(p0: [f64; 2], p1: [f64; 2], role: LoopRole) -> Polyline {
    Polyline { points: vec![p0, p1], closed: false, role }
}

/// A shop-floor bend label: fold DIRECTION + magnitude (+ inside radius).
/// Direction convention matches [`super::tree::Bend::angle_deg`]: `+` folds
/// toward the flat's −normal side = `"DOWN"`, `−` toward +normal = `"UP"`. Uses a
/// real `°` glyph (the DXF writer rewrites it to `%%d`).
fn bend_label(angle_deg: f64, inside_radius: f64) -> String {
    let dir = if angle_deg < 0.0 { "UP" } else { "DOWN" };
    format!(
        "{dir} {}° R{}",
        trim_number(angle_deg.abs()),
        trim_number(inside_radius)
    )
}

/// Format a length/angle for a label: two decimals, trailing zeros (and a bare
/// dot) stripped — `90.0 -> "90"`, `45.5 -> "45.5"`, `1.25 -> "1.25"`.
fn trim_number(v: f64) -> String {
    let s = format!("{v:.2}");
    let trimmed = s.trim_end_matches('0').trim_end_matches('.');
    trimmed.to_string()
}

/// Fold the text baseline angle (degrees) into `(-90, 90]` — a line has no
/// direction, so this keeps the label upright whichever way the fold edge runs.
fn readable_angle(mut deg: f64) -> f64 {
    deg %= 360.0;
    if deg > 180.0 {
        deg -= 360.0;
    } else if deg <= -180.0 {
        deg += 360.0;
    }
    if deg > 90.0 {
        deg -= 180.0;
    } else if deg <= -90.0 {
        deg += 180.0;
    }
    deg
}


/// Sample a closed exact-curve loop (a hole's outer or island loop, control points
/// at local `z = 0`) into a mapped 2D polyline — 16 points per curve, head-to-tail.
fn loop_polyline(curves: &[NurbsCurve], place: Placement) -> Result<Polyline, String> {
    let mut points = Vec::new();
    for curve in curves {
        let [t0, t1] = curve.domain()?;
        for step in 0..16 {
            let t = t0 + (t1 - t0) * (step as f64) / 16.0;
            let p = curve.evaluate(t)?;
            points.push(place.map([p.x, p.y]));
        }
    }
    if points.len() < 3 {
        return Err("sheet-metal flat pattern: degenerate hole loop".into());
    }
    Ok(Polyline { points, closed: true, role: LoopRole::Cut })
}

/// The two flat-local endpoints of a hole-rim segment (its curve's domain ends).
fn curve_endpoints_2d(curve: &NurbsCurve) -> Result<([f64; 2], [f64; 2]), String> {
    let [t0, t1] = curve.domain()?;
    let p0 = curve.evaluate(t0)?;
    let p1 = curve.evaluate(t1)?;
    Ok(([p0.x, p0.y], [p1.x, p1.y]))
}

/// Neutral-fiber bend allowance `|angle| · (midRadius + (k − 0.5)·t)` — the same
/// formula as `Bend::allowance` (tree.rs), for the transient hole-rim bend.
fn bend_allowance(angle_deg: f64, inside_radius: f64, k_factor: f64, thickness: f64) -> f64 {
    let mid = (inside_radius + thickness * 0.5).max(1e-6);
    let neutral = mid + (k_factor - 0.5) * thickness;
    angle_deg.to_radians().abs() * neutral
}

/// Unit vector, or `[0, 0]` for a (near-)zero input.
fn normalized(d: [f64; 2]) -> [f64; 2] {
    let len = (d[0] * d[0] + d[1] * d[1]).sqrt();
    if len > 1e-12 {
        [d[0] / len, d[1] / len]
    } else {
        [0.0, 0.0]
    }
}

// ===========================================================================
// Writers
// ===========================================================================

/// DXF layer for a loop role.
fn layer(role: LoopRole) -> &'static str {
    match role {
        LoopRole::Cut => "CUT",
        LoopRole::Fold => "FOLD",
        LoopRole::BendInternal => "TANGENT",
    }
}

/// DXF layer for bend annotations (kept off the line layers so the labels can be
/// toggled independently).
const BEND_LAYER: &str = "BEND";

/// The exact blue-green (`#00B3A4`) of the cut profile, as a 24-bit RGB int for
/// the DXF `420` true-colour code (blue-green is not a standard ACI).
const CUT_RGB: u32 = 0x00B3A4;

/// DXF style for a role: `(ACI fallback colour, optional 24-bit true colour,
/// linetype name)`. The ACI keeps old readers coloured; the true colour (when
/// present) gives the exact blue-green to readers that understand `420`.
fn dxf_style(role: LoopRole) -> (i32, Option<u32>, &'static str) {
    match role {
        LoopRole::Cut => (3, Some(CUT_RGB), "CONTINUOUS"), // green fallback + blue-green
        LoopRole::Fold => (1, None, "PHANTOM"),            // red, phantom
        LoopRole::BendInternal => (4, None, "DASHED"),     // cyan, dashed
    }
}

/// The DXF entity colour + linetype group codes for a role (`62` before `420` so
/// strict R12 readers still get an ACI).
fn dxf_color_ltype(role: LoopRole) -> String {
    let (aci, tc, lt) = dxf_style(role);
    let mut s = format!("6\n{lt}\n62\n{aci}\n");
    if let Some(c) = tc {
        s.push_str(&format!("420\n{c}\n"));
    }
    s
}

/// Axis-aligned bounds `[min_x, min_y, max_x, max_y]` over every polyline point,
/// or `None` when there is no geometry. Drives the SVG viewport and the text
/// height (a fraction of the pattern span) for both writers.
fn bounds(polys: &[Polyline]) -> Option<[f64; 4]> {
    let mut min_x = f64::INFINITY;
    let mut min_y = f64::INFINITY;
    let mut max_x = f64::NEG_INFINITY;
    let mut max_y = f64::NEG_INFINITY;
    for poly in polys {
        for p in &poly.points {
            min_x = min_x.min(p[0]);
            min_y = min_y.min(p[1]);
            max_x = max_x.max(p[0]);
            max_y = max_y.max(p[1]);
        }
    }
    if min_x.is_finite() {
        Some([min_x, min_y, max_x, max_y])
    } else {
        None
    }
}

/// Annotation text height, ~5% of the pattern span (min 1 unit), from its bounds.
fn text_height(b: &[f64; 4]) -> f64 {
    let span = (b[2] - b[0]).max(b[3] - b[1]).max(1.0);
    (span * 0.05).max(1.0)
}

/// Serialize a flat pattern to a minimal DXF R12 (AC1009) ASCII document: a
/// HEADER (`$ACADVER` + a span-scaled `$LTSCALE`), a TABLES section defining the
/// `CONTINUOUS`/`DASHED`/`PHANTOM` linetypes, and an ENTITIES section of
/// heavyweight `POLYLINE`/`VERTEX`/`SEQEND` loops (LWPOLYLINE does not exist in
/// R12) plus a plain `TEXT` entity per bend (no MTEXT). Cut lines go on layer
/// `CUT` (blue-green), fold centerlines on `FOLD` (red phantom), tangent lines on
/// `TANGENT` (cyan dashed), bend labels on `BEND` (yellow); readers auto-create
/// referenced layers.
pub fn to_dxf(fp: &FlatPattern) -> String {
    let bbox = bounds(&fp.polylines);
    let span = bbox
        .map(|b| (b[2] - b[0]).max(b[3] - b[1]).max(1.0))
        .unwrap_or(1.0);
    let ltscale = (span * 0.01).max(0.1);
    let height = bbox.map(|b| text_height(&b)).unwrap_or(1.0);

    let mut s = String::new();
    // HEADER — version + a span-scaled global linetype scale so the dashed /
    // phantom patterns read at any part size.
    s.push_str("0\nSECTION\n2\nHEADER\n9\n$ACADVER\n1\nAC1009\n");
    s.push_str(&format!("9\n$LTSCALE\n40\n{ltscale:.3}\n"));
    s.push_str("0\nENDSEC\n");
    // TABLES — the linetypes the entities reference (49: + = dash, − = gap).
    s.push_str("0\nSECTION\n2\nTABLES\n0\nTABLE\n2\nLTYPE\n70\n3\n");
    s.push_str("0\nLTYPE\n2\nCONTINUOUS\n70\n64\n3\nSolid line\n72\n65\n73\n0\n40\n0.0\n");
    s.push_str(
        "0\nLTYPE\n2\nDASHED\n70\n64\n3\nDashed __ __ __\n72\n65\n73\n2\n40\n0.75\n49\n0.5\n49\n-0.25\n",
    );
    s.push_str(
        "0\nLTYPE\n2\nPHANTOM\n70\n64\n3\nPhantom ___ _ _ ___\n72\n65\n73\n6\n40\n2.5\n49\n1.25\n49\n-0.25\n49\n0.25\n49\n-0.25\n49\n0.25\n49\n-0.25\n",
    );
    s.push_str("0\nENDTAB\n0\nENDSEC\n");
    // ENTITIES.
    s.push_str("0\nSECTION\n2\nENTITIES\n");
    for poly in &fp.polylines {
        let layer = layer(poly.role);
        s.push_str("0\nPOLYLINE\n8\n");
        s.push_str(layer);
        s.push('\n');
        s.push_str(&dxf_color_ltype(poly.role));
        // 66/1 = vertices follow; 70 bit 1 = closed polyline.
        s.push_str("66\n1\n70\n");
        s.push_str(if poly.closed { "1" } else { "0" });
        s.push('\n');
        for p in &poly.points {
            s.push_str("0\nVERTEX\n8\n");
            s.push_str(layer);
            s.push_str(&format!("\n10\n{:.6}\n20\n{:.6}\n30\n0.0\n", p[0], p[1]));
        }
        s.push_str("0\nSEQEND\n");
    }
    // Bend labels as plain R12 TEXT (yellow, ACI 2), centered on the fold midpoint
    // (72/1 with the alignment point 11/21) and rotated (50) to read along the
    // fold. DXF is Y-up like the source data, so `rotation_deg` and the point map
    // through directly. The `°` glyph is rewritten to the AutoCAD `%%d` code.
    for ann in &fp.annotations {
        let text = ann.text.replace('°', "%%d");
        s.push_str("0\nTEXT\n8\n");
        s.push_str(BEND_LAYER);
        s.push_str("\n62\n2\n");
        s.push_str(&format!("10\n{:.6}\n20\n{:.6}\n30\n0.0\n", ann.pos[0], ann.pos[1]));
        s.push_str(&format!("40\n{height:.6}\n1\n{text}\n"));
        s.push_str(&format!("50\n{:.6}\n72\n1\n", ann.rotation_deg));
        s.push_str(&format!("11\n{:.6}\n21\n{:.6}\n31\n0.0\n", ann.pos[0], ann.pos[1]));
    }
    s.push_str("0\nENDSEC\n0\nEOF\n");
    s
}

/// Serialize a flat pattern to a self-contained SVG sized to the pattern bounds
/// (millimetre units), Y-flipped so the +Y-up flat plane reads right-side-up, over
/// a dark CAD canvas so the coloured layers read. Cut lines draw solid blue-green;
/// fold centerlines draw red with a phantom (long-short-short) dash; tangent lines
/// draw cyan dashed; each bend gets a yellow `<text>` label rotated to read along
/// its fold line.
pub fn to_svg(fp: &FlatPattern) -> String {
    let Some(b @ [min_x, min_y, max_x, max_y]) = bounds(&fp.polylines) else {
        // No geometry — an empty but valid SVG.
        return String::from(
            "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"0mm\" height=\"0mm\" viewBox=\"0 0 0 0\"></svg>\n",
        );
    };
    let span = (max_x - min_x).max(max_y - min_y).max(1.0);
    let margin = span * 0.02;
    let width = (max_x - min_x) + 2.0 * margin;
    let height = (max_y - min_y) + 2.0 * margin;
    let stroke = (span * 0.003).max(0.05);
    let dash = (span * 0.02).max(0.5);
    let font = text_height(&b);

    // Span-scaled dash patterns. Phantom = long, gap, short, gap, short, gap.
    let long = dash * 2.0;
    let short = dash * 0.5;
    let phantom =
        format!("{long:.6} {dash:.6} {short:.6} {dash:.6} {short:.6} {dash:.6}");
    let dashed = format!("{:.6} {:.6}", dash, dash * 0.6);

    // Local -> SVG: shift into the padded box and flip Y (data +Y up -> SVG down).
    let sx = |x: f64| x - min_x + margin;
    let sy = |y: f64| max_y - y + margin;

    let mut s = String::new();
    s.push_str(&format!(
        "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{width:.6}mm\" height=\"{height:.6}mm\" viewBox=\"0 0 {width:.6} {height:.6}\">\n"
    ));
    // Dark canvas so the blue-green / red / cyan / yellow layers are legible.
    s.push_str(&format!(
        "<rect x=\"0\" y=\"0\" width=\"{width:.6}\" height=\"{height:.6}\" fill=\"#151515\"/>\n"
    ));
    s.push_str(&format!("<g fill=\"none\" stroke-width=\"{stroke:.6}\">\n"));
    for poly in &fp.polylines {
        if poly.points.is_empty() {
            continue;
        }
        let mut d = String::new();
        for (i, p) in poly.points.iter().enumerate() {
            d.push_str(if i == 0 { "M " } else { " L " });
            d.push_str(&format!("{:.6} {:.6}", sx(p[0]), sy(p[1])));
        }
        if poly.closed {
            d.push_str(" Z");
        }
        match poly.role {
            LoopRole::Cut => {
                s.push_str(&format!("<path d=\"{d}\" stroke=\"#00b3a4\"/>\n"));
            }
            LoopRole::Fold => {
                s.push_str(&format!(
                    "<path d=\"{d}\" stroke=\"#ff4d4d\" stroke-dasharray=\"{phantom}\"/>\n"
                ));
            }
            LoopRole::BendInternal => {
                s.push_str(&format!(
                    "<path d=\"{d}\" stroke=\"#22ccff\" stroke-dasharray=\"{dashed}\"/>\n"
                ));
            }
        }
    }
    // Bend labels. Translate to the (Y-flipped) midpoint, then rotate: the flip
    // negates the data-frame angle (SVG rotate() is CW / Y-down). `text-anchor`
    // center + a small upward `dy` seats the label just above the fold line.
    // `<text>` needs its own `fill` (the group is `fill="none"`); yellow.
    for ann in &fp.annotations {
        s.push_str(&format!(
            "<text transform=\"translate({:.6} {:.6}) rotate({:.6})\" \
text-anchor=\"middle\" dy=\"{:.6}\" fill=\"#ffd400\" font-size=\"{font:.6}\">{}</text>\n",
            sx(ann.pos[0]),
            sy(ann.pos[1]),
            -ann.rotation_deg,
            -font * 0.3,
            ann.text,
        ));
    }
    s.push_str("</g>\n</svg>\n");
    s
}

// BREP private tests: 6046ba4249ba7320