Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Decompose.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2013 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "precompiled.h"
19 
20 #ifdef _MSC_VER
21 # pragma warning(disable: 4244 4305 4127 4701)
22 #endif
23 
24 /**** Decompose.c ****/
25 /* Ken Shoemake, 1993 */
26 #include <math.h>
27 #include "Decompose.h"
28 
29 /******* Matrix Preliminaries *******/
30 
31 /** Fill out 3x3 matrix to 4x4 **/
32 #define mat_pad(A) (A[W][X]=A[X][W]=A[W][Y]=A[Y][W]=A[W][Z]=A[Z][W]=0,A[W][W]=1)
33 
34 /** Copy nxn matrix A to C using "gets" for assignment **/
35 #define mat_copy(C,gets,A,n) {int i,j; for(i=0;i<n;i++) for(j=0;j<n;j++)\
36  C[i][j] gets (A[i][j]);}
37 
38 /** Copy transpose of nxn matrix A to C using "gets" for assignment **/
39 #define mat_tpose(AT,gets,A,n) {int i,j; for(i=0;i<n;i++) for(j=0;j<n;j++)\
40  AT[i][j] gets (A[j][i]);}
41 
42 /** Assign nxn matrix C the element-wise combination of A and B using "op" **/
43 #define mat_binop(C,gets,A,op,B,n) {int i,j; for(i=0;i<n;i++) for(j=0;j<n;j++)\
44  C[i][j] gets (A[i][j]) op (B[i][j]);}
45 
46 /** Multiply the upper left 3x3 parts of A and B to get AB **/
48 {
49  int i, j;
50  for (i=0; i<3; i++) for (j=0; j<3; j++)
51  AB[i][j] = A[i][0]*B[0][j] + A[i][1]*B[1][j] + A[i][2]*B[2][j];
52 }
53 
54 /** Return dot product of length 3 vectors va and vb **/
55 float vdot(float *va, float *vb)
56 {
57  return (va[0]*vb[0] + va[1]*vb[1] + va[2]*vb[2]);
58 }
59 
60 /** Set v to cross product of length 3 vectors va and vb **/
61 void vcross(float *va, float *vb, float *v)
62 {
63  v[0] = va[1]*vb[2] - va[2]*vb[1];
64  v[1] = va[2]*vb[0] - va[0]*vb[2];
65  v[2] = va[0]*vb[1] - va[1]*vb[0];
66 }
67 
68 /** Set MadjT to transpose of inverse of M times determinant of M **/
70 {
71  vcross(M[1], M[2], MadjT[0]);
72  vcross(M[2], M[0], MadjT[1]);
73  vcross(M[0], M[1], MadjT[2]);
74 }
75 
76 /******* Quaternion Preliminaries *******/
77 
78 /* Construct a (possibly non-unit) quaternion from real components. */
79 Quat Qt_(float x, float y, float z, float w)
80 {
81  Quat qq;
82  qq.x = x; qq.y = y; qq.z = z; qq.w = w;
83  return (qq);
84 }
85 
86 /* Return conjugate of quaternion. */
88 {
89  Quat qq;
90  qq.x = -q.x; qq.y = -q.y; qq.z = -q.z; qq.w = q.w;
91  return (qq);
92 }
93 
94 /* Return quaternion product qL * qR. Note: order is important!
95  * To combine rotations, use the product Mul(qSecond, qFirst),
96  * which gives the effect of rotating by qFirst then qSecond. */
98 {
99  Quat qq;
100  qq.w = qL.w*qR.w - qL.x*qR.x - qL.y*qR.y - qL.z*qR.z;
101  qq.x = qL.w*qR.x + qL.x*qR.w + qL.y*qR.z - qL.z*qR.y;
102  qq.y = qL.w*qR.y + qL.y*qR.w + qL.z*qR.x - qL.x*qR.z;
103  qq.z = qL.w*qR.z + qL.z*qR.w + qL.x*qR.y - qL.y*qR.x;
104  return (qq);
105 }
106 
107 /* Return product of quaternion q by scalar w. */
108 Quat Qt_Scale(Quat q, float w)
109 {
110  Quat qq;
111  qq.w = q.w*w; qq.x = q.x*w; qq.y = q.y*w; qq.z = q.z*w;
112  return (qq);
113 }
114 
115 /* Construct a unit quaternion from rotation matrix. Assumes matrix is
116  * used to multiply column vector on the left: vnew = mat vold. Works
117  * correctly for right-handed coordinate system and right-handed rotations.
118  * Translation and perspective components ignored. */
120 {
121  /* This algorithm avoids near-zero divides by looking for a large component
122  * - first w, then x, y, or z. When the trace is greater than zero,
123  * |w| is greater than 1/2, which is as small as a largest component can be.
124  * Otherwise, the largest diagonal entry corresponds to the largest of |x|,
125  * |y|, or |z|, one of which must be larger than |w|, and at least 1/2. */
126  Quat qu;
127  register double tr, s;
128 
129  tr = mat[X][X] + mat[Y][Y]+ mat[Z][Z];
130  if (tr >= 0.0) {
131  s = sqrt(tr + mat[W][W]);
132  qu.w = s*0.5;
133  s = 0.5 / s;
134  qu.x = (mat[Z][Y] - mat[Y][Z]) * s;
135  qu.y = (mat[X][Z] - mat[Z][X]) * s;
136  qu.z = (mat[Y][X] - mat[X][Y]) * s;
137  } else {
138  int h = X;
139  if (mat[Y][Y] > mat[X][X]) h = Y;
140  if (mat[Z][Z] > mat[h][h]) h = Z;
141  switch (h) {
142 #define caseMacro(i,j,k,I,J,K) \
143  case I:\
144  s = sqrt( (mat[I][I] - (mat[J][J]+mat[K][K])) + mat[W][W] );\
145  qu.i = s*0.5;\
146  s = 0.5 / s;\
147  qu.j = (mat[I][J] + mat[J][I]) * s;\
148  qu.k = (mat[K][I] + mat[I][K]) * s;\
149  qu.w = (mat[K][J] - mat[J][K]) * s;\
150  break
151  caseMacro(x,y,z,X,Y,Z);
152  caseMacro(y,z,x,Y,Z,X);
153  caseMacro(z,x,y,Z,X,Y);
154  }
155  }
156  if (mat[W][W] != 1.0) qu = Qt_Scale(qu, 1/sqrt(mat[W][W]));
157  return (qu);
158 }
159 /******* Decomp Auxiliaries *******/
160 
161 static HMatrix mat_id = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
162 
163 /** Compute either the 1 or infinity norm of M, depending on tpose **/
164 float mat_norm(HMatrix M, int tpose)
165 {
166  int i;
167  float sum, max;
168  max = 0.0;
169  for (i=0; i<3; i++) {
170  if (tpose) sum = fabs(M[0][i])+fabs(M[1][i])+fabs(M[2][i]);
171  else sum = fabs(M[i][0])+fabs(M[i][1])+fabs(M[i][2]);
172  if (max<sum) max = sum;
173  }
174  return max;
175 }
176 
177 float norm_inf(HMatrix M) {return mat_norm(M, 0);}
178 float norm_one(HMatrix M) {return mat_norm(M, 1);}
179 
180 /** Return index of column of M containing maximum abs entry, or -1 if M=0 **/
182 {
183  float abs, max;
184  int i, j, col;
185  max = 0.0; col = -1;
186  for (i=0; i<3; i++) for (j=0; j<3; j++) {
187  abs = M[i][j]; if (abs<0.0) abs = -abs;
188  if (abs>max) {max = abs; col = j;}
189  }
190  return col;
191 }
192 
193 /** Setup u for Household reflection to zero all v components but first **/
194 void make_reflector(float *v, float *u)
195 {
196  float s = sqrt(vdot(v, v));
197  u[0] = v[0]; u[1] = v[1];
198  u[2] = v[2] + ((v[2]<0.0) ? -s : s);
199  s = sqrt(2.0/vdot(u, u));
200  u[0] = u[0]*s; u[1] = u[1]*s; u[2] = u[2]*s;
201 }
202 
203 /** Apply Householder reflection represented by u to column vectors of M **/
204 void reflect_cols(HMatrix M, float *u)
205 {
206  int i, j;
207  for (i=0; i<3; i++) {
208  float s = u[0]*M[0][i] + u[1]*M[1][i] + u[2]*M[2][i];
209  for (j=0; j<3; j++) M[j][i] -= u[j]*s;
210  }
211 }
212 /** Apply Householder reflection represented by u to row vectors of M **/
213 void reflect_rows(HMatrix M, float *u)
214 {
215  int i, j;
216  for (i=0; i<3; i++) {
217  float s = vdot(u, M[i]);
218  for (j=0; j<3; j++) M[i][j] -= u[j]*s;
219  }
220 }
221 
222 /** Find orthogonal factor Q of rank 1 (or less) M **/
224 {
225  float v1[3], v2[3], s;
226  int col;
227  mat_copy(Q,=,mat_id,4);
228  /* If rank(M) is 1, we should find a non-zero column in M */
229  col = find_max_col(M);
230  if (col<0) return; /* Rank is 0 */
231  v1[0] = M[0][col]; v1[1] = M[1][col]; v1[2] = M[2][col];
232  make_reflector(v1, v1); reflect_cols(M, v1);
233  v2[0] = M[2][0]; v2[1] = M[2][1]; v2[2] = M[2][2];
234  make_reflector(v2, v2); reflect_rows(M, v2);
235  s = M[2][2];
236  if (s<0.0) Q[2][2] = -1.0;
237  reflect_cols(Q, v1); reflect_rows(Q, v2);
238 }
239 
240 /** Find orthogonal factor Q of rank 2 (or less) M using adjoint transpose **/
241 void do_rank2(HMatrix M, HMatrix MadjT, HMatrix Q)
242 {
243  float v1[3], v2[3];
244  float w, x, y, z, c, s, d;
245  int col;
246  /* If rank(M) is 2, we should find a non-zero column in MadjT */
247  col = find_max_col(MadjT);
248  if (col<0) {do_rank1(M, Q); return;} /* Rank<2 */
249  v1[0] = MadjT[0][col]; v1[1] = MadjT[1][col]; v1[2] = MadjT[2][col];
250  make_reflector(v1, v1); reflect_cols(M, v1);
251  vcross(M[0], M[1], v2);
252  make_reflector(v2, v2); reflect_rows(M, v2);
253  w = M[0][0]; x = M[0][1]; y = M[1][0]; z = M[1][1];
254  if (w*z>x*y) {
255  c = z+w; s = y-x; d = sqrt(c*c+s*s); c = c/d; s = s/d;
256  Q[0][0] = Q[1][1] = c; Q[0][1] = -(Q[1][0] = s);
257  } else {
258  c = z-w; s = y+x; d = sqrt(c*c+s*s); c = c/d; s = s/d;
259  Q[0][0] = -(Q[1][1] = c); Q[0][1] = Q[1][0] = s;
260  }
261  Q[0][2] = Q[2][0] = Q[1][2] = Q[2][1] = 0.0; Q[2][2] = 1.0;
262  reflect_cols(Q, v1); reflect_rows(Q, v2);
263 }
264 
265 
266 /******* Polar Decomposition *******/
267 
268 /* Polar Decomposition of 3x3 matrix in 4x4,
269  * M = QS. See Nicholas Higham and Robert S. Schreiber,
270  * Fast Polar Decomposition of An Arbitrary Matrix,
271  * Technical Report 88-942, October 1988,
272  * Department of Computer Science, Cornell University.
273  */
275 {
276 #define TOL 1.0e-6
277  HMatrix Mk, MadjTk, Ek;
278  float det, M_one, M_inf, MadjT_one, MadjT_inf, E_one, gamma, g1, g2;
279  int i, j;
280  mat_tpose(Mk,=,M,3);
281  M_one = norm_one(Mk); M_inf = norm_inf(Mk);
282  do {
283  adjoint_transpose(Mk, MadjTk);
284  det = vdot(Mk[0], MadjTk[0]);
285  if (det==0.0) {do_rank2(Mk, MadjTk, Mk); break;}
286  MadjT_one = norm_one(MadjTk); MadjT_inf = norm_inf(MadjTk);
287  gamma = sqrt(sqrt((MadjT_one*MadjT_inf)/(M_one*M_inf))/fabs(det));
288  g1 = gamma*0.5;
289  g2 = 0.5/(gamma*det);
290  mat_copy(Ek,=,Mk,3);
291  mat_binop(Mk,=,g1*Mk,+,g2*MadjTk,3);
292  mat_copy(Ek,-=,Mk,3);
293  E_one = norm_one(Ek);
294  M_one = norm_one(Mk); M_inf = norm_inf(Mk);
295  } while (E_one>(M_one*TOL));
296  mat_tpose(Q,=,Mk,3); mat_pad(Q);
297  mat_mult(Mk, M, S); mat_pad(S);
298  for (i=0; i<3; i++) for (j=i; j<3; j++)
299  S[i][j] = S[j][i] = 0.5*(S[i][j]+S[j][i]);
300  return (det);
301 }
302 
303 
304 
305 
306 
307 
308 
309 
310 
311 
312 
313 
314 
315 
316 
317 
318 
319 /******* Spectral Decomposition *******/
320 
321 /* Compute the spectral decomposition of symmetric positive semi-definite S.
322  * Returns rotation in U and scale factors in result, so that if K is a diagonal
323  * matrix of the scale factors, then S = U K (U transpose). Uses Jacobi method.
324  * See Gene H. Golub and Charles F. Van Loan. Matrix Computations. Hopkins 1983.
325  */
327 {
328  HVect kv;
329  double Diag[3],OffD[3]; /* OffD is off-diag (by omitted index) */
330  double g,h,fabsh,fabsOffDi,t,theta,c,s,tau,ta,OffDq,a,b;
331  static char nxt[] = {Y,Z,X};
332  int sweep, i, j;
333  mat_copy(U,=,mat_id,4);
334  Diag[X] = S[X][X]; Diag[Y] = S[Y][Y]; Diag[Z] = S[Z][Z];
335  OffD[X] = S[Y][Z]; OffD[Y] = S[Z][X]; OffD[Z] = S[X][Y];
336  for (sweep=20; sweep>0; sweep--) {
337  float sm = fabs(OffD[X])+fabs(OffD[Y])+fabs(OffD[Z]);
338  if (sm==0.0) break;
339  for (i=Z; i>=X; i--) {
340  int p = nxt[i]; int q = nxt[p];
341  fabsOffDi = fabs(OffD[i]);
342  g = 100.0*fabsOffDi;
343  if (fabsOffDi>0.0) {
344  h = Diag[q] - Diag[p];
345  fabsh = fabs(h);
346  if (fabsh+g==fabsh) {
347  t = OffD[i]/h;
348  } else {
349  theta = 0.5*h/OffD[i];
350  t = 1.0/(fabs(theta)+sqrt(theta*theta+1.0));
351  if (theta<0.0) t = -t;
352  }
353  c = 1.0/sqrt(t*t+1.0); s = t*c;
354  tau = s/(c+1.0);
355  ta = t*OffD[i]; OffD[i] = 0.0;
356  Diag[p] -= ta; Diag[q] += ta;
357  OffDq = OffD[q];
358  OffD[q] -= s*(OffD[p] + tau*OffD[q]);
359  OffD[p] += s*(OffDq - tau*OffD[p]);
360  for (j=Z; j>=X; j--) {
361  a = U[j][p]; b = U[j][q];
362  U[j][p] -= s*(b + tau*a);
363  U[j][q] += s*(a - tau*b);
364  }
365  }
366  }
367  }
368  kv.x = Diag[X]; kv.y = Diag[Y]; kv.z = Diag[Z]; kv.w = 1.0;
369  return (kv);
370 }
371 
372 /******* Spectral Axis Adjustment *******/
373 
374 /* Given a unit quaternion, q, and a scale vector, k, find a unit quaternion, p,
375  * which permutes the axes and turns freely in the plane of duplicate scale
376  * factors, such that q p has the largest possible w component, i.e. the
377  * smallest possible angle. Permutes k's components to go with q p instead of q.
378  * See Ken Shoemake and Tom Duff. Matrix Animation and Polar Decomposition.
379  * Proceedings of Graphics Interface 1992. Details on p. 262-263.
380  */
382 {
383 #define SQRTHALF (0.7071067811865475244)
384 #define sgn(n,v) ((n)?-(v):(v))
385 #define swap(a,i,j) {a[3]=a[i]; a[i]=a[j]; a[j]=a[3];}
386 #define cycle(a,p) if (p) {a[3]=a[0]; a[0]=a[1]; a[1]=a[2]; a[2]=a[3];}\
387  else {a[3]=a[2]; a[2]=a[1]; a[1]=a[0]; a[0]=a[3];}
388  Quat p;
389  float ka[4];
390  int i, turn = -1;
391  ka[X] = k->x; ka[Y] = k->y; ka[Z] = k->z;
392  if (ka[X]==ka[Y]) {if (ka[X]==ka[Z]) turn = W; else turn = Z;}
393  else {if (ka[X]==ka[Z]) turn = Y; else if (ka[Y]==ka[Z]) turn = X;}
394  if (turn>=0) {
395  Quat qtoz, qp;
396  unsigned neg[3], win;
397  double mag[3], t;
398  static Quat qxtoz = {0,SQRTHALF,0,SQRTHALF};
399  static Quat qytoz = {SQRTHALF,0,0,SQRTHALF};
400  static Quat qppmm = { 0.5, 0.5,-0.5,-0.5};
401  static Quat qpppp = { 0.5, 0.5, 0.5, 0.5};
402  static Quat qmpmm = {-0.5, 0.5,-0.5,-0.5};
403  static Quat qpppm = { 0.5, 0.5, 0.5,-0.5};
404  static Quat q0001 = { 0.0, 0.0, 0.0, 1.0};
405  static Quat q1000 = { 1.0, 0.0, 0.0, 0.0};
406  switch (turn) {
407  default: return (Qt_Conj(q));
408  case X: q = Qt_Mul(q, qtoz = qxtoz); swap(ka,X,Z) break;
409  case Y: q = Qt_Mul(q, qtoz = qytoz); swap(ka,Y,Z) break;
410  case Z: qtoz = q0001; break;
411  }
412  q = Qt_Conj(q);
413  mag[0] = (double)q.z*q.z+(double)q.w*q.w-0.5;
414  mag[1] = (double)q.x*q.z-(double)q.y*q.w;
415  mag[2] = (double)q.y*q.z+(double)q.x*q.w;
416  for (i=0; i<3; i++) if ((neg[i] = (mag[i]<0.0)) != 0) mag[i] = -mag[i];
417  if (mag[0]>mag[1]) {if (mag[0]>mag[2]) win = 0; else win = 2;}
418  else {if (mag[1]>mag[2]) win = 1; else win = 2;}
419  switch (win) {
420  case 0: if (neg[0]) p = q1000; else p = q0001; break;
421  case 1: if (neg[1]) p = qppmm; else p = qpppp; cycle(ka,0) break;
422  case 2: if (neg[2]) p = qmpmm; else p = qpppm; cycle(ka,1) break;
423  }
424  qp = Qt_Mul(q, p);
425  t = sqrt(mag[win]+0.5);
426  p = Qt_Mul(p, Qt_(0.0,0.0,-qp.z/t,qp.w/t));
427  p = Qt_Mul(qtoz, Qt_Conj(p));
428  } else {
429  float qa[4], pa[4];
430  unsigned lo, hi, neg[4], par = 0;
431  double all, big, two;
432  qa[0] = q.x; qa[1] = q.y; qa[2] = q.z; qa[3] = q.w;
433  for (i=0; i<4; i++) {
434  pa[i] = 0.0;
435  if ((neg[i] = (qa[i]<0.0)) != 0) qa[i] = -qa[i];
436  par ^= neg[i];
437  }
438  /* Find two largest components, indices in hi and lo */
439  if (qa[0]>qa[1]) lo = 0; else lo = 1;
440  if (qa[2]>qa[3]) hi = 2; else hi = 3;
441  if (qa[lo]>qa[hi]) {
442  if (qa[lo^1]>qa[hi]) {hi = lo; lo ^= 1;}
443  else {hi ^= lo; lo ^= hi; hi ^= lo;}
444  } else {if (qa[hi^1]>qa[lo]) lo = hi^1;}
445  all = (qa[0]+qa[1]+qa[2]+qa[3])*0.5;
446  two = (qa[hi]+qa[lo])*SQRTHALF;
447  big = qa[hi];
448  if (all>two) {
449  if (all>big) {/*all*/
450  {int i; for (i=0; i<4; i++) pa[i] = sgn(neg[i], 0.5);}
451  cycle(ka,par)
452  } else {/*big*/ pa[hi] = sgn(neg[hi],1.0);}
453  } else {
454  if (two>big) {/*two*/
455  pa[hi] = sgn(neg[hi],SQRTHALF); pa[lo] = sgn(neg[lo], SQRTHALF);
456  if (lo>hi) {hi ^= lo; lo ^= hi; hi ^= lo;}
457  if (hi==W) {hi = "\001\002\000"[lo]; lo = 3-hi-lo;}
458  swap(ka,hi,lo)
459  } else {/*big*/ pa[hi] = sgn(neg[hi],1.0);}
460  }
461  p.x = -pa[0]; p.y = -pa[1]; p.z = -pa[2]; p.w = pa[3];
462  }
463  k->x = ka[X]; k->y = ka[Y]; k->z = ka[Z];
464  return (p);
465 }
466 
467 
468 
469 
470 
471 
472 
473 
474 
475 
476 
477 /******* Decompose Affine Matrix *******/
478 
479 /* Decompose 4x4 affine matrix A as TFRUK(U transpose), where t contains the
480  * translation components, q contains the rotation R, u contains U, k contains
481  * scale factors, and f contains the sign of the determinant.
482  * Assumes A transforms column vectors in right-handed coordinates.
483  * See Ken Shoemake and Tom Duff. Matrix Animation and Polar Decomposition.
484  * Proceedings of Graphics Interface 1992.
485  */
487 {
488  HMatrix Q, S, U;
489  Quat p;
490  float det;
491  parts->t = Qt_(A[X][W], A[Y][W], A[Z][W], 0);
492  det = polar_decomp(A, Q, S);
493  if (det<0.0) {
494  mat_copy(Q,=,-Q,3);
495  parts->f = -1;
496  } else parts->f = 1;
497  parts->q = Qt_FromMatrix(Q);
498  parts->k = spect_decomp(S, U);
499  parts->u = Qt_FromMatrix(U);
500  p = snuggle(parts->u, &parts->k);
501  parts->u = Qt_Mul(parts->u, p);
502 }
503 
504 /******* Invert Affine Decomposition *******/
505 
506 /* Compute inverse of affine decomposition.
507  */
508 void invert_affine(AffineParts *parts, AffineParts *inverse)
509 {
510  Quat t, p;
511  inverse->f = parts->f;
512  inverse->q = Qt_Conj(parts->q);
513  inverse->u = Qt_Mul(parts->q, parts->u);
514  inverse->k.x = (parts->k.x==0.0) ? 0.0 : 1.0/parts->k.x;
515  inverse->k.y = (parts->k.y==0.0) ? 0.0 : 1.0/parts->k.y;
516  inverse->k.z = (parts->k.z==0.0) ? 0.0 : 1.0/parts->k.z;
517  inverse->k.w = parts->k.w;
518  t = Qt_(-parts->t.x, -parts->t.y, -parts->t.z, 0);
519  t = Qt_Mul(Qt_Conj(inverse->u), Qt_Mul(t, inverse->u));
520  t = Qt_(inverse->k.x*t.x, inverse->k.y*t.y, inverse->k.z*t.z, 0);
521  p = Qt_Mul(inverse->q, inverse->u);
522  t = Qt_Mul(p, Qt_Mul(t, Qt_Conj(p)));
523  inverse->t = (inverse->f>0.0) ? t : Qt_(-t.x, -t.y, -t.z, 0);
524 }
#define mat_binop(C, gets, A, op, B, n)
Assign nxn matrix C the element-wise combination of A and B using &quot;op&quot;.
Definition: Decompose.cpp:43
float x
Definition: Decompose.h:21
Definition: Decompose.h:22
int find_max_col(HMatrix M)
Return index of column of M containing maximum abs entry, or -1 if M=0.
Definition: Decompose.cpp:181
HVect k
Definition: Decompose.h:29
float z
Definition: Decompose.h:21
Definition: Decompose.h:22
Quat Qt_Mul(Quat qL, Quat qR)
Definition: Decompose.cpp:97
HVect t
Definition: Decompose.h:26
float norm_one(HMatrix M)
Definition: Decompose.cpp:178
float polar_decomp(HMatrix M, HMatrix Q, HMatrix S)
Definition: Decompose.cpp:274
void adjoint_transpose(HMatrix M, HMatrix MadjT)
Set MadjT to transpose of inverse of M times determinant of M.
Definition: Decompose.cpp:69
float HMatrix[4][4]
Definition: Decompose.h:24
float f
Definition: Decompose.h:30
float vdot(float *va, float *vb)
Return dot product of length 3 vectors va and vb.
Definition: Decompose.cpp:55
float y
Definition: Decompose.h:21
#define mat_tpose(AT, gets, A, n)
Copy transpose of nxn matrix A to C using &quot;gets&quot; for assignment.
Definition: Decompose.cpp:39
#define mat_copy(C, gets, A, n)
Copy nxn matrix A to C using &quot;gets&quot; for assignment.
Definition: Decompose.cpp:35
Definition: Decompose.h:22
void vcross(float *va, float *vb, float *v)
Set v to cross product of length 3 vectors va and vb.
Definition: Decompose.cpp:61
#define caseMacro(i, j, k, I, J, K)
void invert_affine(AffineParts *parts, AffineParts *inverse)
Definition: Decompose.cpp:508
void reflect_cols(HMatrix M, float *u)
Apply Householder reflection represented by u to column vectors of M.
Definition: Decompose.cpp:204
void do_rank2(HMatrix M, HMatrix MadjT, HMatrix Q)
Find orthogonal factor Q of rank 2 (or less) M using adjoint transpose.
Definition: Decompose.cpp:241
static HMatrix mat_id
Definition: Decompose.cpp:161
Quat Qt_FromMatrix(HMatrix mat)
Definition: Decompose.cpp:119
float norm_inf(HMatrix M)
Definition: Decompose.cpp:177
void reflect_rows(HMatrix M, float *u)
Apply Householder reflection represented by u to row vectors of M.
Definition: Decompose.cpp:213
Quat Qt_(float x, float y, float z, float w)
Definition: Decompose.cpp:79
HVect spect_decomp(HMatrix S, HMatrix U)
Definition: Decompose.cpp:326
void mat_mult(HMatrix A, HMatrix B, HMatrix AB)
Multiply the upper left 3x3 parts of A and B to get AB.
Definition: Decompose.cpp:47
Quat Qt_Conj(Quat q)
Definition: Decompose.cpp:87
void make_reflector(float *v, float *u)
Setup u for Household reflection to zero all v components but first.
Definition: Decompose.cpp:194
#define TOL
#define SQRTHALF
Quat snuggle(Quat q, HVect *k)
Definition: Decompose.cpp:381
float w
Definition: Decompose.h:21
void decomp_affine(HMatrix A, AffineParts *parts)
Definition: Decompose.cpp:486
Definition: Decompose.h:22
Definition: Decompose.h:21
#define sgn(n, v)
#define mat_pad(A)
Fill out 3x3 matrix to 4x4.
Definition: Decompose.cpp:32
Quat Qt_Scale(Quat q, float w)
Definition: Decompose.cpp:108
#define swap(a, i, j)
float mat_norm(HMatrix M, int tpose)
Compute either the 1 or infinity norm of M, depending on tpose.
Definition: Decompose.cpp:164
#define cycle(a, p)
void do_rank1(HMatrix M, HMatrix Q)
Find orthogonal factor Q of rank 1 (or less) M.
Definition: Decompose.cpp:223