macro librarylibs/Scene/src/Scene.xtlm

Scene: named v1 scene arrays from a small declarative notation. Import with the ordinary constructors: "sc:" u_se< "Scene". The statement macro resolves labels after parsing, emits literal arrays, and leaves runtime animation/rendering to ordinary functions.

source · imports sc: libs/Scene/src/Scene.xtl; pa: libs/Parsing/src/Parsing.xtl; t: libs/Strings/src/Strings.xtl; v: libs/Vector3/src/Vector3.xtl

ʰb̲ad : Any a => Char -> a

function (private) · line 11

A typed failure caught at the macro boundary and reported at its input.

ʰb̲ad ← { message → "bad-scene" ⎕S̲IGNAL message }

ʰc̲ontext : Any a => (Unit -> a) -> Char -> a

function (private) · line 14

Add the complete declaration to a parser or ordinary-constructor error.

ʰc̲ontext ← { f̲ text →
  'f̲ ⎕T̲RAP '{ e → ⎕R̲ECOVER "bad-scene" ⎕S̲IGNAL text c̲at ": " c̲at ⎕E̲MESSAGE e }
}

ʰw̲ord : Any a => Int -> Box a -> a

function (private) · line 19

One word from a boxed token vector.

ʰw̲ord ← { i words → d̲isclose i s̲elect words }

ʰk̲ey : Char -> Int -> Box Char -> Char

function (private) · line 22

Require a keyword at an already length-checked token position.

ʰk̲ey ← { expected i words →
  actual ← i ʰw̲ord words
  n̲ot expected m̲atch actual ? ʰb̲ad "expected " c̲at expected c̲at ", got " c̲at actual
  ""
}

ʰe̲xact : Int -> a -> Char

function (private) · line 29

Exactly n tokens, with no silently ignored suffix.

ʰe̲xact ← { n words →
  n̲ot n = t̲ally words ? ʰb̲ad "expected " c̲at (f̲ormat n) c̲at " tokens, got " c̲at f̲ormat t̲ally words
  ""
}

ʰn̲ame : Char -> Char

function (private) · line 35

Validate labels in one shared namespace, excluding grammar words.

ʰn̲ame ← { name →
  reserved ← "identity" "camera" "eye" "target" "up" "fov" "light" "position" "color" "material" "reflection" "object" "extrude" "depth" "group" "objects" "pivot"
  n̲ot ᵖᵃn̲ame? name ? ʰb̲ad "invalid label: " c̲at name
  (e̲nclose name) m̲ember? reserved ? ʰb̲ad "reserved label: " c̲at name
  name
}

ʰu̲nsigned : Char -> Char

function (private) · line 43

Drop one optional sign before validating a numeric component.

ʰu̲nsigned ← { text →
  0 = t̲ally text ? text
  (f̲irst text) m̲ember? "+-" ? 1 d̲rop text
  text
}
Used in: ʰn̲umber

ʰd̲ecimal? : Truthy a => Char -> a

function (private) · line 50

Parsing checks a padded unsigned decimal; at least one digit is required.

ʰd̲ecimal? ← { text →
  0 = t̲ally w̲here text m̲ember? ⎕D ? 0 = 1
  a ← { @ → (f̲irst text) = f̲irst "." ? "0" c̲at text◆ text } @
  b ← { @ → (f̲irst -1 t̲ake a) = f̲irst "." ? a c̲at "0"◆ a } @
  ᵖᵃn̲umber? b
}
Used in: ʰn̲umber

ʰn̲umber : Char -> Float

function (private) · line 58

Parse one finite signed decimal with optional signed decimal exponent.

ʰn̲umber ← { text →
  b ← ʰu̲nsigned text
  at ← w̲here b m̲ember? "eE"
  (t̲ally at) > 1 ? ʰb̲ad "invalid number: " c̲at text
  mantissa ← { @ → 0 = t̲ally at ? b◆ ((f̲irst at) − 1) t̲ake b } @
  n̲ot ʰd̲ecimal? mantissa ? ʰb̲ad "invalid number: " c̲at text
  exponent ← { @ → 0 = t̲ally at ? "0"◆ ʰu̲nsigned (f̲irst at) d̲rop b } @
  n̲ot (0 < t̲ally exponent) ∧ '∧ r̲/ exponent m̲ember? ⎕D ? ʰb̲ad "invalid exponent: " c̲at text
  nums ← n̲umbers text
  n̲ot 1 = t̲ally nums ? ʰb̲ad "expected one number: " c̲at text
  value ← f̲irst nums
  n̲ot (value − value) = 0.0 ? ʰb̲ad "number must be finite: " c̲at text
  value
}

