Linear DATA Inperpolation in C#

Hi C# gurus,

Could you help me to reproduce the Interpolate Data component but with 2 inputs to avoid this unnecessary pairing.

I tried to use the linear interpolation in C#, but how can this work with points and numbers (like the standard one).

Another issue that this C# takes a lot of time calculation a kind of bottleneck because of the implicit formula. I would like to optimize it as well.

Thank you!

Linear Data Interpolation.gh (219.6 KB)

Linear Data Interpolation.gh (208.8 KB)

Points3D accepts + - / * operators both in Grasshopper canvas AND Rhinocommon/c# script
(apart from mupliplying or dividing one point with another…)

To make it accepts points and numbers you have to make the inputs enter as generic “object” and only later cast them as points or numbers (“x = (int) y;” is casting object y to x as int type).

Also, to make it fast you’ll want to have the c# script load its input/sources as Lists:

Lastly, by using System.Threading.Tasks you can make your code use all your processor threads.
Note: I created a pre-determined size array to store results (object[] c = new object[n];); this is to ensure that each thread/process works without the risk of “colliding” with others (for example by accessing the same object simultaneously, causing your pc to explode).

Code:

using System.Threading.Tasks;

  private void RunScript(List<System.Object> A, List<System.Object> B, List<double> t, ref object C)
  {
    int n = A.Count;
    object[] c = new object[n];
    Parallel.For(0, n, i => {
      //for(int i = 0;i < n;i++){
      if(A[i] is double && B[i] is double){
        double a = (double) A[i];
        double b = (double) B[i];
        c[i] = (b - a) * t[i] + a;
      }
      if(A[i] is Rhino.Geometry.Point3d && B[i] is Rhino.Geometry.Point3d){
        Rhino.Geometry.Point3d a = (Rhino.Geometry.Point3d) A[i];
        Rhino.Geometry.Point3d b = (Rhino.Geometry.Point3d) B[i];
        c[i] = (b - a) * t[i] + a;
      }
      //}
      });
    C = c;
  }
1 Like

Thank you for the detailed explanation @maje90.
This works really great! and how about in the case that I just want to use a single value in the “t” parameter,
Do I need to clone a single value with my list for it to work?
Thank you again!

Disable c# component.
Set c# input “t” as “item access”.
Edit code from “t[i]” to just “t”.
Enable c# component.