Commit f3555022 authored by Dave Griffiths's avatar Dave Griffiths

first commit

parents
Script and example weaves used for mathematickal arts workshop at FoAM
Brussels 2011
Script and example weaves used for mathematickal arts workshop at FoAM Brussels 2011
;-------------------------------------------------------
; a plain weave "kernel" looks like this, with different
; colour threads in the warp and weft:
;
; R G
; || ||
;B =||======
; || ||
;Y =====||==
; || ||
;
; resulting topmost colours would look like this:
;
; R B
;
; Y G
;---------------------------------------------------------
; this function prints plain weave given warp and weft
; lists where characters represent colours.
; eg: (weave '(O O O O O O O) '(: : : : : : : : :))
; =>
; O : O : O : O
; : O : O : O :
; O : O : O : O
; : O : O : O :
; O : O : O : O
; : O : O : O :
; O : O : O : O
; : O : O : O :
; O : O : O : O
;
; or: (weave '(O O : : O O : : O O) '(O : : O O : : O O :))
; =>
; : O : : : O : : : O
; O : : : O : : : O :
; O O O : O O O : O O
; O O : O O O : O O O
; : O : : : O : : : O
; O : : : O : : : O :
; O O O : O O O : O O
; O O : O O O : O O O
; : O : : : O : : : O
; return warp or weft, dependant on the position
(define (stitch x y warp weft)
(if (eq? (modulo x 2)
(modulo y 2))
warp weft))
; prints out a weaving
(define (weave warp weft)
(for ((x (in-range 0 (length weft))))
(for ((y (in-range 0 (length warp))))
(display (stitch x y
(list-ref warp y)
(list-ref weft x))))
(newline)))
;------------------------------------------------------------
; what happens if we generate the warp and weft colours
; via formal grammar replacement?
;
; * works on lists not strings
; * given axiom and rules where a rule a=>ab is '(a (a b))
;
; eg: (replace '(x) '((x (h e l l o)))) => '(h e l l o)
(define (replace pattern rules)
(foldl
(lambda (item r)
(append
r
(foldl
(lambda (rule r)
(if (eq? item (car rule))
(cadr rule)
r))
(list item)
rules)))
'()
pattern))
;--------------------------------------------------------
; repeat replace multiple times:
; eg: (recurse '(a) '((a (a b)) (b (a a))) 3)
; =>
; (a b a a a b a b)
(define (recurse pattern rules n)
(cond
((zero? n) pattern)
(else
(recurse
(replace pattern rules) rules (- n 1)))))
;--------------------------------------------------------
; plug formal grammars into weave:
(let ((p (recurse '(O)
'(
(O (O : O :))
(: (: O :))
; 2 (O (: O O :))
; 1 (O (O : O))
; (: (: O))
)
3))
(q (recurse '(O)
'(
(O (O : O))
; (: (: O : O : O))
; (O (O O : : O :))
)
3)))
(display "warp:")(display p)(newline)
(display "weft:")(display q)(newline)
(weave p p))
; :::O:O:O::O:O:O::O:O:O
;; 4 3 3 3 3
; O:O:O:OO:O:O:O:OO:O:O:O:OO:O:O:O:OO:OO:O:O:O:OO:O
;: : : : O O : : O O : : : : O O : : O O : : : : : : O O : : O
; O : : : : O O : : O O : : : :
; O : O : : O : O : O : : O : : O : O : O : : O : O : O : : O :
; O : O : : O : : O : O : O : : O :
; some notes:
; one rule cross
; ((O (: O : O)))
;'((O (O : O))
; (: (: O : O : O)))
; complex
; '((O (: O :))
; (: (O O O)))
; wavy
; '((O (: O O :))) 4))
; '((O (O : : O))) 4))
; lozenge
; (O (: O : O :)) 3))
; (O (O : : : O)) 4)
; hyper lozenge
; '((O (: O : O :) 4))
; (O (O : : O O)) 4)
;star
; (O (O : :))
; (: (O))
; boxes
; (O (O : O))
; (: (: O))
q
Python weaving code used for the Raspberry Pi and custom hardware
experiments.
\ No newline at end of file
# invisible driver...
import RPi.GPIO as io
import time
def quick_setup(pins,i):
for pin in pins:
io.setup(pin,i)
def quick_set(pins,v):
debug = ""
for i in range(0,len(pins)):
debug+=str((v>>i)&1)
#if pins[i]==3:
# print(str(pins[i])+":"+str((v>>i)&1))
#time.sleep(0.1)
io.output(pins[i],(v>>i)&1)
#print(debug)
def quick_read(pins):
for i in range(0,len(pins)):
t = io.input(pins[i])
print("pin "+str(i)+" is "+str(t))
def value_read(pins):
v = 0
for i in range(0,len(pins)):
# low is 1, pull up resistor
t = not io.input(pins[i])
#print("pin:"+str(pins[i])+" is "+str(io.input(pins[i])))
v |= t<<i
return v
value_pins = [7,8,25,24,23,18,15,14]
address_pins = [27,17,4,3,2]
def read_addr(addr):
quick_set(address_pins, addr)
return value_read(value_pins)
def read_addr_4bit(addr):
quick_set(address_pins, addr)
# flip addr zero due to plug hw error
if addr==0 or addr==1:
if addr%2==0:
return value_read(value_pins) & 0x0f
else:
return (value_read(value_pins)>>4) & 0x0f
if addr%2==0:
return (value_read(value_pins)>>4) & 0x0f
else:
return value_read(value_pins) & 0x0f
def read_code():
r = []
for i in range(0,16):
r.append(read_addr(i))
return r
def init():
try:
io.setmode(io.BCM)
quick_setup(value_pins, io.IN)
quick_setup(address_pins, io.OUT)
quick_set(address_pins, 0)
except:
print "error"
def read_all():
ret = []
for addr in range(0,32):
ret.append(read_addr_4bit(addr))
return ret
def read_addr(addr):
return read_addr_4bit(addr)
import driver
import mcturtle
import time
PSH = 1
DUP = 2
JMP = 3
JMZ = 4
ADD = 5
SUB = 6
FWD = 7
TNL = 8
TNR = 9
TNU = 10
TND = 11
MAT = 12
class machine:
def __init__(self):
self.stack=[]
self.pc=0
self.turtle = mcturtle.turtle()
def incpc(self):
self.pc=(self.pc+1)%32
def pop_stack(self):
ret = self.stack[0]
self.stack=self.stack[1:]
return ret
def step(self):
v = driver.read_addr(self.pc)
self.incpc()
if v==PSH:
self.stack.insert(0,driver.read_addr(self.pc))
self.incpc()
elif v==DUP:
self.stack.insert(0,self.stack[0])
elif v==FWD:
if len(self.stack)==0:
self.turtle.forward(1)
else:
self.turtle.forward(self.pop_stack())
elif v==TNL:
self.turtle.left()
elif v==TNR:
self.turtle.right()
elif v==TNU:
self.turtle.up()
elif v==TND:
self.turtle.down()
elif v==ADD:
self.stack.insert(0,self.pop_stack()+self.pop_stack())
elif v==SUB:
a = self.pop_stack()
b = self.pop_stack()
self.stack.insert(0,b-a)
print(self.stack[0])
elif v==JMP:
self.pc=driver.read_addr(self.pc)
elif v==JMZ:
if len(self.stack)>0 and self.stack[0]!=0:
newpc = driver.read_addr(self.pc)
self.pc = newpc
else:
self.incpc()
driver.init()
mcturtle.bulldoze(100)
time.sleep(10)
print(driver.read_all())
m = machine()
while 1:
m.step()
time.sleep(0.1)
# Copyright (C) 2014 Dave Griffiths for dBsCode
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Minecraft API wrapper for dBsCode taster course
import sys
# locate api so we can run frm anywhere
sys.path.append("/home/pi/mcpi/api/python/mcpi")
import minecraft
from block import *
import math
import random
from vec3 import Vec3
mc = minecraft.Minecraft.create()
point = Vec3
def debug(msg):
mc.postToChat(str(msg))
def my_pos():
t = mc.player.getPos()
return point(t.x,t.y,t.z)
def move_me_to(p):
mc.player.setPos(p)
def read_block(p):
mc.getBlock(p)
def write_block(blocktype,p):
mc.setBlock(p.x,p.y,p.z,blocktype)
def box(t,pos,size):
mc.setBlocks(pos.x,pos.y,pos.z,
pos.x+size.x-1,pos.y+size.y-1,
pos.z+size.z-1,
t)
#for y in reversed(range(0,int(size.y))):
# for z in range(0, int(size.z)):
# for x in range(0, int(size.x)):
# write_block(t,point(pos.x+x,pos.y+y,pos.z+z))
def sphere(t,pos,radius):
radius=int(radius)
for y in range(-radius, radius):
for z in range(-radius, radius):
for x in range(-radius, radius):
if math.sqrt(x*x+y*y+z*z)<radius:
write_block(t,point(pos.x+x,pos.y+y,pos.z+z))
def cylinder(t,pos,radius,height):
radius=int(radius)
height=int(height)
for y in range(0, height):
for z in range(-radius, radius):
for x in range(-radius, radius):
if math.sqrt(x*x+z*z)<radius:
write_block(t,point(pos.x+x,pos.y+y,pos.z+z))
def cone(t,pos,radius,height):
radius=int(radius)
height=int(height)
for y in range(0, height):
for z in range(-radius, radius):
for x in range(-radius, radius):
if math.sqrt(x*x+z*z)<(radius*(1-y/float(height))):
write_block(t,point(pos.x+x,pos.y+y,pos.z+z))
def toblerone(t,pos,size):
for y in reversed(range(0,int(size.y))):
for z in range(0, int(size.z)):
for x in range(0, int(size.x)):
a = size.x*(1-y/float(size.y))*0.5
if x>size.x/2.0-a and x<a+size.x/2.0:
write_block(t,point(pos.x+x,pos.y+y,pos.z+z))
def mag(p):
return math.sqrt(p.x*p.x+p.y*p.y+p.z*p.z)
def distance(a,b):
return mag(a-b)
def random_point(a,b):
return point(random.randrange(a.x,b.x),
random.randrange(a.y,b.y),
random.randrange(a.z,b.z))
def random_range(a,b):
return random.randint(a,b)
def choose_one(*argv):
return argv[random.randint(0,len(argv)-1)]
def i_am_lost():
move_me_to(point(0,20,0))
def bulldoze(size):
height=100
print("bulldozing")
mc.setBlocks(-size/2,0,-size/2,size/2,height,size/2,AIR)
box(GRASS,point(-size/2,-1,-size/2),point(size,1,size))
print("finished bulldozing")
##########################################################
# turtle stuff
class turtle:
m_pos = point(0,0,0)
m_dir = point(1,0,0)
m_material = MELON
def material(this,m):
this.m_material=m
def forward(this,distance):
box(this.m_material,this.m_pos,this.m_dir*distance)
this.m_pos+=this.m_dir*distance
def roty(this,a):
# rotate around y
a = a*0.0174532925
tx = this.m_dir.x * math.cos(a)+this.m_dir.z * math.sin(a)
tz = this.m_dir.x * -math.sin(a)+this.m_dir.z * math.cos(a)
this.m_dir.x = int(tx)
this.m_dir.z = int(tz)
def rotx(this,a):
# rotate around x
a = a*0.0174532925
tx = this.m_dir.x * math.cos(a)+this.m_dir.y * -math.sin(a)
ty = this.m_dir.x * math.sin(a)+this.m_dir.y * math.cos(a)
this.m_dir.x = int(tx)
this.m_dir.y = int(ty)
def left(this):
this.roty(90)
def right(this):
this.roty(-90)
def up(this):
this.rotx(90)
def down(this):
this.rotx(-90)
import pygame
#import driver
import time
# standard lsystem stuff
def run_rule(str,rule):
ret = ""
for i in range(0,len(str)):
if str[i:i+len(rule[0])]==rule[0]:
ret+=rule[1]
else:
ret+=str[i]
return ret
def explode_lsystem(str,rules,gen):
for g in range(0, gen):
for r in rules:
str = run_rule(str,r)
return str
# a weave structure kernel
class kernel:
def __init__(self,structure,w,h):
self.structure = structure
self.width = w
self.height = h
# return warp or weft, dependant on the position
def stitch(self, x, y, warp, weft):
#if x % 2 == y % 2:
if self.structure[x%self.width+(y%self.height)*self.width]==1:
return warp
else:
return weft
def conv_colour(c):
if c=="R": return [255,50,100]
if c=="r": return [255,50,100]
if c=="B": return [50,200,255]
return [50,200,255]
pygame.init()
screen = pygame.display.set_mode([1024,768])
thr_warp_thin1 = pygame.image.load("thr-warp-thin.png")
thr_warp_thin1.set_palette_at(1,conv_colour("B"))
thr_warp_fat1 = pygame.image.load("thr-warp-fat.png")
thr_warp_fat1.set_palette_at(1,conv_colour("B"))
thr_weft_thin1 = pygame.image.load("thr-weft-thin.png")
thr_weft_thin1.set_palette_at(1,conv_colour("B"))
thr_weft_fat1 = pygame.image.load("thr-weft-fat.png")
thr_weft_fat1.set_palette_at(1,conv_colour("B"))
thr_warp_thin2 = pygame.image.load("thr-warp-thin.png")
thr_warp_thin2.set_palette_at(1,conv_colour("R"))
thr_warp_fat2 = pygame.image.load("thr-warp-fat.png")
thr_warp_fat2.set_palette_at(1,conv_colour("R"))
thr_weft_thin2 = pygame.image.load("thr-weft-thin.png")
thr_weft_thin2.set_palette_at(1,conv_colour("R"))
thr_weft_fat2 = pygame.image.load("thr-weft-fat.png")
thr_weft_fat2.set_palette_at(1,conv_colour("R"))
rect = thr_warp_thin1.get_rect()
#rectv.left=20
fat_red = 1
thin_red = 2
fat_blue = 4
thin_blue = 8
def lsys_from_code(c):
if c==fat_red: return "R"
if c==fat_blue: return "B"
if c==thin_red: return "r"
if c==thin_blue: return "b"
return ""
def rule_from_code(code):
f = lsys_from_code(code[0])
t = ""
for i in range(1,8):
t+=lsys_from_code(code[i])
return [f,t]
def lsys_from_blocks(code):
#axiom
axiom = ""
for i in range(0,8):
axiom+=lsys_from_code(code[i])
rule1=rule_from_code(code[8:16])
rule2=rule_from_code(code[16:24])
print("------")
print(axiom)
print(rule1)
print(rule2)
if (rule2[0]!=""):
return explode_lsystem(axiom,[rule1,rule2],3)
else:
return explode_lsystem(axiom,[rule1],3)
def sprite_from_yarn(c,warp):
if warp: