Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

11.1 KB
Raw Blame
//! Hyperbola and parabola edges (ISO 10303-42 HYPERBOLA, PARABOLA): a cone cut by a plane
//! parallel to its axis meets it in a hyperbola, by a plane parallel to a generator in a
//! parabola. Hand-written files, volumes against closed forms.

use std::f64::consts::{FRAC_PI_4, PI};

use super::*;
use crate::{measure, step_export};

const HEAD: &str = "ISO-10303-21;\nHEADER;\nFILE_DESCRIPTION((''),'2;1');\nFILE_NAME('t','',(''),(''),'','hand','');\nFILE_SCHEMA(('AUTOMOTIVE_DESIGN { 1 0 10303 214 1 1 1 1 }'));\nENDSEC;\nDATA;\n";
const TAIL: &str = "ENDSEC;\nEND-ISO-10303-21;\n";
const CONTEXT: &str = "#90=ADVANCED_BREP_SHAPE_REPRESENTATION('',(#81),#91);\n#91=(GEOMETRIC_REPRESENTATION_CONTEXT(3)GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT((#92))GLOBAL_UNIT_ASSIGNED_CONTEXT((#93,#94))REPRESENTATION_CONTEXT('',''));\n#92=UNCERTAINTY_MEASURE_WITH_UNIT(LENGTH_MEASURE(1.E-6),#93,'','');\n#93=(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.));\n#94=(NAMED_UNIT(*)PLANE_ANGLE_UNIT()SI_UNIT($,.RADIAN.));\n";

/// Height of the cone pieces: the 45° cone `x² + y² = z²` up to `z = H`.
const H: f64 = 10.0;

fn rel(a: f64, b: f64) -> f64 {
    (a - b).abs() / b.abs().max(1e-9)
}

/// A solid bounded by the 45° cone (apex at the origin, axis Z), its top plane `z = H` and a
/// cutting plane. `curve` defines `#30` (the conic edge from `v1` to `v2`, rising parameter) and
/// `#41`, the cutting plane's placement (normal pointing out of the solid). The top circle runs
/// `#51` from `circle_from` to the other vertex counter-clockwise seen from above; the loops say
/// which way each face uses each edge.
fn cone_piece(curve: &str, v1: [f64; 3], v2: [f64; 3], circle_from_v1: bool, loops: [&str; 3]) -> String {
    let mut t = String::from(HEAD);
    t += "#1=CARTESIAN_POINT('',(0.,0.,10.));\n#2=DIRECTION('',(0.,0.,1.));\n#3=DIRECTION('',(1.,0.,0.));\n#4=AXIS2_PLACEMENT_3D('',#1,#2,#3);\n";
    t += &format!("#5=CONICAL_SURFACE('',#4,10.,{FRAC_PI_4:.15});\n#6=PLANE('',#4);\n");
    t += &format!("#10=CARTESIAN_POINT('',({:.15},{:.15},{:.15}));\n#11=VERTEX_POINT('',#10);\n", v1[0], v1[1], v1[2]);
    t += &format!("#12=CARTESIAN_POINT('',({:.15},{:.15},{:.15}));\n#13=VERTEX_POINT('',#12);\n", v2[0], v2[1], v2[2]);
    t += curve;
    t += "#31=EDGE_CURVE('',#11,#13,#30,.T.);\n#42=PLANE('',#41);\n";
    t += "#50=CIRCLE('',#4,10.);\n";
    t += if circle_from_v1 { "#51=EDGE_CURVE('',#11,#13,#50,.T.);\n" } else { "#51=EDGE_CURVE('',#13,#11,#50,.T.);\n" };
    t += "#52=VECTOR('',#3,1.);\n#53=LINE('',#10,#52);\n#54=EDGE_CURVE('',#11,#13,#53,.T.);\n";
    // Faces: the cone, the cutting plane, the top.
    for (k, (lp, surface)) in loops.iter().zip(["#5", "#42", "#6"]).enumerate() {
        let b = 60 + 6 * k;
        let edges: Vec<&str> = lp.split(' ').collect();
        let mut oes = Vec::new();
        for (j, e) in edges.iter().enumerate() {
            let (id, fwd) = e.split_at(e.len() - 1);
            t += &format!("#{}=ORIENTED_EDGE('',*,*,{id},{});\n", b + j, if fwd == "+" { ".T." } else { ".F." });
            oes.push(format!("#{}", b + j));
        }
        t += &format!("#{}=EDGE_LOOP('',({}));\n#{}=FACE_OUTER_BOUND('',#{},.T.);\n", b + 3, oes.join(","), b + 4, b + 3);
        t += &format!("#{}=ADVANCED_FACE('',(#{}),{surface},.T.);\n", b + 5, b + 4);
    }
    t += "#80=CLOSED_SHELL('',(#65,#71,#77));\n#81=MANIFOLD_SOLID_BREP('Piece',#80);\n";
    t += CONTEXT;
    t += TAIL;
    t
}

