def add_anchor(self, anchor): if self.is_leaf(): self.anchors.append(anchor) if len(self.anchors) > self.MAX_PER_NODE and self.depth < self.MAX_DEPTH: child_bboxes = utils.subdivide_bbox(self.bbox) self.children = [AnchorQuadTree(bbox, self.depth + 1) for bbox in child_bboxes] for i, bbox in enumerate(child_bboxes): for anchor in self.anchors: if bbox.intersects(anchor.anchor): self.children[i].add_anchor(anchor) self.anchors = [] else: for child in self.children: if child.bbox.intersects(anchor.anchor): child.add_anchor(anchor)
def add_detection(self, detection): if self.is_leaf(): self.detections.append(detection) if len(self.detections) > self.MAX_PER_NODE and self.depth < self.MAX_DEPTH: child_bboxes = utils.subdivide_bbox(self.bbox) self.children = [DetectionQuadTree(bbox, self.depth + 1) for bbox in child_bboxes] for i, bbox in enumerate(child_bboxes): for detection in self.detections: if bbox.intersects(detection.bbox.x1y1x2y2): self.children[i].add_detection(detection) self.detection = [] else: for child in self.children: if child.bbox.intersects(detection.bbox.x1y1x2y2): child.add_detection(detection)