reg_transform.cpp 52.3 KB
Newer Older
Marc Modat's avatar
Marc Modat committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 *  reg_transform.cpp
 *
 *
 *  Created by Marc Modat on 08/11/2010.
 *  Copyright (c) 2009, University College London. All rights reserved.
 *  Centre for Medical Image Computing (CMIC)
 *  See the LICENSE.txt file in the nifty_reg root folder
 *
 */

#ifndef _MM_TRANSFORM_CPP
#define _MM_TRANSFORM_CPP

15
#include "_reg_ReadWriteImage.h"
Marc Modat's avatar
Marc Modat committed
16
#include "_reg_resampling.h"
17 18
#include "_reg_globalTransformation.h"
#include "_reg_localTransformation.h"
Marc Modat's avatar
Marc Modat committed
19
#include "_reg_tools.h"
Marc Modat's avatar
Marc Modat committed
20 21
#include "_reg_thinPlateSpline.h"

Marc Modat's avatar
Marc Modat committed
22 23
#include "reg_transform.h"

Marc Modat's avatar
Marc Modat committed
24 25 26
#include <fstream>
#include <vector>
#include <iostream>
Marc Modat's avatar
Marc Modat committed
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
typedef struct
{
   char *referenceImageName;
   char *referenceImage2Name;
   char *inputTransName;
   char *input2TransName;
   char *outputTransName;
   float affTransParam[12];
} PARAM;
typedef struct
{
   bool referenceImageFlag;
   bool referenceImage2Flag;
   bool outputDefFlag;
   bool outputDispFlag;
   bool outputFlowFlag;
   bool outputCompFlag;
   bool updSFormFlag;
   bool halfTransFlag;
   bool invertAffFlag;
   bool invertNRRFlag;
   bool makeAffFlag;
   bool aff2rigFlag;
} FLAG;
Marc Modat's avatar
Marc Modat committed
52 53 54 55


void PetitUsage(char *exec)
{
56 57 58
   fprintf(stderr,"Usage:\t%s [OPTIONS].\n",exec);
   fprintf(stderr,"\tSee the help for more details (-h).\n");
   return;
Marc Modat's avatar
Marc Modat committed
59 60 61
}
void Usage(char *exec)
{
62 63
   printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
   printf("Usage:\t%s [OPTIONS].\n",exec);
64 65

#ifdef _SVN_REV
66
   fprintf(stderr,"\n-v Print the subversion revision number\n");
67
#endif
68
   printf("\n* * OPTIONS * *\n\n");
69

70 71 72 73 74
   printf("\t-ref <filename>\n");
   printf("\t\tFilename of the reference image\n");
   printf("\t\tThe Reference image has to be specified when a cubic B-Spline parametrised control point grid is used*.\n");
   printf("\t-ref2 <filename>\n");
   printf("\t\tFilename of the second reference image to be used when dealing with composition\n\n");
75

76 77 78 79
   printf("\t-def <filename1> <filename2>\n");
   printf("\t\tTake a transformation of any recognised type* and compute the corresponding deformation field\n");
   printf("\t\tfilename1 - Input transformation file name\n");
   printf("\t\tfilename2 - Output deformation field file name\n\n");
80

81 82 83 84
   printf("\t-disp <filename1> <filename2>\n");
   printf("\t\tTake a transformation of any recognised type* and compute the corresponding displacement field\n");
   printf("\t\tfilename1 - Input transformation file name\n");
   printf("\t\tfilename2 - Output displacement field file name\n\n");
85

86 87 88 89
   printf("\t-flow <filename1> <filename2>\n");
   printf("\t\tTake a spline parametrised SVF and compute the corresponding flow field\n");
   printf("\t\tfilename1 - Input transformation file name\n");
   printf("\t\tfilename2 - Output flow field file name\n\n");
90

91 92 93
   printf("\t-comp <filename1> <filename2> <filename3>\n");
   printf("\t\tCompose two transformations of any recognised type* and returns a deformation field.\n");
   printf("\t\tTrans3(x) = Trans2(Trans1(x)).\n");
94 95
   printf("\t\tfilename1 - Input transformation 1 file name (associated with -ref if required)\n");
   printf("\t\tfilename2 - Input transformation 2 file name (associated with -ref2 if required)\n");
96
   printf("\t\tfilename3 - Output deformation field file name\n\n");
97

98 99 100 101 102
   printf("\t-updSform <filename1> <filename2> <filename3>\n");
   printf("\t\tUpdate the sform of an image using an affine transformation.\n");
   printf("\t\tFilename1 - Image to be updated\n");
   printf("\t\tFilename2 - Affine transformation defined as Affine x Reference = Floating\n");
   printf("\t\tFilename3 - Updated image.\n\n");
103

104 105 106 107
   printf("\t-invAff <filename1> <filename2>\n");
   printf("\t\tInvert an affine matrix.\n");
   printf("\t\tfilename1 - Input affine transformation file name\n");
   printf("\t\tfilename2 - Output inverted affine transformation file name\n\n");
108

109 110 111 112 113 114 115
   printf("\t-invNrr <filename1> <filename2> <filename3>\n");
   printf("\t\tInvert a non-rigid transformation and save the result as a deformation field.\n");
   printf("\t\tfilename1 - Input transformation file name\n");
   printf("\t\tfilename2 - Input floating (source) image where the inverted transformation is defined\n");
   printf("\t\tfilename3 - Output inverted transformation file name\n\n");
   printf("\t\tNote that the cubic b-spline grid parametrisations can not be inverted without approximation,\n");
   printf("\t\tas a result, they are converted into deformation fields before inversion.\n");
116

117 118 119 120
   printf("\t-half <filename1> <filename2>\n");
   printf("\t\tThe input transformation is halfed and stored using the same transformation type.\n");
   printf("\t\tfilename1 - Input transformation file name\n");
   printf("\t\tfilename2 - Output transformation file name\n\n");
121

122 123
   printf("\t-makeAff <rx> <ry> <rz> <tx> <ty> <tz> <sx> <sy> <sz> <shx> <shy> <shz> <outputFilename>\n");
   printf("\t\tCreate an affine transformation matrix\n\n");
124

125 126 127 128
   printf("\t-aff2rig <filename1> <filename2>\n");
   printf("\t\tExtract the rigid component from an affine transformation matrix\n\n");
   printf("\t\tfilename1 - Input transformation file name\n");
   printf("\t\tfilename2 - Output transformation file name\n\n");
129

130 131 132 133 134 135 136 137
   printf("\t* The supported transformation types are:\n");
   printf("\t\t- cubic B-Spline parametrised grid (reference image is required)\n");
   printf("\t\t- a dense deformation field\n");
   printf("\t\t- a dense displacement field\n");
   printf("\t\t- a cubic B-Spline parametrised stationary velocity field (reference image is required)\n");
   printf("\t\t- a stationary velocity deformation field\n");
   printf("\t\t- a stationary velocity displacement field\n");
   printf("\t\t- an affine matrix\n\n");
138

139 140
   printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
   return;
Marc Modat's avatar
Marc Modat committed
141 142 143 144
}

int main(int argc, char **argv)
{
145 146 147 148 149 150
   // Display the help if no arguments are provided
   if(argc==1)
   {
      PetitUsage(argv[0]);
      return 0;
   }
151

152 153 154
   // Set the variables used to store the parsed data
   PARAM *param = (PARAM *)calloc(1,sizeof(PARAM));
   FLAG *flag = (FLAG *)calloc(1,sizeof(FLAG));
155

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
   // Parse the input data
   for(size_t i=1; i<argc; ++i)
   {
      if(strcmp(argv[i],"-h")==0 || strcmp(argv[i],"--h")==0 ||
            strcmp(argv[i],"-H")==0 || strcmp(argv[i],"--H")==0 ||
            strcmp(argv[i],"-help")==0 || strcmp(argv[i],"--help")==0 ||
            strcmp(argv[i],"-Help")==0 || strcmp(argv[i],"--Help")==0 ||
            strcmp(argv[i],"-HELP")==0 || strcmp(argv[i],"--HELP")==0)
      {
         free(param);
         free(flag);
         Usage(argv[0]);
         return 0;
      }
      else if(strcmp(argv[i],"-ref")==0 || strcmp(argv[i],"--ref")==0 || strcmp(argv[i],"-target")==0)
      {
         flag->referenceImageFlag=true;
         param->referenceImageName=argv[++i];
      }
      else if(strcmp(argv[i],"-ref2")==0 || strcmp(argv[i],"--ref2")==0 || strcmp(argv[i],"-target2")==0)
      {
         flag->referenceImage2Flag=true;
         param->referenceImage2Name=argv[++i];
      }
      else if(strcmp(argv[i],"-def")==0 || strcmp(argv[i],"--def")==0)
      {
         flag->outputDefFlag=true;
         param->inputTransName=argv[++i];
         param->outputTransName=argv[++i];
      }
      else if(strcmp(argv[i],"-disp")==0 || strcmp(argv[i],"--disp")==0)
      {
         flag->outputDispFlag=true;
         param->inputTransName=argv[++i];
         param->outputTransName=argv[++i];
      }
      else if(strcmp(argv[i],"-flow")==0 || strcmp(argv[i],"--flow")==0)
      {
         flag->outputFlowFlag=true;
         param->inputTransName=argv[++i];
         param->outputTransName=argv[++i];
      }
      else if(strcmp(argv[i],"-comp")==0 || strcmp(argv[i],"--comp")==0)
      {
         flag->outputCompFlag=true;
         param->inputTransName=argv[++i];
         param->input2TransName=argv[++i];
         param->outputTransName=argv[++i];
      }
      else if(strcmp(argv[i],"-updSform")==0 || strcmp(argv[i],"--comp")==0)
      {
         flag->updSFormFlag=true;
         param->inputTransName=argv[++i];
         param->input2TransName=argv[++i];
         param->outputTransName=argv[++i];
      }
      else if(strcmp(argv[i],"-half")==0 || strcmp(argv[i],"--half")==0)
      {
         flag->halfTransFlag=true;
         param->inputTransName=argv[++i];
         param->outputTransName=argv[++i];
      }
      else if(strcmp(argv[i],"-invAff")==0 || strcmp(argv[i],"--invAff")==0 ||
              strcmp(argv[i],"-invAffine")==0 || strcmp(argv[i],"--invAffine")==0)
      {
         flag->invertAffFlag=true;
         param->inputTransName=argv[++i];
         param->outputTransName=argv[++i];
      }
      else if(strcmp(argv[i],"-invNrr")==0 || strcmp(argv[i],"--invNrr")==0)
      {
         flag->invertNRRFlag=true;
         param->inputTransName=argv[++i];
         param->input2TransName=argv[++i];
         param->outputTransName=argv[++i];
      }
      else if(strcmp(argv[i],"-makeAff")==0 || strcmp(argv[i],"--makeAff")==0)
      {
         flag->makeAffFlag=true;
         for(int j=0; j<12; ++j)
            param->affTransParam[j]=static_cast<float>(atof(argv[++i]));
         param->outputTransName=argv[++i];
      }
      else if(strcmp(argv[i],"-aff2rig")==0 || strcmp(argv[i],"--aff2rig")==0)
      {
         flag->aff2rigFlag=true;
         param->inputTransName=argv[++i];
         param->outputTransName=argv[++i];
      }
      else
      {
         fprintf(stderr, "[NiftyReg ERROR] Unrecognised argument: %s\n",
                 argv[i]);
         return 1;
      }
   }
252

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
   /* ********************************************** */
   // Generate the deformation or displacement field //
   /* ********************************************** */
   if(flag->outputDefFlag || flag->outputDispFlag || flag->outputFlowFlag)
   {
      // Create some variables
      mat44 *affineTransformation=NULL;
      nifti_image *referenceImage=NULL;
      nifti_image *inputTransformationImage=NULL;
      nifti_image *outputTransformationImage=NULL;
      // First check if the input filename is an image
      if(reg_isAnImageFileName(param->inputTransName))
      {
         inputTransformationImage=reg_io_ReadImageFile(param->inputTransName);
         if(inputTransformationImage==NULL)
         {
            fprintf(stderr, "[NiftyReg ERROR] Error when reading the provided transformation: %s\n",
                    param->inputTransName);
            return 1;
         }
         reg_checkAndCorrectDimension(inputTransformationImage);
         // If the input transformation is a grid, check that the reference image has been specified
         if(inputTransformationImage->intent_p1==SPLINE_GRID ||
               inputTransformationImage->intent_p1==SPLINE_VEL_GRID)
         {
            if(!flag->referenceImageFlag)
            {
               fprintf(stderr, "[NiftyReg ERROR] When using a control point grid parametrisation (%s),",
                       param->inputTransName);
               fprintf(stderr, " a reference image shoud be specified (-ref flag).\n");
               return 1;
284 285
            }
            referenceImage=reg_io_ReadImageHeader(param->referenceImageName);
286 287 288 289 290
            if(referenceImage==NULL)
            {
               fprintf(stderr, "[NiftyReg ERROR] Error when reading the reference image: %s\n",
                       param->referenceImageName);
               return 1;
291 292
            }
            reg_checkAndCorrectDimension(referenceImage);
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
         }
      }
      else
      {
         // Read the affine transformation
         affineTransformation=(mat44 *)malloc(sizeof(mat44));
         reg_tool_ReadAffineFile(affineTransformation,param->inputTransName);
         if(!flag->referenceImageFlag)
         {
            fprintf(stderr, "[NiftyReg ERROR] When using an affine transformation (%s),",
                    param->inputTransName);
            fprintf(stderr, " a reference image shoud be specified (-ref flag).\n");
            return 1;
         }
         referenceImage=reg_io_ReadImageHeader(param->referenceImageName);
         if(referenceImage==NULL)
         {
            fprintf(stderr, "[NiftyReg ERROR] Error when reading the reference image: %s\n",
                    param->referenceImageName);
            return 1;
         }
         reg_checkAndCorrectDimension(referenceImage);
      }
      // Create a dense field
      if(affineTransformation!=NULL ||
            inputTransformationImage->intent_p1==SPLINE_GRID ||
            inputTransformationImage->intent_p1==SPLINE_VEL_GRID)
      {
         // Create a field image from the reference image
         outputTransformationImage=nifti_copy_nim_info(referenceImage);
         outputTransformationImage->ndim=outputTransformationImage->dim[0]=5;
         outputTransformationImage->nt=outputTransformationImage->dim[4]=1;
         outputTransformationImage->nu=outputTransformationImage->dim[5]=outputTransformationImage->nz>1?3:2;
         outputTransformationImage->nvox=(size_t)outputTransformationImage->nx *
                                         outputTransformationImage->ny * outputTransformationImage->nz *
                                         outputTransformationImage->nt * outputTransformationImage->nu;
         outputTransformationImage->nbyper=sizeof(float);
         outputTransformationImage->datatype=NIFTI_TYPE_FLOAT32;
         outputTransformationImage->intent_code=NIFTI_INTENT_VECTOR;
         memset(outputTransformationImage->intent_name, 0, 16);
         strcpy(outputTransformationImage->intent_name,"NREG_TRANS");
      }
      else
      {
         // Create a deformation field from in the input transformation
         outputTransformationImage=nifti_copy_nim_info(inputTransformationImage);
      }
      // Allocate the output field data array
      outputTransformationImage->data=(void *)malloc
                                      (outputTransformationImage->nvox*outputTransformationImage->nbyper);
      // Create a flow field image
      if(flag->outputFlowFlag)
      {
         if(affineTransformation!=NULL)
         {
            fprintf(stderr,"[NiftyReg ERROR] A flow field transformation can not be generated from an affine transformation\n");
            return 1;
         }
         if(inputTransformationImage->intent_p1==SPLINE_GRID)
         {
            fprintf(stderr,"[NiftyReg ERROR] A flow field transformation can not be generated from a cubic spline grid\n");
            return 1;
         }
         if(inputTransformationImage->intent_p1==DEF_FIELD)
         {
            fprintf(stderr,"[NiftyReg ERROR] A flow field transformation can not be generated from a deformation field\n");
            return 1;
         }
         if(inputTransformationImage->intent_p1==DISP_FIELD)
         {
            fprintf(stderr,"[NiftyReg ERROR] A flow field transformation can not be generated from a displacement field\n");
            return 1;
         }
         switch(static_cast<int>(inputTransformationImage->intent_p1))
         {
            break;
         case DEF_VEL_FIELD:
            printf("[NiftyReg] The specified transformation is a deformation velocity field:\n[NiftyReg] %s\n",
                   inputTransformationImage->fname);
            // The current input transformation is copied
            memcpy(outputTransformationImage->data,inputTransformationImage->data,
                   outputTransformationImage->nvox*outputTransformationImage->nbyper);
375
            break;
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
         case DISP_VEL_FIELD:
            printf("[NiftyReg] The specified transformation is a displacement velocity field:\n[NiftyReg] %s\n",
                   inputTransformationImage->fname);
            // The current input transformation is copied and converted
            memcpy(outputTransformationImage->data,inputTransformationImage->data,
                   outputTransformationImage->nvox*outputTransformationImage->nbyper);
            reg_getDisplacementFromDeformation(outputTransformationImage);
            break;
         case SPLINE_VEL_GRID:
            printf("[NiftyReg] The specified transformation is a spline velocity parametrisation:\n[NiftyReg] %s\n",
                   inputTransformationImage->fname);
            reg_spline_getFlowFieldFromVelocityGrid(inputTransformationImage,
                                                    outputTransformationImage);
            break;
         default:
            fprintf(stderr,"[NiftyReg ERROR] Unknown input transformation type\n");
            return 1;
         }
         outputTransformationImage->intent_p1=DEF_VEL_FIELD;
         outputTransformationImage->intent_p2=inputTransformationImage->intent_p2;
      }
      // Create a deformation or displacement field
      else if(flag->outputDefFlag || flag->outputDispFlag)
      {
         if(affineTransformation!=NULL)
         {
            reg_affine_getDeformationField(affineTransformation,outputTransformationImage);
         }
         else
         {
            switch(static_cast<int>(reg_round(inputTransformationImage->intent_p1)))
            {
            case DEF_FIELD:
               printf("[NiftyReg] The specified transformation is a deformation field:\n[NiftyReg] %s\n",
                      inputTransformationImage->fname);
               // the current in transformation is copied
               memcpy(outputTransformationImage->data,inputTransformationImage->data,
                      outputTransformationImage->nvox*outputTransformationImage->nbyper);
               break;
            case DISP_FIELD:
               printf("[NiftyReg] The specified transformation is a displacement field:\n[NiftyReg] %s\n",
                      inputTransformationImage->fname);
               // the current in transformation is copied and converted
               memcpy(outputTransformationImage->data,inputTransformationImage->data,
                      outputTransformationImage->nvox*outputTransformationImage->nbyper);
               reg_getDeformationFromDisplacement(outputTransformationImage);
               break;
            case SPLINE_GRID:
               printf("[NiftyReg] The specified transformation is a spline parametrisation:\n[NiftyReg] %s\n",
                      inputTransformationImage->fname);
               // The output field is filled with an identity deformation field
               memset(outputTransformationImage->data,
                      0,
                      outputTransformationImage->nvox*outputTransformationImage->nbyper);
               reg_getDeformationFromDisplacement(outputTransformationImage);
               // The spline transformation is composed with the identity field
               reg_spline_getDeformationField(inputTransformationImage,
                                              outputTransformationImage,
                                              NULL, // no mask
                                              true, // composition is used,
                                              true // b-spline are used
                                             );
               break;
439
            case DEF_VEL_FIELD:
440 441 442 443 444 445 446 447
               printf("[NiftyReg] The specified transformation is a deformation velocity field:\n[NiftyReg] %s\n",
                      inputTransformationImage->fname);
               // The flow field is exponentiated
               reg_defField_getDeformationFieldFromFlowField(inputTransformationImage,
                     outputTransformationImage,
                     false // step number is not updated
                                                            );
               break;
448
            case DISP_VEL_FIELD:
449 450 451 452 453 454 455 456 457 458
               printf("[NiftyReg] The specified transformation is a displacement velocity field:\n[NiftyReg] %s\n",
                      inputTransformationImage->fname);
               // The input transformation is converted into a def flow
               reg_getDeformationFromDisplacement(outputTransformationImage);
               // The flow field is exponentiated
               reg_defField_getDeformationFieldFromFlowField(inputTransformationImage,
                     outputTransformationImage,
                     false // step number is not updated
                                                            );
               break;
459
            case SPLINE_VEL_GRID:
460 461 462
               printf("[NiftyReg] The specified transformation is a spline velocity parametrisation:\n[NiftyReg] %s\n",
                      inputTransformationImage->fname);
               // The spline parametrisation is converted into a dense flow and exponentiated
463
               reg_spline_getDefFieldFromVelocityGrid(inputTransformationImage,
464 465 466 467
                     outputTransformationImage,
                     false // step number is not updated
                                                             );
               break;
468
            default:
469 470
               fprintf(stderr,"[NiftyReg ERROR] Unknown input transformation type\n");
               return 1;
471
            }
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
         }
         outputTransformationImage->intent_p1=DEF_FIELD;
         outputTransformationImage->intent_p2=0;
         if(flag->outputDispFlag)
            reg_getDisplacementFromDeformation(outputTransformationImage);
      }
      // Save the generated transformation
      reg_io_WriteImageFile(outputTransformationImage,param->outputTransName);
      switch(static_cast<int>(round(outputTransformationImage->intent_p1)))
      {
      case DEF_FIELD:
         printf("[NiftyReg] The deformation field has been saved as:\n[NiftyReg] %s\n",
                param->outputTransName);
         break;
      case DISP_FIELD:
         printf("[NiftyReg] The displacement field has been saved as:\n[NiftyReg] %s\n",
                param->outputTransName);
         break;
      case DEF_VEL_FIELD:
         printf("[NiftyReg] The flow field has been saved as:\n[NiftyReg] %s\n",
                param->outputTransName);
         break;
      }
      // Free the allocated images and arrays
      if(affineTransformation!=NULL) free(affineTransformation);
      if(referenceImage!=NULL) nifti_image_free(referenceImage);
      if(inputTransformationImage!=NULL) nifti_image_free(inputTransformationImage);
      if(outputTransformationImage!=NULL) nifti_image_free(outputTransformationImage);
   }
Marc Modat's avatar
Marc Modat committed
501

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
   /* ************************************ */
   // Start the transformation composition //
   /* ************************************ */
   if(flag->outputCompFlag)
   {
      printf("[NiftyReg] Starting the composition of two transformations\n");
      // Create some variables
      mat44 *affine1Trans=NULL;
      mat44 *affine2Trans=NULL;
      nifti_image *referenceImage=NULL;
      nifti_image *referenceImage2=NULL;
      nifti_image *input1TransImage=NULL;
      nifti_image *input2TransImage=NULL;
      nifti_image *output1TransImage=NULL;
      nifti_image *output2TransImage=NULL;
      // Read the first transformation
      if(!reg_isAnImageFileName(param->inputTransName))
      {
         affine1Trans=(mat44 *)malloc(sizeof(mat44));
         reg_tool_ReadAffineFile(affine1Trans,param->inputTransName);
         printf("[NiftyReg] Transformation 1 is an affine parametrisation:\n[NiftyReg] %s\n",
                param->inputTransName);
      }
      else
      {
         input1TransImage = reg_io_ReadImageFile(param->inputTransName);
         if(input1TransImage==NULL)
         {
            fprintf(stderr, "[NiftyReg ERROR] Error when reading the transformation image: %s\n",
                    param->inputTransName);
            return 1;
         }
         reg_checkAndCorrectDimension(input1TransImage);
      }
      // Read the second transformation
      if(!reg_isAnImageFileName(param->input2TransName))
      {
         affine2Trans=(mat44 *)malloc(sizeof(mat44));
         reg_tool_ReadAffineFile(affine2Trans,param->input2TransName);
      }
      else
      {
         input2TransImage = reg_io_ReadImageFile(param->input2TransName);
         if(input2TransImage==NULL)
         {
            fprintf(stderr, "[NiftyReg ERROR] Error when reading the transformation image: %s\n",
                    param->input2TransName);
            return 1;
         }
         reg_checkAndCorrectDimension(input2TransImage);
      }
      // Check if the two input transformations are affine transformation
      if(affine1Trans!=NULL && affine2Trans!=NULL)
      {
         printf("[NiftyReg] Transformation 2 is an affine parametrisation:\n[NiftyReg] %s\n",
                param->input2TransName);
         *affine1Trans=reg_mat44_mul(affine2Trans,affine1Trans);
         reg_tool_WriteAffineFile(affine1Trans,param->outputTransName);
      }
      else
      {
         // Check if the reference image is required
         if(affine1Trans!=NULL)
         {
            if(!flag->referenceImageFlag)
            {
               fprintf(stderr, "[NiftyReg ERROR] When using an affine transformation (%s),",
                       param->inputTransName);
               fprintf(stderr, " a reference image shoud be specified (-res flag).\n");
               return 1;
572
            }
573 574 575 576 577 578
            referenceImage=reg_io_ReadImageHeader(param->referenceImageName);
            if(referenceImage==NULL)
            {
               fprintf(stderr, "[NiftyReg ERROR] Error when reading the reference image: %s\n",
                       param->referenceImageName);
               return 1;
579
            }
580 581 582 583 584 585 586 587 588 589
            reg_checkAndCorrectDimension(referenceImage);
         }
         else if(input1TransImage->intent_p1==SPLINE_GRID || input1TransImage->intent_p1==SPLINE_VEL_GRID)
         {
            if(!flag->referenceImageFlag)
            {
               fprintf(stderr, "[NiftyReg ERROR] When using an cubic b-spline parametrisation (%s),",
                       param->inputTransName);
               fprintf(stderr, " a reference image shoud be specified (-ref flag).\n");
               return 1;
590
            }
591 592 593 594 595 596
            referenceImage=reg_io_ReadImageHeader(param->referenceImageName);
            if(referenceImage==NULL)
            {
               fprintf(stderr, "[NiftyReg ERROR] Error when reading the reference image: %s\n",
                       param->referenceImageName);
               return 1;
597
            }
598 599 600 601 602 603 604 605 606 607 608
            reg_checkAndCorrectDimension(referenceImage);
         }
         // Read the second reference image if specified
         if(flag->referenceImage2Flag==true)
         {
            referenceImage2=reg_io_ReadImageHeader(param->referenceImage2Name);
            if(referenceImage2==NULL)
            {
               fprintf(stderr, "[NiftyReg ERROR] Error when reading the second reference image: %s\n",
                       param->referenceImage2Name);
               return 1;
609
            }
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
         }
         // Generate the first deformation field
         if(referenceImage!=NULL)
         {
            // The field is created using the reference image space
            output1TransImage=nifti_copy_nim_info(referenceImage);
            output1TransImage->ndim=output1TransImage->dim[0]=5;
            output1TransImage->nt=output1TransImage->dim[4]=1;
            output1TransImage->nu=output1TransImage->dim[5]=output1TransImage->nz>1?3:2;
            output1TransImage->nvox=(size_t)output1TransImage->nx *
                                    output1TransImage->ny * output1TransImage->nz *
                                    output1TransImage->nt * output1TransImage->nu;
            if(referenceImage->datatype!=NIFTI_TYPE_FLOAT32)
            {
               output1TransImage->nbyper=sizeof(float);
               output1TransImage->datatype=NIFTI_TYPE_FLOAT32;
626
            }
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
            printf("[NiftyReg] Transformation 1 is defined in the space of image:\n[NiftyReg] %s\n",
                   referenceImage->fname);
         }
         else
         {
            // The field is created using the input transformation image space
            output1TransImage=nifti_copy_nim_info(input1TransImage);
         }
         output1TransImage->intent_code=NIFTI_INTENT_VECTOR;
         memset(output1TransImage->intent_name, 0, 16);
         strcpy(output1TransImage->intent_name,"NREG_TRANS");
         output1TransImage->intent_p1=DEF_FIELD;
         output1TransImage->data=(void *)calloc
                                 (output1TransImage->nvox,output1TransImage->nbyper);
         if(affine1Trans!=NULL)
         {
            reg_affine_getDeformationField(affine1Trans,output1TransImage);
         }
         else switch(reg_round(input1TransImage->intent_p1))
            {
647
            case SPLINE_GRID:
648 649 650 651 652 653 654 655 656 657 658
               printf("[NiftyReg] Transformation 1 is a spline parametrisation:\n[NiftyReg] %s\n",
                      input1TransImage->fname);
               reg_tools_multiplyValueToImage(output1TransImage,output1TransImage,0.f);
               output1TransImage->intent_p1=DISP_FIELD;
               reg_getDeformationFromDisplacement(output1TransImage);
               reg_spline_getDeformationField(input1TransImage,
                                              output1TransImage,
                                              NULL,
                                              true,
                                              true);
               break;
659
            case DEF_FIELD:
660 661 662 663 664
               printf("[NiftyReg] Transformation 1 is a deformation field:\n[NiftyReg] %s\n",
                      input1TransImage->fname);
               memcpy(output1TransImage->data,input1TransImage->data,
                      output1TransImage->nbyper*output1TransImage->nvox);
               break;
665
            case DISP_FIELD:
666 667 668 669 670 671
               printf("[NiftyReg] Transformation 1 is a displacement field:\n[NiftyReg] %s\n",
                      input1TransImage->fname);
               memcpy(output1TransImage->data,input1TransImage->data,
                      output1TransImage->nbyper*output1TransImage->nvox);
               reg_getDeformationFromDisplacement(output1TransImage);
               break;
672
            case SPLINE_VEL_GRID:
673 674
               printf("[NiftyReg] Transformation 1 is a spline velocity field parametrisation:\n[NiftyReg] %s\n",
                      input1TransImage->fname);
675
               reg_spline_getDefFieldFromVelocityGrid(input1TransImage,
676 677 678 679
                     output1TransImage,
                     false // the number of step is not automatically updated
                                                             );
               break;
680
            case DEF_VEL_FIELD:
681 682 683 684 685 686 687
               printf("[NiftyReg] Transformation 1 is a deformation field velocity:\n[NiftyReg] %s\n",
                      input1TransImage->fname);
               reg_defField_getDeformationFieldFromFlowField(input1TransImage,
                     output1TransImage,
                     false // the number of step is not automatically updated
                                                            );
               break;
688
            case DISP_VEL_FIELD:
689 690 691 692 693 694 695 696
               printf("[NiftyReg] Transformation 1 is a displacement field velocity:\n[NiftyReg] %s\n",
                      input1TransImage->fname);
               reg_getDeformationFromDisplacement(output1TransImage);
               reg_defField_getDeformationFieldFromFlowField(input1TransImage,
                     output1TransImage,
                     false // the number of step is not automatically updated
                                                            );
               break;
697
            default:
698
               fprintf(stderr,"[NiftyReg ERROR] The specified first input transformation type is not recognised: %s\n",
699
                       param->input2TransName);
700
               return 1;
701
            }
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
         if(affine2Trans!=NULL)
         {
            printf("[NiftyReg] Transformation 2 is an affine parametrisation:\n[NiftyReg] %s\n",
                   param->input2TransName);
            // The field is created using the previous image space
            output2TransImage=nifti_copy_nim_info(output1TransImage);
            output2TransImage->intent_code=NIFTI_INTENT_VECTOR;
            memset(output2TransImage->intent_name, 0, 16);
            strcpy(output2TransImage->intent_name,"NREG_TRANS");
            output2TransImage->intent_p1=DEF_FIELD;
            output2TransImage->data=(void *)calloc
                                    (output2TransImage->nvox,output2TransImage->nbyper);
            reg_affine_getDeformationField(affine2Trans,output2TransImage);
            reg_defField_compose(output2TransImage,output1TransImage,NULL);
         }
         else
         {
            switch(reg_round(input2TransImage->intent_p1))
            {
721
            case SPLINE_GRID:
722 723 724 725 726 727 728 729 730
               printf("[NiftyReg] Transformation 2 is a spline parametrisation:\n[NiftyReg] %s\n",
                      input2TransImage->fname);
               reg_spline_getDeformationField(input2TransImage,
                                              output1TransImage,
                                              NULL,
                                              true, // composition
                                              true // b-spline
                                             );
               break;
731
            case DEF_FIELD:
732 733 734 735
               printf("[NiftyReg] Transformation 2 is a deformation field:\n[NiftyReg] %s\n",
                      input2TransImage->fname);
               reg_defField_compose(input2TransImage,output1TransImage,NULL);
               break;
736
            case DISP_FIELD:
737 738 739 740 741
               printf("[NiftyReg] Transformation 2 is a displacement field:\n[NiftyReg] %s\n",
                      input2TransImage->fname);
               reg_getDeformationFromDisplacement(input2TransImage);
               reg_defField_compose(input2TransImage,output1TransImage,NULL);
               break;
742
            case SPLINE_VEL_GRID:
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
               // The field is created using the second reference image space
               if(referenceImage2!=NULL)
               {
                  output2TransImage=nifti_copy_nim_info(referenceImage2);
                  printf("[NiftyReg] Transformation 2 is defined in the space of image:\n[NiftyReg] %s\n",
                         referenceImage2->fname);
               }
               else
               {
                  output2TransImage=nifti_copy_nim_info(output1TransImage);
               }
               output2TransImage->ndim=output2TransImage->dim[0]=5;
               output2TransImage->nt=output2TransImage->dim[4]=1;
               output2TransImage->nu=output2TransImage->dim[5]=output2TransImage->nz>1?3:2;
               output2TransImage->nvox=(size_t)output2TransImage->nx *
                                       output2TransImage->ny * output2TransImage->nz *
                                       output2TransImage->nt * output2TransImage->nu;
               output2TransImage->nbyper=output1TransImage->nbyper;
               output2TransImage->datatype=output1TransImage->datatype;
               output2TransImage->data=(void *)calloc
                                       (output2TransImage->nvox,output2TransImage->nbyper);
               printf("[NiftyReg] Transformation 2 is a spline velocity field parametrisation:\n[NiftyReg] %s\n",
                      input2TransImage->fname);
766
               reg_spline_getDefFieldFromVelocityGrid(input2TransImage,
767 768 769 770 771
                     output2TransImage,
                     false // the number of step is not automatically updated
                                                             );
               reg_defField_compose(output2TransImage,output1TransImage,NULL);
               break;
772
            case DEF_VEL_FIELD:
773 774 775 776 777 778 779 780 781 782 783 784
               printf("[NiftyReg] Transformation 2 is a deformation field velocity:\n[NiftyReg] %s\n",
                      input2TransImage->fname);
               output2TransImage=nifti_copy_nim_info(input2TransImage);
               output2TransImage->intent_p1=DEF_FIELD;
               output2TransImage->data=(void *)calloc
                                       (output2TransImage->nvox,output2TransImage->nbyper);
               reg_defField_getDeformationFieldFromFlowField(input2TransImage,
                     output2TransImage,
                     false // the number of step is not automatically updated
                                                            );
               reg_defField_compose(output2TransImage,output1TransImage,NULL);
               break;
785
            case DISP_VEL_FIELD:
786 787 788 789 790 791 792 793 794 795 796 797 798
               printf("[NiftyReg] Transformation 2 is a displacement field velocity:\n[NiftyReg] %s\n",
                      input2TransImage->fname);
               output2TransImage=nifti_copy_nim_info(input2TransImage);
               output2TransImage->intent_p1=DEF_FIELD;
               output2TransImage->data=(void *)calloc
                                       (output2TransImage->nvox,output2TransImage->nbyper);
               reg_getDeformationFromDisplacement(input2TransImage);
               reg_defField_getDeformationFieldFromFlowField(input2TransImage,
                     output2TransImage,
                     false // the number of step is not automatically updated
                                                            );
               reg_defField_compose(output2TransImage,output1TransImage,NULL);
               break;
799
            default:
800 801 802
               fprintf(stderr,"[NiftyReg ERROR] The specified second input transformation type is not recognised: %s\n",
                       param->input2TransName);
               return 1;
803
            }
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
         }
         // Save the composed transformation
         reg_io_WriteImageFile(output1TransImage,param->outputTransName);
         printf("[NiftyReg] The final deformation field has been saved as:\n[NiftyReg] %s\n",
                param->outputTransName);
      }
      // Free allocated object
      if(affine1Trans!=NULL) free(affine1Trans);
      if(affine2Trans!=NULL) free(affine2Trans);
      if(referenceImage!=NULL) nifti_image_free(referenceImage);
      if(referenceImage2!=NULL) nifti_image_free(referenceImage2);
      if(input1TransImage!=NULL) nifti_image_free(input1TransImage);
      if(input2TransImage!=NULL) nifti_image_free(input2TransImage);
      if(output1TransImage!=NULL) nifti_image_free(output1TransImage);
      if(output2TransImage!=NULL) nifti_image_free(output2TransImage);
   }

   /* **************************************** */
   // Update the SForm matrix of a given image //
   /* **************************************** */
   if(flag->updSFormFlag)
   {
      // Read the input image
      nifti_image *image = reg_io_ReadImageFile(param->inputTransName);
      if(image==NULL)
      {
         fprintf(stderr,"[NiftyReg ERROR] Error when reading the input image: %s\n",
                 param->inputTransName);
         return 1;
      }
      reg_checkAndCorrectDimension(image);
      // Read the affine transformation
      mat44 *affineTransformation = (mat44 *)calloc(1,sizeof(mat44));
      reg_tool_ReadAffineFile(affineTransformation,
                              param->input2TransName);
      //Invert the affine transformation since the flaoting is updated
      *affineTransformation = nifti_mat44_inverse(*affineTransformation);

      // Update the sform
      if(image->sform_code>0)
      {
         image->sto_xyz = reg_mat44_mul(affineTransformation, &(image->sto_xyz));
      }
      else
      {
         image->sform_code = 1;
         image->sto_xyz = reg_mat44_mul(affineTransformation, &(image->qto_xyz));
      }
      image->sto_ijk = nifti_mat44_inverse(image->sto_xyz);

      // Write the output image
      reg_io_WriteImageFile(image,param->outputTransName);
      // Free the allocated image and array
      nifti_image_free(image);
      free(affineTransformation);
   }
   /* ******************************** */
   // Half the provided transformation //
   /* ******************************** */
   if(flag->halfTransFlag)
   {
      // Read the input transformation
      mat44 *affineTrans;
      nifti_image *inputTransImage=NULL;
      if(!reg_isAnImageFileName(param->inputTransName))
      {
         // An affine transformation is considered
         affineTrans=(mat44 *)malloc(sizeof(mat44));
         reg_tool_ReadAffineFile(affineTrans,param->inputTransName);
         // The affine transformation is halfed
         *affineTrans=reg_mat44_logm(affineTrans);
         *affineTrans=reg_mat44_mul(affineTrans,0.5);
         *affineTrans=reg_mat44_expm(affineTrans);
         // The affine transformation is saved
         reg_tool_WriteAffineFile(affineTrans,param->outputTransName);
      }
      else
      {
         // A non-rigid parametrisation is considered
         inputTransImage = reg_io_ReadImageFile(param->inputTransName);
         if(inputTransImage==NULL)
         {
886 887
            fprintf(stderr,"[NiftyReg ERROR] Error when reading the input image: %s\n",
                    param->inputTransName);
Marc Modat's avatar
Marc Modat committed
888
            return 1;
889 890 891 892 893 894 895 896
         }
         reg_checkAndCorrectDimension(inputTransImage);
         switch(reg_round(inputTransImage->intent_p1))
         {
         case SPLINE_GRID:
            reg_getDisplacementFromDeformation(inputTransImage);
            reg_tools_multiplyValueToImage(inputTransImage,inputTransImage,0.5f);
            reg_getDeformationFromDisplacement(inputTransImage);
897
            break;
898 899 900
         case DEF_FIELD:
            reg_getDisplacementFromDeformation(inputTransImage);
            reg_tools_multiplyValueToImage(inputTransImage,inputTransImage,0.5f);
901 902
            reg_getDeformationFromDisplacement(inputTransImage);
            break;
903 904 905 906
         case DISP_FIELD:
            reg_tools_multiplyValueToImage(inputTransImage,inputTransImage,0.5f);
            break;
         case SPLINE_VEL_GRID:
907
            reg_getDisplacementFromDeformation(inputTransImage);
908 909 910 911 912
            reg_tools_multiplyValueToImage(inputTransImage,inputTransImage,0.5f);
            reg_getDeformationFromDisplacement(inputTransImage);
            --inputTransImage->intent_p2;
            if(inputTransImage->num_ext>1)
               --inputTransImage->num_ext;
913
            break;
914 915 916 917 918
         case DEF_VEL_FIELD:
            reg_getDisplacementFromDeformation(inputTransImage);
            reg_tools_multiplyValueToImage(inputTransImage,inputTransImage,0.5f);
            reg_getDeformationFromDisplacement(inputTransImage);
            --inputTransImage->intent_p2;
919
            break;
920 921 922 923 924
         case DISP_VEL_FIELD:
            reg_tools_multiplyValueToImage(inputTransImage,inputTransImage,0.5f);
            --inputTransImage->intent_p2;
            break;
         default:
925 926 927
            fprintf(stderr,"[NiftyReg ERROR] The specified input transformation type is not recognised: %s\n",
                    param->inputTransName);
            return 1;
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
         }
         // Save the image
         reg_io_WriteImageFile(inputTransImage,param->outputTransName);
      }
      // Clear the allocated arrays
      if(affineTrans!=NULL) free(affineTrans);
   }
   /* ******************************************** */
   // Invert the provided non-rigid transformation //
   /* ******************************************** */
   if(flag->invertNRRFlag)
   {
      // Read the provided transformation
      nifti_image *inputTransImage = reg_io_ReadImageFile(param->inputTransName);
      if(inputTransImage==NULL)
      {
         fprintf(stderr,"[NiftyReg ERROR] Error when reading the input image: %s\n",
                 param->inputTransName);
         return 1;
      }
      reg_checkAndCorrectDimension(inputTransImage);
      // Read the provided floating space image
      nifti_image *floatingImage = reg_io_ReadImageFile(param->input2TransName);
      if(floatingImage==NULL)
      {
         fprintf(stderr,"[NiftyReg ERROR] Error when reading the input image: %s\n",
                 param->input2TransName);
         return 1;
      }
      reg_checkAndCorrectDimension(floatingImage);
      // Create a field to store the transformation
      nifti_image *outputTransImage=nifti_copy_nim_info(floatingImage);
      outputTransImage->ndim=outputTransImage->dim[0]=5;
      outputTransImage->nt=outputTransImage->dim[4]=1;
      outputTransImage->nu=outputTransImage->dim[5]=outputTransImage->nz>1?3:2;
      outputTransImage->nvox=(size_t)outputTransImage->nx *
                             outputTransImage->ny * outputTransImage->nz *
                             outputTransImage->nt * outputTransImage->nu;
      outputTransImage->nbyper=inputTransImage->nbyper;
      outputTransImage->datatype=inputTransImage->datatype;
      outputTransImage->intent_code=NIFTI_INTENT_VECTOR;
      memset(outputTransImage->intent_name, 0, 16);
      strcpy(outputTransImage->intent_name,"NREG_TRANS");
      outputTransImage->intent_p1=inputTransImage->intent_p1;
      outputTransImage->intent_p2=inputTransImage->intent_p2;
      outputTransImage->data=(void *)malloc
                             (outputTransImage->nvox*outputTransImage->nbyper);
      // Convert the spline parametrisation into a dense deformation parametrisation
      if(inputTransImage->intent_p1==SPLINE_GRID || inputTransImage->intent_p1==SPLINE_VEL_GRID)
      {
         // Read the reference image
         if(!flag->referenceImageFlag)
         {
            fprintf(stderr, "[NiftyReg ERROR] When using an spline parametrisation transformation (%s),",
                    param->inputTransName);
            fprintf(stderr, " a reference image shoud be specified (-res flag).\n");
            return 1;
         }
         nifti_image *referenceImage=reg_io_ReadImageHeader(param->referenceImageName);
         if(referenceImage==NULL)
         {
            fprintf(stderr, "[NiftyReg ERROR] Error when reading the reference image: %s\n",
                    param->referenceImageName);
            return 1;
         }
         reg_checkAndCorrectDimension(referenceImage);
         // Create a deformation field or a flow field
         nifti_image *tempField=nifti_copy_nim_info(referenceImage);
         tempField->ndim=tempField->dim[0]=5;
         tempField->nt=tempField->dim[4]=1;
         tempField->nu=tempField->dim[5]=tempField->nz>1?3:2;
         tempField->nvox=(size_t)tempField->nx * tempField->ny * tempField->nz *
                         tempField->nt * tempField->nu;
         tempField->nbyper=inputTransImage->nbyper;
         tempField->datatype=inputTransImage->datatype;
         tempField->intent_code=NIFTI_INTENT_VECTOR;
         memset(tempField->intent_name, 0, 16);
         strcpy(tempField->intent_name,"NREG_TRANS");
         tempField->intent_p1=DEF_FIELD;
         if(inputTransImage->intent_p1==SPLINE_VEL_GRID)
         {
            tempField->intent_p1=DEF_VEL_FIELD;
            tempField->intent_p2=inputTransImage->intent_p2;
         }
         tempField->data=(void *)calloc(tempField->nvox,tempField->nbyper);
         // Compute the dense field
         if(inputTransImage->intent_p1==SPLINE_GRID)
            reg_spline_getDeformationField(inputTransImage,
                                           tempField,
                                           NULL,
                                           false,
                                           true);
         else
            reg_spline_getFlowFieldFromVelocityGrid(inputTransImage,
                                                    tempField);
         // The provided transformation file is replaced by the compute dense field
         nifti_image_free(referenceImage);
         nifti_image_free(inputTransImage);
         inputTransImage=tempField;
         tempField=NULL;
      }
      // Invert the provided
      switch(reg_round(inputTransImage->intent_p1))
      {
      case DEF_FIELD:
         reg_defFieldInvert(inputTransImage,outputTransImage,1.0e-6f);
         break;
      case DISP_FIELD:
         reg_getDeformationFromDisplacement(inputTransImage);
         reg_defFieldInvert(inputTransImage,outputTransImage,1.0e-6f);
         reg_getDisplacementFromDeformation(outputTransImage);
         break;
      case DEF_VEL_FIELD:
      {
         // create a temp deformation field containing an identity transformation
         nifti_image *tempField=nifti_copy_nim_info(outputTransImage);
         tempField->intent_p1=DEF_FIELD;
         tempField->data=(void *)calloc(tempField->nvox,tempField->nbyper);
         reg_getDeformationFromDisplacement(tempField);
         reg_getDisplacementFromDeformation(inputTransImage);
         reg_resampleGradient(inputTransImage,
                              outputTransImage,
                              tempField,
                              1,
                              0);
         nifti_image_free(tempField);
         reg_getDeformationFromDisplacement(outputTransImage);
         outputTransImage->intent_p2 *= -1.f;
         break;
      }
      case DISP_VEL_FIELD:
      {
         // create a temp deformation field containing an identity transformation
         nifti_image *tempField=nifti_copy_nim_info(outputTransImage);
         tempField->intent_p1=DEF_FIELD;
         tempField->data=(void *)calloc(tempField->nvox,tempField->nbyper);
         reg_getDeformationFromDisplacement(tempField);
         reg_resampleGradient(inputTransImage,
                              outputTransImage,
                              tempField,
                              1,
                              0);
         nifti_image_free(tempField);
         outputTransImage->intent_p2 *= -1.f;
         break;
      }
      default:
         fprintf(stderr,"[NiftyReg ERROR] The specified input transformation type is not recognised: %s\n",
                 param->inputTransName);
         return 1;
      }
      // Save the inverted transformation
      reg_io_WriteImageFile(outputTransImage,param->outputTransName);
      // Free the allocated images
      nifti_image_free(inputTransImage);
      nifti_image_free(outputTransImage);
   }
   /* ***************************************** */
   // Invert the provided affine transformation //
   /* ***************************************** */
   if(flag->invertAffFlag)
   {
      // Read the affine transformation
      mat44 affineTrans;
      reg_tool_ReadAffineFile(&affineTrans,param->inputTransName);
      // Invert the transformation
      affineTrans = nifti_mat44_inverse(affineTrans);
      // Save the inverted transformation
      reg_tool_WriteAffineFile(&affineTrans,param->outputTransName);
   }
   /* ******************************* */
   // Create an affine transformation //
   /* ******************************* */
   if(flag->makeAffFlag)
   {
      // Create all the required matrices
      mat44 rotationX;
      reg_mat44_eye(&rotationX);
      mat44 translation;
      reg_mat44_eye(&translation);
      mat44 rotationY;
      reg_mat44_eye(&rotationY);
      mat44 rotationZ;
      reg_mat44_eye(&rotationZ);
      mat44 scaling;
      reg_mat44_eye(&scaling);
      mat44 shearing;
      reg_mat44_eye(&shearing);
      // Set up the rotation matrix along the YZ plane
      rotationX.m[1][1]=cosf(param->affTransParam[0]);
      rotationX.m[1][2]=-sinf(param->affTransParam[0]);
      rotationX.m[2][1]=sinf(param->affTransParam[0]);
      rotationX.m[2][2]=cosf(param->affTransParam[0]);
      // Set up the rotation matrix along the XZ plane
      rotationY.m[0][0]=cosf(param->affTransParam[1]);
      rotationY.m[0][2]=-sinf(param->affTransParam[1]);
      rotationY.m[2][0]=sinf(param->affTransParam[1]);
      rotationY.m[2][2]=cosf(param->affTransParam[1]);
      // Set up the rotation matrix along the XY plane
      rotationZ.m[0][0]=cosf(param->affTransParam[2]);
      rotationZ.m[0][1]=-sinf(param->affTransParam[2]);
      rotationZ.m[1][0]=sinf(param->affTransParam[2]);
      rotationZ.m[1][1]=cosf(param->affTransParam[2]);
      // Set up the translation matrix
      translation.m[0][3]=param->affTransParam[3];
      translation.m[1][3]=param->affTransParam[4];
      translation.m[2][3]=param->affTransParam[5];
      // Set up the scaling matrix
      scaling.m[0][0]=param->affTransParam[6];
      scaling.m[1][1]=param->affTransParam[7];
      scaling.m[2][2]=param->affTransParam[8];
      // Set up the shearing matrix
      shearing.m[1][0]=param->affTransParam[9];
      shearing.m[2][0]=param->affTransParam[10];
      shearing.m[2][1]=param->affTransParam[11];
      // Combine all the transformations
      mat44 affine=reg_mat44_mul(&rotationY,&rotationZ);
      affine=reg_mat44_mul(&rotationX,&affine);
      affine=reg_mat44_mul(&scaling,&affine);
      affine=reg_mat44_mul(&shearing,&affine);
      affine=reg_mat44_mul(&translation,&affine);
      // Save the new matrix
      reg_tool_WriteAffineFile(&affine,param->outputTransName);
   }
   /* ************************************************* */
   // Extract the rigid component from an affine matrix //
   /* ************************************************* */
   if(flag->aff2rigFlag)
   {
      mat44 affine;
      reg_tool_ReadAffineFile(&affine,param->inputTransName);
      // Compute the orthonormal matrix
      float qb,qc,qd,qx,qy,qz,dx,dy,dz,qfac;
      nifti_mat44_to_quatern(affine,&qb,&qc,&qd,&qx,&qy,&qz,&dx,&dy,&dz,&qfac);
1162
      affine = nifti_quatern_to_mat44(qb,qc,qd,qx,qy,qz,1.f,1.f,1.f,qfac);
1163 1164 1165 1166 1167
      reg_tool_WriteAffineFile(&affine, param->outputTransName);
   }
   // Free allocated object
   free(param);
   free(flag);
1168

1169
   return 0;
Marc Modat's avatar
Marc Modat committed
1170 1171 1172
}

#endif