istereo
diff libs/vmath/matrix.h @ 28:c0ae8e668447
added vmath library
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Thu, 08 Sep 2011 08:30:42 +0300 |
parents | |
children | ff055bff6a15 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/vmath/matrix.h Thu Sep 08 08:30:42 2011 +0300 1.3 @@ -0,0 +1,187 @@ 1.4 +#ifndef VMATH_MATRIX_H_ 1.5 +#define VMATH_MATRIX_H_ 1.6 + 1.7 +#include <stdio.h> 1.8 +#include "vmath_types.h" 1.9 + 1.10 +#ifdef __cplusplus 1.11 +extern "C" { 1.12 +#endif /* __cplusplus */ 1.13 + 1.14 +/* C matrix 3x3 functions */ 1.15 +static inline void m3_identity(mat3_t m); 1.16 +static inline void m3_cons(mat3_t m, 1.17 + scalar_t m11, scalar_t m12, scalar_t m13, 1.18 + scalar_t m21, scalar_t m22, scalar_t m23, 1.19 + scalar_t m31, scalar_t m32, scalar_t m33); 1.20 +static inline void m3_copy(mat3_t dest, mat3_t src); 1.21 +void m3_to_m4(mat4_t dest, mat3_t src); 1.22 + 1.23 +void m3_print(FILE *fp, mat3_t m); 1.24 + 1.25 +/* C matrix 4x4 functions */ 1.26 +static inline void m4_identity(mat4_t m); 1.27 +static inline void m4_cons(mat4_t m, 1.28 + scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14, 1.29 + scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24, 1.30 + scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34, 1.31 + scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44); 1.32 +static inline void m4_copy(mat4_t dest, mat4_t src); 1.33 +void m4_to_m3(mat3_t dest, mat4_t src); 1.34 + 1.35 +static inline void m4_mult(mat4_t res, mat4_t m1, mat4_t m2); 1.36 + 1.37 +void m4_translate(mat4_t m, scalar_t x, scalar_t y, scalar_t z); 1.38 +void m4_rotate(mat4_t m, scalar_t x, scalar_t y, scalar_t z); 1.39 +void m4_rotate_x(mat4_t m, scalar_t angle); 1.40 +void m4_rotate_y(mat4_t m, scalar_t angle); 1.41 +void m4_rotate_z(mat4_t m, scalar_t angle); 1.42 +void m4_rotate_axis(mat4_t m, scalar_t angle, scalar_t x, scalar_t y, scalar_t z); 1.43 +void m4_rotate_quat(mat4_t m, quat_t q); 1.44 +void m4_scale(mat4_t m, scalar_t x, scalar_t y, scalar_t z); 1.45 +static inline void m4_set_column(mat4_t m, vec4_t v, int idx); 1.46 +static inline void m4_set_row(mat4_t m, vec4_t v, int idx); 1.47 + 1.48 +void m4_transpose(mat4_t res, mat4_t m); 1.49 +scalar_t m4_determinant(mat4_t m); 1.50 +void m4_adjoint(mat4_t res, mat4_t m); 1.51 +void m4_inverse(mat4_t res, mat4_t m); 1.52 + 1.53 +void m4_print(FILE *fp, mat4_t m); 1.54 + 1.55 +#ifdef __cplusplus 1.56 +} 1.57 + 1.58 +/* when included from C++ source files, also define the matrix classes */ 1.59 +#include <iostream> 1.60 + 1.61 +/** 3x3 matrix */ 1.62 +class Matrix3x3 { 1.63 +private: 1.64 + scalar_t m[3][3]; 1.65 + 1.66 +public: 1.67 + 1.68 + static Matrix3x3 identity; 1.69 + 1.70 + Matrix3x3(); 1.71 + Matrix3x3( scalar_t m11, scalar_t m12, scalar_t m13, 1.72 + scalar_t m21, scalar_t m22, scalar_t m23, 1.73 + scalar_t m31, scalar_t m32, scalar_t m33); 1.74 + Matrix3x3(const mat3_t cmat); 1.75 + 1.76 + Matrix3x3(const Matrix4x4 &mat4x4); 1.77 + 1.78 + /* binary operations matrix (op) matrix */ 1.79 + friend Matrix3x3 operator +(const Matrix3x3 &m1, const Matrix3x3 &m2); 1.80 + friend Matrix3x3 operator -(const Matrix3x3 &m1, const Matrix3x3 &m2); 1.81 + friend Matrix3x3 operator *(const Matrix3x3 &m1, const Matrix3x3 &m2); 1.82 + 1.83 + friend void operator +=(Matrix3x3 &m1, const Matrix3x3 &m2); 1.84 + friend void operator -=(Matrix3x3 &m1, const Matrix3x3 &m2); 1.85 + friend void operator *=(Matrix3x3 &m1, const Matrix3x3 &m2); 1.86 + 1.87 + /* binary operations matrix (op) scalar and scalar (op) matrix */ 1.88 + friend Matrix3x3 operator *(const Matrix3x3 &mat, scalar_t scalar); 1.89 + friend Matrix3x3 operator *(scalar_t scalar, const Matrix3x3 &mat); 1.90 + 1.91 + friend void operator *=(Matrix3x3 &mat, scalar_t scalar); 1.92 + 1.93 + inline scalar_t *operator [](int index); 1.94 + inline const scalar_t *operator [](int index) const; 1.95 + 1.96 + inline void reset_identity(); 1.97 + 1.98 + void translate(const Vector2 &trans); 1.99 + void set_translation(const Vector2 &trans); 1.100 + 1.101 + void rotate(scalar_t angle); /* 2d rotation */ 1.102 + void rotate(const Vector3 &euler_angles); /* 3d rotation with euler angles */ 1.103 + void rotate(const Vector3 &axis, scalar_t angle); /* 3d axis/angle rotation */ 1.104 + void set_rotation(scalar_t angle); 1.105 + void set_rotation(const Vector3 &euler_angles); 1.106 + void set_rotation(const Vector3 &axis, scalar_t angle); 1.107 + 1.108 + void scale(const Vector3 &scale_vec); 1.109 + void set_scaling(const Vector3 &scale_vec); 1.110 + 1.111 + void set_column_vector(const Vector3 &vec, unsigned int col_index); 1.112 + void set_row_vector(const Vector3 &vec, unsigned int row_index); 1.113 + Vector3 get_column_vector(unsigned int col_index) const; 1.114 + Vector3 get_row_vector(unsigned int row_index) const; 1.115 + 1.116 + void transpose(); 1.117 + Matrix3x3 transposed() const; 1.118 + scalar_t determinant() const; 1.119 + Matrix3x3 inverse() const; 1.120 + 1.121 + friend std::ostream &operator <<(std::ostream &out, const Matrix3x3 &mat); 1.122 +}; 1.123 + 1.124 +/** 4x4 matrix */ 1.125 +class Matrix4x4 { 1.126 +private: 1.127 + scalar_t m[4][4]; 1.128 + 1.129 +public: 1.130 + 1.131 + static Matrix4x4 identity; 1.132 + 1.133 + Matrix4x4(); 1.134 + Matrix4x4( scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14, 1.135 + scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24, 1.136 + scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34, 1.137 + scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44); 1.138 + Matrix4x4(const mat4_t cmat); 1.139 + 1.140 + Matrix4x4(const Matrix3x3 &mat3x3); 1.141 + 1.142 + /* binary operations matrix (op) matrix */ 1.143 + friend Matrix4x4 operator +(const Matrix4x4 &m1, const Matrix4x4 &m2); 1.144 + friend Matrix4x4 operator -(const Matrix4x4 &m1, const Matrix4x4 &m2); 1.145 + friend Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2); 1.146 + 1.147 + friend void operator +=(Matrix4x4 &m1, const Matrix4x4 &m2); 1.148 + friend void operator -=(Matrix4x4 &m1, const Matrix4x4 &m2); 1.149 + friend inline void operator *=(Matrix4x4 &m1, const Matrix4x4 &m2); 1.150 + 1.151 + /* binary operations matrix (op) scalar and scalar (op) matrix */ 1.152 + friend Matrix4x4 operator *(const Matrix4x4 &mat, scalar_t scalar); 1.153 + friend Matrix4x4 operator *(scalar_t scalar, const Matrix4x4 &mat); 1.154 + 1.155 + friend void operator *=(Matrix4x4 &mat, scalar_t scalar); 1.156 + 1.157 + inline scalar_t *operator [](int index); 1.158 + inline const scalar_t *operator [](int index) const; 1.159 + 1.160 + inline void reset_identity(); 1.161 + 1.162 + void translate(const Vector3 &trans); 1.163 + void set_translation(const Vector3 &trans); 1.164 + 1.165 + void rotate(const Vector3 &euler_angles); /* 3d rotation with euler angles */ 1.166 + void rotate(const Vector3 &axis, scalar_t angle); /* 3d axis/angle rotation */ 1.167 + void set_rotation(const Vector3 &euler_angles); 1.168 + void set_rotation(const Vector3 &axis, scalar_t angle); 1.169 + 1.170 + void scale(const Vector4 &scale_vec); 1.171 + void set_scaling(const Vector4 &scale_vec); 1.172 + 1.173 + void set_column_vector(const Vector4 &vec, unsigned int col_index); 1.174 + void set_row_vector(const Vector4 &vec, unsigned int row_index); 1.175 + Vector4 get_column_vector(unsigned int col_index) const; 1.176 + Vector4 get_row_vector(unsigned int row_index) const; 1.177 + 1.178 + void transpose(); 1.179 + Matrix4x4 transposed() const; 1.180 + scalar_t determinant() const; 1.181 + Matrix4x4 adjoint() const; 1.182 + Matrix4x4 inverse() const; 1.183 + 1.184 + friend std::ostream &operator <<(std::ostream &out, const Matrix4x4 &mat); 1.185 +}; 1.186 +#endif /* __cplusplus */ 1.187 + 1.188 +#include "matrix.inl" 1.189 + 1.190 +#endif /* VMATH_MATRIX_H_ */