Failing miserable with my try kangaroo with c#

Hi kangaroo specialists,

I was about to look into scripting a kangaroo node with c#, but I seem to have a lack of understanding how the entire thing works exactly and therefor just code undirectionned gibberish. Usually the transition between grasshopper and c# is pretty easy, but in this case I really need to understand how Kangaroo works in some steps to stop shooting in the dark with my miserable tries to obtain a pretty simple definition. I sadly already fail doing a simple onMesh goal and feed it into a solver:


private void RunScript(Mesh M, int iter, Mesh onmesh, ref object A)
  {

    var PS = new KangarooSolver.PhysicalSystem();
    List<IGoal> goals = new List<IGoal>();
    Point3d[] pts = M.Vertices.ToPoint3dArray();
    for(int i = 0;i < pts.Length;i++) PS.AddParticle(pts[i], 1);
    //settings
    //bool byCurr = false;
    double thresh = 0.05;
    //weights
    //int tick = 1;
    int omk = 1;

    //TangentInCircles

    /*for(int i = 0;i < M.Faces.Count;i++)
    {
    TangentIncircles tiC = new TangentIncircles(M.Faces[i].A, M.Faces[i].B, M.Faces[i].C, 1, tick);
    PS.AssignPIndex(tiC, 0.01, byCurr);
    goals.Add(tiC);
    }
    */

    //onMeshGoal
    OnMesh om = new OnMesh(new List<Point3d>(pts), M, omk);
    PS.AssignPIndex(om, 0.001);
    goals.Add(om);
    int cnt = 0;
    for(int i = 0;i < iter;i++)
    {
      PS.Step(goals, true, thresh);
      cnt++;
    }

    M.Vertices.Clear();
    M.Vertices.AddVertices(PS.GetPositions());
    M.Vertices.CombineIdentical(true, true);
    M.Compact();
    A = M;
  }

although it is an embarrassing piece of code, it’s the best I could come up with. As you can see, it’s intended to be a circle packing code.

Is there any chance I can find an example for TangentIncircles and OnMesh goals fed into a solver in c#? or some general information on how kangaroo works (e.g. it still makes no sens to me how you can assign all the correct points to the particles)?

If someone could point in the right direction, that would save me a ton of time

thank you

Ben

Hi Ben,

Your script there isn’t too far off.
Here’s an example including an OnMesh goal in a script

(though it’s a little different there as points are being added between iterations)

The indexing is indeed the critical bit. Before Kangaroo starts running it builds a duplicate free set of points, and each goal identifies which of these points it acts on by a set of stored index integers.
You can set these indices directly, but usually the easiest way to do it is to create the Goals with points, then call AssignPIndex before running. This automatically finds the index of the particle(adding one to the system if one doesn’t already exist at this point) and sets it in the goal.

TangentIncircles is a more tricky one to start with because the set of 4 points it works on requires a bit of mesh topology processing to get.

I’ll make another example file…

1 Like

Here’s an example showing how you’d use TangentIncircles in a script.
meshscriptexample.gh (12.9 KB)

Hope that helps.

1 Like

thank you very much Daniel! it will help a lot!

1 Like