Skip to content

Instantly share code, notes, and snippets.

@Luomu
Created October 9, 2013 21:38
Show Gist options
  • Select an option

  • Save Luomu/6908895 to your computer and use it in GitHub Desktop.

Select an option

Save Luomu/6908895 to your computer and use it in GitHub Desktop.
Vector swizzles :)
#http://stackoverflow.com/questions/16446374/generating-all-permutations-with-repetition
alphabet = [ 'x', 'y', 'z', 'w' ]
def symbolic_increment( symbol, alphabet, num_items):
## increment our "symbolic" number by 1
symbol = list(symbol)
## we reverse the symbol to maintain the convention of having the LSD on the "right"
symbol.reverse()
place = 0;
while place < len(symbol):
if (alphabet.index(symbol[place])+1) < num_items:
symbol[place] = alphabet[alphabet.index(symbol[place])+1]
break
else:
symbol[place] = alphabet[0];
place+=1
symbol.reverse()
return ''.join(symbol)
def toVec(symbol):
num = len(symbol)
if (num == 2):
return "AGVec2(%s,%s)" % (symbol[0], symbol[1])
elif (num == 3):
return "AGVec3(%s,%s,%s)" % (symbol[0], symbol[1], symbol[2])
else:
return "AGVec4(%s,%s,%s,%s)" % (symbol[0], symbol[1], symbol[2], symbol[3])
def gen(fromClass, toClass, fromComponents, toComponents):
permutations=[]
r = toComponents
start_symbol = alphabet[0] * (r)
temp_symbol = alphabet[0] * (r)
while 1:
permutations.append(temp_symbol)
print "%s %s() const { return %s; }" % (toClass, temp_symbol, toVec(temp_symbol))
temp_symbol = symbolic_increment( temp_symbol, alphabet, fromComponents)
if( temp_symbol == start_symbol ): break
print "Generated %d permutations" % len(permutations)
#gen("AGVec2", "AGVec2", 2, 2)
#gen("AGVec3", "AGVec2", 3, 2)
#gen("AGVec3", "AGVec3", 3, 3)
gen("AGVec4", "AGVec2", 4, 2)
gen("AGVec4", "AGVec3", 4, 3)
gen("AGVec4", "AGVec4", 4, 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment