f696c2713e8af31693c6730f070d24fbb6c4d65a
[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 $"{{{nameof Ellipse}: {nameof this.Cx} = %f{this.Cx}, {nameof this.Cy} = %f{this.Cy}, {nameof this.A} = %f{this.A}, {nameof this.B} = %f{this.B}, {nameof this.Alpha} = %f{this.Alpha}}}"
48 override this.Equals (other : obj) =
49 match other with
50 | :? Ellipse as otherEllipse ->
51 otherEllipse.Cx = this.Cx &&
52 otherEllipse.Cy = this.Cy &&
53 otherEllipse.A = this.A &&
54 otherEllipse.B = this.B &&
55 otherEllipse.Alpha = this.Alpha
56 | _ -> false
57
58 override this.GetHashCode () = HashCode.Combine (this.Cx, this.Cy, this.A, this.B, this.Alpha)
59
60 [<Struct>]
61 type CellClass = HealthyRBC | InfectedRBC | Peculiar
62
63 type Cell =
64 {
65 cellClass : CellClass
66 center : Point
67 nucleusArea : int
68 parasiteArea : int
69 elements : Matrix<byte>
70 }
71
72 [<Struct>]
73 type Line (a : float32, b : float32) =
74 member this.A = a
75 member this.B = b
76
77 type MaybeBuilder () =
78 member this.Bind (x, f) =
79 match x with
80 | None -> None
81 | Some a -> f a
82
83 member this.ReturnFrom x = x
84
85 member this.TryFinally (body, compensation) =
86 try
87 this.ReturnFrom (body ())
88 finally
89 compensation ()
90
91 member this.Using (disposable : 'a when 'a :> IDisposable, body) =
92 let body' = fun () -> body disposable
93 this.TryFinally (body', fun () ->
94 match disposable with
95 | null -> ()
96 | disp -> disp.Dispose ())
97
98 member this.Zero () =
99 None
100
101 member this.Return x =
102 Some x
103
104 let maybe = MaybeBuilder ()
105
106 type Result<'a> =
107 | Success of 'a
108 | Fail of string // Error message.
109
110 type ResultBuilder () =
111 member this.Bind (res, f) =
112 match res with
113 | Success value -> f value
114 | fail -> fail
115
116 member this.ReturnFrom (x) = x
117
118 let result = ResultBuilder ()
119
120 type AnalysisResult =
121 {
122 Cells : Cell list
123 RBCSize_μm : float<μm>
124 RBCSize_px : float32
125 }