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())
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())
25 type Region = { minX
: float; maxX
: float; minY
: float; maxY
: float } with
26 member this
.Contains px py
: bool =
27 px >= this
.minX
&& px <= this
.maxX
&&
28 py
>= this
.minY
&& py
<= this
.maxY
30 member this
.IsSub otherRegion
: bool =
31 this
.minX
>= otherRegion
.minX
&& this
.maxX
<= otherRegion
.maxX
&&
32 this
.minY
>= otherRegion
.minY
&& this
.maxY
<= otherRegion
.maxY
34 member this
.Intersects otherRegion : bool =
35 this
.minX
< otherRegion.maxX
&& this
.maxX
>= otherRegion.minX
&&
36 this
.minY
< otherRegion.maxY
&& this
.maxY
>= otherRegion.minY
38 type Tree<'a when 'a
:> I2DCoords> =
39 | Node of float * Tree<'a> * Tree<'a
>
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
48 let rec buildTreeFromSortedArray (pXSorted: 'a
[]) (pYSorted
: 'a[]) (depth: int) : Tree<'a
> =
49 if pXSorted
.Length = 1
53 if depth
% 2 = 1 // 'depth' is odd -> vertical splitting else horizontal splitting.
55 let leftX, rightX
= Array.splitAt
((pXSorted
.Length + 1) / 2) pXSorted
56 let splitElement = Array.last
leftX
57 let leftY, rightY
= Array.partition
(fun (e
: 'a) -> cmpX e splitElement <= 0) pYSorted // FIXME: Maybe this operation can be optimized.
58 Node (splitElement.X, buildTreeFromSortedArray leftX leftY (depth + 1), buildTreeFromSortedArray rightX rightY (depth + 1))
60 let downY, upY = Array.splitAt ((pYSorted.Length + 1) / 2) pYSorted
61 let splitElement = Array.last downY
62 let downX, upX = Array.partition (fun (e: 'a
) -> cmpY e
splitElement <= 0) pXSorted
// FIXME: Maybe this operation can be optimized.
63 Node (splitElement.Y, buildTreeFromSortedArray
downX downY (depth
+ 1), buildTreeFromSortedArray upX upY
(depth
+ 1))
65 buildTreeFromSortedArray
xSorted ySorted 1
67 static member search
(tree
: Tree<'a>) (searchRegion: Region) : 'a list
=
68 let rec valuesFrom
(tree
: Tree<'a>) : 'a list
=
71 | Node (_
, part1
, part2
) -> (valuesFrom part1
) @ (valuesFrom part2
)
73 let rec searchWithRegion
(tree
: Tree<'a>) (currentRegion: Region) (depth: int) : 'a list
=
75 | Leaf v -> if searchRegion
.Contains v.X v.Y then [v] else []
76 | Node (splitValue
, part1
, part2
) ->
77 let valuesInRegion (region
: Region) (treeRegion
: Tree<'a>) =
78 if region.IsSub searchRegion
81 elif region.Intersects searchRegion
83 searchWithRegion treeRegion region (depth + 1)
87 if depth % 2 = 1 // Vertical splitting.
89 let leftRegion = { currentRegion with maxX = splitValue }
90 let rightRegion = { currentRegion with minX = splitValue }
91 (valuesInRegion leftRegion part1) @ (valuesInRegion rightRegion part2)
92 else // Horizontal splitting.
93 let downRegion = { currentRegion with maxY = splitValue }
94 let upRegion = { currentRegion with minY = splitValue }
95 (valuesInRegion downRegion part1) @ (valuesInRegion upRegion part2)
97 searchWithRegion tree { minX = Double.MinValue; maxX = Double.MaxValue; minY = Double.MinValue; maxY = Double.MaxValue } 1
100 ///// Tests. TODO: to put in a unit test.
102 type Point (x: float, y: float) =
103 interface I2DCoords with
107 override this.ToString () =
108 sprintf "(%.1f, %.1f)" x y
110 // TODO: test with identical X or Y coords
121 let tree = Tree.buildTree pts
122 Utils.dprintfn "Tree: %A" tree
124 let s1 = Tree.search tree { minX = 0.0; maxX = 5.0; minY = 0.0; maxY = 5.0 } // All points.
125 Utils.dprintfn "s1: %A" s1
127 let s2 = Tree.search tree { minX = 2.8; maxX = 4.5; minY = 3.0; maxY = 4.5 }
128 Utils.dprintfn "s2: %A" s2
130 let s3 = Tree.search tree { minX = 2.0; maxX = 2.0; minY = 2.0; maxY = 2.0 }
131 Utils.dprintfn "s3: %A" s3
139 let tree = Tree.buildTree pts
140 Utils.dprintfn "Tree: %A" tree
142 let s1 = Tree.search tree { minX = 1.0; maxX = 1.0; minY = 1.0; maxY = 1.0 }
143 Utils.dprintfn "s1: %A" s1
145 let s2 = Tree.search tree { minX = 1.0; maxX = 1.0; minY = 2.0; maxY = 2.0 }
146 Utils.dprintfn "s2: %A" s2
148 // This case result is wrong: FIXME
149 let s3 = Tree.search tree { minX = 1.0; maxX = 1.0; minY = 3.0; maxY = 3.0 }
150 Utils.dprintfn "s3: %A" s3
152 let s4 = Tree.search tree { minX = 0.0; maxX = 2.0; minY = 0.0; maxY = 4.0 }
153 Utils.dprintfn "s4: %A" s4