gold-plate quadtrees

db4
Joe Groff 2009-02-02 15:16:03 -06:00
parent e9972d3ff3
commit 82daca7f6f
6 changed files with 59 additions and 19 deletions

View File

@ -0,0 +1 @@
Joe Groff

View File

@ -0,0 +1,34 @@
USING: arrays assocs help.markup help.syntax math.geometry.rect quadtrees quotations sequences ;
IN: quadtrees
ARTICLE: "quadtrees" "Quadtrees"
"The " { $snippet "quadtrees" } " vocabulary implements the quadtree structure in Factor. Quadtrees follow the " { $link "assocs-protocol" } " for insertion, deletion, and querying of exact points, using two-dimensional vectors as keys. Additional words are provided for spatial queries and pruning the tree structure:"
{ $subsection prune }
{ $subsection in-rect }
"The following words are provided to help write quadtree algorithms:"
{ $subsection descend }
{ $subsection each-quadrant }
{ $subsection map-quadrant } ;
ABOUT: "quadtrees"
HELP: prune
{ $values { "tree" quadtree } }
{ $description "Removes empty nodes from " { $snippet "tree" } "." } ;
HELP: in-rect
{ $values { "tree" quadtree } { "rect" rect } { "values" sequence } }
{ $description "Returns a " { $link sequence } " of values from " { $snippet "tree" } " whose keys lie inside " { $snippet "rect" } "." } ;
HELP: descend
{ $values { "pt" sequence } { "node" quadtree } { "subnode" quadtree } }
{ $description "Descends into the subnode of quadtree node " { $snippet "node" } " that contains " { $snippet "pt" } ", leaving " { $snippet "pt" } " on the stack." } ;
HELP: each-quadrant
{ $values { "node" quadtree } { "quot" quotation } }
{ $description "Calls " { $snippet "quot" } " with each subnode of " { $snippet "node" } " on the top of the stack in turn." } ;
HELP: map-quadrant
{ $values { "node" quadtree } { "quot" quotation } { "array" array } }
{ $description "Calls " { $snippet "quot" } " with each subnode of " { $snippet "node" } " on the top of the stack in turn, collecting the four results into " { $snippet "array" } "." } ;

View File

@ -1,3 +1,4 @@
! (c) 2009 Joe Groff, see BSD license
USING: assocs kernel tools.test quadtrees math.geometry.rect sorting ;
IN: quadtrees.tests

View File

@ -1,3 +1,4 @@
! (c) 2009 Joe Groff, see BSD license
USING: assocs kernel math.geometry.rect combinators accessors
math.vectors vectors sequences math math.points math.geometry
combinators.short-circuit arrays fry locals ;
@ -14,6 +15,25 @@ TUPLE: quadtree { bounds rect } point value ll lr ul ur leaf? ;
: rect-center ( rect -- point ) [ loc>> ] [ dim>> 0.5 v*n ] bi v+ ; inline
: (quadrant) ( pt node -- quadrant )
swap [ first 0.0 < ] [ second 0.0 < ] bi
[ [ ll>> ] [ lr>> ] if ]
[ [ ul>> ] [ ur>> ] if ] if ;
: quadrant ( pt node -- quadrant )
[ bounds>> rect-center v- ] keep (quadrant) ;
: descend ( pt node -- pt subnode )
[ drop ] [ quadrant ] 2bi ; inline
:: each-quadrant ( node quot -- )
node ll>> quot call
node lr>> quot call
node ul>> quot call
node ur>> quot call ; inline
: map-quadrant ( node quot: ( child-node -- x ) -- array )
each-quadrant 4array ; inline
<PRIVATE
DEFER: (prune)
@ -35,28 +55,9 @@ DEFER: in-rect*
: ur-bounds ( rect -- rect' )
[ [ loc>> ] [ dim>> { 0.5 0.5 } v* ] bi v+ ] [ child-dim ] bi <rect> ;
: (quadrant) ( pt node -- quadrant )
swap [ first 0.0 < ] [ second 0.0 < ] bi
[ [ ll>> ] [ lr>> ] if ]
[ [ ul>> ] [ ur>> ] if ] if ;
: quadrant ( pt node -- quadrant )
[ bounds>> rect-center v- ] keep (quadrant) ;
: descend ( pt node -- pt subnode )
[ drop ] [ quadrant ] 2bi ; inline
: {quadrants} ( node -- quadrants )
{ [ ll>> ] [ lr>> ] [ ul>> ] [ ur>> ] } cleave 4array ;
:: each-quadrant ( node quot -- array )
node ll>> quot call
node lr>> quot call
node ul>> quot call
node ur>> quot call ; inline
: map-quadrant ( node quot: ( child-node -- x ) -- array )
each-quadrant 4array ; inline
: add-subnodes ( node -- node )
dup bounds>> {
[ ll-bounds <quadtree> >>ll ]

View File

@ -0,0 +1 @@
Quadtree spatial indices

2
extra/quadtrees/tags.txt Normal file
View File

@ -0,0 +1,2 @@
assocs
graphics