module Data.KdTree.Dynamic
(
PointAsListFn
, SquaredDistanceFn
, KdTree
, empty
, singleton
, emptyWithDist
, singletonWithDist
, insert
, nearest
, inRadius
, kNearest
, inRange
, toList
, null
, size
, defaultSqrDist
) where
import Prelude hiding (null)
import qualified Data.Foldable as F
import qualified Data.KdMap.Dynamic as DKDM
import Data.KdMap.Dynamic (PointAsListFn, SquaredDistanceFn, defaultSqrDist)
newtype KdTree a p = KdTree (DKDM.KdMap a p ())
instance F.Foldable (KdTree a) where
foldr :: (a -> b -> b) -> b -> KdTree a a -> b
foldr a -> b -> b
f b
z (KdTree KdMap a a ()
dkdMap) = ((a, ()) -> b -> b) -> b -> KdMap a a () -> b
forall p v b a. ((p, v) -> b -> b) -> b -> KdMap a p v -> b
DKDM.foldrWithKey (a -> b -> b
f (a -> b -> b) -> ((a, ()) -> a) -> (a, ()) -> b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, ()) -> a
forall a b. (a, b) -> a
fst) b
z KdMap a a ()
dkdMap
instance (Show a, Show p) => Show (KdTree a p) where
show :: KdTree a p -> String
show (KdTree KdMap a p ()
kdm) = String
"KdTree " String -> ShowS
forall a. [a] -> [a] -> [a]
++ KdMap a p () -> String
forall a. Show a => a -> String
show KdMap a p ()
kdm
emptyWithDist :: Real a => PointAsListFn a p -> SquaredDistanceFn a p -> KdTree a p
emptyWithDist :: PointAsListFn a p -> SquaredDistanceFn a p -> KdTree a p
emptyWithDist PointAsListFn a p
p2l SquaredDistanceFn a p
d2 = KdMap a p () -> KdTree a p
forall a p. KdMap a p () -> KdTree a p
KdTree (KdMap a p () -> KdTree a p) -> KdMap a p () -> KdTree a p
forall a b. (a -> b) -> a -> b
$ PointAsListFn a p -> SquaredDistanceFn a p -> KdMap a p ()
forall a p v.
PointAsListFn a p -> SquaredDistanceFn a p -> KdMap a p v
DKDM.emptyWithDist PointAsListFn a p
p2l SquaredDistanceFn a p
d2
empty :: Real a => PointAsListFn a p -> KdTree a p
empty :: PointAsListFn a p -> KdTree a p
empty PointAsListFn a p
p2l = PointAsListFn a p -> SquaredDistanceFn a p -> KdTree a p
forall a p.
Real a =>
PointAsListFn a p -> SquaredDistanceFn a p -> KdTree a p
emptyWithDist PointAsListFn a p
p2l (SquaredDistanceFn a p -> KdTree a p)
-> SquaredDistanceFn a p -> KdTree a p
forall a b. (a -> b) -> a -> b
$ PointAsListFn a p -> SquaredDistanceFn a p
forall a p. Num a => PointAsListFn a p -> SquaredDistanceFn a p
defaultSqrDist PointAsListFn a p
p2l
null :: KdTree a p -> Bool
null :: KdTree a p -> Bool
null (KdTree KdMap a p ()
dkdMap) = KdMap a p () -> Bool
forall a p v. KdMap a p v -> Bool
DKDM.null KdMap a p ()
dkdMap
singletonWithDist :: Real a => PointAsListFn a p
-> SquaredDistanceFn a p
-> p
-> KdTree a p
singletonWithDist :: PointAsListFn a p -> SquaredDistanceFn a p -> p -> KdTree a p
singletonWithDist PointAsListFn a p
p2l SquaredDistanceFn a p
d2 p
p = KdMap a p () -> KdTree a p
forall a p. KdMap a p () -> KdTree a p
KdTree (KdMap a p () -> KdTree a p) -> KdMap a p () -> KdTree a p
forall a b. (a -> b) -> a -> b
$ PointAsListFn a p
-> SquaredDistanceFn a p -> (p, ()) -> KdMap a p ()
forall a p v.
Real a =>
PointAsListFn a p -> SquaredDistanceFn a p -> (p, v) -> KdMap a p v
DKDM.singletonWithDist PointAsListFn a p
p2l SquaredDistanceFn a p
d2 (p
p, ())
singleton :: Real a => PointAsListFn a p -> p -> KdTree a p
singleton :: PointAsListFn a p -> p -> KdTree a p
singleton PointAsListFn a p
p2l = PointAsListFn a p -> SquaredDistanceFn a p -> p -> KdTree a p
forall a p.
Real a =>
PointAsListFn a p -> SquaredDistanceFn a p -> p -> KdTree a p
singletonWithDist PointAsListFn a p
p2l (SquaredDistanceFn a p -> p -> KdTree a p)
-> SquaredDistanceFn a p -> p -> KdTree a p
forall a b. (a -> b) -> a -> b
$ PointAsListFn a p -> SquaredDistanceFn a p
forall a p. Num a => PointAsListFn a p -> SquaredDistanceFn a p
defaultSqrDist PointAsListFn a p
p2l
insert :: Real a => KdTree a p -> p -> KdTree a p
insert :: KdTree a p -> p -> KdTree a p
insert (KdTree KdMap a p ()
dkdMap) p
p = KdMap a p () -> KdTree a p
forall a p. KdMap a p () -> KdTree a p
KdTree (KdMap a p () -> KdTree a p) -> KdMap a p () -> KdTree a p
forall a b. (a -> b) -> a -> b
$ KdMap a p () -> p -> () -> KdMap a p ()
forall a p v. Real a => KdMap a p v -> p -> v -> KdMap a p v
DKDM.insert KdMap a p ()
dkdMap p
p ()
nearest :: Real a => KdTree a p -> p -> p
nearest :: KdTree a p -> p -> p
nearest (KdTree KdMap a p ()
dkdMap) = (p, ()) -> p
forall a b. (a, b) -> a
fst ((p, ()) -> p) -> (p -> (p, ())) -> p -> p
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KdMap a p () -> p -> (p, ())
forall a p v. Real a => KdMap a p v -> p -> (p, v)
DKDM.nearest KdMap a p ()
dkdMap
kNearest :: Real a => KdTree a p -> Int -> p -> [p]
kNearest :: KdTree a p -> Int -> p -> [p]
kNearest (KdTree KdMap a p ()
dkdMap) Int
k p
query =
((p, ()) -> p) -> [(p, ())] -> [p]
forall a b. (a -> b) -> [a] -> [b]
map (p, ()) -> p
forall a b. (a, b) -> a
fst ([(p, ())] -> [p]) -> [(p, ())] -> [p]
forall a b. (a -> b) -> a -> b
$ KdMap a p () -> Int -> p -> [(p, ())]
forall a p v. Real a => KdMap a p v -> Int -> p -> [(p, v)]
DKDM.kNearest KdMap a p ()
dkdMap Int
k p
query
inRadius :: Real a => KdTree a p -> a -> p -> [p]
inRadius :: KdTree a p -> a -> p -> [p]
inRadius (KdTree KdMap a p ()
dkdMap) a
radius p
query =
((p, ()) -> p) -> [(p, ())] -> [p]
forall a b. (a -> b) -> [a] -> [b]
map (p, ()) -> p
forall a b. (a, b) -> a
fst ([(p, ())] -> [p]) -> [(p, ())] -> [p]
forall a b. (a -> b) -> a -> b
$ KdMap a p () -> a -> p -> [(p, ())]
forall a p v. Real a => KdMap a p v -> a -> p -> [(p, v)]
DKDM.inRadius KdMap a p ()
dkdMap a
radius p
query
inRange :: Real a => KdTree a p
-> p
-> p
-> [p]
inRange :: KdTree a p -> p -> p -> [p]
inRange (KdTree KdMap a p ()
dkdMap) p
lowers p
uppers =
((p, ()) -> p) -> [(p, ())] -> [p]
forall a b. (a -> b) -> [a] -> [b]
map (p, ()) -> p
forall a b. (a, b) -> a
fst ([(p, ())] -> [p]) -> [(p, ())] -> [p]
forall a b. (a -> b) -> a -> b
$ KdMap a p () -> p -> p -> [(p, ())]
forall a p v. Real a => KdMap a p v -> p -> p -> [(p, v)]
DKDM.inRange KdMap a p ()
dkdMap p
lowers p
uppers
size :: KdTree a p -> Int
size :: KdTree a p -> Int
size (KdTree KdMap a p ()
dkdMap) = KdMap a p () -> Int
forall a p v. KdMap a p v -> Int
DKDM.size KdMap a p ()
dkdMap
toList :: KdTree a p -> [p]
toList :: KdTree a p -> [p]
toList (KdTree KdMap a p ()
dkdMap) = KdMap a p () -> [p]
forall a p v. KdMap a p v -> [p]
DKDM.keys KdMap a p ()
dkdMap