10
0
mirror of https://github.com/LCPQ/quantum_package synced 2024-11-14 01:53:55 +01:00
quantum_package/ocaml/Point3d.ml

45 lines
899 B
OCaml
Raw Normal View History

2014-08-23 17:07:47 +02:00
open Core.Std;;
type t = {
x : float ;
y : float ;
z : float ;
2014-10-25 21:24:21 +02:00
} with sexp
2014-08-23 17:07:47 +02:00
2014-09-18 17:01:43 +02:00
(** Read x y z coordinates in string s with units u *)
let of_string u s =
2014-10-07 19:33:11 +02:00
let f = match u with
| Units.Bohr -> 1.
| Units.Angstrom -> Units.angstrom_to_bohr
2014-09-18 17:01:43 +02:00
in
2014-08-23 17:07:47 +02:00
let l = s
|> String.split ~on:' '
|> List.filter ~f:(fun x -> x <> "")
|> List.map ~f:Float.of_string
|> Array.of_list
in
2014-09-18 17:01:43 +02:00
{ x = l.(0) *. f ;
y = l.(1) *. f ;
z = l.(2) *. f }
2014-08-23 17:07:47 +02:00
;;
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)
;;
let distance p1 p2 = sqrt (distance2 p1 p2)
;;
2014-10-07 19:33:11 +02:00
let to_string u p =
let f = match u with
| Units.Bohr -> 1.
| Units.Angstrom -> Units.bohr_to_angstrom
in
2014-08-23 17:07:47 +02:00
let { x=x ; y=y ; z=z } = p in
2014-10-07 19:33:11 +02:00
Printf.sprintf "%f %f %f" (x*.f) (y*.f) (z*.f)
2014-08-23 17:07:47 +02:00
;;