Repository: HandmadeMath/HandmadeMath (c)

Issue:
No matrix transpose function
We do not have a function for transposing `hmm_mat4`s. Adding one seems like the responsible thing to do! Probably call it `HMM_Transpose(hmm_mat4 Matrix)`.



Discussion:
What does a matrix Transpose function do?

Transposing a matrix [swaps its rows and columns](https://chortle.ccsu.edu/VectorLessons/vmch13/vmch13_14.html)

Heh if you know how to write one off the top of your head it'd be awesome if you could do it. If not then i will figure it out


Patch:
diff --git a/HandmadeMath.h b/HandmadeMath.h
--- a/HandmadeMath.h
+++ b/HandmadeMath.h
@@ -393,6 +393,8 @@ HMMDEF hmm_mat4 HMM_MultiplyMat4f(hmm_mat4 Matrix, float Scalar);
 HMMDEF hmm_vec4 HMM_MultiplyMat4ByVec4(hmm_mat4 Matrix, hmm_vec4 Vector);
 HMMDEF hmm_mat4 HMM_DivideMat4f(hmm_mat4 Matrix, float Scalar);
 
+HMMDEF hmm_mat4 HMM_Transpose(hmm_mat4 Matrix);
+
 HMMDEF hmm_mat4 HMM_Orthographic(float Left, float Right, float Bottom, float Top, float Near, float Far);
 HMMDEF hmm_mat4 HMM_Perspective(float FOV, float AspectRatio, float Near, float Far);
 HMMDEF hmm_mat4 HMM_Translate(hmm_vec3 Translation);
@@ -407,7 +409,6 @@ HMMDEF hmm_mat4 HMM_LookAt(hmm_vec3 Eye, hmm_vec3 Center, hmm_vec3 Up);
 
 #ifdef HANDMADE_MATH_CPP_MODE
 
-
 HMMDEF float HMM_Dot(hmm_vec2 VecOne, hmm_vec2 VecTwo);
 HMMDEF float HMM_Dot(hmm_vec3 VecOne, hmm_vec3 VecTwo);
 HMMDEF float HMM_Dot(hmm_vec4 VecOne, hmm_vec4 VecTwo);
@@ -578,7 +579,6 @@ HMM_ToRadians(float Degrees)
     return (Result);
 }
 
-
 HINLINE float
 HMM_DotVec2(hmm_vec2 VecOne, hmm_vec2 VecTwo)
 {
@@ -597,7 +597,6 @@ HMM_DotVec3(hmm_vec3 VecOne, hmm_vec3 VecTwo)
     return (Result);
 }
 
-
 HINLINE float
 HMM_DotVec4(hmm_vec4 VecOne, hmm_vec4 VecTwo)
 {
@@ -607,7 +606,6 @@ HMM_DotVec4(hmm_vec4 VecOne, hmm_vec4 VecTwo)
     return (Result);
 }
 
-
 HINLINE float
 HMM_LengthSquared(hmm_vec3 A)
 {
@@ -644,7 +642,6 @@ HMM_Power(float Base, int Exponent)
     return (Result);
 }
 
-
 HINLINE float
 HMM_Lerp(float A, float Time, float B)
 {
@@ -671,7 +668,6 @@ HMM_Clamp(float Min, float Value, float Max)
     return (Result);
 }
 
-
 HINLINE hmm_vec3
 HMM_Normalize(hmm_vec3 A)
 {
@@ -1135,6 +1131,24 @@ HMM_DivideMat4f(hmm_mat4 Matrix, float Scalar)
     return (Result);
 }
 
+hmm_mat4
+HMM_Transpose(hmm_mat4 Matrix)
+{
+    hmm_mat4 Result = HMM_Mat4();
+
+    int Columns;
+    for(Columns = 0; Columns < 4; ++Columns)
+    {
+        int Rows;
+        for(Rows = 0; Rows < 4; ++Rows)
+        {
+            Result.Elements[Rows][Columns] = Matrix.Elements[Columns][Rows];
+        }
+    }
+
+    return (Result);
+}
+
 hmm_mat4
 HMM_Orthographic(float Left, float Right, float Bottom, float Top, float Near, float Far)
 {


Test patch:
diff --git a/test/hmm_test.cpp b/test/hmm_test.cpp
--- a/test/hmm_test.cpp
+++ b/test/hmm_test.cpp
@@ -206,6 +206,41 @@ TEST(VectorOps, DotVec4)
     EXPECT_FLOAT_EQ(HMM_Dot(v1, v2), 70.0f);
 }
 
+TEST(MatrixOps, Transpose)
+{
+    hmm_mat4 m4 = HMM_Mat4(); // will have 1 - 16
+
+    // Fill the matrix
+    int Counter = 1;
+    for (int Column = 0; Column < 4; ++Column)
+    {
+        for (int Row = 0; Row < 4; ++Row)
+        {
+            m4.Elements[Column][Row] = Counter;
+            ++Counter;
+        }
+    }
+
+    // Test the matrix
+    hmm_mat4 result = HMM_Transpose(m4);
+    EXPECT_FLOAT_EQ(result.Elements[0][0], 1.0f);
+    EXPECT_FLOAT_EQ(result.Elements[0][1], 5.0f);
+    EXPECT_FLOAT_EQ(result.Elements[0][2], 9.0f);
+    EXPECT_FLOAT_EQ(result.Elements[0][3], 13.0f);
+    EXPECT_FLOAT_EQ(result.Elements[1][0], 2.0f);
+    EXPECT_FLOAT_EQ(result.Elements[1][1], 6.0f);
+    EXPECT_FLOAT_EQ(result.Elements[1][2], 10.0f);
+    EXPECT_FLOAT_EQ(result.Elements[1][3], 14.0f);
+    EXPECT_FLOAT_EQ(result.Elements[2][0], 3.0f);
+    EXPECT_FLOAT_EQ(result.Elements[2][1], 7.0f);
+    EXPECT_FLOAT_EQ(result.Elements[2][2], 11.0f);
+    EXPECT_FLOAT_EQ(result.Elements[2][3], 15.0f);
+    EXPECT_FLOAT_EQ(result.Elements[3][0], 4.0f);
+    EXPECT_FLOAT_EQ(result.Elements[3][1], 8.0f);
+    EXPECT_FLOAT_EQ(result.Elements[3][2], 12.0f);
+    EXPECT_FLOAT_EQ(result.Elements[3][3], 16.0f);
+}
+
 TEST(Addition, Vec2)
 {
     hmm_vec2 v2_1 = HMM_Vec2(1.0f, 2.0f);