Commit b4262197 authored by sumpfralle's avatar sumpfralle

use "__slots__" for kdtree classes


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@1244 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent f752d9b6
...@@ -26,6 +26,9 @@ from pycam.Geometry.kdtree import Node, kdtree ...@@ -26,6 +26,9 @@ from pycam.Geometry.kdtree import Node, kdtree
class PointKdtree(kdtree): class PointKdtree(kdtree):
__slots__ = ["_n", "tolerance"]
def __init__(self, points=None, cutoff=5, cutoff_distance=0.5, def __init__(self, points=None, cutoff=5, cutoff_distance=0.5,
tolerance=epsilon): tolerance=epsilon):
if points is None: if points is None:
...@@ -34,12 +37,7 @@ class PointKdtree(kdtree): ...@@ -34,12 +37,7 @@ class PointKdtree(kdtree):
self.tolerance = tolerance self.tolerance = tolerance
nodes = [] nodes = []
for p in points: for p in points:
n = Node() n = Node(p, (p.x, p.y, p.z))
n.point = p
n.bound = []
n.bound.append(p.x)
n.bound.append(p.y)
n.bound.append(p.z)
nodes.append(n) nodes.append(n)
kdtree.__init__(self, nodes, cutoff, cutoff_distance) kdtree.__init__(self, nodes, cutoff, cutoff_distance)
...@@ -53,19 +51,16 @@ class PointKdtree(kdtree): ...@@ -53,19 +51,16 @@ class PointKdtree(kdtree):
#return Point(x,y,z) #return Point(x,y,z)
if self._n: if self._n:
n = self._n n = self._n
n.bound = (x, y, z)
else: else:
n = Node() n = Node(None, (x, y, z))
n.bound = []
n.bound.append(x)
n.bound.append(y)
n.bound.append(z)
(nn, dist) = self.nearest_neighbor(n, self.dist) (nn, dist) = self.nearest_neighbor(n, self.dist)
if nn and (dist < self.tolerance): if nn and (dist < self.tolerance):
self._n = n self._n = n
return nn.p return nn.obj
else: else:
n.p = Point(x, y, z) n.obj = Point(x, y, z)
self._n = None self._n = None
self.insert(n) self.insert(n)
return n.p return n.obj
...@@ -29,13 +29,13 @@ def SearchKdtree2d(tree, minx, maxx, miny, maxy): ...@@ -29,13 +29,13 @@ def SearchKdtree2d(tree, minx, maxx, miny, maxy):
triangles = [] triangles = []
for n in tree.nodes: for n in tree.nodes:
if not overlaptest: if not overlaptest:
triangles.append(n.triangle) triangles.append(n.obj)
else: else:
if not (n.bound[0] > maxx if not ((n.bound[0] > maxx) or
or n.bound[1] < minx (n.bound[1] < minx) or
or n.bound[2] > maxy (n.bound[2] > maxy) or
or n.bound[3] < miny): (n.bound[3] < miny)):
triangles.append(n.triangle) triangles.append(n.obj)
return triangles return triangles
else: else:
if tree.cutdim == 0: if tree.cutdim == 0:
...@@ -74,16 +74,15 @@ def SearchKdtree2d(tree, minx, maxx, miny, maxy): ...@@ -74,16 +74,15 @@ def SearchKdtree2d(tree, minx, maxx, miny, maxy):
class TriangleKdtree(kdtree): class TriangleKdtree(kdtree):
__slots__ = []
def __init__(self, triangles, cutoff=3, cutoff_distance=1.0): def __init__(self, triangles, cutoff=3, cutoff_distance=1.0):
nodes = [] nodes = []
for t in triangles: for t in triangles:
n = Node() n = Node(t, (min(t.p1.x, t.p2.x, t.p3.x),
n.triangle = t max(t.p1.x, t.p2.x, t.p3.x),
n.bound = [] min(t.p1.y, t.p2.y, t.p3.y),
n.bound.append(min(t.p1.x, t.p2.x, t.p3.x)) max(t.p1.y, t.p2.y, t.p3.y)))
n.bound.append(max(t.p1.x, t.p2.x, t.p3.x))
n.bound.append(min(t.p1.y, t.p2.y, t.p3.y))
n.bound.append(max(t.p1.y, t.p2.y, t.p3.y))
nodes.append(n) nodes.append(n)
super(TriangleKdtree, self).__init__(nodes, cutoff, cutoff_distance) super(TriangleKdtree, self).__init__(nodes, cutoff, cutoff_distance)
......
...@@ -31,6 +31,13 @@ except ImportError: ...@@ -31,6 +31,13 @@ except ImportError:
class Node(object): class Node(object):
__slots__ = ["obj", "bound"]
def __init__(self, obj, bound):
self.obj = obj
self.bound = bound
def __repr__(self): def __repr__(self):
s = "" s = ""
for bound in self.bound: for bound in self.bound:
...@@ -62,6 +69,9 @@ def find_max_spread(nodes): ...@@ -62,6 +69,9 @@ def find_max_spread(nodes):
class kdtree(IDGenerator): class kdtree(IDGenerator):
__slots__ = ["bucket", "dim", "cutoff", "cutoff_distance", "nodes",
"cutdim", "minval", "maxval", "cutval", "hi", "lo"]
def __init__(self, nodes, cutoff, cutoff_distance): def __init__(self, nodes, cutoff, cutoff_distance):
super(kdtree, self).__init__() super(kdtree, self).__init__()
self.bucket = False self.bucket = False
...@@ -83,8 +93,7 @@ class kdtree(IDGenerator): ...@@ -83,8 +93,7 @@ class kdtree(IDGenerator):
else: else:
self.bucket = False self.bucket = False
self.cutdim = cutdim self.cutdim = cutdim
nodes.sort(cmp=lambda x, y: nodes.sort(key=lambda item: item.bound[cutdim])
cmp(x.bound[cutdim], y.bound[cutdim]))
median = len(nodes) / 2 median = len(nodes) / 2
self.minval = nodes[0].bound[cutdim] self.minval = nodes[0].bound[cutdim]
self.maxval = nodes[-1].bound[cutdim] self.maxval = nodes[-1].bound[cutdim]
...@@ -229,4 +238,3 @@ class kdtree(IDGenerator): ...@@ -229,4 +238,3 @@ class kdtree(IDGenerator):
else: else:
self.hi.insert(node) self.hi.insert(node)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment