Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Saturday, August 30

SWARM_01

A first study into particle swarm behavior, with the help of RealFlow and Python scripting.
This code adjusts each particles's velocity and direction to the average value among its neighbors (within a specified distance).
However, in this script, the movement of the particles becomes far too uniform after some frames
already. More to come...




#version: august 29th, 2008
#description: EVENTS SRIPT / basic swarm behaviour test:
#get speed and direction of neighboring particles,
#calculate average and adjust own

def onSimulationBegin():
emitter = scene.addEmitter("Circle")
emitter.setName("Emitter1")
emitter.setParameter("Type","Dumb")
emitter.setParameter("Point size",1.0)
emitter.setParameter("Max particles",0)
import random
for i in range(0,50):
emitter.addParticle(Vector.new(random.uniform(-1,1)*3,random.uniform(-1,1)*3,
random.uniform(-1,1)*3),Vector.new(random.uniform(-1,1)*5,
random.uniform(-1,1)*5,random.uniform(-1,1)*5))


def onSimulationStep():
scene.message("|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||")
emitter = scene.getEmitter("Emitter1")
particle = emitter.getFirstParticle()
while particle:
#scene.message("INITIAL velocity = " + str(particle.getVelocity().x) + " | " + str(particle.getVelocity().y) + " | " +
str(particle.getVelocity().z))
neighlist = particle.getNeighbors(2)
scene.message("neighbors: " + str(len(neighlist)))
velsum = Vector.new(0,0,0)
for i in range (0,len(neighlist)):
neighbor = neighlist[i]
velneigh = neighbor.getVelocity()
#scene.message("velneigh = " + str(velneigh.x) + " | " + str(velneigh.y) + " | " + str(velneigh.z))
velsum = velsum + velneigh
#scene.message("velsum = " + str(velsum.x) + " | " + str(velsum.y) + " | " + str(velsum.z))
velaverage = velsum / Vector.new(len(neighlist),len(neighlist),len(neighlist))
particle.setVelocity(velaverage)
#scene.message("UPDATED velocity = " + str(particle.getVelocity().x) + " | " + str(particle.getVelocity().y) + " | " +
str(particle.getVelocity().z))
scene.message("-----------------------------------------------------------------------------")
particle = particle.getNextParticle()


Thursday, May 1

Recursive Growth_01

It took me some time to understand how Recursion (link1, link2, thxs link3!) works, and to code a recursive version of the last script.
In the older version of the script, each element may only create one new element, so that one single path of growth is created. Yet, in the function below, elements are copied and moved as before, but each new element may trigger the creation of several new ones, meaning that the system grows in all directions, like a branching tree.

Since the recursive function has to be able to stop itself at some level of depth, I added an 'internal' generation counter G. Also, there is an internal element counter (n) that is necessary for the recursive algorithm to add elements in the right branch order of the growth tree.
However, in order to have all distributed elements stored in an array (and to be able to search for elements, like checking for occupied positions), there must be an “external” counter, in this case t, which counts the actual elements being added.


























































'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function functRecGrowth (n, t, arrVec, arrPoints, strVolume, arrFlakes, arrReloc, G)


Dim j

If G >= 20 Then Exit Function

For j = 0 To 5
ReDim Preserve arrVec(n)
ReDim Preserve arrFlakes(n)
ReDim Preserve arrPoints(t)
If (Rnd > 0.7) Then
arrVec(n) = PointAdd (arrVec(n-1), arrReloc(j))
arrPoints(t) = arrVec(n)
If functOccupied (arrPoints, t) = False And IsPointInSurface (strVolume, arrPoints(t))Then
arrFlakes(n) = CopyObject (arrFlakes(0), arrVec(0), arrVec(n))
t = t + 1
'Print ("t = " & t & ", n = " & n)
Call functRecGrowth (n+1, t, arrVec, arrPoints, strVolume, arrFlakes, arrReloc, G+1)
End If
End If
Next

End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


With a bunch of snowflakes (here abstracted),
the first experiments into fractal behaviour/
recursive division...







Friday, April 25

Koch Snowflake 3D_02

























The last script was built to assemble a one degree bigger 3D Koch Snowflake from a picked base flake. If you change the loop contained in it with this one here, you will have a randomly growing snowflake accumulation...! Also, I added a function preventing snowflakes from being copied in positions that are already occupied by other flakes.

If you'd like to try out increasing the max. i value, go ahead! - but I told you beware of your graphics RAM...

