goat3d

view libs/vmath/matrix.h @ 51:fa5c52ea9d59

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 17 Jan 2014 18:16:09 +0200
parents 4deb0b12fe14
children
line source
1 /*
2 libvmath - a vector math library
3 Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
19 #ifndef VMATH_MATRIX_H_
20 #define VMATH_MATRIX_H_
22 #include <stdio.h>
23 #include "vmath_types.h"
25 #ifdef __cplusplus
26 extern "C" {
27 #endif /* __cplusplus */
29 /* C matrix 3x3 functions */
30 static VMATH_INLINE void m3_identity(mat3_t m);
31 static VMATH_INLINE void m3_cons(mat3_t m,
32 scalar_t m11, scalar_t m12, scalar_t m13,
33 scalar_t m21, scalar_t m22, scalar_t m23,
34 scalar_t m31, scalar_t m32, scalar_t m33);
35 static VMATH_INLINE void m3_copy(mat3_t dest, mat3_t src);
36 void m3_to_m4(mat4_t dest, mat3_t src);
38 void m3_print(FILE *fp, mat3_t m);
40 /* C matrix 4x4 functions */
41 static VMATH_INLINE void m4_identity(mat4_t m);
42 static VMATH_INLINE void m4_cons(mat4_t m,
43 scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14,
44 scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24,
45 scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34,
46 scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44);
47 static VMATH_INLINE void m4_copy(mat4_t dest, mat4_t src);
48 void m4_to_m3(mat3_t dest, mat4_t src);
50 static VMATH_INLINE void m4_mult(mat4_t res, mat4_t m1, mat4_t m2);
52 void m4_set_translation(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
53 void m4_translate(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
55 void m4_rotate(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
57 void m4_set_rotation_x(mat4_t m, scalar_t angle);
58 void m4_rotate_x(mat4_t m, scalar_t angle);
59 void m4_set_rotation_y(mat4_t m, scalar_t angle);
60 void m4_rotate_y(mat4_t m, scalar_t angle);
61 void m4_set_rotation_z(mat4_t m, scalar_t angle);
62 void m4_rotate_z(mat4_t m, scalar_t angle);
63 /* axis-angle rotation */
64 void m4_set_rotation_axis(mat4_t m, scalar_t angle, scalar_t x, scalar_t y, scalar_t z);
65 void m4_rotate_axis(mat4_t m, scalar_t angle, scalar_t x, scalar_t y, scalar_t z);
66 /* concatentate a rotation quaternion */
67 void m4_rotate_quat(mat4_t m, quat_t q);
69 void m4_set_scaling(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
70 void m4_scale(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
72 static VMATH_INLINE void m4_set_column(mat4_t m, vec4_t v, int idx);
73 static VMATH_INLINE void m4_set_row(mat4_t m, vec4_t v, int idx);
75 void m4_transpose(mat4_t res, mat4_t m);
76 scalar_t m4_determinant(mat4_t m);
77 void m4_adjoint(mat4_t res, mat4_t m);
78 void m4_inverse(mat4_t res, mat4_t m);
80 void m4_print(FILE *fp, mat4_t m);
82 #ifdef __cplusplus
83 }
85 /* when included from C++ source files, also define the matrix classes */
86 #include <iostream>
88 /** 3x3 matrix */
89 class Matrix3x3 {
90 public:
91 scalar_t m[3][3];
93 static Matrix3x3 identity;
95 Matrix3x3();
96 Matrix3x3( scalar_t m11, scalar_t m12, scalar_t m13,
97 scalar_t m21, scalar_t m22, scalar_t m23,
98 scalar_t m31, scalar_t m32, scalar_t m33);
99 Matrix3x3(const Vector3 &ivec, const Vector3 &jvec, const Vector3 &kvec);
100 Matrix3x3(const mat3_t cmat);
102 Matrix3x3(const Matrix4x4 &mat4x4);
104 /* binary operations matrix (op) matrix */
105 friend Matrix3x3 operator +(const Matrix3x3 &m1, const Matrix3x3 &m2);
106 friend Matrix3x3 operator -(const Matrix3x3 &m1, const Matrix3x3 &m2);
107 friend Matrix3x3 operator *(const Matrix3x3 &m1, const Matrix3x3 &m2);
109 friend void operator +=(Matrix3x3 &m1, const Matrix3x3 &m2);
110 friend void operator -=(Matrix3x3 &m1, const Matrix3x3 &m2);
111 friend void operator *=(Matrix3x3 &m1, const Matrix3x3 &m2);
113 /* binary operations matrix (op) scalar and scalar (op) matrix */
114 friend Matrix3x3 operator *(const Matrix3x3 &mat, scalar_t scalar);
115 friend Matrix3x3 operator *(scalar_t scalar, const Matrix3x3 &mat);
117 friend void operator *=(Matrix3x3 &mat, scalar_t scalar);
119 VMATH_INLINE scalar_t *operator [](int index);
120 VMATH_INLINE const scalar_t *operator [](int index) const;
122 VMATH_INLINE void reset_identity();
124 void translate(const Vector2 &trans);
125 void set_translation(const Vector2 &trans);
127 void rotate(scalar_t angle); /* 2d rotation */
128 void rotate(const Vector3 &euler_angles); /* 3d rotation with euler angles */
129 void rotate(const Vector3 &axis, scalar_t angle); /* 3d axis/angle rotation */
130 void set_rotation(scalar_t angle);
131 void set_rotation(const Vector3 &euler_angles);
132 void set_rotation(const Vector3 &axis, scalar_t angle);
133 Quaternion get_rotation_quat() const;
135 void scale(const Vector3 &scale_vec);
136 void set_scaling(const Vector3 &scale_vec);
138 void set_column_vector(const Vector3 &vec, unsigned int col_index);
139 void set_row_vector(const Vector3 &vec, unsigned int row_index);
140 Vector3 get_column_vector(unsigned int col_index) const;
141 Vector3 get_row_vector(unsigned int row_index) const;
143 void transpose();
144 Matrix3x3 transposed() const;
145 scalar_t determinant() const;
146 Matrix3x3 inverse() const;
148 friend std::ostream &operator <<(std::ostream &out, const Matrix3x3 &mat);
149 };
151 /* binary operations matrix (op) matrix */
152 Matrix3x3 operator +(const Matrix3x3 &m1, const Matrix3x3 &m2);
153 Matrix3x3 operator -(const Matrix3x3 &m1, const Matrix3x3 &m2);
154 Matrix3x3 operator *(const Matrix3x3 &m1, const Matrix3x3 &m2);
156 void operator +=(Matrix3x3 &m1, const Matrix3x3 &m2);
157 void operator -=(Matrix3x3 &m1, const Matrix3x3 &m2);
158 void operator *=(Matrix3x3 &m1, const Matrix3x3 &m2);
160 /* binary operations matrix (op) scalar and scalar (op) matrix */
161 Matrix3x3 operator *(const Matrix3x3 &mat, scalar_t scalar);
162 Matrix3x3 operator *(scalar_t scalar, const Matrix3x3 &mat);
164 void operator *=(Matrix3x3 &mat, scalar_t scalar);
166 std::ostream &operator <<(std::ostream &out, const Matrix3x3 &mat);
170 /** 4x4 matrix */
171 class Matrix4x4 {
172 public:
173 scalar_t m[4][4];
175 static Matrix4x4 identity;
177 Matrix4x4();
178 Matrix4x4( scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14,
179 scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24,
180 scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34,
181 scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44);
182 Matrix4x4(const mat4_t cmat);
184 Matrix4x4(const Matrix3x3 &mat3x3);
186 /* binary operations matrix (op) matrix */
187 friend Matrix4x4 operator +(const Matrix4x4 &m1, const Matrix4x4 &m2);
188 friend Matrix4x4 operator -(const Matrix4x4 &m1, const Matrix4x4 &m2);
189 friend Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2);
191 friend void operator +=(Matrix4x4 &m1, const Matrix4x4 &m2);
192 friend void operator -=(Matrix4x4 &m1, const Matrix4x4 &m2);
193 friend VMATH_INLINE void operator *=(Matrix4x4 &m1, const Matrix4x4 &m2);
195 /* binary operations matrix (op) scalar and scalar (op) matrix */
196 friend Matrix4x4 operator *(const Matrix4x4 &mat, scalar_t scalar);
197 friend Matrix4x4 operator *(scalar_t scalar, const Matrix4x4 &mat);
199 friend void operator *=(Matrix4x4 &mat, scalar_t scalar);
201 VMATH_INLINE scalar_t *operator [](int index);
202 VMATH_INLINE const scalar_t *operator [](int index) const;
204 VMATH_INLINE void reset_identity();
206 void translate(const Vector3 &trans);
207 void set_translation(const Vector3 &trans);
208 Vector3 get_translation() const; /* extract translation */
210 void rotate(const Vector3 &euler_angles); /* 3d rotation with euler angles */
211 void rotate(const Vector3 &axis, scalar_t angle); /* 3d axis/angle rotation */
212 void rotate(const Quaternion &quat);
213 void set_rotation(const Vector3 &euler_angles);
214 void set_rotation(const Vector3 &axis, scalar_t angle);
215 void set_rotation(const Quaternion &quat);
216 Quaternion get_rotation_quat() const; /* extract rotation */
218 void scale(const Vector4 &scale_vec);
219 void set_scaling(const Vector4 &scale_vec);
220 Vector3 get_scaling() const; /* extract scaling */
222 void set_perspective(float vfov, float aspect, float znear, float zfar);
223 void set_orthographic(float left, float right, float bottom, float top, float znear = -1.0, float zfar = 1.0);
225 void set_column_vector(const Vector4 &vec, unsigned int col_index);
226 void set_row_vector(const Vector4 &vec, unsigned int row_index);
227 Vector4 get_column_vector(unsigned int col_index) const;
228 Vector4 get_row_vector(unsigned int row_index) const;
230 void transpose();
231 Matrix4x4 transposed() const;
232 scalar_t determinant() const;
233 Matrix4x4 adjoint() const;
234 Matrix4x4 inverse() const;
236 friend std::ostream &operator <<(std::ostream &out, const Matrix4x4 &mat);
237 };
239 /* binary operations matrix (op) matrix */
240 Matrix4x4 operator +(const Matrix4x4 &m1, const Matrix4x4 &m2);
241 Matrix4x4 operator -(const Matrix4x4 &m1, const Matrix4x4 &m2);
242 VMATH_INLINE Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2);
244 void operator +=(Matrix4x4 &m1, const Matrix4x4 &m2);
245 void operator -=(Matrix4x4 &m1, const Matrix4x4 &m2);
246 VMATH_INLINE void operator *=(Matrix4x4 &m1, const Matrix4x4 &m2);
248 /* binary operations matrix (op) scalar and scalar (op) matrix */
249 Matrix4x4 operator *(const Matrix4x4 &mat, scalar_t scalar);
250 Matrix4x4 operator *(scalar_t scalar, const Matrix4x4 &mat);
252 void operator *=(Matrix4x4 &mat, scalar_t scalar);
254 std::ostream &operator <<(std::ostream &out, const Matrix4x4 &mat);
256 #endif /* __cplusplus */
258 #include "matrix.inl"
260 #endif /* VMATH_MATRIX_H_ */