Variadic Booleans

Union an arbitrary list of triangle soups with a simple fold pattern.

This guide shows a compact variadic union pattern over an arbitrary number of triangle soups.

Referenced API:
Operation::importFromTrianglesF32,
Operation::union,
Operation::exportToTrianglesF32.

Code

#include <vector>
#include <solidean.hh>
#include "ExampleFramework.hh" // example::triangle

auto union_all(solidean::Context& ctx,
               solidean::ExactArithmetic& arithmetic,
               std::vector<std::vector<example::triangle>> const& meshes,
                // optional: choose how to treat inputs (Solid vs Supersolid)
               solidean::MeshType meshType = solidean::MeshType::Solid)
    -> std::vector<example::triangle>
{
    if (meshes.empty()) 
        return {}; // empty union

    auto blob = ctx.execute(arithmetic, [&](solidean::Operation& op)
    {
        // 1) import first mesh
        solidean::MeshOperand acc =
            op.importFromTrianglesF32(solidean::as_triangle3_span(meshes[0]), meshType);

        // 2) fold union across remaining meshes
        for (size_t i = 1; i < meshes.size(); ++i) {
            auto mi = op.importFromTrianglesF32(solidean::as_triangle3_span(meshes[i]), meshType);
            acc = op.union_(acc, mi);
        }

        // 3) export unrolled triangles for simplicity
        return op.exportToTrianglesF32(acc);
    });

    auto span = blob->getTrianglesF32<example::triangle>();
    return std::vector<example::triangle>(span.begin(), span.end());
}

Notes

  • Variadic pattern:
    Instead of a special variadic API, you simply chain binary Booleans in a loop.
    Solidean’s internal scheduler optimizes evaluation so the order is efficient.

  • Export choice:
    This example exports unrolled triangles.
    For connected results and reduced duplication, you can also use Operation::exportToIndexedTrianglesF32 or the more general Operation::exportMesh with ExportFormat::IndexedTriangles.

  • Generalization:
    The same pattern applies for difference or intersection.

  • Performance note:
    Even though you write a sequential loop, Solidean batches and parallelizes internally.
    Variadic Booleans are output-sensitive: cost scales with the complexity of the result, not just the input size.