(n)certainties

GSAPP-Fall 2011

(n)certainties header image 3

spiral/voxel

import rhinoscriptsyntax as rs
import random

class voxels():

def __init__(self, INTX, INTY, INTZ, CRV):
self.x = INTX
self.y = INTY
self.z = INTZ
self.pts = []
self.vals = []
self.vtxs = []
self.fvtxs = []
for i in range(self.x):
ptRows = []
valRows = []
for j in range(self.y):
ptCols = []
valCols = []
for k in range(self.z):
ptCols.append([i,j,k])
#rnd = random.random()
t = rs.CurveClosestPoint(CRV,[i,j,k])
crvPt = rs.EvaluateCurve(CRV,t)
if rs.Distance([i,j,k],crvPt)>2:
valCols.append(0)
else:
valCols.append(1)
ptRows.append(ptCols)
valRows.append(valCols)
self.pts.append(ptRows)
self.vals.append(valRows)
self.voxelize()

def voxelize(self):
for i in range(len(self.vals)):
row = self.vals[i]
for j in range(len(row)):
col = row[j]
for k in range(len(col)):
val = col[k]
###########################################################
if val>0.5:
listOfBool = [True,True,True,True,True,True]
if k!=0 :
if self.pts[i][j][k-1] and self.vals[i][j][k-1]>0.5:
listOfBool[0] = False
if j!=0 :
if self.pts[i][j-1][k] and self.vals[i][j-1][k]>0.5:
listOfBool[1] = False
if i!=self.x-1:
if self.pts[i+1][j][k] and self.vals[i+1][j][k]>0.5:
listOfBool[2] = False
if j!=self.y-1 :
if self.pts[i][j+1][k] and self.vals[i][j+1][k]>0.5:
listOfBool[3] = False
if i!=0 :
if self.pts[i-1][j][k] and self.vals[i-1][j][k]>0.5:
listOfBool[4] = False
if k!=self.z-1 :
if self.pts[i][j][k+1] and self.vals[i][j][k+1]>0.5:
listOfBool[5] = False
###########################################################
self.addMeshBox(self.pts[i][j][k], listOfBool)
self.id = rs.AddMesh(self.vtxs,self.fvtxs)

def addMeshBox(self, pt, listOfBool):
arrMinCorner = [pt[0]-0.5,pt[1]-0.5,pt[2]-0.5]
arrMaxCorner = [pt[0]+0.5,pt[1]+0.5,pt[2]+0.5]
self.vtxs.append([arrMinCorner[0], arrMinCorner[1], arrMinCorner[2]]) #0 -8
self.vtxs.append([arrMaxCorner[0], arrMinCorner[1], arrMinCorner[2]]) #1 -7
self.vtxs.append([arrMaxCorner[0], arrMaxCorner[1], arrMinCorner[2]]) #2 -6
self.vtxs.append([arrMinCorner[0], arrMaxCorner[1], arrMinCorner[2]]) #3 -5
self.vtxs.append([arrMinCorner[0], arrMinCorner[1], arrMaxCorner[2]]) #4 -4
self.vtxs.append([arrMaxCorner[0], arrMinCorner[1], arrMaxCorner[2]]) #5 -3
self.vtxs.append([arrMaxCorner[0], arrMaxCorner[1], arrMaxCorner[2]]) #6 -2
self.vtxs.append([arrMinCorner[0], arrMaxCorner[1], arrMaxCorner[2]]) #7 -1
e = len(self.vtxs)
if listOfBool[0] : self.fvtxs.append([e-8,e-5,e-6,e-7])
#arrFaceVertices.append([0,3,2,1])
if listOfBool[1] : self.fvtxs.append([e-8,e-7,e-3,e-4])
#arrFaceVertices.append([0,1,5,4])
if listOfBool[2] : self.fvtxs.append([e-7,e-6,e-2,e-3])
#arrFaceVertices.append([1,2,6,5])
if listOfBool[3] : self.fvtxs.append([e-6,e-5,e-1,e-2])
#arrFaceVertices.append([2,3,7,6])
if listOfBool[4] : self.fvtxs.append([e-5,e-8,e-4,e-1])
#arrFaceVertices.append([3,0,4,7])
if listOfBool[5] : self.fvtxs.append([e-4,e-3,e-2,e-1])
#arrFaceVertices.append([4,5,6,7])
#return rs.AddMesh (arrVertices, arrFaceVertices)

def Main():
crv = rs.GetObject(“select a crv”,4)
myVoxels = voxels(100,100,100,crv)

Main()

 

 

#################################

 

 

// Spiral Simulation

