From a679883adf5cca9910273ae6ac3230c2506f50cb Mon Sep 17 00:00:00 2001 From: Juan Pablo Caram Date: Tue, 20 Oct 2015 19:53:39 -0400 Subject: [PATCH] RTree now using shapely object's id() instead of __hash__. Fixes 1#163. --- camlib.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/camlib.py b/camlib.py index a29aa1a2..6e8b3a57 100644 --- a/camlib.py +++ b/camlib.py @@ -3654,6 +3654,13 @@ class FlatCAMRTree(object): self.get_points = lambda go: go.coords def grow_obj2points(self, idx): + """ + Increases the size of self.obj2points to fit + idx + 1 items. + + :param idx: Index to fit into list. + :return: None + """ if len(self.obj2points) > idx: # len == 2, idx == 1, ok. return @@ -3699,15 +3706,23 @@ class FlatCAMRTreeStorage(FlatCAMRTree): def insert(self, obj): self.objects.append(obj) idx = len(self.objects) - 1 - self.indexes[obj] = idx + + # Note: Shapely objects are not hashable any more, althought + # there seem to be plans to re-introduce the feature in + # version 2.0. For now, we will index using the object's id, + # but it's important to remember that shapely geometry is + # mutable, ie. it can be modified to a totally different shape + # and continue to have the same id. + # self.indexes[obj] = idx + self.indexes[id(obj)] = idx + super(FlatCAMRTreeStorage, self).insert(idx, obj) #@profile def remove(self, obj): - # Get index in list - # TODO: This is extremely expensive - #objidx = self.objects.index(obj) - objidx = self.indexes[obj] + # See note about self.indexes in insert(). + # objidx = self.indexes[obj] + objidx = self.indexes[id(obj)] # Remove from list self.objects[objidx] = None