ʰn̲umbers : Int -> Box Char -> Float

function (private) · line 74

Parse numeric tokens at an explicit list of positions.

ʰn̲umbers ← { indices words → '{ i → ʰn̲umber i ʰw̲ord words } e̲ach indices }

ʰl̲ines : Char -> Box Char

function (private) · line 77

Strip # comments per line, trim whitespace, and retain nonempty lines.

ʰl̲ines ← { text →
  raw ← "\n" ᵗs̲plit text
  clean ← '{ box →
    line ← d̲isclose box
    comment ← w̲here line = f̲irst "#"
    stop ← (f̲irst comment c̲at 1 + t̲ally line) − 1
    ᵗt̲rim stop t̲ake line
  } m̲ap raw
  ('{ box → 0 < t̲ally d̲isclose box } e̲ach clean) r̲eplicate clean
}

ʰk̲ind : Char -> Char

function (private) · line 89

Declaration kind is its first word; no unknown line is dropped.

ʰk̲ind ← { line → 1 ʰw̲ord ᵗw̲ords line }

ʰo̲f : Char -> Box Char -> Box Char

function (private) · line 92

Select declarations of one kind, preserving source order.

ʰo̲f ← { kind lines → ('{ box → kind m̲atch ʰk̲ind d̲isclose box } e̲ach lines) r̲eplicate lines }

ʰc̲amera : Char -> Float

function (private) · line 95

Camera constructor parses all fifteen tokens and reuses shared checks.

ʰc̲amera ← { line →
  '{ @ →
    w ← ᵗw̲ords line
    _ ← 15 ʰe̲xact w
    _ ← ("eye" ʰk̲ey 2) w
    _ ← ("target" ʰk̲ey 6) w
    _ ← ("up" ʰk̲ey 10) w
    _ ← ("fov" ʰk̲ey 14) w
    ˢᶜc̲amera 3 4 5 7 8 9 11 12 13 15 ʰn̲umbers w
  } ʰc̲ontext line
}

ʰm̲aterial : Char -> (Char, Float)

function (private) · line 108

Compile-time material record: label and one validated numeric row.

ʰm̲aterial ← { line →
  '{ @ →
    w ← ᵗw̲ords line
    _ ← 8 ʰe̲xact w
    _ ← ("color" ʰk̲ey 3) w
    _ ← ("reflection" ʰk̲ey 7) w
    name ← ʰn̲ame 2 ʰw̲ord w
    row ← ˢᶜm̲aterials 1 4 r̲eshape 4 5 6 8 ʰn̲umbers w
    (name, row)
  } ʰc̲ontext line
}

ʰl̲ight : Char -> (Char, Float)

function (private) · line 121

Compile-time light record: label and validated point/intensity row.

ʰl̲ight ← { line →
  '{ @ →
    w ← ᵗw̲ords line
    _ ← 10 ʰe̲xact w
    _ ← ("position" ʰk̲ey 3) w
    _ ← ("color" ʰk̲ey 7) w
    name ← ʰn̲ame 2 ʰw̲ord w
    row ← ˢᶜl̲ights 1 6 r̲eshape 4 5 6 8 9 10 ʰn̲umbers w
    (name, row)
  } ʰc̲ontext line
}

ʰo̲bject : Char -> (Char, Float, Char)

function (private) · line 134

Object record holds its label, mesh and unresolved material reference.

ʰo̲bject ← { line →
  '{ @ →
    w ← ᵗw̲ords line
    n̲ot (8 = t̲ally w) ∨ 12 = t̲ally w ? ʰb̲ad "object needs extrude WORD depth NUMBER material LABEL and optional position X Y Z"
    _ ← ("extrude" ʰk̲ey 3) w
    _ ← ("depth" ʰk̲ey 5) w
    _ ← ("material" ʰk̲ey 7) w
    name ← ʰn̲ame 2 ʰw̲ord w
    asset ← 4 ʰw̲ord w
    depth ← ʰn̲umber 6 ʰw̲ord w
    material ← ʰn̲ame 8 ʰw̲ord w
    offset ← { @ →
      8 = t̲ally w ? 0.0 0.0 0.0
      _ ← ("position" ʰk̲ey 9) w
      10 11 12 ʰn̲umbers w
    } @
    mesh ← ((depth ˢᶜo̲bject asset) offset)
    (name, mesh, material)
  } ʰc̲ontext line
}