/// The cone inside `z ≤ H` cut by the plane `y = D` (parallel to the axis), keeping `y ≥ D`:
/// the cut meets the cone in the hyperbola `z² − x² = D²`, centre `(0, D, 0)`, both semi axes D.
fn hyperbola_piece() -> String {
    const D: f64 = 4.0;
    let w = (H * H - D * D).sqrt();
    // Real axis up the plane (X = +z), normal +y, so the imaginary axis is +x:
    // (x, y, z) = (D·sinh t, D, D·cosh t).
    let curve = "#20=CARTESIAN_POINT('',(0.,4.,0.));\n#21=DIRECTION('',(0.,1.,0.));\n#22=AXIS2_PLACEMENT_3D('',#20,#21,#2);\n#30=HYPERBOLA('',#22,4.,4.);\n#40=DIRECTION('',(0.,-1.,0.));\n#41=AXIS2_PLACEMENT_3D('',#20,#40,#3);\n";
    // Cone: up the hyperbola from +x to −x, back along the arc; the plane (seen from −y):
    // the hyperbola from −x to +x, back along the line; the top: the arc, then the line.
    cone_piece(curve, [-w, D, H], [w, D, H], false, ["#31- #51-", "#31+ #54-", "#51+ #54+"])
}

/// The cone inside `z ≤ H` cut by the plane `z − y = C` (parallel to the generator in the
/// x = 0, y > 0 half plane), keeping `z − y ≥ C`: the cut meets the cone in a parabola with its
/// vertex at `(0, −C/2, C/2)`, axis along (0, 1, 1) and focal distance `C·√2 / 4`.
fn parabola_piece() -> String {
    const C: f64 = 8.0;
    let w = (H * H - (H - C) * (H - C)).sqrt();
    // Normal (0, 1, −1) out of the solid, axis X = (0, 1, 1), so Y = Z × X = +x:
    // (x, y, z) = (4√2·t, −4 + 2t², 4 + 2t²).
    let curve = format!(
        "#20=CARTESIAN_POINT('',(0.,-4.,4.));\n#21=DIRECTION('',(0.,1.,-1.));\n#23=DIRECTION('',(0.,1.,1.));\n#22=AXIS2_PLACEMENT_3D('',#20,#21,#23);\n#30=PARABOLA('',#22,{:.15});\n#41=AXIS2_PLACEMENT_3D('',#20,#21,#23);\n",
        C * 2f64.sqrt() / 4.0
    );
    // Cone (seen from −y): the parabola from −x to +x, back along the arc; the plane (seen from
    // outside, +x on the left): the parabola from +x to −x, back along the line; the top: the
    // arc round through −y, then the line back.
    cone_piece(&curve, [-w, H - C, H], [w, H - C, H], true, ["#31+ #51-", "#31- #54+", "#51+ #54-"])
}

