maximum recursion depth exceeded when inserting points into a quadtree using Python (dying looking for the bug)

徘徊边缘 提交于 2019-12-13 03:00:07

问题


I am trying to build a point region quadtree which stores points on a 2D map with Python, but when I try to insert two points which are close (not too close) to each other, I run into an error: RuntimeError: maximum recursion depth exceeded in cmp. I have tried to raised the maximum recursion number to 10000, but doesn't work. So I guess there's something wrong in my codes. Can someone help me with this please? I am a newbee in programming and have been stuck on this for two days. BTW, if you find any codes written not "professionally" I would really appreciate if you could teach me how to write them in the appropriate way. Many thanks in advance!

Each point consists of its coordinates (x, z) and a pointer which leads to data of this point in another file. In the quadtree, each stores only one point, so a) when inserting a point to a region which has no children and no point, the point just goes into this region; b) when a region has children, we try to insert the point into one of its children. c) when inserting a point to region with no children, but is already occupied by a point, this region is subdivided into four equal subregions, and the old point is taken out from this region and put into one of the subregions. Then, we try to insert the new point into the children.

class point():
    def __init__(self,x,z,pointer):
        self.x = x
        self.z = z
        self.pointer = pointer


class Node():
    #_______________________________________________________
    # In the case of a root node "parent" will be None. The
    # "rect" lists the minx,minz,maxx,maxz of the rectangle
    # represented by the node.
    def __init__(self, parent, rect):
        self.parent = parent
        self.children = None
        self.point = None
        self.leaf = 0   # node is a leaf(=1) if it contains a point

        if parent == None:
            self.depth = 0
        else:
            self.depth = parent.depth + 1
        self.rect = rect
        x0,z0,x1,z1 = rect


    #_______________________________________________________
    # Subdivides a rectangle. Division occurs 
    def subdivide(self):
        self.point = None
        self.leaf = 0
        self.children = [None,None,None,None]
        x0,z0,x1,z1 = self.rect
        h = (x1 - x0)/2
        rects = []
        rects.append( (x0, z0, x0 + h, z0 + h) )
        rects.append( (x0, z0 + h, x0 + h, z1) )
        rects.append( (x0 + h, z0 + h, x1, z1) )
        rects.append( (x0 + h, z0, x1, z0 + h) )
        for n in xrange(len(rects)):
            self.children[n] = Node(self,rects[n])

    #_______________________________________________________
    # A utility proc that returns True if the coordinates of
    # a point are within the bounding box of the node.
    def contains(self, x, z):
        x0,z0,x1,z1 = self.rect
        if x >= x0 and x <= x1 and z >= z0 and z <= z1:
            return True
        return False



    def insert(self, p):
        if self.contains(p.x, p.z) == False:
            return
        if self.children == None:
            if self.leaf == 1:
                temp_point = copy.copy(self.point)
                self.subdivide()
                self.insert(temp_point)
                self.insert(p)
            else:
                self.point = p
                self.leaf = 1
        else:
            for child in self.children:
                child.insert(p)

回答1:


    h = (x1 - x0)/2

If you're using Python 2.7, and if x1 and x0 here are both integers, the result of this division will be truncated down to the nearest integer. For example, if x1 is 1 and x0 is 0, you might expect their midpoint to be 0.5. But (1-0)/2 equals 0. This causes a problem in subdivide where three of the four child rects will be infinitesimally small, and the fourth one will be the same size as the parent rect. Try enforcing floating point division.

    h = (x1 - x0)/2.0


来源:https://stackoverflow.com/questions/26660296/maximum-recursion-depth-exceeded-when-inserting-points-into-a-quadtree-using-pyt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!