Issues: C # script is used to simulate the close points component

C # is used to simulate the function of the close points component, but the speed is much slower. How to speed up the script.

design_1.gh (9.9 KB)

Due to the large list, even a C# script with no code will take more time loading than default Closest Points component computing.
2022-08-28 14_42_45-Grasshopper - design_1_

For the reason, read this:


Rhino have already a class for “a list of points”, the PointCloud class.
PointCloud class have a method for closest point search.
Then, because you want the Closest Points , a small “trick” was needed to recover the correct indexes…


(Standard Closest Points component is still faster. 27ms here are random, usually it doesn’t even display the time.)

design_1 re.gh (14.5 KB)

  private void RunScript(Point3d P, List<System.Object> C, int N, ref object Pt, ref object i, ref object D)
  {
    List<Point3d> pts = new List<Point3d>();
    for(int j = 0;j < C.Count;j++){
      pts.Add((Point3d) C[j]);
    }

    Rhino.Geometry.PointCloud cloud = new Rhino.Geometry.PointCloud(pts);
    Rhino.Geometry.PointCloud cloudbackup = new Rhino.Geometry.PointCloud(cloud);

    List<Point3d> points = new List<Point3d>();
    List<int> indexes = new List<int>();
    List<double> distances = new List<double>();

    for(int j = 0;j < N;j++){
      int k = cloud.ClosestPoint(P);
      Point3d pt = cloud.PointAt(k);
      points.Add(pt);
      distances.Add(pt.DistanceTo(P));
      cloud.RemoveAt(k);
    }
    for(int j = 0;j < N;j++){
      indexes.Add(cloudbackup.ClosestPoint(points[j]));
    }

    Pt = points;
    i = indexes;
    D = distances;
  }
1 Like

Thank you for your reply. I’m very excited. I will try it as soon as possible.