ʰg̲roup : Char -> (Char, Box Char, Float)

function (private) · line 156

Group record holds its label, unresolved object names, and local pivot.

ʰg̲roup ← { line →
  '{ @ →
    w ← ᵗw̲ords line
    (t̲ally w) < 4 ? ʰb̲ad "group needs objects and at least one object label"
    _ ← ("objects" ʰk̲ey 3) w
    name ← ʰn̲ame 2 ʰw̲ord w
    rest ← 3 d̲rop w
    at ← w̲here '{ box → "pivot" m̲atch d̲isclose box } e̲ach rest
    (t̲ally at) > 1 ? ʰb̲ad "group has more than one pivot"
    stop ← (f̲irst at c̲at 1 + t̲ally rest) − 1
    members ← '{ box → ʰn̲ame d̲isclose box } m̲ap stop t̲ake rest
    0 = t̲ally members ? ʰb̲ad "group needs at least one object label"
    n̲ot (t̲ally members) = t̲ally u̲nique members ? ʰb̲ad "group repeats an object label"
    pivot ← { @ →
      0 = t̲ally at ? 0.0 0.0 0.0
      after ← (f̲irst at) d̲rop rest
      _ ← 3 ʰe̲xact after
      1 2 3 ʰn̲umbers after
    } @
    (name, members, pivot)
  } ʰc̲ontext line
}

ʰr̲aze : a -> Box a -> a

function (private) · line 180

Concatenate boxed numeric rows/meshes, including typed empty output.

ʰr̲aze ← { fill boxes →
  0 = t̲ally boxes ? 0 r̲eshape fill
  d̲isclose '{ e̲nclose (r̲avel d̲isclose ⍺) c̲at r̲avel d̲isclose ⍵ } r̲/ boxes
}

ʰf̲ield : Char -> Char -> a -> Char -> Char

function (private) · line 186

Serialize numeric arrays, supplying a typed fill even for an empty shape.

ʰf̲ield ← { name suffix array fill →
  body ← { @ → 0 = t̲ally r̲avel array ? fill◆ f̲ormat r̲avel array } @
  name c̲at suffix c̲at " := " c̲at (f̲ormat s̲hape array) c̲at " r_eshape " c̲at body c̲at "\n"
}

ᵐs̲cene< : Char -> Char -> Char

macro · line 197

"logo" sc:s_cene< TEXT creates the eight prefix-named v1 arrays. Declaration lines contain camera, material, light, extruded word object, or flat group with optional pivot. # starts an end-of-line comment. Object position is optional; forward references are resolved after parsing.

