10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-11-19 04:22:21 +01:00
QCaml/Utils/Coordinate.ml

56 lines
907 B
OCaml
Raw Normal View History

2018-01-17 18:19:38 +01:00
type t = float array
let zero =
[| 0. ; 0. ; 0. |]
let of_float_triplet (x,y,z) =
[|x;y;z|]
let of_3_floats x y z =
[|x;y;z|]
let to_string x =
(string_of_float x.(0))^" "^(string_of_float x.(1))^" "^(string_of_float x.(2))
let x a = a.(0)
let y a = a.(1)
let z a = a.(2)
let coord a = function
| 0 -> a.(0)
| 1 -> a.(1)
| 2 -> a.(2)
| _ -> raise (Invalid_argument "Coordinate")
let to_float_array a = a
(** Linear algebra *)
let (|-) a b =
match a,b with
| [|x;y;z|], [|x';y';z'|] -> [| x-.x'; y-.y'; z-.z' |]
| _ -> assert false
let (|+) a b =
match a,b with
| [|x;y;z|], [|x';y';z'|] -> [| x+.x'; y+.y'; z+.z' |]
| _ -> assert false
let (|.) s a =
match a with
| [|x;y;z|] -> [| s*.x; s*.y; s*.z |]
| _ -> assert false
let dot a b =
match a,b with
| [|x;y;z|], [|x';y';z'|] -> x*.x' +. y*.y' +. z*.z'
| _ -> assert false
let norm u =
sqrt @@ dot u u