638f9f539378e1254a580b4f5f3a6d4cd992bb8f
[master-thesis.git] / Parasitemia / Parasitemia / Utils.fs
1 module Utils
2
3 open System.Diagnostics
4
5 open Types
6
7 let inline roundInt v = v |> round |> int
8
9 let inline dprintfn fmt =
10 Printf.ksprintf System.Diagnostics.Debug.WriteLine fmt
11
12 let mutable log : (string -> unit) =
13 fun m -> ()
14
15 let logTime (m: string) (f: unit -> 'a) : 'a =
16 let sw = Stopwatch()
17 sw.Start()
18 let res = f ()
19 sw.Stop()
20 log <| sprintf "%s (time: %d ms)" m sw.ElapsedMilliseconds
21 res
22
23 let inline lineFromTwoPoints (p1: PointD) (p2: PointD) : Line =
24 let a = (p1.Y - p2.Y) / (p1.X - p2.X)
25 let b = -(p2.X * p1.Y - p1.X * p2.Y) / (p1.X - p2.X)
26 Line(a, b)
27
28 let inline pointFromTwoLines (l1: Line) (l2: Line) : PointD =
29 let x = -(l1.B - l2.B) / (l1.A - l2.A)
30 let y = -(l2.A * l1.B - l1.A * l2.B) / (l1.A - l2.A)
31 PointD(x, y)
32
33 let inline linePassThroughSegment (l: Line) (p1: PointD) (p2: PointD) : bool =
34 let p = pointFromTwoLines l (lineFromTwoPoints p1 p2)
35 sign (p.X - p1.X) <> sign (p.X - p2.X)
36
37 let inline squaredDistanceTwoPoints (p1: PointD) (p2: PointD) =
38 (p1.X - p2.X) ** 2.f + (p1.Y - p2.Y) ** 2.f
39
40 let inline distanceTwoPoints (p1: PointD) (p2: PointD) =
41 squaredDistanceTwoPoints p1 p2 |> sqrt
42
43 let countCells (cells: Cell list) : int * int =
44 cells |> List.fold (fun (total, infected) { cellClass = cellClass } ->
45 match cellClass with
46 | HealthyRBC -> (total + 1, infected)
47 | InfectedRBC -> (total + 1, infected + 1)
48 | Peculiar -> (total, infected)) (0, 0)