librarylibs/Extrusion/src/Extrusion.xtl

Extrusion: validated simple polygons, symmetric meshes and block glyphs. Import with "ex:" u_se< "Extrusion"; depends on Geometry and Vector3. Contours are Geometry's 2 by N Float layout, implicitly closed. Meshes are T by 3 by 3 Float, triangle/vertex/xyz, outward winding.

source · imports ge: libs/Geometry/src/Geometry.xtl; v: libs/Vector3/src/Vector3.xtl

ʰt̲ol : Unit -> Float

function (private) · line 10

Relative length and normalized orientation tolerance.

ʰt̲ol ← { @ → 1e-10 }

ʰf̲inite : Float -> Float

function (private) · line 13

Refuse infinity, NaN and arithmetic overflow.

ʰf̲inite ← { a →
  n̲ot '∧ r̲/ r̲avel (a − a) = 0.0 ? @ p̲anic< "Extrusion requires finite coordinates and results"
  a
}
p̲anic< expands to
(⎕P̲ANIC ("Extrusion requires finite coordinates and results"))

ʰo̲rient : Num a => a -> a -> a -> a

function (private) · line 19

Signed orientation of segment a,b against every column of p.

ʰo̲rient ← { a b p →
  dx ← f̲irst b − a
  dy ← f̲irst 1 d̲rop b − a
  (dx × (2 s̲elect p) − f̲irst 1 d̲rop a) − dy × (1 s̲elect p) − f̲irst a
}

ʰs̲igned : Num a => a -> a

function (private) · line 26

Signed twice-area in already centered/scaled coordinates.

ʰs̲igned ← { p →
  x ← 1 s̲elect p
  y ← 2 s̲elect p
  '+ r̲/ (x × 1 o̲- y) − y × 1 o̲- x
}

ʰn̲ormalized : Float -> Float

function (private) · line 33

Center at the first vertex and scale by the longest bounding-box side.

ʰn̲ormalized ← { p →
  local ← p − o̲\ ((t̲ally f̲irst p) c̲at 2) r̲eshape f̲irst₂ p
  span ← 'm̲ax r̲/ ('m̲ax r̲/₂ local) − 'm̲in r̲/₂ local
  span ≤ 0.0 ? @ p̲anic< "Extrusion contour has no extent"
  ʰf̲inite local ÷ span
}
p̲anic< expands to
(⎕P̲ANIC ("Extrusion contour has no extent"))

ʰc̲rossing? : Truthy a => Float -> Int -> a

function (private) · line 42

Whether edge i touches or crosses any nonadjacent edge, including collinear overlap and contacts at a nonshared vertex.

ʰc̲rossing? ← { p i →
  n ← t̲ally f̲irst p
  a ← i s̲elect₂ p
  b ← (1 + i m̲od n) s̲elect₂ p
  end ← 1 o̲-₂ p
  o1 ← (a ʰo̲rient b) p
  o2 ← (a ʰo̲rient b) end
  dx ← (1 s̲elect end) − 1 s̲elect p
  dy ← (2 s̲elect end) − 2 s̲elect p
  o3 ← (dx × (f̲irst 1 d̲rop a) − 2 s̲elect p) − dy × (f̲irst a) − 1 s̲elect p
  o4 ← (dx × (f̲irst 1 d̲rop b) − 2 s̲elect p) − dy × (f̲irst b) − 1 s̲elect p
  eps ← ʰt̲ol @
  straddle1 ← ((o1 ≤ eps) ∧ o2 ≥ n̲eg eps) ∨ (o2 ≤ eps) ∧ o1 ≥ n̲eg eps
  straddle2 ← ((o3 ≤ eps) ∧ o4 ≥ n̲eg eps) ∨ (o4 ≤ eps) ∧ o3 ≥ n̲eg eps
  low ← p m̲in end
  high ← p m̲ax end
  overlap ← '∧ r̲/ (low ≤ eps + (a m̲ax b) '× t̲able n r̲eshape 1.0) ∧ high ≥ (n̲eg eps) + (a m̲in b) '× t̲able n r̲eshape 1.0
  js ← r̲ange n
  separate ← (js ≠ i) ∧ (js ≠ 1 + i m̲od n) ∧ js ≠ 1 + (i − 2) m̲od n
  '∨ r̲/ separate ∧ overlap ∧ straddle1 ∧ straddle2
}
Used in: ʰc̲ontour

ʰc̲ontour : Num a => a -> Float

function (private) · line 66

Validate contour shape, finite values, distinct vertices, simple edges, nonzero area and adjacent edge backtracking. No repair or convex hull.

ʰc̲ontour ← { contour →
  n̲ot 2 = t̲ally s̲hape contour ? @ p̲anic< "Extrusion requires a 2 by N contour"
  n ← f̲irst r̲ev s̲hape contour
  n̲ot (2 c̲at n) m̲atch s̲hape contour ? @ p̲anic< "Extrusion requires a 2 by N contour"
  n < 3 ? @ p̲anic< "Extrusion requires at least three vertices"
  p ← ʰf̲inite f̲loat contour
  q ← ʰn̲ormalized p
  near ← (ᵍᵉd̲istances q) ≤ ʰt̲ol @
  nearPairs ← near ∧ (r̲ange n) '≠ t̲able r̲ange n
  '∨ r̲/ r̲avel nearPairs ? @ p̲anic< "Extrusion contour has repeated vertices or zero-length edges"
  prev ← -1 o̲-₂ q
  next ← 1 o̲-₂ q
  turns ← '{ i → f̲irst ((i s̲elect₂ prev) ʰo̲rient i s̲elect₂ q) (2 c̲at 1) r̲eshape i s̲elect₂ next } e̲ach r̲ange n
  incoming ← o̲\ q − prev
  outgoing ← o̲\ next − q
  '∨ r̲/ ((a̲bs turns) ≤ ʰt̲ol @) ∧ ('+ r̲/₂ incoming × outgoing) < 0.0 ? @ p̲anic< "Extrusion adjacent edges backtrack or overlap"
  '∨ r̲/ '{ i → q ʰc̲rossing? i } e̲ach r̲ange n ? @ p̲anic< "Extrusion contour self-intersects or touches a nonadjacent edge"
  (a̲bs ʰs̲igned q) ≤ ʰt̲ol @ ? @ p̲anic< "Extrusion contour has zero area at its scale"
  p
}
p̲anic< expands to
(⎕P̲ANIC ("Extrusion requires a 2 by N contour"))
p̲anic< expands to
(⎕P̲ANIC ("Extrusion requires a 2 by N contour"))
p̲anic< expands to
(⎕P̲ANIC ("Extrusion requires at least three vertices"))
p̲anic< expands to
(⎕P̲ANIC ("Extrusion contour has repeated vertices or zero-length edges"))
p̲anic< expands to
(⎕P̲ANIC ("Extrusion adjacent edges backtrack or overlap"))
p̲anic< expands to
(⎕P̲ANIC ("Extrusion contour self-intersects or touches a nonadjacent edge"))
p̲anic< expands to
(⎕P̲ANIC ("Extrusion contour has zero area at its scale"))

ʰo̲rder : Float -> Int

function (private) · line 89

Original vertex IDs in counterclockwise order, preserving the first vertex for an already counterclockwise contour.

ʰo̲rder ← { p →
  (ʰs̲igned ʰn̲ormalized p) > 0.0 ? r̲ange t̲ally f̲irst p
  r̲ev r̲ange t̲ally f̲irst p
}

ʰe̲ar? : Truthy a => Float -> Int -> Int -> a

function (private) · line 96

Whether remaining vertex k is a strict convex ear, with no other remaining vertex inside or on its triangle boundary.

ʰe̲ar? ← { q ids k →
  n ← t̲ally ids
  ia ← f̲irst (1 + (k − 2) m̲od n) s̲elect ids
  ib ← f̲irst k s̲elect ids
  ic ← f̲irst (1 + k m̲od n) s̲elect ids
  a ← ia s̲elect₂ q
  b ← ib s̲elect₂ q
  c ← ic s̲elect₂ q
  turn ← f̲irst (a ʰo̲rient b) 2 1 r̲eshape c
  turn ≤ ʰt̲ol @ ? 0 = 1
  pts ← ids s̲elect₂ q
  inside ← (((a ʰo̲rient b) pts) ≥ n̲eg ʰt̲ol @) ∧ ((((b ʰo̲rient c) pts) ≥ n̲eg ʰt̲ol @) ∧ ((c ʰo̲rient a) pts) ≥ n̲eg ʰt̲ol @)
  others ← (ids ≠ ia) ∧ (ids ≠ ib) ∧ ids ≠ ic
  n̲ot '∨ r̲/ inside ∧ others
}
Used in: ʰe̲ars

ʰe̲ars : Float -> Int -> Int -> Int

