sourcelibs/Extrusion/src/Extrusion.xtl

1⍝│ Extrusion: validated simple polygons, symmetric meshes and block glyphs. 2⍝│ Import with "ex:" u_se< "Extrusion"; depends on Geometry and Vector3. 3⍝│ Contours are Geometry's 2 by N Float layout, implicitly closed. 4⍝│ Meshes are T by 3 by 3 Float, triangle/vertex/xyz, outward winding. 5 6"ge:" u̲se< "Geometry" 7"v:" u̲se< "Vector3" 8 9⍝│ Relative length and normalized orientation tolerance. 10ʰt̲ol ← { @ → 1e-10 } 11 12⍝│ Refuse infinity, NaN and arithmetic overflow. 13ʰf̲inite ← { a → 14 n̲ot '∧ r̲/ r̲avel (a − a) = 0.0 ? @ p̲anic< "Extrusion requires finite coordinates and results"
p̲anic< expands to
(⎕P̲ANIC ("Extrusion requires finite coordinates and results"))
15 a 16} 17 18⍝│ Signed orientation of segment a,b against every column of p. 19ʰo̲rient ← { a b p → 20 dx ← f̲irst b − a 21 dy ← f̲irst 1 d̲rop b − a 22 (dx × (2 s̲elect p) − f̲irst 1 d̲rop a) − dy × (1 s̲elect p) − f̲irst a 23} 24 25⍝│ Signed twice-area in already centered/scaled coordinates. 26ʰs̲igned ← { p → 27 x ← 1 s̲elect p 28 y ← 2 s̲elect p 29 '+ r̲/ (x × 1 o̲- y) − y × 1 o̲- x 30} 31 32⍝│ Center at the first vertex and scale by the longest bounding-box side. 33ʰn̲ormalized ← { p → 34 local ← p − o̲\ ((t̲ally f̲irst p) c̲at 2) r̲eshape f̲irst₂ p 35 span ← 'm̲ax r̲/ ('m̲ax r̲/₂ local) − 'm̲in r̲/₂ local 36 span ≤ 0.0 ? @ p̲anic< "Extrusion contour has no extent"
p̲anic< expands to
(⎕P̲ANIC ("Extrusion contour has no extent"))
37 ʰf̲inite local ÷ span 38} 39 40⍝│ Whether edge i touches or crosses any nonadjacent edge, including 41⍝│ collinear overlap and contacts at a nonshared vertex. 42ʰc̲rossing? ← { p i → 43 n ← t̲ally f̲irst p 44 a ← i s̲elect₂ p 45 b ← (1 + i m̲od n) s̲elect₂ p 46 end ← 1 o̲-₂ p 47 o1 ← (a ʰo̲rient b) p 48 o2 ← (a ʰo̲rient b) end 49 dx ← (1 s̲elect end) − 1 s̲elect p 50 dy ← (2 s̲elect end) − 2 s̲elect p 51 o3 ← (dx × (f̲irst 1 d̲rop a) − 2 s̲elect p) − dy × (f̲irst a) − 1 s̲elect p 52 o4 ← (dx × (f̲irst 1 d̲rop b) − 2 s̲elect p) − dy × (f̲irst b) − 1 s̲elect p 53 eps ← ʰt̲ol @ 54 straddle1 ← ((o1 ≤ eps) ∧ o2 ≥ n̲eg eps) ∨ (o2 ≤ eps) ∧ o1 ≥ n̲eg eps 55 straddle2 ← ((o3 ≤ eps) ∧ o4 ≥ n̲eg eps) ∨ (o4 ≤ eps) ∧ o3 ≥ n̲eg eps 56 low ← p m̲in end 57 high ← p m̲ax end 58 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 59 js ← r̲ange n 60 separate ← (js ≠ i) ∧ (js ≠ 1 + i m̲od n) ∧ js ≠ 1 + (i − 2) m̲od n 61 '∨ r̲/ separate ∧ overlap ∧ straddle1 ∧ straddle2 62} 63 64⍝│ Validate contour shape, finite values, distinct vertices, simple edges, 65⍝│ nonzero area and adjacent edge backtracking. No repair or convex hull. 66ʰc̲ontour ← { contour → 67 n̲ot 2 = t̲ally s̲hape contour ? @ p̲anic< "Extrusion requires a 2 by N contour"
p̲anic< expands to
(⎕P̲ANIC ("Extrusion requires a 2 by N contour"))
68 n ← f̲irst r̲ev s̲hape contour 69 n̲ot (2 c̲at n) m̲atch s̲hape contour ? @ p̲anic< "Extrusion requires a 2 by N contour"
p̲anic< expands to
(⎕P̲ANIC ("Extrusion requires a 2 by N contour"))
70 n < 3 ? @ p̲anic< "Extrusion requires at least three vertices"
p̲anic< expands to
(⎕P̲ANIC ("Extrusion requires at least three vertices"))
71 p ← ʰf̲inite f̲loat contour 72 q ← ʰn̲ormalized p 73 near ← (ᵍᵉd̲istances q) ≤ ʰt̲ol @ 74 nearPairs ← near ∧ (r̲ange n) '≠ t̲able r̲ange n 75 '∨ r̲/ r̲avel nearPairs ? @ p̲anic< "Extrusion contour has repeated vertices or zero-length edges"
p̲anic< expands to
(⎕P̲ANIC ("Extrusion contour has repeated vertices or zero-length edges"))
76 prev ← -1 o̲-₂ q 77 next ← 1 o̲-₂ q 78 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 79 incoming ← o̲\ q − prev 80 outgoing ← o̲\ next − q 81 '∨ r̲/ ((a̲bs turns) ≤ ʰt̲ol @) ∧ ('+ r̲/₂ incoming × outgoing) < 0.0 ? @ p̲anic< "Extrusion adjacent edges backtrack or overlap"
p̲anic< expands to
(⎕P̲ANIC ("Extrusion adjacent edges backtrack or overlap"))
82 '∨ r̲/ '{ i → q ʰc̲rossing? i } e̲ach r̲ange n ? @ p̲anic< "Extrusion contour self-intersects or touches a nonadjacent edge"
p̲anic< expands to
(⎕P̲ANIC ("Extrusion contour self-intersects or touches a nonadjacent edge"))
83 (a̲bs ʰs̲igned q) ≤ ʰt̲ol @ ? @ p̲anic< "Extrusion contour has zero area at its scale"
p̲anic< expands to
(⎕P̲ANIC ("Extrusion contour has zero area at its scale"))
84 p 85} 86 87⍝│ Original vertex IDs in counterclockwise order, preserving the first 88⍝│ vertex for an already counterclockwise contour. 89ʰo̲rder ← { p → 90 (ʰs̲igned ʰn̲ormalized p) > 0.0 ? r̲ange t̲ally f̲irst p 91 r̲ev r̲ange t̲ally f̲irst p 92} 93 94⍝│ Whether remaining vertex k is a strict convex ear, with no other 95⍝│ remaining vertex inside or on its triangle boundary. 96ʰe̲ar? ← { q ids k → 97 n ← t̲ally ids 98 ia ← f̲irst (1 + (k − 2) m̲od n) s̲elect ids 99 ib ← f̲irst k s̲elect ids 100 ic ← f̲irst (1 + k m̲od n) s̲elect ids 101 a ← ia s̲elect₂ q 102 b ← ib s̲elect₂ q 103 c ← ic s̲elect₂ q 104 turn ← f̲irst (a ʰo̲rient b) 2 1 r̲eshape c 105 turn ≤ ʰt̲ol @ ? 0 = 1 106 pts ← ids s̲elect₂ q 107 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 @) 108 others ← (ids ≠ ia) ∧ (ids ≠ ib) ∧ ids ≠ ic 109 n̲ot '∨ r̲/ inside ∧ others 110} 111 112⍝│ Clip the first valid ear in remaining order until one face remains. 113⍝│ Each call removes a vertex; tolerance-ambiguous cases refuse. 114ʰe̲ars ← { q ids faces → 115 n ← t̲ally ids 116 n = 3 ? { @ → 117 tri ← ids s̲elect₂ q 118 area ← f̲irst ((1 s̲elect₂ tri) ʰo̲rient 2 s̲elect₂ tri) 2 1 r̲eshape 3 s̲elect₂ tri 119 area ≤ ʰt̲ol @ ? @ p̲anic< "Extrusion final triangle is degenerate at the geometry tolerance"
p̲anic< expands to
(⎕P̲ANIC ("Extrusion final triangle is degenerate at the geometry tolerance"))
120 faces c̲at 1 3 r̲eshape ids 121 } @ 122 ears ← w̲here '{ k → (q ʰe̲ar? ids) k } e̲ach r̲ange n 123 0 = t̲ally ears ? @ p̲anic< "Extrusion contour cannot be triangulated at the geometry tolerance"
p̲anic< expands to
(⎕P̲ANIC ("Extrusion contour cannot be triangulated at the geometry tolerance"))
124 k ← f̲irst ears 125 face ← (1 + (k − 2) m̲od n) c̲at k c̲at 1 + k m̲od n 126 triangle ← face s̲elect ids 127 rest ← ((r̲ange n) ≠ k) r̲eplicate ids 128 ((q ʰe̲ars rest) faces c̲at 1 3 r̲eshape triangle) 129} 130 131⍝│ t_riangulate contour: (N-2) by 3 Int original vertex IDs, one-based, 132⍝│ counterclockwise. Accept either contour winding; no new vertices. 133ˡt̲riangulate ← { contour → 134 p ← ʰc̲ontour contour 135 ((ʰn̲ormalized p) ʰe̲ars ʰo̲rder p) 0 3 r̲eshape 0 136} 137 138⍝│ Add constant z to N by 2 rows. 139ʰa̲tZ ← { z rows → rows c̲at₂ ((t̲ally rows) c̲at 1) r̲eshape z } 140 141⍝│ depth e_xtrude contour: symmetric z=-depth/2..+depth/2, cap faces 142⍝│ followed by back faces then two outward triangles per contour edge. 143ˡe̲xtrude ← { depth contour → 144 n̲ot 0 = t̲ally s̲hape depth ? @ p̲anic< "Extrusion depth must be scalar"
p̲anic< expands to
(⎕P̲ANIC ("Extrusion depth must be scalar"))
145 d ← ʰf̲inite f̲loat depth 146 (d ≤ 0.0) ∨ 0.0 = 0.5 × d ? @ p̲anic< "Extrusion depth must be positive and representable"
p̲anic< expands to
(⎕P̲ANIC ("Extrusion depth must be positive and representable"))
147 p ← ʰc̲ontour contour 148 ids ← ˡt̲riangulate p 149 k ← t̲ally ids 150 n ← t̲ally f̲irst p 151 rows ← o̲\ p 152 caprows ← (r̲avel ids) s̲elect rows 153 front ← (k c̲at 3 3) r̲eshape (0.5 × d) ʰa̲tZ caprows 154 back ← 3 2 1 s̲elect₂ (k c̲at 3 3) r̲eshape (n̲eg 0.5 × d) ʰa̲tZ caprows 155 boundary ← (ʰo̲rder p) s̲elect rows 156 bottom ← (n̲eg 0.5 × d) ʰa̲tZ boundary 157 top ← (0.5 × d) ʰa̲tZ boundary 158 ab ← (n c̲at 1 3) r̲eshape bottom 159 bb ← (n c̲at 1 3) r̲eshape 1 o̲- bottom 160 at ← (n c̲at 1 3) r̲eshape top 161 bt ← (n c̲at 1 3) r̲eshape 1 o̲- top 162 side1 ← ab c̲at₂ bb c̲at₂ bt 163 side2 ← ab c̲at₂ bt c̲at₂ at 164 sides ← (n c̲at 1 3 3) r̲eshape side1 165 interleaved ← sides c̲at₂ (n c̲at 1 3 3) r̲eshape side2 166 ʰf̲inite front c̲at back c̲at ((2 × n) c̲at 3 3) r̲eshape interleaved 167} 168 169⍝│ g_lyph text: an original 2 by N outline for exactly one character. 170⍝│ Open counters keep lowercase glyphs simple, without polygon holes. 171ˡg̲lyph ← { text → 172 n̲ot (1 r̲eshape 1) m̲atch s̲hape text ? @ p̲anic< "Extrusion glyph requires a one-character string"
p̲anic< expands to
(⎕P̲ANIC ("Extrusion glyph requires a one-character string"))
173 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 174 text m̲atch "_" ? o̲\ 4 2 r̲eshape 0.0 -0.2 1.0 -0.2 1.0 -0.05 0.0 -0.05 175 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 176 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 177 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 178 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 179 @ p̲anic< "Extrusion unsupported glyph: {text}; supported: X _ e T a L"
p̲anic< expands to
⎕P̲ANIC ("Extrusion unsupported glyph: " c̲at (f̲ormat (text)) c̲at "; supported: X _ e T a L")
180} 181 182⍝│ a_dvance text: original glyph width plus a fixed 0.2 scene-unit gap. 183ˡa̲dvance ← { text → 184 p ← ˡg̲lyph text 185 0.2 + ('m̲ax r̲/ f̲irst p) − 'm̲in r̲/ f̲irst p 186} 187 188⍝│ Text from character i at horizontal position x, concatenated solids. 189ʰl̲etters ← { depth text i x mesh → 190 i > t̲ally text ? mesh 191 char ← 1 r̲eshape i s̲elect text 192 outline ← ˡg̲lyph char 193 solid ← depth ˡe̲xtrude outline 194 moved ← (x c̲at 0.0 0.0) ᵛt̲ranslate ((3 × t̲ally solid) c̲at 3) r̲eshape solid 195 meshMoved ← (s̲hape solid) r̲eshape moved 196 ((((depth ʰl̲etters text) i + 1) x + ˡa̲dvance char) mesh c̲at meshMoved) 197} 198 199⍝│ depth l_etters text: supported glyphs in text order, baseline y=0, 200⍝│ deterministic advances, disconnected closed solids with no font assets. 201ˡl̲etters ← { depth text → 202 n̲ot 1 = t̲ally s̲hape text ? @ p̲anic< "Extrusion letters require a string"
p̲anic< expands to
(⎕P̲ANIC ("Extrusion letters require a string"))
203 0 = t̲ally text ? @ p̲anic< "Extrusion letters require nonempty text"
p̲anic< expands to
(⎕P̲ANIC ("Extrusion letters require nonempty text"))
204 ((((depth ʰl̲etters text) 1) 0.0) 0 3 3 r̲eshape 0.0) 205}