ᵐs̲cene< ← { prefix text →
  n̲ot ⎕S̲TATEMENT @ ? "bad-scene call" ⎕R̲EJECT "scene macro creates bindings and must stand as a statement"
  0 = t̲ally prefix ? "bad-scene left" ⎕R̲EJECT "scene prefix must be nonempty"
  n̲ot ᵖᵃn̲ame? prefix ? "bad-scene left" ⎕R̲EJECT "scene prefix must be a lowercase variable name: " c̲at prefix
  '{ @ →
    lines ← ʰl̲ines text
    unknown ← (n̲ot '{ box → (e̲nclose ʰk̲ind d̲isclose box) m̲ember? "camera" "material" "light" "object" "group" } e̲ach lines) r̲eplicate lines
    0 < t̲ally unknown ? ʰb̲ad "unknown declaration: " c̲at d̲isclose f̲irst unknown
    cameras ← "camera" ʰo̲f lines
    n̲ot 1 = t̲ally cameras ? ʰb̲ad "scene needs exactly one camera declaration"
    camera ← ʰc̲amera d̲isclose f̲irst cameras
    materials ← '{ box → ʰm̲aterial d̲isclose box } m̲ap "material" ʰo̲f lines
    lights ← '{ box → ʰl̲ight d̲isclose box } m̲ap "light" ʰo̲f lines
    objects ← '{ box → ʰo̲bject d̲isclose box } m̲ap "object" ʰo̲f lines
    groups ← '{ box → ʰg̲roup d̲isclose box } m̲ap "group" ʰo̲f lines
    materialnames ← '{ box → (name, _) ← d̲isclose box◆ name } m̲ap materials
    lightnames ← '{ box → (name, _) ← d̲isclose box◆ name } m̲ap lights
    objectnames ← '{ box → (name, _, _) ← d̲isclose box◆ name } m̲ap objects
    groupnames ← '{ box → (name, _, _) ← d̲isclose box◆ name } m̲ap groups
    names ← materialnames c̲at lightnames c̲at objectnames c̲at groupnames
    n̲ot (t̲ally names) = t̲ally u̲nique names ? ʰb̲ad "duplicate scene labels: " c̲at ", " ᵗj̲oin names
    ⍝ Resolve each group's members after all object labels are known.
    _ ← '{ box →
      (name, members, _) ← d̲isclose box
      bad ← (n̲ot members m̲ember? objectnames) r̲eplicate members
      0 < t̲ally bad ? ʰb̲ad "group " c̲at name c̲at " has unresolved object " c̲at d̲isclose f̲irst bad
      ""
    } m̲ap groups
    ⍝ Determine one Int group per object, defaulting to identity row 1.
    assignments ← '{ box →
      (name, _, _) ← d̲isclose box
      belongs ← '{ grp → (_, members, _) ← d̲isclose grp◆ (e̲nclose name) m̲ember? members } e̲ach groups
      at ← w̲here belongs
      (t̲ally at) > 1 ? ʰb̲ad "object belongs to multiple groups: " c̲at name
      0 = t̲ally at ? 1
      1 + f̲irst at
    } e̲ach objects
    meshparts ← '{ box → (_, mesh, _) ← d̲isclose box◆ r̲avel mesh } m̲ap objects
    flat ← 0.0 ʰr̲aze meshparts
    triangles ← ˢᶜt̲riangles ((((t̲ally flat) d̲iv 9) c̲at 3 3) r̲eshape flat)
    midparts ← '{ box →
      (name, mesh, material) ← d̲isclose box
      id ← f̲irst materialnames i̲ndexOf e̲nclose material
      id > t̲ally materialnames ? ʰb̲ad "object " c̲at name c̲at " has unresolved material " c̲at material
      (t̲ally mesh) r̲eshape id
    } m̲ap objects
    materialids ← 0 ʰr̲aze midparts
    gidparts ← '{ i → (_, mesh, _) ← d̲isclose i s̲elect objects◆ (t̲ally mesh) r̲eshape f̲irst i s̲elect assignments } m̲ap r̲ange t̲ally objects
    groupids ← 0 ʰr̲aze gidparts
    materialrows ← '{ box → (_, row) ← d̲isclose box◆ row } m̲ap materials
    materialarray ← ˢᶜm̲aterials ((t̲ally materials) c̲at 4) r̲eshape 0.0 ʰr̲aze materialrows
    lightrows ← '{ box → (_, row) ← d̲isclose box◆ row } m̲ap lights
    lightarray ← ˢᶜl̲ights ((t̲ally lights) c̲at 6) r̲eshape 0.0 ʰr̲aze lightrows
    pivotrows ← '{ box → (_, _, pivot) ← d̲isclose box◆ pivot } m̲ap groups
    pivots ← ˢᶜp̲ivots ((1 + t̲ally groups) c̲at 3) r̲eshape 0.0 0.0 0.0 c̲at 0.0 ʰr̲aze pivotrows
    transforms ← ˢᶜi̲dentities 1 + t̲ally groups
    f̲ ← { suffix a → ((prefix ʰf̲ield suffix) a) "0.0" }
    i̲ ← { suffix a → ((prefix ʰf̲ield suffix) a) "0" }
    ("triangles" f̲ triangles) c̲at ("materialids" i̲ materialids) c̲at ("groupids" i̲ groupids) c̲at ("materials" f̲ materialarray) c̲at ("transforms" f̲ transforms) c̲at ("pivots" f̲ pivots) c̲at ("camera" f̲ camera) c̲at "lights" f̲ lightarray
  } ⎕T̲RAP '{ e → ⎕R̲ECOVER "bad-scene right" ⎕R̲EJECT ⎕E̲MESSAGE e }
}
Used in: logotriangles

ʰm̲aterial2 : Char -> (Char, Float, Int, Float, Float)

function (private) · line 262

V2 material record, with optional emission RGB after the base declaration.

