4e20f242497135266d6fedac3b6d47f18122ceaf
[master-thesis.git] / Parasitemia / ParasitemiaCore / KdTree.fs
1 module ParasitemiaCore.KdTree
2
3 open System
4
5 type I2DCoords =
6 abstract X : float32
7 abstract Y : float32
8
9 // Compare 'e1' and 'e2' by X.
10 let cmpX (e1 : I2DCoords) (e2 : I2DCoords) : int =
11 match e1.X.CompareTo(e2.X) with
12 | 0 -> match e1.Y.CompareTo(e2.Y) with
13 | 0 -> e1.GetHashCode().CompareTo(e2.GetHashCode())
14 | v -> v
15 | v -> v
16
17 // Compare 'e1' and 'e2' by Y.
18 let cmpY (e1 : I2DCoords) (e2 : I2DCoords) : int =
19 match e1.Y.CompareTo(e2.Y) with
20 | 0 -> match e1.X.CompareTo(e2.X) with
21 | 0 -> e1.GetHashCode().CompareTo(e2.GetHashCode())
22 | v -> v
23 | v -> v
24
25 type Region = { minX : float32; maxX : float32; minY : float32; maxY : float32 } with
26 member this.Contains px py : bool =
27 px >= this.minX && px <= this.maxX &&
28 py >= this.minY && py <= this.maxY
29
30 member this.IsSub otherRegion : bool =
31 this.minX >= otherRegion.minX && this.maxX <= otherRegion.maxX &&
32 this.minY >= otherRegion.minY && this.maxY <= otherRegion.maxY
33
34 member this.Intersects otherRegion : bool =
35 this.minX < otherRegion.maxX && this.maxX >= otherRegion.minX &&
36 this.minY < otherRegion.maxY && this.maxY >= otherRegion.minY
37
38 type Tree<'a when 'a :> I2DCoords> =
39 | Node of float32 * Tree<'a> * Tree<'a>
40 | Leaf of 'a
41
42 static member BuildTree (l : 'a list) : Tree<'a> =
43 let xSorted = List.toArray l
44 let ySorted = List.toArray l
45 Array.sortInPlaceWith cmpX xSorted
46 Array.sortInPlaceWith cmpY ySorted
47
48 let rec buildTreeFromSortedArray (pXSorted : 'a[]) (pYSorted : 'a[]) (depth : int) : Tree<'a> =
49 if pXSorted.Length = 1 then
50 Leaf pXSorted.[0]
51 else
52 if depth % 2 = 1 then // 'depth' is odd -> vertical splitting else horizontal splitting.
53 let leftX, rightX = Array.splitAt ((pXSorted.Length + 1) / 2) pXSorted
54 let splitElement = Array.last leftX
55 let leftY, rightY = Array.partition (fun (e : 'a) -> cmpX e splitElement <= 0) pYSorted // FIXME: Maybe this operation can be optimized.
56 Node (splitElement.X, buildTreeFromSortedArray leftX leftY (depth + 1), buildTreeFromSortedArray rightX rightY (depth + 1))
57 else
58 let downY, upY = Array.splitAt ((pYSorted.Length + 1) / 2) pYSorted
59 let splitElement = Array.last downY
60 let downX, upX = Array.partition (fun (e : 'a) -> cmpY e splitElement <= 0) pXSorted // FIXME: Maybe this operation can be optimized.
61 Node (splitElement.Y, buildTreeFromSortedArray downX downY (depth + 1), buildTreeFromSortedArray upX upY (depth + 1))
62
63 buildTreeFromSortedArray xSorted ySorted 1
64
65 member this.Search (searchRegion : Region) : 'a list =
66 let rec valuesFrom (tree : Tree<'a>) (acc : 'a list) : 'a list =
67 match tree with
68 | Node (_, left, right) -> (valuesFrom right (valuesFrom left acc))
69 | Leaf v -> v :: acc
70
71 let rec searchWithRegion (tree : Tree<'a>) (currentRegion : Region) (depth : int) : 'a list =
72 match tree with
73 | Leaf v -> if searchRegion.Contains v.X v.Y then [v] else []
74 | Node (splitValue, part1, part2) ->
75 let valuesInRegion (region : Region) (treeRegion : Tree<'a>) =
76 if region.IsSub searchRegion then
77 valuesFrom treeRegion []
78 elif region.Intersects searchRegion then
79 searchWithRegion treeRegion region (depth + 1)
80 else
81 []
82
83 if depth % 2 = 1 then // Vertical splitting.
84 let leftRegion = { currentRegion with maxX = splitValue }
85 let rightRegion = { currentRegion with minX = splitValue }
86 (valuesInRegion leftRegion part1) @ (valuesInRegion rightRegion part2)
87 else // Horizontal splitting.
88 let downRegion = { currentRegion with maxY = splitValue }
89 let upRegion = { currentRegion with minY = splitValue }
90 (valuesInRegion downRegion part1) @ (valuesInRegion upRegion part2)
91
92 searchWithRegion this { minX = Single.MinValue; maxX = Single.MaxValue; minY = Single.MinValue; maxY = Single.MaxValue } 1
93
94 ///// Tests. TODO: to put in a unit test.
95
96 type Point (x : float32, y : float32) =
97 interface I2DCoords with
98 member this.X = x
99 member this.Y = y
100
101 override this.ToString () =
102 sprintf "(%.1f, %.1f)" x y
103
104 // TODO: test with identical X or Y coords
105 let test () =
106 let pts =
107 [
108 Point(1.0f, 1.0f)
109 Point(2.0f, 2.0f)
110 Point(1.5f, 3.6f)
111 Point(3.0f, 3.2f)
112 Point(4.0f, 4.0f)
113 Point(3.5f, 1.5f)
114 Point(2.5f, 0.5f)
115 ]
116
117 let tree = Tree.BuildTree pts
118 Utils.dprintfn "Tree: %A" tree
119
120 let s1 = tree.Search { minX = 0.0f; maxX = 5.0f; minY = 0.0f; maxY = 5.0f } // All points.
121 Utils.dprintfn "s1: %A" s1
122
123 let s2 = tree.Search { minX = 2.8f; maxX = 4.5f; minY = 3.0f; maxY = 4.5f }
124 Utils.dprintfn "s2: %A" s2
125
126 let s3 = tree.Search { minX = 2.0f; maxX = 2.0f; minY = 2.0f; maxY = 2.0f }
127 Utils.dprintfn "s3: %A" s3
128
129 let test2 () =
130 let pts =
131 [
132 Point(1.0f, 1.0f)
133 Point(1.0f, 2.0f)
134 Point(1.0f, 3.0f)
135 ]
136
137 let tree = Tree.BuildTree pts
138 Utils.dprintfn "Tree: %A" tree
139
140 let s1 = tree.Search { minX = 1.0f; maxX = 1.0f; minY = 1.0f; maxY = 1.0f }
141 Utils.dprintfn "s1: %A" s1
142
143 let s2 = tree.Search { minX = 1.0f; maxX = 1.0f; minY = 2.0f; maxY = 2.0f }
144 Utils.dprintfn "s2: %A" s2
145
146 // This case result is wrong: FIXME
147 let s3 = tree.Search { minX = 1.0f; maxX = 1.0f; minY = 3.0f; maxY = 3.0f }
148 Utils.dprintfn "s3: %A" s3
149
150 let s4 = tree.Search { minX = 0.0f; maxX = 2.0f; minY = 0.0f; maxY = 4.0f }
151 Utils.dprintfn "s4: %A" s4
152