goat3d

annotate libs/vmath/matrix.h @ 43:1dcbe87b6a5d

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