nuclear@0: /* nuclear@0: libvmath - a vector math library nuclear@0: Copyright (C) 2004-2011 John Tsiombikas nuclear@0: nuclear@0: This program is free software: you can redistribute it and/or modify nuclear@0: it under the terms of the GNU Lesser General Public License as published nuclear@0: by the Free Software Foundation, either version 3 of the License, or nuclear@0: (at your option) any later version. nuclear@0: nuclear@0: This program is distributed in the hope that it will be useful, nuclear@0: but WITHOUT ANY WARRANTY; without even the implied warranty of nuclear@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the nuclear@0: GNU Lesser General Public License for more details. nuclear@0: nuclear@0: You should have received a copy of the GNU Lesser General Public License nuclear@0: along with this program. If not, see . nuclear@0: */ nuclear@0: nuclear@0: #include nuclear@0: nuclear@0: #ifdef __cplusplus nuclear@0: extern "C" { nuclear@0: #endif /* __cplusplus */ nuclear@0: nuclear@0: /* C matrix 3x3 functions */ nuclear@0: static inline void m3_identity(mat3_t m) nuclear@0: { nuclear@0: static const mat3_t id = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}; nuclear@0: memcpy(m, id, sizeof id); nuclear@0: } nuclear@0: nuclear@0: static inline void m3_cons(mat3_t m, nuclear@0: scalar_t m11, scalar_t m12, scalar_t m13, nuclear@0: scalar_t m21, scalar_t m22, scalar_t m23, nuclear@0: scalar_t m31, scalar_t m32, scalar_t m33) nuclear@0: { nuclear@0: m[0][0] = m11; m[0][1] = m12; m[0][2] = m13; nuclear@0: m[1][0] = m21; m[1][1] = m22; m[1][2] = m23; nuclear@0: m[2][0] = m31; m[2][1] = m32; m[2][2] = m33; nuclear@0: } nuclear@0: nuclear@0: static inline void m3_copy(mat3_t dest, mat3_t src) nuclear@0: { nuclear@0: memcpy(dest, src, sizeof(mat3_t)); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* C matrix 4x4 functions */ nuclear@0: static inline void m4_identity(mat4_t m) nuclear@0: { nuclear@0: static const mat4_t id = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}; nuclear@0: memcpy(m, id, sizeof id); nuclear@0: } nuclear@0: nuclear@0: static inline void m4_cons(mat4_t m, nuclear@0: scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14, nuclear@0: scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24, nuclear@0: scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34, nuclear@0: scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44) nuclear@0: { nuclear@0: m[0][0] = m11; m[0][1] = m12; m[0][2] = m13; m[0][3] = m14; nuclear@0: m[1][0] = m21; m[1][1] = m22; m[1][2] = m23; m[1][3] = m24; nuclear@0: m[2][0] = m31; m[2][1] = m32; m[2][2] = m33; m[2][3] = m34; nuclear@0: m[3][0] = m41; m[3][1] = m42; m[3][2] = m43; m[3][3] = m44; nuclear@0: } nuclear@0: nuclear@0: static inline void m4_copy(mat4_t dest, mat4_t src) nuclear@0: { nuclear@0: memcpy(dest, src, sizeof(mat4_t)); nuclear@0: } nuclear@0: nuclear@0: static inline void m4_mult(mat4_t res, mat4_t m1, mat4_t m2) nuclear@0: { nuclear@0: mat4_t tmp; nuclear@0: nuclear@0: /* nuclear@0: int i, j; nuclear@0: for(i=0; i<4; i++) { nuclear@0: for(j=0; j<4; j++) { nuclear@0: tmp[i][j] = m1[i][0] * m2[0][j] + m1[i][1] * m2[1][j] + m1[i][2] * m2[2][j] + m1[i][3] * m2[3][j]; nuclear@0: } nuclear@0: } nuclear@0: */ nuclear@0: nuclear@0: tmp[0][0] = m1[0][0] * m2[0][0] + m1[0][1] * m2[1][0] + m1[0][2] * m2[2][0] + m1[0][3] * m2[3][0]; nuclear@0: tmp[0][1] = m1[0][0] * m2[0][1] + m1[0][1] * m2[1][1] + m1[0][2] * m2[2][1] + m1[0][3] * m2[3][1]; nuclear@0: tmp[0][2] = m1[0][0] * m2[0][2] + m1[0][1] * m2[1][2] + m1[0][2] * m2[2][2] + m1[0][3] * m2[3][2]; nuclear@0: tmp[0][3] = m1[0][0] * m2[0][3] + m1[0][1] * m2[1][3] + m1[0][2] * m2[2][3] + m1[0][3] * m2[3][3]; nuclear@0: nuclear@0: tmp[1][0] = m1[1][0] * m2[0][0] + m1[1][1] * m2[1][0] + m1[1][2] * m2[2][0] + m1[1][3] * m2[3][0]; nuclear@0: tmp[1][1] = m1[1][0] * m2[0][1] + m1[1][1] * m2[1][1] + m1[1][2] * m2[2][1] + m1[1][3] * m2[3][1]; nuclear@0: tmp[1][2] = m1[1][0] * m2[0][2] + m1[1][1] * m2[1][2] + m1[1][2] * m2[2][2] + m1[1][3] * m2[3][2]; nuclear@0: tmp[1][3] = m1[1][0] * m2[0][3] + m1[1][1] * m2[1][3] + m1[1][2] * m2[2][3] + m1[1][3] * m2[3][3]; nuclear@0: nuclear@0: tmp[2][0] = m1[2][0] * m2[0][0] + m1[2][1] * m2[1][0] + m1[2][2] * m2[2][0] + m1[2][3] * m2[3][0]; nuclear@0: tmp[2][1] = m1[2][0] * m2[0][1] + m1[2][1] * m2[1][1] + m1[2][2] * m2[2][1] + m1[2][3] * m2[3][1]; nuclear@0: tmp[2][2] = m1[2][0] * m2[0][2] + m1[2][1] * m2[1][2] + m1[2][2] * m2[2][2] + m1[2][3] * m2[3][2]; nuclear@0: tmp[2][3] = m1[2][0] * m2[0][3] + m1[2][1] * m2[1][3] + m1[2][2] * m2[2][3] + m1[2][3] * m2[3][3]; nuclear@0: nuclear@0: tmp[3][0] = m1[3][0] * m2[0][0] + m1[3][1] * m2[1][0] + m1[3][2] * m2[2][0] + m1[3][3] * m2[3][0]; nuclear@0: tmp[3][1] = m1[3][0] * m2[0][1] + m1[3][1] * m2[1][1] + m1[3][2] * m2[2][1] + m1[3][3] * m2[3][1]; nuclear@0: tmp[3][2] = m1[3][0] * m2[0][2] + m1[3][1] * m2[1][2] + m1[3][2] * m2[2][2] + m1[3][3] * m2[3][2]; nuclear@0: tmp[3][3] = m1[3][0] * m2[0][3] + m1[3][1] * m2[1][3] + m1[3][2] * m2[2][3] + m1[3][3] * m2[3][3]; nuclear@0: nuclear@0: m4_copy(res, tmp); nuclear@0: } nuclear@0: nuclear@0: static inline void m4_set_column(mat4_t m, vec4_t v, int idx) nuclear@0: { nuclear@0: m[0][idx] = v.x; nuclear@0: m[1][idx] = v.y; nuclear@0: m[2][idx] = v.z; nuclear@0: m[3][idx] = v.w; nuclear@0: } nuclear@0: nuclear@0: static inline void m4_set_row(mat4_t m, vec4_t v, int idx) nuclear@0: { nuclear@0: m[idx][0] = v.x; nuclear@0: m[idx][1] = v.y; nuclear@0: m[idx][2] = v.z; nuclear@0: m[idx][3] = v.w; nuclear@0: } nuclear@0: nuclear@0: #ifdef __cplusplus nuclear@0: } /* extern "C" */ nuclear@0: nuclear@0: nuclear@0: /* unrolled to hell and inline */ nuclear@0: inline Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2) nuclear@0: { nuclear@0: Matrix4x4 res; nuclear@0: nuclear@0: /* nuclear@0: for(i=0; i<4; i++) { nuclear@0: for(j=0; j<4; j++) { nuclear@0: res.m[i][j] = m1.m[i][0] * m2.m[0][j] + m1.m[i][1] * m2.m[1][j] + m1.m[i][2] * m2.m[2][j] + m1.m[i][3] * m2.m[3][j]; nuclear@0: } nuclear@0: } nuclear@0: */ nuclear@0: nuclear@0: res.m[0][0] = m1.m[0][0] * m2.m[0][0] + m1.m[0][1] * m2.m[1][0] + m1.m[0][2] * m2.m[2][0] + m1.m[0][3] * m2.m[3][0]; nuclear@0: res.m[0][1] = m1.m[0][0] * m2.m[0][1] + m1.m[0][1] * m2.m[1][1] + m1.m[0][2] * m2.m[2][1] + m1.m[0][3] * m2.m[3][1]; nuclear@0: res.m[0][2] = m1.m[0][0] * m2.m[0][2] + m1.m[0][1] * m2.m[1][2] + m1.m[0][2] * m2.m[2][2] + m1.m[0][3] * m2.m[3][2]; nuclear@0: res.m[0][3] = m1.m[0][0] * m2.m[0][3] + m1.m[0][1] * m2.m[1][3] + m1.m[0][2] * m2.m[2][3] + m1.m[0][3] * m2.m[3][3]; nuclear@0: nuclear@0: res.m[1][0] = m1.m[1][0] * m2.m[0][0] + m1.m[1][1] * m2.m[1][0] + m1.m[1][2] * m2.m[2][0] + m1.m[1][3] * m2.m[3][0]; nuclear@0: res.m[1][1] = m1.m[1][0] * m2.m[0][1] + m1.m[1][1] * m2.m[1][1] + m1.m[1][2] * m2.m[2][1] + m1.m[1][3] * m2.m[3][1]; nuclear@0: res.m[1][2] = m1.m[1][0] * m2.m[0][2] + m1.m[1][1] * m2.m[1][2] + m1.m[1][2] * m2.m[2][2] + m1.m[1][3] * m2.m[3][2]; nuclear@0: res.m[1][3] = m1.m[1][0] * m2.m[0][3] + m1.m[1][1] * m2.m[1][3] + m1.m[1][2] * m2.m[2][3] + m1.m[1][3] * m2.m[3][3]; nuclear@0: nuclear@0: res.m[2][0] = m1.m[2][0] * m2.m[0][0] + m1.m[2][1] * m2.m[1][0] + m1.m[2][2] * m2.m[2][0] + m1.m[2][3] * m2.m[3][0]; nuclear@0: res.m[2][1] = m1.m[2][0] * m2.m[0][1] + m1.m[2][1] * m2.m[1][1] + m1.m[2][2] * m2.m[2][1] + m1.m[2][3] * m2.m[3][1]; nuclear@0: res.m[2][2] = m1.m[2][0] * m2.m[0][2] + m1.m[2][1] * m2.m[1][2] + m1.m[2][2] * m2.m[2][2] + m1.m[2][3] * m2.m[3][2]; nuclear@0: res.m[2][3] = m1.m[2][0] * m2.m[0][3] + m1.m[2][1] * m2.m[1][3] + m1.m[2][2] * m2.m[2][3] + m1.m[2][3] * m2.m[3][3]; nuclear@0: nuclear@0: res.m[3][0] = m1.m[3][0] * m2.m[0][0] + m1.m[3][1] * m2.m[1][0] + m1.m[3][2] * m2.m[2][0] + m1.m[3][3] * m2.m[3][0]; nuclear@0: res.m[3][1] = m1.m[3][0] * m2.m[0][1] + m1.m[3][1] * m2.m[1][1] + m1.m[3][2] * m2.m[2][1] + m1.m[3][3] * m2.m[3][1]; nuclear@0: res.m[3][2] = m1.m[3][0] * m2.m[0][2] + m1.m[3][1] * m2.m[1][2] + m1.m[3][2] * m2.m[2][2] + m1.m[3][3] * m2.m[3][2]; nuclear@0: res.m[3][3] = m1.m[3][0] * m2.m[0][3] + m1.m[3][1] * m2.m[1][3] + m1.m[3][2] * m2.m[2][3] + m1.m[3][3] * m2.m[3][3]; nuclear@0: nuclear@0: return res; nuclear@0: } nuclear@0: nuclear@0: inline void operator *=(Matrix4x4 &m1, const Matrix4x4 &m2) nuclear@0: { nuclear@0: Matrix4x4 res = m1 * m2; nuclear@0: m1 = res; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: inline scalar_t *Matrix3x3::operator [](int index) nuclear@0: { nuclear@0: return m[index]; nuclear@0: } nuclear@0: nuclear@0: inline const scalar_t *Matrix3x3::operator [](int index) const nuclear@0: { nuclear@0: return m[index]; nuclear@0: } nuclear@0: nuclear@0: inline void Matrix3x3::reset_identity() nuclear@0: { nuclear@0: *this = identity; nuclear@0: } nuclear@0: nuclear@0: inline scalar_t *Matrix4x4::operator [](int index) nuclear@0: { nuclear@0: return m[index]; nuclear@0: } nuclear@0: nuclear@0: inline const scalar_t *Matrix4x4::operator [](int index) const nuclear@0: { nuclear@0: return m[index]; nuclear@0: } nuclear@0: nuclear@0: inline void Matrix4x4::reset_identity() nuclear@0: { nuclear@0: *this = identity; nuclear@0: } nuclear@0: #endif /* __cplusplus */