Skip to contents

While a kd tree is often used to locate nearest neighbors, it works equally well for quickly looking up points contained within a geometry. This type of query obviously doesn't work for points but only circles/spheres and rectangles/cubes. As with kd_tree_search() it is possible to do a fuzzy search by providing an eps, but it works differently. For kd_tree_range() the eps argument provides a fuzzy zone around the query geometry. For circle/spheres, this means that all points inside radius - eps will get reported, points inside radius + eps, but outside radius - eps may get reported, and points outside radius + eps will not get reported. For rectangles/cubes it works the same but instead it dilates and expands the box by the eps arguments to create the fuzzy zones.

Usage

kd_tree_range(geometries, tree, eps = 0, ...)

Arguments

geometries

A vector of geometries to use for queries. Either a euclid_circle2, euclid_sphere, euclid_iso_rect, or euclid_iso_cube vector. euclid_bbox will get coerced to euclid_iso_rect/euclid_iso_cube

tree

a orion_kd_tree

eps

Fuzzyness factor for the query. See the description. Will recycle to the length of geometries

...

Arguments passed on

Value

A list with elements points holding a euclid_point vector and id

matching the points to the index of geometries

See also

Other kd tree queries: kd_tree_search()

Examples

# Create a kd tree with points
pts <- euclid::point(runif(100), runif(100))
tree <- kd_tree(pts)

# Do an exact range query (eps = 0)
pt <- euclid::point(0.4, 0.7)
circ <- euclid::circle(pt, 0.02)
inside <- kd_tree_range(circ, tree)
plot(pts, cex = 1)
euclid_plot(inside$points, cex = 0.6, pch = 16, col = 'red')
euclid_plot(circ, fg = 'green')


# Do a fuzzy range query
circ <- euclid::circle(pt, 0.04)
inside <- kd_tree_range(circ, tree, 0.1)
plot(pts, cex = 1)
euclid_plot(inside$points, cex = 0.6, pch = 16, col = 'red')
euclid_plot(circ, fg = 'green')
euclid_plot(circle(pt, 0.1^2), fg = 'green', lty = 2)
euclid_plot(circle(pt, 0.3^2), fg = 'green', lty = 2)