float gauss(float x){ return exp(-x*x/2.0) / sqrt(2*PI); }
float gaussI(float z){ // integrated gauss [0..1]
if(z 8.0) return 1.0;
float sum=0.0, term=z;
for (int i=3; sum+term!=sum; i+=2){
sum =sum+term;
term=term*z*z/i;
}
return 0.5+sum*gauss(z);
}
float gaussE(float z){ return gaussI(z)*2-1; }// gauss error func==> [-1..0..1]
float randomGauss(){
float x=0,y=0,r,c;
do{ x=random(-1.0,1.0);
y=random(-1.0,1.0);
r=x*x+y*y;
}while(r==0 || r>1);
c=sqrt(-2.0*log(r)/r);
return x*c; //return [x*c, y*c];
}
float randomGaussIn(float L, float H, float mul){ return constrain( randomGauss()*(H-L)*mul + (L+H)/2.0 ,L,H); }
float randomGaussAt(float L, float H, float mul){ return randomGauss()*(H-L)*mul + (L+H)/2.0; }

float pi=4*atan(1);

int stars=1000; // only …
int Rmax=300; // galaxy radius
float speed=0.03; // rotation speed

// stars follow elliptic orbits around the center
float eratioa=1; // ellipse ratio
float etwista=2.0/Rmax; // twisting factor (orbit axes depend on radius)

float eratiob=1; // ellipse ratio
float etwistb=2.0/Rmax; // twisting factor (orbit axes depend on radius)

float eratioc=4; // ellipse ratio
float etwistc=8.0/Rmax; // twisting factor (orbit axes depend on radius)

float []angle=new float[stars];
float []radius=new float[stars];

float cxa; float cya; //center
float cxb; float cyb;
float cxc; float cyc;

PImage img;

void setup(){
size(int(Rmax*3), int(Rmax*3),P3D);
background(0); // back to black
speed=speed/frameRate;

// begin in the center
cxa = width/2;
cya = height/2;

cxb = width/4;
cyb = height/4;

cxc = width/7;
cyc = height/3;
// itit stars
for (int i=0; i< stars; i++){
angle[i]= random(0,2*pi);
//radius[i]=random(1,Rmax);
radius[i]=((abs(randomGauss())))*Rmax*.5+0.5;
}
// gifExport = new gifAnimation.GifMaker(this, “galaxy.gif”);
// gifExport.setRepeat(0);
}

void draw(){
//smooth();
noSmooth();
img=get();
img.resize(round(width*0.5),round(height*0.5));
img.resize(width-2,height-2);
tint(245,250,255);
image(img,0,0);
//fill(0,8); rect(0,0,width,height);
noStroke();
float r,a,x,y,b,s,c,xxa,yya,xxb,yyb,xxc,yyc,dd;
for (int i=0; i< stars; i++){
r=radius[i];
a=angle[i]+speed*(Rmax/r)*3.0; // increment angle
angle[i]=a;
x=r*sin(a);
y=r*eratioa*cos(a);
b=r*etwista;
s=sin(b);
c=cos(b);
xxa=cxa + s*x + c*y; // a bit of trigo
yya=cya + c*x – s*y;
xxb=cxb + s*x + c*y; // a bit of trigo
yyb=cyb + c*x – s*y*0.2;
xxc=cxc + s*x + c*y*0.4; // a bit of trigo
yyc=cyc + c*x – s*y;
//dd=(r-50.0)*7.0;
dd=40000.0/r;
fill(color(dd,dd,dd*0.9,32));
rect(xxa-1.5,yya-1.5,3.0,3.0);
rect(xxb-1.5,yyb-1.5,3.0,3.0);
rect(xxc-1.5,yyc-1.5,3.0,3.0);
}
// saveFrame(“output/frames-####.png”);
// gifExport.setDelay(1); gifExport.addFrame();
}

void keyPressed() {
if(keyCode == UP) { eratioa=eratioa*1.02; }
if(keyCode == DOWN) { eratioa=eratioa/1.02; }
if(keyCode == LEFT) { etwista=etwista+0.001; }
if(keyCode == RIGHT) { etwista=etwista-0.001; }

if(keyCode == 8 ) { eratiob=eratiob*1.02; }
if(keyCode == 2) { eratiob=eratiob/1.02; }
if(keyCode == 4) { etwistb=etwistb+0.001; }
if(keyCode == 6) { etwistb=etwistb-0.001; }

if(keyCode == 7) { eratioc=eratioc*1.02; }
if(keyCode == 1) { eratioc=eratioc/1.02; }
if(keyCode == 9) { etwistc=etwistc+0.001; }
if(keyCode == 3) { etwistc=etwistc-0.001; }
//println(“eratio=”+eratio+” etwist=”+etwist);
}