Get point in pointcloud by index

I’ve been using rhino.geometry.pointcloud to do closest point calculations, but closestpoint() only returns an index. How do I extract a pointcloud point by index? I’m trying to get the first, second and third closest points- maybe there is a faster way? I’m scripting via GhPython.

        import Rhino.Geometry as rg
       
        #basept and mypoints are supplied
        ptcloud=rg.PointCloud(mypoints)
        closestpt=[]
        index=[]
        dist=[]
        for i in range(3): #repeat 3 times
            index.append(_ptcloud.ClosestPoint(basept))
            closestpt.append(ptcloud.GetPoints()[index[-1]])
            dist.append(rg.Point3d.DistanceTo(_pt,closestpt[-1]))
            ptcloud.RemoveAt(index[-1])

I’m doing this for thousands of base points and pointclouds. GetPoints() seems to be slowing things down, especially since it is getting all points when I only need one.

Could rg.PointCloud.Item be a solution? It does not take any arguments though…

I figured it out:

closestpt.append(_ptcloud[index[-1]].Location)

But I still wonder if there is a faster way to search for multiple closest points.

@lawrenceyy, i guess this brings us back to this thread using RTree.

c.

I was hoping that there would be an option for getting the second or third closest point. If it can find the closest, surely it could find the second closest. There is a grasshopper component that does just this.

On the performance side, this operation has been wrapped up in a definition so I can multi-thread (8 cores) via ghpythonlib.parallel. I have 100,000 to search from and 100,000 to search. For my current purposes, I am looking for the closest point only. What’s happening though is that my RAM is quickly running out (32GB). The difinition only returns a list of closest points, a list of indices, and a list of distances (one item per list since I am just looking for the closest). Do I need to “purge” (not sure of terminology) before ending the definition?

The RTree example @stevebaer posted in the linked thread does this. [quote=“lawrenceyy, post:4, topic:37468”]
For my current purposes, I am looking for the closest point only
[/quote]

You might try to put the pointcloud points into a Rhino.Geometry.Collections.Point3dList and use this to get a single closest point from it.

I am not sure RAM wise if this would be more efficient compared to a GH component or any faster compared to the RTree. It may be worth a try to check if ClosestPointInList is threadsave too.

c.