rev |
line source |
nuclear@29
|
1 #include <string.h>
|
nuclear@29
|
2
|
nuclear@29
|
3 #ifdef __cplusplus
|
nuclear@29
|
4 extern "C" {
|
nuclear@29
|
5 #endif /* __cplusplus */
|
nuclear@29
|
6
|
nuclear@29
|
7 /* C matrix 3x3 functions */
|
nuclear@29
|
8 static inline void m3_identity(mat3_t m)
|
nuclear@29
|
9 {
|
nuclear@29
|
10 static const mat3_t id = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
|
nuclear@29
|
11 memcpy(m, id, sizeof id);
|
nuclear@29
|
12 }
|
nuclear@29
|
13
|
nuclear@29
|
14 static inline void m3_cons(mat3_t m,
|
nuclear@29
|
15 scalar_t m11, scalar_t m12, scalar_t m13,
|
nuclear@29
|
16 scalar_t m21, scalar_t m22, scalar_t m23,
|
nuclear@29
|
17 scalar_t m31, scalar_t m32, scalar_t m33)
|
nuclear@29
|
18 {
|
nuclear@29
|
19 m[0][0] = m11; m[0][1] = m12; m[0][2] = m13;
|
nuclear@29
|
20 m[1][0] = m21; m[1][1] = m22; m[1][2] = m23;
|
nuclear@29
|
21 m[2][0] = m31; m[2][1] = m32; m[2][2] = m33;
|
nuclear@29
|
22 }
|
nuclear@29
|
23
|
nuclear@29
|
24 static inline void m3_copy(mat3_t dest, mat3_t src)
|
nuclear@29
|
25 {
|
nuclear@29
|
26 memcpy(dest, src, sizeof(mat3_t));
|
nuclear@29
|
27 }
|
nuclear@29
|
28
|
nuclear@29
|
29
|
nuclear@29
|
30 /* C matrix 4x4 functions */
|
nuclear@29
|
31 static inline void m4_identity(mat4_t m)
|
nuclear@29
|
32 {
|
nuclear@29
|
33 static const mat4_t id = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
|
nuclear@29
|
34 memcpy(m, id, sizeof id);
|
nuclear@29
|
35 }
|
nuclear@29
|
36
|
nuclear@29
|
37 static inline void m4_cons(mat4_t m,
|
nuclear@29
|
38 scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14,
|
nuclear@29
|
39 scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24,
|
nuclear@29
|
40 scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34,
|
nuclear@29
|
41 scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44)
|
nuclear@29
|
42 {
|
nuclear@29
|
43 m[0][0] = m11; m[0][1] = m12; m[0][2] = m13; m[0][3] = m14;
|
nuclear@29
|
44 m[1][0] = m21; m[1][1] = m22; m[1][2] = m23; m[1][3] = m24;
|
nuclear@29
|
45 m[2][0] = m31; m[2][1] = m32; m[2][2] = m33; m[2][3] = m34;
|
nuclear@29
|
46 m[3][0] = m41; m[3][1] = m42; m[3][2] = m43; m[3][3] = m44;
|
nuclear@29
|
47 }
|
nuclear@29
|
48
|
nuclear@29
|
49 static inline void m4_copy(mat4_t dest, mat4_t src)
|
nuclear@29
|
50 {
|
nuclear@29
|
51 memcpy(dest, src, sizeof(mat4_t));
|
nuclear@29
|
52 }
|
nuclear@29
|
53
|
nuclear@29
|
54 static inline void m4_mult(mat4_t res, mat4_t m1, mat4_t m2)
|
nuclear@29
|
55 {
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
60
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
65
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
70
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
75 }
|
nuclear@29
|
76
|
nuclear@29
|
77 static inline void m4_set_column(mat4_t m, vec4_t v, int idx)
|
nuclear@29
|
78 {
|
nuclear@29
|
79 m[0][idx] = v.x;
|
nuclear@29
|
80 m[1][idx] = v.y;
|
nuclear@29
|
81 m[2][idx] = v.z;
|
nuclear@29
|
82 m[3][idx] = v.w;
|
nuclear@29
|
83 }
|
nuclear@29
|
84
|
nuclear@29
|
85 static inline void m4_set_row(mat4_t m, vec4_t v, int idx)
|
nuclear@29
|
86 {
|
nuclear@29
|
87 m[idx][0] = v.x;
|
nuclear@29
|
88 m[idx][1] = v.y;
|
nuclear@29
|
89 m[idx][2] = v.z;
|
nuclear@29
|
90 m[idx][3] = v.w;
|
nuclear@29
|
91 }
|
nuclear@29
|
92
|
nuclear@29
|
93 #ifdef __cplusplus
|
nuclear@29
|
94 } /* extern "C" */
|
nuclear@29
|
95
|
nuclear@29
|
96
|
nuclear@29
|
97 /* unrolled to hell and inline */
|
nuclear@29
|
98 inline Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2) {
|
nuclear@29
|
99 Matrix4x4 res;
|
nuclear@29
|
100
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
105
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
110
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
115
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
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];
|
nuclear@29
|
120
|
nuclear@29
|
121 return res;
|
nuclear@29
|
122 }
|
nuclear@29
|
123
|
nuclear@29
|
124 inline scalar_t *Matrix3x3::operator [](int index) {
|
nuclear@29
|
125 return m[index];
|
nuclear@29
|
126 }
|
nuclear@29
|
127
|
nuclear@29
|
128 inline const scalar_t *Matrix3x3::operator [](int index) const {
|
nuclear@29
|
129 return m[index];
|
nuclear@29
|
130 }
|
nuclear@29
|
131
|
nuclear@29
|
132 inline void Matrix3x3::reset_identity() {
|
nuclear@29
|
133 memcpy(this->m, identity.m, 9 * sizeof(scalar_t));
|
nuclear@29
|
134 }
|
nuclear@29
|
135
|
nuclear@29
|
136 inline scalar_t *Matrix4x4::operator [](int index) {
|
nuclear@29
|
137 return m[index];
|
nuclear@29
|
138 }
|
nuclear@29
|
139
|
nuclear@29
|
140 inline const scalar_t *Matrix4x4::operator [](int index) const {
|
nuclear@29
|
141 return m[index];
|
nuclear@29
|
142 }
|
nuclear@29
|
143
|
nuclear@29
|
144 inline void Matrix4x4::reset_identity() {
|
nuclear@29
|
145 memcpy(this->m, identity.m, 16 * sizeof(scalar_t));
|
nuclear@29
|
146 }
|
nuclear@29
|
147 #endif /* __cplusplus */
|