]> git.lizzy.rs Git - nothing.git/commitdiff
(#999) Introduce trans_mat_vec and make mat3x3 header-only
authorrexim <reximkut@gmail.com>
Sun, 1 Sep 2019 18:34:57 +0000 (01:34 +0700)
committerrexim <reximkut@gmail.com>
Sun, 1 Sep 2019 18:34:57 +0000 (01:34 +0700)
CMakeLists.txt
src/game/level/explosion.c
src/game/level/rigid_bodies.h
src/math/mat3x3.c [deleted file]
src/math/mat3x3.h
src/math/point.c
src/math/point.h
src/math/triangle.c
src/math/triangle.h

index d81f423853d3ded2decfe05e1b152f21af59d77a..4c1182cf9498717350b1c3587f679af154f6413d 100644 (file)
@@ -130,7 +130,6 @@ add_executable(nothing
   src/game/sprite_font.h
   src/main.c
   src/math/extrema.h
-  src/math/mat3x3.c
   src/math/mat3x3.h
   src/math/pi.h
   src/math/point.c
index a0f4f57c696237f4bbad642e03d50aed9206af52..445cbdbc19147be40a9f5aa55e6450a4473bcef7 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "explosion.h"
 #include "math/rand.h"
+#include "math/mat3x3.h"
 #include "system/lt.h"
 #include "system/nth_alloc.h"
 
index b3c8107398d3b75da4227c06db5254c5ca57fa11..160693d5eec79b94a1b50f4d813a9a4254001f1d 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef RIGID_BODIES_H_
 #define RIGID_BODIES_H_
 
+#include "math/mat3x3.h"
+
 typedef struct RigidBodies RigidBodies;
 typedef struct Camera Camera;
 typedef struct Platforms Platforms;
diff --git a/src/math/mat3x3.c b/src/math/mat3x3.c
deleted file mode 100644 (file)
index 6520cb8..0000000
+++ /dev/null
@@ -1,78 +0,0 @@
-#include <math.h>
-
-#include "mat3x3.h"
-
-mat3x3 make_mat3x3(float a11, float a12, float a13,
-                   float a21, float a22, float a23,
-                   float a31, float a32, float a33)
-{
-    const mat3x3 m = {
-        .M = {
-            {a11, a12, a13},
-            {a21, a22, a23},
-            {a31, a32, a33}
-        }
-    };
-
-    return m;
-}
-
-mat3x3 mat3x3_product(mat3x3 m1, mat3x3 m2)
-{
-    mat3x3 result;
-
-    for (int i = 0; i < 3; ++i) {
-        for (int j = 0; j < 3; ++j) {
-            result.M[i][j] = 0;
-            for (int k = 0; k < 3; ++k) {
-                result.M[i][j] += m1.M[i][k] * m2.M[k][j];
-            }
-        }
-    }
-
-    return result;
-}
-
-mat3x3 mat3x3_product2(mat3x3 m1, mat3x3 m2, mat3x3 m3)
-{
-    return mat3x3_product(m1, mat3x3_product(m2, m3));
-}
-
-mat3x3 trans_mat(float x, float y)
-{
-    const mat3x3 m = {
-        .M = {
-            {1.0f, 0.0f, x},
-            {0.0f, 1.0f, y},
-            {0.0f, 0.0f, 1.0f}
-        }
-    };
-
-    return m;
-}
-
-mat3x3 rot_mat(float angle)
-{
-    const mat3x3 m = {
-        .M = {
-            {cosf(angle), -sinf(angle), 0.0f},
-            {sinf(angle), cosf(angle), 0.0f},
-            {0.0f, 0.0f, 1.0f}
-        }
-    };
-
-    return m;
-}
-
-mat3x3 scale_mat(float factor)
-{
-    const mat3x3 m = {
-        .M = {
-            {factor, 0.0f, 0.0f},
-            {0.0f, factor, 0.0f},
-            {0.0f, 0.0f, 1.0f}
-        }
-    };
-
-    return m;
-}
index 8a8d8272c98a014c8209255aa1d03e3fee6db8d8..b1550276489a9a72e27e5cc11c773c4b28be0044 100644 (file)
 #ifndef MAT3X3_H_
 #define MAT3X3_H_
 
+#include "point.h"
+
 typedef struct mat3x3 {
     float M[3][3];
 } mat3x3;
 
+static inline
 mat3x3 make_mat3x3(float a11, float a12, float a13,
                    float a21, float a22, float a23,
-                   float a31, float a32, float a33);
-mat3x3 mat3x3_product(mat3x3 m1, mat3x3 m2);
-mat3x3 mat3x3_product2(mat3x3 m1, mat3x3 m2, mat3x3 m3);
-mat3x3 trans_mat(float x, float y);
-mat3x3 rot_mat(float angle);
-mat3x3 scale_mat(float factor);
+                   float a31, float a32, float a33)
+{
+    const mat3x3 m = {
+        .M = {
+            {a11, a12, a13},
+            {a21, a22, a23},
+            {a31, a32, a33}
+        }
+    };
+
+    return m;
+}
+
+static inline
+mat3x3 mat3x3_product(mat3x3 m1, mat3x3 m2)
+{
+    mat3x3 result;
+
+    for (int i = 0; i < 3; ++i) {
+        for (int j = 0; j < 3; ++j) {
+            result.M[i][j] = 0;
+            for (int k = 0; k < 3; ++k) {
+                result.M[i][j] += m1.M[i][k] * m2.M[k][j];
+            }
+        }
+    }
+
+    return result;
+}
+
+static inline
+mat3x3 mat3x3_product2(mat3x3 m1, mat3x3 m2, mat3x3 m3)
+{
+    return mat3x3_product(m1, mat3x3_product(m2, m3));
+}
+
+static inline
+mat3x3 trans_mat(float x, float y)
+{
+    const mat3x3 m = {
+        .M = {
+            {1.0f, 0.0f, x},
+            {0.0f, 1.0f, y},
+            {0.0f, 0.0f, 1.0f}
+        }
+    };
+
+    return m;
+}
+
+static inline
+mat3x3 trans_mat_vec(Vec v)
+{
+    return trans_mat(v.x, v.y);
+}
+
+static inline
+mat3x3 rot_mat(float angle)
+{
+    const mat3x3 m = {
+        .M = {
+            {cosf(angle), -sinf(angle), 0.0f},
+            {sinf(angle), cosf(angle), 0.0f},
+            {0.0f, 0.0f, 1.0f}
+        }
+    };
+
+    return m;
+}
+
+static inline
+mat3x3 scale_mat(float factor)
+{
+    const mat3x3 m = {
+        .M = {
+            {factor, 0.0f, 0.0f},
+            {0.0f, factor, 0.0f},
+            {0.0f, 0.0f, 1.0f}
+        }
+    };
+
+    return m;
+}
+
+static inline
+Point point_mat3x3_product(Point p, mat3x3 m)
+{
+    /* Convert p to Homogeneous coordinates */
+    const float homo_p[3] = {p.x, p.y, 1};
+
+    /* Transform p with matrix m */
+    const float trans_p[3] = {
+        homo_p[0] * m.M[0][0] + homo_p[1] * m.M[0][1] + homo_p[2] * m.M[0][2],
+        homo_p[0] * m.M[1][0] + homo_p[1] * m.M[1][1] + homo_p[2] * m.M[1][2],
+        homo_p[0] * m.M[2][0] + homo_p[1] * m.M[2][1] + homo_p[2] * m.M[2][2]
+    };
+
+    /* Convert p back to Cartesian coordinates */
+    const Point result_p = {
+        .x = trans_p[0] / trans_p[2],
+        .y = trans_p[1] / trans_p[2]
+    };
+
+    return result_p;
+}
+
+static inline
+Triangle triangle_mat3x3_product(Triangle t, mat3x3 m)
+{
+    Triangle t1 = {
+        .p1 = point_mat3x3_product(t.p1, m),
+        .p2 = point_mat3x3_product(t.p2, m),
+        .p3 = point_mat3x3_product(t.p3, m)
+    };
+
+    return t1;
+}
 
 #endif  // MAT3X3_H_
index dbdfc54537c8a0d28e08e6025333f941a25ff441..e63d5a46654c9ee0c666da80c60d482feab78aa3 100644 (file)
@@ -111,27 +111,6 @@ float rad_to_deg(float a)
     return 180 / PI * a;
 }
 
