{
 "id": "exact-euclidean-distance-transform",
 "title": "Exact Euclidean Distance Transform",
 "aliases": [
  "EDT",
  "Felzenszwalb distance transform"
 ],
 "domain": "geometry",
 "subdomain": "distance transform",
 "stage": "geometry",
 "realtime": "interactive",
 "platforms": [
  "cpu",
  "cuda",
  "webgpu",
  "python"
 ],
 "year_introduced": 2012,
 "inputs": [
  "voxels",
  "texture"
 ],
 "outputs": [
  "sdf",
  "texture"
 ],
 "style": "both",
 "dynamic": "static",
 "summary": "Computes the exact squared Euclidean distance from every grid cell to the nearest feature in linear time by running a one-dimensional lower-envelope-of-parabolas pass along each axis.",
 "how_it_works": "The squared distance to a set of seeds is the minimum over seeds of a parabola per cell: f(q) = min_p ((q-p)^2 + g(p)). In one dimension, that minimum is the lower envelope of parabolas centred at each sample, which can be built in a single left-to-right sweep that keeps a stack of parabola indices and the boundaries where one overtakes the next. Evaluating the envelope at each grid position is a second sweep. Because squared Euclidean distance is separable, doing the 1D transform along rows, then columns (then depth for 3D) yields the exact result in O(n) total. The signed version runs it on both the shape and its complement and subtracts the square roots.",
 "when_to_use": "Choose it when you need exact SDFs from binary images or voxel volumes for baking font SDFs, collision fields, `marching-cubes` input or ambient occlusion, and when CPU or compute-shader time is acceptable. Prefer `jump-flooding-algorithm` for per-frame, GPU-render-pass use where small errors are acceptable.",
 "cost": {
  "class": "cheap",
  "notes": "Linear in grid cells; separable, so each row or column is an independent parallel task."
 },
 "failure_modes": [
  "Distances are to cell centres, not to the sub-cell boundary, so binary inputs yield staircase fields unless anti-aliased input is used",
  "Values near thin features differ by up to half a cell from the true surface distance",
  "Memory-bound for large 3D volumes",
  "Squared values overflow low-precision storage unless scaled"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "url:https://theoryofcomputing.org/articles/v008a019",
   "type": "web",
   "title": "Distance Transforms of Sampled Functions",
   "authors": [
    "Pedro F. Felzenszwalb",
    "Daniel P. Huttenlocher"
   ],
   "year": 2012,
   "venue": "Theory of Computing 8(19), 2012",
   "url": "https://theoryofcomputing.org/articles/v008a019",
   "ids": {},
   "license": null,
   "extra": {
    "http_status": 200,
    "live": true,
    "archive": {
     "url": "http://web.archive.org/web/20261031135048/https://theoryofcomputing.org/articles/v008a019/",
     "timestamp": "20261031135048"
    },
    "page_title": "Distance Transforms of Sampled Functions: Theory of Computing: An Open Access Electronic Journal in Theoretical Computer Science",
    "host": "theoryofcomputing.org",
    "title_confirmed_by_page": true,
    "title_unconfirmed": false
   },
   "provenance": {
    "title": {
     "authority": "http",
     "endpoint": "GET https://theoryofcomputing.org/articles/v008a019 → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "http",
     "endpoint": "GET https://theoryofcomputing.org/articles/v008a019 → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 0.95,
   "__bib": "felzenszwalb2012distance"
  }
 ],
 "presets": [
  {
   "id": "exact-euclidean-distance-transform-specimen",
   "title": "Toy: the distance field this transform outputs (computed by brute force, not the linear-time algorithm)",
   "runtime": "glsl-fragment",
   "params": {
    "N": 14,
    "BAND": 0.06
   },
   "code": "// Exact Euclidean distance to the nearest feature (brute-force reference of what the linear-time EDT outputs), shown as banded field.\nconst int N = 14;\nconst float BAND = 0.06;\nvec2 site(int i) {\n    float f = float(i);\n    return vec2(fract(sin(f * 12.9898) * 43758.5), fract(sin(f * 78.233 + 1.0) * 24634.6)) * 0.84 + 0.08;\n}\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n    vec2 uv = fragCoord / iResolution.xy;\n    float d2 = 1e9; int id = 0;\n    for (int i = 0; i < N; i++) {\n        vec2 q = uv - site(i);\n        float d = dot(q, q);\n        if (d < d2) { d2 = d; id = i; }\n    }\n    float d = sqrt(d2);\n    vec3 col = 0.5 + 0.5 * cos(6.2831 * (d * 2.2) + vec3(0.0, 0.8, 1.6));\n    col *= 0.35 + 0.65 * smoothstep(0.0, 0.5, abs(fract(d / BAND) - 0.5) + 0.3);\n    col = mix(col, vec3(0.0), smoothstep(0.004, 0.0, abs(fract(d / BAND + 0.5) - 0.5) * BAND - 0.001) * 0.6);\n    col = mix(col, vec3(1.0), smoothstep(0.012, 0.008, d));\n    fragColor = vec4(pow(col, vec3(0.8)), 1.0);\n}\n",
   "basis": "original",
   "license_note": "written for this index",
   "execution_fidelity": "conceptual-diagram",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-29",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "exact-euclidean-distance-transform",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-29T15:51:50.471Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.3438,
      "std": 0.2398,
      "unique_colors": 444
     },
     "image": "/specimens/exact-euclidean-distance-transform-specimen.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "jump-flooding-algorithm",
  "sdf-csg-smooth-min",
  "marching-cubes"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "marching-cubes",
   "why": "Exact SDF from a binary volume gives a smoother input for contouring."
  }
 ],
 "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/exact-euclidean-distance-transform.json"
 },
 "solves": [
  "need accurate distance field from a binary image",
  "baking font or shape sdfs gives lumpy errors",
  "approximate distance fields are not precise enough",
  "generate a clean distance volume from voxels"
 ],
 "authority": "Felzenszwalb & Huttenlocher, 2012",
 "url": "https://texelatlas.vercel.app/t/exact-euclidean-distance-transform/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 2.74,
  "tier": "light",
  "p50_ms": 0.0901,
  "p95_ms": 0.0924,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "exact-euclidean-distance-transform-specimen",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 1.92,
   "tier": "feather",
   "p50_ms": 0.0306,
   "p95_ms": 0.0309,
   "speedup_x": 2.94,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 1.74,
   "tier": "feather",
   "p50_ms": 0.0501,
   "p95_ms": 0.0503,
   "speedup_x": 1.8,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}