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.
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.
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))
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?
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.
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).
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: