dbf-halloween2015

annotate libs/vmath/matrix.h @ 4:4316c0c879e9

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