77
88from typing import Any , Tuple
99
10- from .rect_quadtree import RectQuadTree
10+ from ._native import RectQuadTree
1111
1212Point = Tuple [float , float ] # only for type hints in docstrings
1313
@@ -71,25 +71,24 @@ def __init__(
7171 """
7272 if bbox is not None :
7373 x1 , y1 , x2 , y2 = bbox
74- self .rect_quadtree = RectQuadTree (
75- (x1 , y1 , x2 , y2 ), max_items , max_depth = max_depth , track_objects = True
76- )
74+ self ._qt = RectQuadTree ((x1 , y1 , x2 , y2 ), max_items , max_depth = max_depth )
7775
7876 elif (
7977 x is not None and y is not None and width is not None and height is not None
8078 ):
81- self .rect_quadtree = RectQuadTree (
79+ self ._qt = RectQuadTree (
8280 (x - width / 2 , y - height / 2 , x + width / 2 , y + height / 2 ),
8381 max_items ,
8482 max_depth = max_depth ,
85- track_objects = True ,
8683 )
8784
8885 else :
8986 raise ValueError (
9087 "Either the bbox argument must be set, or the x, y, width, and height arguments must be set"
9188 )
9289
90+ self ._id_to_obj : dict [int , Any ] = {}
91+
9392 def insert (self , item : Any , bbox ): # pyright: ignore[reportIncompatibleMethodOverride]
9493 """
9594 Inserts an item into the quadtree along with its bounding box.
@@ -98,7 +97,8 @@ def insert(self, item: Any, bbox): # pyright: ignore[reportIncompatibleMethodOv
9897 - **item**: The item to insert into the index, which will be returned by the intersection method
9998 - **bbox**: The spatial bounding box tuple of the item, with four members (xmin,ymin,xmax,ymax)
10099 """
101- self .rect_quadtree .insert (geom = bbox , obj = item )
100+ self ._id_to_obj [id (item )] = item
101+ self ._qt .insert (id (item ), bbox )
102102
103103 def remove (self , item , bbox ):
104104 """
@@ -110,7 +110,10 @@ def remove(self, item, bbox):
110110
111111 Both parameters need to exactly match the parameters provided to the insert method.
112112 """
113- self .rect_quadtree .delete_by_object (obj = item )
113+ self ._qt .delete (id (item ), bbox )
114+
115+ # Pops
116+ self ._id_to_obj .pop (id (item ), None )
114117
115118 def intersect (self , bbox ):
116119 """
@@ -123,5 +126,6 @@ def intersect(self, bbox):
123126 Returns:
124127 - A list of inserted items whose bounding boxes intersect with the input bbox.
125128 """
126- out = list (self .rect_quadtree .query (bbox , as_items = True ))
127- return [item .obj for item in out ]
129+ results = self ._qt .query (bbox )
130+ # result = (id, x0, y0, x1, y1)
131+ return [self ._id_to_obj [result [0 ]] for result in results ]
0 commit comments