module ParasitemiaCore.Types open System open System.Drawing open System.Collections.Generic open Emgu.CV open Emgu.CV.Structure open Const open UnitsOfMeasure type Points = HashSet type Ellipse (cx : float32, cy : float32, a : float32, b : float32, alpha : float32) = member this.Cx = cx member this.Cy = cy member this.A = a member this.B = b member this.Alpha = alpha member this.Area = a * b * PI // Does the ellipse contain the point (x, y)? member this.Contains x y = ((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 member this.CutAVericalLine (y : float32) : bool = 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 member this.CutAnHorizontalLine (x : float32) : bool = 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 member this.IsOutside (width : float32) (height : float32) = this.Cx < 0.f || this.Cx >= width || this.Cy < 0.f || this.Cy >= height || this.CutAVericalLine 0.f || this.CutAVericalLine width || this.CutAnHorizontalLine 0.f || this.CutAnHorizontalLine height member this.Scale (factor : float32) : Ellipse = Ellipse (this.Cx, this.Cy, this.A * factor, this.B * factor, alpha) // Approximation of Ramanujan. member this.Perimeter = PI * (3.f * (this.A + this.B) - sqrt ((3.f * this.A + this.B) * (this.A + 3.f * this.B))) override this.ToString () = $"{{{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}}}" override this.Equals (other : obj) = match other with | :? Ellipse as otherEllipse -> otherEllipse.Cx = this.Cx && otherEllipse.Cy = this.Cy && otherEllipse.A = this.A && otherEllipse.B = this.B && otherEllipse.Alpha = this.Alpha | _ -> false override this.GetHashCode () = HashCode.Combine (this.Cx, this.Cy, this.A, this.B, this.Alpha) [] type CellClass = HealthyRBC | InfectedRBC | Peculiar type Cell = { cellClass : CellClass center : Point nucleusArea : int parasiteArea : int elements : Matrix } [] type Line (a : float32, b : float32) = member this.A = a member this.B = b type MaybeBuilder () = member this.Bind (x, f) = match x with | None -> None | Some a -> f a member this.ReturnFrom x = x member this.TryFinally (body, compensation) = try this.ReturnFrom (body ()) finally compensation () member this.Using (disposable : 'a when 'a :> IDisposable, body) = let body' = fun () -> body disposable this.TryFinally (body', fun () -> match disposable with | null -> () | disp -> disp.Dispose ()) member this.Zero () = None member this.Return x = Some x let maybe = MaybeBuilder () type Result<'a> = | Success of 'a | Fail of string // Error message. type ResultBuilder () = member this.Bind (res, f) = match res with | Success value -> f value | fail -> fail member this.ReturnFrom (x) = x let result = ResultBuilder () type AnalysisResult = { Cells : Cell list RBCSize_μm : float<μm> RBCSize_px : float32 }