First commit for the free version.

This commit is contained in:
Luc Girod 2014-04-16 01:58:26 +02:00
commit 17fb4378eb
8 changed files with 2568 additions and 0 deletions

166
COPYING.LESSER Normal file
View File

@ -0,0 +1,166 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.

25
Makefile Normal file
View File

@ -0,0 +1,25 @@
# Makefile
UNAME = $(shell uname)
CC = gcc
PROG = libsweet.so
OBJS = sweet_math.o sweet_matrix.o
CFLAGS = -pedantic -static -fPIC
LD = -shared
all: $(PROG)
$(PROG): $(OBJS)
$(CC) $(LD) $(OBJS) -o $(PROG)
.c.o:
$(CC) $(CFLAGS) -c $*.c
clean:
rm -rf $(OBJS)
mrproper:
rm -rf $(OBJS) $(PROG)

29
sweet.h Normal file
View File

@ -0,0 +1,29 @@
/*
* Sweet is a small library for basic math and small matrix operation.
* 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_H
#define SWEET_H
#include "sweet_types.h"
#include "sweet_math.h"
#include "sweet_matrix.h"
#endif

673
sweet_math.c Normal file
View File

@ -0,0 +1,673 @@
/*
* Sweet is a small library for basic math and small matrix operation.
* 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 <stdio.h>
#include <math.h>
#include <stdarg.h>
#include "sweet_math.h"
#include "sweet_types.h"
#define EPSILON 0.0000000001
int
sweet_math_nearest (int number, int size, ...)
{
int i;
int nearest;
va_list ap;
va_start (ap, size);
/* nearest = va_arg (ap, int); */
for (i = 0; i < size; i++)
{
int arg;
arg = va_arg (ap, int);
if (fabs (number - arg) < fabs (number - nearest))
{
nearest = arg;
}
}
va_end (ap);
return nearest;
}
int
sweet_math_quadratic_polynomial (vec2 * r, float a, float b, float c)
{
float delta;
float a2;
a2 = a * 2;
delta = b * b - 2*a2 * c;
if (delta > 0)
{
float d = sqrt (delta);
r->x = (d - b)/a2;
r->y = (-d - b)/a2;
return 2;
}
else if (sweet_math_approx_equals (delta, 0, EPSILON))
{
r->x = -b/a2;
r->y = r->x;
return 1;
}
return 0;
}
#define FRAC_1_ON_3 0.3333333333333333333333333333333333333333333333333333333333333333333
#define FRAC_2_ON_3 0.6666666666666666666666666666666666666666666666666666666666666666666
#define FRAC_1_ON_6 0.1666666666666666666666666666666666666666666666666666666666666666666
#define FRAC_1_ON_9 0.1111111111111111111111111111111111111111111111111111111111111111111
#define FRAC_2_ON_27 0.0740740740740740740740740740740740740740740740740740740740740740740
#define FRAC_1_ON_27 0.0370370370370370370370370370370370370370370370370370370370370370370
int
sweet_math_cubic_polynomial (vec3 * roots, float t, float a, float b, float c)
{
double aa;
double a_over_3;
double p;
double q;
double ppp;
double D;
double r;
double s;
a /= t;
b /= t;
c /= t;
aa = a * a;
p = -FRAC_1_ON_9 * aa + FRAC_1_ON_3 * b;
q = FRAC_1_ON_27 * aa * a - FRAC_1_ON_6 * a * b + 0.5 * c;
ppp = p * p * p;
D = -(ppp + q * q);
s = sqrt (-D);
r = cbrt (-q + s);
s = cbrt (-q - s);
a_over_3 = a/3;
if (D < 0)
{
roots->x = (r + s) - a_over_3;
return 1;
}
else if (sweet_math_approx_equals (D, 0, EPSILON))
{
roots->x = r * 2 - a_over_3;
roots->y = -r - a_over_3;
roots->z = roots->y;
return 2;
}
else
{
float theta;
float m2;
m2 = 2 * sqrt (-p);
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;
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_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_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);
}
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));
}

226
sweet_math.h Normal file
View File

