-
Notifications
You must be signed in to change notification settings - Fork 2
/
matrix.cxx
379 lines (358 loc) · 9.56 KB
/
matrix.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//==============================================================================
// File: matrix.cxx
//
// Copyright (c) 2017, Phil Harvey, Queen's University
//==============================================================================
#include <math.h>
#include <string.h>
#include "matrix.h"
#define NEAR_ZERO 1.0e-20
#define MNUM 3
/*---------------------------------------------------------------------------
** Get the length of a vector
*/
float vectorLen(float x, float y, float z)
{
return(sqrt(x*x+y*y+z*z));
}
int unitVector(Vector3 v)
{
float r = vectorLen(v[0],v[1],v[2]);
if (r) {
v[0]/=r;
v[1]/=r;
v[2]/=r;
return(0);
} else {
return(1);
}
}
/*
** Get the rotation matrix for the given angles:
** theta - angle of rotation axis from the x axis measured ccw in the x-y plane
** phi - angle of rotation axis from the z axis
** alpha - angle of cw rotation about the axis of rotation
*/
void getRotMatrix(Matrix3 rot, float theta, float phi, float alpha)
{
float st,sp,sa;
float ct,cp,ca;
st = sin(theta); sp = sin(phi); sa = sin(alpha);
ct = cos(theta); cp = cos(phi); ca = cos(alpha);
rot[0][0] = ((ct*cp*ca+st*sa)*cp + ct*sp*sp)*ct - (ct*cp*sa-st*ca)*st;
rot[0][1] = ((ct*cp*ca+st*sa)*cp + ct*sp*sp)*st + (ct*cp*sa-st*ca)*ct;
rot[0][2] = -(ct*cp*ca+st*sa)*sp + ct*sp*cp;
rot[1][0] = ((st*cp*ca-ct*sa)*cp + st*sp*sp)*ct - (st*cp*sa+ct*ca)*st;
rot[1][1] = ((st*cp*ca-ct*sa)*cp + st*sp*sp)*st + (st*cp*sa+ct*ca)*ct;
rot[1][2] = -(st*cp*ca-ct*sa)*sp + st*sp*cp;
rot[2][0] = (-sp*ca*cp+cp*sp)*ct + sp*sa*st;
rot[2][1] = (-sp*ca*cp+cp*sp)*st - sp*sa*ct;
rot[2][2] = sp*ca*sp+cp*cp;
}
/*
** Construct 3-D rotation matrix for Euler angles (alpha,beta,gamma)
** using standard y-axis convention.
*/
void get3DMatrix(Matrix3 rot, float alpha, float beta, float gamma)
{
float ca = cos(alpha);
float sa = sin(alpha);
float cb = cos(beta);
float sb = sin(beta);
float cg = cos(gamma);
float sg = sin(gamma);
/*
** alpha) about z axis, beta) about new x axis, gamma) about new z axis
rot[0][0] = cg*ca - cb*sa*sg;
rot[0][1] = cg*sa + cb*ca*sg;
rot[0][2] = sg*sb;
rot[1][0] = -sg*ca - cb*sa*cg;
rot[1][1] = -sg*sa + cb*ca*cg;
rot[1][2] = cg*sb;
rot[2][0] = sb*sa;
rot[2][1] = -sb*ca;
rot[2][2] = cb;
*/
/*
** alpha) about z axis, beta) about new y axis, gamma) about new z axis
*/
rot[0][0] = cg*cb*ca - sg*sa;
rot[0][1] = cg*cb*sa + sg*ca;
rot[0][2] = -cg*sb;
rot[1][0] = -sg*cb*ca - cg*sa;
rot[1][1] = -sg*cb*sa + cg*ca;
rot[1][2] = sg*sb;
rot[2][0] = sb*ca;
rot[2][1] = sb*sa;
rot[2][2] = cb;
}
/*
** Construct a 3-D rotation matrix for rotations about the
** Y, X, then Z axes
*/
void getRotMatrixYXZ(Matrix3 rot, double alpha, double beta, double gamma)
{
double ca = cos(alpha);
double sa = sin(alpha);
double cb = cos(beta);
double sb = sin(beta);
double cg = cos(gamma);
double sg = sin(gamma);
/*
** alpha) about y axis, beta) about new x axis, gamma) about new z axis
*/
rot[0][0] = ca*cg - sa*sb*sg;
rot[0][1] = -cb*sg;
rot[0][2] = sa*cg + ca*sb*sg;
rot[1][0] = ca*sg + sa*sb*cg;
rot[1][1] = cb*cg;
rot[1][2] = sa*sg - ca*sb*cg;
rot[2][0] = -sa*cb;
rot[2][1] = sb;
rot[2][2] = ca*cb;
}
/*
** Multiply a vector by a matrix
*/
void vectorMult(Matrix3 m, Vector3 in, Vector3 out)
{
out[0] = m[0][0]*in[0] + m[0][1]*in[1] + m[0][2]*in[2];
out[1] = m[1][0]*in[0] + m[1][1]*in[1] + m[1][2]*in[2];
out[2] = m[2][0]*in[0] + m[2][1]*in[1] + m[2][2]*in[2];
}
/*
** Multiply a destination matrix (d) by a source matrix (s)
*/
void matrixMult(Matrix3 d, Matrix3 s)
{
int i,j;
Matrix3 t;
memcpy(t,d,sizeof(Matrix3));
for (i=0; i<3; ++i) {
for (j=0; j<3; ++j) {
d[j][i] = s[j][0]*t[0][i] + s[j][1]*t[1][i] + s[j][2]*t[2][i];
}
}
}
/*
** Initialize a matrix to the identity matrix
*/
void matrixIdent(Matrix3 m)
{
memset(m,0,sizeof(Matrix3));
m[0][0] = m[1][1] = m[2][2] = 1;
}
/*.........................................................................
** Get rotation matrix to align (0,0,1) with given vector
** (rotation about y (t) then about x (p), where
** ct=p[2]/r, st=p[1]/r, cp=r, sp=p[0])
**
** Note: input vector must have unit length!
*/
void getRotAlign(Vector3 p, Matrix3 out)
{
float r = sqrt(1 - p[0]*p[0]);
if (!r) {
out[0][0] = 0;
out[0][1] = 0;
out[0][2] = 1;
out[1][0] = 0;
out[1][1] = 1;
out[1][2] = 0;
out[2][0] = -1;
out[2][1] = 0;
out[2][2] = 0;
} else {
out[0][0] = r;
out[0][1] = 0;
out[0][2] = p[0];
out[1][0] = -p[0] * p[1] / r;
out[1][1] = p[2] / r;
out[1][2] = p[1];
out[2][0] = -p[0] * p[2] / r;
out[2][1] = -p[1] / r;
out[2][2] = p[2];
}
}
/* input vector doesn't need to be unit length here */
void getRotAlignScaled(Vector3 p, Matrix3 out)
{
float len = vectorLen(p[0], p[1], p[2]);
float fac = 1.0 / len;
float r = sqrt(1 - p[0]*p[0]*fac*fac);
if (!r) {
out[0][0] = 0;
out[0][1] = 0;
out[0][2] = len;
out[1][0] = 0;
out[1][1] = len;
out[1][2] = 0;
out[2][0] = -len;
out[2][1] = 0;
out[2][2] = 0;
} else {
out[0][0] = len * r;
out[0][1] = 0;
out[0][2] = p[0];
out[1][0] = -p[0] * p[1] * fac / r;
out[1][1] = p[2] / r;
out[1][2] = p[1];
out[2][0] = -p[0] * p[2] * fac / r;
out[2][1] = -p[1] / r;
out[2][2] = p[2];
}
}
/*
** Get rotation matrix to align given vector with (0,0,1)
*/
void getRotAlignInv(Vector3 p, Matrix3 out)
{
float r = sqrt(1 - p[0]*p[0]);
if (!r) {
out[0][0] = 0;
out[0][1] = 0;
out[0][2] = -1;
out[1][0] = 0;
out[1][1] = 1;
out[1][2] = 0;
out[2][0] = 1;
out[2][1] = 0;
out[2][2] = 0;
} else {
out[0][0] = r;
out[0][1] = -p[0] * p[1] / r;
out[0][2] = -p[0] * p[2] / r;
out[1][0] = 0;
out[1][1] = p[2] / r;
out[1][2] = -p[1] / r;
out[2][0] = p[0];
out[2][1] = p[1];
out[2][2] = p[2];
}
}
/*
** Find the transpose of a matrix (this inverts a rotation matrix)
*/
void matrixTranspose(Matrix3 in, Matrix3 out)
{
int i,j;
for (j=0; j<3; ++j) {
for (i=0; i<3; ++i) {
out[j][i] = in[i][j];
}
}
}
/*
** Set element (row,col) of matrix m to zero by combining this row
** with an appropriately scaled multiple of the source row
*/
void combineVector4(float m[3][4], int row, int col, int src)
{
int i;
float f, d = m[src][col];
if (!d) return;
f = m[row][col] / d;
for (i=0; i<4; ++i) m[row][i] -= m[src][i] * f;
}
/*
** Solve a matrix equation by combining the rows
** to eliminate off-diagonal elements.
*/
void matrixSolve(Matrix3 m, Vector3 in, Vector3 out)
{
int i,j,k;
float tmp[3][4], t;
for (i=0; i<3; ++i) {
memcpy(tmp[i],m[i],3*sizeof(float));
tmp[i][3] = in[i];
}
for (i=0; i<6; ++i) {
j = i%3;
k = 2-i/2;
combineVector4(tmp, j, k, k);
if (i>=3) {
t = tmp[j][j];
if (t) out[j] = tmp[j][3] / t;
else out[j] = tmp[j][3];
}
}
}
void lubksb(float a[MNUM][MNUM],int indx[MNUM],float b[MNUM])
{
int i,ii=-1,ip,j;
float sum;
for (i=0; i<MNUM; ++i) {
ip = indx[i];
sum = b[ip];
b[ip] = b[i];
if (ii>=0) for (j=ii; j<i; ++j) sum -= a[i][j]*b[j];
else if (sum) ii = i;
b[i] = sum;
}
for (i=MNUM-1; i>=0; --i) {
sum = b[i];
for (j=i+1; j<MNUM; ++j) sum -= a[i][j]*b[j];
b[i] = sum/a[i][i];
}
}
void ludcmp(float a[MNUM][MNUM],int indx[MNUM], float *d)
{
int i,imax=0,j,k;
float big,dum,sum,temp;
float vv[MNUM];
*d = 1.0;
for (i=0; i<MNUM; ++i) {
big = 0.0;
for (j=0; j<MNUM; ++j) if ((temp=fabs(a[i][j])) > big) big=temp;
if (big == 0.0) return; /* ERROR: singular matrix!!! */
vv[i] = 1.0/big;
}
for (j=0; j<MNUM; ++j) {
for (i=0; i<j; ++i) {
sum = a[i][j];
for (k=0; k<i; ++k) sum -= a[i][k]*a[k][j];
a[i][j] = sum;
}
big = 0.0;
for (i=j; i<MNUM; ++i) {
sum = a[i][j];
for (k=0; k<j; ++k)
sum -= a[i][k]*a[k][j];
a[i][j] = sum;
if ( (dum=vv[i]*fabs(sum)) >= big) {
big = dum;
imax = i;
}
}
if (j != imax) {
for (k=0; k<MNUM; ++k) {
dum = a[imax][k];
a[imax][k] = a[j][k];
a[j][k] = dum;
}
*d = -(*d);
vv[imax] = vv[j];
}
indx[j] = imax;
if (a[j][j] == 0.0) a[j][j] = NEAR_ZERO;
if (j != MNUM-1) {
dum = 1.0/a[j][j];
for (i=j+1; i<MNUM; ++i) a[i][j] *= dum;
}
}
}
void matrixInvert(Matrix3 in, Matrix3 out)
{
int i,j,indx[MNUM];
float d;
Matrix3 tmp;
Vector3 col;
memcpy(tmp,in,sizeof(Matrix3));
ludcmp(tmp,indx,&d);
for (j=0; j<MNUM; ++j) {
for (i=0; i<MNUM; ++i) col[i] = 0.0;
col[j] = 1.0;
lubksb(tmp,indx,col);
for (i=0; i<MNUM; ++i) out[i][j] = col[i];
}
}