Define Emgu as a nuget reference for the project WPF
[master-thesis.git] / Parasitemia / ParasitemiaCore / Types.fs
1 module ParasitemiaCore.Types
2
3 open System
4 open System.Drawing
5 open System.Collections.Generic
6
7 open Emgu.CV
8 open Emgu.CV.Structure
9
10 open Const
11
12 type Points = HashSet<Point>
13
14 type Ellipse (cx : float32, cy : float32, a : float32, b : float32, alpha : float32) =
15 member this.Cx = cx
16 member this.Cy = cy
17 member this.A = a
18 member this.B = b
19 member this.Alpha = alpha
20 member this.Area = a * b * PI
21
22 // Does the ellipse contain the point (x, y)?
23 member this.Contains x y =
24 ((x - cx) * cos alpha + (y - cy) * sin alpha) ** 2.f / a ** 2.f + ((x - cx) * sin alpha - (y - cy) * cos alpha) ** 2.f / b ** 2.f <= 1.f
25
26 member this.CutAVericalLine (y : float32) : bool =
27 a ** 2.f + b ** 2.f - 2.f * y ** 2.f + 4.f * y * cx - 2.f * cx ** 2.f + a ** 2.f * cos (2.f * alpha) - b ** 2.f * cos (2.f * alpha) > 0.f
28
29 member this.CutAnHorizontalLine (x : float32) : bool =
30 a ** 2.f + b ** 2.f - 2.f * x ** 2.f + 4.f * x * cy - 2.f * cy ** 2.f - a ** 2.f * cos (2.f * alpha) + b ** 2.f * cos (2.f * alpha) > 0.f
31
32 member this.IsOutside (width : float32) (height : float32) =
33 this.Cx < 0.f || this.Cx >= width ||
34 this.Cy < 0.f || this.Cy >= height ||
35 this.CutAVericalLine 0.f || this.CutAVericalLine width ||
36 this.CutAnHorizontalLine 0.f || this.CutAnHorizontalLine height
37
38 member this.Scale (factor : float32) : Ellipse =
39 Ellipse(this.Cx, this.Cy, this.A * factor, this.B * factor, alpha)
40
41 // Approximation of Ramanujan.
42 member this.Perimeter =
43 PI * (3.f * (this.A + this.B) - sqrt ((3.f * this.A + this.B) * (this.A + 3.f * this.B)))
44
45 override this.ToString () =
46 sprintf "(cx: %f, cy: %f, a: %f, b: %f, alpha: %f)" this.Cx this.Cy this.A this.B this.Alpha
47
48 type CellClass = HealthyRBC | InfectedRBC | Peculiar
49
50 type Cell =
51 {
52 cellClass : CellClass
53 center : Point
54 nucleusArea : int
55 parasiteArea : int
56 elements : Matrix<byte>
57 }
58
59 [<Struct>]
60 type Line (a : float32, b : float32) =
61 member this.A = a
62 member this.B = b
63
64 type MaybeBuilder () =
65 member this.Bind (x, f) =
66 match x with
67 | None -> None
68 | Some a -> f a
69
70 member this.ReturnFrom x = x
71
72 member this.TryFinally (body, compensation) =
73 try
74 this.ReturnFrom(body())
75 finally
76 compensation()
77
78 member this.Using (disposable : 'a when 'a :> IDisposable, body) =
79 let body' = fun () -> body disposable
80 this.TryFinally(body', fun () ->
81 match disposable with
82 | null -> ()
83 | disp -> disp.Dispose())
84
85 member this.Zero () =
86 None
87
88 member this.Return x =
89 Some x
90
91 let maybe = MaybeBuilder()
92
93 type Result<'a> =
94 | Success of 'a
95 | Fail of string // Error message.
96
97 type ResultBuilder () =
98 member this.Bind (res, f) =
99 match res with
100 | Success value -> f value
101 | fail -> fail
102
103 member this.ReturnFrom (x) = x
104
105 let result = ResultBuilder()