 : ur-bounds ( rect -- rect' )
     [ [ loc>> ] [ dim>> { 0.5 0.5 } v* ] bi v+ ] [ child-dim ] bi <rect> ;
 
-: {quadrants} ( node -- quadrants )
+: node>quadrants ( node -- quadrants )
     { [ ll>> ] [ lr>> ] [ ul>> ] [ ur>> ] } cleave 4array ;
 
 : add-subnodes ( node -- node )


: leaf= ( a b -- ? ) [ [ point>> ] [ value>> ] bi 2array ] same? ;

: node= ( a b -- ? ) [ {quadrants} ] same? ;

: (tree=) ( a b -- ? ) dup leaf?>> [ leaf= ] [ node= ] if ;


: erase ( point tree -- )
    dup leaf?>> [ leaf-erase ] [ node-erase ] if ;

: (?leaf) ( quadrant -- pair/f )
    dup point>> [ swap value>> 2array ] [ drop f ] if* ;
: ?leaf ( quadrants -- pair/f )
    [ (?leaf) ] map sift dup length {
        { 1 [ first ] }
        { 0 [ drop { f f } ] }
        [ 2drop f ]
    } case ;

: collapseable? ( node -- pair/f )
    node>quadrants { [ [ leaf?>> ] all? ] [ ?leaf ] } 1&& ;

: remove-subnodes ( node -- leaf ) f >>ll f >>lr f >>ul f >>ur t >>leaf? ;

