LCOV - code coverage report
Current view: top level - source/collada - Decompose.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 300 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 23 0.0 %

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

Generated by: LCOV version 1.16