Compare commits
No commits in common. "master" and "to_test" have entirely different histories.
0
COPYING.LESSER
Executable file → Normal file
0
COPYING.LESSER
Executable file → Normal file
14
Makefile
Executable file → Normal file
14
Makefile
Executable file → Normal file
@ -4,22 +4,28 @@ CC = gcc
|
||||
|
||||
PROG = libsweet.so
|
||||
|
||||
OBJS = sweet_math.o sweet_matrix.o sweet_geometry.o
|
||||
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
|
||||
|
||||
CFLAGS = -pedantic -fPIC -Wall
|
||||
LD = -shared
|
||||
|
||||
ifeq ($(TARGET),short)
|
||||
all: $(PROG)
|
||||
$(PROG): $(OBJS) $(OBJS_WITH_SHORT)
|
||||
$(CC) $(LD) $(OBJS) $(OBJS_WITH_SHORT) -o $(PROG)
|
||||
else
|
||||
all: $(PROG)
|
||||
|
||||
$(PROG): $(OBJS)
|
||||
$(CC) $(LD) $(OBJS) -o $(PROG)
|
||||
endif
|
||||
|
||||
.c.o:
|
||||
$(CC) $(CFLAGS) -c $*.c
|
||||
|
||||
clean:
|
||||
rm -rf $(OBJS)
|
||||
rm -rf $(OBJS) $(OBJS_WITH_SHORT)
|
||||
|
||||
mrproper:
|
||||
rm -rf $(OBJS) $(PROG)
|
||||
rm -rf $(OBJS) $(OBJS_WITH_SHORT) $(PROG)
|
||||
|
||||
|
||||
2
exemple/exemple_geometry.c
Executable file → Normal file
2
exemple/exemple_geometry.c
Executable file → Normal file
@ -13,7 +13,7 @@ main ()
|
||||
unsigned int nb_vertices;
|
||||
float * vertices;
|
||||
unsigned int * indices;
|
||||
nb_faces = math_icosphere (NB_ITERATIONS, SCALE, &nb_elements, &indices, &nb_vertices, &vertices);
|
||||
nb_faces = sweet_geometry_icosphere (NB_ITERATIONS, SCALE, &nb_elements, &indices, &nb_vertices, &vertices);
|
||||
|
||||
printf ("There are %d faces\n", nb_faces);
|
||||
|
||||
|
||||
6
exemple/exemple_matrix.c
Executable file → Normal file
6
exemple/exemple_matrix.c
Executable file → Normal file
@ -15,14 +15,14 @@ print4 (mat4 * m)
|
||||
int
|
||||
main ()
|
||||
{
|
||||
mat4 m = mat3h_rotation (PI_OVER_2, 1.0, 1.0, 1.0);
|
||||
mat4 m = sweet_matrix_rotation3h (SWEET_PI_OVER_2, 1.0, 1.0, 1.0);
|
||||
|
||||
print4 (&m);
|
||||
|
||||
mat4_inverse (&m, &m);
|
||||
sweet_matrix_inverse4 (&m, &m);
|
||||
print4 (&m);
|
||||
|
||||
mat4_inverse (&m, &m);
|
||||
sweet_matrix_inverse4 (&m, &m);
|
||||
print4 (&m);
|
||||
|
||||
return 0;
|
||||
|
||||
38
exemple/exemple_stack.c
Normal file
38
exemple/exemple_stack.c
Normal file
@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
2
sweet.h
Executable file → Normal file
2
sweet.h
Executable file → Normal file
@ -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
|
||||
|
||||
74
sweet_geometry.c
Executable file → Normal file
74
sweet_geometry.c
Executable file → Normal file
@ -19,7 +19,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include "sweet_types.h"
|
||||
#include "sweet_macro.h"
|
||||
#include "sweet_math.h"
|
||||
|
||||
/* Definition of chain list for faces */
|
||||
@ -220,9 +219,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 = vec3_middle (v->vertices[a], v->vertices[b]);
|
||||
middle = sweet_vector_middle3 (v->vertices[a], v->vertices[b]);
|
||||
|
||||
v->vertices[v->nb_vertices++] = vec3_normalize (middle);
|
||||
v->vertices[v->nb_vertices++] = sweet_vector_normalize3 (middle);
|
||||
return id;
|
||||
}
|
||||
|
||||
@ -235,7 +234,7 @@ middle_point_vt (struct map * map, int * state,
|
||||
|
||||
if (*state == 1)
|
||||
{
|
||||
t->uv[t->nb_uv++] = vec2_middle (t->uv[a], t->uv[b]);
|
||||
t->uv[t->nb_uv++] = sweet_vector_middle2 (t->uv[a], t->uv[b]);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
@ -252,7 +251,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] = vec3_normalize (vec3_new (vertices[j], vertices[j+1], vertices[j+2]));
|
||||
v->vertices[i] = sweet_vector_normalize3 (sweet_vector_new3 (vertices[j], vertices[j+1], vertices[j+2]));
|
||||
}
|
||||
|
||||
root = NULL;
|
||||
@ -275,7 +274,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] = vec2_new (tcoord[j], tcoord[j+1]);
|
||||
t->uv[i] = sweet_vector_new2 (tcoord[j], tcoord[j+1]);
|
||||
}
|
||||
|
||||
return first_iteration (v, nb_indices, indices, nb_vertices, vertices);
|
||||
@ -374,11 +373,11 @@ iterate (struct map * map, struct vertex_array * v, struct tcoord_array * t, str
|
||||
}
|
||||
|
||||
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)
|
||||
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)
|
||||
{
|
||||
struct face_item * root;
|
||||
unsigned int nb_faces;
|
||||
@ -432,11 +431,11 @@ subdivide_mesh_vt (unsigned int nb_iterations, float scale,
|
||||
}
|
||||
|
||||
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)
|
||||
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)
|
||||
{
|
||||
struct face_item * root;
|
||||
unsigned int nb_faces;
|
||||
@ -482,20 +481,20 @@ subdivide_mesh_v (unsigned int nb_iterations, float scale,
|
||||
#define NB_VERTICES 12
|
||||
|
||||
static float icosphere_vertices_v[36] = {
|
||||
-1, GOLDEN_RATIO, 0,
|
||||
1, GOLDEN_RATIO, 0,
|
||||
-1, -GOLDEN_RATIO, 0,
|
||||
1, -GOLDEN_RATIO, 0,
|
||||
-1, SWEET_GOLDEN_RATIO, 0,
|
||||
1, SWEET_GOLDEN_RATIO, 0,
|
||||
-1, -SWEET_GOLDEN_RATIO, 0,
|
||||
1, -SWEET_GOLDEN_RATIO, 0,
|
||||
|
||||
0, -1, GOLDEN_RATIO,
|
||||
0, 1, GOLDEN_RATIO,
|
||||
0, -1, -GOLDEN_RATIO,
|
||||
0, 1, -GOLDEN_RATIO,
|
||||
0, -1, SWEET_GOLDEN_RATIO,
|
||||
0, 1, SWEET_GOLDEN_RATIO,
|
||||
0, -1, -SWEET_GOLDEN_RATIO,
|
||||
0, 1, -SWEET_GOLDEN_RATIO,
|
||||
|
||||
GOLDEN_RATIO, 0, -1,
|
||||
GOLDEN_RATIO, 0, 1,
|
||||
-GOLDEN_RATIO, 0, -1,
|
||||
-GOLDEN_RATIO, 0, 1
|
||||
SWEET_GOLDEN_RATIO, 0, -1,
|
||||
SWEET_GOLDEN_RATIO, 0, 1,
|
||||
-SWEET_GOLDEN_RATIO, 0, -1,
|
||||
-SWEET_GOLDEN_RATIO, 0, 1
|
||||
};
|
||||
|
||||
static unsigned int icosphere_indices_v[NB_INDICES] = {
|
||||
@ -526,15 +525,14 @@ static unsigned int icosphere_indices_v[NB_INDICES] = {
|
||||
|
||||
|
||||
int
|
||||
mesh_icosphere (unsigned int nb_iterations, float scale,
|
||||
unsigned int * count_indices, unsigned int ** mesh_indices,
|
||||
unsigned int * count_vertices, float ** mesh_vertices)
|
||||
sweet_geometry_icosphere (unsigned int nb_iterations, float scale,
|
||||
unsigned int * count_indices, unsigned int ** mesh_indices,
|
||||
unsigned int * count_vertices, float ** 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);
|
||||
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);
|
||||
}
|
||||
|
||||
#undef NB_INDICES
|
||||
#undef NB_VERTICES
|
||||
|
||||
@ -569,9 +567,9 @@ static unsigned int cube_indices_v[NB_INDICES] = {
|
||||
|
||||
|
||||
int
|
||||
mesh_cube (float scale,
|
||||
unsigned int * count_indices, unsigned int ** mesh_indices,
|
||||
unsigned int * count_vertices, float ** mesh_vertices)
|
||||
sweet_geometry_cube (float scale,
|
||||
unsigned int * count_indices, unsigned int ** mesh_indices,
|
||||
unsigned int * count_vertices, float ** mesh_vertices)
|
||||
{
|
||||
int i;
|
||||
|
||||
|
||||
33
sweet_geometry.h
Executable file → Normal file
33
sweet_geometry.h
Executable file → Normal file
@ -20,25 +20,26 @@
|
||||
#define SWEET_GEOMETRY_H
|
||||
|
||||
|
||||
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_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_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_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 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_icosphere (unsigned int nb_iterations, 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);
|
||||
int
|
||||
sweet_geometry_cube (float scale,
|
||||
unsigned int * count_indices, unsigned int ** mesh_indices,
|
||||
unsigned int * count_vertices, float ** mesh_vertices);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@ -1,46 +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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef SWEET_MACRO
|
||||
#define SWEET_MACRO
|
||||
|
||||
#include "sweet_overload_macro.h"
|
||||
|
||||
#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_9_(__VA_ARGS__, mat3_new_9f, NULL, NULL, NULL, NULL, NULL, 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__)
|
||||
|
||||
#define mat2_insert_col(...) _OVERLOAD_4_(__VA_ARGS__, mat2_insert_col_2f, mat2_insert_col_1v, NULL, NULL)(__VA_ARGS__)
|
||||
#define mat3_insert_col(...) _OVERLOAD_5_(__VA_ARGS__, mat3_insert_col_3f, mat3_insert_col_1v1f, mat3_insert_col_1v, NULL, NULL)(__VA_ARGS__)
|
||||
#define mat4_insert_col(...) _OVERLOAD_6_(__VA_ARGS__, mat4_insert_col_4f, mat4_insert_col_1v2f, mat4_insert_col_1v1f, mat4_insert_col_1v, NULL, NULL)(__VA_ARGS__)
|
||||
|
||||
#define swap(a, b, type) \
|
||||
{\
|
||||
type t;\
|
||||
t = a;\
|
||||
a = b;\
|
||||
b = t;\
|
||||
\
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
1403
sweet_math.c
Executable file → Normal file
1403
sweet_math.c
Executable file → Normal file
File diff suppressed because it is too large
Load Diff
358
sweet_math.h
Executable file → Normal file
358
sweet_math.h
Executable file → Normal file
@ -30,216 +30,42 @@
|
||||
**/
|
||||
|
||||
/** This macro define well known numbers with a relative great precision */
|
||||
#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
|
||||
#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
|
||||
|
||||
/** RadianToDegree */
|
||||
/** @param Radian as float or double */
|
||||
/** @return Degree as float */
|
||||
#define radian_2_degree(radian) \
|
||||
#define sweet_math_radian_2_degree(radian) \
|
||||
radian * 57.2957795130823208767981548
|
||||
|
||||
/** DegreeToRadian */
|
||||
/** @param Degree as float or double */
|
||||
/** @return Radian as float */
|
||||
#define degree_2_radian(degree) \
|
||||
#define sweet_math_degree_2_radian(degree) \
|
||||
degree * 0.01745329251994329576923690768488
|
||||
|
||||
#define float_approx_equals(a, b, epsilon) \
|
||||
#define sweet_math_approx_equals(a, b, epsilon) \
|
||||
(a <= (b+epsilon) && a >= (b-epsilon))
|
||||
|
||||
#define float_approx_zero(a, epsilon) \
|
||||
(a <= (epsilon) && a >= (-epsilon))
|
||||
#define sweet_math_approx_zero(a, epsilon) \
|
||||
(a <= (epsilon) && a >= (epsilon))
|
||||
|
||||
#define max(a, b) (a >= b ? a : b)
|
||||
#define min(a, b) (a <= b ? a : b)
|
||||
|
||||
/* */
|
||||
void random_float_numbers(float * array, int n);
|
||||
|
||||
/* 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);
|
||||
|
||||
/** Interpolate */
|
||||
vec2 vec2_interpolate (vec2 u, vec2 v, float t);
|
||||
vec3 vec3_interpolate (vec3 u, vec3 v, float t);
|
||||
vec4 vec4_interpolate (vec4 u, vec4 v, float t);
|
||||
|
||||
/** 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 */
|
||||
vec2 vec2_positive_random ();
|
||||
vec3 vec3_positive_random ();
|
||||
vec4 vec4_positive_random ();
|
||||
|
||||
vec2 vec2_random ();
|
||||
vec3 vec3_random ();
|
||||
vec4 vec4_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);
|
||||
|
||||
/* Integer part */
|
||||
vec2i vec2_int (vec2 v);
|
||||
vec3i vec3_int (vec3 v);
|
||||
vec4i vec4_int (vec4 v);
|
||||
|
||||
/* Integer vector builder */
|
||||
vec2i vec2i_new (int x, int y);
|
||||
vec3i vec3i_new (int x, int y, int z);
|
||||
vec4i vec4i_new (int x, int y, int z, int w);
|
||||
|
||||
/* Integer vector addition */
|
||||
vec2i vec2i_add (vec2i vector1, vec2i vector2);
|
||||
vec3i vec3i_add (vec3i vector1, vec3i vector2);
|
||||
vec4i vec4i_add (vec4i vector1, vec4i vector2);
|
||||
|
||||
/* Quaternion */
|
||||
quat quat_new (float w, float x, float y, float z);
|
||||
quat quat_rotation (float angle, float x, float y, float z);
|
||||
quat quat_conjugate (quat q);
|
||||
quat quat_add (quat q1, quat q2);
|
||||
quat quat_product (quat q1, quat q2);
|
||||
float quat_norm (quat q);
|
||||
quat quat_inverse (quat q);
|
||||
quat quat_normalize (quat q);
|
||||
quat quat_interpolate (quat q1, quat q2, float t);
|
||||
/** Invsqrt */
|
||||
/** @param x argument of square root as float */
|
||||
/** @return 1 over square root as float */
|
||||
float sweet_math_invsqrt (float x);
|
||||
|
||||
/** 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 nearest_integer (int number, int nb_args, ...);
|
||||
int sweet_math_nearest (int number, int nb_args, ...);
|
||||
|
||||
/** Quadratic_polynomial */
|
||||
/** @param r 2D Vector for the reals roots as vec2 pointer */
|
||||
@ -247,7 +73,7 @@ int nearest_integer (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 quadratic_polynomial (vec2 * r, float a, float b, float c);
|
||||
int sweet_math_quadratic_polynomial (vec2 * r, float a, float b, float c);
|
||||
|
||||
/** Cubic_polynomial */
|
||||
/** @param r 3D Vector containing the reals roots as vec3 pointer */
|
||||
@ -256,8 +82,156 @@ int 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 cubic_polynomial (vec3 * r, float a, float b, float c, float d);
|
||||
int sweet_math_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);
|
||||
|
||||
/** 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);
|
||||
|
||||
/** 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 */
|
||||
|
||||
|
||||
54
sweet_math_short.c
Normal file
54
sweet_math_short.c
Normal file
@ -0,0 +1,54 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
|
||||
103
sweet_math_short.h
Normal file
103
sweet_math_short.h
Normal file
@ -0,0 +1,103 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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_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
|
||||
|
||||
998
sweet_matrix.c
Executable file → Normal file
998
sweet_matrix.c
Executable file → Normal file
File diff suppressed because it is too large
Load Diff
169
sweet_matrix.h
Executable file → Normal file
169
sweet_matrix.h
Executable file → Normal file
@ -23,48 +23,25 @@
|
||||
|
||||
/* 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);
|
||||
mat3 mat3_new_9f(float a0, float a1, float a2,
|
||||
float a3, float a4, float a5,
|
||||
float a6, float a7, float a8);
|
||||
|
||||
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);
|
||||
|
||||
|
||||
void mat2_to_str(mat2 * m, char * str, int n);
|
||||
void mat3_to_str(mat3 * m, char * str, int n);
|
||||
void mat4_to_str(mat4 * m, char * str, int n);
|
||||
|
||||
/** Null Matrix */
|
||||
/** @return Null matrix */
|
||||
mat2 mat2_null (void);
|
||||
mat3 mat3_null (void);
|
||||
mat4 mat4_null (void);
|
||||
mat2 sweet_matrix_null2 (void);
|
||||
mat3 sweet_matrix_null3 (void);
|
||||
mat4 sweet_matrix_null4 (void);
|
||||
|
||||
/** Identity */
|
||||
/** @return Indentity matrix */
|
||||
mat2 mat2_identity (void);
|
||||
mat3 mat3_identity (void);
|
||||
mat4 mat4_identity (void);
|
||||
mat2 sweet_matrix_identity2 (void);
|
||||
mat3 sweet_matrix_identity3 (void);
|
||||
mat4 sweet_matrix_identity4 (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 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);
|
||||
mat2 sweet_matrix_ortho_basis2 (vec2 u, vec2 v);
|
||||
mat3 sweet_matrix_ortho_basis3 (vec3 u, vec3 v, vec3 w);
|
||||
mat4 sweet_matrix_ortho_basis4 (vec4 u, vec4 v, vec4 w, vec4 t);
|
||||
|
||||
/** Frustum */
|
||||
/** @param Right as flaot */
|
||||
@ -74,9 +51,9 @@ mat4 mat4_ortho_basis (vec4 u, vec4 v, vec4 w, vec4 t);
|
||||
/** @param zNear as float */
|
||||
/** @param zFar as float */
|
||||
/** @return Perspective matrix 4x4 */
|
||||
mat4 matrix_frustum (float left, float right,
|
||||
float bottom, float top,
|
||||
float zNear, float zFar);
|
||||
mat4 sweet_matrix_frustum (float left, float right,
|
||||
float bottom, float top,
|
||||
float zNear, float zFar);
|
||||
|
||||
/** Perspective */
|
||||
/** @param fovy of the camera in degree as flaot */
|
||||
@ -84,17 +61,7 @@ mat4 matrix_frustum (float left, float right,
|
||||
/** @param zNear as float */
|
||||
/** @param zFar as float */
|
||||
/** @return Perspective matrix 4x4 */
|
||||
mat4 matrix_perspective (double fovy, double aspect, double z_near, double z_far);
|
||||
|
||||
/** Perspective infinite */
|
||||
/** @param fovy of the camera in degree as flaot */
|
||||
/** @param aspect of the view as float */
|
||||
/** @param zNear as float */
|
||||
/** @return Perspective matrix 4x4 but far plant is handled in a way this projection
|
||||
** can support really far values.
|
||||
*/
|
||||
mat4 matrix_perspective_infinite (double fovy, double aspect, double z_near);
|
||||
|
||||
mat4 sweet_matrix_perspective (double fovy, double aspect, double z_near, double z_far);
|
||||
|
||||
/** Ortho */
|
||||
/** @param Right as flaot */
|
||||
@ -104,134 +71,106 @@ mat4 matrix_perspective_infinite (double fovy, double aspect, double z_near);
|
||||
/** @param zNear as float */
|
||||
/** @param zFar as float */
|
||||
/** @return Orthogonal matrix 4x4 */
|
||||
mat4 matrix_ortho (float left, float right,
|
||||
float bottom, float top,
|
||||
float zNear, float zFar);
|
||||
mat4 sweet_matrix_ortho (float left, float right,
|
||||
float bottom, float top,
|
||||
float zNear, float zFar);
|
||||
|
||||
/** @return camera matrix */
|
||||
mat4 matrix_look_at (vec3 pos, vec3 dir, vec3 up);
|
||||
mat4 matrix_look_at_ortho (vec3 pos, vec3 dir, vec3 up, vec3 right);
|
||||
|
||||
|
||||
quat mat3_to_quaternion (mat3 * m);
|
||||
mat4 sweet_matrix_look_at (vec3 pos, vec3 dir, vec3 up);
|
||||
|
||||
/** Rotation from quaterion */
|
||||
/** @param quaternion representing rotation */
|
||||
/** @return 3d Rotation matrix */
|
||||
mat3 mat3_quat_rotation (quat q);
|
||||
mat4 mat3h_quat_rotation (quat q);
|
||||
mat3 sweet_matrix_quat_rotation3 (quaternion q);
|
||||
mat4 sweet_matrix_quat_rotation3h (quaternion q);
|
||||
|
||||
/** Rotation */
|
||||
/** @param Angle in radian as flaot */
|
||||
/** @param X, Y, Z as rotation component */
|
||||
/** @return 3d Rotation matrix */
|
||||
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);
|
||||
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);
|
||||
|
||||
/** Translation */
|
||||
/** @param X,Y,Z or vector */
|
||||
/** @return Translation matrix */
|
||||
mat3 mat3_translation (vec2 v);
|
||||
mat4 mat4_translation (vec3 v);
|
||||
#define mat2h_translation mat3_translation
|
||||
#define mat3h_translation mat4_translation
|
||||
mat4 sweet_matrix_translation3h (vec3 v);
|
||||
mat3 sweet_matrix_translation2h (vec2 v);
|
||||
|
||||
/** Scale */
|
||||
/** @param X,Y,Z scale component */
|
||||
/** @return Scale matrix */
|
||||
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);
|
||||
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);
|
||||
|
||||
/** Texture bias */
|
||||
/** @return texture bias matrix */
|
||||
mat4 matrix_texture_bias (void);
|
||||
mat4 sweet_matrix_texture_bias (void);
|
||||
|
||||
/** Transpose */
|
||||
/** @param Matrix to transpose as mat2 *, mat3 * or mat4 * */
|
||||
void mat2_transpose (mat2 * transpose, mat2 * matrix);
|
||||
void mat3_transpose (mat3 * transpose, mat3 * matrix);
|
||||
void mat4_transpose (mat4 * transpose, mat4 * matrix);
|
||||
void sweet_matrix_transpose2 (mat2 * transpose, mat2 * matrix);
|
||||
void sweet_matrix_transpose3 (mat3 * transpose, mat3 * matrix);
|
||||
void sweet_matrix_transpose4 (mat4 * transpose, mat4 * matrix);
|
||||
|
||||
/** Det */
|
||||
/** @param Matrix as mat2, mat3 or mat4 */
|
||||
/** @return Determinant as float */
|
||||
float mat2_det (mat2 * matrix);
|
||||
float mat3_det (mat3 * matrix);
|
||||
float mat4_det (mat4 * matrix);
|
||||
float sweet_matrix_det2 (mat2 * matrix);
|
||||
float sweet_matrix_det3 (mat3 * matrix);
|
||||
float sweet_matrix_det4 (mat4 * matrix);
|
||||
|
||||
/** Inverse */
|
||||
/** @param matrix as mat2 *, mat3 * or mat4 * */
|
||||
/** @param matrix to inverse as mat2 *, mat3 * or mat4 * */
|
||||
void mat2_inverse (mat2 * inverse, mat2 * matrix);
|
||||
void mat3_inverse (mat3 * inverse, mat3 * matrix);
|
||||
void mat4_inverse (mat4 * inverse, mat4 * matrix);
|
||||
void sweet_matrix_inverse2 (mat2 * inverse, mat2 * matrix);
|
||||
void sweet_matrix_inverse3 (mat3 * inverse, mat3 * matrix);
|
||||
void sweet_matrix_inverse4 (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 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);
|
||||
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);
|
||||
|
||||
/** Matrix Cast */
|
||||
/** Extract upper left sub matrix */
|
||||
/** @param sub as mat2 * or mat3 * */
|
||||
/** @param Matrix */
|
||||
void mat3_submatrix (mat2 * sub, mat3 * matrix);
|
||||
void mat4_submatrix (mat3 * sub, mat4 * matrix);
|
||||
void sweet_matrix_sub3 (mat2 * sub, mat3 * matrix);
|
||||
void sweet_matrix_sub4 (mat3 * sub, mat4 * matrix);
|
||||
|
||||
/** Column */
|
||||
/** Column*/
|
||||
/** @param Matrix */
|
||||
/** @param Column id as column number : begin at 0 */
|
||||
/** @return matrix column as vec2, vec3 or vec4 */
|
||||
vec2 mat2_column (mat2 * matrix, int column_id);
|
||||
vec3 mat3_column (mat3 * matrix, int column_id);
|
||||
vec4 mat4_column (mat4 * matrix, int column_id);
|
||||
|
||||
/** Insert */
|
||||
/** @param Matrix */
|
||||
/** @param Column id as column number : begin at 0 */
|
||||
/** @param column component */
|
||||
void mat2_insert_col_1v (mat2 * matrix, int column_id, vec2 v);
|
||||
void mat2_insert_col_2f (mat2 * matrix, int column_id, float x, float y);
|
||||
|
||||
void mat3_insert_col_1v (mat3 * matrix, int column_id, vec3 v);
|
||||
void mat3_insert_col_1v1f (mat3 * matrix, int column_id, vec2 v, float z);
|
||||
void mat3_insert_col_3f (mat3 * matrix, int column_id, float x, float y, float z);
|
||||
|
||||
void mat4_insert_col_1v (mat4 * matrix, int column_id, vec4 v);
|
||||
void mat4_insert_col_1v1f (mat4 * matrix, int column_id, vec3 v, float w);
|
||||
void mat4_insert_col_1v2f (mat4 * matrix, int column_id, vec2 v, float z, float w);
|
||||
void mat4_insert_col_4f (mat4 * matrix, int column_id, float x, float y, float z, float w);
|
||||
|
||||
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);
|
||||
|
||||
/** Mult*/
|
||||
/** @param Matrix */
|
||||
/** @param Vector */
|
||||
/** @return Matrix * 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);
|
||||
vec2 sweet_matrix_mult2 (mat2 * matrix, vec2 vector);
|
||||
vec3 sweet_matrix_mult3 (mat3 * matrix, vec3 vector);
|
||||
vec4 sweet_matrix_mult4 (mat4 * matrix, vec4 vector);
|
||||
|
||||
/** Eigen Value */
|
||||
int mat3_eigen_values (float * a, float * b, float * c, mat3 * m);
|
||||
int sweet_matrix_eigen_value3 (float * a, float * b, float * c, mat3 * m);
|
||||
|
||||
/** Gaussian elimination */
|
||||
int mat3_gaussian_elimination (mat3 * m, vec3 * v);
|
||||
int sweet_matrix_gaussian_elimination3 (mat3 * m, vec3 * v);
|
||||
|
||||
/** Solve linear system */
|
||||
int mat3_solve (mat3 * a, vec3 * v);
|
||||
int sweet_matrix_solve3 (mat3 * a, vec3 * v);
|
||||
|
||||
/** Eigen Vector */
|
||||
void mat3_eigen_vectors (vec3 * eigen_vectors, float * eigen_values, mat3 * A, int it);
|
||||
|
||||
int sweet_matrix_eigen_vector3 (vec3 * vectors, mat3 * m);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
179
sweet_matrix_short.c
Normal file
179
sweet_matrix_short.c
Normal file
@ -0,0 +1,179 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
97
sweet_matrix_short.h
Normal file
97
sweet_matrix_short.h
Normal file
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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_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_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_vector3
|
||||
|
||||
#endif
|
||||
|
||||
199
sweet_matrix_stack.c
Normal file
199
sweet_matrix_stack.c
Normal file
@ -0,0 +1,199 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#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];
|
||||
}
|
||||
|
||||
92
sweet_matrix_stack.h
Normal file
92
sweet_matrix_stack.h
Normal file
@ -0,0 +1,92 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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
|
||||
|
||||
@ -1,14 +0,0 @@
|
||||
#ifndef SWEET_OVERLOAD_MACRO
|
||||
#define SWEET_OVERLOAD_MACRO
|
||||
|
||||
#define _OVERLOAD_1_(_1, NAME, ...) NAME
|
||||
#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 _OVERLOAD_5_(_1, _2, _3, _4, _5, NAME, ...) NAME
|
||||
#define _OVERLOAD_6_(_1, _2, _3, _4, _5, _6, NAME, ...) NAME
|
||||
#define _OVERLOAD_7_(_1, _2, _3, _4, _5, _6, _7, NAME, ...) NAME
|
||||
#define _OVERLOAD_8_(_1, _2, _3, _4, _5, _6, _7, _8, NAME, ...) NAME
|
||||
#define _OVERLOAD_9_(_1, _2, _3, _4, _5, _6, _7, _8, _9, NAME, ...) NAME
|
||||
|
||||
#endif
|
||||
21
sweet_types.h
Executable file → Normal file
21
sweet_types.h
Executable file → Normal file
@ -50,32 +50,13 @@ typedef struct vec2 {
|
||||
float y;
|
||||
} vec2;
|
||||
|
||||
typedef struct vec4i {
|
||||
int x;
|
||||
int y;
|
||||
int z;
|
||||
int w;
|
||||
} vec4i;
|
||||
|
||||
typedef struct vec3i {
|
||||
int x;
|
||||
int y;
|
||||
int z;
|
||||
} vec3i;
|
||||
|
||||
typedef struct vec2i {
|
||||
int x;
|
||||
int y;
|
||||
} vec2i;
|
||||
|
||||
typedef struct complex {
|
||||
float r;
|
||||
float i;
|
||||
} complex;
|
||||
|
||||
typedef vec4 quat;
|
||||
|
||||
typedef quat quaternion;
|
||||
typedef vec4 quaternion;
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user