···11-(*---------------------------------------------------------------------------
22- Copyright (c) 2025 Thomas Gazagnaire. All rights reserved.
33- SPDX-License-Identifier: ISC
44- ---------------------------------------------------------------------------*)
55-66-(** 3D vector operations. *)
77-88-type t = { x : float; y : float; z : float }
99-1010-let v x y z = { x; y; z }
1111-let zero = { x = 0.; y = 0.; z = 0. }
1212-let add a b = { x = a.x +. b.x; y = a.y +. b.y; z = a.z +. b.z }
1313-let scale s v = { x = s *. v.x; y = s *. v.y; z = s *. v.z }
1414-let sub a b = { x = a.x -. b.x; y = a.y -. b.y; z = a.z -. b.z }
1515-let dot a b = (a.x *. b.x) +. (a.y *. b.y) +. (a.z *. b.z)
1616-1717-let cross a b =
1818- {
1919- x = (a.y *. b.z) -. (a.z *. b.y);
2020- y = (a.z *. b.x) -. (a.x *. b.z);
2121- z = (a.x *. b.y) -. (a.y *. b.x);
2222- }
2323-2424-let length v = sqrt ((v.x *. v.x) +. (v.y *. v.y) +. (v.z *. v.z))
2525-2626-let normalize v =
2727- let n = length v in
2828- if n < 1e-15 then v else scale (1.0 /. n) v
2929-3030-let negate v = { x = -.v.x; y = -.v.y; z = -.v.z }
3131-3232-let equal ?(eps = 1e-12) a b =
3333- Float.abs (a.x -. b.x) <= eps
3434- && Float.abs (a.y -. b.y) <= eps
3535- && Float.abs (a.z -. b.z) <= eps
3636-3737-let pp ppf v = Fmt.pf ppf "(%g, %g, %g)" v.x v.y v.z
-44
lib/vec3.mli
···11-(** 3D vector operations.
22-33- Provides basic arithmetic, products, and comparison for three-component
44- floating-point vectors used throughout the orbital mechanics libraries. *)
55-66-type t = { x : float; y : float; z : float }
77-88-val v : float -> float -> float -> t
99-(** [v x y z] is the vector [(x, y, z)]. *)
1010-1111-val zero : t
1212-(** [zero] is the origin. *)
1313-1414-val add : t -> t -> t
1515-(** [add a b] is the component-wise sum. *)
1616-1717-val scale : float -> t -> t
1818-(** [scale s v] multiplies each component by [s]. *)
1919-2020-val sub : t -> t -> t
2121-(** [sub a b] is the component-wise difference. *)
2222-2323-val dot : t -> t -> float
2424-(** [dot a b] is the dot product. *)
2525-2626-val cross : t -> t -> t
2727-(** [cross a b] is the cross product. *)
2828-2929-val length : t -> float
3030-(** [length v] is the Euclidean length. *)
3131-3232-val normalize : t -> t
3333-(** [normalize v] returns the unit vector in the direction of [v]. Returns [v]
3434- unchanged if its length is near zero. *)
3535-3636-val negate : t -> t
3737-(** [negate v] is the component-wise negation of [v]. *)
3838-3939-val equal : ?eps:float -> t -> t -> bool
4040-(** [equal ?eps a b] is [true] when each component differs by at most [eps]
4141- (default [1e-12]). *)
4242-4343-val pp : t Fmt.t
4444-(** [pp] pretty-prints a vector. *)