istereo
diff libs/vmath/matrix.inl @ 29:fb4c9641059f
added more forgotten files
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Thu, 08 Sep 2011 08:31:15 +0300 |
parents | |
children | ff055bff6a15 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/vmath/matrix.inl Thu Sep 08 08:31:15 2011 +0300 1.3 @@ -0,0 +1,147 @@ 1.4 +#include <string.h> 1.5 + 1.6 +#ifdef __cplusplus 1.7 +extern "C" { 1.8 +#endif /* __cplusplus */ 1.9 + 1.10 +/* C matrix 3x3 functions */ 1.11 +static inline void m3_identity(mat3_t m) 1.12 +{ 1.13 + static const mat3_t id = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}; 1.14 + memcpy(m, id, sizeof id); 1.15 +} 1.16 + 1.17 +static inline void m3_cons(mat3_t m, 1.18 + scalar_t m11, scalar_t m12, scalar_t m13, 1.19 + scalar_t m21, scalar_t m22, scalar_t m23, 1.20 + scalar_t m31, scalar_t m32, scalar_t m33) 1.21 +{ 1.22 + m[0][0] = m11; m[0][1] = m12; m[0][2] = m13; 1.23 + m[1][0] = m21; m[1][1] = m22; m[1][2] = m23; 1.24 + m[2][0] = m31; m[2][1] = m32; m[2][2] = m33; 1.25 +} 1.26 + 1.27 +static inline void m3_copy(mat3_t dest, mat3_t src) 1.28 +{ 1.29 + memcpy(dest, src, sizeof(mat3_t)); 1.30 +} 1.31 + 1.32 + 1.33 +/* C matrix 4x4 functions */ 1.34 +static inline void m4_identity(mat4_t m) 1.35 +{ 1.36 + static const mat4_t id = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}; 1.37 + memcpy(m, id, sizeof id); 1.38 +} 1.39 + 1.40 +static inline void m4_cons(mat4_t m, 1.41 + scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14, 1.42 + scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24, 1.43 + scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34, 1.44 + scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44) 1.45 +{ 1.46 + m[0][0] = m11; m[0][1] = m12; m[0][2] = m13; m[0][3] = m14; 1.47 + m[1][0] = m21; m[1][1] = m22; m[1][2] = m23; m[1][3] = m24; 1.48 + m[2][0] = m31; m[2][1] = m32; m[2][2] = m33; m[2][3] = m34; 1.49 + m[3][0] = m41; m[3][1] = m42; m[3][2] = m43; m[3][3] = m44; 1.50 +} 1.51 + 1.52 +static inline void m4_copy(mat4_t dest, mat4_t src) 1.53 +{ 1.54 + memcpy(dest, src, sizeof(mat4_t)); 1.55 +} 1.56 + 1.57 +static inline void m4_mult(mat4_t res, mat4_t m1, mat4_t m2) 1.58 +{ 1.59 + res[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]; 1.60 + res[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]; 1.61 + res[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]; 1.62 + res[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]; 1.63 + 1.64 + res[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]; 1.65 + res[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]; 1.66 + res[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]; 1.67 + res[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]; 1.68 + 1.69 + res[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]; 1.70 + res[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]; 1.71 + res[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]; 1.72 + res[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]; 1.73 + 1.74 + res[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]; 1.75 + res[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]; 1.76 + res[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]; 1.77 + res[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]; 1.78 +} 1.79 + 1.80 +static inline void m4_set_column(mat4_t m, vec4_t v, int idx) 1.81 +{ 1.82 + m[0][idx] = v.x; 1.83 + m[1][idx] = v.y; 1.84 + m[2][idx] = v.z; 1.85 + m[3][idx] = v.w; 1.86 +} 1.87 + 1.88 +static inline void m4_set_row(mat4_t m, vec4_t v, int idx) 1.89 +{ 1.90 + m[idx][0] = v.x; 1.91 + m[idx][1] = v.y; 1.92 + m[idx][2] = v.z; 1.93 + m[idx][3] = v.w; 1.94 +} 1.95 + 1.96 +#ifdef __cplusplus 1.97 +} /* extern "C" */ 1.98 + 1.99 + 1.100 +/* unrolled to hell and inline */ 1.101 +inline Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2) { 1.102 + Matrix4x4 res; 1.103 + 1.104 + 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]; 1.105 + 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]; 1.106 + 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]; 1.107 + 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]; 1.108 + 1.109 + 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]; 1.110 + 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]; 1.111 + 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]; 1.112 + 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]; 1.113 + 1.114 + 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]; 1.115 + 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]; 1.116 + 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]; 1.117 + 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]; 1.118 + 1.119 + 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]; 1.120 + 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]; 1.121 + 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]; 1.122 + 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]; 1.123 + 1.124 + return res; 1.125 +} 1.126 + 1.127 +inline scalar_t *Matrix3x3::operator [](int index) { 1.128 + return m[index]; 1.129 +} 1.130 + 1.131 +inline const scalar_t *Matrix3x3::operator [](int index) const { 1.132 + return m[index]; 1.133 +} 1.134 + 1.135 +inline void Matrix3x3::reset_identity() { 1.136 + memcpy(this->m, identity.m, 9 * sizeof(scalar_t)); 1.137 +} 1.138 + 1.139 +inline scalar_t *Matrix4x4::operator [](int index) { 1.140 + return m[index]; 1.141 +} 1.142 + 1.143 +inline const scalar_t *Matrix4x4::operator [](int index) const { 1.144 + return m[index]; 1.145 +} 1.146 + 1.147 +inline void Matrix4x4::reset_identity() { 1.148 + memcpy(this->m, identity.m, 16 * sizeof(scalar_t)); 1.149 +} 1.150 +#endif /* __cplusplus */