Adam.Mat2A code cleanup

This commit is contained in:
tali 2024-01-22 13:18:59 -05:00
parent 9964260e0d
commit 0d1fd47715
2 changed files with 19 additions and 68 deletions

View File

@ -49,8 +49,7 @@ module Vec2 = struct
let pp ppf { x; y } = let pp ppf { x; y } =
Format.fprintf ppf "[%.1f; %.1f]" x y Format.fprintf ppf "[%.1f; %.1f]" x y
let[@inline] make (x : float) (y : float) : t = let[@inline] make x y = { x; y }
{ x; y }
let[@inline] set ~x ~y t = let[@inline] set ~x ~y t =
begin begin
@ -84,19 +83,17 @@ module Mat2A = struct
let pp ppf { a0; a1; a2; a3; a4; a5 } = let pp ppf { a0; a1; a2; a3; a4; a5 } =
Format.fprintf ppf "[%.1f %.1f %.1f; %.1f %.1f %.1f]" a0 a1 a2 a3 a4 a5 Format.fprintf ppf "[%.1f %.1f %.1f; %.1f %.1f %.1f]" a0 a1 a2 a3 a4 a5
let[@inline] make (tx : float) (ty : float) (sx : float) (sy : float) : t = (* TODO: skew/rotation *)
{ a0 = sx; a1 = 0.0; a2 = tx; a3 = 0.0; a4 = sy; a5 = ty }
(* TODO: rotation *) let[@inline] make ?(tx = 0.0) ?(ty = 0.0) ?(sx = 1.0) ?(sy = 1.0) () = {
a0 = sx; a1 = 0.0; a2 = tx;
a3 = 0.0; a4 = sy; a5 = ty;
}
let[@inline] set ~tx ~ty ~sx ~sy t = let[@inline] set ~tx ~ty ~sx ~sy t =
begin begin
t.a0 <- sx; t.a0 <- sx; t.a1 <- 0.0; t.a2 <- tx;
t.a1 <- 0.0; t.a3 <- 0.0; t.a4 <- sy; t.a5 <- ty;
t.a2 <- tx;
t.a3 <- 0.0;
t.a4 <- sy;
t.a5 <- ty;
end end
let[@inline] copy ~src dst = let[@inline] copy ~src dst =
@ -109,61 +106,15 @@ module Mat2A = struct
dst.a5 <- src.a5; dst.a5 <- src.a5;
end end
let[@inline] multiply (dst : t) (lhs : t) (rhs : t) : unit = let[@inline] multiply dst lhs rhs =
assert (dst != lhs && dst != rhs);
begin begin
dst.a0 <- (lhs.a0 * rhs.a0) + (lhs.a1 * rhs.a3); dst.a0 <- lhs.a0 * rhs.a0 + lhs.a1 * rhs.a3;
dst.a1 <- (lhs.a0 * rhs.a1) + (lhs.a1 * rhs.a4); dst.a1 <- lhs.a0 * rhs.a1 + lhs.a1 * rhs.a4;
dst.a2 <- (lhs.a0 * rhs.a2) + (lhs.a1 * rhs.a5) + lhs.a2; dst.a2 <- lhs.a0 * rhs.a2 + lhs.a1 * rhs.a5 + lhs.a2;
dst.a3 <- (lhs.a3 * rhs.a0) + (lhs.a4 * rhs.a3); dst.a3 <- lhs.a3 * rhs.a0 + lhs.a4 * rhs.a3;
dst.a4 <- (lhs.a3 * rhs.a1) + (lhs.a4 * rhs.a4); dst.a4 <- lhs.a3 * rhs.a1 + lhs.a4 * rhs.a4;
dst.a5 <- (lhs.a3 * rhs.a2) + (lhs.a4 * rhs.a5) + lhs.a5; dst.a5 <- lhs.a3 * rhs.a2 + lhs.a4 * rhs.a5 + lhs.a5;
end
(*
let[@inline] tra (dst : t) ~(tx : float) ~(ty : float) =
begin
dst.a2 <- (dst.a0 * tx) + (dst.a1 * ty) + dst.a2;
dst.a5 <- (dst.a3 * tx) + (dst.a4 * ty) + dst.a5;
end
let[@inline] sca (dst : t) ~(sx : float) ~(sy : float) =
begin
dst.a0 <- dst.a0 * sx;
dst.a1 <- dst.a1 * sy;
dst.a3 <- dst.a3 * sx;
dst.a4 <- dst.a4 * sy;
end
let inv (dst : t) (src : t) =
begin
let { a0; a1; a2; a3; a4; a5 } = src in
let b2 = (a1 * a5) - (a2 * a4) in
let b5 = (a2 * a3) - (a0 * a5) in
let det = (a0 * a4) - (a1 * a3) in
dst.a0 <- a4 / det;
dst.a1 <- a1 / -det;
dst.a2 <- b2 / det;
dst.a3 <- a3 / -det;
dst.a4 <- a0 / det;
dst.a5 <- b5 / det;
end
*)
let apply (t : t) (v : vec2) =
let Vec2.{ x; y } = v in
begin
v.x <- (t.a0 * x) + (t.a1 * y) + t.a2;
v.y <- (t.a3 * x) + (t.a4 * y) + t.a5;
end
let apply_inv (t : t) (v : vec2) =
let Vec2.{ x; y } = v in
let b2 = (t.a1 * t.a5) - (t.a2 * t.a4) in
let b5 = (t.a2 * t.a3) - (t.a0 * t.a5) in
let det = (t.a0 * t.a4) - (t.a1 * t.a3) in
begin
v.x <- ((t.a4 * x) - (t.a1 * y) + b2) / det;
v.y <- ((t.a0 * y) - (t.a3 * x) + b5) / det;
end end
end end
@ -235,7 +186,7 @@ module Color = struct
set_rgb24 dst v ~a; set_rgb24 dst v ~a;
dst dst
let[@inline] mul ?(r = 1.) ?(g = 1.) ?(b = 1.) ?(a = 1.) (t : t) = let[@inline] multiply ?(r = 1.) ?(g = 1.) ?(b = 1.) ?(a = 1.) (t : t) =
begin begin
t.r <- t.r * r * a; t.r <- t.r * r * a;
t.g <- t.g * g * a; t.g <- t.g * g * a;

View File

@ -36,11 +36,11 @@ module Transform_graph = struct
t.size <- t.size + 1 t.size <- t.size + 1
let add t parent = let add t parent =
let model_tf = mat2a 0.0 0.0 1.0 1.0 in let model_tf = mat2a () in
let parent_tf, world_tf = let parent_tf, world_tf =
match parent with match parent with
| Null -> None, model_tf | Null -> None, model_tf
| Node p -> Some p.world_tf, mat2a 0.0 0.0 1.0 1.0 | Node p -> Some p.world_tf, mat2a ()
in in
let node = let node =
Node { Node {