⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 decompose.c

📁 [Game.Programming].Academic - Graphics Gems (6 books source code)
💻 C
📖 第 1 页 / 共 2 页
字号:
{#define TOL 1.0e-6    HMatrix Mk, MadjTk, Ek;    float det, M_one, M_inf, MadjT_one, MadjT_inf, E_one, gamma, g1, g2;    int i, j;    mat_tpose(Mk,=,M,3);    M_one = norm_one(Mk);  M_inf = norm_inf(Mk);    do {	adjoint_transpose(Mk, MadjTk);	det = vdot(Mk[0], MadjTk[0]);	if (det==0.0) {do_rank2(Mk, MadjTk, Mk); break;}	MadjT_one = norm_one(MadjTk); MadjT_inf = norm_inf(MadjTk);	gamma = sqrt(sqrt((MadjT_one*MadjT_inf)/(M_one*M_inf))/fabs(det));	g1 = gamma*0.5;	g2 = 0.5/(gamma*det);	mat_copy(Ek,=,Mk,3);	mat_binop(Mk,=,g1*Mk,+,g2*MadjTk,3);	mat_copy(Ek,-=,Mk,3);	E_one = norm_one(Ek);	M_one = norm_one(Mk);  M_inf = norm_inf(Mk);    } while (E_one>(M_one*TOL));    mat_tpose(Q,=,Mk,3); mat_pad(Q);    mat_mult(Mk, M, S);	 mat_pad(S);    for (i=0; i<3; i++) for (j=i; j<3; j++)	S[i][j] = S[j][i] = 0.5*(S[i][j]+S[j][i]);    return (det);}/******* Spectral Decomposition *******//* Compute the spectral decomposition of symmetric positive semi-definite S. * Returns rotation in U and scale factors in result, so that if K is a diagonal * matrix of the scale factors, then S = U K (U transpose). Uses Jacobi method. * See Gene H. Golub and Charles F. Van Loan. Matrix Computations. Hopkins 1983. */HVect spect_decomp(HMatrix S, HMatrix U){    HVect kv;    double Diag[3],OffD[3]; /* OffD is off-diag (by omitted index) */    double g,h,fabsh,fabsOffDi,t,theta,c,s,tau,ta,OffDq,a,b;    static char nxt[] = {Y,Z,X};    int sweep, i, j;    mat_copy(U,=,mat_id,4);    Diag[X] = S[X][X]; Diag[Y] = S[Y][Y]; Diag[Z] = S[Z][Z];    OffD[X] = S[Y][Z]; OffD[Y] = S[Z][X]; OffD[Z] = S[X][Y];    for (sweep=20; sweep>0; sweep--) {	float sm = fabs(OffD[X])+fabs(OffD[Y])+fabs(OffD[Z]);	if (sm==0.0) break;	for (i=Z; i>=X; i--) {	    int p = nxt[i]; int q = nxt[p];	    fabsOffDi = fabs(OffD[i]);	    g = 100.0*fabsOffDi;	    if (fabsOffDi>0.0) {		h = Diag[q] - Diag[p];		fabsh = fabs(h);		if (fabsh+g==fabsh) {		    t = OffD[i]/h;		} else {		    theta = 0.5*h/OffD[i];		    t = 1.0/(fabs(theta)+sqrt(theta*theta+1.0));		    if (theta<0.0) t = -t;		}		c = 1.0/sqrt(t*t+1.0); s = t*c;		tau = s/(c+1.0);		ta = t*OffD[i]; OffD[i] = 0.0;		Diag[p] -= ta; Diag[q] += ta;		OffDq = OffD[q];		OffD[q] -= s*(OffD[p] + tau*OffD[q]);		OffD[p] += s*(OffDq   - tau*OffD[p]);		for (j=Z; j>=X; j--) {		    a = U[j][p]; b = U[j][q];		    U[j][p] -= s*(b + tau*a);		    U[j][q] += s*(a - tau*b);		}	    }	}    }    kv.x = Diag[X]; kv.y = Diag[Y]; kv.z = Diag[Z]; kv.w = 1.0;    return (kv);}/******* Spectral Axis Adjustment *******//* Given a unit quaternion, q, and a scale vector, k, find a unit quaternion, p, * which permutes the axes and turns freely in the plane of duplicate scale * factors, such that q p has the largest possible w component, i.e. the * smallest possible angle. Permutes k's components to go with q p instead of q. * See Ken Shoemake and Tom Duff. Matrix Animation and Polar Decomposition. * Proceedings of Graphics Interface 1992. Details on p. 262-263. */Quat snuggle(Quat q, HVect *k){#define SQRTHALF (0.7071067811865475244)#define sgn(n,v)    ((n)?-(v):(v))#define swap(a,i,j) {a[3]=a[i]; a[i]=a[j]; a[j]=a[3];}#define cycle(a,p)  if (p) {a[3]=a[0]; a[0]=a[1]; a[1]=a[2]; a[2]=a[3];}\		    else   {a[3]=a[2]; a[2]=a[1]; a[1]=a[0]; a[0]=a[3];}    Quat p;    float ka[4];    int i, turn = -1;    ka[X] = k->x; ka[Y] = k->y; ka[Z] = k->z;    if (ka[X]==ka[Y]) {if (ka[X]==ka[Z]) turn = W; else turn = Z;}    else {if (ka[X]==ka[Z]) turn = Y; else if (ka[Y]==ka[Z]) turn = X;}    if (turn>=0) {	Quat qtoz, qp;	unsigned neg[3], win;	double mag[3], t;	static Quat qxtoz = {0,SQRTHALF,0,SQRTHALF};	static Quat qytoz = {SQRTHALF,0,0,SQRTHALF};	static Quat qppmm = { 0.5, 0.5,-0.5,-0.5};	static Quat qpppp = { 0.5, 0.5, 0.5, 0.5};	static Quat qmpmm = {-0.5, 0.5,-0.5,-0.5};	static Quat qpppm = { 0.5, 0.5, 0.5,-0.5};	static Quat q0001 = { 0.0, 0.0, 0.0, 1.0};	static Quat q1000 = { 1.0, 0.0, 0.0, 0.0};	switch (turn) {	default: return (Qt_Conj(q));	case X: q = Qt_Mul(q, qtoz = qxtoz); swap(ka,X,Z) break;	case Y: q = Qt_Mul(q, qtoz = qytoz); swap(ka,Y,Z) break;	case Z: qtoz = q0001; break;	}	q = Qt_Conj(q);	mag[0] = (double)q.z*q.z+(double)q.w*q.w-0.5;	mag[1] = (double)q.x*q.z-(double)q.y*q.w;	mag[2] = (double)q.y*q.z+(double)q.x*q.w;	for (i=0; i<3; i++) if (neg[i] = (mag[i]<0.0)) mag[i] = -mag[i];	if (mag[0]>mag[1]) {if (mag[0]>mag[2]) win = 0; else win = 2;}	else		   {if (mag[1]>mag[2]) win = 1; else win = 2;}	switch (win) {	case 0: if (neg[0]) p = q1000; else p = q0001; break;	case 1: if (neg[1]) p = qppmm; else p = qpppp; cycle(ka,0) break;	case 2: if (neg[2]) p = qmpmm; else p = qpppm; cycle(ka,1) break;	}	qp = Qt_Mul(q, p);	t = sqrt(mag[win]+0.5);	p = Qt_Mul(p, Qt_(0.0,0.0,-qp.z/t,qp.w/t));	p = Qt_Mul(qtoz, Qt_Conj(p));    } else {	float qa[4], pa[4];	unsigned lo, hi, neg[4], par = 0;	double all, big, two;	qa[0] = q.x; qa[1] = q.y; qa[2] = q.z; qa[3] = q.w;	for (i=0; i<4; i++) {	    pa[i] = 0.0;	    if (neg[i] = (qa[i]<0.0)) qa[i] = -qa[i];	    par ^= neg[i];	}	/* Find two largest components, indices in hi and lo */	if (qa[0]>qa[1]) lo = 0; else lo = 1;	if (qa[2]>qa[3]) hi = 2; else hi = 3;	if (qa[lo]>qa[hi]) {	    if (qa[lo^1]>qa[hi]) {hi = lo; lo ^= 1;}	    else {hi ^= lo; lo ^= hi; hi ^= lo;}	} else {if (qa[hi^1]>qa[lo]) lo = hi^1;}	all = (qa[0]+qa[1]+qa[2]+qa[3])*0.5;	two = (qa[hi]+qa[lo])*SQRTHALF;	big = qa[hi];	if (all>two) {	    if (all>big) {/*all*/		{int i; for (i=0; i<4; i++) pa[i] = sgn(neg[i], 0.5);}		cycle(ka,par)	    } else {/*big*/ pa[hi] = sgn(neg[hi],1.0);}	} else {	    if (two>big) {/*two*/		pa[hi] = sgn(neg[hi],SQRTHALF); pa[lo] = sgn(neg[lo], SQRTHALF);		if (lo>hi) {hi ^= lo; lo ^= hi; hi ^= lo;}		if (hi==W) {hi = "\001\002\000"[lo]; lo = 3-hi-lo;}		swap(ka,hi,lo)	    } else {/*big*/ pa[hi] = sgn(neg[hi],1.0);}	}	p.x = -pa[0]; p.y = -pa[1]; p.z = -pa[2]; p.w = pa[3];    }    k->x = ka[X]; k->y = ka[Y]; k->z = ka[Z];    return (p);}/******* Decompose Affine Matrix *******//* Decompose 4x4 affine matrix A as TFRUK(U transpose), where t contains the * translation components, q contains the rotation R, u contains U, k contains * scale factors, and f contains the sign of the determinant. * Assumes A transforms column vectors in right-handed coordinates. * See Ken Shoemake and Tom Duff. Matrix Animation and Polar Decomposition. * Proceedings of Graphics Interface 1992. */void decomp_affine(HMatrix A, AffineParts *parts){    HMatrix Q, S, U;    Quat p;    float det;    parts->t = Qt_(A[X][W], A[Y][W], A[Z][W], 0);    det = polar_decomp(A, Q, S);    if (det<0.0) {	mat_copy(Q,=,-Q,3);	parts->f = -1;    } else parts->f = 1;    parts->q = Qt_FromMatrix(Q);    parts->k = spect_decomp(S, U);    parts->u = Qt_FromMatrix(U);    p = snuggle(parts->u, &parts->k);    parts->u = Qt_Mul(parts->u, p);}/******* Invert Affine Decomposition *******//* Compute inverse of affine decomposition. */void invert_affine(AffineParts *parts, AffineParts *inverse){    Quat t, p;    inverse->f = parts->f;    inverse->q = Qt_Conj(parts->q);    inverse->u = Qt_Mul(parts->q, parts->u);    inverse->k.x = (parts->k.x==0.0) ? 0.0 : 1.0/parts->k.x;    inverse->k.y = (parts->k.y==0.0) ? 0.0 : 1.0/parts->k.y;    inverse->k.z = (parts->k.z==0.0) ? 0.0 : 1.0/parts->k.z;    inverse->k.w = parts->k.w;    t = Qt_(-parts->t.x, -parts->t.y, -parts->t.z, 0);    t = Qt_Mul(Qt_Conj(inverse->u), Qt_Mul(t, inverse->u));    t = Qt_(inverse->k.x*t.x, inverse->k.y*t.y, inverse->k.z*t.z, 0);    p = Qt_Mul(inverse->q, inverse->u);    t = Qt_Mul(p, Qt_Mul(t, Qt_Conj(p)));    inverse->t = (inverse->f>0.0) ? t : Qt_(-t.x, -t.y, -t.z, 0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -