{
 "id": "marching-tetrahedra",
 "title": "Marching Tetrahedra",
 "aliases": [],
 "domain": "geometry",
 "subdomain": "isosurface extraction",
 "stage": "geometry",
 "realtime": "interactive",
 "platforms": [
  "cpu",
  "webgl2",
  "webgpu",
  "desktop-gpu",
  "cuda"
 ],
 "year_introduced": 1988,
 "inputs": [
  "voxels",
  "sdf"
 ],
 "outputs": [
  "mesh"
 ],
 "style": "both",
 "dynamic": "both",
 "summary": "Splits each grid cube into tetrahedra and triangulates the iso-surface inside each one using just three cases. It removes the ambiguity of marching cubes and works on arbitrary tetrahedral meshes.",
 "how_it_works": "Each cube is divided into five or six tetrahedra sharing a diagonal (or the grid is a body-centred / Freudenthal tetrahedralization). In a tetrahedron the four vertex signs give at most 16 combinations that collapse to three non-trivial cases: one vertex apart (one triangle), two against two (a quad, i.e. two triangles) and the empty cases. Because a tetrahedron is a simplex, the surface inside it is unambiguous and the resulting mesh is guaranteed watertight and crack-free across neighbours as long as the split directions agree on shared faces. Crossing points are interpolated linearly along edges exactly as in marching cubes. Because it operates on simplices, the same code runs on simulation tetrahedral meshes and adaptive grids.",
 "when_to_use": "Choose it when the field lives on an unstructured tet mesh (FEM output, adaptive grids) or when you want guaranteed topology consistency without a 256-entry table. Prefer `marching-cubes` when you want fewer triangles from a regular grid (tets produce roughly twice as many), and `dual-contouring` when sharp features matter.",
 "cost": {
  "class": "medium",
  "notes": "Linear in cell count with a constant factor several times that of marching cubes because each cube becomes five or six tets."
 },
 "failure_modes": [
  "Noticeably more and thinner triangles than marching cubes",
  "Visible diagonal bias: the tetrahedral split direction leaves a faint directional pattern on flat regions",
  "Same corner-rounding as marching cubes; sharp features are lost",
  "Linear interpolation across a coarse grid gives a faceted look unless normals come from the field"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1016/0167-8396(88)90013-1",
   "type": "paper",
   "title": "Polygonization of implicit surfaces",
   "authors": [
    "Jules Bloomenthal"
   ],
   "year": 1988,
   "venue": "Computer Aided Geometric Design",
   "url": "https://doi.org/10.1016/0167-8396(88)90013-1",
   "ids": {
    "doi": "10.1016/0167-8396(88)90013-1"
   },
   "license": null,
   "extra": {
    "csl_type": "journal-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1016/0167-8396(88)90013-1",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1016/0167-8396(88)90013-1",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1016/0167-8396(88)90013-1",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1016/0167-8396(88)90013-1",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1016/0167-8396(88)90013-1",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "bloomenthal1988polygonization"
  },
  {
   "role": "implementation",
   "key": "gh:mikolalysenko/isosurface",
   "type": "code",
   "title": "mikolalysenko/isosurface",
   "description": "Isosurface polygonizer algorithms",
   "authors": [
    "mikolalysenko"
   ],
   "year": 2013,
   "venue": "GitHub",
   "url": "https://github.com/mikolalysenko/isosurface",
   "ids": {
    "repo": "mikolalysenko/isosurface"
   },
   "license": "MIT",
   "extra": {
    "stars": 195,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2014-04-29T18:52:01Z",
    "language": "JavaScript",
    "homepage": null,
    "license_name": "MIT License"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mikolalysenko/isosurface",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mikolalysenko/isosurface",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mikolalysenko/isosurface",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mikolalysenko/isosurface",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mikolalysenko/isosurface",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mikolalysenko/isosurface",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "mikolalysenko2013mikolalysenkob"
  }
 ],
 "presets": [
  {
   "id": "marching-tetrahedra-live",
   "title": "Marching Tetrahedra — 6-tet Kuhn voxel decomposition (left) vs watertight extracted isosurface (right)",
   "runtime": "glsl-fragment",
   "params": {
    "P_CELL_RES": 7,
    "P_ISO_LEVEL": 0,
    "P_WIRE_GAIN": 0.9,
    "P_SPLIT": 0.48
   },
   "basis": "original",
   "license_note": "MIT — written for this index.",
   "code": "const float P_CELL_RES = 7.0; // P Voxel Grid Res [4.0, 14.0, 1.0]\nconst float P_ISO_LEVEL = 0.0; // P Isovalue Shift [-0.15, 0.15, 0.01]\nconst float P_WIRE_GAIN = 0.90; // P Tet Wireframe [0.0, 1.5, 0.05]\nconst float P_SPLIT = 0.48; // P Tet Facets / Smooth [0.0, 1.0, 0.02]\n\nmat2 rot2(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }\n\nfloat smin(float a, float b, float k) {\n  float h = clamp(0.5 + 0.5 * (b - a) / max(k, 1e-4), 0.0, 1.0);\n  return mix(b, a, h) - k * h * (1.0 - h);\n}\n\n// Smooth organic metaball scalar field f(p) (classic ambiguous saddle for Marching Cubes, clean in Marching Tetrahedra!)\nfloat scalarField(vec3 q, float t, float iso) {\n  vec3 c1 = vec3(0.38 * sin(t * 0.9), 0.24 * cos(t * 0.7), 0.18 * sin(t * 0.5));\n  vec3 c2 = vec3(-0.38 * cos(t * 0.8), -0.22 * sin(t * 0.9), -0.15 * cos(t * 0.6));\n  vec3 c3 = vec3(0.0, 0.34 * sin(t * 1.1), 0.30 * cos(t * 0.85));\n  float d1 = length(q - c1) - 0.38;\n  float d2 = length(q - c2) - 0.36;\n  float d3 = length(q - c3) - 0.32;\n  return smin(smin(d1, d2, 0.28), d3, 0.28) - iso;\n}\n\n// Identify which of the 6 Kuhn tetrahedra inside the unit cube [0,1]^3 contains f:\n// Ordering of (f.x, f.y, f.z) defines the 6 canonical tetrahedra sharing main diagonal (0,0,0)-(1,1,1)!\nint kuhnTetId(vec3 f) {\n  if (f.x >= f.y && f.y >= f.z) return 0;\n  if (f.x >= f.z && f.z >= f.y) return 1;\n  if (f.z >= f.x && f.x >= f.y) return 2;\n  if (f.z >= f.y && f.y >= f.x) return 3;\n  if (f.y >= f.z && f.z >= f.x) return 4;\n  return 5;\n}\n\n// Piecewise-linear tetrahedral interpolation of the scalar field inside the Kuhn simplex!\nfloat evalTetLinear(vec3 q, float res, float t, float iso) {\n  vec3 g = q * res;\n  vec3 cell = floor(g);\n  vec3 f = fract(g);\n  float invR = 1.0 / res;\n\n  // Sort f.x, f.y, f.z to locate the 4 vertices of the enclosing Kuhn tetrahedron\n  vec3 v0 = cell;\n  vec3 v3 = cell + vec3(1.0);\n  vec3 v1, v2;\n  float w0, w1, w2, w3;\n\n  if (f.x >= f.y && f.y >= f.z) {\n    v1 = cell + vec3(1.0, 0.0, 0.0); v2 = cell + vec3(1.0, 1.0, 0.0);\n    w0 = 1.0 - f.x; w1 = f.x - f.y; w2 = f.y - f.z; w3 = f.z;\n  } else if (f.x >= f.z && f.z >= f.y) {\n    v1 = cell + vec3(1.0, 0.0, 0.0); v2 = cell + vec3(1.0, 0.0, 1.0);\n    w0 = 1.0 - f.x; w1 = f.x - f.z; w2 = f.z - f.y; w3 = f.y;\n  } else if (f.z >= f.x && f.x >= f.y) {\n    v1 = cell + vec3(0.0, 0.0, 1.0); v2 = cell + vec3(1.0, 0.0, 1.0);\n    w0 = 1.0 - f.z; w1 = f.z - f.x; w2 = f.x - f.y; w3 = f.y;\n  } else if (f.z >= f.y && f.y >= f.x) {\n    v1 = cell + vec3(0.0, 0.0, 1.0); v2 = cell + vec3(0.0, 1.0, 1.0);\n    w0 = 1.0 - f.z; w1 = f.z - f.y; w2 = f.y - f.x; w3 = f.x;\n  } else if (f.y >= f.z && f.z >= f.x) {\n    v1 = cell + vec3(0.0, 1.0, 0.0); v2 = cell + vec3(0.0, 1.0, 1.0);\n    w0 = 1.0 - f.y; w1 = f.y - f.z; w2 = f.z - f.x; w3 = f.x;\n  } else {\n    v1 = cell + vec3(0.0, 1.0, 0.0); v2 = cell + vec3(1.0, 1.0, 0.0);\n    w0 = 1.0 - f.y; w1 = f.y - f.x; w2 = f.x - f.z; w3 = f.z;\n  }\n\n  return w0 * scalarField(v0 * invR, t, iso)\n       + w1 * scalarField(v1 * invR, t, iso)\n       + w2 * scalarField(v2 * invR, t, iso)\n       + w3 * scalarField(v3 * invR, t, iso);\n}\n\nfloat mapWorld(vec3 p, bool faceted, float res, float t, float iso) {\n  vec3 q = p;\n  q.xz = rot2(t * 0.35) * q.xz;\n  return faceted ? evalTetLinear(q, res, t, iso) : scalarField(q, t, iso);\n}\n\nvec3 calcNormal(vec3 p, bool faceted, float res, float t, float iso) {\n  vec2 e = vec2(faceted ? 0.003 : 0.002, 0.0);\n  return normalize(vec3(\n    mapWorld(p + e.xyy, faceted, res, t, iso) - mapWorld(p - e.xyy, faceted, res, t, iso),\n    mapWorld(p + e.yxy, faceted, res, t, iso) - mapWorld(p - e.yxy, faceted, res, t, iso),\n    mapWorld(p + e.yyx, faceted, res, t, iso) - mapWorld(p - e.yyx, faceted, res, t, iso)\n  ));\n}\n\nvec3 tetColor(int id) {\n  if (id == 0) return vec3(0.95, 0.42, 0.24);\n  if (id == 1) return vec3(0.96, 0.74, 0.22);\n  if (id == 2) return vec3(0.22, 0.84, 0.56);\n  if (id == 3) return vec3(0.18, 0.72, 0.94);\n  if (id == 4) return vec3(0.58, 0.46, 0.96);\n  return vec3(0.92, 0.38, 0.72);\n}\n\nvec3 aces(vec3 x) {\n  x = max(x, 0.0);\n  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n  float screenX = fragCoord.x / max(iResolution.x, 1.0);\n  float split = clamp(P_SPLIT, 0.0, 1.0);\n  bool isSmooth = screenX >= split;\n  float res = clamp(floor(P_CELL_RES + 0.5), 4.0, 14.0);\n  float iso = clamp(P_ISO_LEVEL, -0.18, 0.18);\n  float wireGain = clamp(P_WIRE_GAIN, 0.0, 1.5);\n  float t = iTime;\n\n  vec3 ro = vec3(0.0, 0.06, 2.38);\n  vec3 rd = normalize(vec3(uv, -1.55));\n\n  vec3 col = mix(vec3(0.025, 0.036, 0.058), vec3(0.072, 0.092, 0.142), smoothstep(-0.55, 0.55, uv.y));\n\n  float tHit = 0.0;\n  bool hit = false;\n  for (int i = 0; i < 72; i++) {\n    float d = mapWorld(ro + rd * tHit, !isSmooth, res, t, iso);\n    if (d < 0.002) { hit = true; break; }\n    tHit += d * 0.72;\n    if (tHit > 7.5) break;\n  }\n\n  if (hit) {\n    vec3 p = ro + rd * tHit;\n    vec3 n = calcNormal(p, !isSmooth, res, t, iso);\n\n    vec3 objP = p;\n    objP.xz = rot2(t * 0.35) * objP.xz;\n    vec3 f = fract(objP * res);\n    int tid = kuhnTetId(f);\n\n    // Distance to any of the 6 Kuhn tetrahedron faces (cube boundaries + diagonal planes x=y, y=z, z=x)\n    float dCube = min(min(min(f.x, 1.0 - f.x), min(f.y, 1.0 - f.y)), min(f.z, 1.0 - f.z));\n    float dDiag = min(min(abs(f.x - f.y), abs(f.y - f.z)), abs(f.z - f.x)) * 0.7071;\n    float tetWire = smoothstep(0.055, 0.012, min(dCube, dDiag));\n\n    vec3 l1 = normalize(vec3(0.58, 0.84, 0.52));\n    vec3 l2 = normalize(vec3(-0.68, -0.22, 0.54));\n    float diff = max(dot(n, l1), 0.0) * 0.85 + max(dot(n, l2), 0.0) * 0.25;\n    float spec = pow(max(dot(n, normalize(l1 - rd)), 0.0), 46.0);\n\n    if (!isSmooth) {\n      // Left: Exact piecewise-linear Marching Tetrahedra facets colored by the 6 Kuhn tetrahedron IDs\n      vec3 tCol = tetColor(tid);\n      col = tCol * (0.28 + 0.82 * diff) + vec3(0.95) * spec * 0.35;\n      col = mix(col, vec3(0.04, 0.06, 0.10), tetWire * 0.72 * min(wireGain, 1.2));\n    } else {\n      // Right: Watertight smooth isosurface with subtle tetrahedral edge lattice\n      vec3 alb = vec3(0.20, 0.76, 0.78);\n      col = alb * (0.26 + 0.84 * diff) + vec3(0.98) * spec * 0.62;\n      col = mix(col, vec3(0.06, 0.14, 0.20), tetWire * 0.24 * min(wireGain, 1.0));\n    }\n  }\n\n  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));\n  col = mix(col, vec3(0.95, 0.97, 1.0), sep);\n\n  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "conceptual-diagram",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "marching-tetrahedra",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T19:53:30.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.4535,
      "std": 0.2125,
      "min": 0.1886,
      "max": 0.9177
     },
     "image": "/specimens/marching-tetrahedra-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "marching-cubes",
  "dual-contouring",
  "surface-nets"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "marching-cubes",
   "why": "Same field input; tets trade triangle count for topological certainty."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/geometry/marching-tetrahedra.json"
 },
 "solves": [
  "marching cubes gives ambiguous holes or cracks",
  "iso-surface needed from a tetrahedral mesh",
  "extract surface from fem simulation output",
  "need topology-consistent surface extraction without a big table"
 ],
 "authority": "Bloomenthal, 1988",
 "url": "https://texelatlas.vercel.app/t/marching-tetrahedra/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 44.13,
  "tier": "heavy",
  "p50_ms": 1.3681,
  "p95_ms": 1.4355,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 36,
  "preset_id": "marching-tetrahedra-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 34.98,
   "tier": "heavy",
   "p50_ms": 0.5562,
   "p95_ms": 0.599,
   "speedup_x": 2.46,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 29.76,
   "tier": "heavy",
   "p50_ms": 0.8571,
   "p95_ms": 0.8891,
   "speedup_x": 1.6,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}