1 #include "mrilib.h"
2 #include "mri_genalign.c"
3 
main(int argc,char * argv[])4 int main( int argc , char *argv[] )
5 {
6    mat44 qmat ;
7    int npar ; float parvec[12] ;
8    int iarg=1 ;
9    char lab[2048] ;
10 
11    /*--- help the pitiful luser ---*/
12 
13    if( argc < 2 || strncasecmp(argv[1],"-h",2) == 0 ){
14      printf("\n"
15             "Usage: 1dApar2mat dx dy dz a1 a2 a3 sx sy sz hx hy hz\n"
16             "\n"
17             "* This program computes the affine transformation matrix\n"
18             "  from the set of 3dAllineate parameters.\n"
19             "\n"
20             "* The result is printed to stdout, and can be captured\n"
21             "  by Unix shell redirection (e.g., '|', '>', '>>', etc.).\n"
22             "  See the EXAMPLE, far below.\n"
23             "\n"
24             "* One use for 1dApar2mat is to take a set of parameters\n"
25             "  from '3dAllineate -1Dparam_save', alter them in some way,\n"
26             "  and re-compute the corresponding matrix. For example,\n"
27             "  compute the full affine transform with 12 parameters,\n"
28             "  but then omit the final 6 parameters to see what the\n"
29             "  'pure' shift+rotation matrix looks like.\n"
30             "\n"
31             "* The 12 parameters are, in the order used on the 1dApar2mat command line\n"
32             "  (the same order as output by 3dAllineate):\n"
33             "   x-shift         in mm\n"
34             "   y-shift         in mm\n"
35             "   z-shift         in mm\n"
36             "   z-angle (roll)  in degrees (not radians!)\n"
37             "   x-angle (pitch) in degrees\n"
38             "   y-angle (yaw)   in degrees\n"
39             "   x-scale         unitless factor, in [0.10,10.0]\n"
40             "   y-scale         unitless factor, in [0.10,10.0]\n"
41             "   z-scale         unitless factor, in [0.10,10.0]\n"
42             "   y/x-shear       unitless factor, in [-0.3333,0.3333]\n"
43             "   z/x-shear       unitless factor, in [-0.3333,0.3333]\n"
44             "   z/y-shear       unitless factor, in [-0.3333,0.3333]\n"
45             "\n"
46             "* Parameters omitted from the end of the command line get their\n"
47             "  default values (0 except for scales, which default to 1).\n"
48             "\n"
49             "* At least 1 parameter must be given, or you get this help message :)\n"
50             "  The minimum command line is\n"
51             "   1dApar2mat 0\n"
52             "  which will output the identity matrix.\n"
53             "\n"
54             "* Legal scale and shear factors have limited ranges, as\n"
55             "  described above. An input value outside the given range\n"
56             "  will be reset to the default value for that factor (1 or 0).\n"
57             "\n"
58             "* UNUSUAL SPECIAL CASES:\n"
59             "   If you used 3dAllineate with any of the options described\n"
60             "   under 'CHANGING THE ORDER OF MATRIX APPLICATION' or you\n"
61             "   used the '-EPI' option, then the order of parameters inside\n"
62             "   3dAllineate will no longer be the same as the parameter order\n"
63             "   in 1dApar2mat. In such a situation, the matrix output by\n"
64             "   this program will NOT agree with that output by 3dAllineate\n"
65             "   for the same set of parameter numbers :(\n"
66             "\n"
67             "* EXAMPLE:\n"
68             "   1dApar2mat 0 1 2 3 4 5\n"
69             "  to get a rotation matrix with some shifts; the output is:\n"
70             "  # mat44 1dApar2mat 0 1 2 3 4 5 :\n"
71             "        0.994511      0.058208     -0.086943       0.000000\n"
72             "       -0.052208      0.996197      0.069756       1.000000\n"
73             "        0.090673     -0.064834      0.993768       2.000000\n"
74             "  If you wish to capture this matrix all on one line, you can\n"
75             "  combine various Unix shell and command tricks/tools, as in\n"
76             "   echo `1dApar2mat 0 1 2 3 4 5 | tail -3` > Fred.aff12.1D\n"
77             "  This 12-numbers-in-one-line is the format output by '-1Dmatrix_save'\n"
78             "  in 3dAllineate and 3dvolreg.\n"
79             "\n"
80             "* FANCY EXAMPLE:\n"
81             "  Tricksy command line stuff to compute the inverse of a matrix\n"
82             "    set fred = `1dApar2mat 0 0 0 3 4 5 1 1 1 0.2 0.1 0.2 | tail -3`\n"
83             "    cat_matvec `echo $fred | sed -e 's/ /,/g' -e 's/^/MATRIX('/`')' -I\n"
84             "\n"
85             "* ALSO SEE: Programs cat_matvec and 1dmatcalc for doing\n"
86             "            simple matrix arithmetic on such files.\n"
87             "\n"
88             "* OPTIONS: This program has no options. Love it or leave it :)\n"
89             "\n"
90             "* AUTHOR: Zhark the Most Affine and Sublime - April 2019\n"
91             "\n"
92       ) ;
93       exit(0) ;
94    }
95 
96    /* initialize parvec (not really necessary) */
97 
98    for( npar=0 ; npar < 3 ; npar++ ){
99      parvec[0+npar] = parvec[3+npar] = parvec[9+npar] = 0.0f ;
100      parvec[6+npar] = 1.0f ; /* scale factors */
101    }
102 
103    /* get parvec from command line, and make an informative label */
104 
105    strcpy(lab,"1dApar2mat ") ;
106    for( npar=0 ; iarg < argc && npar < 12; iarg++,npar++ ){
107      parvec[npar] = (float)strtod(argv[iarg],NULL) ;
108      strcat(lab,argv[iarg]) ; strcat(lab," ") ;
109    }
110 
111    /* do the work */
112 
113    qmat = GA_setup_affine( npar , parvec ) ;
114 
115    /* dump the work */
116 
117    DUMP_MAT44(lab,qmat) ;
118    exit(0) ;
119 }
120