ʰm̲aterial2 ← { line →
  '{ @ →
    w ← ᵗw̲ords line
    opaque ← "material" m̲atch ʰk̲ind line
    base ← { @ → opaque ? 8◆ 14 } @
    n̲ot (base = t̲ally w) ∨ (base + 4) = t̲ally w ? ʰb̲ad "material needs its base fields and optional emission R G B"
    em ← { @ →
      base = t̲ally w ? 1 3 r̲eshape 0.0
      _ ← ("emission" ʰk̲ey base + 1) w
      ˢᶜe̲mission 1 3 r̲eshape (base + 2 3 4) ʰn̲umbers w
    } @
    opaque ? { @ →
      (name, row) ← ʰm̲aterial " " ᵗj̲oin base t̲ake w
      (name, row, 0 m̲od 1, 1 5 r̲eshape 0.0 1.0 0.0 0.0 0.0, em)
    } @
    _ ← ("color" ʰk̲ey 3) w
    _ ← ("transmission" ʰk̲ey 7) w
    _ ← ("ior" ʰk̲ey 9) w
    _ ← ("absorption" ʰk̲ey 11) w
    name ← ʰn̲ame 2 ʰw̲ord w
    row ← ˢᶜm̲aterials 1 4 r̲eshape (4 5 6 ʰn̲umbers w) c̲at 0.0
    optics ← ˢᶜo̲ptics 1 5 r̲eshape 8 10 12 13 14 ʰn̲umbers w
    (name, row, 1 + 0 m̲od 1, optics, em)
  } ʰc̲ontext line
}
Used in: ᵐs̲cene2<

ʰe̲mitter : Char -> (Char, Float, Float)

function (private) · line 289

Emitter record: label, validated origin/u/v descriptor and radiance row.

ʰe̲mitter ← { line →
  '{ @ →
    w ← ᵗw̲ords line
    _ ← 18 ʰe̲xact w
    _ ← ("origin" ʰk̲ey 3) w
    _ ← ("u" ʰk̲ey 7) w
    _ ← ("v" ʰk̲ey 11) w
    _ ← ("radiance" ʰk̲ey 15) w
    name ← ʰn̲ame 2 ʰw̲ord w
    rect ← ˢᶜe̲mitters 1 3 3 r̲eshape 4 5 6 8 9 10 12 13 14 ʰn̲umbers w
    radiance ← ˢᶜe̲mission 1 3 r̲eshape 16 17 18 ʰn̲umbers w
    (name, rect, radiance)
  } ʰc̲ontext line
}

ʰl̲ens : Char -> Float

function (private) · line 305

Optional v2 lens declaration with aperture radius and axial focus distance.

ʰl̲ens ← { line →
  '{ @ →
    w ← ᵗw̲ords line
    _ ← 5 ʰe̲xact w
    _ ← ("aperture" ʰk̲ey 2) w
    _ ← ("focus" ʰk̲ey 4) w
    ˢᶜl̲ens 3 5 ʰn̲umbers w
  } ʰc̲ontext line
}
Used in: ᵐs̲cene2<

ʰo̲bject2 : Char -> (Char, Float, Float, Char, Int)

function (private) · line 316

V2 object record: label, triangles, spheres, material, closed-volume flag.

