From 005d4b93f25c0e22df9b78b2cfa46105febbd8b3 Mon Sep 17 00:00:00 2001 From: bjorn Date: Thu, 15 Nov 2018 08:03:51 -0800 Subject: [PATCH] Move math helpers into lib; They aren't really part of the math module since they are shared across several modules, more "util"-y. --- src/api/math.c | 2 - src/api/math.h | 2 +- src/api/types/modelData.c | 1 - src/api/types/transform.c | 1 - src/audio/audio.c | 3 +- src/data/modelData.c | 4 +- src/graphics/animator.c | 3 - src/graphics/animator.h | 2 +- src/graphics/graphics.c | 3 +- src/graphics/model.c | 1 - src/graphics/model.h | 2 +- src/graphics/opengl.c | 2 +- src/headset/fake.c | 4 +- src/headset/openvr.c | 3 +- src/{math/mat4.c => lib/math.c} | 189 ++++++++++++++++++++++++++++++-- src/{math/mat4.h => lib/math.h} | 30 ++++- src/math/curve.h | 2 +- src/math/math.h | 7 +- src/math/quat.c | 124 --------------------- src/math/quat.h | 13 --- src/math/transform.c | 1 - src/math/transform.h | 2 +- src/math/vec3.c | 55 ---------- src/math/vec3.h | 13 --- src/physics/physics.c | 2 +- 25 files changed, 222 insertions(+), 249 deletions(-) rename src/{math/mat4.c => lib/math.c} (75%) rename src/{math/mat4.h => lib/math.h} (64%) delete mode 100644 src/math/quat.c delete mode 100644 src/math/quat.h delete mode 100644 src/math/vec3.c delete mode 100644 src/math/vec3.h diff --git a/src/api/math.c b/src/api/math.c index 5482c3ac..5b2e3962 100644 --- a/src/api/math.c +++ b/src/api/math.c @@ -1,8 +1,6 @@ #include "api.h" #include "api/math.h" #include "math/math.h" -#include "math/mat4.h" -#include "math/quat.h" #include "math/curve.h" #include "math/randomGenerator.h" #include "math/transform.h" diff --git a/src/api/math.h b/src/api/math.h index d149c24e..a74e1863 100644 --- a/src/api/math.h +++ b/src/api/math.h @@ -1,4 +1,4 @@ -#include "math/mat4.h" +#include "lib/math.h" #include "math/randomGenerator.h" int luax_readtransform(lua_State* L, int index, mat4 transform, int scaleComponents); diff --git a/src/api/types/modelData.c b/src/api/types/modelData.c index fe0a113a..dde14a2f 100644 --- a/src/api/types/modelData.c +++ b/src/api/types/modelData.c @@ -1,7 +1,6 @@ #include "api.h" #include "data/modelData.h" #include "math/transform.h" -#include "math/mat4.h" int l_lovrModelDataGetVertexData(lua_State* L) { ModelData* modelData = luax_checktype(L, 1, ModelData); diff --git a/src/api/types/transform.c b/src/api/types/transform.c index 0327e5a8..4baf5f96 100644 --- a/src/api/types/transform.c +++ b/src/api/types/transform.c @@ -1,5 +1,4 @@ #include "api.h" -#include "math/mat4.h" #include "math/transform.h" int luax_readtransform(lua_State* L, int index, mat4 m, int scaleComponents) { diff --git a/src/audio/audio.c b/src/audio/audio.c index 01f7a9a7..a52b589a 100644 --- a/src/audio/audio.c +++ b/src/audio/audio.c @@ -1,6 +1,5 @@ #include "audio/audio.h" -#include "math/vec3.h" -#include "math/quat.h" +#include "lib/math.h" #include "util.h" #include diff --git a/src/data/modelData.c b/src/data/modelData.c index 39daabac..68f1203a 100644 --- a/src/data/modelData.c +++ b/src/data/modelData.c @@ -1,9 +1,7 @@ #include "data/modelData.h" #include "filesystem/filesystem.h" #include "filesystem/file.h" -#include "math/mat4.h" -#include "math/quat.h" -#include "math/vec3.h" +#include "lib/math.h" #include #include #include diff --git a/src/graphics/animator.c b/src/graphics/animator.c index 8c771ace..edf6d342 100644 --- a/src/graphics/animator.c +++ b/src/graphics/animator.c @@ -1,7 +1,4 @@ #include "graphics/animator.h" -#include "math/mat4.h" -#include "math/vec3.h" -#include "math/quat.h" #include #include diff --git a/src/graphics/animator.h b/src/graphics/animator.h index dc320f82..10bf4e18 100644 --- a/src/graphics/animator.h +++ b/src/graphics/animator.h @@ -1,6 +1,6 @@ #include "data/modelData.h" -#include "math/mat4.h" #include "util.h" +#include "lib/math.h" #include "lib/map/map.h" #include "lib/vec/vec.h" #include diff --git a/src/graphics/graphics.c b/src/graphics/graphics.c index 72106af0..f2eb1c2e 100644 --- a/src/graphics/graphics.c +++ b/src/graphics/graphics.c @@ -3,10 +3,9 @@ #include "data/rasterizer.h" #include "event/event.h" #include "filesystem/filesystem.h" -#include "math/mat4.h" -#include "math/vec3.h" #include "util.h" #include "lib/glfw.h" +#include "lib/math.h" #include "lib/stb/stb_image.h" #define _USE_MATH_DEFINES #include diff --git a/src/graphics/model.c b/src/graphics/model.c index 7c673059..ca07b36d 100644 --- a/src/graphics/model.c +++ b/src/graphics/model.c @@ -4,7 +4,6 @@ #include "data/blob.h" #include "data/textureData.h" #include "data/vertexData.h" -#include "math/mat4.h" #include #include #include diff --git a/src/graphics/model.h b/src/graphics/model.h index ef781f3e..6f7a5627 100644 --- a/src/graphics/model.h +++ b/src/graphics/model.h @@ -3,7 +3,7 @@ #include "graphics/material.h" #include "graphics/mesh.h" #include "graphics/texture.h" -#include "math/mat4.h" +#include "lib/math.h" #include "util.h" #include diff --git a/src/graphics/opengl.c b/src/graphics/opengl.c index 19eb37aa..1fd74e5a 100644 --- a/src/graphics/opengl.c +++ b/src/graphics/opengl.c @@ -5,7 +5,7 @@ #include "graphics/texture.h" #include "resources/shaders.h" #include "data/modelData.h" -#include "math/mat4.h" +#include "lib/math.h" #include "lib/vec/vec.h" #include #include diff --git a/src/headset/fake.c b/src/headset/fake.c index f0b404c6..ca81ae50 100644 --- a/src/headset/fake.c +++ b/src/headset/fake.c @@ -1,8 +1,6 @@ #include "event/event.h" #include "graphics/graphics.h" -#include "math/mat4.h" -#include "math/vec3.h" -#include "math/quat.h" +#include "lib/math.h" #include "lib/glfw.h" #define _USE_MATH_DEFINES #include diff --git a/src/headset/openvr.c b/src/headset/openvr.c index 4c540406..f86ae592 100644 --- a/src/headset/openvr.c +++ b/src/headset/openvr.c @@ -1,10 +1,9 @@ #include "event/event.h" #include "graphics/graphics.h" #include "graphics/canvas.h" -#include "math/mat4.h" -#include "math/quat.h" #include "platform.h" #include "util.h" +#include "lib/math.h" #include #include #include diff --git a/src/math/mat4.c b/src/lib/math.c similarity index 75% rename from src/math/mat4.c rename to src/lib/math.c index d4288864..fb57ab8b 100644 --- a/src/math/mat4.c +++ b/src/lib/math.c @@ -1,16 +1,191 @@ -#include "math/mat4.h" -#include "math/quat.h" -#include "math/vec3.h" +#include "math.h" #include #include #ifdef LOVR_USE_SSE #include #endif -// m0 m4 m8 m12 -// m1 m5 m9 m13 -// m2 m6 m10 m14 -// m3 m7 m11 m15 +// vec3 + +vec3 vec3_init(vec3 v, vec3 u) { + return vec3_set(v, u[0], u[1], u[2]); +} + +vec3 vec3_set(vec3 v, float x, float y, float z) { + v[0] = x; + v[1] = y; + v[2] = z; + return v; +} + +vec3 vec3_add(vec3 v, vec3 u) { + v[0] += u[0]; + v[1] += u[1]; + v[2] += u[2]; + return v; +} + +vec3 vec3_scale(vec3 v, float s) { + v[0] *= s; + v[1] *= s; + v[2] *= s; + return v; +} + +vec3 vec3_normalize(vec3 v) { + float len = vec3_length(v); + return len == 0 ? v : vec3_scale(v, 1 / len); +} + +float vec3_length(vec3 v) { + return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); +} + +float vec3_dot(vec3 v, vec3 u) { + return v[0] * u[0] + v[1] * u[1] + v[2] * u[2]; +} + +vec3 vec3_cross(vec3 v, vec3 u) { + return vec3_set(v, + v[1] * u[2] - v[2] * u[1], + v[2] * u[0] - v[0] * u[2], + v[0] * u[1] - v[1] * u[0] + ); +} + +vec3 vec3_lerp(vec3 v, vec3 u, float t) { + v[0] = v[0] + (u[0] - v[0]) * t; + v[1] = v[1] + (u[1] - v[1]) * t; + v[2] = v[2] + (u[2] - v[2]) * t; + return v; +} + +// quat + +quat quat_init(quat q, quat r) { + return quat_set(q, r[0], r[1], r[2], r[3]); +} + +quat quat_set(quat q, float x, float y, float z, float w) { + q[0] = x; + q[1] = y; + q[2] = z; + q[3] = w; + return q; +} + +quat quat_fromAngleAxis(quat q, float angle, vec3 axis) { + vec3_normalize(axis); + float s = sin(angle * .5f); + float c = cos(angle * .5f); + q[0] = s * axis[0]; + q[1] = s * axis[1]; + q[2] = s * axis[2]; + q[3] = c; + return q; +} + +#ifndef MAX +#define MAX(a, b) (a > b ? a : b) +#endif +quat quat_fromMat4(quat q, mat4 m) { + float x = sqrt(MAX(0, 1 + m[0] - m[5] - m[10])) / 2; + float y = sqrt(MAX(0, 1 - m[0] + m[5] - m[10])) / 2; + float z = sqrt(MAX(0, 1 - m[0] - m[5] + m[10])) / 2; + float w = sqrt(MAX(0, 1 + m[0] + m[5] + m[10])) / 2; + x = (m[9] - m[6]) > 0 ? -x : x; + y = (m[2] - m[8]) > 0 ? -y : y; + z = (m[4] - m[1]) > 0 ? -z : z; + q[0] = x; + q[1] = y; + q[2] = z; + q[3] = w; + return q; +} + +quat quat_normalize(quat q) { + float len = quat_length(q); + if (len == 0) { + return q; + } + + len = 1 / len; + q[0] *= len; + q[1] *= len; + q[2] *= len; + q[3] *= len; + return q; +} + +float quat_length(quat q) { + return sqrt(q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]); +} + +quat quat_slerp(quat q, quat r, float t) { + float dot = q[0] * r[0] + q[1] * r[1] + q[2] * r[2] + q[3] * r[3]; + if (fabs(dot) >= 1.f) { + return q; + } + + if (dot < 0) { + q[0] *= -1; + q[1] *= -1; + q[2] *= -1; + q[3] *= -1; + dot *= -1; + } + + float halfTheta = acos(dot); + float sinHalfTheta = sqrt(1.f - dot * dot); + + if (fabs(sinHalfTheta) < .001) { + q[0] = q[0] * .5 + r[0] * .5; + q[1] = q[1] * .5 + r[1] * .5; + q[2] = q[2] * .5 + r[2] * .5; + q[3] = q[3] * .5 + r[3] * .5; + return q; + } + + float a = sin((1 - t) * halfTheta) / sinHalfTheta; + float b = sin(t * halfTheta) / sinHalfTheta; + + q[0] = q[0] * a + r[0] * b; + q[1] = q[1] * a + r[1] * b; + q[2] = q[2] * a + r[2] * b; + q[3] = q[3] * a + r[3] * b; + + return q; +} + +void quat_rotate(quat q, vec3 v) { + float s = q[3]; + float u[3]; + float c[3]; + vec3_init(u, q); + vec3_cross(vec3_init(c, u), v); + float uu = vec3_dot(u, u); + float uv = vec3_dot(u, v); + vec3_scale(u, 2 * uv); + vec3_scale(v, s * s - uu); + vec3_scale(c, 2 * s); + vec3_add(v, vec3_add(u, c)); +} + +void quat_getAngleAxis(quat q, float* angle, float* x, float* y, float* z) { + if (q[3] > 1 || q[3] < -1) { + quat_normalize(q); + } + + float qw = q[3]; + float s = sqrt(1 - qw * qw); + s = s < .0001 ? 1 : 1 / s; + *angle = 2 * acos(qw); + *x = q[0] * s; + *y = q[1] * s; + *z = q[2] * s; +} + +// mat4 mat4 mat4_set(mat4 m, mat4 n) { return memcpy(m, n, 16 * sizeof(float)); diff --git a/src/math/mat4.h b/src/lib/math.h similarity index 64% rename from src/math/mat4.h rename to src/lib/math.h index a8f9c471..4f347553 100644 --- a/src/math/mat4.h +++ b/src/lib/math.h @@ -1,9 +1,33 @@ -#include "math/math.h" - #pragma once -#define MAT4_IDENTITY { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 } +typedef float* vec3; +typedef float* quat; +typedef float* mat4; +// vec3 +vec3 vec3_init(vec3 v, vec3 u); +vec3 vec3_set(vec3 v, float x, float y, float z); +vec3 vec3_add(vec3 v, vec3 u); +vec3 vec3_scale(vec3 v, float s); +vec3 vec3_normalize(vec3 v); +float vec3_length(vec3 v); +float vec3_dot(vec3 v, vec3 u); +vec3 vec3_cross(vec3 v, vec3 u); +vec3 vec3_lerp(vec3 v, vec3 u, float t); + +// quat +quat quat_init(quat q, quat r); +quat quat_set(quat q, float x, float y, float z, float w); +quat quat_fromAngleAxis(quat q, float angle, vec3 axis); +quat quat_fromMat4(quat q, mat4 m); +quat quat_normalize(quat q); +float quat_length(quat q); +quat quat_slerp(quat q, quat r, float t); +void quat_rotate(quat q, vec3 v); +void quat_getAngleAxis(quat q, float* angle, float* x, float* y, float* z); + +// mat4 +#define MAT4_IDENTITY { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 } #define mat4_init mat4_set mat4 mat4_set(mat4 m, mat4 n); mat4 mat4_fromMat34(mat4 m, float (*n)[4]); diff --git a/src/math/curve.h b/src/math/curve.h index 6f3dc6cf..38396adc 100644 --- a/src/math/curve.h +++ b/src/math/curve.h @@ -1,5 +1,5 @@ -#include "math/vec3.h" #include "util.h" +#include "lib/math.h" #include "lib/vec/vec.h" typedef struct { diff --git a/src/math/math.h b/src/math/math.h index b030f8ea..fa8f1344 100644 --- a/src/math/math.h +++ b/src/math/math.h @@ -1,14 +1,9 @@ -#include "lib/vec/vec.h" #include "math/randomGenerator.h" +#include "lib/math.h" #include #pragma once -typedef float* vec3; -typedef float* quat; -typedef float* mat4; -typedef vec_t(mat4) vec_mat4_t; - typedef struct { bool initialized; RandomGenerator* generator; diff --git a/src/math/quat.c b/src/math/quat.c deleted file mode 100644 index e95cbfd4..00000000 --- a/src/math/quat.c +++ /dev/null @@ -1,124 +0,0 @@ -#include "math/quat.h" -#include "math/vec3.h" -#include "util.h" -#include - -quat quat_init(quat q, quat r) { - return quat_set(q, r[0], r[1], r[2], r[3]); -} - -quat quat_set(quat q, float x, float y, float z, float w) { - q[0] = x; - q[1] = y; - q[2] = z; - q[3] = w; - return q; -} - -quat quat_fromAngleAxis(quat q, float angle, vec3 axis) { - vec3_normalize(axis); - float s = sin(angle * .5f); - float c = cos(angle * .5f); - q[0] = s * axis[0]; - q[1] = s * axis[1]; - q[2] = s * axis[2]; - q[3] = c; - return q; -} - -quat quat_fromMat4(quat q, mat4 m) { - float x = sqrt(MAX(0, 1 + m[0] - m[5] - m[10])) / 2; - float y = sqrt(MAX(0, 1 - m[0] + m[5] - m[10])) / 2; - float z = sqrt(MAX(0, 1 - m[0] - m[5] + m[10])) / 2; - float w = sqrt(MAX(0, 1 + m[0] + m[5] + m[10])) / 2; - x = (m[9] - m[6]) > 0 ? -x : x; - y = (m[2] - m[8]) > 0 ? -y : y; - z = (m[4] - m[1]) > 0 ? -z : z; - q[0] = x; - q[1] = y; - q[2] = z; - q[3] = w; - return q; -} - -quat quat_normalize(quat q) { - float len = quat_length(q); - if (len == 0) { - return q; - } - - len = 1 / len; - q[0] *= len; - q[1] *= len; - q[2] *= len; - q[3] *= len; - return q; -} - -float quat_length(quat q) { - return sqrt(q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]); -} - -quat quat_slerp(quat q, quat r, float t) { - float dot = q[0] * r[0] + q[1] * r[1] + q[2] * r[2] + q[3] * r[3]; - if (fabs(dot) >= 1.f) { - return q; - } - - if (dot < 0) { - q[0] *= -1; - q[1] *= -1; - q[2] *= -1; - q[3] *= -1; - dot *= -1; - } - - float halfTheta = acos(dot); - float sinHalfTheta = sqrt(1.f - dot * dot); - - if (fabs(sinHalfTheta) < .001) { - q[0] = q[0] * .5 + r[0] * .5; - q[1] = q[1] * .5 + r[1] * .5; - q[2] = q[2] * .5 + r[2] * .5; - q[3] = q[3] * .5 + r[3] * .5; - return q; - } - - float a = sin((1 - t) * halfTheta) / sinHalfTheta; - float b = sin(t * halfTheta) / sinHalfTheta; - - q[0] = q[0] * a + r[0] * b; - q[1] = q[1] * a + r[1] * b; - q[2] = q[2] * a + r[2] * b; - q[3] = q[3] * a + r[3] * b; - - return q; -} - -void quat_rotate(quat q, vec3 v) { - float s = q[3]; - float u[3]; - float c[3]; - vec3_init(u, q); - vec3_cross(vec3_init(c, u), v); - float uu = vec3_dot(u, u); - float uv = vec3_dot(u, v); - vec3_scale(u, 2 * uv); - vec3_scale(v, s * s - uu); - vec3_scale(c, 2 * s); - vec3_add(v, vec3_add(u, c)); -} - -void quat_getAngleAxis(quat q, float* angle, float* x, float* y, float* z) { - if (q[3] > 1 || q[3] < -1) { - quat_normalize(q); - } - - float qw = q[3]; - float s = sqrt(1 - qw * qw); - s = s < .0001 ? 1 : 1 / s; - *angle = 2 * acos(qw); - *x = q[0] * s; - *y = q[1] * s; - *z = q[2] * s; -} diff --git a/src/math/quat.h b/src/math/quat.h deleted file mode 100644 index 165c4284..00000000 --- a/src/math/quat.h +++ /dev/null @@ -1,13 +0,0 @@ -#include "math/math.h" - -#pragma once - -quat quat_init(quat q, quat r); -quat quat_set(quat q, float x, float y, float z, float w); -quat quat_fromAngleAxis(quat q, float angle, vec3 axis); -quat quat_fromMat4(quat q, mat4 m); -quat quat_normalize(quat q); -float quat_length(quat q); -quat quat_slerp(quat q, quat r, float t); -void quat_rotate(quat q, vec3 v); -void quat_getAngleAxis(quat q, float* angle, float* x, float* y, float* z); diff --git a/src/math/transform.c b/src/math/transform.c index 3e752b90..37918785 100644 --- a/src/math/transform.c +++ b/src/math/transform.c @@ -1,5 +1,4 @@ #include "transform.h" -#include "math/mat4.h" #include Transform* lovrTransformCreate(mat4 transfrom) { diff --git a/src/math/transform.h b/src/math/transform.h index ab55041a..7f9ffdeb 100644 --- a/src/math/transform.h +++ b/src/math/transform.h @@ -1,5 +1,5 @@ #include "util.h" -#include "math/math.h" +#include "lib/math.h" #include #pragma once diff --git a/src/math/vec3.c b/src/math/vec3.c deleted file mode 100644 index 3bef4d8e..00000000 --- a/src/math/vec3.c +++ /dev/null @@ -1,55 +0,0 @@ -#include "math/vec3.h" -#include - -vec3 vec3_init(vec3 v, vec3 u) { - return vec3_set(v, u[0], u[1], u[2]); -} - -vec3 vec3_set(vec3 v, float x, float y, float z) { - v[0] = x; - v[1] = y; - v[2] = z; - return v; -} - -vec3 vec3_add(vec3 v, vec3 u) { - v[0] += u[0]; - v[1] += u[1]; - v[2] += u[2]; - return v; -} - -vec3 vec3_scale(vec3 v, float s) { - v[0] *= s; - v[1] *= s; - v[2] *= s; - return v; -} - -vec3 vec3_normalize(vec3 v) { - float len = vec3_length(v); - return len == 0 ? v : vec3_scale(v, 1 / len); -} - -float vec3_length(vec3 v) { - return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); -} - -float vec3_dot(vec3 v, vec3 u) { - return v[0] * u[0] + v[1] * u[1] + v[2] * u[2]; -} - -vec3 vec3_cross(vec3 v, vec3 u) { - return vec3_set(v, - v[1] * u[2] - v[2] * u[1], - v[2] * u[0] - v[0] * u[2], - v[0] * u[1] - v[1] * u[0] - ); -} - -vec3 vec3_lerp(vec3 v, vec3 u, float t) { - v[0] = v[0] + (u[0] - v[0]) * t; - v[1] = v[1] + (u[1] - v[1]) * t; - v[2] = v[2] + (u[2] - v[2]) * t; - return v; -} diff --git a/src/math/vec3.h b/src/math/vec3.h deleted file mode 100644 index df204365..00000000 --- a/src/math/vec3.h +++ /dev/null @@ -1,13 +0,0 @@ -#include "math/math.h" - -#pragma once - -vec3 vec3_init(vec3 v, vec3 u); -vec3 vec3_set(vec3 v, float x, float y, float z); -vec3 vec3_add(vec3 v, vec3 u); -vec3 vec3_scale(vec3 v, float s); -vec3 vec3_normalize(vec3 v); -float vec3_length(vec3 v); -float vec3_dot(vec3 v, vec3 u); -vec3 vec3_cross(vec3 v, vec3 u); -vec3 vec3_lerp(vec3 v, vec3 u, float t); diff --git a/src/physics/physics.c b/src/physics/physics.c index 02b6d4c1..df1dd45f 100644 --- a/src/physics/physics.c +++ b/src/physics/physics.c @@ -1,5 +1,5 @@ #include "physics.h" -#include "math/quat.h" +#include "lib/math.h" #include #include