istereo

view libs/vmath/matrix.inl @ 33:33e9eedad390

ah.. blunder
author John Tsiombikas <nuclear@mutantstargoat.com>
date Fri, 09 Sep 2011 00:03:37 +0300
parents
children ff055bff6a15
line source
1 #include <string.h>
3 #ifdef __cplusplus
4 extern "C" {
5 #endif /* __cplusplus */
7 /* C matrix 3x3 functions */
8 static inline void m3_identity(mat3_t m)
9 {
10 static const mat3_t id = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
11 memcpy(m, id, sizeof id);
12 }
14 static inline void m3_cons(mat3_t m,
15 scalar_t m11, scalar_t m12, scalar_t m13,
16 scalar_t m21, scalar_t m22, scalar_t m23,
17 scalar_t m31, scalar_t m32, scalar_t m33)
18 {
19 m[0][0] = m11; m[0][1] = m12; m[0][2] = m13;
20 m[1][0] = m21; m[1][1] = m22; m[1][2] = m23;
21 m[2][0] = m31; m[2][1] = m32; m[2][2] = m33;
22 }
24 static inline void m3_copy(mat3_t dest, mat3_t src)
25 {
26 memcpy(dest, src, sizeof(mat3_t));
27 }
30 /* C matrix 4x4 functions */
31 static inline void m4_identity(mat4_t m)
32 {
33 static const mat4_t id = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
34 memcpy(m, id, sizeof id);
35 }
37 static inline void m4_cons(mat4_t m,
38 scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14,
39 scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24,
40 scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34,
41 scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44)
42 {
43 m[0][0] = m11; m[0][1] = m12; m[0][2] = m13; m[0][3] = m14;
44 m[1][0] = m21; m[1][1] = m22; m[1][2] = m23; m[1][3] = m24;
45 m[2][0] = m31; m[2][1] = m32; m[2][2] = m33; m[2][3] = m34;
46 m[3][0] = m41; m[3][1] = m42; m[3][2] = m43; m[3][3] = m44;
47 }
49 static inline void m4_copy(mat4_t dest, mat4_t src)
50 {
51 memcpy(dest, src, sizeof(mat4_t));
52 }
54 static inline void m4_mult(mat4_t res, mat4_t m1, mat4_t m2)
55 {
56 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];
57 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];
58 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];
59 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];
61 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];
62 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];
63 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];
64 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];
66 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];
67 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];
68 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];
69 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];
71 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];
72 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];
73 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];
74 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];
75 }
77 static inline void m4_set_column(mat4_t m, vec4_t v, int idx)
78 {
79 m[0][idx] = v.x;
80 m[1][idx] = v.y;
81 m[2][idx] = v.z;
82 m[3][idx] = v.w;
83 }
85 static inline void m4_set_row(mat4_t m, vec4_t v, int idx)
86 {
87 m[idx][0] = v.x;
88 m[idx][1] = v.y;
89 m[idx][2] = v.z;
90 m[idx][3] = v.w;
91 }
93 #ifdef __cplusplus
94 } /* extern "C" */
97 /* unrolled to hell and inline */
98 inline Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2) {
99 Matrix4x4 res;
101 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];
102 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];
103 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];
104 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];
106 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];
107 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];
108 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];
109 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];
111 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];
112 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];
113 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];
114 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];
116 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];
117 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];
118 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];
119 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];
121 return res;
122 }
124 inline scalar_t *Matrix3x3::operator [](int index) {
125 return m[index];
126 }
128 inline const scalar_t *Matrix3x3::operator [](int index) const {
129 return m[index];
130 }
132 inline void Matrix3x3::reset_identity() {
133 memcpy(this->m, identity.m, 9 * sizeof(scalar_t));
134 }
136 inline scalar_t *Matrix4x4::operator [](int index) {
137 return m[index];
138 }
140 inline const scalar_t *Matrix4x4::operator [](int index) const {
141 return m[index];
142 }
144 inline void Matrix4x4::reset_identity() {
145 memcpy(this->m, identity.m, 16 * sizeof(scalar_t));
146 }
147 #endif /* __cplusplus */