Skip to main content

brep_kernel/construction/
revolve_topology.rs

1use crate::sweep_topology::{curve_to_plane_parameters, parameter_line};
2use crate::topology::{
3    BrepSolid, CoedgeRecord, EdgeRecord, FaceRecord, LoopRecord, ShellRecord, VertexRecord,
4};
5use crate::{make_line, make_plane, make_revolution, NurbsCurve, NurbsSurface, Vec3, Vec4};
6
7const TOLERANCE: f64 = 1e-5;
8
9fn next_id(counter: &mut u64) -> u64 {
10    let id = *counter;
11    *counter += 1;
12    id
13}
14
15fn rotate_point(point: Vec3, origin: Vec3, axis: Vec3, angle: f64) -> Vec3 {
16    let vector = point.sub(origin);
17    let cosine = angle.cos();
18    let sine = angle.sin();
19    origin
20        .add(vector.scale(cosine))
21        .add(axis.cross(vector).scale(sine))
22        .add(axis.scale(axis.dot(vector) * (1.0 - cosine)))
23}
24
25fn rotate_curve(
26    curve: &NurbsCurve,
27    origin: Vec3,
28    axis: Vec3,
29    angle: f64,
30) -> Result<NurbsCurve, String> {
31    NurbsCurve::new(
32        curve.degree,
33        curve.knots.clone(),
34        curve
35            .control_points
36            .iter()
37            .map(|point| {
38                let euclidean = Vec3::new(point.x / point.w, point.y / point.w, point.z / point.w);
39                Vec4::from_point(rotate_point(euclidean, origin, axis, angle), point.w)
40            })
41            .collect(),
42    )
43}
44
45fn signed_profile_area(
46    curves: &[NurbsCurve],
47    origin: Vec3,
48    radial: Vec3,
49    axis: Vec3,
50) -> Result<f64, String> {
51    let mut area = 0.0;
52    for curve in curves {
53        let [start, end] = curve.domain()?;
54        let mut previous = curve.evaluate(start)?;
55        for index in 1..=64 {
56            let point = curve.evaluate(start + (end - start) * index as f64 / 64.0)?;
57            let a = previous.sub(origin);
58            let b = point.sub(origin);
59            area += 0.5 * (a.dot(radial) * b.dot(axis) - b.dot(radial) * a.dot(axis));
60            previous = point;
61        }
62    }
63    Ok(area)
64}
65
66struct RevolveProfile {
67    curves: Vec<NurbsCurve>,
68    points: Vec<Vec3>,
69    axis_points: Vec<bool>,
70    axis_curves: Vec<bool>,
71    /// For each entry of `curves`, true when it is a straight segment lying
72    /// (within tolerance) in a plane PERPENDICULAR to the axis — a radial
73    /// generatrix.  Revolving one sweeps a flat disk/annulus sector, so the
74    /// partial-revolve side face is genuinely PLANAR and is built with a
75    /// `make_plane` carrier (not `make_revolution`); otherwise the shallow
76    /// "cone" carrier is misrecognized (conical, not planar) and its boolean
77    /// intersections fall to the marcher and shed slivers.
78    radial_curves: Vec<bool>,
79    radial: Vec3,
80    axis: Vec3,
81    /// For each entry of `curves`, the index of the INPUT curve it came
82    /// from.  Winding normalization reverses the loop and axis-lying
83    /// curves produce no side face, so callers cannot recover this mapping
84    /// from the emitted face order — side-face names must travel through
85    /// it (the extrude naming permutation bug, revolve edition).
86    input_indices: Vec<usize>,
87}
88
89/// A straight two-point segment: moving either end keeps it straight.
90fn is_segment(curve: &NurbsCurve) -> bool {
91    curve.degree == 1 && curve.control_points.len() == 2
92}
93
94/// True when the curve interpolates its first (`at_start`) or last control
95/// point, so that control point IS the curve's end.
96fn clamped_at(curve: &NurbsCurve, at_start: bool) -> bool {
97    let order = curve.degree + 1;
98    let knots = &curve.knots;
99    if at_start {
100        knots[..order].iter().all(|knot| *knot == knots[0])
101    } else {
102        knots[knots.len() - order..].iter().all(|knot| *knot == knots[knots.len() - 1])
103    }
104}
105
106fn end_control_point(curve: &NurbsCurve, at_start: bool) -> Vec3 {
107    let point = if at_start {
108        curve.control_points[0]
109    } else {
110        curve.control_points[curve.control_points.len() - 1]
111    };
112    Vec3::new(point.x / point.w, point.y / point.w, point.z / point.w)
113}
114
115/// Move a clamped end to `target`, its weight kept.
116fn move_end(curve: &mut NurbsCurve, at_start: bool, target: Vec3) -> Result<(), String> {
117    let mut control_points = curve.control_points.clone();
118    let index = if at_start { 0 } else { control_points.len() - 1 };
119    control_points[index] = Vec4::from_point(target, control_points[index].w);
120    *curve = NurbsCurve::new(curve.degree, curve.knots.clone(), control_points)?;
121    Ok(())
122}
123
124/// Make every junction of the closed chain exact. Where a straight segment
125/// meets a curve that is not one, the segment's end moves (it stays straight
126/// and the curve stays exact); otherwise the next curve's start moves onto
127/// the previous curve's end, the sketch chainer's convention. Junctions whose
128/// ends are not clamped are left as they are. Returns whether anything moved.
129fn weld_junctions(curves: &mut [NurbsCurve]) -> Result<bool, String> {
130    let count = curves.len();
131    let mut moved = false;
132    for index in 0..count {
133        let next = (index + 1) % count;
134        if !clamped_at(&curves[index], false) || !clamped_at(&curves[next], true) {
135            continue;
136        }
137        let end = end_control_point(&curves[index], false);
138        let start = end_control_point(&curves[next], true);
139        if end.x == start.x && end.y == start.y && end.z == start.z {
140            continue;
141        }
142        if is_segment(&curves[index]) && !is_segment(&curves[next]) {
143            move_end(&mut curves[index], false, start)?;
144        } else {
145            move_end(&mut curves[next], true, end)?;
146        }
147        moved = true;
148    }
149    Ok(moved)
150}
151
152/// A partial revolve builds a radial generatrix's side face on a plane
153/// perpendicular to the axis (`radial_plane_face`), while its edges are the
154/// profile's own curves. `is_straight_radial` admits a slope, so without this
155/// the plane sits between the segment's two axial coordinates and every edge
156/// of the face stands off it by up to half their difference. Each run of
157/// consecutive radial segments is given one axial coordinate: that of the
158/// run's end vertices whose outer neighbour is not a straight segment (moving
159/// such a vertex would bend the curve), their mean if there are two, and the
160/// mean over the run's vertices when every neighbour is straight. Returns
161/// whether anything moved.
162fn flatten_radial_generatrices(
163    curves: &mut [NurbsCurve],
164    radial: &[bool],
165    axis_point: Vec3,
166    axis: Vec3,
167) -> Result<bool, String> {
168    let count = curves.len();
169    let Some(first) = (0..count).find(|index| !radial[*index]) else {
170        return Ok(false);
171    };
172    let axial = |point: Vec3| point.sub(axis_point).dot(axis);
173    let mut moved = false;
174    let mut offset = 1;
175    while offset <= count {
176        let begin = (first + offset) % count;
177        if !radial[begin] {
178            offset += 1;
179            continue;
180        }
181        let mut run = vec![begin];
182        while radial[(run[run.len() - 1] + 1) % count] {
183            run.push((run[run.len() - 1] + 1) % count);
184        }
185        offset += run.len();
186        // Vertex k of the run is the start of run[k]; the last is the end of the run.
187        let last = run[run.len() - 1];
188        if run.iter().any(|index| !clamped_at(&curves[*index], true) || !clamped_at(&curves[*index], false)) {
189            continue;
190        }
191        let mut vertices: Vec<Vec3> = run.iter().map(|index| end_control_point(&curves[*index], true)).collect();
192        vertices.push(end_control_point(&curves[last], false));
193        let before = (begin + count - 1) % count;
194        let after = (last + 1) % count;
195        let mut fixed = Vec::new();
196        if !is_segment(&curves[before]) {
197            fixed.push(axial(vertices[0]));
198        }
199        if !is_segment(&curves[after]) {
200            fixed.push(axial(vertices[vertices.len() - 1]));
201        }
202        let pool: Vec<f64> = if fixed.is_empty() { vertices.iter().map(|point| axial(*point)).collect() } else { fixed };
203        let target = pool.iter().sum::<f64>() / pool.len() as f64;
204        for (k, vertex) in vertices.iter().enumerate() {
205            let shift = target - axial(*vertex);
206            if shift == 0.0 {
207                continue;
208            }
209            let point = vertex.add(axis.scale(shift));
210            let (incoming, outgoing) = if k == 0 {
211                (before, run[0])
212            } else if k == run.len() {
213                (last, after)
214            } else {
215                (run[k - 1], run[k])
216            };
217            if clamped_at(&curves[incoming], false) {
218                move_end(&mut curves[incoming], false, point)?;
219            }
220            if clamped_at(&curves[outgoing], true) {
221                move_end(&mut curves[outgoing], true, point)?;
222            }
223            moved = true;
224        }
225    }
226    Ok(moved)
227}
228
229fn prepare_profile(
230    input: &[NurbsCurve],
231    axis_point: Vec3,
232    axis_direction: Vec3,
233    flatten_radial: bool,
234) -> Result<RevolveProfile, String> {
235    if input.len() < 2 {
236        return Err("profile needs at least 2 curves".into());
237    }
238    let axis = axis_direction.normalized()?;
239    let mut curves: Vec<NurbsCurve> = input
240        .iter()
241        .map(|curve| {
242            NurbsCurve::new(
243                curve.degree,
244                curve.knots.clone(),
245                curve.control_points.clone(),
246            )
247        })
248        .collect::<Result<_, _>>()?;
249    let closure_points = |curves: &[NurbsCurve]| -> Result<Vec<Vec3>, String> {
250        let mut points = Vec::with_capacity(curves.len());
251        for index in 0..curves.len() {
252            let [start, end] = curves[index].domain()?;
253            let next_start = curves[(index + 1) % curves.len()].domain()?[0];
254            let point = curves[index].evaluate(start)?;
255            if curves[index]
256                .evaluate(end)?
257                .sub(curves[(index + 1) % curves.len()].evaluate(next_start)?)
258                .length()
259                > TOLERANCE
260            {
261                return Err(format!("profile not closed at curve {index}"));
262            }
263            points.push(point);
264        }
265        Ok(points)
266    };
267    let mut points = closure_points(&curves)?;
268
269    let radial_distance = |point: Vec3| {
270        let delta = point.sub(axis_point);
271        delta.sub(axis.scale(delta.dot(axis))).length()
272    };
273    let mut radial = None;
274    'outer: for curve in &curves {
275        let [start, end] = curve.domain()?;
276        for index in 0..=24 {
277            let point = curve.evaluate(start + (end - start) * index as f64 / 24.0)?;
278            let delta = point.sub(axis_point);
279            let candidate = delta.sub(axis.scale(delta.dot(axis)));
280            if candidate.length() > TOLERANCE * 100.0 {
281                radial = Some(candidate.normalized()?);
282                break 'outer;
283            }
284        }
285    }
286    let radial = radial.ok_or_else(|| "revolveSolid: profile touches the axis".to_string())?;
287    let curve_on_axis = |curve: &NurbsCurve| -> Result<bool, String> {
288        let [start, end] = curve.domain()?;
289        for index in 0..=8 {
290            if radial_distance(curve.evaluate(start + (end - start) * index as f64 / 8.0)?)
291                > TOLERANCE * 100.0
292            {
293                return Ok(false);
294            }
295        }
296        Ok(true)
297    };
298    for curve in &curves {
299        let [start, end] = curve.domain()?;
300        let on_axis = curve_on_axis(curve)?;
301        for index in 0..=24 {
302            let point = curve.evaluate(start + (end - start) * index as f64 / 24.0)?;
303            let delta = point.sub(axis_point);
304            let in_plane_radial = delta.dot(radial);
305            let off_plane = delta
306                .sub(axis.scale(delta.dot(axis)))
307                .sub(radial.scale(in_plane_radial))
308                .length();
309            if off_plane > TOLERANCE * 100.0 {
310                return Err("revolveSolid: profile not in a half-plane containing the axis".into());
311            }
312            if in_plane_radial < -TOLERANCE * 100.0 {
313                return Err("revolveSolid: profile crosses the revolve axis".into());
314            }
315            if in_plane_radial < TOLERANCE * 100.0 && index != 0 && index != 24 && !on_axis {
316                return Err(
317                    "revolveSolid: profile may only touch the axis at boundary vertices or axis edges"
318                        .into(),
319                );
320            }
321        }
322    }
323    let mut input_indices: Vec<usize> = (0..curves.len()).collect();
324    if signed_profile_area(&curves, axis_point, radial, axis)? < 0.0 {
325        curves = curves
326            .iter()
327            .rev()
328            .map(NurbsCurve::reversed)
329            .collect::<Result<_, _>>()?;
330        points = closure_points(&curves)?;
331        input_indices.reverse();
332    }
333    let mut axis_points: Vec<bool> = points
334        .iter()
335        .map(|point| radial_distance(*point) <= TOLERANCE * 100.0)
336        .collect();
337    if axis_points.iter().any(|flag| *flag) {
338        let snapped: Vec<Vec3> = points
339            .iter()
340            .enumerate()
341            .map(|(index, point)| {
342                if axis_points[index] {
343                    axis_point.add(axis.scale(point.sub(axis_point).dot(axis)))
344                } else {
345                    *point
346                }
347            })
348            .collect();
349        for index in 0..curves.len() {
350            let next = (index + 1) % curves.len();
351            if !axis_points[index] && !axis_points[next] {
352                continue;
353            }
354            let mut control_points = curves[index].control_points.clone();
355            if axis_points[index] {
356                let weight = control_points[0].w;
357                control_points[0] = Vec4::from_point(snapped[index], weight);
358            }
359            if axis_points[next] {
360                let last = control_points.len() - 1;
361                let weight = control_points[last].w;
362                control_points[last] = Vec4::from_point(snapped[next], weight);
363            }
364            curves[index] = NurbsCurve::new(
365                curves[index].degree,
366                curves[index].knots.clone(),
367                control_points,
368            )?;
369        }
370        points = closure_points(&curves)?;
371        axis_points = points
372            .iter()
373            .map(|point| radial_distance(*point) <= TOLERANCE * 100.0)
374            .collect();
375    }
376    // The closure test accepts a junction within TOLERANCE, and every builder
377    // below reads a junction from both sides: face i trims at curve i's end,
378    // face i+1 and the junction edge at curve i+1's start. A gap kept here is a
379    // trim gap along the whole junction rim, and the shell does not close.
380    // Hatch: `BREP_REVOLVE_WELD=0`.
381    if std::env::var("BREP_REVOLVE_WELD").as_deref() != Ok("0") && weld_junctions(&mut curves)? {
382        points = closure_points(&curves)?;
383    }
384    let axis_curves = curves
385        .iter()
386        .map(curve_on_axis)
387        .collect::<Result<Vec<_>, _>>()?;
388    // A radial generatrix is a straight segment whose direction is
389    // perpendicular to the axis: its two endpoints share an axial coordinate,
390    // so the revolved patch is a flat disk/annulus sector (a plane), not a
391    // cone.  Curves that lie ON the axis produce no side face and are excluded.
392    let radial_of = |curves: &[NurbsCurve]| {
393        curves
394            .iter()
395            .enumerate()
396            .map(|(index, curve)| {
397                if axis_curves[index] {
398                    return Ok(false);
399                }
400                Ok(is_straight_radial(curve, axis)?)
401            })
402            .collect::<Result<Vec<_>, String>>()
403    };
404    let mut radial_curves = radial_of(&curves)?;
405    // Hatch: `BREP_REVOLVE_FLAT_RADIAL=0`.
406    let flatten_radial = flatten_radial && std::env::var("BREP_REVOLVE_FLAT_RADIAL").as_deref() != Ok("0");
407    if flatten_radial && flatten_radial_generatrices(&mut curves, &radial_curves, axis_point, axis)? {
408        points = closure_points(&curves)?;
409        radial_curves = radial_of(&curves)?;
410    }
411    Ok(RevolveProfile {
412        curves,
413        points,
414        axis_points,
415        axis_curves,
416        radial_curves,
417        radial,
418        axis,
419        input_indices,
420    })
421}
422
423/// True when `curve` is a straight, unit-weight line segment whose direction is
424/// perpendicular to `axis` (a radial generatrix).  The threshold is a small
425/// relative slope (~0.057 deg from perpendicular): it captures sketch-solver
426/// noise on an intended flat cap without swallowing a genuine shallow cone.
427fn is_straight_radial(curve: &NurbsCurve, axis: Vec3) -> Result<bool, String> {
428    if curve.degree != 1 || curve.control_points.len() != 2 {
429        return Ok(false);
430    }
431    let [start, end] = curve.domain()?;
432    let p0 = curve.evaluate(start)?;
433    let p1 = curve.evaluate(end)?;
434    let direction = p1.sub(p0);
435    let length = direction.length();
436    if length <= TOLERANCE {
437        return Ok(false);
438    }
439    Ok(direction.dot(axis).abs() <= 1e-3 * length)
440}
441
442fn junction_curve(
443    index: usize,
444    surfaces: &[Option<NurbsSurface>],
445    points: &[Vec3],
446) -> Result<NurbsCurve, String> {
447    if let Some(surface) = &surfaces[index] {
448        return surface.iso_curve_v(surface.knots_v[surface.degree_v]);
449    }
450    let previous = (index + surfaces.len() - 1) % surfaces.len();
451    if let Some(surface) = &surfaces[previous] {
452        return surface.iso_curve_v(surface.knots_v[surface.knots_v.len() - 1 - surface.degree_v]);
453    }
454    make_line(points[index], points[index])
455}
456
457/// Build the PLANAR side face of a partial revolve whose generatrix is radial
458/// (perpendicular to the axis): a flat disk/annulus sector.  The face carrier
459/// is an affine `make_plane` — so it recognizes as a `Plane` (not a shallow
460/// "cone"), reads planar in the app, and its boolean intersections take the
461/// exact analytic path instead of the sliver-prone marcher.  The bounding
462/// loop reuses the same edges as the revolution branch; only the surface and
463/// its pcurves differ (the edges are projected into the plane's frame).
464#[allow(clippy::too_many_arguments)]
465fn radial_plane_face(
466    profile: &RevolveProfile,
467    index: usize,
468    count: usize,
469    origin: Vec3,
470    revolution: &NurbsSurface,
471    arc_curves: &[NurbsCurve],
472    end_curves: &[NurbsCurve],
473    arc_ids: &[u64],
474    start_edge_ids: &[u64],
475    end_edge_ids: &[u64],
476    counter: &mut u64,
477    name: Option<String>,
478) -> Result<FaceRecord, String> {
479    let axis = profile.axis;
480    let a_start = profile.points[index].sub(origin).dot(axis);
481    let a_end = profile.points[(index + 1) % count].sub(origin).dot(axis);
482    let plane_center = origin.add(axis.scale(0.5 * (a_start + a_end)));
483    let e1 = profile.radial;
484    let e2 = axis.cross(e1);
485
486    let next = (index + 1) % count;
487    let boundary: [(NurbsCurve, u64, bool); 4] = [
488        (arc_curves[index].clone(), arc_ids[index], true),
489        (end_curves[index].clone(), end_edge_ids[index], true),
490        (arc_curves[next].clone(), arc_ids[next], false),
491        (profile.curves[index].clone(), start_edge_ids[index], false),
492    ];
493
494    let mut min1 = f64::INFINITY;
495    let mut max1 = f64::NEG_INFINITY;
496    let mut min2 = f64::INFINITY;
497    let mut max2 = f64::NEG_INFINITY;
498    for (curve, _, _) in &boundary {
499        let [s, e] = curve.domain()?;
500        for k in 0..=16 {
501            let point = curve.evaluate(s + (e - s) * k as f64 / 16.0)?;
502            let d = point.sub(plane_center);
503            let c1 = d.dot(e1);
504            let c2 = d.dot(e2);
505            min1 = min1.min(c1);
506            max1 = max1.max(c1);
507            min2 = min2.min(c2);
508            max2 = max2.max(c2);
509        }
510    }
511    let padding = (max1 - min1).max(max2 - min2) * 0.05 + 1e-6;
512    let width = max1 - min1 + 2.0 * padding;
513    let height = max2 - min2 + 2.0 * padding;
514    let corner = plane_center
515        .add(e1.scale(min1 - padding))
516        .add(e2.scale(min2 - padding));
517    let plane = make_plane(corner, e1, e2, width, height)?;
518
519    // Preserve the revolution carrier's outward orientation: the plane's
520    // geometric normal is e1 x e2, so same_sense follows whether that agrees
521    // with the revolution's normal at the patch midpoint.
522    let [ru0, ru1] = revolution.domain_u()?;
523    let [rv0, rv1] = revolution.domain_v()?;
524    let rev_normal = revolution.normal((ru0 + ru1) * 0.5, (rv0 + rv1) * 0.5)?;
525    let same_sense = rev_normal.dot(e1.cross(e2)) >= 0.0;
526
527    let mut coedges = Vec::with_capacity(4);
528    for (curve, edge_id, forward) in boundary {
529        let mapped = curve_to_plane_parameters(&curve, corner, e1, e2)?;
530        let pcurve = if forward { mapped } else { mapped.reversed()? };
531        coedges.push(CoedgeRecord {
532            id: next_id(counter),
533            edge_id,
534            forward,
535            pcurve,
536        });
537    }
538    let loop_id = next_id(counter);
539    Ok(FaceRecord {
540        id: next_id(counter),
541        surface: plane,
542        same_sense,
543        loops: vec![LoopRecord {
544            id: loop_id,
545            coedges,
546        }],
547        name,
548    })
549}
550
551pub fn revolve_profile_brep(
552    input: &[NurbsCurve],
553    axis_point: Vec3,
554    axis_direction: Vec3,
555    angle: f64,
556) -> Result<BrepSolid, String> {
557    revolve_profile_brep_named(input, axis_point, axis_direction, angle, &[], &[])
558}
559
560/// Revolve with caller-supplied names.  `side_names` is aligned with the
561/// INPUT curves; the kernel carries it through winding normalization and
562/// axis-curve skips, which the caller cannot reconstruct from the emitted
563/// face order.  `cap_names` is `[start_cap, end_cap]` for partial
564/// revolutions.  Empty slices leave faces unnamed.
565pub fn revolve_profile_brep_named(
566    input: &[NurbsCurve],
567    axis_point: Vec3,
568    axis_direction: Vec3,
569    angle: f64,
570    side_names: &[Option<String>],
571    cap_names: &[Option<String>],
572) -> Result<BrepSolid, String> {
573    if angle <= 1e-9 || angle > std::f64::consts::TAU + 1e-9 {
574        return Err("revolveSolid: angle must be in (0, 2*PI]".into());
575    }
576    if !side_names.is_empty() && side_names.len() != input.len() {
577        return Err(format!(
578            "revolveSolid: {} side names for {} profile curves",
579            side_names.len(),
580            input.len()
581        ));
582    }
583    let full_turn = angle > std::f64::consts::TAU - 1e-9;
584    let built = prepare_profile(input, axis_point, axis_direction, !full_turn).and_then(|profile| {
585        if full_turn {
586            revolve_full(profile, axis_point, side_names)
587        } else {
588            revolve_partial(profile, axis_point, angle, side_names, cap_names)
589        }
590    });
591    dump_revolve(input, axis_point, axis_direction, angle, &built);
592    built
593}
594
595/// `BREP_REVOLVE_DUMP` names a directory. Every revolve then writes its input
596/// profile, axis and angle, with the solid it built or its error, as
597/// `<pid>-<serial>.json`, so a probe can rebuild the same inputs on another
598/// kernel and read each shell's closure (`revolve_shell_probe dumped`).
599fn dump_revolve(
600    input: &[NurbsCurve],
601    axis_point: Vec3,
602    axis_direction: Vec3,
603    angle: f64,
604    built: &Result<BrepSolid, String>,
605) {
606    static SERIAL: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
607    let Some(directory) = std::env::var("BREP_REVOLVE_DUMP").ok().filter(|value| !value.is_empty()) else {
608        return;
609    };
610    let serial = SERIAL.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
611    let record = serde_json::json!({
612        "curves": input,
613        "axis_point": axis_point,
614        "axis_direction": axis_direction,
615        "angle": angle,
616        "solid": built.as_ref().ok(),
617        "error": built.as_ref().err(),
618    });
619    let path = std::path::Path::new(&directory).join(format!("{}-{serial:05}.json", std::process::id()));
620    if let Err(error) = std::fs::write(&path, record.to_string()) {
621        eprintln!("BREP_REVOLVE_DUMP {}: {error}", path.display());
622    }
623}
624
625fn side_name(
626    profile: &RevolveProfile,
627    curve_index: usize,
628    side_names: &[Option<String>],
629) -> Option<String> {
630    profile
631        .input_indices
632        .get(curve_index)
633        .and_then(|input_index| side_names.get(*input_index))
634        .cloned()
635        .flatten()
636}
637
638fn revolve_full(
639    profile: RevolveProfile,
640    origin: Vec3,
641    side_names: &[Option<String>],
642) -> Result<BrepSolid, String> {
643    let count = profile.curves.len();
644    let surfaces: Vec<Option<NurbsSurface>> = profile
645        .curves
646        .iter()
647        .enumerate()
648        .map(|(index, curve)| {
649            if profile.axis_curves[index] {
650                Ok(None)
651            } else {
652                make_revolution(origin, profile.axis, curve, std::f64::consts::TAU).map(Some)
653            }
654        })
655        .collect::<Result<_, String>>()?;
656    let vertices: Vec<VertexRecord> = profile
657        .points
658        .iter()
659        .enumerate()
660        .map(|(index, point)| VertexRecord {
661            id: index as u64 + 1,
662            point: *point,
663        })
664        .collect();
665    let mut counter = 100_u64;
666    let mut edges = Vec::new();
667    let mut circle_ids = Vec::with_capacity(count);
668    for index in 0..count {
669        let curve = junction_curve(index, &surfaces, &profile.points)?;
670        let [start, end] = curve.domain()?;
671        let id = next_id(&mut counter);
672        circle_ids.push(id);
673        edges.push(EdgeRecord {
674            id,
675            curve,
676            t0: start,
677            t1: end,
678            start_vertex_id: index as u64 + 1,
679            end_vertex_id: index as u64 + 1,
680            degenerate: profile.axis_points[index],
681            name: None,
682        });
683    }
684    let mut faces = Vec::new();
685    for index in 0..count {
686        if profile.axis_curves[index] {
687            continue;
688        }
689        let curve = profile.curves[index].clone();
690        let [start, end] = curve.domain()?;
691        let seam_id = next_id(&mut counter);
692        edges.push(EdgeRecord {
693            id: seam_id,
694            curve,
695            t0: start,
696            t1: end,
697            start_vertex_id: index as u64 + 1,
698            end_vertex_id: ((index + 1) % count) as u64 + 1,
699            degenerate: false,
700            name: None,
701        });
702        let coedges = vec![
703            CoedgeRecord {
704                id: next_id(&mut counter),
705                edge_id: circle_ids[index],
706                forward: true,
707                pcurve: parameter_line(0.0, start, 1.0, start)?,
708            },
709            CoedgeRecord {
710                id: next_id(&mut counter),
711                edge_id: seam_id,
712                forward: true,
713                pcurve: parameter_line(1.0, start, 1.0, end)?,
714            },
715            CoedgeRecord {
716                id: next_id(&mut counter),
717                edge_id: circle_ids[(index + 1) % count],
718                forward: false,
719                pcurve: parameter_line(1.0, end, 0.0, end)?,
720            },
721            CoedgeRecord {
722                id: next_id(&mut counter),
723                edge_id: seam_id,
724                forward: false,
725                pcurve: parameter_line(0.0, end, 0.0, start)?,
726            },
727        ];
728        let loop_id = next_id(&mut counter);
729        faces.push(FaceRecord {
730            id: next_id(&mut counter),
731            surface: surfaces[index].clone().unwrap(),
732            same_sense: true,
733            loops: vec![LoopRecord {
734                id: loop_id,
735                coedges,
736            }],
737            name: side_name(&profile, index, side_names),
738        });
739    }
740    let solid = BrepSolid {
741        id: next_id(&mut counter),
742        vertices,
743        edges,
744        shells: vec![ShellRecord {
745            id: next_id(&mut counter),
746            faces,
747        }],
748        genus: if profile.axis_points.iter().any(|flag| *flag) {
749            0
750        } else {
751            1
752        },
753    };
754    let issues = solid.validate();
755    if issues.is_empty() {
756        Ok(solid)
757    } else {
758        Err(format!("Rust full revolution is invalid: {issues:?}"))
759    }
760}
761
762fn revolve_partial(
763    profile: RevolveProfile,
764    origin: Vec3,
765    angle: f64,
766    side_names: &[Option<String>],
767    cap_names: &[Option<String>],
768) -> Result<BrepSolid, String> {
769    let count = profile.curves.len();
770    let end_curves: Vec<NurbsCurve> = profile
771        .curves
772        .iter()
773        .map(|curve| rotate_curve(curve, origin, profile.axis, angle))
774        .collect::<Result<_, _>>()?;
775    let surfaces: Vec<Option<NurbsSurface>> = profile
776        .curves
777        .iter()
778        .enumerate()
779        .map(|(index, curve)| {
780            if profile.axis_curves[index] {
781                Ok(None)
782            } else {
783                make_revolution(origin, profile.axis, curve, angle).map(Some)
784            }
785        })
786        .collect::<Result<_, String>>()?;
787    let mut vertices = Vec::new();
788    let mut start_vertex_ids = Vec::with_capacity(count);
789    let mut end_vertex_ids = Vec::with_capacity(count);
790    let mut counter = 100_u64;
791    for point in &profile.points {
792        let id = next_id(&mut counter);
793        start_vertex_ids.push(id);
794        vertices.push(VertexRecord { id, point: *point });
795    }
796    for index in 0..count {
797        if profile.axis_points[index] {
798            end_vertex_ids.push(start_vertex_ids[index]);
799        } else {
800            let id = next_id(&mut counter);
801            end_vertex_ids.push(id);
802            vertices.push(VertexRecord {
803                id,
804                point: rotate_point(profile.points[index], origin, profile.axis, angle),
805            });
806        }
807    }
808
809    let mut edges = Vec::new();
810    let mut arc_ids = Vec::with_capacity(count);
811    let mut arc_curves = Vec::with_capacity(count);
812    for index in 0..count {
813        let curve = junction_curve(index, &surfaces, &profile.points)?;
814        let [start, end] = curve.domain()?;
815        let id = next_id(&mut counter);
816        arc_ids.push(id);
817        arc_curves.push(curve.clone());
818        edges.push(EdgeRecord {
819            id,
820            curve,
821            t0: start,
822            t1: end,
823            start_vertex_id: start_vertex_ids[index],
824            end_vertex_id: end_vertex_ids[index],
825            degenerate: profile.axis_points[index],
826            name: None,
827        });
828    }
829    let mut start_edge_ids = Vec::with_capacity(count);
830    let mut end_edge_ids = Vec::with_capacity(count);
831    for index in 0..count {
832        let [start, end] = profile.curves[index].domain()?;
833        let start_id = next_id(&mut counter);
834        start_edge_ids.push(start_id);
835        edges.push(EdgeRecord {
836            id: start_id,
837            curve: profile.curves[index].clone(),
838            t0: start,
839            t1: end,
840            start_vertex_id: start_vertex_ids[index],
841            end_vertex_id: start_vertex_ids[(index + 1) % count],
842            degenerate: false,
843            name: None,
844        });
845        if profile.axis_curves[index] {
846            end_edge_ids.push(start_id);
847        } else {
848            let end_id = next_id(&mut counter);
849            end_edge_ids.push(end_id);
850            edges.push(EdgeRecord {
851                id: end_id,
852                curve: end_curves[index].clone(),
853                t0: start,
854                t1: end,
855                start_vertex_id: end_vertex_ids[index],
856                end_vertex_id: end_vertex_ids[(index + 1) % count],
857                degenerate: false,
858                name: None,
859            });
860        }
861    }
862
863    let mut faces = Vec::new();
864    for index in 0..count {
865        if profile.axis_curves[index] {
866            continue;
867        }
868        if profile.radial_curves[index] {
869            faces.push(radial_plane_face(
870                &profile,
871                index,
872                count,
873                origin,
874                surfaces[index].as_ref().unwrap(),
875                &arc_curves,
876                &end_curves,
877                &arc_ids,
878                &start_edge_ids,
879                &end_edge_ids,
880                &mut counter,
881                side_name(&profile, index, side_names),
882            )?);
883            continue;
884        }
885        let [start, end] = profile.curves[index].domain()?;
886        let coedges = vec![
887            CoedgeRecord {
888                id: next_id(&mut counter),
889                edge_id: arc_ids[index],
890                forward: true,
891                pcurve: parameter_line(0.0, start, 1.0, start)?,
892            },
893            CoedgeRecord {
894                id: next_id(&mut counter),
895                edge_id: end_edge_ids[index],
896                forward: true,
897                pcurve: parameter_line(1.0, start, 1.0, end)?,
898            },
899            CoedgeRecord {
900                id: next_id(&mut counter),
901                edge_id: arc_ids[(index + 1) % count],
902                forward: false,
903                pcurve: parameter_line(1.0, end, 0.0, end)?,
904            },
905            CoedgeRecord {
906                id: next_id(&mut counter),
907                edge_id: start_edge_ids[index],
908                forward: false,
909                pcurve: parameter_line(0.0, end, 0.0, start)?,
910            },
911        ];
912        let loop_id = next_id(&mut counter);
913        faces.push(FaceRecord {
914            id: next_id(&mut counter),
915            surface: surfaces[index].clone().unwrap(),
916            same_sense: true,
917            loops: vec![LoopRecord {
918                id: loop_id,
919                coedges,
920            }],
921            name: side_name(&profile, index, side_names),
922        });
923    }
924
925    let mut min_radial = f64::INFINITY;
926    let mut max_radial = f64::NEG_INFINITY;
927    let mut min_axis = f64::INFINITY;
928    let mut max_axis = f64::NEG_INFINITY;
929    for curve in &profile.curves {
930        let [start, end] = curve.domain()?;
931        for index in 0..=24 {
932            let delta = curve
933                .evaluate(start + (end - start) * index as f64 / 24.0)?
934                .sub(origin);
935            let radial = delta.dot(profile.radial);
936            let axial = delta.dot(profile.axis);
937            min_radial = min_radial.min(radial);
938            max_radial = max_radial.max(radial);
939            min_axis = min_axis.min(axial);
940            max_axis = max_axis.max(axial);
941        }
942    }
943    let padding = (max_radial - min_radial).max(max_axis - min_axis) * 0.05 + 1e-6;
944    let width = max_radial - min_radial + 2.0 * padding;
945    let height = max_axis - min_axis + 2.0 * padding;
946    let start_origin = origin
947        .add(profile.radial.scale(min_radial - padding))
948        .add(profile.axis.scale(min_axis - padding));
949    let mut start_coedges = Vec::with_capacity(count);
950    for index in 0..count {
951        start_coedges.push(CoedgeRecord {
952            id: next_id(&mut counter),
953            edge_id: start_edge_ids[index],
954            forward: true,
955            pcurve: curve_to_plane_parameters(
956                &profile.curves[index],
957                start_origin,
958                profile.radial,
959                profile.axis,
960            )?,
961        });
962    }
963    let start_loop_id = next_id(&mut counter);
964    faces.push(FaceRecord {
965        id: next_id(&mut counter),
966        surface: make_plane(start_origin, profile.radial, profile.axis, width, height)?,
967        same_sense: true,
968        loops: vec![LoopRecord {
969            id: start_loop_id,
970            coedges: start_coedges,
971        }],
972        name: cap_names.first().cloned().flatten(),
973    });
974
975    let end_radial = rotate_point(origin.add(profile.radial), origin, profile.axis, angle)
976        .sub(origin)
977        .normalized()?;
978    let end_origin = origin
979        .add(end_radial.scale(min_radial - padding))
980        .add(profile.axis.scale(min_axis - padding));
981    let mut end_coedges = Vec::with_capacity(count);
982    for index in (0..count).rev() {
983        end_coedges.push(CoedgeRecord {
984            id: next_id(&mut counter),
985            edge_id: end_edge_ids[index],
986            forward: false,
987            pcurve: curve_to_plane_parameters(
988                &end_curves[index],
989                end_origin,
990                profile.axis,
991                end_radial,
992            )?
993            .reversed()?,
994        });
995    }
996    let end_loop_id = next_id(&mut counter);
997    faces.push(FaceRecord {
998        id: next_id(&mut counter),
999        surface: make_plane(end_origin, profile.axis, end_radial, height, width)?,
1000        same_sense: true,
1001        loops: vec![LoopRecord {
1002            id: end_loop_id,
1003            coedges: end_coedges,
1004        }],
1005        name: cap_names.get(1).cloned().flatten(),
1006    });
1007
1008    let solid = BrepSolid {
1009        id: next_id(&mut counter),
1010        vertices,
1011        edges,
1012        shells: vec![ShellRecord {
1013            id: next_id(&mut counter),
1014            faces,
1015        }],
1016        genus: 0,
1017    };
1018    let issues = solid.validate();
1019    if issues.is_empty() {
1020        Ok(solid)
1021    } else {
1022        Err(format!("Rust partial revolution is invalid: {issues:?}"))
1023    }
1024}
1025