Skip to content

Commit

Permalink
Minor speedup for UV-Quadtree
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pherbers committed May 25, 2016
1 parent c213020 commit c2109ce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
3 changes: 1 addition & 2 deletions pam/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
25 changes: 14 additions & 11 deletions pam/utils/quadtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down

0 comments on commit c2109ce

Please sign in to comment.