function (private) · line 114

Clip the first valid ear in remaining order until one face remains. Each call removes a vertex; tolerance-ambiguous cases refuse.

ʰe̲ars ← { q ids faces →
  n ← t̲ally ids
  n = 3 ? { @ →
    tri ← ids s̲elect₂ q
    area ← f̲irst ((1 s̲elect₂ tri) ʰo̲rient 2 s̲elect₂ tri) 2 1 r̲eshape 3 s̲elect₂ tri
    area ≤ ʰt̲ol @ ? @ p̲anic< "Extrusion final triangle is degenerate at the geometry tolerance"
    faces c̲at 1 3 r̲eshape ids
  } @
  ears ← w̲here '{ k → (q ʰe̲ar? ids) k } e̲ach r̲ange n
  0 = t̲ally ears ? @ p̲anic< "Extrusion contour cannot be triangulated at the geometry tolerance"
  k ← f̲irst ears
  face ← (1 + (k − 2) m̲od n) c̲at k c̲at 1 + k m̲od n
  triangle ← face s̲elect ids
  rest ← ((r̲ange n) ≠ k) r̲eplicate ids
  ((q ʰe̲ars rest) faces c̲at 1 3 r̲eshape triangle)
}
p̲anic< expands to
(⎕P̲ANIC ("Extrusion final triangle is degenerate at the geometry tolerance"))
p̲anic< expands to
(⎕P̲ANIC ("Extrusion contour cannot be triangulated at the geometry tolerance"))

ˡt̲riangulate : Num a => a -> Int

function · line 133

t_riangulate contour: (N-2) by 3 Int original vertex IDs, one-based, counterclockwise. Accept either contour winding; no new vertices.

ˡt̲riangulate ← { contour →
  p ← ʰc̲ontour contour
  ((ʰn̲ormalized p) ʰe̲ars ʰo̲rder p) 0 3 r̲eshape 0
}
Used in: ˡe̲xtrude

ʰa̲tZ : a -> a -> a

function (private) · line 139

Add constant z to N by 2 rows.

ʰa̲tZ ← { z rows → rows c̲at₂ ((t̲ally rows) c̲at 1) r̲eshape z }
Used in: ˡe̲xtrude

ˡe̲xtrude : (Num a, Num b) => a -> b -> Float

function · line 143

depth e_xtrude contour: symmetric z=-depth/2..+depth/2, cap faces followed by back faces then two outward triangles per contour edge.

ˡe̲xtrude ← { depth contour →
  n̲ot 0 = t̲ally s̲hape depth ? @ p̲anic< "Extrusion depth must be scalar"
  d ← ʰf̲inite f̲loat depth
  (d ≤ 0.0) ∨ 0.0 = 0.5 × d ? @ p̲anic< "Extrusion depth must be positive and representable"
  p ← ʰc̲ontour contour
  ids ← ˡt̲riangulate p
  k ← t̲ally ids
  n ← t̲ally f̲irst p
  rows ← o̲\ p
  caprows ← (r̲avel ids) s̲elect rows
  front ← (k c̲at 3 3) r̲eshape (0.5 × d) ʰa̲tZ caprows
  back ← 3 2 1 s̲elect₂ (k c̲at 3 3) r̲eshape (n̲eg 0.5 × d) ʰa̲tZ caprows
  boundary ← (ʰo̲rder p) s̲elect rows
  bottom ← (n̲eg 0.5 × d) ʰa̲tZ boundary
  top ← (0.5 × d) ʰa̲tZ boundary
  ab ← (n c̲at 1 3) r̲eshape bottom
  bb ← (n c̲at 1 3) r̲eshape 1 o̲- bottom
  at ← (n c̲at 1 3) r̲eshape top
  bt ← (n c̲at 1 3) r̲eshape 1 o̲- top
  side1 ← ab c̲at₂ bb c̲at₂ bt
  side2 ← ab c̲at₂ bt c̲at₂ at
  sides ← (n c̲at 1 3 3) r̲eshape side1
  interleaved ← sides c̲at₂ (n c̲at 1 3 3) r̲eshape side2
  ʰf̲inite front c̲at back c̲at ((2 × n) c̲at 3 3) r̲eshape interleaved
}
p̲anic< expands to
(⎕P̲ANIC ("Extrusion depth must be scalar"))
p̲anic< expands to
(⎕P̲ANIC ("Extrusion depth must be positive and representable"))

ˡg̲lyph : Char -> Float

function · line 171

