Skip to content

armgit / forma

Public Imported from GitHub
0

A 1.2 MB pure-Rust rewrite of headless Blender built for AI agents — 3D B-rep modeling engine, CLI, and MCP server

5.9 KB
Raw Blame
//! Boolean seams that must close:
//! - Holes drilled one after another into one face of a board. The first cut leaves the face as a
//!   fan with a long sliver from a corner to the hole's rim, thinner than the T-junction tolerance;
//!   the second cut's seam vertices then lie within that tolerance of both its long edges.
//! - Two round prisms of the same radius with different facets (near-parallel faces crossing at
//!   small angles), intersected.

use forma_core::boolean::{boolean_op, BoolOp};
use forma_core::invariants::MeshInvariants;
use forma_core::math::Vec3;
use forma_core::mesh::PolyMesh;
use forma_core::primitives::{make_box, make_cone};

/// 564 x 560 x 18, from (18, 0, 100) to (582, 560, 118).
fn board() -> PolyMesh {
    let mut b = make_box(564.0, 560.0, 18.0, 0);
    b.translate(Vec3::new(300.0, 280.0, 109.0));
    b
}

/// A 25-sided rod of radius 4, 9.5 long along `axis` (a column-major rotation) from `at`.
fn rod(axis: [f64; 9], at: Vec3) -> PolyMesh {
    let mut c = make_cone(4.0, 4.0, 9.5, 25, 0);
    c.translate(Vec3::new(0.0, 0.0, 4.75));
    let m = axis;
    for v in &mut c.verts {
        let (x, y, z) = (v.x, v.y, v.z);
        *v = Vec3::new(m[0] * x + m[3] * y + m[6] * z, m[1] * x + m[4] * y + m[7] * z, m[2] * x + m[5] * y + m[8] * z) + at;
    }
    c
}

/// Up from the bottom face, turned a quarter about z; down from the top face; into the front
/// face (y = 0); into the left face (x = 18).
const UP: [f64; 9] = [0.0, -1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0];
const DOWN: [f64; 9] = [1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, -1.0];
const ALONG_Y: [f64; 9] = [1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0];
const ALONG_X: [f64; 9] = [0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0];

#[test]
fn two_holes_in_a_line_leave_a_closed_board() {
    let once = boolean_op(&board(), &rod(UP, Vec3::new(550.0, 50.0, 99.5)), BoolOp::Difference, None, 0).unwrap();
    assert!(MeshInvariants::compute(&once).is_valid_solid(), "first hole");
    let twice = boolean_op(&once, &rod(UP, Vec3::new(550.0, 510.0, 99.5)), BoolOp::Difference, None, 0).unwrap();
    let inv = MeshInvariants::compute(&twice);
    assert!(inv.is_valid_solid(), "second hole: {inv:?}");
}

/// Drills `holes` 9 mm deep holes, one by one, at seeded positions into the board's `faces` (0
/// bottom, 1 top, 2 front, 3 left), half of them on a 32 mm grid so rims line up; after each the
/// board must be a closed solid with one more hole's volume gone.
fn drill_seeded(seed: u64, holes: usize, faces: &[usize]) {
    let mut s = seed.wrapping_mul(0x9E37_79B9_7F4A_7C15) | 1;
    let mut next = move || {
        s ^= s << 13;
        s ^= s >> 7;
        s ^= s << 17;
        (s >> 11) as f64 / (1u64 << 53) as f64
    };
    let hole = 0.5 * 25.0 * 16.0 * (2.0 * std::f64::consts::PI / 25.0).sin() * 9.0;
    let mut part = board();
    let expected = MeshInvariants::compute(&part).volume;
    let mut taken: Vec<(usize, f64, f64)> = Vec::new();
    let mut k = 0;
    while k < holes {
        let face = faces[(next() * faces.len() as f64) as usize];
        let grid = next() < 0.5;
        // (u, v) on the face: x then y, x then z, or y then z; clear of the board's edges.
        let (u_lo, u_hi, v_lo, v_hi) = match face {
            0 | 1 => (50.0, 550.0, 30.0, 530.0),
            2 => (30.0, 570.0, 109.0, 109.0),
            _ => (30.0, 530.0, 109.0, 109.0),
        };
        let pick = |t: f64, lo: f64, hi: f64| if grid { lo + (t * (hi - lo) / 32.0).floor() * 32.0 } else { lo + t * (hi - lo) };
        let (u, v) = (pick(next(), u_lo, u_hi), pick(next(), v_lo, v_hi));
        // Top and bottom holes meet at mid-thickness, so they keep clear of each other too.
        let near = |f: usize| f == face || f.max(face) == 1;
        if taken.iter().any(|&(f, tu, tv)| near(f) && (tu - u).hypot(tv - v) < 12.0) {
            continue;
        }
        taken.push((face, u, v));
        let tool = match face {
            0 => rod(UP, Vec3::new(u, v, 99.5)),
            1 => rod(DOWN, Vec3::new(u, v, 118.5)),
            2 => rod(ALONG_Y, Vec3::new(u, -0.5, v)),
            _ => rod(ALONG_X, Vec3::new(17.5, u, v)),
        };
        part = boolean_op(&part, &tool, BoolOp::Difference, None, 0)
            .expect("boolean difference succeeds");
        k += 1;
        let inv = MeshInvariants::compute(&part);
        let want = expected - k as f64 * hole;
        assert!(
            inv.is_valid_solid(),
            "seed {seed}, hole {k} {:?}: {inv:?}",
            taken.last().expect("taken has current hole")
        );
        assert!((inv.volume - want).abs() < 1e-6 * want, "seed {seed}, hole {k}: volume {} not {want}", inv.volume);
    }
}

#[test]
fn many_holes_in_one_face_leave_a_closed_board() {
    for seed in 0..20 {
        drill_seeded(seed, 24, &[0]);
    }
}

#[test]
fn many_holes_in_several_faces_leave_a_closed_board() {
    for seed in 110..120 {
        drill_seeded(seed, 24, &[0, 1, 2, 3]);
    }
}

#[test]
fn round_prisms_with_different_facets_intersect_closed() {
    for na in [120usize, 122, 124] {
        for nb in [120usize, 122, 124] {
            for turn in [0.0f64, 0.1] {
                let a = make_cone(450.0, 450.0, 100.0, na, 0);
                let mut b = make_cone(450.0, 450.0, 300.0, nb, 0);
                let (sin, cos) = turn.to_radians().sin_cos();
                for v in &mut b.verts {
                    *v = Vec3::new(v.x * cos - v.y * sin, v.x * sin + v.y * cos, v.z);
                }
                let both = boolean_op(&a, &b, BoolOp::Intersect, None, 0).unwrap();
                let inv = MeshInvariants::compute(&both);
                let va = MeshInvariants::compute(&a).volume;
                assert!(inv.is_valid_solid(), "{na} and {nb} sides, turned {turn} deg: {inv:?}");
                // Both are within their sagitta (~0.1 mm) of the same round.
                assert!(inv.volume <= va * (1.0 + 1e-9) && inv.volume > va * (1.0 - 1e-3), "{na} and {nb} sides, turned {turn} deg: volume {} of {va}", inv.volume);
            }
        }
    }
}