Skip to content

Instantly share code, notes, and snippets.

@sigman78
Created December 17, 2013 19:10
Show Gist options
  • Save sigman78/8010812 to your computer and use it in GitHub Desktop.
Save sigman78/8010812 to your computer and use it in GitHub Desktop.
generate sequence of cube rotation group
# Generate all cube rotation group (permutations)
# There are 24 of them, including identity
# Pure integer arithmetic
#
#
# {1,a,a2,a3,b,ba,ba2,ba3,b2,b2a,b2a2,b2a3,b3,
# b3a,b3a2,b3a3,ab,aba,aba2,aba3,a3b,a3ba,a3ba2,a3ba3}
#
#
from numpy import *
import math
from itertools import *
from PIL import Image
# Rotation matrix permutations
PM = [(1, 2), (2, 0), (0, 1)]
# (cos phi, sin pgi) Pi/2*n cycle values
CF = [1, 0, -1, 0]
SF = [0, 1, 0, -1]
# Identity reference matrix
ID = mat(identity(3), dtype=int)
# Cube rotation group, encoded
GRP = ["","a","a2","a3","b","ba","ba2","ba3","b2","b2a","b2a2","b2a3","b3",
"b3a","b3a2","b3a3","ab","aba","aba2","aba3","a3b","a3ba","a3ba2","a3ba3"]
def brot(pq, c, s):
"""Build rotation 3x3 matrix from PQ & cos, sin values"""
r = mat(identity(3), dtype=int)
r[pq[0], pq[0]] = c
r[pq[0], pq[1]] = s
r[pq[1], pq[0]] = -s
r[pq[1], pq[1]] = c
return r
def brota(axis, angle):
"""Build rotation matrix from axis[X, Y, Z] and angle[0, pi/2, pi, 3pi/2]"""
pq = PM[axis]
c, s = CF[angle], SF[angle]
return brot(pq, c, s)
def bgen(form):
"""Build rotation matrix from RotGroup code, like 'a3b' - one b rot, then a rot"""
A = ID.copy()
Rx = brota(0, 1) # rotate against x (pi/2)
Ry = brota(1, 1) # rotate against y (pi/2)
# interpret form code (reversed direction)
rep = 1
for ch in form[::-1]:
if ch.isdigit():
rep = int(ch)
continue
if ch is "a":
for _ in range(rep):
A = A * Rx
elif ch is "b":
for _ in range(rep):
A = A * Ry
else: raise Error("Moron!")
rep = 1
return A
def pcolor(alteration, val):
V0 = [(0, 200, 0), (200, 0, 0), (0, 0, 200)]
V1 = [(0, 255, 0), (255, 0, 0), (0, 0, 255)]
v = V0 if alteration else V1
return v[0] if val == 0 else v[1] if val <0 else v[2]
"""im = Image.new("RGB", (9, 24))
Y = 0
for form in GRP: #zip(GRP, product(range(6*3), range(4*3))):
m = bgen(form)
a = array(m).reshape(-1,)
#l = [str.format("{:3n}", n) for n in a]
#print ",".join(l)
for X, er in enumerate(a):
pixel = (0, 255, 0)
if er < 0: pixel = (0, 0, 255)
elif er > 0: pixel = (255, 0, 0)
im.putpixel((X, Y), pixel)
Y += 1
im.save("_cubegroup1.png")
"""
"""
for _ in range(1, 4):
A = A * R
print A
print "=="
for c, s in zip(CF, SF):
print "--"
print brot(PM[0], c, s)
print
"""
ROTS = [bgen(form) for form in GRP]
RXP = brota(0, 1) # x pi/2
RXM = brota(0, 3) # x -pi/2 = 3pi/2
RYP = brota(1, 1)
RYM = brota(1, 3)
RZP = brota(2, 1)
RZM = brota(2, 3)
def rindex(rot):
for n, m in enumerate(ROTS):
if (m == rot).all(): return n
return -1
def transps(rot):
res = []
for n, m in enumerate(ROTS):
# rotate step
R = m * rot
# lookup for the index
res.append(rindex(R))
return res
print transps(RXP)
print transps(RXM)
print transps(RYP)
print transps(RYM)
print transps(RZP)
print transps(RZM)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment