sweet/sweet_matrix.h
2020-12-29 00:52:24 +01:00

238 lines
7.7 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_MATRIX_H
#define SWEET_MATRIX_H
#include "sweet_types.h"
/* 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);
/** Identity */
/** @return Indentity matrix */
mat2 mat2_identity (void);
mat3 mat3_identity (void);
mat4 mat4_identity (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);
/** 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 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 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);
/** 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 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);
/** Rotation from quaterion */
/** @param quaternion representing rotation */
/** @return 3d Rotation matrix */
mat3 mat3_quat_rotation (quat q);
mat4 mat3h_quat_rotation (quat 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);
/** 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
/** 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);
/** Texture bias */
/** @return texture bias matrix */
mat4 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);
/** 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);
/** 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);
/** 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);
/** 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);
/** 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);
/** 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);
/** Eigen Value */
int mat3_eigen_values (float * a, float * b, float * c, mat3 * m);
/** Gaussian elimination */
int mat3_gaussian_elimination (mat3 * m, vec3 * v);
/** Solve linear system */
int mat3_solve (mat3 * a, vec3 * v);
/** Eigen Vector */
void mat3_eigen_vectors (vec3 * eigen_vectors, float * eigen_values, mat3 * A, int it);
#endif