From 84621fa2815cf4c40687f46c55009ebfa5197aac Mon Sep 17 00:00:00 2001 From: luc Date: Thu, 28 Mar 2019 01:26:06 +0100 Subject: [PATCH] refactor --- Makefile | 12 +- exemple/exemple_geometry.c | 2 +- exemple/exemple_matrix.c | 6 +- exemple/exemple_stack.c | 38 -- sweet.h | 2 +- sweet_geometry.c | 74 +-- sweet_geometry.h | 33 +- sweet_macro.h | 35 + sweet_math.c | 1245 ++++++++++++++++++------------------ sweet_math.h | 335 +++++----- sweet_math_short.c | 54 -- sweet_math_short.h | 106 --- sweet_matrix.c | 596 ++++++++++------- sweet_matrix.h | 131 ++-- sweet_matrix_short.c | 179 ------ sweet_matrix_short.h | 99 --- sweet_matrix_stack.c | 199 ------ sweet_matrix_stack.h | 92 --- 18 files changed, 1341 insertions(+), 1897 deletions(-) delete mode 100644 exemple/exemple_stack.c create mode 100644 sweet_macro.h delete mode 100644 sweet_math_short.c delete mode 100644 sweet_math_short.h delete mode 100644 sweet_matrix_short.c delete mode 100644 sweet_matrix_short.h delete mode 100644 sweet_matrix_stack.c delete mode 100644 sweet_matrix_stack.h diff --git a/Makefile b/Makefile index fac5263..ce134ee 100644 --- a/Makefile +++ b/Makefile @@ -4,22 +4,22 @@ CC = gcc PROG = libsweet.so -OBJS = sweet_math.o sweet_matrix.o sweet_matrix_stack.o sweet_geometry.o -OBJS_WITH_SHORT = sweet_math_short.o sweet_matrix_short.o +OBJS = sweet_math.o sweet_matrix.o sweet_geometry.o CFLAGS = -pedantic -fPIC -Wall LD = -shared all: $(PROG) -$(PROG): $(OBJS) $(OBJS_WITH_SHORT) - $(CC) $(LD) $(OBJS) $(OBJS_WITH_SHORT) -o $(PROG) + +$(PROG): $(OBJS) + $(CC) $(LD) $(OBJS) -o $(PROG) .c.o: $(CC) $(CFLAGS) -c $*.c clean: - rm -rf $(OBJS) $(OBJS_WITH_SHORT) + rm -rf $(OBJS) mrproper: - rm -rf $(OBJS) $(OBJS_WITH_SHORT) $(PROG) + rm -rf $(OBJS) $(PROG) diff --git a/exemple/exemple_geometry.c b/exemple/exemple_geometry.c index 74609e7..0954009 100644 --- a/exemple/exemple_geometry.c +++ b/exemple/exemple_geometry.c @@ -13,7 +13,7 @@ main () unsigned int nb_vertices; float * vertices; unsigned int * indices; - nb_faces = sweet_geometry_icosphere (NB_ITERATIONS, SCALE, &nb_elements, &indices, &nb_vertices, &vertices); + nb_faces = math_icosphere (NB_ITERATIONS, SCALE, &nb_elements, &indices, &nb_vertices, &vertices); printf ("There are %d faces\n", nb_faces); diff --git a/exemple/exemple_matrix.c b/exemple/exemple_matrix.c index 65f281f..70f8fed 100644 --- a/exemple/exemple_matrix.c +++ b/exemple/exemple_matrix.c @@ -15,14 +15,14 @@ print4 (mat4 * m) int main () { - mat4 m = sweet_matrix_rotation3h (SWEET_PI_OVER_2, 1.0, 1.0, 1.0); + mat4 m = mat3h_rotation (PI_OVER_2, 1.0, 1.0, 1.0); print4 (&m); - sweet_matrix_inverse4 (&m, &m); + mat4_inverse (&m, &m); print4 (&m); - sweet_matrix_inverse4 (&m, &m); + mat4_inverse (&m, &m); print4 (&m); return 0; diff --git a/exemple/exemple_stack.c b/exemple/exemple_stack.c deleted file mode 100644 index 801d1fa..0000000 --- a/exemple/exemple_stack.c +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -#include "sweet.h" - -void -print4 (mat4 * m) -{ - printf ("%f %f %f %f\n", m->v[0], m->v[4], m->v[8], m->v[12]); - printf ("%f %f %f %f\n", m->v[1], m->v[5], m->v[9], m->v[13]); - printf ("%f %f %f %f\n", m->v[2], m->v[6], m->v[10], m->v[14]); - printf ("%f %f %f %f\n", m->v[3], m->v[7], m->v[11], m->v[15]); - printf ("\n"); -} - -int -main () -{ - matrix_stack4 ms = sweet_matrix_stack4_new (); - - print4 (sweet_matrix_stack4_get_matrix_pointer (&ms)); - - mat4 m = sweet_matrix_rotation3h (SWEET_PI_OVER_2, 1.0, 1.0, 1.0); - - sweet_matrix_stack4_mult (&ms, &m); - print4 (sweet_matrix_stack4_get_matrix_pointer (&ms)); - - sweet_matrix_stack4_push (&ms); - print4 (sweet_matrix_stack4_get_matrix_pointer (&ms)); - - sweet_matrix_stack4_mult (&ms, &m); - print4 (sweet_matrix_stack4_get_matrix_pointer (&ms)); - - sweet_matrix_stack4_pop (&ms); - print4 (sweet_matrix_stack4_get_matrix_pointer (&ms)); - - return 0; -} - diff --git a/sweet.h b/sweet.h index a16fa28..504ec3f 100644 --- a/sweet.h +++ b/sweet.h @@ -19,10 +19,10 @@ #ifndef SWEET_H #define SWEET_H +#include "sweet_macro.h" #include "sweet_types.h" #include "sweet_math.h" #include "sweet_matrix.h" -#include "sweet_matrix_stack.h" #include "sweet_geometry.h" #endif diff --git a/sweet_geometry.c b/sweet_geometry.c index 3436a07..3ef5f21 100644 --- a/sweet_geometry.c +++ b/sweet_geometry.c @@ -19,6 +19,7 @@ #include #include #include "sweet_types.h" +#include "sweet_macro.h" #include "sweet_math.h" /* Definition of chain list for faces */ @@ -219,9 +220,9 @@ middle_point_v (struct map * map, int * state, if (map_add_item (map, hash, a, b, id) == -1) { *state = -1; return 0; } *state = 1; - middle = sweet_vector_middle3 (v->vertices[a], v->vertices[b]); + middle = vec3_middle (v->vertices[a], v->vertices[b]); - v->vertices[v->nb_vertices++] = sweet_vector_normalize3 (middle); + v->vertices[v->nb_vertices++] = vec3_normalize (middle); return id; } @@ -234,7 +235,7 @@ middle_point_vt (struct map * map, int * state, if (*state == 1) { - t->uv[t->nb_uv++] = sweet_vector_middle2 (t->uv[a], t->uv[b]); + t->uv[t->nb_uv++] = vec2_middle (t->uv[a], t->uv[b]); } return id; } @@ -251,7 +252,7 @@ first_iteration (struct vertex_array * v, v->nb_vertices = nb_vertices; for (i = 0, j = 0; i < v->nb_vertices; i++, j += 3) { - v->vertices[i] = sweet_vector_normalize3 (sweet_vector_new3 (vertices[j], vertices[j+1], vertices[j+2])); + v->vertices[i] = vec3_normalize (vec3_new (vertices[j], vertices[j+1], vertices[j+2])); } root = NULL; @@ -274,7 +275,7 @@ first_iteration_vt (struct vertex_array * v, struct tcoord_array * t, t->nb_uv = nb_vertices; for (i = 0, j = 0; i < t->nb_uv; i++, j += 2) { - t->uv[i] = sweet_vector_new2 (tcoord[j], tcoord[j+1]); + t->uv[i] = vec2_new (tcoord[j], tcoord[j+1]); } return first_iteration (v, nb_indices, indices, nb_vertices, vertices); @@ -373,11 +374,11 @@ iterate (struct map * map, struct vertex_array * v, struct tcoord_array * t, str } int -sweet_geometry_subdivide_mesh_vt (unsigned int nb_iterations, float scale, - unsigned int nb_indices, unsigned int * indices, - unsigned int nb_vertices, float * vcoord, float * tcoord, - unsigned int * count_indices, unsigned int ** mesh_indices, - unsigned int * count_vertices, float ** mesh_vertices, float ** mesh_tcoord) +subdivide_mesh_vt (unsigned int nb_iterations, float scale, + unsigned int nb_indices, unsigned int * indices, + unsigned int nb_vertices, float * vcoord, float * tcoord, + unsigned int * count_indices, unsigned int ** mesh_indices, + unsigned int * count_vertices, float ** mesh_vertices, float ** mesh_tcoord) { struct face_item * root; unsigned int nb_faces; @@ -431,11 +432,11 @@ sweet_geometry_subdivide_mesh_vt (unsigned int nb_iterations, float scale, } int -sweet_geometry_subdivide_mesh_v (unsigned int nb_iterations, float scale, - unsigned int nb_indices, unsigned int * indices, - unsigned int nb_vertices, float * vcoord, - unsigned int * count_indices, unsigned int ** mesh_indices, - unsigned int * count_vertices, float ** mesh_vertices) +subdivide_mesh_v (unsigned int nb_iterations, float scale, + unsigned int nb_indices, unsigned int * indices, + unsigned int nb_vertices, float * vcoord, + unsigned int * count_indices, unsigned int ** mesh_indices, + unsigned int * count_vertices, float ** mesh_vertices) { struct face_item * root; unsigned int nb_faces; @@ -481,20 +482,20 @@ sweet_geometry_subdivide_mesh_v (unsigned int nb_iterations, float scale, #define NB_VERTICES 12 static float icosphere_vertices_v[36] = { - -1, SWEET_GOLDEN_RATIO, 0, - 1, SWEET_GOLDEN_RATIO, 0, - -1, -SWEET_GOLDEN_RATIO, 0, - 1, -SWEET_GOLDEN_RATIO, 0, + -1, GOLDEN_RATIO, 0, + 1, GOLDEN_RATIO, 0, + -1, -GOLDEN_RATIO, 0, + 1, -GOLDEN_RATIO, 0, - 0, -1, SWEET_GOLDEN_RATIO, - 0, 1, SWEET_GOLDEN_RATIO, - 0, -1, -SWEET_GOLDEN_RATIO, - 0, 1, -SWEET_GOLDEN_RATIO, + 0, -1, GOLDEN_RATIO, + 0, 1, GOLDEN_RATIO, + 0, -1, -GOLDEN_RATIO, + 0, 1, -GOLDEN_RATIO, - SWEET_GOLDEN_RATIO, 0, -1, - SWEET_GOLDEN_RATIO, 0, 1, - -SWEET_GOLDEN_RATIO, 0, -1, - -SWEET_GOLDEN_RATIO, 0, 1 + GOLDEN_RATIO, 0, -1, + GOLDEN_RATIO, 0, 1, + -GOLDEN_RATIO, 0, -1, + -GOLDEN_RATIO, 0, 1 }; static unsigned int icosphere_indices_v[NB_INDICES] = { @@ -525,14 +526,15 @@ static unsigned int icosphere_indices_v[NB_INDICES] = { int -sweet_geometry_icosphere (unsigned int nb_iterations, float scale, - unsigned int * count_indices, unsigned int ** mesh_indices, - unsigned int * count_vertices, float ** mesh_vertices) +mesh_icosphere (unsigned int nb_iterations, float scale, + unsigned int * count_indices, unsigned int ** mesh_indices, + unsigned int * count_vertices, float ** mesh_vertices) { - return sweet_geometry_subdivide_mesh_v (nb_iterations, scale, - NB_INDICES, icosphere_indices_v, NB_VERTICES, icosphere_vertices_v, - count_indices, mesh_indices, count_vertices, mesh_vertices); + return subdivide_mesh_v (nb_iterations, scale, + NB_INDICES, icosphere_indices_v, NB_VERTICES, icosphere_vertices_v, + count_indices, mesh_indices, count_vertices, mesh_vertices); } + #undef NB_INDICES #undef NB_VERTICES @@ -567,9 +569,9 @@ static unsigned int cube_indices_v[NB_INDICES] = { int -sweet_geometry_cube (float scale, - unsigned int * count_indices, unsigned int ** mesh_indices, - unsigned int * count_vertices, float ** mesh_vertices) +mesh_cube (float scale, + unsigned int * count_indices, unsigned int ** mesh_indices, + unsigned int * count_vertices, float ** mesh_vertices) { int i; diff --git a/sweet_geometry.h b/sweet_geometry.h index 3feb02b..af96646 100644 --- a/sweet_geometry.h +++ b/sweet_geometry.h @@ -20,26 +20,25 @@ #define SWEET_GEOMETRY_H -int sweet_geometry_subdivide_mesh_v (unsigned int nb_iterations, float scale, - unsigned int nb_indices, unsigned int * indices, - unsigned int nb_vertices, float * vcoord, - unsigned int * count_indices, unsigned int ** mesh_indices, - unsigned int * count_vertices, float ** mesh_vertices); +int subdivide_mesh_v (unsigned int nb_iterations, float scale, + unsigned int nb_indices, unsigned int * indices, + unsigned int nb_vertices, float * vcoord, + unsigned int * count_indices, unsigned int ** mesh_indices, + unsigned int * count_vertices, float ** mesh_vertices); -int sweet_geometry_subdivide_mesh_vt (unsigned int nb_iterations, float scale, - unsigned int nb_indices, unsigned int * indices, - unsigned int nb_vertices, float * vcoord, float * tcoord, - unsigned int * count_indices, unsigned int ** mesh_indices, - unsigned int * count_vertices, float ** mesh_vertices, float ** mesh_tcoord); +int subdivide_mesh_vt (unsigned int nb_iterations, float scale, + unsigned int nb_indices, unsigned int * indices, + unsigned int nb_vertices, float * vcoord, float * tcoord, + unsigned int * count_indices, unsigned int ** mesh_indices, + unsigned int * count_vertices, float ** mesh_vertices, float ** mesh_tcoord); -int sweet_geometry_icosphere (unsigned int nb_iterations, float scale, - unsigned int * count_indices, unsigned int ** mesh_indices, - unsigned int * count_vertices, float ** mesh_vertices); +int mesh_icosphere (unsigned int nb_iterations, float scale, + unsigned int * count_indices, unsigned int ** mesh_indices, + unsigned int * count_vertices, float ** mesh_vertices); -int -sweet_geometry_cube (float scale, - unsigned int * count_indices, unsigned int ** mesh_indices, - unsigned int * count_vertices, float ** mesh_vertices); +int mesh_cube (float scale, + unsigned int * count_indices, unsigned int ** mesh_indices, + unsigned int * count_vertices, float ** mesh_vertices); #endif diff --git a/sweet_macro.h b/sweet_macro.h new file mode 100644 index 0000000..c4dc6f3 --- /dev/null +++ b/sweet_macro.h @@ -0,0 +1,35 @@ +/* + * Sweet is a small library for basic math and small matrix operations. + * Copyright 2014 Luc Girod. + * + * This library is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ + +#ifndef SWEET_MACRO +#define SWEET_MACRO + +#define _OVERLOAD_2_(_1, _2, NAME, ...) NAME +#define _OVERLOAD_3_(_1, _2, _3, NAME, ...) NAME +#define _OVERLOAD_4_(_1, _2, _3, _4, NAME, ...) NAME + +#define vec2_new(...) _OVERLOAD_2_(__VA_ARGS__, vec2_new_2f, vec2_new_v3, NULL)(__VA_ARGS__) +#define vec3_new(...) _OVERLOAD_3_(__VA_ARGS__, vec3_new_3f, vec3_new_v2_1f, vec3_new_v4, NULL)(__VA_ARGS__) +#define vec4_new(...) _OVERLOAD_4_(__VA_ARGS__, vec4_new_4f, vec4_new_v2_2f, vec4_new_v3_1f, vec4_new_v4, NULL)(__VA_ARGS__) + +#define mat2_new(...) _OVERLOAD_4_(__VA_ARGS__, mat2_new_4f, mat2_new_3f, mat2_new_2v, mat2_new_m3, NULL)(__VA_ARGS__) +#define mat3_new(...) _OVERLOAD_3_(__VA_ARGS__, mat3_new_3v, mat3_new_2v, mat3_new_m4, NULL)(__VA_ARGS__) +#define mat4_new(...) _OVERLOAD_4_(__VA_ARGS__, mat4_new_4v, mat4_new_3v, mat4_new_2v, mat4_new_m4, NULL)(__VA_ARGS__) + +#endif + diff --git a/sweet_math.c b/sweet_math.c index 869b324..9b652f9 100644 --- a/sweet_math.c +++ b/sweet_math.c @@ -23,10 +23,637 @@ #include "sweet_math.h" #include "sweet_types.h" -#define EPSILON 0.0000000001 + +vec2 +vec2_zero () +{ + vec2 v; + v.x = 0; + v.y = 0; + return v; +} + +vec3 +vec3_zero () +{ + vec3 v; + v.x = 0; + v.y = 0; + return v; +} + +vec4 +vec4_zero () +{ + vec4 v; + v.x = 0; + v.y = 0; + return v; +} + +vec2 +vec2_new_2f (float a, float b) +{ + vec2 v; + v.x = a; + v.y = b; + return v; +} + +vec3 +vec3_new_3f (float a, float b, float c) +{ + vec3 v; + v.x = a; + v.y = b; + v.z = c; + return v; +} + +vec4 +vec4_new_4f (float a, float b, float c, float d) +{ + vec4 v; + v.x = a; + v.y = b; + v.z = c; + v.w = d; + + return v; +} + +vec2 vec2_new_v3(vec3 w) +{ + vec2 v; + v.x = w.x; + v.y = w.y; + return v; +} + +vec3 vec3_new_v2_1f(vec2 w, float z) +{ + vec3 v; + v.x = w.x; + v.y = w.y; + v.z = z; + return v; +} + +vec3 vec3_new_v4(vec4 w) +{ + vec3 v; + v.x = w.x; + v.y = w.y; + v.z = w.z; + return v; +} + +vec4 vec4_new_v2_2f(vec2 u, float z, float w) +{ + vec4 v; + v.x = u.x; + v.y = u.y; + v.z = z; + v.w = w; + return v; +} + +vec4 vec4_new_v3_1f(vec3 u, float w) +{ + vec4 v; + v.x = u.x; + v.y = u.y; + v.z = u.z; + v.w = w; + return v; +} + +vec4 vec4_new_v4 (vec4 v) +{ + return v; +} + + +float +vec2_norm (vec2 v) +{ + return sqrt (v.x * v.x + v.y * v.y); +} + +float +vec3_norm (vec3 v) +{ + return sqrt (v.x * v.x + v.y * v.y + v.z * v.z); +} + +float +vec4_norm (vec4 v) +{ + return sqrt (v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w); +} + +float +vec2_square_norm (vec2 v) +{ + return (v.x * v.x + v.y * v.y); +} + +float +vec3_square_norm (vec3 v) +{ + return (v.x * v.x + v.y * v.y + v.z * v.z); +} + +float +vec4_square_norm (vec4 v) +{ + return (v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w); +} + +float +vect2_dist (vec2 a, vec2 b) +{ + float dx = a.x - b.x; + float dy = a.y - b.y; + return sqrt (dx * dx + dy * dy); +} + +float +vec3_dist (vec3 a, vec3 b) +{ + float dx = a.x - b.x; + float dy = a.y - b.y; + float dz = a.z - b.z; + return sqrt (dx * dx + dy * dy + dz * dz); +} + +float +vec4_dist (vec4 a, vec4 b) +{ + float dx = a.x - b.x; + float dy = a.y - b.y; + float dz = a.z - b.z; + return sqrt (dx * dx + dy * dy + dz * dz); +} + +float +vec2_square_dist (vec2 a, vec2 b) +{ + float dx = a.x - b.x; + float dy = a.y - b.y; + return (dx * dx + dy * dy); +} + +float +vec3_square_dist (vec3 a, vec3 b) +{ + float dx = a.x - b.x; + float dy = a.y - b.y; + float dz = a.z - b.z; + return (dx * dx + dy * dy + dz * dz); +} + +float +vec4_square_dist (vec4 a, vec4 b) +{ + float dx = a.x - b.x; + float dy = a.y - b.y; + float dz = a.z - b.z; + return (dx * dx + dy * dy + dz * dz); +} + +float +vec2_dot (vec2 a, vec2 b) +{ + return (a.x * b.x) + (a.y * b.y); +} + +float +vec3_dot (vec3 a, vec3 b) +{ + return (a.x * b.x) + (a.y * b.y) + (a.z * b.z); +} + +float +vec4_dot (vec4 a, vec4 b) +{ + return (a.x * b.x) + (a.y * b.y) + (a.z * b.z) + (a.w * b.w); +} + +vec3 +cross (vec3 v1, vec3 v2) +{ + vec3 n; + n.x = (v1.y * v2.z) - (v1.z * v2.y); + n.y = -(v1.x * v2.z) + (v1.z * v2.x); + n.z = (v1.x * v2.y) - (v1.y * v2.x); + + return n; +} + +float +triple_product (vec3 v1, vec3 v2, vec3 v3) +{ + v2 = cross (v2, v3); + + return vec3_dot (v1, v2); +} + +vec2 +vec2_normalize (vec2 v) +{ + vec2 nv; + + float inv_size = 1.0 / sqrt ((v.x * v.x) + (v.y * v.y)); + + nv.x = v.x * inv_size; + nv.y = v.y * inv_size; + + return nv; +} + +vec3 +vec3_normalize (vec3 v) +{ + vec3 nv; + + float inv_size = 1.0 / sqrt ((v.x * v.x) + (v.y * v.y) + (v.z * v.z)); + + nv.x = v.x * inv_size; + nv.y = v.y * inv_size; + nv.z = v.z * inv_size; + + return nv; +} + +vec4 +vec4_normalize (vec4 v) +{ + vec4 nv; + + float inv_size = 1.0 / sqrt ((v.x * v.x) + (v.y * v.y) + (v.z * v.z) + (v.w * v.w)); + + nv.x = v.x * inv_size; + nv.y = v.y * inv_size; + nv.z = v.z * inv_size; + nv.w = v.w * inv_size; + + return nv; +} + + +vec2 +vec2_scale (vec2 v, float s) +{ + v.x *= s; + v.y *= s; + + return v; +} + +vec3 +vec3_scale (vec3 v, float s) +{ + v.x *= s; + v.y *= s; + v.z *= s; + + return v; +} + +vec4 +vec4_scale (vec4 v, float s) +{ + v.x *= s; + v.y *= s; + v.z *= s; + v.w *= s; + + return v; +} + +vec2 +vec2_rescale (vec2 v, float size) +{ + v = vec2_normalize (v); + v = vec2_scale (v, size); + + return v; +} + +vec3 +vec3_rescale (vec3 v, float size) +{ + v = vec3_normalize (v); + v = vec3_scale (v, size); + + return v; +} + +vec4 +vec4_rescale (vec4 v, float size) +{ + v = vec4_normalize (v); + v = vec4_scale (v, size); + + return v; +} + +vec2 +vec2_inverse (vec2 v) +{ + v.x = - v.x; + v.y = - v.y; + return v; +} + +vec3 +vec3_inverse (vec3 v) +{ + v.x = - v.x; + v.y = - v.y; + v.z = - v.z; + return v; +} + +vec4 +vec4_inverse (vec4 v) +{ + v.x = - v.x; + v.y = - v.y; + v.z = - v.z; + v.w = - v.w; + return v; +} + +vec2 +vec2_add (vec2 v1, vec2 v2) +{ + v1.x += v2.x; + v1.y += v2.y; + + return v1; +} + +vec3 +vec3_add (vec3 v1, vec3 v2) +{ + v1.x += v2.x; + v1.y += v2.y; + v1.z += v2.z; + + return v1; +} + +vec4 +vec4_add (vec4 v1, vec4 v2) +{ + v1.x += v2.x; + v1.y += v2.y; + v1.z += v2.z; + v1.w += v2.w; + + return v1; +} + +vec2 +vec2_middle (vec2 vector1, vec2 vector2) +{ + vec2 v; + v.x = (vector1.x + vector2.x) * 0.5; + v.y = (vector1.y + vector2.y) * 0.5; + return v; +} + +vec3 +vec3_middle (vec3 vector1, vec3 vector2) +{ + vec3 v; + v.x = (vector1.x + vector2.x) * 0.5; + v.y = (vector1.y + vector2.y) * 0.5; + v.z = (vector1.z + vector2.z) * 0.5; + return v; +} + +vec4 +vec4_middle (vec4 vector1, vec4 vector2) +{ + vec4 v; + v.x = (vector1.x + vector2.x) * 0.5; + v.y = (vector1.y + vector2.y) * 0.5; + v.z = (vector1.z + vector2.z) * 0.5; + v.w = (vector1.w + vector2.w) * 0.5; + return v; +} + +vec2 +vec2_sub (vec2 v1, vec2 v2) +{ + v1.x -= v2.x; + v1.y -= v2.y; + + return v1; +} + +vec3 +vec3_sub (vec3 v1, vec3 v2) +{ + v1.x -= v2.x; + v1.y -= v2.y; + v1.z -= v2.z; + + return v1; +} + +vec4 +vec4_sub (vec4 v1, vec4 v2) +{ + v1.x -= v2.x; + v1.y -= v2.y; + v1.z -= v2.z; + v1.w -= v2.w; + + return v1; +} + +float +vec2_angle (vec2 a, vec2 b) +{ + vec2 na = vec2_normalize (a); + vec2 nb = vec2_normalize (b); + + float dot = vec2_dot (na, nb); + + return acos (dot); +} + +float +vec3_angle (vec3 a, vec3 b) +{ + vec3 na = vec3_normalize (a); + vec3 nb = vec3_normalize (b); + + float dot = vec3_dot (na, nb); + + return acos (dot); +} + +vec2 +vec2_random () +{ + vec2 v; + v.x = (float)rand() / (float)RAND_MAX; + v.y = (float)rand() / (float)RAND_MAX; + v.x = 2.0 * (v.x - 0.5); + v.y = 2.0 * (v.y - 0.5); + return vec2_normalize (v); +} + +vec3 +vec3_random () +{ + vec3 v; + v.x = (float)rand() / (float)RAND_MAX; + v.y = (float)rand() / (float)RAND_MAX; + v.z = (float)rand() / (float)RAND_MAX; + + v.x = 2.0 * (v.x - 0.5); + v.y = 2.0 * (v.y - 0.5); + v.z = 2.0 * (v.z - 0.5); + return vec3_normalize (v); +} + +vec4 +vec4_random () +{ + vec4 v; + v.x = (float)rand() / (float)RAND_MAX; + v.y = (float)rand() / (float)RAND_MAX; + v.z = (float)rand() / (float)RAND_MAX; + v.w = (float)rand() / (float)RAND_MAX; + + v.x = 2.0 * (v.x - 0.5); + v.y = 2.0 * (v.y - 0.5); + v.z = 2.0 * (v.z - 0.5); + v.w = 2.0 * (v.w - 0.5); + return vec4_normalize (v); +} + +vec3 +vec3_random_ortho(vec3 v) +{ + vec3 u = vec3_random (); + v = cross(u, v); + return vec3_normalize(v); +} + +vec2 +vec2_product (vec2 v1, vec2 v2) +{ + v1.x *= v2.x; + v1.y *= v2.y; + + return v1; +} + +vec3 +vec3_product (vec3 v1, vec3 v2) +{ + v1.x *= v2.x; + v1.y *= v2.y; + v1.z *= v2.z; + + return v1; +} + +vec4 +vec4_product (vec4 v1, vec4 v2) +{ + v1.x *= v2.x; + v1.y *= v2.y; + v1.z *= v2.z; + v1.w *= v2.w; + + return v1; +} + +quaternion +quat_new (float w, float x, float y, float z) +{ + quaternion q; + q.w = w; + q.x = x; + q.y = y; + q.z = z; + + return q; +} + +quaternion +quat_rotation (float angle, float x, float y, float z) +{ + quaternion q; + float s; + + s = sin (0.5 * angle); + + q.w = cos (0.5 * angle); + q.x = x * s; + q.y = y * s; + q.z = z * s; + + return q; +} + +quaternion +quat_conjugate (quaternion q) +{ + quaternion conjugate; + + conjugate.x = -q.x; + conjugate.y = -q.y; + conjugate.z = -q.z; + conjugate.w = q.w; + + return conjugate; +} + +quaternion +quat_add (quaternion q1, quaternion q2) +{ + quaternion sum; + + sum.w = q1.w + q2.w; + sum.x = q1.x + q2.x; + sum.y = q1.y + q2.y; + sum.z = q1.z + q2.z; + + return sum; +} + +quaternion +quat_product (quaternion q1, quaternion q2) +{ + quaternion prod; + + prod.w = (q1.w * q2.w) - (q1.x * q2.x) - (q1.y * q2.y) - (q1.z * q2.z); + prod.x = (q1.w * q2.x) + (q1.x * q2.w) + (q1.y * q2.z) - (q1.z * q2.y); + prod.y = (q1.w * q2.y) - (q1.x * q2.z) + (q1.y * q2.w) + (q1.z * q2.x); + prod.z = (q1.w * q2.z) + (q1.x * q2.y) - (q1.y * q2.x) + (q1.z * q2.w); + + return prod; +} + +float +quat_norm (quaternion q) +{ + return sqrt ((q.w * q.w) + (q.x * q.x) + (q.y * q.y) + (q.z * q.z)); +} + + int -sweet_math_nearest (int number, int size, ...) +nearest_integer (int number, int size, ...) { int i; int nearest; @@ -53,7 +680,7 @@ sweet_math_nearest (int number, int size, ...) } int -sweet_math_quadratic_polynomial (vec2 * r, float a, float b, float c) +quadratic_polynomial (vec2 * r, float a, float b, float c) { float delta; float a2; @@ -68,7 +695,7 @@ sweet_math_quadratic_polynomial (vec2 * r, float a, float b, float c) r->y = (-d - b)/a2; return 2; } - else if (sweet_math_approx_equals (delta, 0, EPSILON)) + else if (float_approx_zero (delta, EPSILON)) { r->x = -b/a2; r->y = r->x; @@ -86,7 +713,7 @@ sweet_math_quadratic_polynomial (vec2 * r, float a, float b, float c) #define FRAC_1_ON_27 0.0370370370370370370370370370370370370370370370370370370370370370370 int -sweet_math_cubic_polynomial (vec3 * roots, float t, float a, float b, float c) +cubic_polynomial (vec3 * roots, float t, float a, float b, float c) { double aa; double a_over_3; @@ -122,7 +749,7 @@ sweet_math_cubic_polynomial (vec3 * roots, float t, float a, float b, float c) roots->z = roots->y; return 1; } - else if (sweet_math_approx_equals (D, 0, EPSILON)) + else if (float_approx_zero (D, EPSILON)) { roots->x = r * 2 - a_over_3; roots->y = -r - a_over_3; @@ -139,613 +766,11 @@ sweet_math_cubic_polynomial (vec3 * roots, float t, float a, float b, float c) theta = FRAC_1_ON_3 * acos (-q / sqrt(-ppp)); roots->x = m2 * cos (theta) - a_over_3; - roots->y = m2 * cos (theta + 2 * SWEET_PI_OVER_3) - a_over_3; - roots->z = m2 * cos (theta - 2 * SWEET_PI_OVER_3) - a_over_3; + roots->y = m2 * cos (theta + 2 * PI_OVER_3) - a_over_3; + roots->z = m2 * cos (theta - 2 * PI_OVER_3) - a_over_3; return 3; } return 0; } -vec2 -sweet_vector_new2 (float a, float b) -{ - vec2 v; - v.x = a; - v.y = b; - return v; -} - -vec3 -sweet_vector_new3 (float a, float b, float c) -{ - vec3 v; - v.x = a; - v.y = b; - v.z = c; - return v; -} - -vec4 -sweet_vector_new4 (float a, float b, float c, float d) -{ - vec4 v; - v.x = a; - v.y = b; - v.z = c; - v.w = d; - - return v; -} - -float -sweet_vector_norm2 (vec2 v) -{ - return sqrt (v.x * v.x + v.y * v.y); -} - -float -sweet_vector_norm3 (vec3 v) -{ - return sqrt (v.x * v.x + v.y * v.y + v.z * v.z); -} - -float -sweet_vector_norm4 (vec4 v) -{ - return sqrt (v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w); -} - -float -sweet_vector_norm2h (vec3 v) -{ - v.x = v.x / v.z; - v.y = v.y / v.z; - return sqrt ((v.x * v.x) + (v.y * v.y)); -} - -float -sweet_vector_norm3h (vec4 v) -{ - v.x = v.x / v.w; - v.y = v.y / v.w; - v.z = v.z / v.w; - return sqrt ((v.x * v.x) + (v.y * v.y) + (v.z * v.z)); -} - -float -sweet_vector_square_norm2 (vec2 v) -{ - return (v.x * v.x + v.y * v.y); -} - -float -sweet_vector_square_norm3 (vec3 v) -{ - return (v.x * v.x + v.y * v.y + v.z * v.z); -} - -float -sweet_vector_square_norm4 (vec4 v) -{ - return (v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w); -} - -float -sweet_vector_square_norm2h (vec3 v) -{ - v.x = v.x / v.z; - v.y = v.y / v.z; - return (v.x * v.x + v.y * v.y); -} - -float -sweet_vector_square_norm3h (vec4 v) -{ - v.x = v.x / v.w; - v.y = v.y / v.w; - v.z = v.z / v.w; - return (v.x * v.x + v.y * v.y + v.z * v.z); -} - -float -sweet_vector_dist2 (vec2 a, vec2 b) -{ - float dx = a.x - b.x; - float dy = a.y - b.y; - return sqrt (dx * dx + dy * dy); -} - -float -sweet_vector_dist3 (vec3 a, vec3 b) -{ - float dx = a.x - b.x; - float dy = a.y - b.y; - float dz = a.z - b.z; - return sqrt (dx * dx + dy * dy + dz * dz); -} - -float -sweet_vector_dist4 (vec4 a, vec4 b) -{ - float dx = a.x - b.x; - float dy = a.y - b.y; - float dz = a.z - b.z; - return sqrt (dx * dx + dy * dy + dz * dz); -} - -float -sweet_vector_square_dist2 (vec2 a, vec2 b) -{ - float dx = a.x - b.x; - float dy = a.y - b.y; - return (dx * dx + dy * dy); -} - -float -sweet_vector_square_dist3 (vec3 a, vec3 b) -{ - float dx = a.x - b.x; - float dy = a.y - b.y; - float dz = a.z - b.z; - return (dx * dx + dy * dy + dz * dz); -} - -float -sweet_vector_square_dist4 (vec4 a, vec4 b) -{ - float dx = a.x - b.x; - float dy = a.y - b.y; - float dz = a.z - b.z; - return (dx * dx + dy * dy + dz * dz); -} - -float -sweet_vector_dot2 (vec2 a, vec2 b) -{ - return (a.x * b.x) + (a.y * b.y); -} - -float -sweet_vector_dot3 (vec3 a, vec3 b) -{ - return (a.x * b.x) + (a.y * b.y) + (a.z * b.z); -} - -float -sweet_vector_dot4 (vec4 a, vec4 b) -{ - return (a.x * b.x) + (a.y * b.y) + (a.z * b.z) + (a.w * b.w); -} - -float -sweet_vector_dot2h (vec3 a, vec3 b) -{ - a.x = a.x / a.z; - a.y = a.y / a.z; - - b.x = b.x / b.z; - b.y = b.y / b.z; - return (a.x * b.x) + (a.y * b.y); -} - -float -sweet_vector_dot3h (vec4 a, vec4 b) -{ - a.x = a.x / a.w; - a.y = a.y / a.w; - a.z = a.z / a.w; - - b.x = b.x / b.w; - b.y = b.y / b.w; - b.z = b.z / b.w; - return (a.x * b.x) + (a.y * b.y) + (a.z * b.z); -} - -vec3 -sweet_vector_cross (vec3 v1, vec3 v2) -{ - vec3 n; - n.x = (v1.y * v2.z) - (v1.z * v2.y); - n.y = -(v1.x * v2.z) + (v1.z * v2.x); - n.z = (v1.x * v2.y) - (v1.y * v2.x); - - return n; -} - -vec4 -sweet_vector_crossh (vec4 v1, vec4 v2) -{ - vec4 n; - v1.x = v1.x / v1.w; - v1.y = v1.y / v1.w; - v1.z = v1.z / v1.w; - - v2.x = v2.x / v2.w; - v2.y = v2.y / v2.w; - v2.z = v2.z / v2.w; - - n.x = (v1.y * v2.z) - (v1.z * v2.y); - n.y = -(v1.x * v2.z) + (v1.z * v2.x); - n.z = (v1.x * v2.y) - (v1.y * v2.x); - n.w = 1.0; - - return n; -} - -float -sweet_vector_triple_product (vec3 v1, vec3 v2, vec3 v3) -{ - v2 = sweet_vector_cross (v2, v3); - - return sweet_vector_dot3 (v1, v2); -} - -float -sweet_vector_triple_producth (vec4 v1, vec4 v2, vec4 v3) -{ - v2 = sweet_vector_crossh (v2, v3); - - return sweet_vector_dot3h (v1, v2); -} - -vec2 -sweet_vector_normalize2 (vec2 v) -{ - vec2 nv; - - float inv_size = 1.0 / sqrt ((v.x * v.x) + (v.y * v.y)); - - nv.x = v.x * inv_size; - nv.y = v.y * inv_size; - - return nv; -} - -vec3 -sweet_vector_normalize3 (vec3 v) -{ - vec3 nv; - - float inv_size = 1.0 / sqrt ((v.x * v.x) + (v.y * v.y) + (v.z * v.z)); - - nv.x = v.x * inv_size; - nv.y = v.y * inv_size; - nv.z = v.z * inv_size; - - return nv; -} - -vec4 -sweet_vector_normalize4 (vec4 v) -{ - vec4 nv; - - float inv_size = 1.0 / sqrt ((v.x * v.x) + (v.y * v.y) + (v.z * v.z) + (v.w * v.w)); - - nv.x = v.x * inv_size; - nv.y = v.y * inv_size; - nv.z = v.z * inv_size; - nv.w = v.w * inv_size; - - return nv; -} - - -vec2 -sweet_vector_scale2 (vec2 v, float s) -{ - v.x *= s; - v.y *= s; - - return v; -} - -vec3 -sweet_vector_scale3 (vec3 v, float s) -{ - v.x *= s; - v.y *= s; - v.z *= s; - - return v; -} - -vec4 -sweet_vector_scale4 (vec4 v, float s) -{ - v.x *= s; - v.y *= s; - v.z *= s; - v.w *= s; - - return v; -} - -vec2 -sweet_vector_rescale2 (vec2 v, float size) -{ - v = sweet_vector_normalize2 (v); - v = sweet_vector_scale2 (v, size); - - return v; -} - -vec3 -sweet_vector_rescale3 (vec3 v, float size) -{ - v = sweet_vector_normalize3 (v); - v = sweet_vector_scale3 (v, size); - - return v; -} - -vec4 -sweet_vector_rescale4 (vec4 v, float size) -{ - v = sweet_vector_normalize4 (v); - v = sweet_vector_scale4 (v, size); - - return v; -} - -vec2 -sweet_vector_inverse2 (vec2 v) -{ - v.x = - v.x; - v.y = - v.y; - return v; -} - -vec3 -sweet_vector_inverse3 (vec3 v) -{ - v.x = - v.x; - v.y = - v.y; - v.z = - v.z; - return v; -} - -vec4 -sweet_vector_inverse4 (vec4 v) -{ - v.x = - v.x; - v.y = - v.y; - v.z = - v.z; - v.w = - v.w; - return v; -} - -vec2 -sweet_vector_add2 (vec2 v1, vec2 v2) -{ - v1.x += v2.x; - v1.y += v2.y; - - return v1; -} - -vec3 -sweet_vector_add3 (vec3 v1, vec3 v2) -{ - v1.x += v2.x; - v1.y += v2.y; - v1.z += v2.z; - - return v1; -} - -vec4 -sweet_vector_add4 (vec4 v1, vec4 v2) -{ - v1.x += v2.x; - v1.y += v2.y; - v1.z += v2.z; - v1.w += v2.w; - - return v1; -} - -vec2 -sweet_vector_middle2 (vec2 vector1, vec2 vector2) -{ - vec2 v; - v.x = (vector1.x + vector2.x) * 0.5; - v.y = (vector1.y + vector2.y) * 0.5; - return v; -} - -vec3 -sweet_vector_middle3 (vec3 vector1, vec3 vector2) -{ - vec3 v; - v.x = (vector1.x + vector2.x) * 0.5; - v.y = (vector1.y + vector2.y) * 0.5; - v.z = (vector1.z + vector2.z) * 0.5; - return v; -} - -vec4 -sweet_vector_middle4 (vec4 vector1, vec4 vector2) -{ - vec4 v; - v.x = (vector1.x + vector2.x) * 0.5; - v.y = (vector1.y + vector2.y) * 0.5; - v.z = (vector1.z + vector2.z) * 0.5; - v.w = (vector1.w + vector2.w) * 0.5; - return v; -} - -vec2 -sweet_vector_sub2 (vec2 v1, vec2 v2) -{ - v1.x -= v2.x; - v1.y -= v2.y; - - return v1; -} - -vec3 -sweet_vector_sub3 (vec3 v1, vec3 v2) -{ - v1.x -= v2.x; - v1.y -= v2.y; - v1.z -= v2.z; - - return v1; -} - -vec4 -sweet_vector_sub4 (vec4 v1, vec4 v2) -{ - v1.x -= v2.x; - v1.y -= v2.y; - v1.z -= v2.z; - v1.w -= v2.w; - - return v1; -} - -float -sweet_vector_angle2 (vec2 a, vec2 b) -{ - vec2 na = sweet_vector_normalize2 (a); - vec2 nb = sweet_vector_normalize2 (b); - - float dot = sweet_vector_dot2 (na, nb); - - return acos (dot); -} - -float -sweet_vector_angle3 (vec3 a, vec3 b) -{ - vec3 na = sweet_vector_normalize3 (a); - vec3 nb = sweet_vector_normalize3 (b); - - float dot = sweet_vector_dot3 (na, nb); - - return acos (dot); -} - -vec3 -sweet_vector_normalized_random () -{ - vec3 v; - v.x = (float)rand() / (float)RAND_MAX; - v.y = (float)rand() / (float)RAND_MAX; - v.z = (float)rand() / (float)RAND_MAX; - return sweet_vector_normalize3 (v); -} - -vec3 -sweet_vector_random_ortho(vec3 v) -{ - vec3 u = sweet_vector_normalized_random (); - v = sweet_vector_cross(u, v); - return sweet_vector_normalize3(v); -} - -vec2 -sweet_vector_product2 (vec2 v1, vec2 v2) -{ - v1.x *= v2.x; - v1.y *= v2.y; - - return v1; -} - -vec3 -sweet_vector_product3 (vec3 v1, vec3 v2) -{ - v1.x *= v2.x; - v1.y *= v2.y; - v1.z *= v2.z; - - return v1; -} - -vec4 -sweet_vector_product4 (vec4 v1, vec4 v2) -{ - v1.x *= v2.x; - v1.y *= v2.y; - v1.z *= v2.z; - v1.w *= v2.w; - - return v1; -} - -quaternion -sweet_quaternion_new (float w, float x, float y, float z) -{ - quaternion q; - q.w = w; - q.x = x; - q.y = y; - q.z = z; - - return q; -} - -quaternion -sweet_quaternion_rotation (float angle, float x, float y, float z) -{ - quaternion q; - float s; - - s = sin (0.5 * angle); - - q.w = cos (0.5 * angle); - q.x = x * s; - q.y = y * s; - q.z = z * s; - - return q; -} - -quaternion -sweet_quaternion_conjugate (quaternion q) -{ - quaternion conjugate; - - conjugate.x = -q.x; - conjugate.y = -q.y; - conjugate.z = -q.z; - conjugate.w = q.w; - - return conjugate; -} - -quaternion -sweet_quaternion_add (quaternion q1, quaternion q2) -{ - quaternion sum; - - sum.w = q1.w + q2.w; - sum.x = q1.x + q2.x; - sum.y = q1.y + q2.y; - sum.z = q1.z + q2.z; - - return sum; -} - -quaternion -sweet_quaternion_product (quaternion q1, quaternion q2) -{ - quaternion prod; - - prod.w = (q1.w * q2.w) - (q1.x * q2.x) - (q1.y * q2.y) - (q1.z * q2.z); - prod.x = (q1.w * q2.x) + (q1.x * q2.w) + (q1.y * q2.z) - (q1.z * q2.y); - prod.y = (q1.w * q2.y) - (q1.x * q2.z) + (q1.y * q2.w) + (q1.z * q2.x); - prod.z = (q1.w * q2.z) + (q1.x * q2.y) - (q1.y * q2.x) + (q1.z * q2.w); - - return prod; -} - -float -sweet_quaternion_norm (quaternion q) -{ - return sqrt ((q.w * q.w) + (q.x * q.x) + (q.y * q.y) + (q.z * q.z)); -} - diff --git a/sweet_math.h b/sweet_math.h index a3c76a0..221aa19 100644 --- a/sweet_math.h +++ b/sweet_math.h @@ -30,42 +30,184 @@ **/ /** This macro define well known numbers with a relative great precision */ -#define SWEET_PI_OVER_3 1.047197551196597746154214 -#define SWEET_PI_OVER_2 1.570796326794896619231321 -#define SWEET_PI 3.141592653589793238462643 -#define SWEET_2PI 6.283185307179586476925286 -#define SWEET_GOLDEN_RATIO 1.618033988749894848204586 -#define SWEET_EPSILON 0.00000001 +#define PI_OVER_3 1.047197551196597746154214 +#define PI_OVER_2 1.570796326794896619231321 +#define PI 3.141592653589793238462643 +#define PI_2 6.283185307179586476925286 +#define GOLDEN_RATIO 1.618033988749894848204586 +#define EPSILON 0.0000000001 /** RadianToDegree */ /** @param Radian as float or double */ /** @return Degree as float */ -#define sweet_math_radian_2_degree(radian) \ +#define radian_2_degree(radian) \ radian * 57.2957795130823208767981548 /** DegreeToRadian */ /** @param Degree as float or double */ /** @return Radian as float */ -#define sweet_math_degree_2_radian(degree) \ +#define degree_2_radian(degree) \ degree * 0.01745329251994329576923690768488 -#define sweet_math_approx_equals(a, b, epsilon) \ +#define float_approx_equals(a, b, epsilon) \ (a <= (b+epsilon) && a >= (b-epsilon)) -#define sweet_math_approx_zero(a, epsilon) \ +#define float_approx_zero(a, epsilon) \ (a <= (epsilon) && a >= (epsilon)) -/** Invsqrt */ -/** @param x argument of square root as float */ -/** @return 1 over square root as float */ -float sweet_math_invsqrt (float x); +/* Vector */ + +vec2 vec2_zero(); +vec3 vec3_zero(); +vec4 vec4_zero(); + +/** Vector constructor */ +/** @return Vector */ +vec2 vec2_new_2f (float x, float y); +vec2 vec2_new_2f(float x, float y); +vec2 vec2_new_v3(vec3 w); +vec3 vec3_new_3f (float x, float y, float z); +vec3 vec3_new_v2_1f(vec2 w, float z); +vec3 vec3_new_v4(vec4 w); +vec4 vec4_new_4f (float x, float y, float z, float w); +vec4 vec4_new_v2_2f(vec2 u, float z, float w); +vec4 vec4_new_v3_1f(vec3 u, float w); +vec4 vec4_new_v4 (vec4 v); + +/** Norm */ +/** @param v as vec2, vec3 or vec4*/ +/** @return vector norm or squared norm */ +float vec2_norm (vec2 v); +float vec3_norm (vec3 v); +float vec4_norm (vec4 v); + +float vec2_square_norm (vec2 v); +float vec3_square_norm (vec3 v); +float vec4_square_norm (vec4 v); + +/** Dist */ +/** @param a as vec2, vec3 or vec4 */ +/** @param b as vec2, vec3 or vec4 */ +/** @return distance or squared distance between a and b */ +float vec2_dist (vec2 a, vec2 b); +float vec3_dist (vec3 a, vec3 b); +float vec4_dist (vec4 a, vec4 b); + +float vec2_square_dist (vec2 a, vec2 b); +float vec3_square_dist (vec3 a, vec3 b); +float vec4_square_dist (vec4 a, vec4 b); + + +/** Dot */ +/** @param v1 as vector */ +/** @param v2 as vector */ +/** @return Dot product as float */ +float vec2_dot (vec2 vector1, vec2 vector2); +float vec3_dot (vec3 vector1, vec3 vector2); +float vec4_dot (vec4 vector1, vec4 vector2); + +/** Cross */ +/** @param vector1 as vec3 */ +/** @param vector1 as vec3 */ +/** @return Cross product as vec3 */ +vec3 cross (vec3 vector1, vec3 vector2); + +/** TripleProduct */ +/** @param vector1 as vec3 */ +/** @param vector2 as vec3 */ +/** @param vector3 as vec3 */ +/** @return v1 . (v2 x v3) as float */ +float triple_product (vec3 vector1, vec3 vector2, vec3 vector3); + +/** Normalize */ +/** @param vector as vec2, vec3 or vec4 */ +/** @return Normalized vector */ +vec2 vec2_normalize (vec2 vector); +vec3 vec3_normalize (vec3 vector); +vec4 vec4_normalize (vec4 vector); + +/** Scale */ +/** @param Vector as vector */ +/** @param Scalar as float */ +/** @return Vector * scalar */ +vec2 vec2_scale (vec2 vector, float scalar); +vec3 vec3_scale (vec3 vector, float scalar); +vec4 vec4_scale (vec4 vector, float scalar); + +/** Rescale */ +/** @param Vector as vector */ +/** @param Size as float */ +/** @return Vector * size / lenght */ +vec2 vec2_rescale (vec2 vector, float size); +vec3 vec3_rescale (vec3 vector, float size); +vec4 vec4_rescale (vec4 vector, float size); + +/** Inverse */ +/** @param Vector as vector*/ +/** @return - vector */ +vec2 vec2_inverse (vec2 vector); +vec3 vec3_inverse (vec3 vector); +vec4 vec4_inverse (vec4 vector); + +/** Add */ +/** @param Vector1 as vector */ +/** @param Vector2 as vector */ +/** @return vector1 + vector2 */ +vec2 vec2_add (vec2 vector1, vec2 vector2); +vec3 vec3_add (vec3 vector1, vec3 vector2); +vec4 vec4_add (vec4 vector1, vec4 vector2); + +/** Middle */ +/** @param Vector1 as vector */ +/** @param Vector2 as vector */ +/** @return middle point between vector1 and vector2 */ +vec2 vec2_middle (vec2 vector1, vec2 vector2); +vec3 vec3_middle (vec3 vector1, vec3 vector2); +vec4 vec4_middle (vec4 vector1, vec4 vector2); + +/** Sub */ +/** @param Vector1 as vector */ +/** @param Vector2 as vector */ +/** @return vector1 - vector2 */ +vec2 vec2_sub (vec2 vector1, vec2 vector2); +vec3 vec3_sub (vec3 vector1, vec3 vector2); +vec4 vec4_sub (vec4 vector1, vec4 vector2); + +/** Angle */ +/** @param Vector1 as vector */ +/** @param Vector2 as vector */ +/** @return Angle between the two vector in radian */ +float vec2_angle (vec2 vector1, vec2 vector2); +float vec3_angle (vec3 vector1, vec3 vector2); + +/** Random */ +vec3 vec3_random (); +vec3 vec3_random_ortho (vec3 v); + +/** Mult */ +/** @param Vector1 as vector */ +/** @param Vector2 as vector */ +/** @return Product of vector1 and vector2 */ +vec2 vec2_product (vec2 vector1, vec2 vector2); +vec3 vec3_product (vec3 vector1, vec3 vector2); +vec4 vec4_product (vec4 vector1, vec4 vector2); + + +/* Quaternion */ +quaternion quat_new (float w, float x, float y, float z); +quaternion quat_rotation (float angle, float x, float y, float z); +quaternion quat_conjugate (quaternion q); +quaternion quat_add (quaternion q1, quaternion q2); +quaternion quat_product (quaternion q1, quaternion q2); +float quat_norm (quaternion q); + /** Nearest */ /** @param number integer to compare as int */ /** @param nb_args number of arguments as int */ /** @param ... integer arguments */ /** @return nearest argument to number */ -int sweet_math_nearest (int number, int nb_args, ...); +int nearest_integer (int number, int nb_args, ...); /** Quadratic_polynomial */ /** @param r 2D Vector for the reals roots as vec2 pointer */ @@ -73,7 +215,7 @@ int sweet_math_nearest (int number, int nb_args, ...); /** @param b coefficiant of x as float */ /** @param c coefficiant of constant as float */ /** @return number of roots as int */ -int sweet_math_quadratic_polynomial (vec2 * r, float a, float b, float c); +int quadratic_polynomial (vec2 * r, float a, float b, float c); /** Cubic_polynomial */ /** @param r 3D Vector containing the reals roots as vec3 pointer */ @@ -82,167 +224,8 @@ int sweet_math_quadratic_polynomial (vec2 * r, float a, float b, float c); /** @param c coefficiant of x as float */ /** @param d coefficiant of constant as float */ /** @return number of roots as int */ -int sweet_math_cubic_polynomial (vec3 * r, float a, float b, float c, float d); +int cubic_polynomial (vec3 * r, float a, float b, float c, float d); -/* Vector */ - -/** New */ -/** @param Vector componants as float */ -/** @return Vector */ -vec2 sweet_vector_new2 (float x, float y); -vec3 sweet_vector_new3 (float x, float y, float z); -vec4 sweet_vector_new4 (float x, float y, float z, float w); - -/** Norm */ -/** @param v as vec2, vec3 or vec4*/ -/** @return vector norm or squared norm */ -float sweet_vector_norm2 (vec2 v); -float sweet_vector_norm3 (vec3 v); -float sweet_vector_norm4 (vec4 v); -float sweet_vector_norm2h (vec3 v); -float sweet_vector_norm3h (vec4 v); - -float sweet_vector_square_norm2 (vec2 v); -float sweet_vector_square_norm3 (vec3 v); -float sweet_vector_square_norm4 (vec4 v); -float sweet_vector_square_norm2h (vec3 v); -float sweet_vector_square_norm3h (vec4 v); - -/** Dist */ -/** @param a as vec2, vec3 or vec4 */ -/** @param b as vec2, vec3 or vec4 */ -/** @return distance or squared distance between a and b */ -float sweet_vector_dist2 (vec2 a, vec2 b); -float sweet_vector_dist3 (vec3 a, vec3 b); -float sweet_vector_dist4 (vec4 a, vec4 b); - -float sweet_vector_square_dist2 (vec2 a, vec2 b); -float sweet_vector_square_dist3 (vec3 a, vec3 b); -float sweet_vector_square_dist4 (vec4 a, vec4 b); - - -/** Dot */ -/** @param v1 as vector */ -/** @param v2 as vector */ -/** @return Dot product as float */ -float sweet_vector_dot2 (vec2 vector1, vec2 vector2); -float sweet_vector_dot3 (vec3 vector1, vec3 vector2); -float sweet_vector_dot4 (vec4 vector1, vec4 vector2); - -/** Dot homogeneous */ -/** @param v1 as vector */ -/** @param v2 as vector */ -/** @return Dot product as float */ -float sweet_vector_dot2h (vec3 vector1, vec3 vector2); -float sweet_vector_dot3h (vec4 vector1, vec4 vector2); - -/** Cross */ -/** @param vector1 as vec3 */ -/** @param vector1 as vec3 */ -/** @return Cross product as vec3 */ -vec3 sweet_vector_cross (vec3 vector1, vec3 vector2); - -/** Cross homogeneous */ -/** @param vector1 as vec4 */ -/** @param vector1 as vec4 */ -/** @return Cross product as vec4 */ -vec4 sweet_vector_crossh (vec4 vector1, vec4 vector2); - -/** TripleProduct */ -/** @param vector1 as vec3 */ -/** @param vector2 as vec3 */ -/** @param vector3 as vec3 */ -/** @return v1 . (v2 x v3) as float */ -float sweet_vector_triple_product (vec3 vector1, vec3 vector2, vec3 vector3); - -/** TripleProduct */ -/** @param vector1 as vec3 */ -/** @param vector2 as vec3 */ -/** @param vector3 as vec3 */ -/** @return v1 . (v2 x v3) as float */ -float sweet_vector_triple_producth (vec4 vector1, vec4 vector2, vec4 vector3); - -/** Normalize */ -/** @param vector as vec2, vec3 or vec4 */ -/** @return Normalized vector */ -vec2 sweet_vector_normalize2 (vec2 vector); -vec3 sweet_vector_normalize3 (vec3 vector); -vec4 sweet_vector_normalize4 (vec4 vector); - -/** Scale */ -/** @param Vector as vector */ -/** @param Scalar as float */ -/** @return Vector * scalar */ -vec2 sweet_vector_scale2 (vec2 vector, float scalar); -vec3 sweet_vector_scale3 (vec3 vector, float scalar); -vec4 sweet_vector_scale4 (vec4 vector, float scalar); - -/** Rescale */ -/** @param Vector as vector */ -/** @param Size as float */ -/** @return Vector / lenght * size */ -vec2 sweet_vector_rescale2 (vec2 vector, float size); -vec3 sweet_vector_rescale3 (vec3 vector, float size); -vec4 sweet_vector_rescale4 (vec4 vector, float size); - -/** Inverse */ -/** @param Vector as vector*/ -/** @return - vector */ -vec2 sweet_vector_inverse2 (vec2 vector); -vec3 sweet_vector_inverse3 (vec3 vector); -vec4 sweet_vector_inverse4 (vec4 vector); - -/** Add */ -/** @param Vector1 as vector */ -/** @param Vector2 as vector */ -/** @return vector1 + vector2 */ -vec2 sweet_vector_add2 (vec2 vector1, vec2 vector2); -vec3 sweet_vector_add3 (vec3 vector1, vec3 vector2); -vec4 sweet_vector_add4 (vec4 vector1, vec4 vector2); - -/** Middle */ -/** @param Vector1 as vector */ -/** @param Vector2 as vector */ -/** @return middle point between vector1 and vector2 */ -vec2 sweet_vector_middle2 (vec2 vector1, vec2 vector2); -vec3 sweet_vector_middle3 (vec3 vector1, vec3 vector2); -vec4 sweet_vector_middle4 (vec4 vector1, vec4 vector2); - -/** Sub */ -/** @param Vector1 as vector */ -/** @param Vector2 as vector */ -/** @return vector1 - vector2 */ -vec2 sweet_vector_sub2 (vec2 vector1, vec2 vector2); -vec3 sweet_vector_sub3 (vec3 vector1, vec3 vector2); -vec4 sweet_vector_sub4 (vec4 vector1, vec4 vector2); - -/** Angle */ -/** @param Vector1 as vector */ -/** @param Vector2 as vector */ -/** @return Angle between the two vector in radian */ -float sweet_vector_angle2 (vec2 vector1, vec2 vector2); -float sweet_vector_angle3 (vec3 vector1, vec3 vector2); - -/** Random */ -vec3 sweet_vector_normalized_random (); -vec3 sweet_vector_random_ortho(vec3 v); - -/** Mult */ -/** @param Vector1 as vector */ -/** @param Vector2 as vector */ -/** @return Direct product of vector1 and vector2 */ -vec2 sweet_vector_product2 (vec2 vector1, vec2 vector2); -vec3 sweet_vector_product3 (vec3 vector1, vec3 vector2); -vec4 sweet_vector_product4 (vec4 vector1, vec4 vector2); - - -/* Quaternion */ -quaternion sweet_quaternion_new (float w, float x, float y, float z); -quaternion sweet_quaternion_rotation (float angle, float x, float y, float z); -quaternion sweet_quaternion_conjugate (quaternion q); -quaternion sweet_quaternion_add (quaternion q1, quaternion q2); -quaternion sweet_quaternion_product (quaternion q1, quaternion q2); -float sweet_quaternion_norm (quaternion q); #endif /*MATH_H */ diff --git a/sweet_math_short.c b/sweet_math_short.c deleted file mode 100644 index dd06f43..0000000 --- a/sweet_math_short.c +++ /dev/null @@ -1,54 +0,0 @@ -#include "sweet_math.h" - -vec2 vec2_new_v3(vec3 w) -{ - vec2 v; - v.x = w.x; - v.y = w.y; - return v; -} - -vec3 vec3_new_v2_1f(vec2 w, float z) -{ - vec3 v; - v.x = w.x; - v.y = w.y; - v.z = z; - return v; -} - -vec3 vec3_new_v4(vec4 w) -{ - vec3 v; - v.x = w.x; - v.y = w.y; - v.z = w.z; - return v; -} - -vec4 vec4_new_v2_2f(vec2 u, float z, float w) -{ - vec4 v; - v.x = u.x; - v.y = u.y; - v.z = z; - v.w = w; - return v; -} - -vec4 vec4_new_v3_1f(vec3 u, float w) -{ - vec4 v; - v.x = u.x; - v.y = u.y; - v.z = u.z; - v.w = w; - return v; -} - -vec4 vec4_new_v4 (vec4 v) -{ - return v; -} - - diff --git a/sweet_math_short.h b/sweet_math_short.h deleted file mode 100644 index e2dddfb..0000000 --- a/sweet_math_short.h +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Sweet is a small library for basic math and small matrix operations. - * Copyright 2014 Luc Girod. - * - * This library is free software: you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation, either - * version 3 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see . - */ - -#ifndef SWEET_MATH_SHORT_H -#define SWEET_MATH_SHORT_H - -#include "sweet_math.h" - -#define VEC2_BUILDER(_1, _2, NAME, ...) NAME -#define VEC3_BUILDER(_1, _2, _3, NAME, ...) NAME -#define VEC4_BUILDER(_1, _2, _3, _4, NAME, ...) NAME - -#define vec2_new(...) VEC2_BUILDER(__VA_ARGS__, sweet_vector_new2, vec2_new_v3, NULL)(__VA_ARGS__) -#define vec3_new(...) VEC3_BUILDER(__VA_ARGS__, sweet_vector_new3, vec3_new_v2_1f, vec3_new_v4, NULL)(__VA_ARGS__) -#define vec4_new(...) VEC4_BUILDER(__VA_ARGS__, sweet_vector_new4, vec4_new_v2_2f, vec4_new_v3_1f, vec4_new_v4, NULL)(__VA_ARGS__) - -vec2 vec2_new_v3(vec3 w); -vec3 vec3_new_v2_1f(vec2 w, float z); -vec3 vec3_new_v4(vec4 w); -vec4 vec4_new_v2_2f(vec2 u, float z, float w); -vec4 vec4_new_v3_1f(vec3 u, float w); -vec4 vec4_new_v4 (vec4 v); - -#define radian_2_degree(radian) radian * 57.2957795130823208767981548 -#define degree_2_radian(degree) degree * 0.01745329251994329576923690768488 - -#define EPSILON SWEET_EPSILON -#define is_equals(a, b, epsilon) sweet_math_approx_equals(a, b, epsilon) -#define is_zero(a, epsilon) sweet_math_approx_zero(a, epsilon) - -#define invsqrt sweet_math_invsqrt -#define nearest sweet_math_nearest -#define quadratic_polynomial sweet_math_quadratic_polynomial -#define cubic_polynomial sweet_math_cubic_polynomial -#define vec2_norm sweet_vector_norm2 -#define vec3_norm sweet_vector_norm3 -#define vec4_norm sweet_vector_norm4 -#define vec2h_norm sweet_vector_norm2h -#define vec3h_norm sweet_vector_norm3h -#define vec2_square_norm sweet_vector_square_norm2 -#define vec3_square_norm sweet_vector_square_norm3 -#define vec4_square_norm sweet_vector_square_norm4 -#define vec2h_square_norm sweet_vector_square_norm2h -#define vec3h_square_norm sweet_vector_square_norm3h -#define vec2_dist sweet_vector_dist2 -#define vec3_dist sweet_vector_dist3 -#define vec4_dist sweet_vector_dist4 -#define vec2_dist2 sweet_vector_square_dist2 -#define vec3_dist2 sweet_vector_square_dist3 -#define vec4_dist2 sweet_vector_square_dist4 -#define vec2_dot sweet_vector_dot2 -#define vec3_dot sweet_vector_dot3 -#define vec4_dot sweet_vector_dot4 -#define vec2h_dot sweet_vector_dot2h -#define vec3h_dot sweet_vector_dot3h -#define cross sweet_vector_cross -#define crossh sweet_vector_crossh -#define vec3_triple_product sweet_vector_triple_product -#define vec3h_triple_product sweet_vector_triple_producth -#define vec2_normalize sweet_vector_normalize2 -#define vec3_normalize sweet_vector_normalize3 -#define vec4_normalize sweet_vector_normalize4 -#define vec2_scale sweet_vector_scale2 -#define vec3_scale sweet_vector_scale3 -#define vec4_scale sweet_vector_scale4 -#define vec2_rescale sweet_vector_rescale2 -#define vec3_rescale sweet_vector_rescale3 -#define vec4_rescale sweet_vector_rescale4 -#define vec2_inverse sweet_vector_inverse2 -#define vec3_inverse sweet_vector_inverse3 -#define vec4_inverse sweet_vector_inverse4 -#define vec2_add sweet_vector_add2 -#define vec3_add sweet_vector_add3 -#define vec4_add sweet_vector_add4 -#define vec2_sub sweet_vector_sub2 -#define vec3_sub sweet_vector_sub3 -#define vec4_sub sweet_vector_sub4 -#define vec2_angle sweet_vector_angle2 -#define vec3_angle sweet_vector_angle3 -#define vec2_product sweet_vector_product2 -#define vec3_product sweet_vector_product3 -#define vec4_product sweet_vector_product4 -#define quat_new sweet_quaternion_new -#define quat_rot sweet_quaternion_rotation -#define quat_conjugate sweet_quaternion_conjugate -#define quat_add sweet_quaternion_add -#define quat_product sweet_quaternion_product -#define sweet_quaternion_norm quat_norm - -#endif - diff --git a/sweet_matrix.c b/sweet_matrix.c index 4e3b761..c1d590a 100644 --- a/sweet_matrix.c +++ b/sweet_matrix.c @@ -18,12 +18,184 @@ #include #include +#include "sweet_macro.h" #include "sweet_matrix.h" #include "sweet_math.h" #include "sweet_types.h" +mat2 mat2_new_m3 (mat3 m) +{ + mat2 n; + n.v[0] = m.v[0]; + n.v[1] = m.v[1]; + n.v[2] = m.v[3]; + n.v[3] = m.v[4]; + return n; +} + +mat2 mat2_new_2v (vec2 e1, vec2 e2) +{ + mat2 m; + m.v[0] = e1.x; + m.v[1] = e1.y; + m.v[2] = e2.x; + m.v[3] = e2.y; + return m; +} + +mat2 mat2_new_3f (float a, float b, float c) +{ + return mat2_new_4f (a, b, c, 0); +} + +mat2 mat2_new_4f (float f0, float f1, float f2, float f3) +{ + mat2 n; + n.v[0] = f0; + n.v[1] = f1; + n.v[2] = f2; + n.v[3] = f3; + return n; +} + +mat3 mat3_new_m4 (mat4 m) +{ + mat3 n; + n.v[0] = m.v[0]; + n.v[1] = m.v[1]; + n.v[2] = m.v[2]; + + n.v[3] = m.v[4]; + n.v[4] = m.v[5]; + n.v[5] = m.v[6]; + + n.v[6] = m.v[8]; + n.v[7] = m.v[9]; + n.v[8] = m.v[10]; + return n; +} + +mat3 mat3_new_2v (vec2 e1, vec2 e2) +{ + mat3 m; + m.v[0] = e1.x; + m.v[1] = e1.y; + m.v[2] = 0; + + m.v[3] = e2.x; + m.v[4] = e2.y; + m.v[5] = 0; + + m.v[6] = 0; + m.v[7] = 0; + m.v[8] = 1; + return m; +} + +mat3 mat3_new_3v (vec3 e1, vec3 e2, vec3 e3) +{ + mat3 m; + m.v[0] = e1.x; + m.v[1] = e1.y; + m.v[2] = e1.z; + + m.v[3] = e2.x; + m.v[4] = e2.y; + m.v[5] = e2.z; + + m.v[6] = e3.x; + m.v[7] = e3.y; + m.v[8] = e3.z; + return m; +} + +mat4 mat4_new_m4 (mat4 m) +{ + return m; +} + +mat4 mat4_new_2v (vec2 e1, vec2 e2) +{ + mat4 m; + m.v[0] = e1.x; + m.v[1] = e1.y; + m.v[2] = 0; + m.v[3] = 0; + + m.v[4] = e2.x; + m.v[5] = e2.y; + m.v[6] = 0; + m.v[7] = 0; + + m.v[8] = 0; + m.v[9] = 0; + m.v[10] = 1; + m.v[11] = 0; + + m.v[12] = 0; + m.v[13] = 0; + m.v[14] = 0; + m.v[15] = 1; + + return m; +} + +mat4 mat4_new_3v (vec3 e1, vec3 e2, vec3 e3) +{ + mat4 m; + + m.v[0] = e1.x; + m.v[1] = e1.y; + m.v[2] = e1.z; + m.v[3] = 0; + + m.v[4] = e2.x; + m.v[5] = e2.y; + m.v[6] = e2.z; + m.v[7] = 0; + + m.v[8] = e3.x; + m.v[9] = e3.y; + m.v[10] = e3.z; + m.v[11] = 0; + + m.v[12] = 0; + m.v[13] = 0; + m.v[14] = 0; + m.v[15] = 1; + return m; +} + +mat4 mat4_new_4v (vec4 e1, vec4 e2, vec4 e3, vec4 e4) +{ + mat4 m; + + m.v[0] = e1.x; + m.v[1] = e1.y; + m.v[2] = e1.z; + m.v[3] = e1.w; + + m.v[4] = e2.x; + m.v[5] = e2.y; + m.v[6] = e2.z; + m.v[7] = e2.w; + + m.v[8] = e3.x; + m.v[9] = e3.y; + m.v[10] = e3.z; + m.v[11] = e3.w; + + m.v[12] = e4.x; + m.v[13] = e4.y; + m.v[14] = e4.z; + m.v[15] = e4.w; + + return m; +} + + mat2 -sweet_matrix_null2 (void) +mat2_null (void) { mat2 m; @@ -37,7 +209,7 @@ sweet_matrix_null2 (void) } mat3 -sweet_matrix_null3 (void) +mat3_null (void) { mat3 m; @@ -57,7 +229,7 @@ sweet_matrix_null3 (void) } mat4 -sweet_matrix_null4 (void) +mat4_null (void) { mat4 m; @@ -85,7 +257,7 @@ sweet_matrix_null4 (void) } mat2 -sweet_matrix_identity2 (void) +mat2_identity (void) { mat2 m; @@ -99,7 +271,7 @@ sweet_matrix_identity2 (void) } mat3 -sweet_matrix_identity3 (void) +mat3_identity (void) { mat3 m; @@ -119,7 +291,7 @@ sweet_matrix_identity3 (void) } mat4 -sweet_matrix_identity4 (void) +mat4_identity (void) { mat4 m; @@ -147,7 +319,7 @@ sweet_matrix_identity4 (void) } mat2 -sweet_matrix_ortho_basis2 (vec2 u, vec2 v) +mat2_ortho_basis (vec2 u, vec2 v) { mat2 m; m.v[0] = u.x; @@ -160,7 +332,26 @@ sweet_matrix_ortho_basis2 (vec2 u, vec2 v) } mat3 -sweet_matrix_ortho_basis3 (vec3 u, vec3 v, vec3 w) +mat2h_ortho_basis (vec2 p, vec2 u, vec2 v) +{ + mat3 m; + m.v[0] = u.x; + m.v[1] = v.x; + m.v[2] = 0; + + m.v[3] = u.y; + m.v[4] = v.y; + m.v[5] = 0; + + m.v[6] = - p.x * u.x - p.y * u.y; + m.v[7] = - p.x * v.x - p.y * v.y; + m.v[8] = 1; + + return m; +} + +mat3 +mat3_ortho_basis (vec3 u, vec3 v, vec3 w) { mat3 m; m.v[0] = u.x; @@ -179,7 +370,7 @@ sweet_matrix_ortho_basis3 (vec3 u, vec3 v, vec3 w) } mat4 -sweet_matrix_ortho_basis3h (vec3 p, vec3 u, vec3 v, vec3 w) +mat3h_ortho_basis (vec3 p, vec3 u, vec3 v, vec3 w) { mat4 m; m.v[0] = u.x; @@ -206,7 +397,7 @@ sweet_matrix_ortho_basis3h (vec3 p, vec3 u, vec3 v, vec3 w) } mat4 -sweet_matrix_ortho_basis4 (vec4 u, vec4 v, vec4 w, vec4 t) +mat4_ortho_basis (vec4 u, vec4 v, vec4 w, vec4 t) { mat4 m; m.v[0] = u.x; @@ -233,12 +424,12 @@ sweet_matrix_ortho_basis4 (vec4 u, vec4 v, vec4 w, vec4 t) } mat4 -sweet_matrix_perspective (double fovy, double aspect, double z_near, double z_far) +matrix_perspective (double fovy, double aspect, double z_near, double z_far) { mat4 m; float f; - fovy = sweet_math_degree_2_radian (fovy); + fovy = degree_2_radian (fovy); f = 1.0 / tan (fovy / 2.0); m.v[0] = f / aspect; @@ -265,9 +456,9 @@ sweet_matrix_perspective (double fovy, double aspect, double z_near, double z_fa } mat4 -sweet_matrix_frustum (float left, float right, - float bottom, float top, - float z_near, float z_far) +matrix_frustum (float left, float right, + float bottom, float top, + float z_near, float z_far) { mat4 m; @@ -295,9 +486,9 @@ sweet_matrix_frustum (float left, float right, } mat4 -sweet_matrix_ortho (float left, float right, - float bottom, float top, - float zNear, float zFar) +matrix_ortho (float left, float right, + float bottom, float top, + float zNear, float zFar) { mat4 m; @@ -322,14 +513,14 @@ sweet_matrix_ortho (float left, float right, } mat4 -sweet_matrix_look_at (vec3 pos, vec3 dir, vec3 up) +matrix_look_at (vec3 pos, vec3 dir, vec3 up) { mat4 m; vec3 side; - side = sweet_vector_cross (dir, up); - side = sweet_vector_normalize3 (side); - up = sweet_vector_cross (side, dir); - up = sweet_vector_normalize3 (up); + side = cross (dir, up); + side = vec3_normalize (side); + up = cross (side, dir); + up = vec3_normalize (up); m.v[0] = side.x; m.v[1] = up.x; @@ -355,15 +546,14 @@ sweet_matrix_look_at (vec3 pos, vec3 dir, vec3 up) } mat4 -sweet_matrix_look_at_ortho (vec3 pos, vec3 dir, vec3 up, vec3 right) +matrix_look_at_ortho (vec3 pos, vec3 dir, vec3 up, vec3 right) { - mat4 m; - dir = sweet_vector_scale3 (dir, -1); - return sweet_matrix_ortho_basis3h (pos, right, up, dir); + dir = vec3_inverse (dir); + return mat3h_ortho_basis (pos, right, up, dir); } mat3 -sweet_matrix_quat_rotation3 (quaternion q) +mat3_quat_rotation (quaternion q) { mat3 m; @@ -383,7 +573,7 @@ sweet_matrix_quat_rotation3 (quaternion q) } mat4 -sweet_matrix_quat_rotation3h (quaternion q) +mat3h_quat_rotation (quaternion q) { mat4 m; @@ -410,8 +600,60 @@ sweet_matrix_quat_rotation3h (quaternion q) return m; } + +mat2 +mat2_rotation (float angle) +{ + mat2 m; + + m.v[0] = cos (angle); + m.v[1] = sin (angle); + m.v[2] = -sin (angle); + m.v[3] = cos (angle); + + return m; +} + +mat3 +mat2h_rotation (float angle) +{ + mat3 m; + + m.v[0] = cos (angle); + m.v[1] = sin (angle); + m.v[2] = 0.0; + m.v[3] = -sin (angle); + m.v[4] = cos (angle); + m.v[5] = 0.0; + m.v[6] = 0.0; + m.v[7] = 0.0; + m.v[8] = 1.0; + + return m; +} + +mat3 +mat3_rotation (float angle, float x, float y, float z) +{ + mat3 m; + + m.v[0] = x * x * (1 - cos (angle)) + cos (angle); + m.v[1] = x * y * (1 - cos (angle)) + z * sin (angle); + m.v[2] = x * z * (1 - cos (angle)) - y * sin (angle); + + m.v[3] = x * y * (1 - cos (angle)) - z * sin (angle); + m.v[4] = y * y * (1 - cos (angle)) + cos (angle); + m.v[5] = y * z * (1 - cos (angle)) + x * sin (angle); + + m.v[6] = x * z * (1 - cos (angle)) + y * sin (angle); + m.v[7] = y * z * (1 - cos (angle)) - x * sin (angle); + m.v[8] = z * z * (1 - cos (angle)) + cos (angle); + + return m; +} + mat4 -sweet_matrix_rotation3h (float angle, float x, float y, float z) +mat3h_rotation (float angle, float x, float y, float z) { mat4 m; @@ -439,58 +681,27 @@ sweet_matrix_rotation3h (float angle, float x, float y, float z) } mat3 -sweet_matrix_rotation3 (float angle, float x, float y, float z) +mat2h_translation (vec2 v) { mat3 m; - m.v[0] = x * x * (1 - cos (angle)) + cos (angle); - m.v[1] = x * y * (1 - cos (angle)) + z * sin (angle); - m.v[2] = x * z * (1 - cos (angle)) - y * sin (angle); - - m.v[3] = x * y * (1 - cos (angle)) - z * sin (angle); - m.v[4] = y * y * (1 - cos (angle)) + cos (angle); - m.v[5] = y * z * (1 - cos (angle)) + x * sin (angle); - - m.v[6] = x * z * (1 - cos (angle)) + y * sin (angle); - m.v[7] = y * z * (1 - cos (angle)) - x * sin (angle); - m.v[8] = z * z * (1 - cos (angle)) + cos (angle); - - return m; -} - -mat2 -sweet_matrix_rotation2 (float angle) -{ - mat2 m; - - m.v[0] = cos (angle); - m.v[1] = sin (angle); - m.v[2] = -sin (angle); - m.v[3] = cos (angle); - - return m; -} - -mat3 -sweet_matrix_rotation2h (float angle) -{ - mat3 m; - - m.v[0] = cos (angle); - m.v[1] = sin (angle); + m.v[0] = 1.0; + m.v[1] = 0.0; m.v[2] = 0.0; - m.v[3] = -sin (angle); - m.v[4] = cos (angle); + + m.v[3] = 0.0; + m.v[4] = 1.0; m.v[5] = 0.0; - m.v[6] = 0.0; - m.v[7] = 0.0; + + m.v[6] = v.x; + m.v[7] = v.y; m.v[8] = 1.0; return m; } mat4 -sweet_matrix_translation3h (vec3 v) +mat3h_translation (vec3 v) { mat4 m; @@ -517,28 +728,42 @@ sweet_matrix_translation3h (vec3 v) return m; } +mat2 +mat2_scale (float x, float y) +{ + mat2 m; + + m.v[0] = x; + m.v[1] = 0; + + m.v[2] = 0; + m.v[3] = y; + + return m; +} + mat3 -sweet_matrix_translation2h (vec2 v) +mat3_scale (float x, float y, float z) { mat3 m; - m.v[0] = 1.0; - m.v[1] = 0.0; - m.v[2] = 0.0; + m.v[0] = x; + m.v[1] = 0; + m.v[2] = 0; - m.v[3] = 0.0; - m.v[4] = 1.0; - m.v[5] = 0.0; + m.v[3] = 0; + m.v[4] = y; + m.v[5] = 0; - m.v[6] = v.x; - m.v[7] = v.y; - m.v[8] = 1.0; + m.v[6] = 0; + m.v[7] = 0; + m.v[8] = z; return m; } mat4 -sweet_matrix_scale4 (float x, float y, float z, float w) +mat4_scale (float x, float y, float z, float w) { mat4 m; @@ -565,42 +790,8 @@ sweet_matrix_scale4 (float x, float y, float z, float w) return m; } -mat3 -sweet_matrix_scale3 (float x, float y, float z) -{ - mat3 m; - - m.v[0] = x; - m.v[1] = 0; - m.v[2] = 0; - - m.v[3] = 0; - m.v[4] = y; - m.v[5] = 0; - - m.v[6] = 0; - m.v[7] = 0; - m.v[8] = z; - - return m; -} - -mat2 -sweet_matrix_scale2 (float x, float y) -{ - mat2 m; - - m.v[0] = x; - m.v[1] = 0; - - m.v[2] = 0; - m.v[3] = y; - - return m; -} - mat4 -sweet_matrix_texture_bias (void) +matrix_texture_bias (void) { mat4 m; m.v[0] = 0.5; @@ -627,7 +818,7 @@ sweet_matrix_texture_bias (void) } void -sweet_matrix_transpose2 (mat2 * mt, mat2 * m) +mat2_transpose (mat2 * mt, mat2 * m) { float mv1 = m->v[1]; mt->v[0] = m->v[0]; @@ -637,7 +828,7 @@ sweet_matrix_transpose2 (mat2 * mt, mat2 * m) } void -sweet_matrix_transpose3 (mat3 * mt, mat3 * m) +mat3_transpose (mat3 * mt, mat3 * m) { float mv1 = m->v[1]; float mv2 = m->v[2]; @@ -654,7 +845,7 @@ sweet_matrix_transpose3 (mat3 * mt, mat3 * m) } void -sweet_matrix_transpose4 (mat4 * mt, mat4 * m) +mat4_transpose (mat4 * mt, mat4 * m) { float mv2 = m->v[2]; float mv3 = m->v[3]; @@ -681,13 +872,13 @@ sweet_matrix_transpose4 (mat4 * mt, mat4 * m) } float -sweet_matrix_det2 (mat2 * m) +mat2_det (mat2 * m) { return m->v[0] * m->v[3] - m->v[1] * m->v[2]; } float -sweet_matrix_det3 (mat3 * m) +mat3_det (mat3 * m) { float det = m->v[0] * (m->v[4] * m->v[8] - m->v[5] * m->v[7]) - @@ -698,7 +889,7 @@ sweet_matrix_det3 (mat3 * m) } float -sweet_matrix_det4 (mat4 * m) +mat4_det (mat4 * m) { float det = m->v[0] * (m->v[ 5] * (m->v[10] * m->v[15] - m->v[11] * m->v[14]) - @@ -718,9 +909,9 @@ sweet_matrix_det4 (mat4 * m) } void -sweet_matrix_inverse2 (mat2 * inv, mat2 * m) +mat2_inverse (mat2 * inv, mat2 * m) { - float invdet = 1.0 / sweet_matrix_det2 (m); + float invdet = 1.0 / mat2_det (m); float mv0 = m->v[0]; inv->v[0] = m->v[3] * invdet; @@ -730,7 +921,7 @@ sweet_matrix_inverse2 (mat2 * inv, mat2 * m) } void -sweet_matrix_inverse3 (mat3 * inv, mat3 * m) +mat3_inverse (mat3 * inv, mat3 * m) { float mv0 = m->v[0]; float mv1 = m->v[1]; @@ -740,7 +931,7 @@ sweet_matrix_inverse3 (mat3 * inv, mat3 * m) float mv6 = m->v[6]; float invdet; - invdet = 1.0 / sweet_matrix_det3 (m); + invdet = 1.0 / mat3_det (m); inv->v[0] = ((m->v[4] * m->v[8]) - (m->v[5] * m->v[7])) * invdet; inv->v[1] = (-(m->v[1] * m->v[8]) + (m->v[2] * m->v[7])) * invdet; @@ -771,7 +962,7 @@ matrix_inverse4 (mat4 * inv, mat4 * m) float mv10 = m->v[10]; float mv12 = m->v[12]; float mv13 = m->v[13]; - float invdet = 1.0 / sweet_matrix_det4 (m); + float invdet = 1.0 / mat4_det (m); inv->v[0] = m->v[5] * (m->v[10] * m->v[15] - m->v[11] * m->v[14]) - m->v[6] * (m->v[9] * m->v[15] - m->v[11] * m->v[13]) + @@ -856,7 +1047,7 @@ matrix_inverse4 (mat4 * inv, mat4 * m) } void -sweet_matrix_inverse4 (mat4 * inv, mat4 * m) +mat4_inverse (mat4 * inv, mat4 * m) { float detp; float invdet; @@ -868,7 +1059,7 @@ sweet_matrix_inverse4 (mat4 * inv, mat4 * m) detp = m->v[0] * m->v[5] - m->v[1] * m->v[4]; - if (sweet_math_approx_equals (detp, 0, 0.0000001)) + if (float_approx_zero (detp, 0.0000001)) { /* In this case the standard algorithm is used */ matrix_inverse4 (inv, m); @@ -928,7 +1119,7 @@ sweet_matrix_inverse4 (mat4 * inv, mat4 * m) } void -sweet_matrix_product2 (mat2 * p, mat2 * a, mat2 * b) +mat2_product (mat2 * p, mat2 * a, mat2 * b) { float av0 = a->v[0]; float av1 = a->v[1]; @@ -941,7 +1132,7 @@ sweet_matrix_product2 (mat2 * p, mat2 * a, mat2 * b) } void -sweet_matrix_product3 (mat3 * p, mat3 * a, mat3 * b) +mat3_product (mat3 * p, mat3 * a, mat3 * b) { float bv0 = b->v[0]; float bv1 = b->v[1]; @@ -970,7 +1161,7 @@ sweet_matrix_product3 (mat3 * p, mat3 * a, mat3 * b) } void -sweet_matrix_product4 (mat4 * p, mat4 * a, mat4 * b) +mat4_product (mat4 * p, mat4 * a, mat4 * b) { float bv0 = b->v[0]; float bv1 = b->v[1]; @@ -1019,7 +1210,7 @@ sweet_matrix_product4 (mat4 * p, mat4 * a, mat4 * b) } void -sweet_matrix_add_diag3 (mat3 * out, mat3 * in, float x, float y, float z) +mat3_add_diag (mat3 * out, mat3 * in, float x, float y, float z) { out->v[0] = in->v[0] + x; out->v[1] = in->v[1]; @@ -1033,7 +1224,7 @@ sweet_matrix_add_diag3 (mat3 * out, mat3 * in, float x, float y, float z) } void -sweet_matrix_substract_diag3 (mat3 * out, mat3 * in, float x, float y, float z) +mat3_substract_diag (mat3 * out, mat3 * in, float x, float y, float z) { out->v[0] = in->v[0] - x; out->v[1] = in->v[1]; @@ -1047,7 +1238,7 @@ sweet_matrix_substract_diag3 (mat3 * out, mat3 * in, float x, float y, float z) } void -sweet_matrix_add3 (mat3 * out, mat3 * m1, mat3 * m2) +mat3_add (mat3 * out, mat3 * m1, mat3 * m2) { out->v[0] = m1->v[0] + m2->v[0]; out->v[1] = m1->v[1] + m2->v[1]; @@ -1061,7 +1252,7 @@ sweet_matrix_add3 (mat3 * out, mat3 * m1, mat3 * m2) } void -sweet_matrix_substract3 (mat3 * out, mat3 * m1, mat3 * m2) +mat3_substract (mat3 * out, mat3 * m1, mat3 * m2) { out->v[0] = m1->v[0] - m2->v[0]; out->v[1] = m1->v[1] - m2->v[1]; @@ -1074,31 +1265,8 @@ sweet_matrix_substract3 (mat3 * out, mat3 * m1, mat3 * m2) out->v[8] = m1->v[8] - m2->v[8]; } -void -sweet_matrix_sub3 (mat2 * sub, mat3 * m) -{ - sub->v[0] = m->v[0]; - sub->v[1] = m->v[1]; - sub->v[2] = m->v[4]; - sub->v[3] = m->v[5]; -} - -void -sweet_matrix_sub4 (mat3 * sub, mat4 * m) -{ - sub->v[0] = m->v[0]; - sub->v[1] = m->v[1]; - sub->v[2] = m->v[2]; - sub->v[3] = m->v[4]; - sub->v[4] = m->v[5]; - sub->v[5] = m->v[6]; - sub->v[6] = m->v[8]; - sub->v[7] = m->v[9]; - sub->v[8] = m->v[10]; -} - vec2 -sweet_matrix_column2 (mat2 * m, int idx) +mat2_column (mat2 * m, int idx) { vec2 v; idx = idx * 2; @@ -1109,7 +1277,7 @@ sweet_matrix_column2 (mat2 * m, int idx) } vec3 -sweet_matrix_column3 (mat3 * m, int idx) +mat3_column (mat3 * m, int idx) { vec3 v; idx = idx * 3; @@ -1121,7 +1289,7 @@ sweet_matrix_column3 (mat3 * m, int idx) } vec4 -sweet_matrix_column4 (mat4 * m, int idx) +mat4_column (mat4 * m, int idx) { vec4 v; idx = idx * 4; @@ -1134,7 +1302,7 @@ sweet_matrix_column4 (mat4 * m, int idx) } vec2 -sweet_matrix_mult2 (mat2 * mat, vec2 vec) +mat2_mult (mat2 * mat, vec2 vec) { vec2 result; @@ -1145,7 +1313,7 @@ sweet_matrix_mult2 (mat2 * mat, vec2 vec) } vec3 -sweet_matrix_mult3 (mat3 * mat, vec3 vec) +mat3_mult (mat3 * mat, vec3 vec) { vec3 r; @@ -1157,7 +1325,7 @@ sweet_matrix_mult3 (mat3 * mat, vec3 vec) } vec4 -sweet_matrix_mult4 (mat4 * mat, vec4 vec) +mat4_mult (mat4 * mat, vec4 vec) { vec4 result; @@ -1170,13 +1338,13 @@ sweet_matrix_mult4 (mat4 * mat, vec4 vec) } int -sweet_matrix_eigen_value3 (float * h1, float * h2, float * h3, mat3 * m) +mat3_eigen_values (float * h1, float * h2, float * h3, mat3 * m) { float a; float b; float c; int n; - vec3 v = sweet_vector_new3 (0,0,0); + vec3 v = vec3_zero (); a = m->v[0] + m->v[4] + m->v[8]; @@ -1187,7 +1355,7 @@ sweet_matrix_eigen_value3 (float * h1, float * h2, float * h3, mat3 * m) m->v[1] * (m->v[3] * m->v[8] - m->v[5] * m->v[6]) + m->v[2] * (m->v[3] * m->v[7] - m->v[4] * m->v[6]); - n = sweet_math_cubic_polynomial (&v, -1, a, b, c); + n = cubic_polynomial (&v, -1, a, b, c); if (v.y > v.x) { float t = v.x; v.x = v.y; v.y = t; @@ -1305,10 +1473,8 @@ static void substract_row (mat3 * a, float * s, int l1, int l2, int i) } -#define EPSILON 1e-05f - int -sweet_matrix_gaussian_elimination3 (mat3 * a, vec3 * v) +mat3_gaussian_elimination (mat3 * a, vec3 * v) { float s[3]; float max; @@ -1320,7 +1486,7 @@ sweet_matrix_gaussian_elimination3 (mat3 * a, vec3 * v) i_max = find_pivot (&max, a, 0); /* If matrix is singular return error */ - if (sweet_math_approx_equals (max, 0, EPSILON)) { return -1; } + if (float_approx_zero (max, 1e-05f)) { return -1; } swap_rows (a, s, 0, i_max); normalize_row (a, s, 0, 0); substract_row (a, s, 0, 1, 0); @@ -1328,13 +1494,13 @@ sweet_matrix_gaussian_elimination3 (mat3 * a, vec3 * v) i_max = find_pivot (&max, a, 1); /* If matrix is singular return error */ - if (sweet_math_approx_equals (max, 0, EPSILON)) { return -1; } + if (float_approx_zero (max, 1e-05f)) { return -1; } swap_rows (a, s, 1, i_max); normalize_row (a, s, 1, 1); substract_row (a, s, 1, 2, 1); /* If matrix is singular return error */ - if (sweet_math_approx_equals (a->v[8], 0, EPSILON)) { return -1; } + if (float_approx_zero (a->v[8], 1e-05f)) { return -1; } normalize_row (a, s, 2, 2); v->x = s[0]; @@ -1344,10 +1510,10 @@ sweet_matrix_gaussian_elimination3 (mat3 * a, vec3 * v) } int -sweet_matrix_solve3 (mat3 * m, vec3 * v) +mat3_solve (mat3 * m, vec3 * v) { mat3 a = *m; - if (sweet_matrix_gaussian_elimination3 (&a, v) < 0) { return -1; } + if (mat3_gaussian_elimination (&a, v) < 0) { return -1; } v->y = v->y - v->z * a.v[7]; v->x = v->x - v->z * a.v[6] - v->y * a.v[3]; @@ -1359,10 +1525,10 @@ static void build_Q0 (mat3 * q, mat3 * m) float a = sqrt (m->v[0] * m->v[0] + m->v[1] * m->v[1] + m->v[2] * m->v[2]); if (m->v[0] > 0) { a = - a; } - vec3 v = sweet_vector_new3 (m->v[0] - a, m->v[1], m->v[2]); - float norm = sweet_vector_norm3 (v); + vec3 v = vec3_new (m->v[0] - a, m->v[1], m->v[2]); + float norm = vec3_norm (v); if (norm > 0) - v = sweet_vector_scale3 (v, 1.0 / norm); + v = vec3_scale (v, 1.0 / norm); q->v[0] = 1 - 2 * v.x * v.x; q->v[1] = - 2 * v.y * v.x; @@ -1382,10 +1548,10 @@ static void build_Q1 (mat3 * q, mat3 * m) float a = sqrt (m->v[4] * m->v[4] + m->v[5] * m->v[5]); if (m->v[4] > 0) { a = -a; } - vec2 v = sweet_vector_new2 (m->v[4] - a, m->v[5]); - float norm = sweet_vector_norm2 (v); + vec2 v = vec2_new (m->v[4] - a, m->v[5]); + float norm = vec2_norm (v); if (norm > 0) - v = sweet_vector_scale2 (v, 1.0 / norm); + v = vec2_scale (v, 1.0 / norm); q->v[0] = 1; q->v[1] = 0; @@ -1398,22 +1564,22 @@ static void build_Q1 (mat3 * q, mat3 * m) q->v[8] = 1 - 2 * v.y * v.y; } -void sweet_matrix_QR3 (mat3 * Q, mat3 * R, mat3 * m) +void mat3_QR (mat3 * Q, mat3 * R, mat3 * m) { mat3 tmp; mat3 Q0; mat3 Q1; build_Q0 (&Q0, m); - sweet_matrix_product3 (&tmp, &Q0, m); + mat3_product (&tmp, &Q0, m); build_Q1 (&Q1, &tmp); - sweet_matrix_product3 (Q, &Q0, &Q1); + mat3_product (Q, &Q0, &Q1); - sweet_matrix_transpose3 (&tmp, Q); - sweet_matrix_product3 (R, &tmp, m); + mat3_transpose (&tmp, Q); + mat3_product (R, &tmp, m); } -void sweet_matrix_hess3 (mat3 * H, mat3 * Q, mat3 * m) +void mat3_hess (mat3 * H, mat3 * Q, mat3 * m) { mat3 tmp; @@ -1421,10 +1587,10 @@ void sweet_matrix_hess3 (mat3 * H, mat3 * Q, mat3 * m) if (m->v[1] > 0) a = - a; - vec2 v = sweet_vector_new2 (m->v[1] - a, m->v[2]); - float norm = sweet_vector_norm2 (v); + vec2 v = vec2_new (m->v[1] - a, m->v[2]); + float norm = vec2_norm (v); if (norm > 0) - v = sweet_vector_scale2 (v, 1.0/norm); + v = vec2_scale (v, 1.0/norm); Q->v[0] = 1; Q->v[1] = 0; @@ -1436,8 +1602,8 @@ void sweet_matrix_hess3 (mat3 * H, mat3 * Q, mat3 * m) Q->v[7] = - 2 * v.x * v.y; Q->v[8] = 1 - 2 * v.y * v.y; - sweet_matrix_product3 (&tmp, Q, m); - sweet_matrix_product3 (H, &tmp, Q); + mat3_product (&tmp, Q, m); + mat3_product (H, &tmp, Q); } static void shift (mat3 * m, float s) @@ -1448,15 +1614,6 @@ static void shift (mat3 * m, float s) } -void print_matrix (char * title, mat3 * m) -{ - printf ("%s:\n[%.10f %.10f %.10f]\n[%.10f %.10f %.10f]\n[%.10f %.10f %.10f]\n", - title, - m->v[0], m->v[3], m->v[6], - m->v[1], m->v[4], m->v[7], - m->v[2], m->v[5], m->v[8]); -} - static void sort3 (float * val, vec3 * vec) { if (val[1] > val[0]) @@ -1477,7 +1634,6 @@ static void sort3 (float * val, vec3 * vec) vec[1] = vec[2]; vec[2] = v; } - /* if (val[1] > val[0]) { float t = val[0]; @@ -1486,46 +1642,38 @@ static void sort3 (float * val, vec3 * vec) val[1] = t; vec[0] = vec[1]; vec[1] = v; - }*/ + } } -void sweet_matrix_eigen_vectors3 (vec3 * eigen_vectors, float * eigen_values, mat3 * A, int it) +void mat3_eigen_vectors (vec3 * eigen_vectors, float * eigen_values, mat3 * A, int it) { int i; mat3 H, S, Q, W, R; - sweet_matrix_hess3 (&H, &W, A); + mat3_hess (&H, &W, A); float s = H.v[8]; shift (&H, -H.v[8]); - sweet_matrix_QR3 (&Q, &R, &H); - sweet_matrix_product3 (&H, &R, &Q); + mat3_QR (&Q, &R, &H); + mat3_product (&H, &R, &Q); S = Q; for (i = 0; i < it; i++) { - sweet_matrix_QR3 (&Q, &R, &H); - sweet_matrix_product3 (&H, &R, &Q); - sweet_matrix_product3 (&S, &S, &Q); + mat3_QR (&Q, &R, &H); + mat3_product (&H, &R, &Q); + mat3_product (&S, &S, &Q); } shift (&H, s); - sweet_matrix_product3 (&S, &W, &S); + mat3_product (&S, &W, &S); eigen_values[0] = H.v[0]; eigen_values[1] = H.v[4]; eigen_values[2] = H.v[8]; - eigen_vectors[0] = sweet_vector_new3 (S.v[0], S.v[1], S.v[2]); - eigen_vectors[1] = sweet_vector_new3 (S.v[3], S.v[4], S.v[5]); - eigen_vectors[2] = sweet_vector_new3 (S.v[6], S.v[7], S.v[8]); + eigen_vectors[0] = vec3_new (S.v[0], S.v[1], S.v[2]); + eigen_vectors[1] = vec3_new (S.v[3], S.v[4], S.v[5]); + eigen_vectors[2] = vec3_new (S.v[6], S.v[7], S.v[8]); sort3 (eigen_values, eigen_vectors); - -/* - printf ("%f %f %f\n", eigen_vectors[0].x, eigen_vectors[0].y, eigen_vectors[0].z); - printf ("%f %f %f\n", eigen_vectors[1].x, eigen_vectors[1].y, eigen_vectors[1].z); - printf ("%f %f %f\n", eigen_vectors[2].x, eigen_vectors[2].y, eigen_vectors[2].z); - - printf ("%f %f %f\n", eigen_values[0], eigen_values[1], eigen_values[2]); - */ } diff --git a/sweet_matrix.h b/sweet_matrix.h index 2e69a8e..7ac2c33 100644 --- a/sweet_matrix.h +++ b/sweet_matrix.h @@ -23,26 +23,40 @@ /* Matrix */ +mat2 mat2_new_m3 (mat3 m); +mat2 mat2_new_v2 (vec2 e1, vec2 e2); +mat2 mat2_new_3f (float a, float b, float c); +mat2 mat2_new_4f (float f0, float f1, float f2, float f3); + +mat3 mat3_new_m4 (mat4 m); +mat3 mat3_new_2v (vec2 e1, vec2 e2); +mat3 mat3_new_3v (vec3 e1, vec3 e2, vec3 e3); + +mat4 mat4_new_m4 (mat4 m); +mat4 mat4_new_2v (vec2 e1, vec2 e2); +mat4 mat4_new_3v (vec3 e1, vec3 e2, vec3 e3); +mat4 mat4_new_4v (vec4 e1, vec4 e2, vec4 e3, vec4 e4); + /** Null Matrix */ /** @return Null matrix */ -mat2 sweet_matrix_null2 (void); -mat3 sweet_matrix_null3 (void); -mat4 sweet_matrix_null4 (void); +mat2 mat2_null (void); +mat3 mat3_null (void); +mat4 mat4_null (void); /** Identity */ /** @return Indentity matrix */ -mat2 sweet_matrix_identity2 (void); -mat3 sweet_matrix_identity3 (void); -mat4 sweet_matrix_identity4 (void); +mat2 mat2_identity (void); +mat3 mat3_identity (void); +mat4 mat4_identity (void); /** Basis */ /** Vectors must be orthogonal */ /** @param u, v, w, t as vector of the new basis */ /** @return transform matrix to the new basis */ -mat2 sweet_matrix_ortho_basis2 (vec2 u, vec2 v); -mat3 sweet_matrix_ortho_basis3 (vec3 u, vec3 v, vec3 w); -mat4 sweet_matrix_ortho_basis3h (vec3 p, vec3 u, vec3 v, vec3 w); -mat4 sweet_matrix_ortho_basis4 (vec4 u, vec4 v, vec4 w, vec4 t); +mat2 mat2_ortho_basis (vec2 u, vec2 v); +mat3 mat3_ortho_basis (vec3 u, vec3 v, vec3 w); +mat4 mat3h_ortho_basis (vec3 p, vec3 u, vec3 v, vec3 w); +mat4 mat4_ortho_basis (vec4 u, vec4 v, vec4 w, vec4 t); /** Frustum */ /** @param Right as flaot */ @@ -52,9 +66,9 @@ mat4 sweet_matrix_ortho_basis4 (vec4 u, vec4 v, vec4 w, vec4 t); /** @param zNear as float */ /** @param zFar as float */ /** @return Perspective matrix 4x4 */ -mat4 sweet_matrix_frustum (float left, float right, - float bottom, float top, - float zNear, float zFar); +mat4 matrix_frustum (float left, float right, + float bottom, float top, + float zNear, float zFar); /** Perspective */ /** @param fovy of the camera in degree as flaot */ @@ -62,7 +76,7 @@ mat4 sweet_matrix_frustum (float left, float right, /** @param zNear as float */ /** @param zFar as float */ /** @return Perspective matrix 4x4 */ -mat4 sweet_matrix_perspective (double fovy, double aspect, double z_near, double z_far); +mat4 matrix_perspective (double fovy, double aspect, double z_near, double z_far); /** Ortho */ /** @param Right as flaot */ @@ -72,108 +86,113 @@ mat4 sweet_matrix_perspective (double fovy, double aspect, double z_near, double /** @param zNear as float */ /** @param zFar as float */ /** @return Orthogonal matrix 4x4 */ -mat4 sweet_matrix_ortho (float left, float right, - float bottom, float top, - float zNear, float zFar); +mat4 matrix_ortho (float left, float right, + float bottom, float top, + float zNear, float zFar); /** @return camera matrix */ -mat4 sweet_matrix_look_at (vec3 pos, vec3 dir, vec3 up); - -mat4 sweet_matrix_look_at_ortho (vec3 pos, vec3 dir, vec3 up, vec3 right); +mat4 matrix_look_at (vec3 pos, vec3 dir, vec3 up); +mat4 matrix_look_at_ortho (vec3 pos, vec3 dir, vec3 up, vec3 right); /** Rotation from quaterion */ /** @param quaternion representing rotation */ /** @return 3d Rotation matrix */ -mat3 sweet_matrix_quat_rotation3 (quaternion q); -mat4 sweet_matrix_quat_rotation3h (quaternion q); +mat3 mat3_quat_rotation (quaternion q); +mat4 mat3h_quat_rotation (quaternion q); /** Rotation */ /** @param Angle in radian as flaot */ /** @param X, Y, Z as rotation component */ /** @return 3d Rotation matrix */ -mat2 sweet_matrix_rotation2 (float angle); -mat3 sweet_matrix_rotation2h (float angle); -mat3 sweet_matrix_rotation3 (float angle, float x, float y, float z); -mat4 sweet_matrix_rotation3h (float angle, float x, float y, float z); +mat2 mat2_rotation (float angle); +mat3 mat2h_rotation (float angle); +mat3 mat3_rotation (float angle, float x, float y, float z); +mat4 mat3h_rotation (float angle, float x, float y, float z); /** Translation */ /** @param X,Y,Z or vector */ /** @return Translation matrix */ -mat4 sweet_matrix_translation3h (vec3 v); -mat3 sweet_matrix_translation2h (vec2 v); +mat3 mat3_translation (vec2 v); +mat4 mat4_translation (vec3 v); +#define mat2h_translation mat3_translation +#define mat3h_translation mat4_translation /** Scale */ /** @param X,Y,Z scale component */ /** @return Scale matrix */ -mat4 sweet_matrix_scale4 (float x, float y, float z, float w); -mat3 sweet_matrix_scale3 (float x, float y, float z); -mat2 sweet_matrix_scale2 (float x, float y); +mat2 mat2_scale (float x, float y); +mat3 mat3_scale (float x, float y, float z); +mat4 mat4_scale (float x, float y, float z, float w); /** Texture bias */ /** @return texture bias matrix */ -mat4 sweet_matrix_texture_bias (void); +mat4 matrix_texture_bias (void); /** Transpose */ /** @param Matrix to transpose as mat2 *, mat3 * or mat4 * */ -void sweet_matrix_transpose2 (mat2 * transpose, mat2 * matrix); -void sweet_matrix_transpose3 (mat3 * transpose, mat3 * matrix); -void sweet_matrix_transpose4 (mat4 * transpose, mat4 * matrix); +void mat2_transpose (mat2 * transpose, mat2 * matrix); +void mat3_transpose (mat3 * transpose, mat3 * matrix); +void mat4_transpose (mat4 * transpose, mat4 * matrix); /** Det */ /** @param Matrix as mat2, mat3 or mat4 */ /** @return Determinant as float */ -float sweet_matrix_det2 (mat2 * matrix); -float sweet_matrix_det3 (mat3 * matrix); -float sweet_matrix_det4 (mat4 * matrix); +float mat2_det (mat2 * matrix); +float mat3_det (mat3 * matrix); +float mat4_det (mat4 * matrix); /** Inverse */ /** @param matrix as mat2 *, mat3 * or mat4 * */ /** @param matrix to inverse as mat2 *, mat3 * or mat4 * */ -void sweet_matrix_inverse2 (mat2 * inverse, mat2 * matrix); -void sweet_matrix_inverse3 (mat3 * inverse, mat3 * matrix); -void sweet_matrix_inverse4 (mat4 * inverse, mat4 * matrix); +void mat2_inverse (mat2 * inverse, mat2 * matrix); +void mat3_inverse (mat3 * inverse, mat3 * matrix); +void mat4_inverse (mat4 * inverse, mat4 * matrix); /** Product */ /** @param Product of the Matrix1 times Matrix2 as mat2 *, mat3 * or mat4 * */ /** @param Matrix1, Matrix2 as mat2 *, mat3 * or mat4 * */ -void sweet_matrix_product2 (mat2 * product, mat2 * matrix1, mat2 * matrix2); -void sweet_matrix_product3 (mat3 * product, mat3 * matrix1, mat3 * matrix2); -void sweet_matrix_product4 (mat4 * product, mat4 * matrix1, mat4 * matrix2); +void mat2_product (mat2 * product, mat2 * matrix1, mat2 * matrix2); +void mat3_product (mat3 * product, mat3 * matrix1, mat3 * matrix2); +void mat4_product (mat4 * product, mat4 * matrix1, mat4 * matrix2); /** Matrix Cast */ /** Extract upper left sub matrix */ /** @param sub as mat2 * or mat3 * */ /** @param Matrix */ -void sweet_matrix_sub3 (mat2 * sub, mat3 * matrix); -void sweet_matrix_sub4 (mat3 * sub, mat4 * matrix); +void mat3_submatrix (mat2 * sub, mat3 * matrix); +void mat4_submatrix (mat3 * sub, mat4 * matrix); /** Column*/ /** @param Matrix */ /** @param Column id as column number : begin at 0 */ /** @return matrix column as vec2, vec3 or vec4 */ -vec2 sweet_matrix_column2 (mat2 * matrix, int column_id); -vec3 sweet_matrix_column3 (mat3 * matrix, int column_id); -vec4 sweet_matrix_column4 (mat4 * matrix, int column_id); +vec2 mat2_column (mat2 * matrix, int column_id); +vec3 mat3_column (mat3 * matrix, int column_id); +vec4 mat4_column (mat4 * matrix, int column_id); /** Mult*/ /** @param Matrix */ /** @param Vector */ /** @return Matrix * Vector */ -vec2 sweet_matrix_mult2 (mat2 * matrix, vec2 vector); -vec3 sweet_matrix_mult3 (mat3 * matrix, vec3 vector); -vec4 sweet_matrix_mult4 (mat4 * matrix, vec4 vector); +vec2 mat2_mult (mat2 * matrix, vec2 vector); +vec3 mat3_mult (mat3 * matrix, vec3 vector); +vec4 mat4_mult (mat4 * matrix, vec4 vector); + + +void mat3_QR (mat3 * Q, mat3 * R, mat3 * m); +void mat3_hess (mat3 * H, mat3 * Q, mat3 * m); /** Eigen Value */ -int sweet_matrix_eigen_value3 (float * a, float * b, float * c, mat3 * m); +int mat3_eigen_values (float * a, float * b, float * c, mat3 * m); /** Gaussian elimination */ -int sweet_matrix_gaussian_elimination3 (mat3 * m, vec3 * v); +int mat3_gaussian_elimination (mat3 * m, vec3 * v); /** Solve linear system */ -int sweet_matrix_solve3 (mat3 * a, vec3 * v); +int mat3_solve (mat3 * a, vec3 * v); /** Eigen Vector */ -void sweet_matrix_eigen_vectors3 (vec3 * eigen_vectors, float * eigen_values, mat3 * A, int it); +void mat3_eigen_vectors (vec3 * eigen_vectors, float * eigen_values, mat3 * A, int it); #endif diff --git a/sweet_matrix_short.c b/sweet_matrix_short.c deleted file mode 100644 index de8b412..0000000 --- a/sweet_matrix_short.c +++ /dev/null @@ -1,179 +0,0 @@ -#include "sweet_types.h" -#include "sweet_matrix_short.h" - - -mat2 mat2_new_m3 (mat3 m) -{ - mat2 n; - n.v[0] = m.v[0]; - n.v[1] = m.v[1]; - n.v[2] = m.v[3]; - n.v[3] = m.v[4]; - return n; -} - -mat2 mat2_new_2v (vec2 e1, vec2 e2) -{ - mat2 m; - m.v[0] = e1.x; - m.v[1] = e1.y; - m.v[2] = e2.x; - m.v[3] = e2.y; - return m; -} - -mat2 mat2_new_3f (float a, float b, float c) -{ - return mat2_new_4f (a, b, c, 0); -} - -mat2 mat2_new_4f (float f0, float f1, float f2, float f3) -{ - mat2 n; - n.v[0] = f0; - n.v[1] = f1; - n.v[2] = f2; - n.v[3] = f3; - return n; -} - -mat3 mat3_new_m4 (mat4 m) -{ - mat3 n; - n.v[0] = m.v[0]; - n.v[1] = m.v[1]; - n.v[2] = m.v[2]; - - n.v[3] = m.v[4]; - n.v[4] = m.v[5]; - n.v[5] = m.v[6]; - - n.v[6] = m.v[8]; - n.v[7] = m.v[9]; - n.v[8] = m.v[10]; - return n; -} - -mat3 mat3_new_2v (vec2 e1, vec2 e2) -{ - mat3 m; - m.v[0] = e1.x; - m.v[1] = e1.y; - m.v[2] = 0; - - m.v[3] = e2.x; - m.v[4] = e2.y; - m.v[5] = 0; - - m.v[6] = 0; - m.v[7] = 0; - m.v[8] = 1; - return m; -} - -mat3 mat3_new_3v (vec3 e1, vec3 e2, vec3 e3) -{ - mat3 m; - m.v[0] = e1.x; - m.v[1] = e1.y; - m.v[2] = e1.z; - - m.v[3] = e2.x; - m.v[4] = e2.y; - m.v[5] = e2.z; - - m.v[6] = e3.x; - m.v[7] = e3.y; - m.v[8] = e3.z; - return m; -} - -mat4 mat4_new_m4 (mat4 m) -{ - return m; -} - -mat4 mat4_new_2v (vec2 e1, vec2 e2) -{ - mat4 m; - m.v[0] = e1.x; - m.v[1] = e1.y; - m.v[2] = 0; - m.v[3] = 0; - - m.v[4] = e2.x; - m.v[5] = e2.y; - m.v[6] = 0; - m.v[7] = 0; - - m.v[8] = 0; - m.v[9] = 0; - m.v[10] = 1; - m.v[11] = 0; - - m.v[12] = 0; - m.v[13] = 0; - m.v[14] = 0; - m.v[15] = 1; - - return m; -} - -mat4 mat4_new_3v (vec3 e1, vec3 e2, vec3 e3) -{ - mat4 m; - - m.v[0] = e1.x; - m.v[1] = e1.y; - m.v[2] = e1.z; - m.v[3] = 0; - - m.v[4] = e2.x; - m.v[5] = e2.y; - m.v[6] = e2.z; - m.v[7] = 0; - - m.v[8] = e3.x; - m.v[9] = e3.y; - m.v[10] = e3.z; - m.v[11] = 0; - - m.v[12] = 0; - m.v[13] = 0; - m.v[14] = 0; - m.v[15] = 1; - return m; -} - -mat4 mat4_new_4v (vec4 e1, vec4 e2, vec4 e3, vec4 e4) -{ - mat4 m; - - m.v[0] = e1.x; - m.v[1] = e1.y; - m.v[2] = e1.z; - m.v[3] = e1.w; - - m.v[4] = e2.x; - m.v[5] = e2.y; - m.v[6] = e2.z; - m.v[7] = e2.w; - - m.v[8] = e3.x; - m.v[9] = e3.y; - m.v[10] = e3.z; - m.v[11] = e3.w; - - m.v[12] = e4.x; - m.v[13] = e4.y; - m.v[14] = e4.z; - m.v[15] = e4.w; - - return m; -} - - - - - - diff --git a/sweet_matrix_short.h b/sweet_matrix_short.h deleted file mode 100644 index ee2126f..0000000 --- a/sweet_matrix_short.h +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Sweet is a small library for basic math and small matrix operations. - * Copyright 2014 Luc Girod. - * - * This library is free software: you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation, either - * version 3 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see . - */ - -#ifndef SWEET_MATRIX_SHORT_H -#define SWEET_MATRIX_SHORT_H - -#include "sweet_matrix.h" - -#define MAT2_BUILDER(_1, _2, _3, _4, NAME, ...) NAME -#define MAT3_BUILDER(_1, _2, _3, NAME, ...) NAME -#define MAT4_BUILDER(_1, _2, _3, _4, NAME, ...) NAME - -#define mat2_new(...) MAT2_BUILDER(__VA_ARGS__, mat2_new_4f, mat2_new_3f, mat2_new_2v, mat2_new_m3, NULL)(__VA_ARGS__) -#define mat3_new(...) MAT3_BUILDER(__VA_ARGS__, mat3_new_3v, mat3_new_2v, mat3_new_m4, NULL)(__VA_ARGS__) -#define mat4_new(...) MAT4_BUILDER(__VA_ARGS__, mat4_new_4v, mat4_new_3v, mat4_new_2v, mat4_new_m4, NULL)(__VA_ARGS__) - -mat2 mat2_new_m3 (mat3 m); -mat2 mat2_new_v2 (vec2 e1, vec2 e2); -mat2 mat2_new_3f (float a, float b, float c); -mat2 mat2_new_4f (float f0, float f1, float f2, float f3); - -mat3 mat3_new_m4 (mat4 m); -mat3 mat3_new_2v (vec2 e1, vec2 e2); -mat3 mat3_new_3v (vec3 e1, vec3 e2, vec3 e3); - -mat4 mat4_new_m4 (mat4 m); -mat4 mat4_new_2v (vec2 e1, vec2 e2); -mat4 mat4_new_3v (vec3 e1, vec3 e2, vec3 e3); -mat4 mat4_new_4v (vec4 e1, vec4 e2, vec4 e3, vec4 e4); - -#define mat_null2 sweet_matrix_null2 -#define mat_null3 sweet_matrix_null3 -#define mat_null4 sweet_matrix_null4 -#define identity2 sweet_matrix_identity2 -#define identity3 sweet_matrix_identity3 -#define identity4 sweet_matrix_identity4 -#define ortho_basis2 sweet_matrix_ortho_basis2 -#define ortho_basis3 sweet_matrix_ortho_basis3 -#define ortho_basis3h sweet_matrix_ortho_basis3h -#define ortho_basis4 sweet_matrix_ortho_basis4 -#define matrix_frustum sweet_matrix_frustum -#define matrix_perspective sweet_matrix_perspective -#define matrix_ortho sweet_matrix_ortho -#define matrix_look_at sweet_matrix_look_at -#define matrix_look_at_ortho sweet_matrix_look_at_ortho -#define matrix_quat_rotation3 sweet_matrix_quat_rotation3 -#define matrix_quat_rotation3h sweet_matrix_quat_rotation3h -#define rotation2 sweet_matrix_rotation2 -#define rotation2h sweet_matrix_rotation2h -#define rotation3 sweet_matrix_rotation3 -#define rotation3h sweet_matrix_rotation3h -#define translation3h sweet_matrix_translation3h -#define translation2h sweet_matrix_translation2h -#define scale4 sweet_matrix_scale4 -#define scale3 sweet_matrix_scale3 -#define scale2 sweet_matrix_scale2 -#define matrix_texture_bias sweet_matrix_texture_bias -#define transpose2 sweet_matrix_transpose2 -#define transpose3 sweet_matrix_transpose3 -#define transpose4 sweet_matrix_transpose4 -#define mat_det2 sweet_matrix_det2 -#define mat_det3 sweet_matrix_det3 -#define mat_det4 sweet_matrix_det4 -#define mat_inverse2 sweet_matrix_inverse2 -#define mat_inverse3 sweet_matrix_inverse3 -#define mat_inverse4 sweet_matrix_inverse4 -#define mat_product2 sweet_matrix_product2 -#define mat_product3 sweet_matrix_product3 -#define mat_product4 sweet_matrix_product4 -#define mat_sub3 sweet_matrix_sub3 -#define mat_sub4 sweet_matrix_sub4 -#define mat_column2 sweet_matrix_column2 -#define mat_column3 sweet_matrix_column3 -#define mat_column4 sweet_matrix_column4 -#define mat_mult2 sweet_matrix_mult2 -#define mat_mult3 sweet_matrix_mult3 -#define mat_mult4 sweet_matrix_mult4 -#define mat3_eigen_value sweet_matrix_eigen_value3 -#define mat3_gaussian_elimination sweet_matrix_gaussian_elimination3 -#define mat3_solve sweet_matrix_solve3 -#define mat3_eigen_vector sweet_matrix_eigen_vectors3 - -#endif - diff --git a/sweet_matrix_stack.c b/sweet_matrix_stack.c deleted file mode 100644 index b38ec14..0000000 --- a/sweet_matrix_stack.c +++ /dev/null @@ -1,199 +0,0 @@ -/* - * Sweet is a small library for basic math and small matrix operations. - * Copyright 2014 Luc Girod. - * - * This library is free software: you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation, either - * version 3 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see . - */ - -#include -#include "sweet_matrix_stack.h" - -matrix_stack2 -sweet_matrix_stack2_new (void) -{ - matrix_stack2 m; - m.position = 0; - m.matrix[0] = sweet_matrix_identity2 (); - return m; -} - -matrix_stack3 -sweet_matrix_stack3_new (void) -{ - matrix_stack3 m; - m.position = 0; - m.matrix[0] = sweet_matrix_identity3 (); - return m; -} - -matrix_stack4 -sweet_matrix_stack4_new (void) -{ - matrix_stack4 m; - m.position = 0; - m.matrix[0] = sweet_matrix_identity4 (); - return m; -} - -int -sweet_matrix_stack2_push (matrix_stack2 * ms) -{ - if (ms->position >= SWEET_MATRIX_STACK_SIZE) { return 1; } - - ms->matrix[ms->position + 1] = ms->matrix[ms->position]; - ms->position++; - return 0; -} - -int -sweet_matrix_stack3_push (matrix_stack3 * ms) -{ - if (ms->position >= SWEET_MATRIX_STACK_SIZE) { return 1; } - - ms->matrix[ms->position + 1] = ms->matrix[ms->position]; - ms->position++; - return 0; -} - -int -sweet_matrix_stack4_push (matrix_stack4 * ms) -{ - if (ms->position >= SWEET_MATRIX_STACK_SIZE) { return 1; } - - ms->matrix[ms->position + 1] = ms->matrix[ms->position]; - ms->position++; - return 0; -} - - -int -sweet_matrix_stack2_pop (matrix_stack2 * ms) -{ - if (ms->position <= 0) { return 1; } - - ms->position--; - return 0; -} - -int -sweet_matrix_stack3_pop (matrix_stack3 * ms) -{ - if (ms->position <= 0) { return 1; } - - ms->position--; - return 0; -} - -int -sweet_matrix_stack4_pop (matrix_stack4 * ms) -{ - if (ms->position <= 0) { return 1; } - - ms->position--; - return 0; -} - -void -sweet_matrix_stack2_mult (matrix_stack2 * ms, mat2 * m) -{ - sweet_matrix_product2 (ms->matrix + ms->position, - ms->matrix + ms->position, m); -} - -void -sweet_matrix_stack3_mult (matrix_stack3 * ms, mat3 * m) -{ - sweet_matrix_product3 (ms->matrix + ms->position, - ms->matrix + ms->position, m); -} - -void -sweet_matrix_stack4_mult (matrix_stack4 * ms, mat4 * m) -{ - sweet_matrix_product4 (ms->matrix + ms->position, - ms->matrix + ms->position, m); -} - -void -sweet_matrix_stack2_identity (matrix_stack2 * ms) -{ - ms->matrix[ms->position] = sweet_matrix_identity2 (); -} - -void -sweet_matrix_stack3_identity (matrix_stack3 * ms) -{ - ms->matrix[ms->position] = sweet_matrix_identity3 (); -} - -void -sweet_matrix_stack4_identity (matrix_stack4 * ms) -{ - ms->matrix[ms->position] = sweet_matrix_identity4 (); -} - -void -sweet_matrix_stack2_set (matrix_stack2 * ms, mat2 * m) -{ - ms->matrix[ms->position] = *m; -} - -void -sweet_matrix_stack3_set (matrix_stack3 * ms, mat3 * m) -{ - ms->matrix[ms->position] = *m; -} - -void -sweet_matrix_stack4_set (matrix_stack4 * ms, mat4 * m) -{ - ms->matrix[ms->position] = *m; -} - -mat2 * -sweet_matrix_stack2_get_matrix_pointer (matrix_stack2 * ms) -{ - return (ms->matrix + ms->position); -} - -mat3 * -sweet_matrix_stack3_get_matrix_pointer (matrix_stack3 * ms) -{ - return (ms->matrix + ms->position); -} - -mat4 * -sweet_matrix_stack4_get_matrix_pointer (matrix_stack4 * ms) -{ - return (ms->matrix + ms->position); -} - -mat2 -sweet_matrix_stack2_get_matrix (matrix_stack2 * ms) -{ - return ms->matrix[ms->position]; -} - -mat3 -sweet_matrix_stack3_get_matrix (matrix_stack3 * ms) -{ - return ms->matrix[ms->position]; -} - -mat4 -sweet_matrix_stack4_get_matrix (matrix_stack4 * ms) -{ - return ms->matrix[ms->position]; -} - diff --git a/sweet_matrix_stack.h b/sweet_matrix_stack.h deleted file mode 100644 index 4fae2e7..0000000 --- a/sweet_matrix_stack.h +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Sweet is a small library for basic math and small matrix operations. - * Copyright 2014 Luc Girod. - * - * This library is free software: you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation, either - * version 3 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see . - */ - -#ifndef SWEET_MATRIX_STACK -#define SWEET_MATRIX_STACK - -#include "sweet_types.h" -#include "sweet_matrix.h" - -#define SWEET_MATRIX_STACK_SIZE 128 - -typedef struct matrix_stack2 -{ - mat2 matrix[SWEET_MATRIX_STACK_SIZE]; - unsigned int position; - -}matrix_stack2; - -typedef struct matrix_stack3 -{ - mat3 matrix[SWEET_MATRIX_STACK_SIZE]; - unsigned int position; - -}matrix_stack3; - -typedef struct matrix_stack4 -{ - mat4 matrix[SWEET_MATRIX_STACK_SIZE]; - unsigned int position; - -}matrix_stack4; - - -matrix_stack2 sweet_matrix_stack2_new (void); - -matrix_stack3 sweet_matrix_stack3_new (void); - -matrix_stack4 sweet_matrix_stack4_new (void); - -int sweet_matrix_stack2_push (matrix_stack2 * ms); - -int sweet_matrix_stack3_push (matrix_stack3 * ms); - -int sweet_matrix_stack4_push (matrix_stack4 * ms); - -int sweet_matrix_stack2_pop (matrix_stack2 * ms); - -int sweet_matrix_stack3_pop (matrix_stack3 * ms); - -int sweet_matrix_stack4_pop (matrix_stack4 * ms); - -void sweet_matrix_stack2_mult (matrix_stack2 * ms, mat2 * m); - -void sweet_matrix_stack3_mult (matrix_stack3 * ms, mat3 * m); - -void sweet_matrix_stack4_mult (matrix_stack4 * ms, mat4 * m); - -void sweet_matrix_stack2_set (matrix_stack2 * ms, mat2 * m); - -void sweet_matrix_stack3_set (matrix_stack3 * ms, mat3 * m); - -void sweet_matrix_stack4_set (matrix_stack4 * ms, mat4 * m); - -mat2 * sweet_matrix_stack2_get_matrix_pointer (matrix_stack2 * m); - -mat3 * sweet_matrix_stack3_get_matrix_pointer (matrix_stack3 * m); - -mat4 * sweet_matrix_stack4_get_matrix_pointer (matrix_stack4 * m); - -mat2 sweet_matrix_stack2_get_matrix (matrix_stack2 * m); - -mat3 sweet_matrix_stack3_get_matrix (matrix_stack3 * m); - -mat4 sweet_matrix_stack4_get_matrix (matrix_stack4 * m); - -#endif -