ʰo̲bject2 ← { line →
  kind ← ʰk̲ind line
  "emitter" m̲atch kind ? { @ →
    (name, rect, _) ← ʰe̲mitter line
    (name, ˢᶜr̲ectangle 3 3 r̲eshape rect, 0 4 r̲eshape 0.0, name, 0 m̲od 1)
  } @
  "object" m̲atch kind ? { @ →
    (name, mesh, material) ← ʰo̲bject line
    (name, mesh, 0 4 r̲eshape 0.0, material, 1 + 0 m̲od 1)
  } @
  '{ @ →
    w ← ᵗw̲ords line
    name ← ʰn̲ame 2 ʰw̲ord w
    "sphere" m̲atch kind ? { @ →
      _ ← 10 ʰe̲xact w
      _ ← ("center" ʰk̲ey 3) w
      _ ← ("radius" ʰk̲ey 7) w
      _ ← ("material" ʰk̲ey 9) w
      balls ← ˢᶜs̲pheres 1 4 r̲eshape 4 5 6 8 ʰn̲umbers w
      (name, 0 3 3 r̲eshape 0.0, balls, ʰn̲ame 10 ʰw̲ord w, 1 + 0 m̲od 1)
    } @
    "floor" m̲atch kind ? { @ →
      _ ← 16 ʰe̲xact w
      _ ← ("origin" ʰk̲ey 3) w
      _ ← ("u" ʰk̲ey 7) w
      _ ← ("v" ʰk̲ey 11) w
      _ ← ("material" ʰk̲ey 15) w
      mesh ← ˢᶜr̲ectangle 3 3 r̲eshape 4 5 6 8 9 10 12 13 14 ʰn̲umbers w
      (name, mesh, 0 4 r̲eshape 0.0, ʰn̲ame 16 ʰw̲ord w, 0 m̲od 1)
    } @
    n̲ot (8 = t̲ally w) ∨ 12 = t̲ally w ? ʰb̲ad "polyhedron needs size/radii X Y Z material NAME and optional position X Y Z"
    key ← { @ → "box" m̲atch kind ? "size"◆ "radii" } @
    _ ← (key ʰk̲ey 3) w
    _ ← ("material" ʰk̲ey 7) w
    size ← 4 5 6 ʰn̲umbers w
    mesh ← { @ → "box" m̲atch kind ? ˢᶜb̲ox size◆ ˢᶜo̲ctahedron size } @
    offset ← { @ →
      8 = t̲ally w ? 0.0 0.0 0.0
      _ ← ("position" ʰk̲ey 9) w
      10 11 12 ʰn̲umbers w
    } @
    moved ← (s̲hape mesh) r̲eshape offset ᵛt̲ranslate ((3 × t̲ally mesh) c̲at 3) r̲eshape mesh
    (name, moved, 0 4 r̲eshape 0.0, ʰn̲ame 8 ʰw̲ord w, 1 + 0 m̲od 1)
  } ʰc̲ontext line
}
Used in: ᵐs̲cene2<

ʰk̲inds : Box Char -> Box Char -> Box Char

function (private) · line 363

Select multiple declaration kinds while retaining their shared order.

