summaryrefslogtreecommitdiff
path: root/bindgen/ocaml/lib
diff options
context:
space:
mode:
authoromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-07-28 00:10:00 +1000
committeromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-07-28 00:10:00 +1000
commit7b86e3251a28406862fe16d49f8533beb8ca3150 (patch)
treea90166418ada7ad88500843ce8881a453c1d4721 /bindgen/ocaml/lib
parent6b004c5ac6a25f1020774276803b62e8619ea61e (diff)
start on ocaml bindings
Diffstat (limited to 'bindgen/ocaml/lib')
-rw-r--r--bindgen/ocaml/lib/dune2
-rw-r--r--bindgen/ocaml/lib/lib.ml0
-rw-r--r--bindgen/ocaml/lib/maths.ml42
3 files changed, 44 insertions, 0 deletions
diff --git a/bindgen/ocaml/lib/dune b/bindgen/ocaml/lib/dune
new file mode 100644
index 0000000..6803cec
--- /dev/null
+++ b/bindgen/ocaml/lib/dune
@@ -0,0 +1,2 @@
+(library
+ (name celeritas))
diff --git a/bindgen/ocaml/lib/lib.ml b/bindgen/ocaml/lib/lib.ml
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/bindgen/ocaml/lib/lib.ml
diff --git a/bindgen/ocaml/lib/maths.ml b/bindgen/ocaml/lib/maths.ml
new file mode 100644
index 0000000..8810f48
--- /dev/null
+++ b/bindgen/ocaml/lib/maths.ml
@@ -0,0 +1,42 @@
+(** Maths and linear algebra.
+ Ideally we should avoid doing large amounts of math computation in OCaml,
+ and so exposing more functionality for batch processing via Core is preferred *)
+
+(** Base functions that a 2D Vector must support *)
+module type Vector = sig
+ type t
+
+ val add : t -> t -> t
+ val sub : t -> t -> t
+ val scalar_mul : float -> t -> t
+ val dot : t -> t -> float
+end
+
+(** Functor that takes a module implementing the Vector signature and returns
+ a module that implements most vector space operations *)
+module VectorSpace (V : Vector) = struct
+ include V
+
+ let scalar_div s v = scalar_mul (1. /. s) v
+ let neg v = scalar_mul (-1.0) v
+ let length_squared v = dot v v
+ let length v = sqrt (dot v v)
+ let magnitude = length
+
+ let normalize v =
+ let len = length v in
+ if len > 0.0 then scalar_mul (1.0 /. len) v else v
+
+ let distance u v = sub u v |> length
+end
+
+module Vec3Float = struct
+ type t = { x : float; y : float; z : float }
+
+ let add u v = { x = u.x +. v.x; y = u.y +. v.y; z = u.z +. v.z }
+ let sub u v = { x = u.x -. v.x; y = u.y -. v.y; z = u.z -. v.z }
+ let scalar_mul k v = { x = k *. v.x; y = k *. v.y; z = k *. v.z }
+ let dot u v = (u.x *. v.x) +. (u.y *. v.y) +. (u.z *. v.z)
+end
+
+module Vec3 = VectorSpace (Vec3Float) \ No newline at end of file