Title: | Spatial Search Trees |
---|---|
Description: | The QuadTree data structure is useful for fast, neighborhood-restricted lookups. We use it to implement fast k-Nearest Neighbor and Rectangular range lookups in 2 dimenions. The primary target is high performance interactive graphics. |
Authors: | Gabriel Becker |
Maintainer: | Gabriel Becker <[email protected]> |
License: | LGPL |
Version: | 0.5.6 |
Built: | 2024-11-09 03:51:34 UTC |
Source: | https://github.com/gmbecker/searchtrees |
Create a search tree from the supplied data for use in during future lookups.
createTree(data, treeType = "quad", dataType = "point", columns = if (dataType=="point") 1:2 else 1:4, ...)
createTree(data, treeType = "quad", dataType = "point", columns = if (dataType=="point") 1:2 else 1:4, ...)
data |
data.frame or matrix. Data to be indexed. |
treeType |
Character. Indicates type of index tree to be created. Currently only "quad" (quad trees) is supported. |
dataType |
Character. Indicates type of data being indexed. Currently "point", and "rect" are supported corresponding to points and rectangles, respectively. Defaults to "point". |
columns |
Numeric. Indicates columns in |
... |
Any additional/type specific parameters to be passed to the tree creation function. These include:
|
For a point based tree, the two columns specified in columns
represent the x and y values of the points.
For a rectangle based tree, four columns must be specified. These columns represent the x and y coordinates of point 1 and the x and y coordinates of point 2, in that order (where point 1 and point 2 specify the rectangle to be stored).
The class of the returned object depends on the tree type created,
though all will inherit from the SearchTree
S4 class and have the
following slots:
ref |
An external pointer to the C level data structure. |
numNodes |
Total number of nodes comprising the tree. |
dataNodes |
Number of nodes which store at least one data point. |
maxDepth |
Maximum depth of the tree. |
maxBucket |
Maximum number of data points stored in a single node. |
totalData |
Number of items indexed in the tree. |
dataType |
Type of objects stored in the tree. |
Gabriel Becker
Finkel, R. A. and Bentley, J. L. "Quad Trees, a Data Structure for Retrieval on Composite Keys." Acta Informatica 4, 1-9, 1974.
SearchTree
linkS4Class{QuadTree}
x = rnorm(100) y = rnorm(100) dat = cbind(x,y) tree = createTree(dat)
x = rnorm(100) y = rnorm(100) dat = cbind(x,y) tree = createTree(dat)
This function performs fast k-Nearest Neighbors lookup on a SearchTree object
knnLookup(tree, newx, newy, newdat, columns = 1:2, k = 5)
knnLookup(tree, newx, newy, newdat, columns = 1:2, k = 5)
tree |
An object which inherits from the |
newx |
Numeric. Vector of x values for the points to look up neighbors for. |
newy |
Numeric. Vector of x values for the points to look up neighbors for. |
newdat |
Matrix or data.frame. Data containing x and y values of the points
to look up neighbors for. Ignored if |
columns |
Numeric. Columns x and y values can be found in within |
k |
Numeric. Number of neighbors to find for each point. Must be
|
The return value is an integer matrix indicating the indices in
the original data used to create treE
where the nearest neighbors were found. Row indicates
the indice of the new point, while column indicates the order of the k neighbors.
No defined order is specified for exact ties in distance.
Gabriel Becker
x = rnorm(100) y = rnorm(100) tree = createTree(cbind(x,y)) newx = c(0, .5) newy = c(.5, 0) inds = knnLookup(tree, newx, newy, k=7) ch = rep(1, times=100) ch[inds[1:7]] = 3 ch[inds[8:14]] = 5 cls = rep("black", times=100) cls[inds[1:7]] = "red" cls[inds[8:14]] ="blue" plot(x,y, pch=ch, col = cls) abline(v=newx[1], h = newy[1] , col="red") abline(v=newx[2], h = newy[2], col = "blue")
x = rnorm(100) y = rnorm(100) tree = createTree(cbind(x,y)) newx = c(0, .5) newy = c(.5, 0) inds = knnLookup(tree, newx, newy, k=7) ch = rep(1, times=100) ch[inds[1:7]] = 3 ch[inds[8:14]] = 5 cls = rep("black", times=100) cls[inds[1:7]] = "red" cls[inds[8:14]] ="blue" plot(x,y, pch=ch, col = cls) abline(v=newx[1], h = newy[1] , col="red") abline(v=newx[2], h = newy[2], col = "blue")
knnLookup
in Package SearchTrees ~~~~ Methods for function knnLookup
in package SearchTrees ~~
signature(tree = "QuadTree")
"QuadTree"
A class representing a Quad Tree object for storing 2 dimensional points for efficient rectangular range and knn lookup.
Objects can be created by calls of the form new("QuadTree", ...)
.
ref
:Object of class "externalptr"
Pointer to
the internal representation of the tree
numNodes
:Object of class "integer"
Number of
nodes in the tree
dataNodes
:Object of class "integer"
Number of
nodes in the tree which are storing data
maxDepth
:Object of class "integer"
Maximum
depth of the tree.
maxBucket
:Object of class "integer"
Maximum
number of data points which are stored at a single node
totalData
:Object of class "integer"
Number of
objects stored in the tree
dataType
:Object of class "character"
Indicates
type of data stored in the tree.
Class "SearchTree"
, directly.
signature(tree = "QuadTree")
: ...
signature(tree = "QuadTree")
: ...
When using createIndex to create a quadTree, only two columns of the
matrix/data.frame passed to the function will be used to create the
tree. See the columns argument in createTree
Gabriel Becker
showClass("QuadTree")
showClass("QuadTree")
Determine which objects, stored in a SearchTrees indexing object, fall within a given rectangle in two-dimensional space.
rectLookup(tree, ptOne, ptTwo, xlims, ylims)
rectLookup(tree, ptOne, ptTwo, xlims, ylims)
tree |
SearchTree. A SearchTree object to perform the lookup on. |
ptOne |
Numeric. A numeric of length two indicating x and y values for one corner of the rectangle. |
ptTwo |
Numeric. A numeric of length two indicating x and y values for the
corner of the rectangle opposite to |
xlims |
Numeric. A numeric vector indicating the minimum and maximum x value
for the rectangle. Overrides |
ylims |
Numeric. A numeric vector indicating the minimum and maximum y value for
the rectangle. Overrides |
In the case of lookup for rectangular objects, any rectangle which overlaps the query rectangle will be returned.
A numeric vector indicating the indicies of the object (in the order they were in when the SearchTree object was created) which fall (at least partially) within the rectangular query.
Gabriel Becker
x = rnorm(100) y = rnorm(100) x2 = x + runif(100, .5, 2) y2 = y + runif(100, .5, 2) dat2 = cbind(x, y, x2, y2) tree2 = createTree(dat2, dataType="rect", columns= 1:4) inrect = rectLookup(tree2, xlim = c(0,1), ylim=c(0, 1)) col = rgb(0, 1, 0, alpha=.5) plot(x, y2, col="white") rect(x[inrect], y[inrect], x2[inrect], y2[inrect], col=col) rect(0, 0, 1, 1, col="blue", lwd=3)
x = rnorm(100) y = rnorm(100) x2 = x + runif(100, .5, 2) y2 = y + runif(100, .5, 2) dat2 = cbind(x, y, x2, y2) tree2 = createTree(dat2, dataType="rect", columns= 1:4) inrect = rectLookup(tree2, xlim = c(0,1), ylim=c(0, 1)) col = rgb(0, 1, 0, alpha=.5) plot(x, y2, col="white") rect(x[inrect], y[inrect], x2[inrect], y2[inrect], col=col) rect(0, 0, 1, 1, col="blue", lwd=3)
rectLookup
in Package SearchTrees
Methods for function rectLookup
in package SearchTrees
signature(tree = "QuadTree")
"SearchTree"
A virtual class representing a search tree for storing geometric points in a manner designed for efficient lookup.
This is a virtual class so objects of class SearchTree cannot be created directly.No methods defined with class "SearchTree" in the signature.
ref
:Object of class "externalptr"
Pointer to
the internal representation of the tree.
numNodes
:Object of class "integer"
Number of
nodes in the tree
dataNodes
:Object of class "integer"
Number of
nodes in the tree which are storing data.
maxDepth
:Object of class "integer"
Maximum
depth of the tree
maxBucket
:Object of class "integer"
Maximum
number of data points stored in a single node
totalData
:Object of class "integer"
Number of
data objects stored in the tree.
dataType
:Object of class "character"
Indicates
type of data stored in the tree.
knnLookup
, rectLookup
Gabriel Becker