From b0675358c3971abe39c0d0b94f7aa7b85ea9b054 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 6 Feb 2024 14:09:05 +0000 Subject: [PATCH] Add `angle_between` and `matrix2_rotate` --- core/math/linalg/general.odin | 25 +++++++++++++++++++++++ core/math/linalg/specific.odin | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/core/math/linalg/general.odin b/core/math/linalg/general.odin index 24bc4c7b3..4275dcb42 100644 --- a/core/math/linalg/general.odin +++ b/core/math/linalg/general.odin @@ -267,6 +267,31 @@ to_ptr :: proc{vector_to_ptr, matrix_to_ptr} +vector_angle_between :: proc "contextless" (a, b: $V/[$N]$E) -> E { + a0 := normalize0(a) + b0 := normalize0(b) + return math.acos(dot(a0, b0)) +} +quaternion64_angle_between :: proc "contextless" (a, b: $Q/quaternion64) -> f16 { + c := normalize0(conj(a) * b) + return math.acos(c.w) +} +quaternion128_angle_between :: proc "contextless" (a, b: $Q/quaternion128) -> f32 { + c := normalize0(conj(a) * b) + return math.acos(c.w) +} +quaternion256_angle_between :: proc "contextless" (a, b: $Q/quaternion256) -> f64 { + c := normalize0(conj(a) * b) + return math.acos(c.w) +} +angle_between :: proc{ + vector_angle_between, + quaternion64_angle_between, + quaternion128_angle_between, + quaternion256_angle_between, +} + + // Splines diff --git a/core/math/linalg/specific.odin b/core/math/linalg/specific.odin index 36783e1e2..4a0f5ee40 100644 --- a/core/math/linalg/specific.odin +++ b/core/math/linalg/specific.odin @@ -1270,6 +1270,43 @@ matrix2_adjoint :: proc{ } +@(require_results) +matrix2_rotate_f16 :: proc "contextless" (angle_radians: f16) -> Matrix2f16 { + c := math.cos(angle_radians) + s := math.sin(angle_radians) + + return Matrix2f16{ + c, -s, + s, c, + } +} +@(require_results) +matrix2_rotate_f32 :: proc "contextless" (angle_radians: f32) -> Matrix2f32 { + c := math.cos(angle_radians) + s := math.sin(angle_radians) + + return Matrix2f32{ + c, -s, + s, c, + } +} +@(require_results) +matrix2_rotate_f64 :: proc "contextless" (angle_radians: f64) -> Matrix2f64 { + c := math.cos(angle_radians) + s := math.sin(angle_radians) + + return Matrix2f64{ + c, -s, + s, c, + } +} +matrix2_rotate :: proc{ + matrix2_rotate_f16, + matrix2_rotate_f32, + matrix2_rotate_f64, +} + + @(require_results) matrix3_from_quaternion_f16 :: proc "contextless" (q: Quaternionf16) -> (m: Matrix3f16) { qxx := q.x * q.x