istereo2
diff libs/vmath/matrix.h @ 2:81d35769f546
added the tunnel effect source
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 19 Sep 2015 05:51:51 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/vmath/matrix.h Sat Sep 19 05:51:51 2015 +0300 1.3 @@ -0,0 +1,205 @@ 1.4 +/* 1.5 +libvmath - a vector math library 1.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org> 1.7 + 1.8 +This program is free software: you can redistribute it and/or modify 1.9 +it under the terms of the GNU Lesser General Public License as published 1.10 +by the Free Software Foundation, either version 3 of the License, or 1.11 +(at your option) any later version. 1.12 + 1.13 +This program is distributed in the hope that it will be useful, 1.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of 1.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.16 +GNU Lesser General Public License for more details. 1.17 + 1.18 +You should have received a copy of the GNU Lesser General Public License 1.19 +along with this program. If not, see <http://www.gnu.org/licenses/>. 1.20 +*/ 1.21 + 1.22 +#ifndef VMATH_MATRIX_H_ 1.23 +#define VMATH_MATRIX_H_ 1.24 + 1.25 +#include <stdio.h> 1.26 +#include "vmath_types.h" 1.27 + 1.28 +#ifdef __cplusplus 1.29 +extern "C" { 1.30 +#endif /* __cplusplus */ 1.31 + 1.32 +/* C matrix 3x3 functions */ 1.33 +static inline void m3_identity(mat3_t m); 1.34 +static inline void m3_cons(mat3_t m, 1.35 + scalar_t m11, scalar_t m12, scalar_t m13, 1.36 + scalar_t m21, scalar_t m22, scalar_t m23, 1.37 + scalar_t m31, scalar_t m32, scalar_t m33); 1.38 +static inline void m3_copy(mat3_t dest, mat3_t src); 1.39 +void m3_to_m4(mat4_t dest, mat3_t src); 1.40 + 1.41 +void m3_print(FILE *fp, mat3_t m); 1.42 + 1.43 +/* C matrix 4x4 functions */ 1.44 +static inline void m4_identity(mat4_t m); 1.45 +static inline void m4_cons(mat4_t m, 1.46 + scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14, 1.47 + scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24, 1.48 + scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34, 1.49 + scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44); 1.50 +static inline void m4_copy(mat4_t dest, mat4_t src); 1.51 +void m4_to_m3(mat3_t dest, mat4_t src); 1.52 + 1.53 +static inline void m4_mult(mat4_t res, mat4_t m1, mat4_t m2); 1.54 + 1.55 +void m4_translate(mat4_t m, scalar_t x, scalar_t y, scalar_t z); 1.56 +void m4_rotate(mat4_t m, scalar_t x, scalar_t y, scalar_t z); 1.57 +void m4_rotate_x(mat4_t m, scalar_t angle); 1.58 +void m4_rotate_y(mat4_t m, scalar_t angle); 1.59 +void m4_rotate_z(mat4_t m, scalar_t angle); 1.60 +void m4_rotate_axis(mat4_t m, scalar_t angle, scalar_t x, scalar_t y, scalar_t z); 1.61 +void m4_rotate_quat(mat4_t m, quat_t q); 1.62 +void m4_scale(mat4_t m, scalar_t x, scalar_t y, scalar_t z); 1.63 +static inline void m4_set_column(mat4_t m, vec4_t v, int idx); 1.64 +static inline void m4_set_row(mat4_t m, vec4_t v, int idx); 1.65 + 1.66 +void m4_transpose(mat4_t res, mat4_t m); 1.67 +scalar_t m4_determinant(mat4_t m); 1.68 +void m4_adjoint(mat4_t res, mat4_t m); 1.69 +void m4_inverse(mat4_t res, mat4_t m); 1.70 + 1.71 +void m4_print(FILE *fp, mat4_t m); 1.72 + 1.73 +#ifdef __cplusplus 1.74 +} 1.75 + 1.76 +/* when included from C++ source files, also define the matrix classes */ 1.77 +#include <iostream> 1.78 + 1.79 +/** 3x3 matrix */ 1.80 +class Matrix3x3 { 1.81 +private: 1.82 + scalar_t m[3][3]; 1.83 + 1.84 +public: 1.85 + 1.86 + static Matrix3x3 identity; 1.87 + 1.88 + Matrix3x3(); 1.89 + Matrix3x3( scalar_t m11, scalar_t m12, scalar_t m13, 1.90 + scalar_t m21, scalar_t m22, scalar_t m23, 1.91 + scalar_t m31, scalar_t m32, scalar_t m33); 1.92 + Matrix3x3(const mat3_t cmat); 1.93 + 1.94 + Matrix3x3(const Matrix4x4 &mat4x4); 1.95 + 1.96 + /* binary operations matrix (op) matrix */ 1.97 + friend Matrix3x3 operator +(const Matrix3x3 &m1, const Matrix3x3 &m2); 1.98 + friend Matrix3x3 operator -(const Matrix3x3 &m1, const Matrix3x3 &m2); 1.99 + friend Matrix3x3 operator *(const Matrix3x3 &m1, const Matrix3x3 &m2); 1.100 + 1.101 + friend void operator +=(Matrix3x3 &m1, const Matrix3x3 &m2); 1.102 + friend void operator -=(Matrix3x3 &m1, const Matrix3x3 &m2); 1.103 + friend void operator *=(Matrix3x3 &m1, const Matrix3x3 &m2); 1.104 + 1.105 + /* binary operations matrix (op) scalar and scalar (op) matrix */ 1.106 + friend Matrix3x3 operator *(const Matrix3x3 &mat, scalar_t scalar); 1.107 + friend Matrix3x3 operator *(scalar_t scalar, const Matrix3x3 &mat); 1.108 + 1.109 + friend void operator *=(Matrix3x3 &mat, scalar_t scalar); 1.110 + 1.111 + inline scalar_t *operator [](int index); 1.112 + inline const scalar_t *operator [](int index) const; 1.113 + 1.114 + inline void reset_identity(); 1.115 + 1.116 + void translate(const Vector2 &trans); 1.117 + void set_translation(const Vector2 &trans); 1.118 + 1.119 + void rotate(scalar_t angle); /* 2d rotation */ 1.120 + void rotate(const Vector3 &euler_angles); /* 3d rotation with euler angles */ 1.121 + void rotate(const Vector3 &axis, scalar_t angle); /* 3d axis/angle rotation */ 1.122 + void set_rotation(scalar_t angle); 1.123 + void set_rotation(const Vector3 &euler_angles); 1.124 + void set_rotation(const Vector3 &axis, scalar_t angle); 1.125 + 1.126 + void scale(const Vector3 &scale_vec); 1.127 + void set_scaling(const Vector3 &scale_vec); 1.128 + 1.129 + void set_column_vector(const Vector3 &vec, unsigned int col_index); 1.130 + void set_row_vector(const Vector3 &vec, unsigned int row_index); 1.131 + Vector3 get_column_vector(unsigned int col_index) const; 1.132 + Vector3 get_row_vector(unsigned int row_index) const; 1.133 + 1.134 + void transpose(); 1.135 + Matrix3x3 transposed() const; 1.136 + scalar_t determinant() const; 1.137 + Matrix3x3 inverse() const; 1.138 + 1.139 + friend std::ostream &operator <<(std::ostream &out, const Matrix3x3 &mat); 1.140 +}; 1.141 + 1.142 +/** 4x4 matrix */ 1.143 +class Matrix4x4 { 1.144 +private: 1.145 + scalar_t m[4][4]; 1.146 + 1.147 +public: 1.148 + 1.149 + static Matrix4x4 identity; 1.150 + 1.151 + Matrix4x4(); 1.152 + Matrix4x4( scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14, 1.153 + scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24, 1.154 + scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34, 1.155 + scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44); 1.156 + Matrix4x4(const mat4_t cmat); 1.157 + 1.158 + Matrix4x4(const Matrix3x3 &mat3x3); 1.159 + 1.160 + /* binary operations matrix (op) matrix */ 1.161 + friend Matrix4x4 operator +(const Matrix4x4 &m1, const Matrix4x4 &m2); 1.162 + friend Matrix4x4 operator -(const Matrix4x4 &m1, const Matrix4x4 &m2); 1.163 + friend Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2); 1.164 + 1.165 + friend void operator +=(Matrix4x4 &m1, const Matrix4x4 &m2); 1.166 + friend void operator -=(Matrix4x4 &m1, const Matrix4x4 &m2); 1.167 + friend inline void operator *=(Matrix4x4 &m1, const Matrix4x4 &m2); 1.168 + 1.169 + /* binary operations matrix (op) scalar and scalar (op) matrix */ 1.170 + friend Matrix4x4 operator *(const Matrix4x4 &mat, scalar_t scalar); 1.171 + friend Matrix4x4 operator *(scalar_t scalar, const Matrix4x4 &mat); 1.172 + 1.173 + friend void operator *=(Matrix4x4 &mat, scalar_t scalar); 1.174 + 1.175 + inline scalar_t *operator [](int index); 1.176 + inline const scalar_t *operator [](int index) const; 1.177 + 1.178 + inline void reset_identity(); 1.179 + 1.180 + void translate(const Vector3 &trans); 1.181 + void set_translation(const Vector3 &trans); 1.182 + 1.183 + void rotate(const Vector3 &euler_angles); /* 3d rotation with euler angles */ 1.184 + void rotate(const Vector3 &axis, scalar_t angle); /* 3d axis/angle rotation */ 1.185 + void set_rotation(const Vector3 &euler_angles); 1.186 + void set_rotation(const Vector3 &axis, scalar_t angle); 1.187 + 1.188 + void scale(const Vector4 &scale_vec); 1.189 + void set_scaling(const Vector4 &scale_vec); 1.190 + 1.191 + void set_column_vector(const Vector4 &vec, unsigned int col_index); 1.192 + void set_row_vector(const Vector4 &vec, unsigned int row_index); 1.193 + Vector4 get_column_vector(unsigned int col_index) const; 1.194 + Vector4 get_row_vector(unsigned int row_index) const; 1.195 + 1.196 + void transpose(); 1.197 + Matrix4x4 transposed() const; 1.198 + scalar_t determinant() const; 1.199 + Matrix4x4 adjoint() const; 1.200 + Matrix4x4 inverse() const; 1.201 + 1.202 + friend std::ostream &operator <<(std::ostream &out, const Matrix4x4 &mat); 1.203 +}; 1.204 +#endif /* __cplusplus */ 1.205 + 1.206 +#include "matrix.inl" 1.207 + 1.208 +#endif /* VMATH_MATRIX_H_ */