@ -0,0 +1,226 @@
/*
* Sweet is a small library for basic math and small matrix operation.
* 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_H
#define SWEET_MATH_H
#include "sweet_types.h"
/*
* Functions with name finished by 2, 3 or 4 use vec2, vec3, vec4 respectively.
*
* Functions with name finished by 2h 3h use vec3, vec4 repectively but
* treat last component as homogeneous coordinates.
*
**/
/** This macro define the PI number 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
/** RadianToDegree */
/** @param Radian as float or double */
/** @return Degree as float */
#define sweet_math_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) \
degree * 0.01745329251994329576923690768488
#define sweet_math_approx_equals(a, b, epsilon) \
(a <= (b+epsilon) && a >= (b-epsilon))
/** 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 sweet_math_nearest (int number, int nb_args, ...);
/** Quadratic_polynomial */
/** @param r 2D Vector for the reals roots as vec2 pointer */
/** @param a coefficiant of x^2 as float */
/** @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);
/** Cubic_polynomial */
/** @param r 3D Vector containing the reals roots as vec3 pointer */
/** @param a coefficiant of x^3 as float */
/** @param b coefficiant of x^2 as float */
/** @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);
/* 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 x v2 . 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 x v2 . 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);
/** 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 */

1218
sweet_matrix.c Normal file

File diff suppressed because it is too large Load Diff

169
sweet_matrix.h Normal file
View File

@ -0,0 +1,169 @@
/*
* Sweet is a small library for basic math and small matrix operation.
* 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_H
#define SWEET_MATRIX_H
#include "sweet_types.h"
/* Matrix */
/** Null Matrix */
/** @return Null matrix */
mat2 sweet_matrix_null2 ();
mat3 sweet_matrix_null3 ();
mat4 sweet_matrix_null4 ();
/** Identity */
/** @return Indentity matrix */
mat2 sweet_matrix_identity2 ();
mat3 sweet_matrix_identity3 ();
mat4 sweet_matrix_identity4 ();
/** Frustum */
/** @param Right as flaot */
/** @param Left as float */
/** @param Top as float */
/** @param Bottom as float */
/** @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);
/** Perspective */
/** @param fovy of the camera in degree as flaot */
/** @param aspect of the view as float */
/** @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);
/** Ortho */
/** @param Right as flaot */
/** @param Left as float */
/** @param Top as float */
/** @param Bottom as float */
/** @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);
/** @return camera matrix */
mat4 sweet_matrix_look_at (vec3 pos, vec3 dir, vec3 up);
/** 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);
/** 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);
/** Translation */
/** @param X,Y,Z or vector */
/** @return Translation matrix */
mat4 sweet_matrix_translation3h (vec3 v);
mat3 sweet_matrix_translation2h (vec2 v);
/** 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);
/** Texture bias */
/** @return texture bias matrix */
mat4 sweet_matrix_texture_bias ();
/** 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);
/** 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);
/** 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);
/** 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);
/** 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);
/** 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);
/** 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);
/** Eigen Value */
int sweet_matrix_eigen_value3 (float * a, float * b, float * c, mat3 * m);
/** Gaussian elimination */
int sweet_matrix_gaussian_elimination3 (mat3 * m, vec3 * v);
/** Solve linear system */
int sweet_matrix_solve3 (mat3 * a, vec3 * v);
/** Eigen Vector */
int sweet_matrix_eigen_vector3 (vec3 * vectors, mat3 * m);
#endif

62
sweet_types.h Normal file
View File

@ -0,0 +1,62 @@
/*
* Sweet is a small library for basic math and small matrix operation.
* 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_TYPES_H
#define SWEET_TYPES_H
typedef struct mat4 {
float v[16];
} mat4;
typedef struct mat3 {
float v[9];
} mat3;
typedef struct mat2 {
float v[4];
} mat2;
typedef struct vec4 {
float x;
float y;
float z;
float w;
} vec4;
typedef struct vec3 {
float x;
float y;
float z;
} vec3;
typedef struct vec2 {
float x;
float y;
} vec2;
typedef struct complex {
float r;
float i;
} complex;
typedef vec4 quaternion;
#endif