Spherical Harmonics

Wanna be more inventive than Liebeskind, Ghery or Zaha?

Well, you easily can.

sphharmonics.jpg

The following will generate quite a few possible shapes, some of which will surely look better than this.

Option Explicit
‘Script written by didi stefanescu
‘Released under the Creative Commons Attribution-Noncommercial-Share Alike 3.0
http://creativecommons.org/licenses/by/3.0/legalcode

Dim arrVertices
Dim arrFaces

””””””””””””””””””””””””
”CHANGE THE RESOLUTION/DETAIL LEVEL BELOW    ”
”BUT BEAR IN MIND THAT 128 IS QUITE HIGH     ”
”SO IF YOU’RE ON A SLOWER COMPUTER YOU WOULD ”
”LIKE TO USE SOMETHING LIKE res = 64         ”
””””””””””””””””””””””””

Dim res : res = 128
Dim PI : PI = Rhino.Pi()
Dim du, dv

Dim m0, m1, m2, m3, m4, m5, m6, m7
Dim colors : colors = True
Dim MESH

Call Main()

Sub Main()

du = PI * 2 / res
dv = PI / res

Call initParams()
Rhino.EnableRedraw(False)

arrVertices = createVertices()
arrFaces = createFaces()

MESH = Rhino.AddMesh(arrVertices,arrFaces)

If(colors) Then
Call ColorMe(MESH)
End If

Rhino.EnableRedraw(True)
End Sub

””””””””””””””””””””””””
”CHANGE THESE PARAMETERS TO WHATEVER YOU LIKE”
”AS LONG AS THEY STAY INTEGERS               ”
””””””””””””””””””””””””

Function initParams()
m0 = 4 : m1 = 1 : m2 = 5 : m3 = 2
m4 = 2 : m5 = 1 : m6 = 4 : m7 = 5
End Function

Function createVertices()
Dim u, v, i, j
Dim k : k = -1
Dim arrVert()

For    i = 0 To res Step 1
u = i * du
For j = 0 To res Step 1
k = k + 1
v = j * dv
ReDim Preserve arrVert(k)
arrVert(k) = sphHarmonics(u, v)
Next
Next
createVertices = arrVert
End Function

Function createFaces()
Dim i, j
Dim k : k = -1
Dim base
Dim arrF()
For i = 0 To res – 1 Step 1
For j = 0 To res – 1 Step 1
k = k + 1

ReDim Preserve ArrF(k)
base = i * (res + 1) + j
arrF(k) = Array(base, base + 1, base + res + 2, base + res + 1)
Next
Next

createFaces = arrF

End Function

Function sphHarmonics(ByVal th, ByVal phi)

Dim r, x, y, z

r = sin(m0 * phi) ^ m1 + cos(m2 * th) ^ m3 + sin(m4 * phi) ^ m5 + cos(m6 * th) ^ m7

x = r * sin(phi) * cos(th)
y = r * cos(phi)
z = r * sin(phi) * sin(th)

sphHarmonics = Array(x, y, z)
End Function

Function colorMe(ByVal obj2Color)
Dim vert : vert = Rhino.MeshVertices(obj2Color)
Dim face : face = Rhino.MeshFaceVertices(obj2Color)
Dim color() : ReDim color(Ubound(vert))

Dim i, tempV

For i = 0 To Ubound(vert) Step 1
tempV = vert(i)
color(i) = RGB(abs(tempV(0)*200), abs(tempV(1)*200), abs(tempV(2)*100))
Next

Call Rhino.AddMesh(vert, face, , , color)
Call Rhino.DeleteObject(obj2Color)
End Function

Advertisements

One thought on “Spherical Harmonics

  1. Pingback: arhitectura

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s