Manifold Geometry Library

repository·master·Indexed 22 days ago

https://github.com/elalish/manifold

A high-performance geometry library specialized in creating and performing Boolean operations on manifold triangle meshes. It ensures reliable solid modeling for CAD and manufacturing with guaranteed manifold output. Manifold supports arbitrary vertex properties, SDF evaluation, and parallelization via TBB. It provides bindings for Python (manifold3d), TypeScript/JavaScript (manifold-3d via WASM), C++, Rust, C#, Java, Julia, and Swift.

Tokens
15.5K
Snippets
18
Records
118
Agent score
80%

What's inside Manifold

  1. Overview of Manifold geometry library

    master

    Manifold is a high-performance geometry library designed for creating and operating on manifold triangle meshes. A manifold mesh represents a solid object, making it essential for CAD, manufacturing, and structural analysis.

    Key features include:

    • Guaranteed Manifold Output: A robust mesh Boolean algorithm designed to handle edge cases reliably.
    • Performance: Extensive use of parallelization (via TBB) and efficient algorithms.
    • Vertex Properties: Support for arbitrary vertex properties and material mapping.
    • Smooth Interpolation: A suite of refining functions for smooth mesh interpolation (handling triangles, quads, and flat polygonal faces).
    • SDF Support: A level set function for evaluating signed-distance functions (SDF) that outperforms Marching Cubes.
  2. Ways to embed Manifold in your project

    master

    You can integrate Manifold into your applications using one of three primary methods depending on your requirements for sandboxing, utility access, or low-level control:

    1. ManifoldCAD worker: A web worker-based code evaluation sandbox. It bundles scripts, includes dependencies (even fetching them from a CDN at runtime), and evaluates scripts as dynamically created functions. This provides script sandboxing and runtime package inclusion but introduces more overhead.
    2. ManifoldCAD libraries directly: Use the ManifoldCAD modules (importers, exporters, garbage collection, etc.) outside of the full ManifoldCAD environment. These modules are relatively lightweight.
    3. Manifold WASM module directly: Use the raw WASM module without the ManifoldCAD abstractions or worker sandboxing.
  3. ManifoldCAD: Scriptable solid modeling

    master
    ManifoldCAD is a solid modeling web application built with Manifold, TypeScript, and glTF. It allows users to script in JS/TS to create shapes and save them as GLB or 3MF files. It is a faster and more flexible alternative to OpenSCAD for web-based geometry scripting.
  4. What is a manifold mesh?

    master
    A manifold mesh represents a solid object and is essential for manufacturing, CAD, and structural analysis. To be considered 'manifold', a mesh must have no gaps or tears, and all faces must be oriented outwards. The Manifold library is specifically designed to create and operate on these types of triangle meshes with a focus on guaranteed manifold output and high performance through parallelization.
  5. Recommended 3D file formats for Manifold

    master

    When working with manifold meshes, choosing the right file format is critical to preserving topology and vertex properties:

    • Avoid STL: STL is lossy and inefficient. Saving a manifold mesh to STL does not guarantee the re-imported mesh will remain manifold because topology is lost.
    • Use 3MF: Highly recommended for manifold meshes representing solid objects.
    • Use glTF/GLB: Recommended if using vertex properties (like interpolated normals or UV coordinates). Specifically, use the EXT_mesh_manifold extension to allow lossless and efficient transmission of manifoldness even with property boundaries.
    • OBJ: Supported with high precision, but limited in functionality and primarily intended for testing.
  6. Understand the Boolean2 CrossSection implementation

    master
    Boolean2 is the default and only CrossSection implementation in Manifold. It is a manifold-native 2D arrangement pipeline used for polygon fill and Boolean operations. Unlike other potential backends, it does not require external dependencies like Clipper2 and is always built in-tree. It uses a sweep-line arrangement (Bentley-Ottmann) to handle 2D geometry robustly using rounded arithmetic and Smith's block rule for near-concurrent events.
  7. Configure Boolean2 Winding Rules

    master

    Boolean2 uses winding numbers to determine which sub-edges to retain. A sub-edge is kept if the requested rule classifies its two sides differently (one side inside the result, the other outside).

    Supported logical operations via winding rules:

    • Add (Union/Fill): Retains edges where the winding number w > 0. This is the default positive-winding rule.
    • Subtract: Implemented by appending the second input with negative multiplicity and then applying the Add rule.
    • Intersect: Retains edges where the winding number w > 1 (assuming normalized unit-winding operands).
  8. Understand ManifoldCAD coordinate systems and units

    master

    ManifoldCAD uses a right-handed coordinate system where +Z is up. Length units are in millimetres.

    Note on glTF Interop: Standard glTF uses a right-handed system but specifies +Y as up and uses metres as the unit. ManifoldCAD automatically handles scaling and rotation during glTF import/export to ensure consistency. For example, an arrow pointing in the +Z direction in ManifoldCAD will point in the +Y direction in a glTF file, but it will maintain its orientation (pointing up) and its physical size.

  9. Manage Epsilon and Regularization in Boolean2

    master

    Boolean2 operates on manifold::Polygons, which means output is automatically regularized: zero-area loops, collapsed edges, and cancelled opposing sub-edges are dropped.

    Epsilon Behavior:

    • Input Quantization: Epsilon is applied during the vertex merge and incidence pre-split phases to quantize input features.
    • Sweep Phase: The sweep-line engine itself is tolerance-free; it uses exact sign predicates and the block rule to resolve clusters rather than distance-based snapping.
    • Custom Epsilon: Callers can pass an explicit epsilon. If a non-positive epsilon is provided, the core infers an operation scale based on the local floating-point budget used by Boolean2 predicates.
    • Note on Stability: Do not rely on repeated Simplify() calls for stability. Tiny perturbations from floating-point arithmetic or transforms can change cleanup decisions within the epsilon regime.
  10. Build Manifold with Fuzzing support

    master

    To build a version of Manifold with fuzzing support, use the following CMake configuration:

    • -DMANIFOLD_FUZZ=ON
    • -DMANIFOLD_PYBIND=OFF
    • -DCMAKE_CXX_COMPILER=clang++
    • -DMANIFOLD_PAR=OFF (may be required)

    Note: On MacOS, you may need to set ASAN_OPTIONS=detect_container_overflow=0 when building the binary.

  11. Use degrees instead of radians for rotations

    master

    Manifold's rotation API expects values in degrees. This design allows the library to eliminate floating point errors for multiples of 90° and utilize more efficient code paths.

    Important: Avoid passing computed approximations (like -89.999999999999) instead of exact degree values (like -90), as this can cause mesh cracks. If your logic uses radians (e.g., from Math.atan2), convert them to degrees before calling Manifold rotation methods.

    // Pass degrees directly
    box.rotate([90, 0, 0]);
    box.rotate([0, 45, 0]);
    
    // Convert radians to degrees if necessary
    const toDeg = (rad) => rad * (180 / Math.PI);
    const angle = Math.atan2(y, x); // radians
    box.rotate([0, 0, toDeg(angle)]);