g_lyph text: an original 2 by N outline for exactly one character. Open counters keep lowercase glyphs simple, without polygon holes.

ˡg̲lyph ← { text →
  n̲ot (1 r̲eshape 1) m̲atch s̲hape text ? @ p̲anic< "Extrusion glyph requires a one-character string"
  text m̲atch "X" ? o̲\ 12 2 r̲eshape 0.0 0.0  0.3 0.0  0.5 0.35  0.7 0.0  1.0 0.0  0.65 0.5  1.0 1.0  0.7 1.0  0.5 0.65  0.3 1.0  0.0 1.0  0.35 0.5
  text m̲atch "_" ? o̲\ 4 2 r̲eshape 0.0 -0.2  1.0 -0.2  1.0 -0.05  0.0 -0.05
  text m̲atch "e" ? o̲\ 23 2 r̲eshape 0.8 0.38  0.16 0.38  0.17 0.25  0.29 0.16  0.53 0.16  0.68 0.23  0.78 0.1  0.58 0.01  0.24 0  0.06 0.13  0 0.28  0 0.53  0.09 0.7  0.25 0.8  0.54 0.8  0.73 0.68  0.8 0.54  0.63 0.54  0.55 0.64  0.3 0.64  0.18 0.53  0.18 0.52  0.8 0.52
  text m̲atch "T" ? o̲\ 8 2 r̲eshape 0.0 0.8  0.4 0.8  0.4 0.0  0.6 0.0  0.6 0.8  1.0 0.8  1.0 1.0  0.0 1.0
  text m̲atch "a" ? o̲\ 25 2 r̲eshape 0.65 0  0.85 0  0.85 0.8  0.67 0.8  0.67 0.72  0.56 0.8  0.56 0.6  0.65 0.5  0.65 0.34  0.52 0.24  0.28 0.24  0.17 0.34  0.17 0.52  0.29 0.63  0.47 0.63  0.54 0.6  0.54 0.8  0.22 0.8  0.07 0.66  0 0.5  0 0.28  0.12 0.12  0.25 0.08  0.53 0.08  0.65 0.16
  text m̲atch "L" ? o̲\ 6 2 r̲eshape 0.0 0.0  1.0 0.0  1.0 0.2  0.2 0.2  0.2 1.0  0.0 1.0
  @ p̲anic< "Extrusion unsupported glyph: {text}; supported: X _ e T a L"
}
p̲anic< expands to
(⎕P̲ANIC ("Extrusion glyph requires a one-character string"))
p̲anic< expands to
⎕P̲ANIC ("Extrusion unsupported glyph: " c̲at (f̲ormat (text)) c̲at "; supported: X _ e T a L")

ˡa̲dvance : Char -> Float

function · line 183

a_dvance text: original glyph width plus a fixed 0.2 scene-unit gap.

ˡa̲dvance ← { text →
  p ← ˡg̲lyph text
  0.2 + ('m̲ax r̲/ f̲irst p) − 'm̲in r̲/ f̲irst p
}

ʰl̲etters : Num a => a -> Char -> Int -> Float -> Float -> Float

function (private) · line 189

Text from character i at horizontal position x, concatenated solids.

ʰl̲etters ← { depth text i x mesh →
  i > t̲ally text ? mesh
  char ← 1 r̲eshape i s̲elect text
  outline ← ˡg̲lyph char
  solid ← depth ˡe̲xtrude outline
  moved ← (x c̲at 0.0 0.0) ᵛt̲ranslate ((3 × t̲ally solid) c̲at 3) r̲eshape solid
  meshMoved ← (s̲hape solid) r̲eshape moved
  ((((depth ʰl̲etters text) i + 1) x + ˡa̲dvance char) mesh c̲at meshMoved)
}

ˡl̲etters : Num a => a -> Char -> Float

function · line 201

depth l_etters text: supported glyphs in text order, baseline y=0, deterministic advances, disconnected closed solids with no font assets.

ˡl̲etters ← { depth text →
  n̲ot 1 = t̲ally s̲hape text ? @ p̲anic< "Extrusion letters require a string"
  0 = t̲ally text ? @ p̲anic< "Extrusion letters require nonempty text"
  ((((depth ʰl̲etters text) 1) 0.0) 0 3 3 r̲eshape 0.0)
}
p̲anic< expands to
(⎕P̲ANIC ("Extrusion letters require a string"))
p̲anic< expands to
(⎕P̲ANIC ("Extrusion letters require nonempty text"))