From c2109ceb0420bb86727c6964f7d609c9189db33b Mon Sep 17 00:00:00 2001 From: Patrick Herbers Date: Wed, 25 May 2016 14:50:14 +0200 Subject: [PATCH] Minor speedup for UV-Quadtree Getting polygons from a quadtree now returns a generator instead of a list and is now iterative instead of recursive. This speeds up the connection computation slightly. --- pam/mesh.py | 3 +-- pam/utils/quadtree.py | 25 ++++++++++++++----------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/pam/mesh.py b/pam/mesh.py index 1b09885..e49c90d 100644 --- a/pam/mesh.py +++ b/pam/mesh.py @@ -180,8 +180,7 @@ def mapUVPointTo3d(obj_uv, uv_list, check_edges = False, cleanup=True): for i in point_indices: point = uv_list[i] - polygons = qtree.getPolygons(point) - for polygon in polygons: + for polygon in qtree.getPolygons(point): uvs = polygon[0] p3ds = polygon[1] diff --git a/pam/utils/quadtree.py b/pam/utils/quadtree.py index f238ae0..d629421 100644 --- a/pam/utils/quadtree.py +++ b/pam/utils/quadtree.py @@ -36,17 +36,20 @@ def addPolygon(self, polygon): def getPolygons(self, point): """Gives a list of all polygons in the quadtree that may contain the point""" - p = point - if p[0] < self.left or p[0] > self.right or p[1] < self.top or p[1] > self.bottom: - return [] - else: - result = list(self.polygons) - if all(self.children): - result.extend(self.children[0].getPolygons(p)) - result.extend(self.children[1].getPolygons(p)) - result.extend(self.children[2].getPolygons(p)) - result.extend(self.children[3].getPolygons(p)) - return result + node = self + while True: + for polygon in node.polygons: + yield polygon + if not node.children[0]: + break + if point[0] < node.children[0].right: + c = 0 + else: + c = 1 + if point[1] > node.children[0].bottom: + c += 2 + node = node.children[c] + def buildQuadtree(depth = 2, left = 0.0, top = 0.0, right = 1.0, bottom = 1.0): """Builds a new quadtree recursively with the given depth."""