--- ray/src/common/xf.c 1989/03/24 17:10:43 1.3 +++ ray/src/common/xf.c 1989/10/13 19:34:51 1.6 @@ -12,22 +12,29 @@ static char SCCSid[] = "$SunId$ LBL"; #define PI 3.14159265358979323846 +#define d2r(a) ((PI/180.)*(a)) -#define checkarg(a,n) if (strcmp(av[i],a) || i+n >= ac) return(i) +#define checkarg(a,n) if (av[i][a] || i+n >= ac) goto done int -xf(xfmat, xfsca, ac, av) /* get transform specification */ -double xfmat[4][4]; -double *xfsca; +xf(retmat, retsca, ac, av) /* get transform specification */ +double retmat[4][4]; +double *retsca; int ac; char *av[]; { double atof(), sin(), cos(); - double m4[4][4]; - double theta; - int i; + double xfmat[4][4], m4[4][4]; + double xfsca, theta; + int i, icnt; + setident4(retmat); + *retsca = 1.0; + + setident4(xfmat); + xfsca = 1.0; + for (i = 0; i < ac && av[i][0] == '-'; i++) { setident4(m4); @@ -35,7 +42,7 @@ char *av[]; switch (av[i][1]) { case 't': /* translate */ - checkarg("-t",3); + checkarg(2,3); m4[3][0] = atof(av[++i]); m4[3][1] = atof(av[++i]); m4[3][2] = atof(av[++i]); @@ -44,20 +51,20 @@ char *av[]; case 'r': /* rotate */ switch (av[i][2]) { case 'x': - checkarg("-rx",1); - theta = PI/180.0 * atof(av[++i]); + checkarg(3,1); + theta = d2r(atof(av[++i])); m4[1][1] = m4[2][2] = cos(theta); m4[2][1] = -(m4[1][2] = sin(theta)); break; case 'y': - checkarg("-ry",1); - theta = PI/180.0 * atof(av[++i]); + checkarg(3,1); + theta = d2r(atof(av[++i])); m4[0][0] = m4[2][2] = cos(theta); m4[0][2] = -(m4[2][0] = sin(theta)); break; case 'z': - checkarg("-rz",1); - theta = PI/180.0 * atof(av[++i]); + checkarg(3,1); + theta = d2r(atof(av[++i])); m4[0][0] = m4[1][1] = cos(theta); m4[1][0] = -(m4[0][1] = sin(theta)); break; @@ -67,8 +74,8 @@ char *av[]; break; case 's': /* scale */ - checkarg("-s",1); - *xfsca *= + checkarg(2,1); + xfsca *= m4[0][0] = m4[1][1] = m4[2][2] = atof(av[++i]); @@ -77,18 +84,18 @@ char *av[]; case 'm': /* mirror */ switch (av[i][2]) { case 'x': - checkarg("-mx",0); - *xfsca *= + checkarg(3,0); + xfsca *= m4[0][0] = -1.0; break; case 'y': - checkarg("-my",0); - *xfsca *= + checkarg(3,0); + xfsca *= m4[1][1] = -1.0; break; case 'z': - checkarg("-mz",0); - *xfsca *= + checkarg(3,0); + xfsca *= m4[2][2] = -1.0; break; default: @@ -96,29 +103,49 @@ char *av[]; } break; + case 'i': /* iterate */ + checkarg(2,1); + icnt = atoi(av[++i]); + while (icnt-- > 0) { + multmat4(retmat, retmat, xfmat); + *retsca *= xfsca; + } + setident4(xfmat); + xfsca = 1.0; + break; + default: return(i); } multmat4(xfmat, xfmat, m4); } +done: + multmat4(retmat, retmat, xfmat); + *retsca *= xfsca; return(i); } #ifdef INVXF int -invxf(xfmat, xfsca, ac, av) /* invert transform specification */ -double xfmat[4][4]; -double *xfsca; +invxf(retmat, retsca, ac, av) /* invert transform specification */ +double retmat[4][4]; +double *retsca; int ac; char *av[]; { double atof(), sin(), cos(); - double m4[4][4]; - double theta; - int i; + double xfmat[4][4], m4[4][4]; + double xfsca, theta; + int i, icnt; + setident4(retmat); + *retsca = 1.0; + + setident4(xfmat); + xfsca = 1.0; + for (i = 0; i < ac && av[i][0] == '-'; i++) { setident4(m4); @@ -126,7 +153,7 @@ char *av[]; switch (av[i][1]) { case 't': /* translate */ - checkarg("-t",3); + checkarg(2,3); m4[3][0] = -atof(av[++i]); m4[3][1] = -atof(av[++i]); m4[3][2] = -atof(av[++i]); @@ -135,20 +162,20 @@ char *av[]; case 'r': /* rotate */ switch (av[i][2]) { case 'x': - checkarg("-rx",1); - theta = -PI/180.0 * atof(av[++i]); + checkarg(3,1); + theta = -d2r(atof(av[++i])); m4[1][1] = m4[2][2] = cos(theta); m4[2][1] = -(m4[1][2] = sin(theta)); break; case 'y': - checkarg("-ry",1); - theta = -PI/180.0 * atof(av[++i]); + checkarg(3,1); + theta = -d2r(atof(av[++i])); m4[0][0] = m4[2][2] = cos(theta); m4[0][2] = -(m4[2][0] = sin(theta)); break; case 'z': - checkarg("-rz",1); - theta = -PI/180.0 * atof(av[++i]); + checkarg(3,1); + theta = -d2r(atof(av[++i])); m4[0][0] = m4[1][1] = cos(theta); m4[1][0] = -(m4[0][1] = sin(theta)); break; @@ -158,8 +185,8 @@ char *av[]; break; case 's': /* scale */ - checkarg("-s",1); - *xfsca *= + checkarg(2,1); + xfsca *= m4[0][0] = m4[1][1] = m4[2][2] = 1.0 / atof(av[++i]); @@ -168,18 +195,18 @@ char *av[]; case 'm': /* mirror */ switch (av[i][2]) { case 'x': - checkarg("-mx",0); - *xfsca *= + checkarg(3,0); + xfsca *= m4[0][0] = -1.0; break; case 'y': - checkarg("-my",0); - *xfsca *= + checkarg(3,0); + xfsca *= m4[1][1] = -1.0; break; case 'z': - checkarg("-mz",0); - *xfsca *= + checkarg(3,0); + xfsca *= m4[2][2] = -1.0; break; default: @@ -187,12 +214,26 @@ char *av[]; } break; + case 'i': /* iterate */ + checkarg(2,1); + icnt = atoi(av[++i]); + while (icnt-- > 0) { + multmat4(retmat, xfmat, retmat); + *retsca *= xfsca; + } + setident4(xfmat); + xfsca = 1.0; + break; + default: return(i); } multmat4(xfmat, m4, xfmat); /* left multiply */ } +done: + multmat4(retmat, xfmat, retmat); + *retsca *= xfsca; return(i); } #endif