From 60b9e875ae23cd93f87ed8ef16695d4eba14ca22 Mon Sep 17 00:00:00 2001 From: Marc Sunet Date: Mon, 26 Dec 2022 09:31:30 -0800 Subject: Format. --- include/math/camera.h | 17 ++--- include/math/defs.h | 4 +- include/math/fwd.h | 10 +-- include/math/mat4.h | 162 +++++++++++++++++++++++++----------------------- include/math/quat.h | 30 ++++----- include/math/spatial3.h | 27 ++++---- include/math/vec2.h | 20 ++---- include/math/vec3.h | 8 +-- include/math/vec4.h | 4 +- 9 files changed, 143 insertions(+), 139 deletions(-) diff --git a/include/math/camera.h b/include/math/camera.h index 1621927..cb2c50b 100644 --- a/include/math/camera.h +++ b/include/math/camera.h @@ -5,7 +5,7 @@ typedef struct Camera { Spatial3 spatial; - mat4 projection; + mat4 projection; } Camera; /// Create an orthographic camera. @@ -19,11 +19,11 @@ typedef struct Camera { /// \param top The coordinate for the top horizontal clipping plane. /// \param near The distance to the near clipping plane. /// \param far The distance to the far clipping plane. -static inline Camera camera_orthographic(R left, R right, R bottom, R top, - R near, R far) { - return (Camera){.spatial = spatial3_make(), - .projection = - mat4_ortho(left, right, bottom, top, near, far)}; +static inline Camera camera_orthographic( + R left, R right, R bottom, R top, R near, R far) { + return (Camera){ + .spatial = spatial3_make(), + .projection = mat4_ortho(left, right, bottom, top, near, far)}; } /// Create a perspective camera. @@ -37,6 +37,7 @@ static inline Camera camera_orthographic(R left, R right, R bottom, R top, /// \param near Distance to the near clipping plane. /// \param far Distance to the far clipping plane. static inline Camera camera_perspective(R fovy, R aspect, R near, R far) { - return (Camera){.spatial = spatial3_make(), - .projection = mat4_perspective(fovy, aspect, near, far)}; + return (Camera){ + .spatial = spatial3_make(), + .projection = mat4_perspective(fovy, aspect, near, far)}; } diff --git a/include/math/defs.h b/include/math/defs.h index e3c3b55..f67ee97 100644 --- a/include/math/defs.h +++ b/include/math/defs.h @@ -14,12 +14,12 @@ typedef double R; #define R_MIN DBL_MIN #define R_MAX DBL_MAX #else // floats -typedef float R; +typedef float R; #define R_MIN FLT_MIN #define R_MAX FLT_MAX #endif -#define PI 3.14159265359 +#define PI 3.14159265359 #define INV_PI 0.31830988618 /// Radians per degree. diff --git a/include/math/fwd.h b/include/math/fwd.h index 6c72ded..952df4a 100644 --- a/include/math/fwd.h +++ b/include/math/fwd.h @@ -1,9 +1,9 @@ /// Forward declarations for all math objects. #pragma once -typedef struct Camera Camera; -typedef struct mat4 mat4; +typedef struct Camera Camera; +typedef struct mat4 mat4; typedef struct spatial3 spatial3; -typedef struct vec2 vec2; -typedef struct vec3 vec3; -typedef struct vec4 vec4; +typedef struct vec2 vec2; +typedef struct vec3 vec3; +typedef struct vec4 vec4; diff --git a/include/math/mat4.h b/include/math/mat4.h index 3ceb75b..e808d73 100644 --- a/include/math/mat4.h +++ b/include/math/mat4.h @@ -20,9 +20,9 @@ typedef struct mat4 { /// [ m10 m11 m12 m13 ] /// [ m20 m21 m22 m23 ] /// [ m30 m31 m32 m33 ] -static inline mat4 mat4_make(R m00, R m01, R m02, R m03, R m10, R m11, R m12, - R m13, R m20, R m21, R m22, R m23, R m30, R m31, - R m32, R m33) { +static inline mat4 mat4_make( + R m00, R m01, R m02, R m03, R m10, R m11, R m12, R m13, R m20, R m21, R m22, + R m23, R m30, R m31, R m32, R m33) { mat4 m; m.val[0][0] = m00; m.val[0][1] = m10; @@ -73,22 +73,24 @@ static inline mat4 mat4_from_array(const R M[16]) { /// Construct the identity matrix. static inline mat4 mat4_id() { - return mat4_make(1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, - 0.0, 0.0, 0.0, 1.0); + return mat4_make( + 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, + 1.0); } /// Construct a matrix from 4 column vectors. static inline mat4 mat4_from_vec4(vec4 v0, vec4 v1, vec4 v2, vec4 v3) { - return mat4_make(v0.x, v0.y, v0.z, v0.w, v1.x, v1.y, v1.z, v1.w, v2.x, v2.y, - v2.z, v2.w, v3.x, v3.y, v3.z, v3.w); + return mat4_make( + v0.x, v0.y, v0.z, v0.w, v1.x, v1.y, v1.z, v1.w, v2.x, v2.y, v2.z, v2.w, + v3.x, v3.y, v3.z, v3.w); } /// Construct a transformation matrix from 4 vectors. -static inline mat4 mat4_from_vec3(vec3 right, vec3 up, vec3 forward, - vec3 position) { - return mat4_make(right.x, right.y, right.z, 0.0, up.x, up.y, up.z, 0.0, - forward.x, forward.y, forward.z, 0.0, position.x, position.y, - position.z, 1.0); +static inline mat4 mat4_from_vec3( + vec3 right, vec3 up, vec3 forward, vec3 position) { + return mat4_make( + right.x, right.y, right.z, 0.0, up.x, up.y, up.z, 0.0, forward.x, + forward.y, forward.z, 0.0, position.x, position.y, position.z, 1.0); } /// Return the value at the specified position. @@ -205,23 +207,25 @@ static inline mat4 mat4_mul(mat4 A, mat4 B) { mat4_at(A, 3, 2) * mat4_at(B, 2, 3) + mat4_at(A, 3, 3) * mat4_at(B, 3, 3); - return mat4_make(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, - m30, m31, m32, m33); + return mat4_make( + m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, + m33); } /// Return the translation component of the matrix. static inline mat4 mat4_translation(mat4 m) { - return mat4_make(1.0, 0.0, 0.0, mat4_at(m, 0, 3), 0.0, 1.0, 0.0, - mat4_at(m, 1, 3), 0.0, 0.0, 1.0, mat4_at(m, 2, 3), 0.0, 0.0, - 0.0, 1.0); + return mat4_make( + 1.0, 0.0, 0.0, mat4_at(m, 0, 3), 0.0, 1.0, 0.0, mat4_at(m, 1, 3), 0.0, + 0.0, 1.0, mat4_at(m, 2, 3), 0.0, 0.0, 0.0, 1.0); } /// Return the rotation component of the matrix. static inline mat4 mat4_rotation(mat4 m) { - return mat4_make(mat4_at(m, 0, 0), mat4_at(m, 0, 1), mat4_at(m, 0, 2), 0.0, - mat4_at(m, 1, 0), mat4_at(m, 1, 1), mat4_at(m, 1, 2), 0.0, - mat4_at(m, 2, 0), mat4_at(m, 2, 1), mat4_at(m, 2, 2), 0.0, - 0.0, 0.0, 0.0, 1.0); + return mat4_make( + mat4_at(m, 0, 0), mat4_at(m, 0, 1), mat4_at(m, 0, 2), 0.0, + mat4_at(m, 1, 0), mat4_at(m, 1, 1), mat4_at(m, 1, 2), 0.0, + mat4_at(m, 2, 0), mat4_at(m, 2, 1), mat4_at(m, 2, 2), 0.0, 0.0, 0.0, 0.0, + 1.0); } /// Create an X-axis rotation matrix. @@ -247,22 +251,22 @@ static inline mat4 mat4_rotz(R angle) { /// Create a rotation matrix. static inline mat4 mat4_rot(vec3 axis, R angle) { - const R s = sin(angle); - const R c = cos(angle); - const R x = axis.x; - const R y = axis.y; - const R z = axis.z; - const R xy = x * y; - const R xz = x * z; - const R yz = y * z; - const R sx = s * x; - const R sy = s * y; - const R sz = s * z; + const R s = sin(angle); + const R c = cos(angle); + const R x = axis.x; + const R y = axis.y; + const R z = axis.z; + const R xy = x * y; + const R xz = x * z; + const R yz = y * z; + const R sx = s * x; + const R sy = s * y; + const R sz = s * z; const R omc = 1.0 - c; - return mat4_make(c + omc * x * x, omc * xy - sz, omc * xz + sy, 0, - omc * xy + sz, c + omc * y * y, omc * yz - sx, 0, - omc * xz - sy, omc * yz + sx, c + omc * z * z, 0, 0, 0, 0, - 1); + return mat4_make( + c + omc * x * x, omc * xy - sz, omc * xz + sy, 0, omc * xy + sz, + c + omc * y * y, omc * yz - sx, 0, omc * xz - sy, omc * yz + sx, + c + omc * z * z, 0, 0, 0, 0, 1); } /// Create a scaling matrix. @@ -295,15 +299,16 @@ static inline mat4 mat4_from_forward(vec3 forward) { const vec3 f = vec3_normalize(forward); const vec3 r = vec3_normalize(vec3_cross(f, up3())); const vec3 u = vec3_normalize(vec3_cross(r, f)); - return mat4_make(r.x, u.x, -f.x, 0.0, r.y, u.y, -f.y, 0.0, r.z, u.z, -f.z, - 0.0, 0.0, 0.0, 0.0, 1.0); + return mat4_make( + r.x, u.x, -f.x, 0.0, r.y, u.y, -f.y, 0.0, r.z, u.z, -f.z, 0.0, 0.0, 0.0, + 0.0, 1.0); } /// Create a transformation matrix. static inline mat4 mat4_lookat(vec3 position, vec3 target, vec3 up) { - const vec3 fwd = vec3_normalize(vec3_sub(target, position)); + const vec3 fwd = vec3_normalize(vec3_sub(target, position)); const vec3 right = vec3_normalize(vec3_cross(fwd, up)); - up = vec3_normalize(vec3_cross(right, fwd)); + up = vec3_normalize(vec3_cross(right, fwd)); return mat4_from_vec3(right, up, fwd, position); } @@ -318,8 +323,9 @@ static inline mat4 mat4_ortho(R left, R right, R bottom, R top, R near, R far) { const R tx = -(right + left) / (right - left); const R ty = -(top + bottom) / (top - bottom); const R tz = -(far + near) / (far - near); - return mat4_make(2 / (right - left), 0, 0, tx, 0, 2 / (top - bottom), 0, ty, - 0, 0, -2 / (far - near), tz, 0, 0, 0, 1); + return mat4_make( + 2 / (right - left), 0, 0, tx, 0, 2 / (top - bottom), 0, ty, 0, 0, + -2 / (far - near), tz, 0, 0, 0, 1); } /// Create a perspective projection matrix. @@ -332,9 +338,11 @@ static inline mat4 mat4_perspective(R fovy, R aspect, R near, R far) { R f = tan(fovy / 2.0); assert(f > 0.0); f = 1.0 / f; + const R a = near - far; - return mat4_make(f / aspect, 0, 0, 0, 0, f, 0, 0, 0, 0, (far + near) / a, - (2 * far * near / a), 0, 0, -1, 0); + return mat4_make( + f / aspect, 0, 0, 0, 0, f, 0, 0, 0, 0, (far + near) / a, + (2 * far * near / a), 0, 0, -1, 0); } /// Create the inverse of a perspective projection matrix. @@ -346,18 +354,18 @@ static inline mat4 mat4_perspective(R fovy, R aspect, R near, R far) { static inline mat4 mat4_perspective_inverse(R fovy, R aspect, R near, R far) { R f = tan(fovy / 2.0); assert(f > 0.0); - f = 1.0 / f; - const R a = far * near; + f = 1.0 / f; + const R a = far * near; const R P32 = 0.5 * (near - far) / a; const R P33 = 0.5 * (far + near) / a; - return mat4_make(aspect / f, 0, 0, 0, 0, 1.0f / f, 0, 0, 0, 0, 0, -1, 0, 0, - P32, P33); + return mat4_make( + aspect / f, 0, 0, 0, 0, 1.0f / f, 0, 0, 0, 0, 0, -1, 0, 0, P32, P33); } /// Return the matrix's determinant. static inline R mat4_det(mat4 m) { - const R* M = (const R*)(m.val); - const R inv0 = M[5] * M[10] * M[15] - M[5] * M[11] * M[14] - + const R* M = (const R*)(m.val); + const R inv0 = M[5] * M[10] * M[15] - M[5] * M[11] * M[14] - M[9] * M[6] * M[15] + M[9] * M[7] * M[14] + M[13] * M[6] * M[11] - M[13] * M[7] * M[10]; const R inv1 = -M[4] * M[10] * M[15] + M[4] * M[11] * M[14] + @@ -413,10 +421,11 @@ static inline mat4 mat4_inverse(mat4 m) { R det = M[0] * inv[0] + M[1] * inv[4] + M[2] * inv[8] + M[3] * inv[12]; assert(det != 0.0); det = 1.0 / det; - return mat4_make(inv[0] * det, inv[4] * det, inv[8] * det, inv[12] * det, - inv[1] * det, inv[5] * det, inv[9] * det, inv[13] * det, - inv[2] * det, inv[6] * det, inv[10] * det, inv[14] * det, - inv[3] * det, inv[7] * det, inv[11] * det, inv[15] * det); + return mat4_make( + inv[0] * det, inv[4] * det, inv[8] * det, inv[12] * det, inv[1] * det, + inv[5] * det, inv[9] * det, inv[13] * det, inv[2] * det, inv[6] * det, + inv[10] * det, inv[14] * det, inv[3] * det, inv[7] * det, inv[11] * det, + inv[15] * det); } /// Invert the transformation matrix. @@ -428,9 +437,9 @@ static inline mat4 mat4_inverse_transform(mat4 m) { const vec3 u = mat4_v1(m); const vec3 f = mat4_v2(m); const vec3 t = mat4_v3(m); - return mat4_make(r.x, r.y, r.z, -vec3_dot(r, t), u.x, u.y, u.z, - -vec3_dot(u, t), f.x, f.y, f.z, -vec3_dot(f, t), 0.0, 0.0, - 0.0, 1.0); + return mat4_make( + r.x, r.y, r.z, -vec3_dot(r, t), u.x, u.y, u.z, -vec3_dot(u, t), f.x, f.y, + f.z, -vec3_dot(f, t), 0.0, 0.0, 0.0, 1.0); } /// Transpose the matrix. @@ -473,23 +482,24 @@ static inline vec4 mat4_mul_vec4(mat4 m, vec4 v) { /// within |eps|, false if there is at least one ij-value difference that is /// greater than eps. static inline bool mat4_eq(mat4 m, mat4 w, float eps) { - return (float_eq(mat4_at(m, 0, 0), mat4_at(w, 0, 0), eps) && - float_eq(mat4_at(m, 0, 1), mat4_at(w, 0, 1), eps) && - float_eq(mat4_at(m, 0, 2), mat4_at(w, 0, 2), eps) && - float_eq(mat4_at(m, 0, 3), mat4_at(w, 0, 3), eps) && - - float_eq(mat4_at(m, 1, 0), mat4_at(w, 1, 0), eps) && - float_eq(mat4_at(m, 1, 1), mat4_at(w, 1, 1), eps) && - float_eq(mat4_at(m, 1, 2), mat4_at(w, 1, 2), eps) && - float_eq(mat4_at(m, 1, 3), mat4_at(w, 1, 3), eps) && - - float_eq(mat4_at(m, 2, 0), mat4_at(w, 2, 0), eps) && - float_eq(mat4_at(m, 2, 1), mat4_at(w, 2, 1), eps) && - float_eq(mat4_at(m, 2, 2), mat4_at(w, 2, 2), eps) && - float_eq(mat4_at(m, 2, 3), mat4_at(w, 2, 3), eps) && - - float_eq(mat4_at(m, 3, 0), mat4_at(w, 3, 0), eps) && - float_eq(mat4_at(m, 3, 1), mat4_at(w, 3, 1), eps) && - float_eq(mat4_at(m, 3, 2), mat4_at(w, 3, 2), eps) && - float_eq(mat4_at(m, 3, 3), mat4_at(w, 3, 3), eps)); + return ( + float_eq(mat4_at(m, 0, 0), mat4_at(w, 0, 0), eps) && + float_eq(mat4_at(m, 0, 1), mat4_at(w, 0, 1), eps) && + float_eq(mat4_at(m, 0, 2), mat4_at(w, 0, 2), eps) && + float_eq(mat4_at(m, 0, 3), mat4_at(w, 0, 3), eps) && + + float_eq(mat4_at(m, 1, 0), mat4_at(w, 1, 0), eps) && + float_eq(mat4_at(m, 1, 1), mat4_at(w, 1, 1), eps) && + float_eq(mat4_at(m, 1, 2), mat4_at(w, 1, 2), eps) && + float_eq(mat4_at(m, 1, 3), mat4_at(w, 1, 3), eps) && + + float_eq(mat4_at(m, 2, 0), mat4_at(w, 2, 0), eps) && + float_eq(mat4_at(m, 2, 1), mat4_at(w, 2, 1), eps) && + float_eq(mat4_at(m, 2, 2), mat4_at(w, 2, 2), eps) && + float_eq(mat4_at(m, 2, 3), mat4_at(w, 2, 3), eps) && + + float_eq(mat4_at(m, 3, 0), mat4_at(w, 3, 0), eps) && + float_eq(mat4_at(m, 3, 1), mat4_at(w, 3, 1), eps) && + float_eq(mat4_at(m, 3, 2), mat4_at(w, 3, 2), eps) && + float_eq(mat4_at(m, 3, 3), mat4_at(w, 3, 3), eps)); } diff --git a/include/math/quat.h b/include/math/quat.h index 4503abd..b284567 100644 --- a/include/math/quat.h +++ b/include/math/quat.h @@ -23,14 +23,14 @@ static inline quat quat_from_array(const R xyzw[4]) { /// Construct a rotation quaternion. static inline quat qmake_rot(R angle, R x, R y, R z) { - const R a = angle * 0.5; - const R sa = sin(a); - const R w = cos(a); - R mag = sqrt(x * x + y * y + z * z); - mag = mag == 0.0 ? 1.0 : mag; - x = x * sa; - y = y * sa; - z = z * sa; + const R a = angle * 0.5; + const R sa = sin(a); + const R w = cos(a); + R mag = sqrt(x * x + y * y + z * z); + mag = mag == 0.0 ? 1.0 : mag; + x = x * sa; + y = y * sa; + z = z * sa; return (quat){x / mag, y / mag, z / mag, w}; } @@ -46,7 +46,7 @@ static inline quat qmul(quat q1, quat q2) { /// Invert the quaternion. static inline quat qinv(quat q) { R magsq = q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z; - magsq = magsq == 0.0f ? 1.0f : magsq; + magsq = magsq == 0.0f ? 1.0f : magsq; return (quat){-q.x / magsq, -q.y / magsq, -q.z / magsq, q.w / magsq}; } @@ -55,9 +55,9 @@ static inline quat qconj(quat q) { return (quat){-q.x, -q.y, -q.z, q.w}; } /// Rotate the given vector by the given unit quaternion. static inline vec3 qrot(quat q, vec3 v) { - const quat p = qconj(q); + const quat p = qconj(q); const quat qv = (quat){v.x, v.y, v.z, 0}; - const quat u = qmul(qmul(q, qv), p); + const quat u = qmul(qmul(q, qv), p); return vec3_make(u.x, u.y, u.z); } @@ -72,8 +72,8 @@ static inline mat4 mat4_from_quat(quat q) { const R wx = q.w * q.x; const R wy = q.w * q.y; const R wz = q.w * q.z; - return mat4_make(1 - 2 * yy - 2 * zz, 2 * xy - 2 * wz, 2 * xz + 2 * wy, 0, - 2 * xy + 2 * wz, 1 - 2 * xx - 2 * zz, 2 * yz - 2 * wx, 0, - 2 * xz - 2 * wy, 2 * yz + 2 * wx, 1 - 2 * xx - 2 * yy, 0, 0, - 0, 0, 1); + return mat4_make( + 1 - 2 * yy - 2 * zz, 2 * xy - 2 * wz, 2 * xz + 2 * wy, 0, 2 * xy + 2 * wz, + 1 - 2 * xx - 2 * zz, 2 * yz - 2 * wx, 0, 2 * xz - 2 * wy, 2 * yz + 2 * wx, + 1 - 2 * xx - 2 * yy, 0, 0, 0, 0, 1); } diff --git a/include/math/spatial3.h b/include/math/spatial3.h index f8caf5d..8de38bf 100644 --- a/include/math/spatial3.h +++ b/include/math/spatial3.h @@ -23,8 +23,9 @@ static inline mat4 spatial3_transform(const Spatial3* spatial) { const vec3 r = spatial->r; const vec3 u = spatial->u; const vec3 f = spatial->f; - return mat4_make(r.x, u.x, -f.x, p.x, r.y, u.y, -f.y, p.y, r.z, u.z, -f.z, - p.z, 0.0, 0.0, 0.0, 1.0f); + return mat4_make( + r.x, u.x, -f.x, p.x, r.y, u.y, -f.y, p.y, r.z, u.z, -f.z, p.z, 0.0, 0.0, + 0.0, 1.0f); } /// Return the spatial's inverse transformation matrix (from world to local @@ -70,10 +71,10 @@ static inline void spatial3_move_down(Spatial3* spatial, R speed) { /// Rotate the spatial about the given axis by the given angle. static inline void spatial3_rotate(Spatial3* spatial, vec3 axis, R angle) { - mat4 transf = spatial3_transform(spatial); + mat4 transf = spatial3_transform(spatial); const vec3 local_axis = vec3_normalize(mat4_mul_vec3(mat4_inverse_transform(transf), axis, 0.0)); - transf = mat4_mul(transf, mat4_rot(local_axis, angle)); + transf = mat4_mul(transf, mat4_rot(local_axis, angle)); spatial->r = vec3_normalize(mat4_v0(transf)); spatial->u = vec3_normalize(mat4_v1(transf)); spatial->f = vec3_normalize(vec3_neg(mat4_v2(transf))); @@ -131,8 +132,8 @@ static inline void spatial3_lookat(Spatial3* spatial, vec3 target) { } /// Make the spatial look at the given target. -static inline void spatial3_lookat_spatial(Spatial3* spatial, - const Spatial3* target) { +static inline void spatial3_lookat_spatial( + Spatial3* spatial, const Spatial3* target) { spatial3_set_forward(spatial, vec3_sub(target->p, spatial->p)); } @@ -141,14 +142,15 @@ static inline void spatial3_lookat_spatial(Spatial3* spatial, /// \param radius Radial distance. /// \param azimuth Azimuthal (horizontal) angle. /// \param zenith Polar (vertical) angle. -static inline void spatial3_orbit(Spatial3* spatial, vec3 target, R radius, - R azimuth, R zenith) { +static inline void spatial3_orbit( + Spatial3* spatial, vec3 target, R radius, R azimuth, R zenith) { const R sx = sin(azimuth); const R sy = sin(zenith); const R cx = cos(azimuth); const R cy = cos(zenith); - spatial->p = (vec3){target.x + radius * cy * sx, target.y + radius * sy, - target.z + radius * cx * cy}; + spatial->p = (vec3){ + target.x + radius * cy * sx, target.y + radius * sy, + target.z + radius * cx * cy}; } /// Make the spatial orbit around the given target. @@ -156,9 +158,8 @@ static inline void spatial3_orbit(Spatial3* spatial, vec3 target, R radius, /// \param radius Radial distance. /// \param azimuth Azimuthal (horizontal) angle. /// \param zenith Polar (vertical) angle. -static inline void spatial3_orbit_spatial(Spatial3* spatial, - const Spatial3* target, R radius, - R azimuth, R zenith) { +static inline void spatial3_orbit_spatial( + Spatial3* spatial, const Spatial3* target, R radius, R azimuth, R zenith) { spatial3_orbit(spatial, target->p, radius, azimuth, zenith); } diff --git a/include/math/vec2.h b/include/math/vec2.h index dc51c17..5a74705 100644 --- a/include/math/vec2.h +++ b/include/math/vec2.h @@ -52,19 +52,13 @@ static inline vec2 vec2_div(vec2 a, vec2 b) { } /// Scale a vector by a scalar value. -static inline vec2 vec2_scale(vec2 v, R s) { - return (vec2){v.x * s, v.y * s}; -} +static inline vec2 vec2_scale(vec2 v, R s) { return (vec2){v.x * s, v.y * s}; } /// Compare two vectors for equality. -static inline bool vec2_eq(vec2 a, vec2 b) { - return a.x == b.x && a.y == b.y; -} +static inline bool vec2_eq(vec2 a, vec2 b) { return a.x == b.x && a.y == b.y; } /// Return the absolute value of the vector. -static inline vec2 vec2_abs(vec2 v) { - return (vec2){rabs(v.x), rabs(v.y)}; -} +static inline vec2 vec2_abs(vec2 v) { return (vec2){rabs(v.x), rabs(v.y)}; } /// Compare two vectors for inequality. static inline bool vec2_ne(vec2 a, vec2 b) { return !(vec2_eq(a, b)); } @@ -92,9 +86,7 @@ static inline vec2 vec2_normalize(vec2 v) { } /// Return the dot product of two vectors. -static inline R vec2_dot(vec2 a, vec2 b) { - return a.x * b.x + a.y * b.y; -} +static inline R vec2_dot(vec2 a, vec2 b) { return a.x * b.x + a.y * b.y; } /// Reflect the vector about the normal. static inline vec2 vec2_reflect(vec2 v, vec2 n) { @@ -108,8 +100,8 @@ static inline vec2 vec2_refract(vec2 v, vec2 n, R e) { const R k = 1.0 - e * e * (1.0 - vec2_dot(n, v) * vec2_dot(n, v)); assert(k >= 0); // r = e*v - (e * dot(n,v) + sqrt(k)) * n - return vec2_sub(vec2_scale(v, e), - vec2_scale(n, e * vec2_dot(n, v) * sqrt(k))); + return vec2_sub( + vec2_scale(v, e), vec2_scale(n, e * vec2_dot(n, v) * sqrt(k))); } /// Elevate the vector to a power. diff --git a/include/math/vec3.h b/include/math/vec3.h index caa212e..641c02f 100644 --- a/include/math/vec3.h +++ b/include/math/vec3.h @@ -98,8 +98,8 @@ static inline R vec3_dot(vec3 a, vec3 b) { /// Return the cross product of two vectors. static inline vec3 vec3_cross(vec3 a, vec3 b) { - return (vec3){a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, - a.x * b.y - a.y * b.x}; + return (vec3){ + a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}; } /// Reflect the vector about the normal. @@ -114,8 +114,8 @@ static inline vec3 vec3_refract(vec3 v, vec3 n, R e) { const R k = 1.0 - e * e * (1.0 - vec3_dot(n, v) * vec3_dot(n, v)); assert(k >= 0); // r = e*v - (e * dot(n,v) + sqrt(k)) * n - return vec3_sub(vec3_scale(v, e), - vec3_scale(n, e * vec3_dot(n, v) * sqrt(k))); + return vec3_sub( + vec3_scale(v, e), vec3_scale(n, e * vec3_dot(n, v) * sqrt(k))); } /// Elevate the vector to a power. diff --git a/include/math/vec4.h b/include/math/vec4.h index 60da464..5a797f6 100644 --- a/include/math/vec4.h +++ b/include/math/vec4.h @@ -112,8 +112,8 @@ static inline vec4 vec4_refract(vec4 v, vec4 n, R e) { const R k = 1.0 - e * e * (1.0 - vec4_dot(n, v) * vec4_dot(n, v)); assert(k >= 0); // r = e*v - (e * dot(n,v) + sqrt(k)) * n - return vec4_sub(vec4_scale(v, e), - vec4_scale(n, e * vec4_dot(n, v) * sqrt(k))); + return vec4_sub( + vec4_scale(v, e), vec4_scale(n, e * vec4_dot(n, v) * sqrt(k))); } /// Elevate the vector to a power. -- cgit v1.2.3