Grasshopper

algorithmic modeling for Rhino

Hi,

another question. After trying 10 different methods to have one engine perform different operations on a list of clouds, my collogue - who visited your last workshop in Zurich - told me, it´s not possible, but you had some workaround. Anemone?

Cheers.

Views: 315

Replies to This Discussion

Can't think what it was... 

You can always reference the volvox dlls and gha files in a scripting component, and just execute the instructions that way... but I guess it's not a perfect solution. Nevertheless, here is how to do that:

Volvox.gha contains the Instructions objects which are the default instruction available in the plugin

Volvox_Instr.dll has the abstract classes from which the gha instructions inherit

Volvox_Cloud.dll finally contains the GH_Cloud object which tells Grasshopper how to treat the geometry. 

You have to reference all 3 of them to make this script work. (they should be in the place you've installed Volvox, GH libraries by default). Just copy paste it then into a VB scripting component.

'create 2 instructions - these classes are from the Volvox.gha library
Dim in1 As New volvox.Instr_planeclip(plane.WorldXY)
Dim in2 As New Volvox.Instr_RandomSub(0.5, 123)

'create 2 rhino pointclouds
Dim pc1 As New pointcloud
Dim pc2 As New PointCloud

'populate them with some points
For i As Integer = -100 To 100 Step 1
pc1.Add(New point3d(10, 0, i))
pc2.Add(New point3d(0, 0, i))
Next

'execute the instructions onto the pointclouds.
in1.Execute(pc1)
in1.Execute(pc2)

'note the random subsampling instruction has the same seed in both cases,
'hence it will sift the points in the same pattern
in2.Execute(pc1)
in2.Execute(pc2)

'get the clouds out with the GH_Cloud class,
'so that Grasshopper understands it as geometry object
a = New GH_cloud(pc1)
b = new GH_Cloud(pc2)

Now if you want to make it parallel, this might be a proper approach. This code will run a separate thread for each of the 2 point clouds, effectively almost cutting by half the processing speed (IF you have 2 logical cores OFC ;)). Feel free to extend it to any amount of threads, the strategy should be very similar.

In this script, every of the 2 (potentially more) point clouds gets a separate list of instructions - those are classes as well, and I'm not sure how would they react to be called from 2 separate threads. After creating a list of instructions, we run the Parallel.For loop which takes care of all the multithreading mess for us. Select Case is probably not the nicest way of deciding on which list to take in, but it is good in showing visually what is going on in there.

Note that this code wasn't tested properly, I did run it only with those 2 simple point clouds. Double save everything whenever multithreading...

'create 2 lists of instructions, one for each thread
Dim inList1 As New list(Of Volvox_Instr.Instr_Base)
Dim inList2 As New list(Of Volvox_Instr.Instr_Base)

'create and add the instructions to the lists
inList1.add(New volvox.Instr_planeclip(plane.WorldXY))
inList2.add(New volvox.Instr_planeclip(plane.WorldXY))
inList1.add(New Volvox.Instr_RandomSub(0.5, 123))
inList2.add(New Volvox.Instr_RandomSub(0.5, 234))

'create 2 rhino pointclouds
Dim pc1 As New pointcloud
Dim pc2 As New PointCloud

'populate them with some points
For i As Integer = -100 To 100 Step 1
pc1.Add(New point3d(10, 0, i))
pc2.Add(New point3d(0, 0, i))
Next

'a good coding practice
Dim pccount As Integer = 2

'a classic parallel for loop, note the upper bound is exclusive
System.Threading.Tasks.Parallel.For(0, pccount, Sub(index As Integer)
Select Case index
Case 0
For i As Integer = 0 To inList1.count - 1 Step 1
inList1(i).execute(pc1)
Next
Case 1
For i As Integer = 0 To inList2.count - 1 Step 1
inList2(i).execute(pc2)
Next
End Select
End Sub)

'get the clouds out with the GH_Cloud class,
'so that Grasshopper understands it as geometry object
a = New GH_cloud(pc1)
b = New GH_Cloud(pc2)

Now the cool part: some instructions (like the RandomSub) are also multithreaded internally. It means that the threads you set up (or even if you work with the first example) are going to create even more threads. Just watch the CPU usage, if it jumps to 60-100% you know why.

Thanks a lot for your help. In my case the number of clouds can vary, so I guess I´d rather go with looping the component. But thanks for giving an insight how the engine works. Best, p

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

© 2019   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service