brepkit-operations 3.2.17

CAD modeling operations (booleans, fillets, extrusions) for brepkit
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
//! Pipe sweep: sweep a profile along a path with optional scaling guide.
//!
//! Extends the basic sweep by allowing a guide curve that controls
//! profile scaling along the path.

use brepkit_math::nurbs::curve::NurbsCurve;
use brepkit_math::tolerance::Tolerance;
use brepkit_math::vec::Vec3;
use brepkit_topology::Topology;
use brepkit_topology::edge::{Edge, EdgeCurve};
use brepkit_topology::face::{Face, FaceId, FaceSurface};
use brepkit_topology::shell::Shell;
use brepkit_topology::solid::{Solid, SolidId};
use brepkit_topology::vertex::Vertex;
use brepkit_topology::wire::{OrientedEdge, Wire};

use crate::dot_normal_point;

/// Data from sweeping a single inner wire through pipe frames.
struct InnerPipeData {
    ring_verts: Vec<Vec<brepkit_topology::vertex::VertexId>>,
    ring_edges: Vec<Vec<brepkit_topology::edge::EdgeId>>,
    path_edges: Vec<Vec<brepkit_topology::edge::EdgeId>>,
    n: usize,
}

/// Sweep a profile along a path with scaling controlled by a guide curve.
///
/// The guide curve defines how the profile scales at each point along
/// the path. The scale factor at parameter `t` is the distance from
/// the guide curve to the path curve at that parameter, divided by
/// the initial distance (at `t=0`). This means the profile starts at
/// its original size and scales proportionally along the path.
///
/// If no guide is provided, this behaves identically to a regular sweep.
///
/// The profile surface may be planar or curved — only its boundary is used, and
/// the end caps are filled from that boundary (a planar ring gets a `Plane`
/// cap, a non-planar 4-sided ring a bilinear patch).
///
/// # Errors
///
/// Returns an error if the path is too short, the guide curve produces
/// degenerate scaling, or a section boundary is non-planar with more than four
/// edges or with holes (unsupported cap).
#[allow(clippy::too_many_lines)]
pub fn pipe(
    topo: &mut Topology,
    profile: FaceId,
    path: &NurbsCurve,
    guide: Option<&NurbsCurve>,
) -> Result<SolidId, crate::OperationsError> {
    let tol = Tolerance::new();

    if path.control_points().len() < 2 {
        return Err(crate::OperationsError::InvalidInput {
            reason: "pipe path must have at least 2 control points".into(),
        });
    }

    let face_data = topo.face(profile)?;
    let input_wire_id = face_data.outer_wire();
    let inner_wire_ids: Vec<brepkit_topology::wire::WireId> = face_data.inner_wires().to_vec();

    let input_wire = topo.wire(input_wire_id)?;
    let input_oriented: Vec<_> = input_wire.edges().to_vec();
    let n = input_oriented.len();

    if n == 0 {
        return Err(crate::OperationsError::InvalidInput {
            reason: "pipe profile has no edges".into(),
        });
    }

    let mut input_verts = Vec::with_capacity(n);
    for oe in &input_oriented {
        let edge = topo.edge(oe.edge())?;
        let vid = oe.oriented_start(edge);
        input_verts.push(topo.vertex(vid)?.point());
    }

    // Up-hint for the profile frame: the section boundary's own normal (Newell),
    // which works for planar and non-planar profiles alike.
    let mut input_normal = crate::winding::newell_normal(&input_verts)
        .normalize()
        .unwrap_or(Vec3::new(0.0, 0.0, 1.0));

    // Ensure CCW winding relative to path direction at the domain start.
    // CW-wound profiles make `edge_dir.cross(path_dir)` point inward. All
    // parameters map into the curve's own domain: a curve_split sub-path
    // carries a shifted domain, and raw [0,1] sampling would extrapolate
    // its clamped end spans (the compute_frames bug class).
    let (dom_t0, dom_t1) = path.domain();
    let dom = |f: f64| dom_t0 + (dom_t1 - dom_t0) * f;
    let path_tangent_0 = path.tangent(dom_t0)?;
    if crate::winding::ensure_ccw_positions(&mut input_verts, path_tangent_0) {
        input_normal = -input_normal;
    }

    // As-positioned placement (the reference-kernel pipe semantic): a profile
    // perpendicular to the path sweeps from where it lies — offsets are
    // measured from the path start, making ring 0 a pure identity. Edge-on or
    // oblique profiles keep the legacy centroid-on-path translation.
    let reference = match crate::sweep::resolve_placement(
        crate::sweep::ProfilePlacement::AsPositioned,
        input_normal,
        path_tangent_0,
    ) {
        crate::sweep::ProfilePlacement::AsPositioned => path.evaluate(dom_t0),
        crate::sweep::ProfilePlacement::CentroidOnPath => {
            crate::winding::polygon_centroid(&input_verts)
        }
    };

    let num_segments = (path.control_points().len() * 2).max(4);
    let scale_factors = compute_scale_factors(path, guide, num_segments, tol)?;

    let initial_tangent = path_tangent_0;
    let initial_up = orthogonalize(input_normal, initial_tangent);
    let initial_right = initial_tangent.cross(initial_up);

    let mut ring_verts: Vec<Vec<brepkit_topology::vertex::VertexId>> =
        Vec::with_capacity(num_segments + 1);

    for (k, &scale) in scale_factors.iter().enumerate() {
        #[allow(clippy::cast_precision_loss)]
        let t_param = dom((k as f64) / (num_segments as f64));

        let origin = path.evaluate(t_param);
        let tangent = path.tangent(t_param)?;
        let up = orthogonalize(initial_up, tangent);
        let right = tangent.cross(up);

        let ring: Vec<_> = input_verts
            .iter()
            .map(|&pos| {
                let offset = pos - reference;
                let local_r = initial_right.dot(offset) * scale;
                let local_u = initial_up.dot(offset) * scale;
                let local_t = initial_tangent.dot(offset);
                let transformed = origin + right * local_r + up * local_u + tangent * local_t;
                topo.add_vertex(Vertex::new(transformed, tol.linear))
            })
            .collect();
        ring_verts.push(ring);
    }

    let mut inner_pipe_data: Vec<InnerPipeData> = Vec::new();

    for &iw_id in &inner_wire_ids {
        let iw = topo.wire(iw_id)?;
        let iw_oriented: Vec<_> = iw.edges().to_vec();
        let iw_n = iw_oriented.len();

        let mut iw_verts = Vec::with_capacity(iw_n);
        for oe in &iw_oriented {
            let edge = topo.edge(oe.edge())?;
            let vid = if oe.is_forward() {
                edge.start()
            } else {
                edge.end()
            };
            iw_verts.push(topo.vertex(vid)?.point());
        }

        let mut iw_ring_verts: Vec<Vec<brepkit_topology::vertex::VertexId>> =
            Vec::with_capacity(num_segments + 1);

        for (k, &scale) in scale_factors.iter().enumerate() {
            #[allow(clippy::cast_precision_loss)]
            let t_param = dom((k as f64) / (num_segments as f64));
            let origin = path.evaluate(t_param);
            let tangent = path.tangent(t_param)?;
            let up = orthogonalize(initial_up, tangent);
            let right = tangent.cross(up);

            let ring: Vec<_> = iw_verts
                .iter()
                .map(|&pos| {
                    let offset = pos - reference;
                    let local_r = initial_right.dot(offset) * scale;
                    let local_u = initial_up.dot(offset) * scale;
                    let local_t = initial_tangent.dot(offset);
                    let transformed = origin + right * local_r + up * local_u + tangent * local_t;
                    topo.add_vertex(Vertex::new(transformed, tol.linear))
                })
                .collect();
            iw_ring_verts.push(ring);
        }

        let mut iw_ring_edges: Vec<Vec<brepkit_topology::edge::EdgeId>> =
            Vec::with_capacity(num_segments + 1);
        for ring in &iw_ring_verts {
            let edges: Vec<_> = (0..iw_n)
                .map(|i| {
                    let next = (i + 1) % iw_n;
                    topo.add_edge(Edge::new(ring[i], ring[next], EdgeCurve::Line))
                })
                .collect();
            iw_ring_edges.push(edges);
        }

        let mut iw_path_edges: Vec<Vec<brepkit_topology::edge::EdgeId>> =
            Vec::with_capacity(num_segments);
        for seg in 0..num_segments {
            let edges: Vec<_> = (0..iw_n)
                .map(|i| {
                    topo.add_edge(Edge::new(
                        iw_ring_verts[seg][i],
                        iw_ring_verts[seg + 1][i],
                        EdgeCurve::Line,
                    ))
                })
                .collect();
            iw_path_edges.push(edges);
        }

        inner_pipe_data.push(InnerPipeData {
            ring_verts: iw_ring_verts,
            ring_edges: iw_ring_edges,
            path_edges: iw_path_edges,
            n: iw_n,
        });
    }

    let mut ring_edges: Vec<Vec<brepkit_topology::edge::EdgeId>> =
        Vec::with_capacity(num_segments + 1);
    for ring in &ring_verts {
        let edges: Vec<_> = (0..n)
            .map(|i| {
                let next = (i + 1) % n;
                topo.add_edge(Edge::new(ring[i], ring[next], EdgeCurve::Line))
            })
            .collect();
        ring_edges.push(edges);
    }

    let mut path_edges: Vec<Vec<brepkit_topology::edge::EdgeId>> = Vec::with_capacity(num_segments);
    for seg in 0..num_segments {
        let edges: Vec<_> = (0..n)
            .map(|i| {
                topo.add_edge(Edge::new(
                    ring_verts[seg][i],
                    ring_verts[seg + 1][i],
                    EdgeCurve::Line,
                ))
            })
            .collect();
        path_edges.push(edges);
    }

    let mut all_faces = Vec::with_capacity(num_segments * n + 2);

    let mut start_inner_wires = Vec::new();
    for ipd in &inner_pipe_data {
        let iw_edges: Vec<OrientedEdge> = (0..ipd.n)
            .rev()
            .map(|i| OrientedEdge::new(ipd.ring_edges[0][i], false))
            .collect();
        let iw = Wire::new(iw_edges, true).map_err(crate::OperationsError::Topology)?;
        start_inner_wires.push(topo.add_wire(iw));
    }
    let start_verts = crate::cap::ring_point_positions(topo, &ring_verts[0])?;
    let start_outward = crate::cap::outward_normal(&start_verts, -path_tangent_0)?;
    all_faces.push(crate::cap::build_cap_face(
        topo,
        &ring_edges[0],
        start_inner_wires,
        &start_verts,
        start_outward,
        true,
    )?);

    for seg in 0..num_segments {
        for i in 0..n {
            let next_i = (i + 1) % n;
            let p0 = topo.vertex(ring_verts[seg][i])?.point();
            let p1 = topo.vertex(ring_verts[seg][next_i])?.point();
            let p_next = topo.vertex(ring_verts[seg + 1][i])?.point();
            let edge_dir = p1 - p0;
            let path_dir = p_next - p0;
            let side_normal = edge_dir.cross(path_dir).normalize()?;
            let side_d = dot_normal_point(side_normal, p0);

            let side_wire = Wire::new(
                vec![
                    OrientedEdge::new(ring_edges[seg][i], true),
                    OrientedEdge::new(path_edges[seg][next_i], true),
                    OrientedEdge::new(ring_edges[seg + 1][i], false),
                    OrientedEdge::new(path_edges[seg][i], false),
                ],
                true,
            )
            .map_err(crate::OperationsError::Topology)?;

            let side_wire_id = topo.add_wire(side_wire);
            let side_face = topo.add_face(Face::new(
                side_wire_id,
                vec![],
                FaceSurface::Plane {
                    normal: side_normal,
                    d: side_d,
                },
            ));
            all_faces.push(side_face);
        }
    }

    for ipd in &inner_pipe_data {
        for seg in 0..num_segments {
            for i in 0..ipd.n {
                let next_i = (i + 1) % ipd.n;
                let p0 = topo.vertex(ipd.ring_verts[seg][i])?.point();
                let p1 = topo.vertex(ipd.ring_verts[seg][next_i])?.point();
                let p_next = topo.vertex(ipd.ring_verts[seg + 1][i])?.point();
                let edge_dir = p1 - p0;
                let path_dir = p_next - p0;
                let side_normal = path_dir
                    .cross(edge_dir)
                    .normalize()
                    .unwrap_or(Vec3::new(1.0, 0.0, 0.0));
                let side_d = dot_normal_point(side_normal, p0);

                let side_wire = Wire::new(
                    vec![
                        OrientedEdge::new(ipd.path_edges[seg][i], true),
                        OrientedEdge::new(ipd.ring_edges[seg + 1][i], true),
                        OrientedEdge::new(ipd.path_edges[seg][next_i], false),
                        OrientedEdge::new(ipd.ring_edges[seg][i], false),
                    ],
                    true,
                )
                .map_err(crate::OperationsError::Topology)?;

                let side_wire_id = topo.add_wire(side_wire);
                let fid = topo.add_face(Face::new(
                    side_wire_id,
                    vec![],
                    FaceSurface::Plane {
                        normal: side_normal,
                        d: side_d,
                    },
                ));
                all_faces.push(fid);
            }
        }
    }

    let mut end_inner_wires = Vec::new();
    for ipd in &inner_pipe_data {
        let iw_edges: Vec<OrientedEdge> = (0..ipd.n)
            .map(|i| OrientedEdge::new(ipd.ring_edges[num_segments][i], true))
            .collect();
        let iw = Wire::new(iw_edges, true).map_err(crate::OperationsError::Topology)?;
        end_inner_wires.push(topo.add_wire(iw));
    }
    let end_verts = crate::cap::ring_point_positions(topo, &ring_verts[num_segments])?;
    let end_outward = crate::cap::outward_normal(&end_verts, path.tangent(1.0)?)?;
    all_faces.push(crate::cap::build_cap_face(
        topo,
        &ring_edges[num_segments],
        end_inner_wires,
        &end_verts,
        end_outward,
        false,
    )?);

    let shell = Shell::new(all_faces).map_err(crate::OperationsError::Topology)?;
    let shell_id = topo.add_shell(shell);
    Ok(topo.add_solid(Solid::new(shell_id, vec![])))
}

