From b57db64c8e6749ded1a3db31768d2532b4ba7d55 Mon Sep 17 00:00:00 2001 From: jakubtomsu <66876057+jakubtomsu@users.noreply.github.com> Date: Mon, 18 May 2026 19:34:53 +0200 Subject: [PATCH] linalg: gram schmidt in any order --- core/math/linalg/specific.odin | 69 +++++++++++++--------------------- 1 file changed, 27 insertions(+), 42 deletions(-) diff --git a/core/math/linalg/specific.odin b/core/math/linalg/specific.odin index e90af23ed..e6778ca93 100644 --- a/core/math/linalg/specific.odin +++ b/core/math/linalg/specific.odin @@ -2768,52 +2768,16 @@ matrix2_orthonormalize :: proc{ @(require_results) -matrix3_orthonormalize_f16 :: proc "contextless" (m: Matrix3f16) -> (r: Matrix3f16) #no_bounds_check { - r = m - r[0] = normalize(m[0]) - - d0 := dot(r[0], r[1]) - r[1] -= r[0] * d0 - r[1] = normalize(r[1]) - - d1 := dot(r[1], r[2]) - d0 = dot(r[0], r[2]) - r[2] -= r[0]*d0 + r[1]*d1 - r[2] = normalize(r[2]) - - return +matrix3_orthonormalize_f16 :: proc "contextless" (m: Matrix3f16) -> Matrix3f16 #no_bounds_check { + return matrix3_gram_schmidt(m, 0, 1, 2) } @(require_results) -matrix3_orthonormalize_f32 :: proc "contextless" (m: Matrix3f32) -> (r: Matrix3f32) #no_bounds_check { - r = m - r[0] = normalize(m[0]) - - d0 := dot(r[0], r[1]) - r[1] -= r[0] * d0 - r[1] = normalize(r[1]) - - d1 := dot(r[1], r[2]) - d0 = dot(r[0], r[2]) - r[2] -= r[0]*d0 + r[1]*d1 - r[2] = normalize(r[2]) - - return +matrix3_orthonormalize_f32 :: proc "contextless" (m: Matrix3f32) -> Matrix3f32 #no_bounds_check { + return matrix3_gram_schmidt(m, 0, 1, 2) } @(require_results) -matrix3_orthonormalize_f64 :: proc "contextless" (m: Matrix3f64) -> (r: Matrix3f64) #no_bounds_check { - r = m - r[0] = normalize(m[0]) - - d0 := dot(r[0], r[1]) - r[1] -= r[0] * d0 - r[1] = normalize(r[1]) - - d1 := dot(r[1], r[2]) - d0 = dot(r[0], r[2]) - r[2] -= r[0]*d0 + r[1]*d1 - r[2] = normalize(r[2]) - - return +matrix3_orthonormalize_f64 :: proc "contextless" (m: Matrix3f64) -> Matrix3f64 #no_bounds_check { + return matrix3_gram_schmidt(m, 0, 1, 2) } matrix3_orthonormalize :: proc{ matrix3_orthonormalize_f16, @@ -2822,6 +2786,27 @@ matrix3_orthonormalize :: proc{ } +@(require_results) +matrix3_gram_schmidt :: proc "contextless" (m: matrix[3, 3]$E, $A, $B, $C: int) -> (r: matrix[3, 3]E) + where A != B, A != C, B != C #no_bounds_check +{ + r = m + r[A] = normalize(m[A]) + + d0 := dot(r[A], r[B]) + r[B] -= r[A] * d0 + r[B] = normalize(r[B]) + + d1 := dot(r[B], r[C]) + d0 = dot(r[A], r[C]) + r[C] -= r[A]*d0 + r[B]*d1 + r[C] = normalize(r[C]) + + return +} + + + @(require_results) vector3_orthonormalize_f16 :: proc "contextless" (x, y: Vector3f16) -> (z: Vector3f16) { return normalize(x - y * dot(y, x))