rev |
line source |
nuclear@2
|
1 /*
|
nuclear@2
|
2 libvmath - a vector math library
|
nuclear@2
|
3 Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
|
nuclear@2
|
4
|
nuclear@2
|
5 This program is free software: you can redistribute it and/or modify
|
nuclear@2
|
6 it under the terms of the GNU Lesser General Public License as published
|
nuclear@2
|
7 by the Free Software Foundation, either version 3 of the License, or
|
nuclear@2
|
8 (at your option) any later version.
|
nuclear@2
|
9
|
nuclear@2
|
10 This program is distributed in the hope that it will be useful,
|
nuclear@2
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
nuclear@2
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
nuclear@2
|
13 GNU Lesser General Public License for more details.
|
nuclear@2
|
14
|
nuclear@2
|
15 You should have received a copy of the GNU Lesser General Public License
|
nuclear@2
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
nuclear@2
|
17 */
|
nuclear@2
|
18
|
nuclear@2
|
19 #include <string.h>
|
nuclear@2
|
20
|
nuclear@2
|
21 #ifdef __cplusplus
|
nuclear@2
|
22 extern "C" {
|
nuclear@2
|
23 #endif /* __cplusplus */
|
nuclear@2
|
24
|
nuclear@2
|
25 /* C matrix 3x3 functions */
|
nuclear@2
|
26 static inline void m3_identity(mat3_t m)
|
nuclear@2
|
27 {
|
nuclear@2
|
28 static const mat3_t id = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
|
nuclear@2
|
29 memcpy(m, id, sizeof id);
|
nuclear@2
|
30 }
|
nuclear@2
|
31
|
nuclear@2
|
32 static inline void m3_cons(mat3_t m,
|
nuclear@2
|
33 scalar_t m11, scalar_t m12, scalar_t m13,
|
nuclear@2
|
34 scalar_t m21, scalar_t m22, scalar_t m23,
|
nuclear@2
|
35 scalar_t m31, scalar_t m32, scalar_t m33)
|
nuclear@2
|
36 {
|
nuclear@2
|
37 m[0][0] = m11; m[0][1] = m12; m[0][2] = m13;
|
nuclear@2
|
38 m[1][0] = m21; m[1][1] = m22; m[1][2] = m23;
|
nuclear@2
|
39 m[2][0] = m31; m[2][1] = m32; m[2][2] = m33;
|
nuclear@2
|
40 }
|
nuclear@2
|
41
|
nuclear@2
|
42 static inline void m3_copy(mat3_t dest, mat3_t src)
|
nuclear@2
|
43 {
|
nuclear@2
|
44 memcpy(dest, src, sizeof(mat3_t));
|
nuclear@2
|
45 }
|
nuclear@2
|
46
|
nuclear@2
|
47
|
nuclear@2
|
48 /* C matrix 4x4 functions */
|
nuclear@2
|
49 static inline void m4_identity(mat4_t m)
|
nuclear@2
|
50 {
|
nuclear@2
|
51 static const mat4_t id = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
|
nuclear@2
|
52 memcpy(m, id, sizeof id);
|
nuclear@2
|
53 }
|
nuclear@2
|
54
|
nuclear@2
|
55 static inline void m4_cons(mat4_t m,
|
nuclear@2
|
56 scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14,
|
nuclear@2
|
57 scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24,
|
nuclear@2
|
58 scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34,
|
nuclear@2
|
59 scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44)
|
nuclear@2
|
60 {
|
nuclear@2
|
61 m[0][0] = m11; m[0][1] = m12; m[0][2] = m13; m[0][3] = m14;
|
nuclear@2
|
62 m[1][0] = m21; m[1][1] = m22; m[1][2] = m23; m[1][3] = m24;
|
nuclear@2
|
63 m[2][0] = m31; m[2][1] = m32; m[2][2] = m33; m[2][3] = m34;
|
nuclear@2
|
64 m[3][0] = m41; m[3][1] = m42; m[3][2] = m43; m[3][3] = m44;
|
nuclear@2
|
65 }
|
nuclear@2
|
66
|
nuclear@2
|
67 static inline void m4_copy(mat4_t dest, mat4_t src)
|
nuclear@2
|
68 {
|
nuclear@2
|
69 memcpy(dest, src, sizeof(mat4_t));
|
nuclear@2
|
70 }
|
nuclear@2
|
71
|
nuclear@2
|
72 static inline void m4_mult(mat4_t res, mat4_t m1, mat4_t m2)
|
nuclear@2
|
73 {
|
nuclear@2
|
74 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@2
|
75 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@2
|
76 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@2
|
77 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@2
|
78
|
nuclear@2
|
79 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@2
|
80 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@2
|
81 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@2
|
82 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@2
|
83
|
nuclear@2
|
84 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@2
|
85 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@2
|
86 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@2
|
87 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@2
|
88
|
nuclear@2
|
89 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@2
|
90 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@2
|
91 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@2
|
92 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@2
|
93 }
|
nuclear@2
|
94
|
nuclear@2
|
95 static inline void m4_set_column(mat4_t m, vec4_t v, int idx)
|
nuclear@2
|
96 {
|
nuclear@2
|
97 m[0][idx] = v.x;
|
nuclear@2
|
98 m[1][idx] = v.y;
|
nuclear@2
|
99 m[2][idx] = v.z;
|
nuclear@2
|
100 m[3][idx] = v.w;
|
nuclear@2
|
101 }
|
nuclear@2
|
102
|
nuclear@2
|
103 static inline void m4_set_row(mat4_t m, vec4_t v, int idx)
|
nuclear@2
|
104 {
|
nuclear@2
|
105 m[idx][0] = v.x;
|
nuclear@2
|
106 m[idx][1] = v.y;
|
nuclear@2
|
107 m[idx][2] = v.z;
|
nuclear@2
|
108 m[idx][3] = v.w;
|
nuclear@2
|
109 }
|
nuclear@2
|
110
|
nuclear@2
|
111 #ifdef __cplusplus
|
nuclear@2
|
112 } /* extern "C" */
|
nuclear@2
|
113
|
nuclear@2
|
114
|
nuclear@2
|
115 /* unrolled to hell and inline */
|
nuclear@2
|
116 inline Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2) {
|
nuclear@2
|
117 Matrix4x4 res;
|
nuclear@2
|
118
|
nuclear@2
|
119 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@2
|
120 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@2
|
121 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@2
|
122 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@2
|
123
|
nuclear@2
|
124 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@2
|
125 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@2
|
126 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@2
|
127 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@2
|
128
|
nuclear@2
|
129 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@2
|
130 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@2
|
131 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@2
|
132 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@2
|
133
|
nuclear@2
|
134 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@2
|
135 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@2
|
136 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@2
|
137 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@2
|
138
|
nuclear@2
|
139 return res;
|
nuclear@2
|
140 }
|
nuclear@2
|
141
|
nuclear@2
|
142 inline scalar_t *Matrix3x3::operator [](int index) {
|
nuclear@2
|
143 return m[index];
|
nuclear@2
|
144 }
|
nuclear@2
|
145
|
nuclear@2
|
146 inline const scalar_t *Matrix3x3::operator [](int index) const {
|
nuclear@2
|
147 return m[index];
|
nuclear@2
|
148 }
|
nuclear@2
|
149
|
nuclear@2
|
150 inline void Matrix3x3::reset_identity() {
|
nuclear@2
|
151 memcpy(this->m, identity.m, 9 * sizeof(scalar_t));
|
nuclear@2
|
152 }
|
nuclear@2
|
153
|
nuclear@2
|
154 inline scalar_t *Matrix4x4::operator [](int index) {
|
nuclear@2
|
155 return m[index];
|
nuclear@2
|
156 }
|
nuclear@2
|
157
|
nuclear@2
|
158 inline const scalar_t *Matrix4x4::operator [](int index) const {
|
nuclear@2
|
159 return m[index];
|
nuclear@2
|
160 }
|
nuclear@2
|
161
|
nuclear@2
|
162 inline void Matrix4x4::reset_identity() {
|
nuclear@2
|
163 memcpy(this->m, identity.m, 16 * sizeof(scalar_t));
|
nuclear@2
|
164 }
|
nuclear@2
|
165 #endif /* __cplusplus */
|