sourceextensions/scene/demos/Rubik.xtl
1⍝│ Rubik.xtl -- a Rubik's cube as arrays, for the voxel cube demos
2⍝│ (voxels-rubik and after). The cube is its 54 stickers; each sticker is
3⍝│ where it is -- its cubie's position x y z, each -1 0 or 1 -- and which
4⍝│ way it faces, a unit normal. A turn rotates the stickers of one layer
5⍝│ by a quarter turn; matching the rotated stickers against the stickers
6⍝│ gives where each one goes, a permutation of 54, and turning the cube
7⍝│ is selecting its colors by that permutation. Every position reached
8⍝│ by turns is a real cube's.
9⍝│ Import: "rb:" u_se< "Rubik"
10
11"sc:" u̲se< "Scene"
12
13⍝━━ Stickers
14
15⍝ the six faces' normals, in the order of the colors: up (+y), down,
16⍝ right (+x), left, front (+z, toward the viewer), back
17normals ← 6 3 r̲eshape 0 1 0 0 -1 0 1 0 0 -1 0 0 0 0 1 0 0 -1
18
19⍝ the 27 cubies' positions, x slowest
20q ← (r̲ange 27) − 1
21cubies ← 2 1 t̲ranspose 3 27 r̲eshape ((q d̲iv 9) − 1) c̲at (((q d̲iv 3) m̲od 3) − 1) c̲at (q m̲od 3) − 1
22
23⍝│ h:m_akeStickers 0: the stickers, face by face: the cubies whose
24⍝│ coordinate along the face's normal is the normal's sign
25ʰm̲akeStickers ← { z →
26 pick ← cubies '+ '× i̲nner 2 1 t̲ranspose normals
27 on ← pick = 1
28 rows ← '{ (⍵ s̲elect₂ on) r̲eplicate cubies } m̲ap r̲ange 6
29 ns ← '{ 9 3 r̲eshape ⍵ s̲elect normals } m̲ap r̲ange 6
30 pos ← d̲isclose '{ e̲nclose (d̲isclose ⍺) c̲at d̲isclose ⍵ } r̲/ rows
31 nrm ← d̲isclose '{ e̲nclose (d̲isclose ⍺) c̲at d̲isclose ⍵ } r̲/ ns
32 pos c̲at₂ nrm
33}
34
35⍝│ a sticker for each face and each of its 9 cubies, 54 by 6: x y z of
36⍝│ the cubie, then the normal; the faces in the order up down right left
37⍝│ front back, 9 stickers each
38⍝│ >> "rb:" u_se< "Rubik"
39⍝│ >> s_hape rb:stickers
40⍝│ 54 6
41ˡstickers ← ʰm̲akeStickers 0
42
43⍝│ the solved cube's colors: each sticker its face's number, 0 to 5
44⍝│ (up down right left front back)
45⍝│ >> "rb:" u_se< "Rubik"
46⍝│ >> 10 t_ake rb:solved
47⍝│ 0 0 0 0 0 0 0 0 0 1
48ˡsolved ← 9 r̲eplicate (r̲ange 6) − 1
49
50⍝━━ Turns
51
52⍝│ h:k_ey m: one number for each sticker row (x y z nx ny nz), the same
53⍝│ for the same place and facing
54ʰk̲ey ← { m → (3 3 3 3 3 3 d̲ecode 2 1 t̲ranspose m + 1) }
55
56⍝│ k h:r_otate m: the rows (x y z, and the normal) turned a quarter turn
57⍝│ counterclockwise about the axis k (1 x, 2 y, 3 z), seen from its
58⍝│ positive end -- x y z goes to x -z y about x, z y -x about y, -y x z
59⍝│ about z
60ʰr̲otate ← { k m →
61 x ← 1 s̲elect₂ m
62 y ← 2 s̲elect₂ m
63 z ← 3 s̲elect₂ m
64 a ← 4 s̲elect₂ m
65 b ← 5 s̲elect₂ m
66 c ← 6 s̲elect₂ m
67 k = 1 ? 2 1 t̲ranspose 6 54 r̲eshape x c̲at (0 − z) c̲at y c̲at a c̲at (0 − c) c̲at b
68 k = 2 ? 2 1 t̲ranspose 6 54 r̲eshape z c̲at y c̲at (0 − x) c̲at c c̲at b c̲at 0 − a
69 2 1 t̲ranspose 6 54 r̲eshape (0 − y) c̲at x c̲at z c̲at (0 − b) c̲at a c̲at c
70}
71
72⍝│ (axis layer quarters) rb:p_ermutation 0: the permutation of the 54
73⍝│ stickers for turning the layer (the cubies whose coordinate on the
74⍝│ axis is the layer, -1 or 1) by that many counterclockwise quarter
75⍝│ turns about the axis: the colors after the turn are this selection of
76⍝│ the colors before
77ˡp̲ermutation ← { t z →
78 k ← 1 s̲elect t
79 v ← 2 s̲elect t
80 n ← 3 s̲elect t
81 m ← ˡstickers
82 turned ← n '{ k ʰr̲otate ⍵ } p̲ower m
83 inlayer ← v = k s̲elect₂ m
84 moved ← (2 1 t̲ranspose 6 54 r̲eshape inlayer) + 0
85 after ← (moved × turned) + (1 − moved) × m
86 g̲rade (ʰk̲ey m) i̲ndexOf ʰk̲ey after
87}
88
89⍝│ the twelve turns' names: a face turned clockwise as you look at it,
90⍝│ and with ' counterclockwise
91ˡnames ← "U U' D D' R R' L L' F F' B B'"
92
93⍝ each turn as axis (1 x, 2 y, 3 z), layer, counterclockwise quarters:
94⍝ a face turned clockwise as seen from outside it is a quarter turn
95⍝ clockwise about its outward normal -- three counterclockwise quarters
96⍝ about the axis for up, right and front, one for down, left and back
97specs ← 12 3 r̲eshape 2 1 3 2 1 1 2 -1 1 2 -1 3 1 1 3 1 1 1 1 -1 1 1 -1 3 3 1 3 3 1 1 3 -1 1 3 -1 3
98⍝│ the twelve turns' permutations, 12 by 54, in the order of rb:names
99ˡmoves ← 12 54 r̲eshape r̲avel d̲isclose '{ e̲nclose (d̲isclose ⍺) c̲at d̲isclose ⍵ } r̲/ '{ (⍵ s̲elect specs) ˡp̲ermutation 0 } m̲ap r̲ange 12
100
101⍝│ i rb:t_urn c: the colors c after turn i (1 to 12, in the order of
102⍝│ rb:names)
103⍝│ >> "rb:" u_se< "Rubik"
104⍝│ >> (4 '{ 1 rb:t_urn _r } p_ower rb:solved) m_atch rb:solved
105⍝│ 1
106ˡt̲urn ← { i c → (i s̲elect ˡmoves) s̲elect c }
107
108⍝│ rb:u_ndo i: the turn that undoes turn i (U and U', and so on)
109⍝│ >> "rb:" u_se< "Rubik"
110⍝│ >> rb:u_ndo 1 2 5
111⍝│ 2 1 6
112ˡu̲ndo ← { i → i + 1 − 2 × 0 = i m̲od 2 }
113
114⍝━━ Drawing
115
116⍝│ (offset half) h:s_quaresAt rows: a square for each row (x y z of a
117⍝│ cubie, then a normal), its center offset along the normal, its half
118⍝│ side half, its 4 corners in order round it; 4n by 3, row by row
119ʰs̲quaresAt ← { oh pn →
120 p ← f̲loat 1 2 3 s̲elect₂ pn
121 n ← f̲loat 4 5 6 s̲elect₂ pn
122 ax ← (a̲bs 4 s̲elect₂ pn) + (2 × a̲bs 5 s̲elect₂ pn) + 3 × a̲bs 6 s̲elect₂ pn
123 u ← ax s̲elect 3 3 r̲eshape 0.0 1.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0
124 w ← ax s̲elect 3 3 r̲eshape 0.0 0.0 1.0 0.0 0.0 1.0 0.0 1.0 0.0
125 c ← p + (1 s̲elect oh) × n
126 h ← 2 s̲elect oh
127 k1 ← c + h × (0.0 − u) − w
128 k2 ← c + h × u − w
129 k3 ← c + h × u + w
130 k4 ← c + h × w − u
131 m ← t̲ally pn
132 ((4 × m) c̲at 3) r̲eshape 2 1 3 t̲ranspose (4 c̲at m c̲at 3) r̲eshape k1 c̲at k2 c̲at k3 c̲at k4
133}
134
135⍝│ the stickers as squares for scene, 4 corners each in order round
136⍝│ them (216 by 3), in the stickers' order, just outside the cubies'
137⍝│ dark bodies
138ˡsquares ← 0.485 0.42 ʰs̲quaresAt ˡstickers
139
140⍝ the 26 cubies one can see (all but the middle), with each normal
141shell ← (0 < '+ r̲/₂ a̲bs cubies) r̲eplicate cubies
142bodyrows ← d̲isclose '{ e̲nclose (d̲isclose ⍺) c̲at d̲isclose ⍵ } r̲/ '{ shell c̲at₂ 26 3 r̲eshape ⍵ s̲elect normals } m̲ap r̲ange 6
143
144⍝│ the cubies' dark bodies as squares for scene (624 by 3): six faces
145⍝│ each, 0.96 wide
146ˡbodies ← 0.48 0.48 ʰs̲quaresAt bodyrows
147
148⍝│ k rb:c_olorSquares c: the squares of the stickers that are color k
149⍝│ in the colors c (k 0 to 5), for one object of quads
150⍝│ >> "rb:" u_se< "Rubik"
151⍝│ >> s_hape 0 rb:c_olorSquares rb:solved
152⍝│ 36 3
153ˡc̲olorSquares ← { k c →
154 i ← w̲here c = k
155 (r̲avel (4 × i − 1) '+ t̲able 1 2 3 4) s̲elect ˡsquares
156}
157
158⍝│ each color, red green blue: white (up), yellow (down), red (right),
159⍝│ orange (left), green (front), blue (back)
160ˡrgb ← 6 3 r̲eshape 0.95 0.95 0.95 1.0 0.84 0.1 0.82 0.12 0.12 1.0 0.5 0.08 0.1 0.62 0.25 0.12 0.32 0.85
161
162⍝━━ Turning, drawn
163
164⍝│ rb:s_pec i: turn i's axis (1 x, 2 y, 3 z), layer (-1 or 1) and
165⍝│ counterclockwise quarters about the axis (1 or 3)
166⍝│ >> "rb:" u_se< "Rubik"
167⍝│ >> rb:s_pec 1
168⍝│ 2 1 3
169ˡs̲pec ← { i → i s̲elect specs }
170
171⍝│ (axis, degrees) rb:r_otation 0: the 3 by 3 matrix turning points that
172⍝│ many degrees counterclockwise about the axis, seen from its positive
173⍝│ end (90 degrees is the quarter turn of h:r_otate)
174ˡr̲otation ← { (k, d) z →
175 t ← d × 3.141592653589793 ÷ 180.0
176 c ← c̲os t
177 s ← s̲in t
178 k = 1 ? 3 3 r̲eshape 1.0 0.0 0.0 0.0 c̲at c c̲at (0.0 − s) c̲at 0.0 c̲at s c̲at c
179 k = 2 ? 3 3 r̲eshape c c̲at 0.0 c̲at s c̲at 0.0 1.0 0.0 c̲at (0.0 − s) c̲at 0.0 c̲at c
180 3 3 r̲eshape c c̲at (0.0 − s) c̲at 0.0 c̲at s c̲at c c̲at 0.0 0.0 0.0 1.0
181}
182
183⍝│ r rb:t_urned pts: the points (n by 3) turned by the matrix r
184ˡt̲urned ← { r pts → pts '+ '× i̲nner 2 1 t̲ranspose r }
185
186⍝ the rows of the 4 corners of each square k, for squares by mask
187ʰc̲orners ← { m → r̲avel (4 × (w̲here m) − 1) '+ t̲able 1 2 3 4 }
188
189⍝│ (colors, turn, fraction) rb:d_raw w: the cube drawn in scene window w
190⍝│ with turn i (1 to 12; 0 for none) that fraction of the way done: the
191⍝│ turning layer's cubies -- their bodies and stickers -- rotated, the
192⍝│ rest still. Objects: 1 and 2 the bodies still and turning, 10 + k
193⍝│ and 20 + k the stickers of color k still and turning.
194ˡd̲raw ← { (c, i, frac) w →
195 sp ← (1 m̲ax i) s̲elect specs
196 ax ← 1 s̲elect sp
197 turning ← 0 < i
198 deg ← frac × 90.0 × (f̲loat 2 − 3 s̲elect sp) × f̲loat turning
199 r ← (ax, deg) ˡr̲otation 0
200 sm ← turning × (2 s̲elect sp) = ax s̲elect₂ ˡstickers
201 bm ← turning × (2 s̲elect sp) = ax s̲elect₂ bodyrows
202 f ← f̲loat w
203 _ ← (f c̲at 1.0 0.1 0.1 0.11) ˢᶜq̲uads! (ʰc̲orners bm = 0) s̲elect ˡbodies
204 _ ← (f c̲at 2.0 0.1 0.1 0.11) ˢᶜq̲uads! r ˡt̲urned (ʰc̲orners bm) s̲elect ˡbodies
205 _ ← '{ (f c̲at (10.0 + f̲loat ⍵) c̲at (⍵ + 1) s̲elect ˡrgb) ˢᶜq̲uads! (ʰc̲orners (c = ⍵) × sm = 0) s̲elect ˡsquares } m̲ap (r̲ange 6) − 1
206 _ ← '{ (f c̲at (20.0 + f̲loat ⍵) c̲at (⍵ + 1) s̲elect ˡrgb) ˢᶜq̲uads! r ˡt̲urned (ʰc̲orners (c = ⍵) × sm) s̲elect ˡsquares } m̲ap (r̲ange 6) − 1
207 0
208}