diff options
author | pommicket <pommicket@gmail.com> | 2023-01-02 14:43:08 -0500 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2023-01-02 14:43:08 -0500 |
commit | d5f1b97cadb8a1ad2d08cbcdded66e5140b81eb4 (patch) | |
tree | ab9e9ae111bbed260450a4af99d5930309148a82 /util.c | |
parent | c91c49ab846603e332be8e6eacebcbbb0c45017d (diff) |
remove a bunch of unused math functions
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 303 |
1 files changed, 4 insertions, 299 deletions
@@ -606,15 +606,6 @@ float randf(void) { return (float)rand() / (float)((ulong)RAND_MAX + 1); } -float rand_gauss(void) { - // https://en.wikipedia.org/wiki/Normal_distribution#Generating_values_from_normal_distribution - float U, V; - do - U = randf(), V = randf(); - while (U == 0 || V == 0); - return sqrtf(-2 * logf(U)) * cosf(TAUf * V); -} - u32 rand_u32(void) { return ((u32)rand() & 0xfff) | ((u32)rand() & 0xfff) << 12 @@ -724,96 +715,6 @@ v2 v2_polar(float r, float theta) { return V2(r * cosf(theta), r * sinf(theta)); } -v3 V3(float x, float y, float z) { - v3 v; - v.x = x; - v.y = y; - v.z = z; - return v; -} - -v3 v3_from_v2(v2 v) { - return V3(v.x, v.y, 0); -} - -v3 v3_add(v3 a, v3 b) { - return V3(a.x + b.x, a.y + b.y, a.z + b.z); -} - -v3 v3_sub(v3 a, v3 b) { - return V3(a.x - b.x, a.y - b.y, a.z - b.z); -} - -v3 v3_scale(v3 v, float s) { - return V3(v.x * s, v.y * s, v.z * s); -} - -v3 v3_lerp(float x, v3 a, v3 b) { - return V3(lerpf(x, a.x, b.x), lerpf(x, a.y, b.y), lerpf(x, a.z, b.z)); -} - -float v3_dot(v3 u, v3 v) { - return u.x*v.x + u.y*v.y + u.z*v.z; -} - -v3 v3_cross(v3 u, v3 v) { - v3 prod = V3(u.y*v.z - u.z*v.y, u.z*v.x - u.x*v.z, u.x*v.y - u.y*v.x); - return prod; -} - -float v3_len(v3 v) { - return sqrtf(v3_dot(v, v)); -} - -float v3_dist(v3 a, v3 b) { - return v3_len(v3_sub(a, b)); -} - -float v3_dist_squared(v3 a, v3 b) { - v3 diff = v3_sub(a, b); - return v3_dot(diff, diff); -} - -v3 v3_normalize(v3 v) { - float len = v3_len(v); - float mul = len == 0.0f ? 1.0f : 1.0f/len; - return v3_scale(v, mul); -} - -v2 v3_xy(v3 v) { - return V2(v.x, v.y); -} - -// a point on a unit sphere -v3 v3_on_sphere(float yaw, float pitch) { - return V3(cosf(yaw) * cosf(pitch), sinf(pitch), sinf(yaw) * cosf(pitch)); -} - -void v3_print(v3 v) { - printf("(%f, %f, %f)\n", v.x, v.y, v.z); -} - -v3 v3_rand(void) { - return V3(randf(), randf(), randf()); -} - -v3 v3_rand_unit(void) { - /* - monte carlo method - keep generating random points in cube of radius 1 (width 2) centered at origin, - until you get a point in the unit sphere, then extend it to find the point lying - on the sphere. - */ - while (1) { - v3 v = V3(rand_uniform(-1.0f, +1.0f), rand_uniform(-1.0f, +1.0f), rand_uniform(-1.0f, +1.0f)); - float dist_squared_to_origin = v3_dot(v, v); - if (dist_squared_to_origin <= 1 && dist_squared_to_origin != 0.0f) { - return v3_scale(v, 1.0f / sqrtf(dist_squared_to_origin)); - } - } - return V3(0, 0, 0); -} - v4 V4(float x, float y, float z, float w) { v4 v; v.x = x; @@ -823,207 +724,11 @@ v4 V4(float x, float y, float z, float w) { return v; } -v4 v4_add(v4 a, v4 b) { - return V4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w); -} - -v4 v4_sub(v4 a, v4 b) { - return V4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w); -} - -v4 v4_scale(v4 v, float s) { - return V4(v.x * s, v.y * s, v.z * s, v.w * s); -} - -v4 v4_scale_xyz(v4 v, float s) { - return V4(v.x * s, v.y * s, v.z * s, v.w); -} - -v4 v4_lerp(float x, v4 a, v4 b) { - return V4(lerpf(x, a.x, b.x), lerpf(x, a.y, b.y), lerpf(x, a.z, b.z), lerpf(x, a.w, b.w)); -} - -float v4_dot(v4 u, v4 v) { - return u.x*v.x + u.y*v.y + u.z*v.z + u.w*v.w; -} - -// create a new vector by multiplying the respective components of u and v -v4 v4_mul(v4 u, v4 v) { - return V4(u.x * v.x, u.y * v.y, u.z * v.z, u.w * v.w); -} - -float v4_len(v4 v) { - return sqrtf(v4_dot(v, v)); -} - -v4 v4_normalize(v4 v) { - float len = v4_len(v); - float mul = len == 0.0f ? 1.0f : 1.0f/len; - return v4_scale(v, mul); -} - -v3 v4_xyz(v4 v) { - return V3(v.x, v.y, v.z); -} - -v4 v4_rand(void) { - return V4(randf(), randf(), randf(), randf()); -} - -void v4_print(v4 v) { - printf("(%f, %f, %f, %f)\n", v.x, v.y, v.z, v.w); -} - - v2d V2D(double x, double y) { - v2d v; - v.x = x; - v.y = y; - return v; -} - -void m4_print(m4 m) { - int i; - for (i = 0; i < 4; ++i) - printf("[ %f %f %f %f ]\n", m.e[i], m.e[i+4], m.e[i+8], m.e[i+12]); - printf("\n"); -} - -m4 M4( - float a, float b, float c, float d, - float e, float f, float g, float h, - float i, float j, float k, float l, - float m, float n, float o, float p) { - m4 ret; - float *x = ret.e; - x[0] = a; x[4] = b; x[ 8] = c; x[12] = d; - x[1] = e; x[5] = f; x[ 9] = g; x[13] = h; - x[2] = i; x[6] = j; x[10] = k; x[14] = l; - x[3] = m; x[7] = n; x[11] = o; x[15] = p; - return ret; -} - -// see https://en.wikipedia.org/wiki/Rotation_matrix#General_rotations -m4 m4_yaw(float yaw) { - float c = cosf(yaw), s = sinf(yaw); - return M4( - c, 0, -s, 0, - 0, 1, 0, 0, - s, 0, c, 0, - 0, 0, 0, 1 - ); -} - -m4 m4_pitch(float pitch) { - float c = cosf(pitch), s = sinf(pitch); - return M4( - 1, 0, 0, 0, - 0, c, -s, 0, - 0, s, c, 0, - 0, 0, 0, 1 - ); -} - -// https://en.wikipedia.org/wiki/Translation_(geometry) -m4 m4_translate(v3 t) { - return M4( - 1, 0, 0, t.x, - 0, 1, 0, t.y, - 0, 0, 1, t.z, - 0, 0, 0, 1 - ); -} - -// multiply m by [v.x, v.y, v.z, 1] -v3 m4_mul_v3(m4 m, v3 v) { - return v3_add(v3_scale(V3(m.e[0], m.e[1], m.e[2]), v.x), v3_add(v3_scale(V3(m.e[4], m.e[5], m.e[6]), v.y), - v3_add(v3_scale(V3(m.e[8], m.e[9], m.e[10]), v.z), V3(m.e[12], m.e[13], m.e[14])))); -} - -/* -4x4 perspective matrix. -fov - field of view in radians, aspect - width:height aspect ratio, z_near/z_far - clipping planes -math stolen from gluPerspective (https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml) -*/ -m4 m4_perspective(float fov, float aspect, float z_near, float z_far) { - float f = 1.0f / tanf(fov / 2.0f); - return M4( - f/aspect, 0, 0, 0, - 0, f, 0, 0, - 0, 0, (z_far+z_near) / (z_near-z_far), (2.0f*z_far*z_near) / (z_near-z_far), - 0, 0, -1, 0 - ); -} - -// windows.h defines near and far, so let's not use those -m4 m4_ortho(float left, float right, float bottom, float top, float near_, float far_) { - float tx = -(right + left)/(right - left); - float ty = -(top + bottom)/(top - bottom); - float tz = -(far_ + near_)/(far_ - near_); - return M4( - 2.0f / (right - left), 0, 0, tx, - 0, 2.0f / (top - bottom), 0, ty, - 0, 0, -2.0f / (far_ - near_), tz, - 0, 0, 0, 1 - ); -} - - -m4 m4_mul(m4 a, m4 b) { - m4 prod = {0}; - int i, j; - float *x = prod.e; - for (i = 0; i < 4; ++i) { - for (j = 0; j < 4; ++j, ++x) { - float *as = &a.e[j]; - float *bs = &b.e[4*i]; - *x = as[0]*bs[0] + as[4]*bs[1] + as[8]*bs[2] + as[12]*bs[3]; - } - } - return prod; -} - -m4 m4_inv(m4 mat) { - m4 ret; - float *inv = ret.e; - float *m = mat.e; - - inv[0] = 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]; - inv[4] = -m[4] * m[10] * m[15] + m[4] * m[11] * m[14] + m[8] * m[6] * m[15] - m[8] * m[7] * m[14] - m[12] * m[6] * m[11] + m[12] * m[7] * m[10]; - inv[8] = m[4] * m[9] * m[15] - m[4] * m[11] * m[13] - m[8] * m[5] * m[15] + m[8] * m[7] * m[13] + m[12] * m[5] * m[11] - m[12] * m[7] * m[9]; - inv[12] = -m[4] * m[9] * m[14] + m[4] * m[10] * m[13] + m[8] * m[5] * m[14] - m[8] * m[6] * m[13] - m[12] * m[5] * m[10] + m[12] * m[6] * m[9]; - inv[1] = -m[1] * m[10] * m[15] + m[1] * m[11] * m[14] + m[9] * m[2] * m[15] - m[9] * m[3] * m[14] - m[13] * m[2] * m[11] + m[13] * m[3] * m[10]; - inv[5] = m[0] * m[10] * m[15] - m[0] * m[11] * m[14] - m[8] * m[2] * m[15] + m[8] * m[3] * m[14] + m[12] * m[2] * m[11] - m[12] * m[3] * m[10]; - inv[9] = -m[0] * m[9] * m[15] + m[0] * m[11] * m[13] + m[8] * m[1] * m[15] - m[8] * m[3] * m[13] - m[12] * m[1] * m[11] + m[12] * m[3] * m[9]; - inv[13] = m[0] * m[9] * m[14] - m[0] * m[10] * m[13] - m[8] * m[1] * m[14] + m[8] * m[2] * m[13] + m[12] * m[1] * m[10] - m[12] * m[2] * m[9]; - inv[2] = m[1] * m[6] * m[15] - m[1] * m[7] * m[14] - m[5] * m[2] * m[15] + m[5] * m[3] * m[14] + m[13] * m[2] * m[7] - m[13] * m[3] * m[6]; - inv[6] = -m[0] * m[6] * m[15] + m[0] * m[7] * m[14] + m[4] * m[2] * m[15] - m[4] * m[3] * m[14] - m[12] * m[2] * m[7] + m[12] * m[3] * m[6]; - inv[10] = m[0] * m[5] * m[15] - m[0] * m[7] * m[13] - m[4] * m[1] * m[15] + m[4] * m[3] * m[13] + m[12] * m[1] * m[7] - m[12] * m[3] * m[5]; - inv[14] = -m[0] * m[5] * m[14] + m[0] * m[6] * m[13] + m[4] * m[1] * m[14] - m[4] * m[2] * m[13] - m[12] * m[1] * m[6] + m[12] * m[2] * m[5]; - inv[3] = -m[1] * m[6] * m[11] + m[1] * m[7] * m[10] + m[5] * m[2] * m[11] - m[5] * m[3] * m[10] - m[9] * m[2] * m[7] + m[9] * m[3] * m[6]; - inv[7] = m[0] * m[6] * m[11] - m[0] * m[7] * m[10] - m[4] * m[2] * m[11] + m[4] * m[3] * m[10] + m[8] * m[2] * m[7] - m[8] * m[3] * m[6]; - inv[11] = -m[0] * m[5] * m[11] + m[0] * m[7] * m[9] + m[4] * m[1] * m[11] - m[4] * m[3] * m[9] - m[8] * m[1] * m[7] + m[8] * m[3] * m[5]; - inv[15] = m[0] * m[5] * m[10] - m[0] * m[6] * m[9] - m[4] * m[1] * m[10] + m[4] * m[2] * m[9] + m[8] * m[1] * m[6] - m[8] * m[2] * m[5]; - - float det = m[0] * inv[0] + m[1] * inv[4] + m[2] * inv[8] + m[3] * inv[12]; - - if (det == 0) { - memset(inv, 0, sizeof *inv); - } else { - det = 1 / det; - - for (int i = 0; i < 16; i++) - inv[i] *= det; - } - - return ret; -} - -v2i V2I(int x, int y) { - v2i v; - v.x = x; - v.y = y; - return v; + return (v2d) { + .x = x, + .y = y + }; } void rgba_u32_to_floats(u32 rgba, float floats[4]) { |