/// ∫ from D to H of the circular segment `y ≥ D` of the disc of radius z.
fn hyperbola_piece_volume() -> f64 {
    let d = 4.0;
    let s = (H * H - d * d).sqrt();
    H.powi(3) / 3.0 * (d / H).acos() - 2.0 / 3.0 * d * H * s + d.powi(3) / 3.0 * ((H + s) / d).ln()
}

/// ∫ from C/2 to H of the part `y ≤ z − C` of the disc of radius z.
fn parabola_piece_volume() -> f64 {
    let c = 8.0;
    let q: f64 = 2.0 * c * H - c * c;
    H.powi(3) / 3.0 * (PI - (1.0 - c / H).acos())
        - (0.4 * q.powf(2.5) + 4.0 / 3.0 * c * c * q.powf(1.5) + 2.0 * c.powi(4) * q.sqrt()) / (24.0 * c * c)
        + (0.4 * q.powf(2.5) - 2.0 / 3.0 * c * c * q.powf(1.5)) / (4.0 * c * c)
}

fn import_piece(text: &str, want: f64) -> StepImport {
    assert_eq!(step_orientation_errors(text).unwrap(), Vec::<String>::new());
    let imp = step_import(text).unwrap();
    assert!(imp.warnings.is_empty(), "{:?}", imp.warnings);
    assert_eq!(imp.bodies.len(), 1);
    assert!(imp.bodies[0].closed);
    let m = measure(&imp.bodies[0].body).unwrap();
    assert_eq!(m.faces, 3);
    assert!(rel(m.volume, want) < 1e-4, "volume {} vs {want}", m.volume);
    imp
}

/// Export the imported piece and read it back: the conic edges are written as exact rational
/// B-splines and come back as the same solid.
fn round_trip(imp: &StepImport, want: f64) {
    let text = step_export(&[&imp.bodies[0].body], "conic").unwrap();
    step_validate(&text).unwrap();
    let back = step_import(&text).unwrap();
    assert!(back.warnings.is_empty(), "{:?}", back.warnings);
    assert_eq!(back.bodies.len(), 1);
    assert!(back.bodies[0].closed);
    let v = measure(&back.bodies[0].body).unwrap().volume;
    assert!(rel(v, want) < 1e-4, "round trip volume {v} vs {want}");
}

#[test]
fn hyperbola_bounded_faces_import() {
    let want = hyperbola_piece_volume();
    assert!(rel(want, 175.447_506_9) < 1e-9, "{want}");
    let imp = import_piece(&hyperbola_piece(), want);
    round_trip(&imp, want);
    let v = super::tests::schema_4_volume(&hyperbola_piece());
    assert!(rel(v, want) < 1e-4, "schema 4: {v} vs {want}");
}

#[test]
fn parabola_bounded_faces_import() {
    let want = parabola_piece_volume();
    assert!(rel(want, 447.014_684_29) < 1e-9, "{want}");
    let imp = import_piece(&parabola_piece(), want);
    round_trip(&imp, want);
    let v = super::tests::schema_4_volume(&parabola_piece());
    assert!(rel(v, want) < 1e-4, "schema 4: {v} vs {want}");
}

/// Hostile conics are errors (the face is reported and left out), never panics.
#[test]
fn hostile_conics_never_panic() {
    let good = hyperbola_piece();
    for bad in [
        good.replace("HYPERBOLA('',#22,4.,4.)", "HYPERBOLA('',#22,0.,4.)"),
        good.replace("HYPERBOLA('',#22,4.,4.)", "HYPERBOLA('',#22,4.,-1.)"),
        good.replace("HYPERBOLA('',#22,4.,4.)", "HYPERBOLA('',#22,4.)"),
        good.replace("HYPERBOLA('',#22,4.,4.)", "HYPERBOLA('',#22,1.E300,4.)"),
        good.replace("HYPERBOLA('',#22,4.,4.)", "HYPERBOLA('',#22,1.E-300,1.E-300)"),
        good.replace("HYPERBOLA('',#22,4.,4.)", "HYPERBOLA('',#22,5.,4.)"),
        good.replace("HYPERBOLA('',#22,4.,4.)", "PARABOLA('',#22,0.)"),
        good.replace("HYPERBOLA('',#22,4.,4.)", "PARABOLA('',#22,1.)"),
        good.replace("#31=EDGE_CURVE('',#11,#13,#30,.T.)", "#31=EDGE_CURVE('',#11,#11,#30,.T.)"),
    ] {
        let r = std::panic::catch_unwind(|| step_import(&bad));
        let Ok(r) = r else { panic!("STEP import panicked") };
        if let Ok(imp) = r {
            assert!(!imp.warnings.is_empty(), "a broken conic imported silently");
        }
    }
}

