istereo

view libs/vmath/matrix.h @ 33:33e9eedad390

ah.. blunder
author John Tsiombikas <nuclear@mutantstargoat.com>
date Fri, 09 Sep 2011 00:03:37 +0300
parents
children ff055bff6a15
line source
1 #ifndef VMATH_MATRIX_H_
2 #define VMATH_MATRIX_H_
4 #include <stdio.h>
5 #include "vmath_types.h"
7 #ifdef __cplusplus
8 extern "C" {
9 #endif /* __cplusplus */
11 /* C matrix 3x3 functions */
12 static inline void m3_identity(mat3_t m);
13 static inline void m3_cons(mat3_t m,
14 scalar_t m11, scalar_t m12, scalar_t m13,
15 scalar_t m21, scalar_t m22, scalar_t m23,
16 scalar_t m31, scalar_t m32, scalar_t m33);
17 static inline void m3_copy(mat3_t dest, mat3_t src);
18 void m3_to_m4(mat4_t dest, mat3_t src);
20 void m3_print(FILE *fp, mat3_t m);
22 /* C matrix 4x4 functions */
23 static inline void m4_identity(mat4_t m);
24 static inline void m4_cons(mat4_t m,
25 scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14,
26 scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24,
27 scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34,
28 scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44);
29 static inline void m4_copy(mat4_t dest, mat4_t src);
30 void m4_to_m3(mat3_t dest, mat4_t src);
32 static inline void m4_mult(mat4_t res, mat4_t m1, mat4_t m2);
34 void m4_translate(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
35 void m4_rotate(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
36 void m4_rotate_x(mat4_t m, scalar_t angle);
37 void m4_rotate_y(mat4_t m, scalar_t angle);
38 void m4_rotate_z(mat4_t m, scalar_t angle);
39 void m4_rotate_axis(mat4_t m, scalar_t angle, scalar_t x, scalar_t y, scalar_t z);
40 void m4_rotate_quat(mat4_t m, quat_t q);
41 void m4_scale(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
42 static inline void m4_set_column(mat4_t m, vec4_t v, int idx);
43 static inline void m4_set_row(mat4_t m, vec4_t v, int idx);
45 void m4_transpose(mat4_t res, mat4_t m);
46 scalar_t m4_determinant(mat4_t m);
47 void m4_adjoint(mat4_t res, mat4_t m);
48 void m4_inverse(mat4_t res, mat4_t m);
50 void m4_print(FILE *fp, mat4_t m);
52 #ifdef __cplusplus
53 }
55 /* when included from C++ source files, also define the matrix classes */
56 #include <iostream>
58 /** 3x3 matrix */
59 class Matrix3x3 {
60 private:
61 scalar_t m[3][3];
63 public:
65 static Matrix3x3 identity;
67 Matrix3x3();
68 Matrix3x3( scalar_t m11, scalar_t m12, scalar_t m13,
69 scalar_t m21, scalar_t m22, scalar_t m23,
70 scalar_t m31, scalar_t m32, scalar_t m33);
71 Matrix3x3(const mat3_t cmat);
73 Matrix3x3(const Matrix4x4 &mat4x4);
75 /* binary operations matrix (op) matrix */
76 friend Matrix3x3 operator +(const Matrix3x3 &m1, const Matrix3x3 &m2);
77 friend Matrix3x3 operator -(const Matrix3x3 &m1, const Matrix3x3 &m2);
78 friend Matrix3x3 operator *(const Matrix3x3 &m1, const Matrix3x3 &m2);
80 friend void operator +=(Matrix3x3 &m1, const Matrix3x3 &m2);
81 friend void operator -=(Matrix3x3 &m1, const Matrix3x3 &m2);
82 friend void operator *=(Matrix3x3 &m1, const Matrix3x3 &m2);
84 /* binary operations matrix (op) scalar and scalar (op) matrix */
85 friend Matrix3x3 operator *(const Matrix3x3 &mat, scalar_t scalar);
86 friend Matrix3x3 operator *(scalar_t scalar, const Matrix3x3 &mat);
88 friend void operator *=(Matrix3x3 &mat, scalar_t scalar);
90 inline scalar_t *operator [](int index);
91 inline const scalar_t *operator [](int index) const;
93 inline void reset_identity();
95 void translate(const Vector2 &trans);
96 void set_translation(const Vector2 &trans);
98 void rotate(scalar_t angle); /* 2d rotation */
99 void rotate(const Vector3 &euler_angles); /* 3d rotation with euler angles */
100 void rotate(const Vector3 &axis, scalar_t angle); /* 3d axis/angle rotation */
101 void set_rotation(scalar_t angle);
102 void set_rotation(const Vector3 &euler_angles);
103 void set_rotation(const Vector3 &axis, scalar_t angle);
105 void scale(const Vector3 &scale_vec);
106 void set_scaling(const Vector3 &scale_vec);
108 void set_column_vector(const Vector3 &vec, unsigned int col_index);
109 void set_row_vector(const Vector3 &vec, unsigned int row_index);
110 Vector3 get_column_vector(unsigned int col_index) const;
111 Vector3 get_row_vector(unsigned int row_index) const;
113 void transpose();
114 Matrix3x3 transposed() const;
115 scalar_t determinant() const;
116 Matrix3x3 inverse() const;
118 friend std::ostream &operator <<(std::ostream &out, const Matrix3x3 &mat);
119 };
121 /** 4x4 matrix */
122 class Matrix4x4 {
123 private:
124 scalar_t m[4][4];
126 public:
128 static Matrix4x4 identity;
130 Matrix4x4();
131 Matrix4x4( scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14,
132 scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24,
133 scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34,
134 scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44);
135 Matrix4x4(const mat4_t cmat);
137 Matrix4x4(const Matrix3x3 &mat3x3);
139 /* binary operations matrix (op) matrix */
140 friend Matrix4x4 operator +(const Matrix4x4 &m1, const Matrix4x4 &m2);
141 friend Matrix4x4 operator -(const Matrix4x4 &m1, const Matrix4x4 &m2);
142 friend Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2);
144 friend void operator +=(Matrix4x4 &m1, const Matrix4x4 &m2);
145 friend void operator -=(Matrix4x4 &m1, const Matrix4x4 &m2);
146 friend inline void operator *=(Matrix4x4 &m1, const Matrix4x4 &m2);
148 /* binary operations matrix (op) scalar and scalar (op) matrix */
149 friend Matrix4x4 operator *(const Matrix4x4 &mat, scalar_t scalar);
150 friend Matrix4x4 operator *(scalar_t scalar, const Matrix4x4 &mat);
152 friend void operator *=(Matrix4x4 &mat, scalar_t scalar);
154 inline scalar_t *operator [](int index);
155 inline const scalar_t *operator [](int index) const;
157 inline void reset_identity();
159 void translate(const Vector3 &trans);
160 void set_translation(const Vector3 &trans);
162 void rotate(const Vector3 &euler_angles); /* 3d rotation with euler angles */
163 void rotate(const Vector3 &axis, scalar_t angle); /* 3d axis/angle rotation */
164 void set_rotation(const Vector3 &euler_angles);
165 void set_rotation(const Vector3 &axis, scalar_t angle);
167 void scale(const Vector4 &scale_vec);
168 void set_scaling(const Vector4 &scale_vec);
170 void set_column_vector(const Vector4 &vec, unsigned int col_index);
171 void set_row_vector(const Vector4 &vec, unsigned int row_index);
172 Vector4 get_column_vector(unsigned int col_index) const;
173 Vector4 get_row_vector(unsigned int row_index) const;
175 void transpose();
176 Matrix4x4 transposed() const;
177 scalar_t determinant() const;
178 Matrix4x4 adjoint() const;
179 Matrix4x4 inverse() const;
181 friend std::ostream &operator <<(std::ostream &out, const Matrix4x4 &mat);
182 };
183 #endif /* __cplusplus */
185 #include "matrix.inl"
187 #endif /* VMATH_MATRIX_H_ */