ʰk̲inds ← { kinds lines →
  ('{ box → (e̲nclose ʰk̲ind d̲isclose box) m̲ember? kinds } e̲ach lines) r̲eplicate lines
}
Used in: ᵐs̲cene2<

ᵐs̲cene2< : Char -> Char -> Char

macro · line 372

Explicit v2 opt-in; all v1 declarations retain their meanings. New sphere, box, octahedron, floor and dielectric declarations share the namespace. Emits v1 arrays and the separate v2 fields, with opaque/pinhole defaults.

ᵐs̲cene2< ← { prefix text →
  n̲ot ⎕S̲TATEMENT @ ? "bad-scene call" ⎕R̲EJECT "scene2 macro creates bindings and must stand as a statement"
  (0 = t̲ally prefix) ∨ n̲ot ᵖᵃn̲ame? prefix ? "bad-scene left" ⎕R̲EJECT "scene prefix must be a nonempty lowercase variable name"
  '{ @ →
    lines ← ʰl̲ines text
    allowed ← "camera" "material" "dielectric" "light" "object" "group" "sphere" "box" "octahedron" "floor" "lens" "emitter"
    unknown ← (n̲ot '{ box → (e̲nclose ʰk̲ind d̲isclose box) m̲ember? allowed } e̲ach lines) r̲eplicate lines
    0 < t̲ally unknown ? ʰb̲ad "unknown v2 declaration: " c̲at d̲isclose f̲irst unknown
    cameras ← "camera" ʰo̲f lines
    n̲ot 1 = t̲ally cameras ? ʰb̲ad "scene needs exactly one camera declaration"
    camera ← ʰc̲amera d̲isclose f̲irst cameras
    authored ← '{ box → ʰm̲aterial2 d̲isclose box } m̲ap "material" "dielectric" ʰk̲inds lines
    emitterrecords ← '{ box → ʰe̲mitter d̲isclose box } m̲ap "emitter" ʰo̲f lines
    automatic ← '{ box →
      (name, _, radiance) ← d̲isclose box
      (name, 1 4 r̲eshape 0.0, 0 m̲od 1, 1 5 r̲eshape 0.0 1.0 0.0 0.0 0.0, radiance)
    } m̲ap emitterrecords
    materials ← authored c̲at automatic
    lenslines ← "lens" ʰo̲f lines
    (t̲ally lenslines) > 1 ? ʰb̲ad "scene permits at most one lens declaration"
    lens ← { @ → 0 = t̲ally lenslines ? ˢᶜl̲ens 0.0 1.0◆ ʰl̲ens d̲isclose f̲irst lenslines } @
    lights ← '{ box → ʰl̲ight d̲isclose box } m̲ap "light" ʰo̲f lines
    objects ← '{ box → ʰo̲bject2 d̲isclose box } m̲ap "object" "sphere" "box" "octahedron" "floor" "emitter" ʰk̲inds lines
    groups ← '{ box → ʰg̲roup d̲isclose box } m̲ap "group" ʰo̲f lines
    materialnames ← '{ box → (name, _, _, _, _) ← d̲isclose box◆ name } m̲ap materials
    lightnames ← '{ box → (name, _) ← d̲isclose box◆ name } m̲ap lights
    objectnames ← '{ box → (name, _, _, _, _) ← d̲isclose box◆ name } m̲ap objects
    groupnames ← '{ box → (name, _, _) ← d̲isclose box◆ name } m̲ap groups
    authorednames ← '{ box → (name, _, _, _, _) ← d̲isclose box◆ name } m̲ap authored
    names ← authorednames c̲at lightnames c̲at objectnames c̲at groupnames
    n̲ot (t̲ally names) = t̲ally u̲nique names ? ʰb̲ad "duplicate scene labels"
    reserved ← "dielectric" "transmission" "ior" "absorption" "sphere" "center" "radius" "box" "size" "octahedron" "radii" "floor" "origin" "u" "v" "emitter" "radiance" "lens" "aperture" "focus" "emission"
    '∨ r̲/ names m̲ember? reserved ? ʰb̲ad "reserved v2 scene label"
    _ ← '{ box →
      (name, members, _) ← d̲isclose box
      n̲ot '∧ r̲/ members m̲ember? objectnames ? ʰb̲ad "unresolved group object in " c̲at name
      ""
    } m̲ap groups
    assignments ← '{ box →
      (name, _, _, _, _) ← d̲isclose box
      at ← w̲here '{ grp → (_, members, _) ← d̲isclose grp◆ (e̲nclose name) m̲ember? members } e̲ach groups
      (t̲ally at) > 1 ? ʰb̲ad "object belongs to multiple groups: " c̲at name
      0 = t̲ally at ? 1
      1 + f̲irst at
    } e̲ach objects
    mids ← '{ box →
      (name, mesh, _, material, closed) ← d̲isclose box
      id ← f̲irst materialnames i̲ndexOf e̲nclose material
      id > t̲ally materialnames ? ʰb̲ad "unresolved material for " c̲at name
      (_, _, kind, _, _) ← d̲isclose id s̲elect materials
      (kind = 1) ∧ closed = 0 ? ʰb̲ad "dielectric requires a closed object: " c̲at name
      _ ← { @ → (kind = 1) ∧ (t̲ally mesh) > 0 ? t̲ally ˢᶜc̲losed mesh◆ 0 } @
      id
    } e̲ach objects
    meshflat ← r̲avel 0.0 ʰr̲aze '{ box → (_, mesh, _, _, _) ← d̲isclose box◆ mesh } m̲ap objects
    triangles ← ˢᶜt̲riangles ((((t̲ally meshflat) d̲iv 9) c̲at 3 3) r̲eshape meshflat)
    ballflat ← r̲avel 0.0 ʰr̲aze '{ box → (_, _, balls, _, _) ← d̲isclose box◆ balls } m̲ap objects
    spheres ← ˢᶜs̲pheres ((((t̲ally ballflat) d̲iv 4) c̲at 4) r̲eshape ballflat)
    counts ← '{ box → (_, mesh, balls, _, _) ← d̲isclose box◆ (t̲ally mesh) + t̲ally balls } e̲ach objects
    ⍝ Prefix sums count primitives of both kinds in source declaration order.
    starts ← '{ i → 1 + '+ r̲/ (i − 1) t̲ake counts } e̲ach r̲ange t̲ally objects
    tri̲ ← { field → 0 ʰr̲aze '{ i →
      (_, mesh, _, _, closed) ← d̲isclose i s̲elect objects
      n ← t̲ally mesh
      value ← { @ → field = 1 ? f̲irst i s̲elect mids◆ field = 2 ? f̲irst i s̲elect assignments◆ i × closed } @
      n r̲eshape value
    } m̲ap r̲ange t̲ally objects }
    ball̲ ← { field → 0 ʰr̲aze '{ i →
      (_, _, balls, _, _) ← d̲isclose i s̲elect objects
      value ← { @ → field = 1 ? f̲irst i s̲elect mids◆ field = 2 ? f̲irst i s̲elect assignments◆ i } @
      (t̲ally balls) r̲eshape value
    } m̲ap r̲ange t̲ally objects }
    triangleids ← 0 ʰr̲aze '{ i → (_, mesh, _, _, _) ← d̲isclose i s̲elect objects◆ ((f̲irst i s̲elect starts) − 1) + r̲ange t̲ally mesh } m̲ap r̲ange t̲ally objects
    sphereids ← 0 ʰr̲aze '{ i → (_, _, balls, _, _) ← d̲isclose i s̲elect objects◆ ((f̲irst i s̲elect starts) − 1) + r̲ange t̲ally balls } m̲ap r̲ange t̲ally objects
    materialarray ← ˢᶜm̲aterials ((t̲ally materials) c̲at 4) r̲eshape 0.0 ʰr̲aze '{ box → (_, row, _, _, _) ← d̲isclose box◆ row } m̲ap materials
    kinds ← ˢᶜm̲aterialKinds 0 ʰr̲aze '{ box → (_, _, tag, _, _) ← d̲isclose box◆ 1 r̲eshape tag } m̲ap materials
    optics ← ˢᶜo̲ptics ((t̲ally materials) c̲at 5) r̲eshape 0.0 ʰr̲aze '{ box → (_, _, _, row, _) ← d̲isclose box◆ row } m̲ap materials
    emission ← ˢᶜe̲mission ((t̲ally materials) c̲at 3) r̲eshape 0.0 ʰr̲aze '{ box → (_, _, _, _, row) ← d̲isclose box◆ row } m̲ap materials
    lightarray ← ˢᶜl̲ights ((t̲ally lights) c̲at 6) r̲eshape 0.0 ʰr̲aze '{ box → (_, row) ← d̲isclose box◆ row } m̲ap lights
    pivots ← ˢᶜp̲ivots ((1 + t̲ally groups) c̲at 3) r̲eshape 0.0 0.0 0.0 c̲at 0.0 ʰr̲aze '{ box → (_, _, pivot) ← d̲isclose box◆ pivot } m̲ap groups
    f̲ ← { suffix a → ((prefix ʰf̲ield suffix) a) "0.0" }
    i̲ ← { suffix a → ((prefix ʰf̲ield suffix) a) "0" }
    base ← ("triangles" f̲ triangles) c̲at ("materialids" i̲ tri̲ 1) c̲at ("groupids" i̲ tri̲ 2) c̲at ("materials" f̲ materialarray) c̲at ("transforms" f̲ ˢᶜi̲dentities 1 + t̲ally groups) c̲at ("pivots" f̲ pivots) c̲at ("camera" f̲ camera) c̲at "lights" f̲ lightarray
    ids ← ("triangleids" i̲ triangleids) c̲at ("sphereids" i̲ sphereids) c̲at ("triangleobjectids" i̲ tri̲ 3) c̲at ("sphereobjectids" i̲ ball̲ 3) c̲at ("spherematerialids" i̲ ball̲ 1) c̲at ("spheregroupids" i̲ ball̲ 2)
    emitters ← ˢᶜe̲mitters ((t̲ally emitterrecords) c̲at 3 3) r̲eshape 0.0 ʰr̲aze '{ box → (_, rect, _) ← d̲isclose box◆ rect } m̲ap emitterrecords
    emitterradiance ← ˢᶜe̲mission ((t̲ally emitterrecords) c̲at 3) r̲eshape 0.0 ʰr̲aze '{ box → (_, _, radiance) ← d̲isclose box◆ radiance } m̲ap emitterrecords
    defaults ← (prefix c̲at "sceneversion := 2\n") c̲at ("lens" f̲ lens) c̲at ("emitters" f̲ emitters) c̲at "emitterradiance" f̲ emitterradiance
    base c̲at ids c̲at defaults c̲at ("spheres" f̲ spheres) c̲at ("materialkind" i̲ kinds) c̲at ("optics" f̲ optics) c̲at "emission" f̲ emission
  } ⎕T̲RAP '{ e → ⎕R̲ECOVER "bad-scene right" ⎕R̲EJECT ⎕E̲MESSAGE e }
}