istereo
view libs/vmath/matrix.h @ 43:73813c1176de
better hgignore
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 14 Aug 2015 04:33:42 +0300 |
parents | c0ae8e668447 |
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 inline void m3_identity(mat3_t m);
31 static 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 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 inline void m4_identity(mat4_t m);
42 static 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 inline void m4_copy(mat4_t dest, mat4_t src);
48 void m4_to_m3(mat3_t dest, mat4_t src);
50 static inline void m4_mult(mat4_t res, mat4_t m1, mat4_t m2);
52 void m4_translate(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
53 void m4_rotate(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
54 void m4_rotate_x(mat4_t m, scalar_t angle);
55 void m4_rotate_y(mat4_t m, scalar_t angle);
56 void m4_rotate_z(mat4_t m, scalar_t angle);
57 void m4_rotate_axis(mat4_t m, scalar_t angle, scalar_t x, scalar_t y, scalar_t z);
58 void m4_rotate_quat(mat4_t m, quat_t q);
59 void m4_scale(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
60 static inline void m4_set_column(mat4_t m, vec4_t v, int idx);
61 static inline void m4_set_row(mat4_t m, vec4_t v, int idx);
63 void m4_transpose(mat4_t res, mat4_t m);
64 scalar_t m4_determinant(mat4_t m);
65 void m4_adjoint(mat4_t res, mat4_t m);
66 void m4_inverse(mat4_t res, mat4_t m);
68 void m4_print(FILE *fp, mat4_t m);
70 #ifdef __cplusplus
71 }
73 /* when included from C++ source files, also define the matrix classes */
74 #include <iostream>
76 /** 3x3 matrix */
77 class Matrix3x3 {
78 private:
79 scalar_t m[3][3];
81 public:
83 static Matrix3x3 identity;
85 Matrix3x3();
86 Matrix3x3( scalar_t m11, scalar_t m12, scalar_t m13,
87 scalar_t m21, scalar_t m22, scalar_t m23,
88 scalar_t m31, scalar_t m32, scalar_t m33);
89 Matrix3x3(const mat3_t cmat);
91 Matrix3x3(const Matrix4x4 &mat4x4);
93 /* binary operations matrix (op) matrix */
94 friend Matrix3x3 operator +(const Matrix3x3 &m1, const Matrix3x3 &m2);
95 friend Matrix3x3 operator -(const Matrix3x3 &m1, const Matrix3x3 &m2);
96 friend Matrix3x3 operator *(const Matrix3x3 &m1, const Matrix3x3 &m2);
98 friend void operator +=(Matrix3x3 &m1, const Matrix3x3 &m2);
99 friend void operator -=(Matrix3x3 &m1, const Matrix3x3 &m2);
100 friend void operator *=(Matrix3x3 &m1, const Matrix3x3 &m2);
102 /* binary operations matrix (op) scalar and scalar (op) matrix */
103 friend Matrix3x3 operator *(const Matrix3x3 &mat, scalar_t scalar);
104 friend Matrix3x3 operator *(scalar_t scalar, const Matrix3x3 &mat);
106 friend void operator *=(Matrix3x3 &mat, scalar_t scalar);
108 inline scalar_t *operator [](int index);
109 inline const scalar_t *operator [](int index) const;
111 inline void reset_identity();
113 void translate(const Vector2 &trans);
114 void set_translation(const Vector2 &trans);
116 void rotate(scalar_t angle); /* 2d rotation */
117 void rotate(const Vector3 &euler_angles); /* 3d rotation with euler angles */
118 void rotate(const Vector3 &axis, scalar_t angle); /* 3d axis/angle rotation */
119 void set_rotation(scalar_t angle);
120 void set_rotation(const Vector3 &euler_angles);
121 void set_rotation(const Vector3 &axis, scalar_t angle);
123 void scale(const Vector3 &scale_vec);
124 void set_scaling(const Vector3 &scale_vec);
126 void set_column_vector(const Vector3 &vec, unsigned int col_index);
127 void set_row_vector(const Vector3 &vec, unsigned int row_index);
128 Vector3 get_column_vector(unsigned int col_index) const;
129 Vector3 get_row_vector(unsigned int row_index) const;
131 void transpose();
132 Matrix3x3 transposed() const;
133 scalar_t determinant() const;
134 Matrix3x3 inverse() const;
136 friend std::ostream &operator <<(std::ostream &out, const Matrix3x3 &mat);
137 };
139 /** 4x4 matrix */
140 class Matrix4x4 {
141 private:
142 scalar_t m[4][4];
144 public:
146 static Matrix4x4 identity;
148 Matrix4x4();
149 Matrix4x4( scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14,
150 scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24,
151 scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34,
152 scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44);
153 Matrix4x4(const mat4_t cmat);
155 Matrix4x4(const Matrix3x3 &mat3x3);
157 /* binary operations matrix (op) matrix */
158 friend Matrix4x4 operator +(const Matrix4x4 &m1, const Matrix4x4 &m2);
159 friend Matrix4x4 operator -(const Matrix4x4 &m1, const Matrix4x4 &m2);
160 friend Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2);
162 friend void operator +=(Matrix4x4 &m1, const Matrix4x4 &m2);
163 friend void operator -=(Matrix4x4 &m1, const Matrix4x4 &m2);
164 friend inline void operator *=(Matrix4x4 &m1, const Matrix4x4 &m2);
166 /* binary operations matrix (op) scalar and scalar (op) matrix */
167 friend Matrix4x4 operator *(const Matrix4x4 &mat, scalar_t scalar);
168 friend Matrix4x4 operator *(scalar_t scalar, const Matrix4x4 &mat);
170 friend void operator *=(Matrix4x4 &mat, scalar_t scalar);
172 inline scalar_t *operator [](int index);
173 inline const scalar_t *operator [](int index) const;
175 inline void reset_identity();
177 void translate(const Vector3 &trans);
178 void set_translation(const Vector3 &trans);
180 void rotate(const Vector3 &euler_angles); /* 3d rotation with euler angles */
181 void rotate(const Vector3 &axis, scalar_t angle); /* 3d axis/angle rotation */
182 void set_rotation(const Vector3 &euler_angles);
183 void set_rotation(const Vector3 &axis, scalar_t angle);
185 void scale(const Vector4 &scale_vec);
186 void set_scaling(const Vector4 &scale_vec);
188 void set_column_vector(const Vector4 &vec, unsigned int col_index);
189 void set_row_vector(const Vector4 &vec, unsigned int row_index);
190 Vector4 get_column_vector(unsigned int col_index) const;
191 Vector4 get_row_vector(unsigned int row_index) const;
193 void transpose();
194 Matrix4x4 transposed() const;
195 scalar_t determinant() const;
196 Matrix4x4 adjoint() const;
197 Matrix4x4 inverse() const;
199 friend std::ostream &operator <<(std::ostream &out, const Matrix4x4 &mat);
200 };
201 #endif /* __cplusplus */
203 #include "matrix.inl"
205 #endif /* VMATH_MATRIX_H_ */