Safe Haskell | None |
---|---|
Language | Haskell2010 |
- type PointAsListFn a p = p -> [a]
- type SquaredDistanceFn a p = p -> p -> a
- data KdMap a p v
- buildKdMap :: Real a => PointAsListFn a p -> [(p, v)] -> KdMap a p v
- buildKdMapWithDistFn :: Real a => PointAsListFn a p -> SquaredDistanceFn a p -> [(p, v)] -> KdMap a p v
- nearestNeighbor :: Real a => KdMap a p v -> p -> (p, v)
- pointsInRadius :: Real a => KdMap a p v -> a -> p -> [(p, v)]
- kNearestNeighbors :: Real a => KdMap a p v -> Int -> p -> [(p, v)]
- pointsInRange :: Real a => KdMap a p v -> p -> p -> [(p, v)]
- assocs :: KdMap a p v -> [(p, v)]
- points :: KdMap a p v -> [p]
- values :: KdMap a p v -> [v]
- size :: KdMap a p v -> Int
- foldrKdMap :: ((p, v) -> b -> b) -> b -> KdMap a p v -> b
- defaultDistSqrFn :: Num a => PointAsListFn a p -> SquaredDistanceFn a p
- runTests :: IO Bool
Usage
The KdMap
is a variant of KdTree
where each point in
the tree is associated with some data. When talking about KdMap
s,
we'll refer to the points and their associated data as the points
and values of the KdMap
, respectively. It might help to think
of KdTree
and KdMap
as being analogous to
Set
and Map
.
Suppose you wanted to perform point queries on a set of 3D points,
where each point is associated with a String
. Here's how to build
a KdMap
of the data and perform a nearest neighbor query (if this
doesn't make sense, start with the documentation for
KdTree
):
>>> let points = [(Point3d 0.0 0.0 0.0), (Point3d 1.0 1.0 1.0)] >>> let valueStrings = ["First", "Second"] >>> let pointValuePairs = zip points valueStrings >>> let kdm = buildKdMap point3dAsList pointValuePairs >>> nearestNeighbor kdm (Point3d 0.1 0.1 0.1) [Point3d {x = 0.0, y = 0.0, z = 0.0}, "First"]
Reference
Types
type PointAsListFn a p = p -> [a] Source
Converts a point of type p
with axis values of type
a
into a list of axis values [a].
type SquaredDistanceFn a p = p -> p -> a Source
Returns the squared distance between two points of type
p
with axis values of type a
.
A k-d tree structure that stores points of type p
with axis
values of type a
. Additionally, each point is associated with a
value of type v
.
k-d map construction
buildKdMap :: Real a => PointAsListFn a p -> [(p, v)] -> KdMap a p v Source
Builds a KdTree
from a list of pairs of points (of type p) and
values (of type v) using a default squared distance function
defaultDistSqrFn
.
Average complexity: O(n * log(n)) for n data points.
Worst case time complexity: O(n^2) for n data points.
Worst case space complexity: O(n) for n data points.
Throws an error if given an empty list of data points.
buildKdMapWithDistFn :: Real a => PointAsListFn a p -> SquaredDistanceFn a p -> [(p, v)] -> KdMap a p v Source
Builds a KdMap
from a list of pairs of points (of type p) and
values (of type v), using a user-specified squared distance
function.
Average time complexity: O(n * log(n)) for n data points.
Worst case time complexity: O(n^2) for n data points.
Worst case space complexity: O(n) for n data points.
Throws an error if given an empty list of data points.
Query
nearestNeighbor :: Real a => KdMap a p v -> p -> (p, v) Source
kNearestNeighbors :: Real a => KdMap a p v -> Int -> p -> [(p, v)] Source
Given a KdMap
, a query point, and a number k
, returns the k
point-value pairs with the nearest points to the query.
Neighbors are returned in order of increasing distance from query point.
Average time complexity: log(k) * log(n) for k nearest neighbors on a structure with n data points.
Worst case time complexity: n * log(k) for k nearest neighbors on a structure with n data points.
:: Real a | |
=> KdMap a p v | |
-> p | lower bounds of range |
-> p | upper bounds of range |
-> [(p, v)] | point-value pairs within given range |
Finds all point-value pairs in a KdMap
with points within a
given range, where the range is specified as a set of lower and
upper bounds.
Points are not returned in any particular order.
Worst case time complexity: O(n) for n data points and a range that spans all the points.
TODO: Maybe use known bounds on entire tree structure to be able to automatically count whole portions of tree as being within given range.
assocs :: KdMap a p v -> [(p, v)] Source
Returns a list of all the point-value pairs in the KdMap
.
Time complexity: O(n) for n data points.
points :: KdMap a p v -> [p] Source
Returns all points in the KdMap
.
Time complexity: O(n) for n data points.
values :: KdMap a p v -> [v] Source
Returns all values in the KdMap
.
Time complexity: O(n) for n data points.
size :: KdMap a p v -> Int Source
Returns the number of point-value pairs in the KdMap
.
Time complexity: O(1)
Folds
foldrKdMap :: ((p, v) -> b -> b) -> b -> KdMap a p v -> b Source
Performs a foldr over each point-value pair in the KdMap
.
Utilities
defaultDistSqrFn :: Num a => PointAsListFn a p -> SquaredDistanceFn a p Source
A default implementation of squared distance given two points and
a PointAsListFn
.