sweet/sweet_matrix.h

169 lines
5.6 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_MATRIX_H
#define SWEET_MATRIX_H
#include "sweet_types.h"
/* Matrix */
/** Null Matrix */
/** @return Null matrix */
mat2 sweet_matrix_null2 (void);
mat3 sweet_matrix_null3 (void);
mat4 sweet_matrix_null4 (void);
/** Identity */
/** @return Indentity matrix */
mat2 sweet_matrix_identity2 (void);
mat3 sweet_matrix_identity3 (void);
mat4 sweet_matrix_identity4 (void);
/** 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 (void);
/** Transpose */
/** @param Matrix to transpose as mat2 *, mat3 * or mat4 * */
void sweet_matrix_transpose2 (mat2 * transpose, mat2 * matrix);
void sweet_matrix_transpose3 (mat3 * transpose, mat3 * matrix);
void sweet_matrix_transpose4 (mat4 * transpose, mat4 * matrix);
/** 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