sweet/sweet_math.h
2019-04-11 03:17:26 +02:00

235 lines
7.1 KiB
C
Executable File

/*
* 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_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 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
/** RadianToDegree */
/** @param Radian as float or double */
/** @return Degree as float */
#define radian_2_degree(radian) \
radian * 57.2957795130823208767981548
/** DegreeToRadian */
/** @param Degree as float or double */
/** @return Radian as float */
#define degree_2_radian(degree) \
degree * 0.01745329251994329576923690768488
#define float_approx_equals(a, b, epsilon) \
(a <= (b+epsilon) && a >= (b-epsilon))
#define float_approx_zero(a, epsilon) \
(a <= (epsilon) && a >= (epsilon))
#define max(a, b) (a >= b ? a : b)
#define min(a, b) (a <= b ? a : b)
/* Vector */
vec2 vec2_zero();
vec3 vec3_zero();
vec4 vec4_zero();
/** Vector constructor */
/** @return Vector */
vec2 vec2_new_2f (float x, float y);
vec2 vec2_new_2f(float x, float y);
vec2 vec2_new_v3(vec3 w);
vec3 vec3_new_3f (float x, float y, float z);
vec3 vec3_new_v2_1f(vec2 w, float z);
vec3 vec3_new_v4(vec4 w);
vec4 vec4_new_4f (float x, float y, float z, float w);
vec4 vec4_new_v2_2f(vec2 u, float z, float w);
vec4 vec4_new_v3_1f(vec3 u, float w);
vec4 vec4_new_v4 (vec4 v);
/** Norm */
/** @param v as vec2, vec3 or vec4*/
/** @return vector norm or squared norm */
float vec2_norm (vec2 v);
float vec3_norm (vec3 v);
float vec4_norm (vec4 v);
float vec2_square_norm (vec2 v);
float vec3_square_norm (vec3 v);
float vec4_square_norm (vec4 v);
/** Dist */
/** @param a as vec2, vec3 or vec4 */
/** @param b as vec2, vec3 or vec4 */
/** @return distance or squared distance between a and b */
float vec2_dist (vec2 a, vec2 b);
float vec3_dist (vec3 a, vec3 b);
float vec4_dist (vec4 a, vec4 b);
float vec2_square_dist (vec2 a, vec2 b);
float vec3_square_dist (vec3 a, vec3 b);
float vec4_square_dist (vec4 a, vec4 b);
/** Dot */
/** @param v1 as vector */
/** @param v2 as vector */
/** @return Dot product as float */
float vec2_dot (vec2 vector1, vec2 vector2);
float vec3_dot (vec3 vector1, vec3 vector2);
float vec4_dot (vec4 vector1, vec4 vector2);
/** Cross */
/** @param vector1 as vec3 */
/** @param vector1 as vec3 */
/** @return Cross product as vec3 */
vec3 cross (vec3 vector1, vec3 vector2);
/** TripleProduct */
/** @param vector1 as vec3 */
/** @param vector2 as vec3 */
/** @param vector3 as vec3 */
/** @return v1 . (v2 x v3) as float */
float triple_product (vec3 vector1, vec3 vector2, vec3 vector3);
/** Normalize */
/** @param vector as vec2, vec3 or vec4 */
/** @return Normalized vector */
vec2 vec2_normalize (vec2 vector);
vec3 vec3_normalize (vec3 vector);
vec4 vec4_normalize (vec4 vector);
/** Scale */
/** @param Vector as vector */
/** @param Scalar as float */
/** @return Vector * scalar */
vec2 vec2_scale (vec2 vector, float scalar);
vec3 vec3_scale (vec3 vector, float scalar);
vec4 vec4_scale (vec4 vector, float scalar);
/** Rescale */
/** @param Vector as vector */
/** @param Size as float */
/** @return Vector * size / lenght */
vec2 vec2_rescale (vec2 vector, float size);
vec3 vec3_rescale (vec3 vector, float size);
vec4 vec4_rescale (vec4 vector, float size);
/** Inverse */
/** @param Vector as vector*/
/** @return - vector */
vec2 vec2_inverse (vec2 vector);
vec3 vec3_inverse (vec3 vector);
vec4 vec4_inverse (vec4 vector);
/** Add */
/** @param Vector1 as vector */
/** @param Vector2 as vector */
/** @return vector1 + vector2 */
vec2 vec2_add (vec2 vector1, vec2 vector2);
vec3 vec3_add (vec3 vector1, vec3 vector2);
vec4 vec4_add (vec4 vector1, vec4 vector2);
/** Middle */
/** @param Vector1 as vector */
/** @param Vector2 as vector */
/** @return middle point between vector1 and vector2 */
vec2 vec2_middle (vec2 vector1, vec2 vector2);
vec3 vec3_middle (vec3 vector1, vec3 vector2);
vec4 vec4_middle (vec4 vector1, vec4 vector2);
/** Sub */
/** @param Vector1 as vector */
/** @param Vector2 as vector */
/** @return vector1 - vector2 */
vec2 vec2_sub (vec2 vector1, vec2 vector2);
vec3 vec3_sub (vec3 vector1, vec3 vector2);
vec4 vec4_sub (vec4 vector1, vec4 vector2);
/** Angle */
/** @param Vector1 as vector */
/** @param Vector2 as vector */
/** @return Angle between the two vector in radian */
float vec2_angle (vec2 vector1, vec2 vector2);
float vec3_angle (vec3 vector1, vec3 vector2);
/** Random */
vec3 vec3_random ();
vec3 vec3_random_ortho (vec3 v);
/** Mult */
/** @param Vector1 as vector */
/** @param Vector2 as vector */
/** @return Product of vector1 and vector2 */
vec2 vec2_product (vec2 vector1, vec2 vector2);
vec3 vec3_product (vec3 vector1, vec3 vector2);
vec4 vec4_product (vec4 vector1, vec4 vector2);
/* Quaternion */
quaternion quat_new (float w, float x, float y, float z);
quaternion quat_rotation (float angle, float x, float y, float z);
quaternion quat_conjugate (quaternion q);
quaternion quat_add (quaternion q1, quaternion q2);
quaternion quat_product (quaternion q1, quaternion q2);
float quat_norm (quaternion q);
/** Nearest */
/** @param number integer to compare as int */
/** @param nb_args number of arguments as int */
/** @param ... integer arguments */
/** @return nearest argument to number */
int nearest_integer (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 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 cubic_polynomial (vec3 * r, float a, float b, float c, float d);
#endif /*MATH_H */