-Point point_mat3x3_product(Point p, mat3x3 m)
-{
-    /* Convert p to Homogeneous coordinates */
-    const float homo_p[3] = {p.x, p.y, 1};
-
-    /* Transform p with matrix m */
-    const float trans_p[3] = {
-        homo_p[0] * m.M[0][0] + homo_p[1] * m.M[0][1] + homo_p[2] * m.M[0][2],
-        homo_p[0] * m.M[1][0] + homo_p[1] * m.M[1][1] + homo_p[2] * m.M[1][2],
-        homo_p[0] * m.M[2][0] + homo_p[1] * m.M[2][1] + homo_p[2] * m.M[2][2]
-    };
-
-    /* Convert p back to Cartesian coordinates */
-    const Point result_p = {
-        .x = trans_p[0] / trans_p[2],
-        .y = trans_p[1] / trans_p[2]
-    };
-
-    return result_p;
-}
-
 Vec vec_norm(Vec v)
 {
     // TODO(#657): math/point/vec_norm: using vec_length is too expensive
index de8eb5ce5f51b4fc1f5ddc69efbaf09a24000fee..fc055be39dabceaf7f275bddb92edc241dc28201 100644 (file)
@@ -1,7 +1,6 @@
 #ifndef POINT_H_
 #define POINT_H_
 
-#include "math/mat3x3.h"
 #include "math/pi.h"
 
 typedef struct Point {
@@ -31,6 +30,6 @@ Vec vec_entry_mult(Vec v1, Vec v2);
 Vec vec_entry_div(Vec v1, Vec v2);
 Vec vec_norm(Vec v);
 
-Point point_mat3x3_product(Point p, mat3x3 m);
+
 
 #endif  // POINT_H_
index 91b473503eb9a4a23a90bfc8ad3ff17b9a8b90b7..668f120863229ee2f53307484b4e53c6c596dea9 100644 (file)
@@ -70,14 +70,3 @@ void rect_as_triangles(Rect rect, Triangle triangles[2])
     triangles[0] = t1;
     triangles[1] = t2;
 }
-
-Triangle triangle_mat3x3_product(Triangle t, mat3x3 m)
-{
-    Triangle t1 = {
-        .p1 = point_mat3x3_product(t.p1, m),
-        .p2 = point_mat3x3_product(t.p2, m),
-        .p3 = point_mat3x3_product(t.p3, m)
-    };
-
-    return t1;
-}
index 7586311bdff5f0b27cbc52ff5930a2759e15ec17..a035432a7b0aa81d56d5b59fcef61ca2afa47907 100644 (file)
@@ -14,6 +14,4 @@ Triangle random_triangle(float radius);
 Triangle triangle_sorted_by_y(Triangle t);
 void rect_as_triangles(Rect rect, Triangle triangles[2]);
 
-Triangle triangle_mat3x3_product(Triangle t, mat3x3 m);
-
 #endif  // TRIANGLE_H_