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
class PointKdtree(kdtree):
__slots__ = ["_n", "tolerance"]
def __init__(self, points=None, cutoff=5, cutoff_distance=0.5,
tolerance=epsilon):
if points is None:
......@@ -34,12 +37,7 @@ class PointKdtree(kdtree):
self.tolerance = tolerance
nodes = []
for p in points:
n = Node()
n.point = p
n.bound = []
n.bound.append(p.x)
n.bound.append(p.y)
n.bound.append(p.z)
n = Node(p, (p.x, p.y, p.z))
nodes.append(n)
kdtree.__init__(self, nodes, cutoff, cutoff_distance)
......@@ -53,19 +51,16 @@ class PointKdtree(kdtree):
#return Point(x,y,z)
if self._n:
n = self._n
n.bound = (x, y, z)
else:
n = Node()
n.bound = []
n.bound.append(x)
n.bound.append(y)
n.bound.append(z)
n = Node(None, (x, y, z))
(nn, dist) = self.nearest_neighbor(n, self.dist)
if nn and (dist < self.tolerance):
self._n = n
return nn.p
return nn.obj
else:
n.p = Point(x, y, z)
n.obj = Point(x, y, z)
self._n = None
self.insert(n)
return n.p
return n.obj
......@@ -29,13 +29,13 @@ def SearchKdtree2d(tree, minx, maxx, miny, maxy):
triangles = []
for n in tree.nodes:
if not overlaptest:
triangles.append(n.triangle)
triangles.append(n.obj)
else:
if not (n.bound[0] > maxx
or n.bound[1] < minx
or n.bound[2] > maxy
or n.bound[3] < miny):
triangles.append(n.triangle)
if not ((n.bound[0] > maxx) or
(n.bound[1] < minx) or
(n.bound[2] > maxy) or
(n.bound[3] < miny)):
triangles.append(n.obj)
return triangles
else:
if tree.cutdim == 0:
......@@ -74,16 +74,15 @@ def SearchKdtree2d(tree, minx, maxx, miny, maxy):
class TriangleKdtree(kdtree):
__slots__ = []
def __init__(self, triangles, cutoff=3, cutoff_distance=1.0):
nodes = []
for t in triangles:
n = Node()
n.triangle = t
n.bound = []
n.bound.append(min(t.p1.x, t.p2.x, t.p3.x))
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))
n = Node(t, (min(t.p1.x, t.p2.x, t.p3.x),
max(t.p1.x, t.p2.x, t.p3.x),
min(t.p1.y, t.p2.y, t.p3.y),
max(t.p1.y, t.p2.y, t.p3.y)))
nodes.append(n)
super(TriangleKdtree, self).__init__(nodes, cutoff, cutoff_distance)
......
......@@ -31,6 +31,13 @@ except ImportError:
class Node(object):
__slots__ = ["obj", "bound"]
def __init__(self, obj, bound):
self.obj = obj
self.bound = bound
def __repr__(self):
s = ""
for bound in self.bound:
......@@ -62,6 +69,9 @@ def find_max_spread(nodes):
class kdtree(IDGenerator):
__slots__ = ["bucket", "dim", "cutoff", "cutoff_distance", "nodes",
"cutdim", "minval", "maxval", "cutval", "hi", "lo"]
def __init__(self, nodes, cutoff, cutoff_distance):
super(kdtree, self).__init__()
self.bucket = False
......@@ -83,8 +93,7 @@ class kdtree(IDGenerator):
else:
self.bucket = False
self.cutdim = cutdim
nodes.sort(cmp=lambda x, y:
cmp(x.bound[cutdim], y.bound[cutdim]))
nodes.sort(key=lambda item: item.bound[cutdim])
median = len(nodes) / 2
self.minval = nodes[0].bound[cutdim]
self.maxval = nodes[-1].bound[cutdim]
......@@ -228,5 +237,4 @@ class kdtree(IDGenerator):
self.lo.insert(node)
else:
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