/// Compute scale factors along the path from the guide curve.
///
/// At each sample point, the scale is the ratio of the guide-to-path
/// distance at that point versus the initial distance at the curves'
/// domain starts (guide and path domains are mapped independently).
fn compute_scale_factors(
    path: &NurbsCurve,
    guide: Option<&NurbsCurve>,
    num_segments: usize,
    tol: Tolerance,
) -> Result<Vec<f64>, crate::OperationsError> {
    let Some(guide) = guide else {
        return Ok(vec![1.0; num_segments + 1]);
    };

    let (p_t0, p_t1) = path.domain();
    let (g_t0, g_t1) = guide.domain();
    let initial_dist = (guide.evaluate(g_t0) - path.evaluate(p_t0)).length();
    if initial_dist < tol.linear {
        return Err(crate::OperationsError::InvalidInput {
            reason: "guide curve coincides with path at t=0 (zero initial distance)".into(),
        });
    }

    let mut factors = Vec::with_capacity(num_segments + 1);
    for k in 0..=num_segments {
        #[allow(clippy::cast_precision_loss)]
        let f = (k as f64) / (num_segments as f64);
        let dist = (guide.evaluate(g_t0 + (g_t1 - g_t0) * f)
            - path.evaluate(p_t0 + (p_t1 - p_t0) * f))
        .length();
        factors.push(dist / initial_dist);
    }

    Ok(factors)
}

/// Project `v` to be perpendicular to `tangent`, then normalize.
///
/// Precondition: `tangent` must be unit-length (from `NurbsCurve::tangent`).
fn orthogonalize(v: Vec3, tangent: Vec3) -> Vec3 {
    let projected = v - tangent * tangent.dot(v);
    projected.normalize().unwrap_or_else(|_| {
        let candidate = if tangent.x().abs() < 0.9 {
            Vec3::new(1.0, 0.0, 0.0)
        } else {
            Vec3::new(0.0, 1.0, 0.0)
        };
        let proj2 = candidate - tangent * tangent.dot(candidate);
        proj2.normalize().unwrap_or(Vec3::new(0.0, 0.0, 1.0))
    })
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]

    use brepkit_math::nurbs::curve::NurbsCurve;
    use brepkit_math::vec::Point3;
    use brepkit_topology::Topology;
    use brepkit_topology::test_utils::make_unit_square_face;

    use super::*;

    fn straight_z_path(length: f64) -> NurbsCurve {
        NurbsCurve::new(
            1,
            vec![0.0, 0.0, 1.0, 1.0],
            vec![Point3::new(0.0, 0.0, 0.0), Point3::new(0.0, 0.0, length)],
            vec![1.0, 1.0],
        )
        .unwrap()
    }

    fn diverging_guide(start_dist: f64, end_dist: f64) -> NurbsCurve {
        NurbsCurve::new(
            1,
            vec![0.0, 0.0, 1.0, 1.0],
            vec![
                Point3::new(start_dist, 0.0, 0.0),
                Point3::new(end_dist, 0.0, 2.0),
            ],
            vec![1.0, 1.0],
        )
        .unwrap()
    }

    /// A curve_split sub-path carries a shifted domain; raw [0,1] sampling
    /// would extrapolate its clamped end spans and sweep the wrong length.
    /// Discriminating: fails with `path.evaluate(k / n)` sampling.
    #[test]
    fn pipe_shifted_domain_path_matches_unit_domain() {
        let mut topo = Topology::new();
        let face = make_unit_square_face(&mut topo);
        let path = straight_z_path(2.0);
        let unit = pipe(&mut topo, face, &path, None).unwrap();
        let unit_vol = crate::measure::solid_volume(&topo, unit, 0.05).unwrap();

        // Same geometry, domain [2, 3].
        let shifted_path = NurbsCurve::new(
            1,
            vec![2.0, 2.0, 3.0, 3.0],
            vec![Point3::new(0.0, 0.0, 0.0), Point3::new(0.0, 0.0, 2.0)],
            vec![1.0, 1.0],
        )
        .unwrap();
        let mut topo2 = Topology::new();
        let face2 = make_unit_square_face(&mut topo2);
        let shifted = pipe(&mut topo2, face2, &shifted_path, None).unwrap();
        let shifted_vol = crate::measure::solid_volume(&topo2, shifted, 0.05).unwrap();

        assert!(
            (unit_vol - shifted_vol).abs() < 1e-6,
            "shifted-domain path must sweep identically: unit={unit_vol} shifted={shifted_vol}"
        );
        // Volume alone cannot catch a TRANSLATED tube (raw sampling
        // extrapolates the clamped end spans into a same-length pipe at the
        // wrong position); the solid must actually start at the path start.
        let mut z_min = f64::INFINITY;
        for fid in brepkit_topology::explorer::solid_faces(&topo2, shifted).unwrap() {
            let face = topo2.face(fid).unwrap();
            for oe in topo2.wire(face.outer_wire()).unwrap().edges() {
                let e = topo2.edge(oe.edge()).unwrap();
                for vid in [e.start(), e.end()] {
                    z_min = z_min.min(topo2.vertex(vid).unwrap().point().z());
                }
            }
        }
        assert!(
            z_min.abs() < 1e-9,
            "shifted-domain pipe must start at the path start (z=0), got z_min={z_min}"
        );
    }

    #[test]
    fn pipe_without_guide_is_like_sweep() {
        let mut topo = Topology::new();
        let face = make_unit_square_face(&mut topo);
        let path = straight_z_path(2.0);

        let solid = pipe(&mut topo, face, &path, None).unwrap();

        let s = topo.solid(solid).unwrap();
        let sh = topo.shell(s.outer_shell()).unwrap();
        assert!(!sh.faces().is_empty(), "pipe should produce faces");

        let vol = crate::measure::solid_volume(&topo, solid, 0.1).unwrap();
        assert!(vol > 0.5, "pipe should have positive volume, got {vol}");
    }

    #[test]
    fn pipe_with_expanding_guide() {
        let mut topo = Topology::new();
        let face = make_unit_square_face(&mut topo);
        let path = straight_z_path(2.0);
        let guide = diverging_guide(1.0, 2.0); // Scale doubles along path.

        let solid = pipe(&mut topo, face, &path, Some(&guide)).unwrap();

        let s = topo.solid(solid).unwrap();
        let sh = topo.shell(s.outer_shell()).unwrap();
        assert!(!sh.faces().is_empty());

        let vol = crate::measure::solid_volume(&topo, solid, 0.1).unwrap();
        assert!(
            vol > 0.5,
            "guided pipe should have positive volume, got {vol}"
        );
    }

    #[test]
    fn pipe_with_contracting_guide() {
        let mut topo = Topology::new();
        let face = make_unit_square_face(&mut topo);
        let path = straight_z_path(2.0);
        let guide = diverging_guide(2.0, 1.0); // Scale halves along path.

        let solid = pipe(&mut topo, face, &path, Some(&guide)).unwrap();

        let vol = crate::measure::solid_volume(&topo, solid, 0.1).unwrap();
        assert!(
            vol > 0.1,
            "contracting pipe should have positive volume, got {vol}"
        );
    }

    #[test]
    fn pipe_guide_at_origin_error() {
        let mut topo = Topology::new();
        let face = make_unit_square_face(&mut topo);
        let path = straight_z_path(2.0);

        // Guide starts at the same point as the path (distance = 0).
        let guide = NurbsCurve::new(
            1,
            vec![0.0, 0.0, 1.0, 1.0],
            vec![Point3::new(0.0, 0.0, 0.0), Point3::new(1.0, 0.0, 2.0)],
            vec![1.0, 1.0],
        )
        .unwrap();

        assert!(pipe(&mut topo, face, &path, Some(&guide)).is_err());
    }

    #[test]
    fn pipe_short_path_error() {
        let mut topo = Topology::new();
        let face = make_unit_square_face(&mut topo);

        let path = NurbsCurve::new(
            0,
            vec![0.0, 1.0],
            vec![Point3::new(0.0, 0.0, 0.0)],
            vec![1.0],
        )
        .unwrap();

        assert!(pipe(&mut topo, face, &path, None).is_err());
    }

    #[test]
    fn pipe_cw_profile_produces_correct_solid() {
        let path = straight_z_path(3.0);
        crate::test_helpers::assert_cw_profile_produces_valid_solid(
            |topo, face| pipe(topo, face, &path, None).unwrap(),
            3.0,
            0.05,
        );
    }

    /// Translation invariance for CW-wound pipe.
    #[test]
    fn pipe_cw_profile_translation_invariant() {
        use brepkit_topology::test_utils::make_cw_unit_square_face;

        let mut topo1 = Topology::new();
        let face1 = make_cw_unit_square_face(&mut topo1);
        let path1 = straight_z_path(3.0);
        let solid1 = pipe(&mut topo1, face1, &path1, None).unwrap();
        let vol1 = crate::measure::solid_volume(&topo1, solid1, 0.1).unwrap();

        let mut topo2 = Topology::new();
        let face2 = make_cw_unit_square_face(&mut topo2);
        let path2 = straight_z_path(3.0);
        let solid2 = pipe(&mut topo2, face2, &path2, None).unwrap();
        crate::transform::transform_solid(
            &mut topo2,
            solid2,
            &brepkit_math::mat::Mat4::translation(1000.0, 1000.0, 1000.0),
        )
        .unwrap();
        let vol2 = crate::measure::solid_volume(&topo2, solid2, 0.1).unwrap();

        let rel_err = (vol1 - vol2).abs() / vol1.max(1e-12);
        assert!(
            rel_err < 0.01,
            "CW pipe volumes should match: origin={vol1}, translated={vol2}, \
             rel_err={rel_err:.2e}"
        );
    }

    #[test]
    fn pipe_planar_profile_caps_are_planar() {
        // Regression: a planar profile must still produce flat `Plane` caps
        // (pipe has no straight-extrude fast path, so this exercises the cap
        // code directly).
        let mut topo = Topology::new();
        let profile = make_unit_square_face(&mut topo);
        let path = straight_z_path(3.0);
        let solid = pipe(&mut topo, profile, &path, None).unwrap();
        let sh = topo
            .shell(topo.solid(solid).unwrap().outer_shell())
            .unwrap();
        let planar = sh
            .faces()
            .iter()
            .filter(|&&fid| topo.face(fid).unwrap().surface().is_planar())
            .count();
        assert_eq!(planar, sh.faces().len(), "planar pipe stays all-planar");
    }

    #[test]
    fn pipe_nonplanar_saddle_profile_is_valid_solid() {
        // A non-planar (saddle) profile piped along a straight path: previously
        // rejected by the planar-only gate, now closed with bilinear caps.
        let mut topo = Topology::new();
        let profile = crate::test_helpers::make_saddle_profile(&mut topo, 2.0);
        assert!(!topo.face(profile).unwrap().surface().is_planar());
        let path = straight_z_path(6.0);

        let solid = pipe(&mut topo, profile, &path, None).unwrap();
        let sh = topo
            .shell(topo.solid(solid).unwrap().outer_shell())
            .unwrap();
        let nurbs_caps = sh
            .faces()
            .iter()
            .filter(|&&fid| matches!(topo.face(fid).unwrap().surface(), FaceSurface::Nurbs(_)))
            .count();
        assert_eq!(
            nurbs_caps, 2,
            "non-planar ring caps are bilinear NURBS fills"
        );

        assert!(
            crate::validate::validate_solid(&topo, solid)
                .unwrap()
                .is_valid(),
            "non-planar pipe must be a valid solid"
        );
        let vol = crate::measure::solid_volume(&topo, solid, 0.1).unwrap();
        assert!(
            vol > 85.0 && vol < 110.0,
            "non-planar pipe volume out of expected range, got {vol}"
        );
    }
}