/// A cone to a point written with no vertex at its point (#258): the cone face's only loop is
/// its base circle, or that circle and a VERTEX_LOOP at the point, which has no edges. It was
/// read as a sliver at the base and the solid came in as a flat disc; the face reaches the
/// point now.
#[test]
fn a_cone_bounded_only_by_its_base_circle_reaches_its_point() {
    let (r, h) = (6.0f64, 15.0f64);
    for apex_loop in [false, true] {
        let mut t = String::from(HEAD);
        t += "#1=CARTESIAN_POINT('',(0.,0.,0.));\n#2=DIRECTION('',(0.,0.,-1.));\n#3=DIRECTION('',(1.,0.,0.));\n#4=AXIS2_PLACEMENT_3D('',#1,#2,#3);\n";
        t += &format!("#5=CONICAL_SURFACE('',#4,{r:.1},{:.15});\n", (r / h).atan());
        t += "#6=DIRECTION('',(0.,0.,1.));\n#7=AXIS2_PLACEMENT_3D('',#1,#6,#3);\n#8=PLANE('',#7);\n";
        t += &format!("#9=CIRCLE('',#7,{r:.1});\n#10=CARTESIAN_POINT('',({r:.1},0.,0.));\n#11=VERTEX_POINT('',#10);\n");
        t += "#12=EDGE_CURVE('',#11,#11,#9,.T.);\n";
        t += "#20=ORIENTED_EDGE('',*,*,#12,.F.);\n#21=EDGE_LOOP('',(#20));\n#22=FACE_OUTER_BOUND('',#21,.T.);\n#23=ADVANCED_FACE('',(#22),#8,.F.);\n";
        t += "#30=ORIENTED_EDGE('',*,*,#12,.T.);\n#31=EDGE_LOOP('',(#30));\n#32=FACE_OUTER_BOUND('',#31,.T.);\n";
        if apex_loop {
            t += &format!(
                "#33=CARTESIAN_POINT('',(0.,0.,{h:.1}));\n#34=VERTEX_POINT('',#33);\n#35=VERTEX_LOOP('',#34);\n#36=FACE_BOUND('',#35,.T.);\n"
            );
            t += "#37=ADVANCED_FACE('',(#32,#36),#5,.T.);\n";
        } else {
            t += "#37=ADVANCED_FACE('',(#32),#5,.T.);\n";
        }
        t += "#80=CLOSED_SHELL('',(#23,#37));\n#81=MANIFOLD_SOLID_BREP('Cone',#80);\n";
        t += CONTEXT;
        t += TAIL;
        let imp = step_import(&t).unwrap();
        let body = &imp.bodies[0].body;
        let m = measure(body).unwrap();
        let exact = PI * r * r * h / 3.0;
        assert!(rel(m.volume, exact) < 2e-3, "vertex loop {apex_loop}: volume {} vs {exact}", m.volume);
        let top = body.mesh(&crate::MeshTolerance::chord(0.01)).unwrap().positions.iter().map(|p| p.z).fold(f64::MIN, f64::max);
        assert!((top - h).abs() < 1e-6, "vertex loop {apex_loop}: reaches z = {top}, not {h}");
    }
}