"""
design team: Vincenzo D'Auria, Tingwei Duan, Nicolo Friedman, Shuliang Wang
ENCODED MATTER, BARTLETT 2012
Instructor: Ezio Blasetti
code developed in python for rhinoceros beta 5.0
"""
import rhinoscriptsyntax as rs
import math
a = []
class voxels():
def __init__(self, INTX, INTY, INTZ, BBOX):
self.x = INTX
self.y = INTY
self.z = INTZ
self.bbox = BBOX
self.Xinc = (self.bbox[6][0]-self.bbox[0][0])/self.x
self.Yinc = (self.bbox[6][1]-self.bbox[0][1])/self.y
self.Zinc = (self.bbox[6][2]-self.bbox[0][2])/self.z
self.pts = []
self.vals = []
self.vtxs = []
self.fvtxs = []
def voxelizeCrvs(self, crvs, freq):
for i in range(self.x):
ptRows = []
valRows = []
for j in range(self.y):
ptCols = []
valCols = []
for k in range(self.z):
thisPt = [self.bbox[0][0]+i*self.Xinc, self.bbox[0][1]+j*self.Yinc, self.bbox[0][2]+k*self.Zinc]
ptCols.append(thisPt)
sum = 0
for e in range(len(crvs)):
crv = crvs[e]
w = 2*math.pi/freq[e]
t = rs.CurveClosestPoint(crv,thisPt)
testPt = rs.EvaluateCurve(crv,t)
testDist = rs.Distance(thisPt,testPt)
sum = sum + math.sin(w*testDist)/testDist
if sum>fallout[e] :
valCols.append(1)
else:
valCols.append(0)
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)
a.append(self.id)
def addMeshBox(self, pt, listOfBool):
arrMinCorner = [pt[0],pt[1],pt[2]]
arrMaxCorner = [pt[0]+self.Xinc,pt[1]+self.Yinc,pt[2]+self.Zinc]
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():
"""
crvs = rs.GetObjects("select a few crvs",4)
box = rs.GetObject("select the box",rs.filter.polysurface)
u = 10
v = 10
w = 10
rad = 3
"""
bbox = rs.BoundingBox(box)
myVoxels = voxels(u,v,w,bbox)
myVoxels.voxelizeCrvs(crvs,rad)
Main() |