---
title: "Tetrahedralize a triangle mesh in C#"
url: https://recursiveai.org/onephysics/cookbook/tetrahedralize-surface-mesh/
description: "Fill a closed triangle surface with tetrahedra in C# using OnePhysics.Tools, inspect volume and quality, and create a soft-body mesh."
version: "1.0.0"
---

# Tetrahedralize a triangle mesh in C#

**OnePhysics** is a free, open-source physics library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePhysics.Tools) · [GitHub](https://github.com/recursive-ai-oss/onephysics-csharp) · [Docs](https://recursiveai.org/onephysics/docs/) · [Download example (.zip)](https://recursiveai.org/onephysics/cookbook/tetrahedralize-surface-mesh/tetrahedralize-surface-mesh.zip)

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.

```shell
dotnet add package RecursiveAI.OnePhysics.Tools
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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}"));
```

```text
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](https://recursiveai.org/onephysics/cookbook/tetrahedralize-surface-mesh/tetrahedralize-surface-mesh.zip), 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.

```csharp
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%.");
```

```text
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.

```csharp
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}");
```

```text
Tetrahedra: 896
Soft-body mesh: True
```

## Related recipes

- [Simulate cloth in C#](https://recursiveai.org/onephysics/cookbook/simulate-cloth/)
- [Run a 3D physics simulation](https://recursiveai.org/onephysics/cookbook/3d-preview/)

## 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](https://github.com/recursive-ai-oss/onephysics-csharp/blob/11dad30ed91fae25354edb55930c67cc0f71fe25/docs/spec/tools.md#tetrahedralization-tetrahedralizer) 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.
