Merge branch 'master' of gburri.org:master-thesis
[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 open UnitsOfMeasure
12
13 type Points = HashSet<Point>
14
15 type Ellipse (cx : float32, cy : float32, a : float32, b : float32, alpha : float32) =
16 member this.Cx = cx
17 member this.Cy = cy
18 member this.A = a
19 member this.B = b
20 member this.Alpha = alpha
21 member this.Area = a * b * PI
22
23 // Does the ellipse contain the point (x, y)?
24 member this.Contains x y =
25 ((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
26
27 member this.CutAVericalLine (y : float32) : bool =
28 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
29
30 member this.CutAnHorizontalLine (x : float32) : bool =
31 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
32
33 member this.IsOutside (width : float32) (height : float32) =
34 this.Cx < 0.f || this.Cx >= width ||
35 this.Cy < 0.f || this.Cy >= height ||
36 this.CutAVericalLine 0.f || this.CutAVericalLine width ||
37 this.CutAnHorizontalLine 0.f || this.CutAnHorizontalLine height
38
39 member this.Scale (factor : float32) : Ellipse =
40 Ellipse (this.Cx, this.Cy, this.A * factor, this.B * factor, alpha)
41
42 // Approximation of Ramanujan.
43 member this.Perimeter =
44 PI * (3.f * (this.A + this.B) - sqrt ((3.f * this.A + this.B) * (this.A + 3.f * this.B)))
45
46 override this.ToString () =
47 sprintf "{Ellipse: cx = %f, cy = %f, a = %f, b = %f, alpha = %f}" this.Cx this.Cy this.A this.B this.Alpha
48
49 type CellClass = HealthyRBC | InfectedRBC | Peculiar
50
51 type Cell =
52 {
53 cellClass : CellClass
54 center : Point
55 nucleusArea : int
56 parasiteArea : int
57 elements : Matrix<byte>
58 }
59
60 [<Struct>]
61 type Line (a : float32, b : float32) =
62 member this.A = a
63 member this.B = b
64
65 type MaybeBuilder () =
66 member this.Bind (x, f) =
67 match x with
68 | None -> None
69 | Some a -> f a
70
71 member this.ReturnFrom x = x
72
73 member this.TryFinally (body, compensation) =
74 try
75 this.ReturnFrom (body ())
76 finally
77 compensation ()
78
79 member this.Using (disposable : 'a when 'a :> IDisposable, body) =
80 let body' = fun () -> body disposable
81 this.TryFinally (body', fun () ->
82 match disposable with
83 | null -> ()
84 | disp -> disp.Dispose ())
85
86 member this.Zero () =
87 None
88
89 member this.Return x =
90 Some x
91
92 let maybe = MaybeBuilder ()
93
94 type Result<'a> =
95 | Success of 'a
96 | Fail of string // Error message.
97
98 type ResultBuilder () =
99 member this.Bind (res, f) =
100 match res with
101 | Success value -> f value
102 | fail -> fail
103
104 member this.ReturnFrom (x) = x
105
106 let result = ResultBuilder ()
107
108 type AnalysisResult =
109 {
110 Cells : Cell list
111 RBCSize_μm : float<μm>
112 RBCSize_px : float32
113 }