__
EnableRedraw (False)
'''''''''''''''''''''''''''''''''''''''''''''''
For i = 0 To 50
For j = 0 To 5
ReDim Preserve arrVec(n)
ReDim Preserve arrFlakes(n)
If i = 0 And (Rnd > 0.5) Then
arrVec(n) = PointAdd (arrFlakeBase, arrReloc(j))
arrFlakes(n) = CopyObject (strFlake0, arrFlakeBase, arrVec(n))
n = n + 1
ElseIf n >= 1 And (Rnd > 0.5) Then
arrVec(n) = PointAdd (arrVec(n-1), arrReloc(j))
If functOccupied (arrVec, n) = False Then
arrFlakes(n) = CopyObject (strFlake0, arrFlakeBase, arrVec(n))
n = n + 1
End If
End If
Next
Next
'''''''''''''''''''''''''''''''''''''''''''''''
EnableRedraw (True)


'''''''''''''''''''''''''''''''''''''''''''''''
Function functOccupied (arrVec, n)
Dim k
functOccupied = False

For k = 0 To Ubound (arrVec) - 1
If PointCompare (arrVec(k), arrVec(n)) = True Then
functOccupied = True
Exit For
End If
Next

End Function
'''''''''''''''''''''''''''''''''''''''''''''''


Monday, April 21

Koch Snowflake 3D_01


































Worked on a script that builds 3D Koch Snowflakes from a given Base-Flake (1°), here is what it looks like.
The key was to have the coordinates of the object's bounding box. The Flake fits perfectly in, it's extremities all touch the box at /2, /3 or /4 edge lengths... holy geometry!

There are some reasons why I decided to try the Koch Snowflake geometry:
It it sort of a 3D tiling pattern, it grows recursively, like a fractal, it consists of just one simple module (itself being built from 8 even tetrahedrons) and last but not least, I just loved the snowflake allusion!

___
Option Explicit
'written by cubic on April 21th, 2008
'pick 3D Koch Snowflake to build +1° bigger flake


Call Main
Sub Main

Dim strFlake0
Dim arrBBox
Dim BoxX, BoxY, BoxZ
Dim arrFlakeBase
Dim arrReloc(7)
Dim arrVec(7)
Dim arrFlakes(7)
Dim n : n = 0

strFlake0 = Rhino.GetObject ("Please pick a 3D Koch Snowflake")
arrBBox = Rhino.BoundingBox (strFlake0)


'''''''''''''''''''''''''''''''''''''''''''''''
'Calculate base point (arrFlakeBase) for picked strFlake0, from flake's Bounding Box.

BoxX = Rhino.Distance (arrBBox(0), arrBBox(1))
BoxY = Rhino.Distance (arrBBox(0), arrBBox(3))
BoxZ = Rhino.Distance (arrBBox(0), arrBBox(4))

arrFlakeBase = Rhino.PointAdd (arrBBox(0), Array(0, BoxY/4, BoxZ/3))


'''''''''''''''''''''''''''''''''''''''''''''''
'Calculate relocation vectors for copies of strFlake0.

arrReloc(0) = Array(BoxX, 0, 0)
arrReloc(1) = Array(BoxX/2, BoxY/4, BoxZ/1.5)
arrReloc(2) = Array(BoxX/2, -BoxY/4, BoxZ/3)
arrReloc(3) = Array(BoxX/2, BoxY*0.75, 0)
arrReloc(4) = Array(0, BoxY/2, BoxZ/3)
arrReloc(5) = Array(BoxX, BoxY/2, BoxZ/3)
arrReloc(6) = Array(BoxX/2, BoxY/4, -BoxZ/3)


'''''''''''''''''''''''''''''''''''''''''''''''
'Make copies of strFlake0 and move to new positions ...

For n = 0 To 6
arrVec(n) = Rhino.PointAdd (arrFlakeBase, arrReloc(n))
arrFlakes(n) = Rhino.CopyObject (strFlake0, arrFlakeBase, arrVec(n))
Next

End Sub

Thursday, March 13

pointcloud filling volume























This is somehow my first script, it plots random points into any picked solid (closed surface or polysurface).
To deal with the "any", the idea was to first draw the points into an enclosing volume and then decide whether they fit into the desired solid or not. I guess there are smarter ways to do it, so - something to keep thinking about:




Option Explicit
'written by philipp hoppe on March 13th, 2008
'plots random points within picked volume; points stored in array


Call Main
Sub Main

Dim strVolume
Dim arrPts()
Dim extX : extX = 20
Dim extY : extY = 20
Dim extZ : extZ = 20
Dim i
Dim n : n = 0

strVolume = Rhino.GetObject ("Pick a closed surface or polysurface", 8 + 16)

Rhino.EnableRedraw (False)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
For i = 1 To 10000
ReDim Preserve arrPts(n)
arrPts(n) = Array(Rnd * extX, Rnd * extY, Rnd * extZ)

If Rhino.IsPointInSurface (strVolume, arrPts(n)) Then
Call Rhino.AddPoint (arrPts(n))
End If

n = n + 1
Next
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Rhino.EnableRedraw (True)

End Sub