Tetrahedralization fills a closed triangle surface with four-cornered volume elements for a soft-body simulation. The input must be closed, manifold and outward-facing.
dotnet add package RecursiveAI.OnePhysics.ToolsRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneMath; using RecursiveAI.OnePhysics.Tools; Vector3F[] vertices = [new(0,0,0), new(1,0,0), new(0,1,0), new(0,0,1)]; int[] triangles = [1,2,3, 0,3,2, 0,1,3, 0,2,1]; var mesh = Tetrahedralizer.Generate(vertices, triangles); Console.WriteLine($"Tetrahedra: {mesh.Tetrahedra.Length / 4}"); Console.WriteLine(FormattableString.Invariant( $"Volume: {mesh.Volume:F6} / {mesh.MeshVolume:F6}"));
Tetrahedra: 7680 Volume: 0.162921 / 0.166667
The four input vertices define the surface, not the output resolution: the default lattice cell size is the longest bounding-box extent divided by 16, which produces 7680 tetrahedra inside this shape.
Unzip the example, then dotnet run.
The full program
The four positions and four outward-facing triangles above define the entire input: a tetrahedron with unit-length coordinate-axis edges. No model files are needed. For a real asset, supply vertices from your existing mesh’s positions and triangles from its triangle-index array, with three zero-based indices per outward-facing triangle. The download also prints the binding count and quality flag, and throws if the volume differs from the input by more than the example’s chosen 5% tolerance.
using RecursiveAI.OneMath; using RecursiveAI.OnePhysics.Tools; Vector3F[] vertices = [new(0,0,0), new(1,0,0), new(0,1,0), new(0,0,1)]; int[] triangles = [1,2,3, 0,3,2, 0,1,3, 0,2,1]; var mesh = Tetrahedralizer.Generate(vertices, triangles); Console.WriteLine($"Tetrahedra: {mesh.Tetrahedra.Length / 4}"); Console.WriteLine(FormattableString.Invariant( $"Volume: {mesh.Volume:F6} / {mesh.MeshVolume:F6}")); Console.WriteLine($"Bindings: {mesh.Bindings.Length}"); Console.WriteLine($"Quality met: {mesh.QualityMet}"); if (Math.Abs(mesh.Volume - mesh.MeshVolume) / mesh.MeshVolume > 0.05) throw new InvalidOperationException("Volume differs by more than 5%.");
Tetrahedra: 7680 Volume: 0.162921 / 0.166667 Bindings: 4 Quality met: True
Invalid geometry or options throw GeometryToolException; its kind and message identify the problem. Self-intersections are not detected.
Variations
Choose a coarser lattice and prepare a soft-body mesh. Replace the generation and print lines in the snippet at the top with these lines; keep its input arrays and using lines.
var mesh = Tetrahedralizer.Generate(vertices, triangles, new TetrahedralizeOptions { TargetEdgeLength = 0.125f }); Console.WriteLine($"Tetrahedra: {mesh.Tetrahedra.Length / 4}"); var softMesh = mesh.ToSoftBodyMesh(); Console.WriteLine($"Soft-body mesh: {softMesh is not null}");
Tetrahedra: 896 Soft-body mesh: True
Related recipes
The short answer
Use Tetrahedralizer.Generate from RecursiveAI.OnePhysics.Tools. Pass vertex positions and a flat array of triangle indices. The result contains volume vertices, four indices per tetrahedron, boundary triangles and bindings back to the input surface. This is a geometry-processing operation: it does not start a simulation or read a model file.
For the unit-edge input above, the generated volume is close to, but not identical to, the original volume. Compare Volume with MeshVolume before using the result. For your own assets, choose acceptable volume and surface error based on their smallest important features.
What are tetrahedral meshes?
A triangle mesh describes a surface. A tetrahedral mesh describes a volume using elements with four vertices each. In this API, Tetrahedra is a flat index array: consecutive groups of four refer to entries in Vertices. Divide its length by four to count elements, not by three as you would with a surface mesh.
SurfaceTriangles is the generated volume’s outward-facing boundary, with three indices per face. It is not simply the input triangles copied into a new array. The generator cuts elements from a body-centred cubic lattice, keeps those whose centroids are inside the input, then moves boundary vertices toward the surface subject to quality checks. Consequently the output boundary need not reproduce every input feature exactly.
The pinned authoring-tools specification describes that process and its input requirements. Open surfaces, inconsistent edge orientation and reversed winding are not suitable inputs. A closed surface alone is also not evidence that the surface has no self-intersections: this tool does not detect them.
Why use a tetrahedral mesh?
Use volume elements when you need material throughout the interior rather than a surface-only representation. OnePhysics exposes ToSoftBodyMesh() to turn the generated tetrahedra into the mesh type used by its soft-body API. That conversion prepares geometry; it does not add a body to a world, select a material or run a step.
The Bindings array has one entry per input vertex. Each entry identifies a tetrahedron and barycentric weights on its vertices, allowing the original surface to follow the volume’s displacement. Retain those bindings if you intend to display the input surface rather than the generated boundary. They connect the two meshes; they do not guarantee a zero-distance match between them.
How do I tetrahedralize a triangle mesh in C#?
Start with the runnable example, then replace both input arrays together. Indices must refer to the supplied positions. The default cell size is the longest bounding-box extent divided by 16. TargetEdgeLength instead selects the lattice cell side explicitly, in the same units as the positions. It is not a promise that every output edge has exactly that length.
Check more than the element count. QualityMet reports the radius-edge quality condition, not whether the volume error meets your needs. SurfaceDeviation measures the boundary fit at the documented sample points; SkipDeviation skips that measurement and makes the property NaN. Volume and MeshVolume give you a separate check of the filled amount. None of these alone establishes that a thin feature has enough elements for your intended deformation.
A smaller cell increases resolution but also uses more lattice cells. Excessive resolution raises an invalid-options error rather than silently accepting an unbounded grid. Inspect the result at a practical resolution before refining it. The coarse variation above shows how increasing the cell size reduces the element count for this input.