236 lines
7.9 KiB
C
236 lines
7.9 KiB
C
/*
|
|
* 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 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
|
|
|
|
/** 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 . (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 */
|
|
|