Advertisement
Geometrian

Continuous Subdivision Quadtree Pseudocode

Oct 4th, 2018
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. class SpecialQuadtree():
  2.  
  3.     def insert(self, point,associated_data):
  4.         cell = self.putIntoQuadtree( point, associated_data )
  5.  
  6.         min_depth = associated_data.getMinDepth()
  7.         while cell.getDepth() < min_depth:
  8.             cell.subdivide()
  9.  
  10.         self.enforceInvariant()
  11.  
  12.     def enforceInvariant(self):
  13.         """Ensure that every cell has no more than two neighbors along any edge"""
  14.  
  15.         changed = True
  16.         while changed:
  17.             changed = False
  18.  
  19.             for cell in self.quadtree:
  20.                 if (
  21.                     len( cell.neighborsToLeft()   ) > 2 or
  22.                     len( cell.neighborsToRight()  ) > 2 or
  23.                     len( cell.neighborsToBottom() ) > 2 or
  24.                     len( cell.neighborsToTop()    ) > 2
  25.                 ):
  26.                     cell.subdivide()
  27.                     changed = True
  28.  
  29. def my_code():
  30.     quadtree = SpecialQuadtree()
  31.  
  32.     for i in range(1000000):
  33.         point = generate_random_point()
  34.         associated_data = long_computation( point )
  35.  
  36.         quadtree.insert(point,associated_data)
  37.  
  38.     while quadtree.hasEmptyCells():
  39.         empty_cell = quadtree.getAnyEmptyCell()
  40.  
  41.         point = generate_random_point_inside( empty_cell.getBounds() )
  42.         associated_data = long_computation( point )
  43.  
  44.         quadtree.insert(point,associated_data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement