Removing nodes off rtree

I have a list of points and I want to remove all nodes off my rtree within 2mm radius of each point on the list, but the code below is making my rhino crash. I feel like I’m not doing this in the most optimum way tbh. Any advice?

        def Remove_Nodes(sender, e):
           node = nodes[e.Id]
            r_tree.Remove(node, e.Id)
        
        # removing surrounding nodes
        for point in points:
            bounding_sphere = rg.Sphere(point, 2)
            r_tree.Search(bounding_sphere, Remove_Nodes)
  • nodes is another list of points, and the rtree was created from those, so they should have the same index I assume.

Where do r_tree and nodes come from?

Try it with only one point, to see if it’s the number of points or the code in the loop…

Both the r_tree and the nodes came from the same point cloud. The only different is that nodes is a list of points while the r_tree is an instance of the rtree class.

Quick update, rhino actually crashes during the “r_tree.Remove(node, e.Id)” line.

I tried with one point and it still crashed.

What are you trying to accomplish? Because removing all points within 2mm of each point will leave you with no points in your rtree.

Also, I’m not able to reproduce the crash with the code below, single point, in Rhino 8.7.

points = [rg.Point3d(0,0,0), rg.Point3d(10,0,0)]

pc = rg.PointCloud(points)

rtree = rg.RTree.CreatePointCloudTree(pc)

Rhino.RhinoApp.WriteLine("There are {0} points in the rtree".format(rtree.Count))

def Remove_Nodes(sender, e):
    rtree.Remove(points[e.Id], e.Id)

for point in points:
    boundingSphere = rg.Sphere(point, 2)
    rtree.Search(boundingSphere, Remove_Nodes)

Rhino.RhinoApp.WriteLine("There are {0} points in the rtree".format(rtree.Count))
1 Like

The rtree and the nodes list have the same points in it, but the points list - the one I’m using as the center point for the sphere - has a different set of fewer points. So removing all nodes within 2mm of each point in the points list off the rtree won’t eliminate all nodes, not even close.

I reduced the radius of the search to 0.5 and it worked. Apparently picking up too many points is what is making Remove() crash. Unfortunately I do need the 2mm radius and I’m unsure how to solve this, any ideas?

has a different set of fewer points

got it, that makes sense.

Let’s start with establishing that this should not crash, so there’s definitely something going on that shouldn’t happen, and that we can hopefully fix.

To let us help you, you’ll need to help us a little bit too. If you could share a complete code example where this happens, including the points of the RTree and the points you iterate over for removal, that’d be great, so I can test this on my development enviroment to try and figure out what is going on.

Although I can’t share the script I’m working on, I created this test one which shows a similar behavior.

script_test.py (603 Bytes)
test_part.3dm (14.2 MB)

Thank you for looking into this, I really appreciate.

I can’t think how you could’ve simplified the code further - thanks.

That crashes Rhino for me too on my l’il laptop.

The only idea I have is that the point cloud is just too huge for the computation:

Point cloud with 618638 points.
  (-21.898,-11.944,0.000), (-21.400,-11.944,0.000), (-20.902,-11.944,0.000),

The point cloud is indeed quite big, but I don’t think that should crash rhino. I’m running it on a desktop and neither my memory nor my cpu get anywhere near 100% before the crash. I also have other scripts doing way more complex calculation with the same point cloud and they don’t crash rhino (but none of them use the remove() method).

“I don’t think” is all very well, but it should be ruled out. Try the same code on a much smaller point cloud.

You probably shouldn’t edit the point cloud while you’re still searching through it. Instead of removing right away collect the e.Id in a list (or set as I do here), then after the search do your removal:

This is typically wat you should when editing datastructures in this way: first phase collect data to remove, second phase apply the removal.

3 Likes

Thank you Nathan, that solved the issue.