Is it possible to create flow in C# for multi-threading?


I’m trying to improve the performance of my grasshopper file and it looks like the flow component is the biggest user of processing power when making adjustments. Would it be possible to write Flow into a C# script to unlock the multithreading capabilities?

If you know what settings you need and the input (in your case Point3ds I believe), there appears to be over a ten fold increase in speed even if single threaded. Each of these gives the same resultant points:

  private void RunScript(List<Point3d> x, Curve b, Curve t, ref object A)
    Rhino.Geometry.Morphs.FlowSpaceMorph myFlow = new Rhino.Geometry.Morphs.FlowSpaceMorph(b, t, false, false, true);
    for(int i = 0; i < x.Count; i++) x[i] = myFlow.MorphPoint(x[i]);
    A = x;
  } (8.7 KB)

Wow that’s great! Thanks. I wonder why the flow component is so heavy?

Yep, it’s funny. I think because the gh components have to be quite general for all different types of geometry, there’s also potential casting checks and things on the I/O parameters I guess. It’s amazing how much using Rhinocommon directly can speed up a definition if you know what you want, but that’s thanks to David of course for allowing us to have scripting components in the first place. That flow one does seem a little bizarre in terms of speed increase though :slight_smile:

Do you know why there is only 3 boolean toggles in the c# version? I’m trying to figure out which boolean is which for the script version.

There are the three tags are:

public FlowSpaceMorph(
	Curve curve0,
	Curve curve1,
	bool reverseCurve0,
	bool reverseCurve1,
	bool preventStretching

From here:

So rather confusingly, the preventStretching boolean you choose has to be the opposite of the gh one.

Now, the fourth one is odd, I’m not entirely sure. The Grasshopper input param states:

“Geometry will not be deformed as it is transformed”

Which tends to suggest the extra line be added to the C# script:

myFlow.PreserveStructure = true;

To be honest, I don’t think we’re going to know… However, with using points it doesn’t seem to change anything so I think you’re fine with your use case. It would be interesting to test something like a nurbs, because I think that’s where the PreserveStructure property has a use (according to the doc). Maybe I’ll give it a go later just to scratch an itch, but I think you should be fine right?


Perfect, It looks like we’ve cut down my processing time significantly. The only other things that are taking a moderate amount of time are the mesh inclusion, arithmetic mean, and dispatch components. Not sure if anything can be done about those?

yeah sure, but what are you plugging into them? I think you’ll have to share a script at some point. Sure they can be made efficient through scripting.