rev |
line source |
nuclear@27
|
1 /*
|
nuclear@27
|
2 libvmath - a vector math library
|
nuclear@27
|
3 Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
|
nuclear@27
|
4
|
nuclear@27
|
5 This program is free software: you can redistribute it and/or modify
|
nuclear@27
|
6 it under the terms of the GNU Lesser General Public License as published
|
nuclear@27
|
7 by the Free Software Foundation, either version 3 of the License, or
|
nuclear@27
|
8 (at your option) any later version.
|
nuclear@27
|
9
|
nuclear@27
|
10 This program is distributed in the hope that it will be useful,
|
nuclear@27
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
nuclear@27
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
nuclear@27
|
13 GNU Lesser General Public License for more details.
|
nuclear@27
|
14
|
nuclear@27
|
15 You should have received a copy of the GNU Lesser General Public License
|
nuclear@27
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
nuclear@27
|
17 */
|
nuclear@27
|
18
|
nuclear@27
|
19
|
nuclear@27
|
20 #include <stdio.h>
|
nuclear@27
|
21 #include "matrix.h"
|
nuclear@27
|
22 #include "vector.h"
|
nuclear@27
|
23 #include "quat.h"
|
nuclear@27
|
24
|
nuclear@27
|
25 void m3_to_m4(mat4_t dest, mat3_t src)
|
nuclear@27
|
26 {
|
nuclear@27
|
27 int i, j;
|
nuclear@27
|
28
|
nuclear@27
|
29 memset(dest, 0, sizeof(mat4_t));
|
nuclear@27
|
30 for(i=0; i<3; i++) {
|
nuclear@27
|
31 for(j=0; j<3; j++) {
|
nuclear@27
|
32 dest[i][j] = src[i][j];
|
nuclear@27
|
33 }
|
nuclear@27
|
34 }
|
nuclear@27
|
35 dest[3][3] = 1.0;
|
nuclear@27
|
36 }
|
nuclear@27
|
37
|
nuclear@27
|
38 void m3_print(FILE *fp, mat3_t m)
|
nuclear@27
|
39 {
|
nuclear@27
|
40 int i;
|
nuclear@27
|
41 for(i=0; i<3; i++) {
|
nuclear@27
|
42 fprintf(fp, "[ %12.5f %12.5f %12.5f ]\n", (float)m[i][0], (float)m[i][1], (float)m[i][2]);
|
nuclear@27
|
43 }
|
nuclear@27
|
44 }
|
nuclear@27
|
45
|
nuclear@27
|
46 /* C matrix 4x4 functions */
|
nuclear@27
|
47 void m4_to_m3(mat3_t dest, mat4_t src)
|
nuclear@27
|
48 {
|
nuclear@27
|
49 int i, j;
|
nuclear@27
|
50 for(i=0; i<3; i++) {
|
nuclear@27
|
51 for(j=0; j<3; j++) {
|
nuclear@27
|
52 dest[i][j] = src[i][j];
|
nuclear@27
|
53 }
|
nuclear@27
|
54 }
|
nuclear@27
|
55 }
|
nuclear@27
|
56
|
nuclear@27
|
57 void m4_set_translation(mat4_t m, scalar_t x, scalar_t y, scalar_t z)
|
nuclear@27
|
58 {
|
nuclear@27
|
59 m4_identity(m);
|
nuclear@27
|
60 m[0][3] = x;
|
nuclear@27
|
61 m[1][3] = y;
|
nuclear@27
|
62 m[2][3] = z;
|
nuclear@27
|
63 }
|
nuclear@27
|
64
|
nuclear@27
|
65 void m4_translate(mat4_t m, scalar_t x, scalar_t y, scalar_t z)
|
nuclear@27
|
66 {
|
nuclear@27
|
67 mat4_t tm;
|
nuclear@27
|
68 m4_set_translation(tm, x, y, z);
|
nuclear@27
|
69 m4_mult(m, m, tm);
|
nuclear@27
|
70 }
|
nuclear@27
|
71
|
nuclear@27
|
72 void m4_rotate(mat4_t m, scalar_t x, scalar_t y, scalar_t z)
|
nuclear@27
|
73 {
|
nuclear@27
|
74 m4_rotate_x(m, x);
|
nuclear@27
|
75 m4_rotate_y(m, y);
|
nuclear@27
|
76 m4_rotate_z(m, z);
|
nuclear@27
|
77 }
|
nuclear@27
|
78
|
nuclear@27
|
79 void m4_set_rotation_x(mat4_t m, scalar_t angle)
|
nuclear@27
|
80 {
|
nuclear@27
|
81 m4_identity(m);
|
nuclear@27
|
82 m[1][1] = cos(angle); m[1][2] = -sin(angle);
|
nuclear@27
|
83 m[2][1] = sin(angle); m[2][2] = cos(angle);
|
nuclear@27
|
84 }
|
nuclear@27
|
85
|
nuclear@27
|
86 void m4_rotate_x(mat4_t m, scalar_t angle)
|
nuclear@27
|
87 {
|
nuclear@27
|
88 mat4_t rm;
|
nuclear@27
|
89 m4_set_rotation_x(m, angle);
|
nuclear@27
|
90 m4_mult(m, m, rm);
|
nuclear@27
|
91 }
|
nuclear@27
|
92
|
nuclear@27
|
93 void m4_set_rotation_y(mat4_t m, scalar_t angle)
|
nuclear@27
|
94 {
|
nuclear@27
|
95 m4_identity(m);
|
nuclear@27
|
96 m[0][0] = cos(angle); m[0][2] = sin(angle);
|
nuclear@27
|
97 m[2][0] = -sin(angle); m[2][2] = cos(angle);
|
nuclear@27
|
98 }
|
nuclear@27
|
99
|
nuclear@27
|
100 void m4_rotate_y(mat4_t m, scalar_t angle)
|
nuclear@27
|
101 {
|
nuclear@27
|
102 mat4_t rm;
|
nuclear@27
|
103 m4_set_rotation_y(rm, angle);
|
nuclear@27
|
104 m4_mult(m, m, rm);
|
nuclear@27
|
105 }
|
nuclear@27
|
106
|
nuclear@27
|
107 void m4_set_rotation_z(mat4_t m, scalar_t angle)
|
nuclear@27
|
108 {
|
nuclear@27
|
109 m4_identity(m);
|
nuclear@27
|
110 m[0][0] = cos(angle); m[0][1] = -sin(angle);
|
nuclear@27
|
111 m[1][0] = sin(angle); m[1][1] = cos(angle);
|
nuclear@27
|
112 }
|
nuclear@27
|
113
|
nuclear@27
|
114 void m4_rotate_z(mat4_t m, scalar_t angle)
|
nuclear@27
|
115 {
|
nuclear@27
|
116 mat4_t rm;
|
nuclear@27
|
117 m4_set_rotation_z(rm, angle);
|
nuclear@27
|
118 m4_mult(m, m, rm);
|
nuclear@27
|
119 }
|
nuclear@27
|
120
|
nuclear@27
|
121 void m4_set_rotation_axis(mat4_t m, scalar_t angle, scalar_t x, scalar_t y, scalar_t z)
|
nuclear@27
|
122 {
|
nuclear@27
|
123 scalar_t sina = sin(angle);
|
nuclear@27
|
124 scalar_t cosa = cos(angle);
|
nuclear@27
|
125 scalar_t one_minus_cosa = 1.0 - cosa;
|
nuclear@27
|
126 scalar_t nxsq = x * x;
|
nuclear@27
|
127 scalar_t nysq = y * y;
|
nuclear@27
|
128 scalar_t nzsq = z * z;
|
nuclear@27
|
129
|
nuclear@27
|
130 m[0][0] = nxsq + (1.0 - nxsq) * cosa;
|
nuclear@27
|
131 m[0][1] = x * y * one_minus_cosa - z * sina;
|
nuclear@27
|
132 m[0][2] = x * z * one_minus_cosa + y * sina;
|
nuclear@27
|
133 m[1][0] = x * y * one_minus_cosa + z * sina;
|
nuclear@27
|
134 m[1][1] = nysq + (1.0 - nysq) * cosa;
|
nuclear@27
|
135 m[1][2] = y * z * one_minus_cosa - x * sina;
|
nuclear@27
|
136 m[2][0] = x * z * one_minus_cosa - y * sina;
|
nuclear@27
|
137 m[2][1] = y * z * one_minus_cosa + x * sina;
|
nuclear@27
|
138 m[2][2] = nzsq + (1.0 - nzsq) * cosa;
|
nuclear@27
|
139
|
nuclear@27
|
140 /* the rest are identity */
|
nuclear@27
|
141 m[3][0] = m[3][1] = m[3][2] = m[0][3] = m[1][3] = m[2][3] = 0.0;
|
nuclear@27
|
142 m[3][3] = 1.0;
|
nuclear@27
|
143 }
|
nuclear@27
|
144
|
nuclear@27
|
145 void m4_rotate_axis(mat4_t m, scalar_t angle, scalar_t x, scalar_t y, scalar_t z)
|
nuclear@27
|
146 {
|
nuclear@27
|
147 mat4_t xform;
|
nuclear@27
|
148 m4_set_rotation_axis(xform, angle, x, y, z);
|
nuclear@27
|
149 m4_mult(m, m, xform);
|
nuclear@27
|
150 }
|
nuclear@27
|
151
|
nuclear@27
|
152 void m4_rotate_quat(mat4_t m, quat_t q)
|
nuclear@27
|
153 {
|
nuclear@27
|
154 mat4_t rm;
|
nuclear@27
|
155 quat_to_mat4(rm, q);
|
nuclear@27
|
156 m4_mult(m, m, rm);
|
nuclear@27
|
157 }
|
nuclear@27
|
158
|
nuclear@27
|
159 void m4_scale(mat4_t m, scalar_t x, scalar_t y, scalar_t z)
|
nuclear@27
|
160 {
|
nuclear@27
|
161 mat4_t sm;
|
nuclear@27
|
162 m4_identity(sm);
|
nuclear@27
|
163 sm[0][0] = x;
|
nuclear@27
|
164 sm[1][1] = y;
|
nuclear@27
|
165 sm[2][2] = z;
|
nuclear@27
|
166 m4_mult(m, m, sm);
|
nuclear@27
|
167 }
|
nuclear@27
|
168
|
nuclear@27
|
169 void m4_transpose(mat4_t res, mat4_t m)
|
nuclear@27
|
170 {
|
nuclear@27
|
171 int i, j;
|
nuclear@27
|
172 mat4_t tmp;
|
nuclear@27
|
173 m4_copy(tmp, m);
|
nuclear@27
|
174
|
nuclear@27
|
175 for(i=0; i<4; i++) {
|
nuclear@27
|
176 for(j=0; j<4; j++) {
|
nuclear@27
|
177 res[i][j] = tmp[j][i];
|
nuclear@27
|
178 }
|
nuclear@27
|
179 }
|
nuclear@27
|
180 }
|
nuclear@27
|
181
|
nuclear@27
|
182 scalar_t m4_determinant(mat4_t m)
|
nuclear@27
|
183 {
|
nuclear@27
|
184 scalar_t det11 = (m[1][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
|
nuclear@27
|
185 (m[1][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
|
nuclear@27
|
186 (m[1][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
|
nuclear@27
|
187
|
nuclear@27
|
188 scalar_t det12 = (m[1][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
|
nuclear@27
|
189 (m[1][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
|
nuclear@27
|
190 (m[1][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
|
nuclear@27
|
191
|
nuclear@27
|
192 scalar_t det13 = (m[1][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
|
nuclear@27
|
193 (m[1][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
|
nuclear@27
|
194 (m[1][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
|
nuclear@27
|
195
|
nuclear@27
|
196 scalar_t det14 = (m[1][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
|
nuclear@27
|
197 (m[1][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
|
nuclear@27
|
198 (m[1][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
|
nuclear@27
|
199
|
nuclear@27
|
200 return m[0][0] * det11 - m[0][1] * det12 + m[0][2] * det13 - m[0][3] * det14;
|
nuclear@27
|
201 }
|
nuclear@27
|
202
|
nuclear@27
|
203 void m4_adjoint(mat4_t res, mat4_t m)
|
nuclear@27
|
204 {
|
nuclear@27
|
205 int i, j;
|
nuclear@27
|
206 mat4_t coef;
|
nuclear@27
|
207
|
nuclear@27
|
208 coef[0][0] = (m[1][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
|
nuclear@27
|
209 (m[1][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
|
nuclear@27
|
210 (m[1][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
|
nuclear@27
|
211 coef[0][1] = (m[1][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
|
nuclear@27
|
212 (m[1][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
|
nuclear@27
|
213 (m[1][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
|
nuclear@27
|
214 coef[0][2] = (m[1][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
|
nuclear@27
|
215 (m[1][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
|
nuclear@27
|
216 (m[1][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
|
nuclear@27
|
217 coef[0][3] = (m[1][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
|
nuclear@27
|
218 (m[1][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
|
nuclear@27
|
219 (m[1][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
|
nuclear@27
|
220
|
nuclear@27
|
221 coef[1][0] = (m[0][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
|
nuclear@27
|
222 (m[0][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
|
nuclear@27
|
223 (m[0][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
|
nuclear@27
|
224 coef[1][1] = (m[0][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
|
nuclear@27
|
225 (m[0][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
|
nuclear@27
|
226 (m[0][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
|
nuclear@27
|
227 coef[1][2] = (m[0][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
|
nuclear@27
|
228 (m[0][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
|
nuclear@27
|
229 (m[0][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
|
nuclear@27
|
230 coef[1][3] = (m[0][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
|
nuclear@27
|
231 (m[0][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
|
nuclear@27
|
232 (m[0][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
|
nuclear@27
|
233
|
nuclear@27
|
234 coef[2][0] = (m[0][1] * (m[1][2] * m[3][3] - m[3][2] * m[1][3])) -
|
nuclear@27
|
235 (m[0][2] * (m[1][1] * m[3][3] - m[3][1] * m[1][3])) +
|
nuclear@27
|
236 (m[0][3] * (m[1][1] * m[3][2] - m[3][1] * m[1][2]));
|
nuclear@27
|
237 coef[2][1] = (m[0][0] * (m[1][2] * m[3][3] - m[3][2] * m[1][3])) -
|
nuclear@27
|
238 (m[0][2] * (m[1][0] * m[3][3] - m[3][0] * m[1][3])) +
|
nuclear@27
|
239 (m[0][3] * (m[1][0] * m[3][2] - m[3][0] * m[1][2]));
|
nuclear@27
|
240 coef[2][2] = (m[0][0] * (m[1][1] * m[3][3] - m[3][1] * m[1][3])) -
|
nuclear@27
|
241 (m[0][1] * (m[1][0] * m[3][3] - m[3][0] * m[1][3])) +
|
nuclear@27
|
242 (m[0][3] * (m[1][0] * m[3][1] - m[3][0] * m[1][1]));
|
nuclear@27
|
243 coef[2][3] = (m[0][0] * (m[1][1] * m[3][2] - m[3][1] * m[1][2])) -
|
nuclear@27
|
244 (m[0][1] * (m[1][0] * m[3][2] - m[3][0] * m[1][2])) +
|
nuclear@27
|
245 (m[0][2] * (m[1][0] * m[3][1] - m[3][0] * m[1][1]));
|
nuclear@27
|
246
|
nuclear@27
|
247 coef[3][0] = (m[0][1] * (m[1][2] * m[2][3] - m[2][2] * m[1][3])) -
|
nuclear@27
|
248 (m[0][2] * (m[1][1] * m[2][3] - m[2][1] * m[1][3])) +
|
nuclear@27
|
249 (m[0][3] * (m[1][1] * m[2][2] - m[2][1] * m[1][2]));
|
nuclear@27
|
250 coef[3][1] = (m[0][0] * (m[1][2] * m[2][3] - m[2][2] * m[1][3])) -
|
nuclear@27
|
251 (m[0][2] * (m[1][0] * m[2][3] - m[2][0] * m[1][3])) +
|
nuclear@27
|
252 (m[0][3] * (m[1][0] * m[2][2] - m[2][0] * m[1][2]));
|
nuclear@27
|
253 coef[3][2] = (m[0][0] * (m[1][1] * m[2][3] - m[2][1] * m[1][3])) -
|
nuclear@27
|
254 (m[0][1] * (m[1][0] * m[2][3] - m[2][0] * m[1][3])) +
|
nuclear@27
|
255 (m[0][3] * (m[1][0] * m[2][1] - m[2][0] * m[1][1]));
|
nuclear@27
|
256 coef[3][3] = (m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2])) -
|
nuclear@27
|
257 (m[0][1] * (m[1][0] * m[2][2] - m[2][0] * m[1][2])) +
|
nuclear@27
|
258 (m[0][2] * (m[1][0] * m[2][1] - m[2][0] * m[1][1]));
|
nuclear@27
|
259
|
nuclear@27
|
260 m4_transpose(res, coef);
|
nuclear@27
|
261
|
nuclear@27
|
262 for(i=0; i<4; i++) {
|
nuclear@27
|
263 for(j=0; j<4; j++) {
|
nuclear@27
|
264 res[i][j] = j % 2 ? -res[i][j] : res[i][j];
|
nuclear@27
|
265 if(i % 2) res[i][j] = -res[i][j];
|
nuclear@27
|
266 }
|
nuclear@27
|
267 }
|
nuclear@27
|
268 }
|
nuclear@27
|
269
|
nuclear@27
|
270 void m4_inverse(mat4_t res, mat4_t m)
|
nuclear@27
|
271 {
|
nuclear@27
|
272 int i, j;
|
nuclear@27
|
273 mat4_t adj;
|
nuclear@27
|
274 scalar_t det;
|
nuclear@27
|
275
|
nuclear@27
|
276 m4_adjoint(adj, m);
|
nuclear@27
|
277 det = m4_determinant(m);
|
nuclear@27
|
278
|
nuclear@27
|
279 for(i=0; i<4; i++) {
|
nuclear@27
|
280 for(j=0; j<4; j++) {
|
nuclear@27
|
281 res[i][j] = adj[i][j] / det;
|
nuclear@27
|
282 }
|
nuclear@27
|
283 }
|
nuclear@27
|
284 }
|
nuclear@27
|
285
|
nuclear@27
|
286 void m4_print(FILE *fp, mat4_t m)
|
nuclear@27
|
287 {
|
nuclear@27
|
288 int i;
|
nuclear@27
|
289 for(i=0; i<4; i++) {
|
nuclear@27
|
290 fprintf(fp, "[ %12.5f %12.5f %12.5f %12.5f ]\n", (float)m[i][0], (float)m[i][1], (float)m[i][2], (float)m[i][3]);
|
nuclear@27
|
291 }
|
nuclear@27
|
292 }
|