9
1
mirror of https://github.com/QuantumPackage/qp2.git synced 2024-10-04 15:15:58 +02:00
qp2/ocaml/Point3d.ml

55 lines
1.2 KiB
OCaml
Raw Normal View History

2019-03-13 11:35:21 +01:00
open Qptypes
2020-05-25 11:31:28 +02:00
open Qputils
2019-03-13 11:35:21 +01:00
open Sexplib.Std
2019-01-25 11:39:31 +01:00
type t = {
x : float ;
y : float ;
z : float ;
} [@@deriving sexp]
let of_tuple ~units (x,y,z) =
let f = match units with
| Units.Bohr -> 1.
| Units.Angstrom -> Units.angstrom_to_bohr
in
{ x = x *. f ; y = y *. f ; z = z *. f }
(** Read x y z coordinates in string s with units u *)
let of_string ~units s =
let f = match units with
| Units.Bohr -> 1.
| Units.Angstrom -> Units.angstrom_to_bohr
in
let l = s
2019-03-13 11:35:21 +01:00
|> String_ext.split ~on:' '
|> List.filter (fun x -> x <> "")
2020-05-25 11:31:28 +02:00
|> list_map float_of_string
2019-03-13 11:35:21 +01:00
|> Array.of_list
2019-01-25 11:39:31 +01:00
in
{ x = l.(0) *. f ;
y = l.(1) *. f ;
z = l.(2) *. f }
let distance2 p1 p2 =
let { x=x1 ; y=y1 ; z=z1 } = p1
and { x=x2 ; y=y2 ; z=z2 } = p2 in
(x2-.x1)*.(x2-.x1) +. (y2-.y1)*.(y2-.y1) +. (z2-.z1)*.(z2-.z1)
|> Positive_float.of_float
let distance p1 p2 =
sqrt (Positive_float.to_float (distance2 p1 p2))
let to_string ~units p =
let f = match units with
| Units.Bohr -> 1.
| Units.Angstrom -> Units.bohr_to_angstrom
in
let { x=x ; y=y ; z=z } = p in
Printf.sprintf "%16.8f %16.8f %16.8f" (x*.f) (y*.f) (z*.f)