Skip to main content

boolmesh/
lib.rs

1//--- Copyright (C) 2025 Saki Komikado <komietty@gmail.com>,
2//--- This Source Code Form is subject to the terms of the Mozilla Public License v.2.0.
3
4#![allow(clippy::too_many_arguments)]
5#![allow(clippy::cast_abs_to_unsigned)]
6#![allow(unused_braces)]
7
8mod manifold;
9mod triangulation;
10mod simplification;
11mod common;
12mod boolean03;
13mod boolean45;
14mod compose;
15mod tests;
16
17use crate::boolean03::boolean03;
18use crate::boolean45::boolean45;
19use crate::simplification::simplify_topology;
20use crate::triangulation::triangulate;
21use crate::common::*;
22use crate::manifold::*;
23
24pub use crate::common::{Real, Vec2, Vec3, Vec4, Mat3, K_PRECISION};
25
26pub mod prelude {
27    pub use crate::common::OpType;
28    pub use crate::manifold::Manifold;
29    pub use crate::compute_boolean;
30    pub use crate::compose::{
31        compose,
32        fractal,
33        extrude,
34        generate_cone,
35        generate_cube,
36        generate_torus,
37        generate_cylinder,
38        generate_uv_sphere,
39        generate_icosphere,
40    };
41}
42
43pub fn compute_boolean(
44    mp: &Manifold,
45    mq: &Manifold,
46    op: OpType,
47) -> Result<Manifold, String> {
48    let eps = mp.eps.max(mq.eps);
49    let tol = mp.tol.max(mq.tol);
50
51    let     b03 = boolean03(mp, mq, &op);
52    let mut b45 = boolean45(mp, mq, &b03, &op);
53    let mut trg = triangulate(mp, mq, &b45, eps)?;
54
55    simplify_topology(
56        &mut trg.hs,
57        &mut b45.ps,
58        &mut trg.ns,
59        &mut trg.rs,
60        b45.nv_from_p,
61        b45.nv_from_q,
62        eps
63    );
64
65    cleanup_unused_verts(
66        &mut b45.ps,
67        &mut trg.hs
68    );
69
70    Manifold::new_impl(
71        b45.ps,
72        trg.hs.chunks(3).map(|hs| Vec3u::new(hs[0].tail, hs[1].tail, hs[2].tail)).collect(),
73        Some(eps),
74        Some(tol)
75    )
76}
77
78//pub fn compute_boolean_from_raw_data(
79//    pos0: &[Real],
80//    idx0: &[usize],
81//    pos1: &[Real],
82//    idx1: &[usize],
83//    op_type: usize
84//) -> Result<Manifold, String>{
85//    let mp = Manifold::new(&pos0, &idx0)?;
86//    let mq = Manifold::new(&pos1, &idx1)?;
87//    let op = match op_type {
88//        0 => OpType::Add,
89//        1 => OpType::Subtract,
90//        2 => OpType::Intersect,
91//        _ => return Err("Invalid op_type".into())
92//    };
93//    compute_boolean(&mp, &mq, op)
94//}
95
96
97
98
99