Python - RhinoList.PointCloudKNeighbors

Hello,

Does anyone know what PointCloudKNeighhors returns? It’s supposed to be an array of integers, which I was hoping were the index numbers from the point cloud.

However, I have no idea what to do with the thing it returns.

If there are any Python Wizards out there that can give me a couple of pointers, I would be most grateful!

Ash

Hi @acichocki1,

No, it returns an array of indices for each needle point, which means a list of indices for each point that you search from (cf. reference), a list of lists so to speak.
But you’re right the indices indeed point to points from the original point cloud.

You should be able to simply loop over the returned array and get individual items/indices by using two array operators (i.e. my_array[0][0])

Hi P1r4t3b0y,

Thanks for getting back to me so quickly! That’s what I was expecting too.

Below is a simplification of what I was trying to do, but it just returns

Runtime error (TypeErrorException): ‘d__5[Point3d]’ object is not subscriptable

from Rhino.Geometry import Point3d, PointCloud
from Rhino.Collections.RhinoList import PointCloudKNeighbors

ptcloud = PointCloud()
for i in range(7):
    for j in range(7):
        for k in range(7):
            ptcloud.Add(Point3d(k, j, i))

reference_pts = []
for i in range(3):
    reference_pts.append(Point3d(i+2, 4, 4))

ptc_index = PointCloudKNeighbors(ptcloud, reference_pts, 2)

print ptc_index[0]

Rather than getting an array, I seem to be getting a:

<Rhino.Collections.RhinoList+<KNeighbors>d__5'1[[Rhino.Geometry.Point3d, RhinoCommon, Version=7.0.20252.11325, Culture=neutral, PublicKeyToken=552281e97c755530]] object at 0x0000000000000089 [Rhino.Collections.RhinoList+<KNeighbors>d__5'1[Rhino.Geometry.Point3d]]>

I’m definitely doing something wrong, but I can’t figure out what it is.

Hi @acichocki1
PointCloudKNeighbors returns IEnumerable[Array[int]],https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1?view=netcore-3.1

You can use list(IEnumerable) to convert IEnumerable to list.

I hope this help you.

2 Likes

Thanks NARUTO!

That works! I need to get used to that VB documentation. I am only a python novice, so trying to read it makes my brain ache.

Can I be cheeky and ask if there is to directly use the output of PointCloudKNeighbors to get a list of the points from the PointCloud?

Currently the process is:

PointCloud -> KNeighbors -> Index List -> Point list

Would is there a way to go directly from:

PointCloud -> KNeighbors -> Point list

The goal of using PointCloud is that I have a big array of points, and so I want to make my code as quick as possible.

Thanks again!

It’s C#, also referred by .Net, I believe. :wink:

NARUTO-sama’s code already seems quite concise. It takes him exactly two lines (i.e. 13-14) to do what you asked for. The rest is just constructing the point cloud and reference points list.

If you want to get the neighboring points quickly with a one liner - continuing NARUTO-sama’s code on line 18 -, you can do this:

# Get the neighboring points per reference point in a nested list (or list of lists)
nested_npts = [[ptcloud.PointAt(i) for i in ref_ipt] for ref_ipt in ptc_index ]
# Get all the neighboring points in a flat, one-dimensional list
flat_npts = [ptcloud.PointAt(i) for ref_ipt in ptc_index for i in ref_ipt]

You could even wrap these lines, including NARUTO-sama’s, into a function that returns the points in whatever data structure you prefer.

1 Like

Thanks P1r4t3b0y. You’ve exposed my total lack of knowledge of other languages :smiley:

Those two one-liners are a handy tip. Thanks for sharing that.