nuclear@28: #ifndef VMATH_MATRIX_H_ nuclear@28: #define VMATH_MATRIX_H_ nuclear@28: nuclear@28: #include nuclear@28: #include "vmath_types.h" nuclear@28: nuclear@28: #ifdef __cplusplus nuclear@28: extern "C" { nuclear@28: #endif /* __cplusplus */ nuclear@28: nuclear@28: /* C matrix 3x3 functions */ nuclear@28: static inline void m3_identity(mat3_t m); nuclear@28: static inline void m3_cons(mat3_t m, nuclear@28: scalar_t m11, scalar_t m12, scalar_t m13, nuclear@28: scalar_t m21, scalar_t m22, scalar_t m23, nuclear@28: scalar_t m31, scalar_t m32, scalar_t m33); nuclear@28: static inline void m3_copy(mat3_t dest, mat3_t src); nuclear@28: void m3_to_m4(mat4_t dest, mat3_t src); nuclear@28: nuclear@28: void m3_print(FILE *fp, mat3_t m); nuclear@28: nuclear@28: /* C matrix 4x4 functions */ nuclear@28: static inline void m4_identity(mat4_t m); nuclear@28: static inline void m4_cons(mat4_t m, nuclear@28: scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14, nuclear@28: scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24, nuclear@28: scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34, nuclear@28: scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44); nuclear@28: static inline void m4_copy(mat4_t dest, mat4_t src); nuclear@28: void m4_to_m3(mat3_t dest, mat4_t src); nuclear@28: nuclear@28: static inline void m4_mult(mat4_t res, mat4_t m1, mat4_t m2); nuclear@28: nuclear@28: void m4_translate(mat4_t m, scalar_t x, scalar_t y, scalar_t z); nuclear@28: void m4_rotate(mat4_t m, scalar_t x, scalar_t y, scalar_t z); nuclear@28: void m4_rotate_x(mat4_t m, scalar_t angle); nuclear@28: void m4_rotate_y(mat4_t m, scalar_t angle); nuclear@28: void m4_rotate_z(mat4_t m, scalar_t angle); nuclear@28: void m4_rotate_axis(mat4_t m, scalar_t angle, scalar_t x, scalar_t y, scalar_t z); nuclear@28: void m4_rotate_quat(mat4_t m, quat_t q); nuclear@28: void m4_scale(mat4_t m, scalar_t x, scalar_t y, scalar_t z); nuclear@28: static inline void m4_set_column(mat4_t m, vec4_t v, int idx); nuclear@28: static inline void m4_set_row(mat4_t m, vec4_t v, int idx); nuclear@28: nuclear@28: void m4_transpose(mat4_t res, mat4_t m); nuclear@28: scalar_t m4_determinant(mat4_t m); nuclear@28: void m4_adjoint(mat4_t res, mat4_t m); nuclear@28: void m4_inverse(mat4_t res, mat4_t m); nuclear@28: nuclear@28: void m4_print(FILE *fp, mat4_t m); nuclear@28: nuclear@28: #ifdef __cplusplus nuclear@28: } nuclear@28: nuclear@28: /* when included from C++ source files, also define the matrix classes */ nuclear@28: #include nuclear@28: nuclear@28: /** 3x3 matrix */ nuclear@28: class Matrix3x3 { nuclear@28: private: nuclear@28: scalar_t m[3][3]; nuclear@28: nuclear@28: public: nuclear@28: nuclear@28: static Matrix3x3 identity; nuclear@28: nuclear@28: Matrix3x3(); nuclear@28: Matrix3x3( scalar_t m11, scalar_t m12, scalar_t m13, nuclear@28: scalar_t m21, scalar_t m22, scalar_t m23, nuclear@28: scalar_t m31, scalar_t m32, scalar_t m33); nuclear@28: Matrix3x3(const mat3_t cmat); nuclear@28: nuclear@28: Matrix3x3(const Matrix4x4 &mat4x4); nuclear@28: nuclear@28: /* binary operations matrix (op) matrix */ nuclear@28: friend Matrix3x3 operator +(const Matrix3x3 &m1, const Matrix3x3 &m2); nuclear@28: friend Matrix3x3 operator -(const Matrix3x3 &m1, const Matrix3x3 &m2); nuclear@28: friend Matrix3x3 operator *(const Matrix3x3 &m1, const Matrix3x3 &m2); nuclear@28: nuclear@28: friend void operator +=(Matrix3x3 &m1, const Matrix3x3 &m2); nuclear@28: friend void operator -=(Matrix3x3 &m1, const Matrix3x3 &m2); nuclear@28: friend void operator *=(Matrix3x3 &m1, const Matrix3x3 &m2); nuclear@28: nuclear@28: /* binary operations matrix (op) scalar and scalar (op) matrix */ nuclear@28: friend Matrix3x3 operator *(const Matrix3x3 &mat, scalar_t scalar); nuclear@28: friend Matrix3x3 operator *(scalar_t scalar, const Matrix3x3 &mat); nuclear@28: nuclear@28: friend void operator *=(Matrix3x3 &mat, scalar_t scalar); nuclear@28: nuclear@28: inline scalar_t *operator [](int index); nuclear@28: inline const scalar_t *operator [](int index) const; nuclear@28: nuclear@28: inline void reset_identity(); nuclear@28: nuclear@28: void translate(const Vector2 &trans); nuclear@28: void set_translation(const Vector2 &trans); nuclear@28: nuclear@28: void rotate(scalar_t angle); /* 2d rotation */ nuclear@28: void rotate(const Vector3 &euler_angles); /* 3d rotation with euler angles */ nuclear@28: void rotate(const Vector3 &axis, scalar_t angle); /* 3d axis/angle rotation */ nuclear@28: void set_rotation(scalar_t angle); nuclear@28: void set_rotation(const Vector3 &euler_angles); nuclear@28: void set_rotation(const Vector3 &axis, scalar_t angle); nuclear@28: nuclear@28: void scale(const Vector3 &scale_vec); nuclear@28: void set_scaling(const Vector3 &scale_vec); nuclear@28: nuclear@28: void set_column_vector(const Vector3 &vec, unsigned int col_index); nuclear@28: void set_row_vector(const Vector3 &vec, unsigned int row_index); nuclear@28: Vector3 get_column_vector(unsigned int col_index) const; nuclear@28: Vector3 get_row_vector(unsigned int row_index) const; nuclear@28: nuclear@28: void transpose(); nuclear@28: Matrix3x3 transposed() const; nuclear@28: scalar_t determinant() const; nuclear@28: Matrix3x3 inverse() const; nuclear@28: nuclear@28: friend std::ostream &operator <<(std::ostream &out, const Matrix3x3 &mat); nuclear@28: }; nuclear@28: nuclear@28: /** 4x4 matrix */ nuclear@28: class Matrix4x4 { nuclear@28: private: nuclear@28: scalar_t m[4][4]; nuclear@28: nuclear@28: public: nuclear@28: nuclear@28: static Matrix4x4 identity; nuclear@28: nuclear@28: Matrix4x4(); nuclear@28: Matrix4x4( scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14, nuclear@28: scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24, nuclear@28: scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34, nuclear@28: scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44); nuclear@28: Matrix4x4(const mat4_t cmat); nuclear@28: nuclear@28: Matrix4x4(const Matrix3x3 &mat3x3); nuclear@28: nuclear@28: /* binary operations matrix (op) matrix */ nuclear@28: friend Matrix4x4 operator +(const Matrix4x4 &m1, const Matrix4x4 &m2); nuclear@28: friend Matrix4x4 operator -(const Matrix4x4 &m1, const Matrix4x4 &m2); nuclear@28: friend Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2); nuclear@28: nuclear@28: friend void operator +=(Matrix4x4 &m1, const Matrix4x4 &m2); nuclear@28: friend void operator -=(Matrix4x4 &m1, const Matrix4x4 &m2); nuclear@28: friend inline void operator *=(Matrix4x4 &m1, const Matrix4x4 &m2); nuclear@28: nuclear@28: /* binary operations matrix (op) scalar and scalar (op) matrix */ nuclear@28: friend Matrix4x4 operator *(const Matrix4x4 &mat, scalar_t scalar); nuclear@28: friend Matrix4x4 operator *(scalar_t scalar, const Matrix4x4 &mat); nuclear@28: nuclear@28: friend void operator *=(Matrix4x4 &mat, scalar_t scalar); nuclear@28: nuclear@28: inline scalar_t *operator [](int index); nuclear@28: inline const scalar_t *operator [](int index) const; nuclear@28: nuclear@28: inline void reset_identity(); nuclear@28: nuclear@28: void translate(const Vector3 &trans); nuclear@28: void set_translation(const Vector3 &trans); nuclear@28: nuclear@28: void rotate(const Vector3 &euler_angles); /* 3d rotation with euler angles */ nuclear@28: void rotate(const Vector3 &axis, scalar_t angle); /* 3d axis/angle rotation */ nuclear@28: void set_rotation(const Vector3 &euler_angles); nuclear@28: void set_rotation(const Vector3 &axis, scalar_t angle); nuclear@28: nuclear@28: void scale(const Vector4 &scale_vec); nuclear@28: void set_scaling(const Vector4 &scale_vec); nuclear@28: nuclear@28: void set_column_vector(const Vector4 &vec, unsigned int col_index); nuclear@28: void set_row_vector(const Vector4 &vec, unsigned int row_index); nuclear@28: Vector4 get_column_vector(unsigned int col_index) const; nuclear@28: Vector4 get_row_vector(unsigned int row_index) const; nuclear@28: nuclear@28: void transpose(); nuclear@28: Matrix4x4 transposed() const; nuclear@28: scalar_t determinant() const; nuclear@28: Matrix4x4 adjoint() const; nuclear@28: Matrix4x4 inverse() const; nuclear@28: nuclear@28: friend std::ostream &operator <<(std::ostream &out, const Matrix4x4 &mat); nuclear@28: }; nuclear@28: #endif /* __cplusplus */ nuclear@28: nuclear@28: #include "matrix.inl" nuclear@28: nuclear@28: #endif /* VMATH_MATRIX_H_ */