Pella86

Quad-tree.py

Mar 10th, 2012
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.31 KB | None | 0 0
  1.         def analyze_node(self, node, obj, r):
  2.             #return true if one of the leaf or subleaf is in range of v2
  3.             #return false if the node is out of range
  4.             #it doesn't analyze all the children if it finds one in range it return
  5.             if node.is_leaf() and not node.is_empty():
  6.                 #if the node data is in range return true
  7.                 if intersect_p_r(node.data.get_position(), obj.position, r) and not id(obj) == node.data.get_id():
  8.                     return True
  9.             elif node.is_split_node():
  10.                 for children in node.nodes:
  11.                     if self.analyze_node(children, obj, r):
  12.                         return True
  13.             return False
  14.        
  15.         def find_split_node(self, node, obj, r):
  16.             #return the parent node with at least one intersecting point
  17.             parent = node.parent
  18.             if parent == None:
  19.                 #it's the root
  20.                 return node
  21.             if self.analyze_node(parent, obj, r):
  22.                 #parent has children in range
  23.                 if parent.parent == None:
  24.                     #print "I'm the root"
  25.                     return parent #stop the iteration
  26.                 return self.find_split_node(parent.parent, obj, r)
  27.             else:
  28.                 return parent #found a node before the root that doesn't have children intersecting the range
  29.                    
  30.         def point_in_range(self, split_node, obj, r, point_list):
  31.             for children in split_node.nodes:
  32.                 if children.is_leaf() and not children.is_empty():
  33.                     if intersect_p_r(children.data.get_position(), obj.position, r) and not  obj.position.is_equal(children.data.get_position()):
  34.                         point_list.append(children.data)
  35.                 if children.is_split_node():
  36.                     point_list = self.point_in_range(children, obj, r, point_list)
  37.             return point_list
  38.                
  39.         def range_search(self, obj, r):
  40.             point_list = [] #the objects/point in range list
  41.             tnode = self.find_obj(obj) #find the node that corresponds to the object
  42.             split_node = self.find_split_node(tnode, obj, r) #the node where the parent has at least one intersecting children
  43.             for children in split_node.nodes:
  44.                 if children.is_leaf() and not children.is_empty():
  45.                     #check if the node itersect with the children
  46.                     if intersect_p_r(children.data.get_position(), obj.position, r) and not  id(obj) == children.data.get_id():
  47.                         point_list.append(children.data) #append the points contained in the node
  48.                 elif children.is_split_node():
  49.                     #else recurse over the childrens
  50.                     point_list = self.point_in_range(children, obj, r, point_list)
Advertisement
Add Comment
Please, Sign In to add comment