_reg_base.cpp 44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/**
 * @file _reg_base.cpp
 * @author Marc Modat
 * @date 15/11/2012
 *
 * Copyright (c) 2012, University College London. All rights reserved.
 * Centre for Medical Image Computing (CMIC)
 * See the LICENSE.txt file in the nifty_reg root folder
 *
 */

#ifndef _REG_BASE_CPP
#define _REG_BASE_CPP

#include "_reg_base.h"

/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template <class T>
reg_base<T>::reg_base(int refTimePoint,int floTimePoint)
{
22 23 24 25 26 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
   this->optimiser=NULL;
   this->maxiterationNumber=300;
   this->optimiseX=true;
   this->optimiseY=true;
   this->optimiseZ=true;
   this->perturbationNumber=0;
   this->useConjGradient=true;
   this->useApproxGradient=false;

   this->measure_ssd=NULL;
   this->measure_kld=NULL;
   this->measure_dti=NULL;
   this->measure_lncc=NULL;
   this->measure_nmi=NULL;
   this->measure_multichannel_nmi=NULL;

   this->similarityWeight=0.; // is automatically set depending of the penalty term weights

   this->executableName=(char *)"NiftyReg BASE";
   this->referenceTimePoint=refTimePoint;
   this->floatingTimePoint=floTimePoint;
   this->inputReference=NULL; // pointer to external
   this->inputFloating=NULL; // pointer to external
   this->maskImage=NULL; // pointer to external
   this->affineTransformation=NULL;  // pointer to external
   this->referenceMask=NULL;
   this->referenceSmoothingSigma=0.;
   this->floatingSmoothingSigma=0.;
   this->referenceThresholdUp=new float[this->referenceTimePoint];
   this->referenceThresholdLow=new float[this->referenceTimePoint];
   this->floatingThresholdUp=new float[this->floatingTimePoint];
   this->floatingThresholdLow=new float[this->floatingTimePoint];
   for(int i=0; i<this->referenceTimePoint; i++)
   {
      this->referenceThresholdUp[i]=std::numeric_limits<T>::max();
      this->referenceThresholdLow[i]=-std::numeric_limits<T>::max();
   }
   for(int i=0; i<this->floatingTimePoint; i++)
   {
      this->floatingThresholdUp[i]=std::numeric_limits<T>::max();
      this->floatingThresholdLow[i]=-std::numeric_limits<T>::max();
   }
   this->warpedPaddingValue=std::numeric_limits<T>::quiet_NaN();
   this->levelNumber=3;
   this->levelToPerform=0;
   this->gradientSmoothingSigma=0;
   this->verbose=true;
   this->usePyramid=true;
   this->forwardJacobianMatrix=NULL;


   this->initialised=false;
   this->referencePyramid=NULL;
   this->floatingPyramid=NULL;
   this->maskPyramid=NULL;
   this->activeVoxelNumber=NULL;
   this->currentReference=NULL;
   this->currentFloating=NULL;
   this->currentMask=NULL;
   this->warped=NULL;
   this->deformationFieldImage=NULL;
   this->warpedGradientImage=NULL;
   this->voxelBasedMeasureGradientImage=NULL;

   this->interpolation=1;

   this->funcProgressCallback=NULL;
   this->paramsProgressCallback=NULL;
90 91

#ifndef NDEBUG
92
   reg_print_fct_debug("reg_base<T>::reg_base");
93 94 95 96 97 98 99
#endif
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template <class T>
reg_base<T>::~reg_base()
{
100 101 102 103 104 105 106 107 108 109 110 111 112 113
   this->ClearWarped();
   this->ClearWarpedGradient();
   this->ClearDeformationField();
   this->ClearVoxelBasedMeasureGradient();
   if(this->referencePyramid!=NULL)
   {
      if(this->usePyramid)
      {
         for(unsigned int i=0; i<this->levelToPerform; i++)
         {
            if(referencePyramid[i]!=NULL)
            {
               nifti_image_free(referencePyramid[i]);
               referencePyramid[i]=NULL;
114
            }
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
         }
      }
      else
      {
         if(referencePyramid[0]!=NULL)
         {
            nifti_image_free(referencePyramid[0]);
            referencePyramid[0]=NULL;
         }
      }
      free(referencePyramid);
      referencePyramid=NULL;
   }
   if(this->maskPyramid!=NULL)
   {
      if(this->usePyramid)
      {
         for(unsigned int i=0; i<this->levelToPerform; i++)
         {
            if(this->maskPyramid[i]!=NULL)
            {
               free(this->maskPyramid[i]);
               this->maskPyramid[i]=NULL;
138
            }
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
         }
      }
      else
      {
         if(this->maskPyramid[0]!=NULL)
         {
            free(this->maskPyramid[0]);
            this->maskPyramid[0]=NULL;
         }
      }
      free(this->maskPyramid);
      maskPyramid=NULL;
   }
   if(this->floatingPyramid!=NULL)
   {
      if(this->usePyramid)
      {
         for(unsigned int i=0; i<this->levelToPerform; i++)
         {
            if(floatingPyramid[i]!=NULL)
            {
               nifti_image_free(floatingPyramid[i]);
               floatingPyramid[i]=NULL;
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
         }
      }
      else
      {
         if(floatingPyramid[0]!=NULL)
         {
            nifti_image_free(floatingPyramid[0]);
            floatingPyramid[0]=NULL;
         }
      }
      free(floatingPyramid);
      floatingPyramid=NULL;
   }
   if(this->activeVoxelNumber!=NULL)
   {
      free(activeVoxelNumber);
      this->activeVoxelNumber=NULL;
   }
   if(this->referenceThresholdUp!=NULL)
   {
      delete []this->referenceThresholdUp;
      this->referenceThresholdUp=NULL;
   }
   if(this->referenceThresholdLow!=NULL)
   {
      delete []this->referenceThresholdLow;
      this->referenceThresholdLow=NULL;
   }
   if(this->floatingThresholdUp!=NULL)
   {
      delete []this->floatingThresholdUp;
      this->floatingThresholdUp=NULL;
   }
   if(this->floatingThresholdLow!=NULL)
   {
      delete []this->floatingThresholdLow;
      this->floatingThresholdLow=NULL;
   }
   if(this->activeVoxelNumber!=NULL)
   {
      delete []this->activeVoxelNumber;
      this->activeVoxelNumber=NULL;
   }
   if(this->optimiser!=NULL)
   {
      delete this->optimiser;
      this->optimiser=NULL;
   }

   if(this->measure_nmi!=NULL)
      delete this->measure_nmi;
   if(this->measure_multichannel_nmi!=NULL)
      delete this->measure_multichannel_nmi;
   if(this->measure_ssd!=NULL)
      delete this->measure_ssd;
   if(this->measure_kld!=NULL)
      delete this->measure_kld;
   if(this->measure_dti!=NULL)
      delete this->measure_dti;
   if(this->measure_lncc!=NULL)
      delete this->measure_lncc;
224
#ifndef NDEBUG
225
   reg_print_fct_debug("reg_base<T>::~reg_base");
226 227 228 229 230 231 232
#endif
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::SetReferenceImage(nifti_image *r)
{
233
   this->inputReference = r;
234
#ifndef NDEBUG
235
   reg_print_fct_debug("reg_base<T>::SetReferenceImage");
236
#endif
237 238 239 240 241
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::SetFloatingImage(nifti_image *f)
{
242
   this->inputFloating = f;
243
#ifndef NDEBUG
244
   reg_print_fct_debug("reg_base<T>::SetFloatingImage");
245
#endif
246 247 248
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
249
void reg_base<T>::SetMaximalIterationNumber(unsigned int iter)
250
{
251
   this->maxiterationNumber=iter;
252
#ifndef NDEBUG
253
   reg_print_fct_debug("reg_base<T>::SetMaximalIterationNumber");
254
#endif
255 256 257 258 259
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::SetReferenceMask(nifti_image *m)
{
260
   this->maskImage = m;
261
#ifndef NDEBUG
262
   reg_print_fct_debug("reg_base<T>::SetReferenceMask");
263
#endif
264 265 266 267 268
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::SetAffineTransformation(mat44 *a)
{
269
   this->affineTransformation=a;
270
#ifndef NDEBUG
271
   reg_print_fct_debug("reg_base<T>::SetAffineTransformation");
272
#endif
273 274 275 276 277
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::SetReferenceSmoothingSigma(T s)
{
278
   this->referenceSmoothingSigma = s;
279
#ifndef NDEBUG
280
   reg_print_fct_debug("reg_base<T>::SetReferenceSmoothingSigma");
281
#endif
282 283 284 285 286
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::SetFloatingSmoothingSigma(T s)
{
287
   this->floatingSmoothingSigma = s;
288
#ifndef NDEBUG
289
   reg_print_fct_debug("reg_base<T>::SetFloatingSmoothingSigma");
290
#endif
291 292 293 294 295
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::SetReferenceThresholdUp(unsigned int i, T t)
{
296
   this->referenceThresholdUp[i] = t;
297
#ifndef NDEBUG
298
   reg_print_fct_debug("reg_base<T>::SetReferenceThresholdUp");
299
#endif
300 301 302 303 304
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::SetReferenceThresholdLow(unsigned int i, T t)
{
305
   this->referenceThresholdLow[i] = t;
306
#ifndef NDEBUG
307
   reg_print_fct_debug("reg_base<T>::SetReferenceThresholdLow");
308
#endif
309 310 311 312 313
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::SetFloatingThresholdUp(unsigned int i, T t)
{
314
   this->floatingThresholdUp[i] = t;
315
#ifndef NDEBUG
316
   reg_print_fct_debug("reg_base<T>::SetFloatingThresholdUp");
317
#endif
318 319 320 321 322
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::SetFloatingThresholdLow(unsigned int i, T t)
{
323
   this->floatingThresholdLow[i] = t;
324
#ifndef NDEBUG
325
   reg_print_fct_debug("reg_base<T>::SetFloatingThresholdLow");
326
#endif
327 328 329 330 331
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::SetWarpedPaddingValue(T p)
{
332
   this->warpedPaddingValue = p;
333
#ifndef NDEBUG
334
   reg_print_fct_debug("reg_base<T>::SetWarpedPaddingValue");
335
#endif
336 337 338 339 340
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::SetLevelNumber(unsigned int l)
{
341
   this->levelNumber = l;
342
#ifndef NDEBUG
343
   reg_print_fct_debug("reg_base<T>::SetLevelNumber");
344
#endif
345 346 347 348 349
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::SetLevelToPerform(unsigned int l)
{
350
   this->levelToPerform = l;
351
#ifndef NDEBUG
352
   reg_print_fct_debug("reg_base<T>::SetLevelToPerform");
353
#endif
354 355 356 357 358
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::SetGradientSmoothingSigma(T g)
{
359
   this->gradientSmoothingSigma = g;
360
#ifndef NDEBUG
361
   reg_print_fct_debug("reg_base<T>::SetGradientSmoothingSigma");
362
#endif
363 364 365 366 367
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::UseConjugateGradient()
{
368 369
   this->useConjGradient = true;
   this->useApproxGradient = false;
370
#ifndef NDEBUG
371
   reg_print_fct_debug("reg_base<T>::UseConjugateGradient");
372
#endif
373 374 375 376 377
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::DoNotUseConjugateGradient()
{
378 379
   this->useConjGradient = false;
   this->useApproxGradient = false;
380
#ifndef NDEBUG
381
   reg_print_fct_debug("reg_base<T>::DoNotUseConjugateGradient");
382
#endif
383 384 385 386 387
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::UseApproximatedGradient()
{
388 389
   this->useConjGradient = false;
   this->useApproxGradient = true;
390
#ifndef NDEBUG
391
   reg_print_fct_debug("reg_base<T>::UseApproximatedGradient");
392
#endif
393 394 395 396 397
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::DoNotUseApproximatedGradient()
{
398 399
   this->useConjGradient = true;
   this->useApproxGradient = false;
400
#ifndef NDEBUG
401
   reg_print_fct_debug("reg_base<T>::DoNotUseApproximatedGradient");
402
#endif
403 404 405 406 407
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::PrintOutInformation()
{
408
   this->verbose = true;
409
#ifndef NDEBUG
410
   reg_print_fct_debug("reg_base<T>::PrintOutInformation");
411
#endif
412 413 414 415 416
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::DoNotPrintOutInformation()
{
417
   this->verbose = false;
418
#ifndef NDEBUG
419
   reg_print_fct_debug("reg_base<T>::DoNotPrintOutInformation");
420
#endif
421 422 423 424 425
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::DoNotUsePyramidalApproach()
{
426
   this->usePyramid=false;
427
#ifndef NDEBUG
428
   reg_print_fct_debug("reg_base<T>::DoNotUsePyramidalApproach");
429
#endif
430 431 432 433 434
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::UseNeareatNeighborInterpolation()
{
435
   this->interpolation=0;
436
#ifndef NDEBUG
437
   reg_print_fct_debug("reg_base<T>::UseNeareatNeighborInterpolation");
438
#endif
439 440 441 442 443
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::UseLinearInterpolation()
{
444
   this->interpolation=1;
445
#ifndef NDEBUG
446
   reg_print_fct_debug("reg_base<T>::UseLinearInterpolation");
447
#endif
448 449 450 451 452
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::UseCubicSplineInterpolation()
{
453
   this->interpolation=3;
454
#ifndef NDEBUG
455
   reg_print_fct_debug("reg_base<T>::UseCubicSplineInterpolation");
456
#endif
457 458
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
459
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
460 461 462
template <class T>
void reg_base<T>::ClearCurrentInputImage()
{
463 464 465
   this->currentReference=NULL;
   this->currentMask=NULL;
   this->currentFloating=NULL;
466
#ifndef NDEBUG
467
   reg_print_fct_debug("reg_base<T>::ClearCurrentInputImage");
468
#endif
469 470
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
471
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
472 473 474
template <class T>
void reg_base<T>::AllocateWarped()
{
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
   if(this->currentReference==NULL)
   {
      fprintf(stderr, "[NiftyReg ERROR] The reference image is not defined\n");
      reg_exit(1);
   }
   reg_base<T>::ClearWarped();
   this->warped = nifti_copy_nim_info(this->currentReference);
   this->warped->dim[0]=this->warped->ndim=this->currentFloating->ndim;
   this->warped->dim[4]=this->warped->nt=this->currentFloating->nt;
   this->warped->pixdim[4]=this->warped->dt=1.0;
   this->warped->nvox =
      (size_t)this->warped->nx *
      (size_t)this->warped->ny *
      (size_t)this->warped->nz *
      (size_t)this->warped->nt;
490 491
   this->warped->scl_slope=1.f;
   this->warped->scl_inter=0.f;
492 493 494
   this->warped->datatype = this->currentFloating->datatype;
   this->warped->nbyper = this->currentFloating->nbyper;
   this->warped->data = (void *)calloc(this->warped->nvox, this->warped->nbyper);
495
#ifndef NDEBUG
496
   reg_print_fct_debug("reg_base<T>::AllocateWarped");
497
#endif
498 499 500 501 502
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template <class T>
void reg_base<T>::ClearWarped()
{
503 504 505
   if(this->warped!=NULL)
      nifti_image_free(this->warped);
   this->warped=NULL;
506
#ifndef NDEBUG
507
   reg_print_fct_debug("reg_base<T>::ClearWarped");
508
#endif
509 510 511 512 513 514
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template <class T>
void reg_base<T>::AllocateDeformationField()
{
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
   if(this->currentReference==NULL)
   {
      fprintf(stderr, "[NiftyReg ERROR] The reference image is not defined\n");
      reg_exit(1);
   }
   reg_base<T>::ClearDeformationField();
   this->deformationFieldImage = nifti_copy_nim_info(this->currentReference);
   this->deformationFieldImage->dim[0]=this->deformationFieldImage->ndim=5;
   this->deformationFieldImage->dim[1]=this->deformationFieldImage->nx=this->currentReference->nx;
   this->deformationFieldImage->dim[2]=this->deformationFieldImage->ny=this->currentReference->ny;
   this->deformationFieldImage->dim[3]=this->deformationFieldImage->nz=this->currentReference->nz;
   this->deformationFieldImage->dim[4]=this->deformationFieldImage->nt=1;
   this->deformationFieldImage->pixdim[4]=this->deformationFieldImage->dt=1.0;
   if(this->currentReference->nz==1)
      this->deformationFieldImage->dim[5]=this->deformationFieldImage->nu=2;
   else this->deformationFieldImage->dim[5]=this->deformationFieldImage->nu=3;
   this->deformationFieldImage->pixdim[5]=this->deformationFieldImage->du=1.0;
   this->deformationFieldImage->dim[6]=this->deformationFieldImage->nv=1;
   this->deformationFieldImage->pixdim[6]=this->deformationFieldImage->dv=1.0;
   this->deformationFieldImage->dim[7]=this->deformationFieldImage->nw=1;
   this->deformationFieldImage->pixdim[7]=this->deformationFieldImage->dw=1.0;
   this->deformationFieldImage->nvox =
      (size_t)this->deformationFieldImage->nx *
      (size_t)this->deformationFieldImage->ny *
      (size_t)this->deformationFieldImage->nz *
      (size_t)this->deformationFieldImage->nt *
      (size_t)this->deformationFieldImage->nu;
   this->deformationFieldImage->nbyper = sizeof(T);
   if(sizeof(T)==sizeof(float))
      this->deformationFieldImage->datatype = NIFTI_TYPE_FLOAT32;
   else this->deformationFieldImage->datatype = NIFTI_TYPE_FLOAT64;
   this->deformationFieldImage->data = (void *)calloc(this->deformationFieldImage->nvox,
                                       this->deformationFieldImage->nbyper);
   this->deformationFieldImage->intent_code=NIFTI_INTENT_VECTOR;
   memset(this->deformationFieldImage->intent_name, 0, 16);
   strcpy(this->deformationFieldImage->intent_name,"NREG_TRANS");
   this->deformationFieldImage->intent_p1=DEF_FIELD;

   if(this->measure_dti!=NULL)
      this->forwardJacobianMatrix=(mat33 *)malloc(
                                     this->deformationFieldImage->nx *
                                     this->deformationFieldImage->ny *
                                     this->deformationFieldImage->nz *
                                     sizeof(mat33));
559
#ifndef NDEBUG
560
   reg_print_fct_debug("reg_base<T>::AllocateDeformationField");
561
#endif
562 563 564 565 566
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template <class T>
void reg_base<T>::ClearDeformationField()
{
567 568 569 570 571 572 573 574
   if(this->deformationFieldImage!=NULL)
   {
      nifti_image_free(this->deformationFieldImage);
      this->deformationFieldImage=NULL;
   }
   if(this->forwardJacobianMatrix!=NULL)
      free(this->forwardJacobianMatrix);
   this->forwardJacobianMatrix=NULL;
575
#ifndef NDEBUG
576
   reg_print_fct_debug("reg_base<T>::ClearDeformationField");
577
#endif
578 579 580 581 582
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template <class T>
void reg_base<T>::AllocateWarpedGradient()
{
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
   if(this->deformationFieldImage==NULL)
   {
      fprintf(stderr, "[NiftyReg ERROR] The deformation field image is not defined\n");
      reg_exit(1);
   }
   reg_base<T>::ClearWarpedGradient();
   this->warpedGradientImage = nifti_copy_nim_info(this->deformationFieldImage);
   this->warpedGradientImage->dim[0]=this->warpedGradientImage->ndim=5;
   this->warpedGradientImage->nt = this->warpedGradientImage->dim[4] = this->currentFloating->nt;
   this->warpedGradientImage->nvox =
      (size_t)this->warpedGradientImage->nx *
      (size_t)this->warpedGradientImage->ny *
      (size_t)this->warpedGradientImage->nz *
      (size_t)this->warpedGradientImage->nt *
      (size_t)this->warpedGradientImage->nu;
   this->warpedGradientImage->data = (void *)calloc(this->warpedGradientImage->nvox,
                                     this->warpedGradientImage->nbyper);
600
#ifndef NDEBUG
601
   reg_print_fct_debug("reg_base<T>::AllocateWarpedGradient");
602
#endif
603 604 605 606 607
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template <class T>
void reg_base<T>::ClearWarpedGradient()
{
608 609 610 611 612
   if(this->warpedGradientImage!=NULL)
   {
      nifti_image_free(this->warpedGradientImage);
      this->warpedGradientImage=NULL;
   }
613
#ifndef NDEBUG
614
   reg_print_fct_debug("reg_base<T>::ClearWarpedGradient");
615
#endif
616 617 618 619 620
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template <class T>
void reg_base<T>::AllocateVoxelBasedMeasureGradient()
{
621 622 623 624 625 626 627 628 629
   if(this->deformationFieldImage==NULL)
   {
      fprintf(stderr, "[NiftyReg ERROR] The deformation field image is not defined\n");
      reg_exit(1);
   }
   reg_base<T>::ClearVoxelBasedMeasureGradient();
   this->voxelBasedMeasureGradientImage = nifti_copy_nim_info(this->deformationFieldImage);
   this->voxelBasedMeasureGradientImage->data = (void *)calloc(this->voxelBasedMeasureGradientImage->nvox,
         this->voxelBasedMeasureGradientImage->nbyper);
630
#ifndef NDEBUG
631
   reg_print_fct_debug("reg_base<T>::AllocateVoxelBasedMeasureGradient");
632
#endif
633 634 635 636 637
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template <class T>
void reg_base<T>::ClearVoxelBasedMeasureGradient()
{
638 639 640 641 642
   if(this->voxelBasedMeasureGradientImage!=NULL)
   {
      nifti_image_free(this->voxelBasedMeasureGradientImage);
      this->voxelBasedMeasureGradientImage=NULL;
   }
643
#ifndef NDEBUG
644
   reg_print_fct_debug("reg_base<T>::ClearVoxelBasedMeasureGradient");
645
#endif
646 647 648 649 650
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::CheckParameters()
{
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
   // CHECK THAT BOTH INPUT IMAGES ARE DEFINED
   if(this->inputReference==NULL)
   {
      fprintf(stderr,"[NiftyReg ERROR] No reference image has been defined.\n");
      reg_exit(1);
   }
   if(this->inputFloating==NULL)
   {
      fprintf(stderr,"[NiftyReg ERROR] No floating image has been defined.\n");
      reg_exit(1);
   }

   // CHECK THE MASK DIMENSION IF IT IS DEFINED
   if(this->maskImage!=NULL)
   {
      if(this->inputReference->nx != this->maskImage->nx ||
            this->inputReference->ny != this->maskImage->ny ||
            this->inputReference->nz != this->maskImage->nz )
      {
         printf("x: %i %i\n",this->inputReference->nx, this->maskImage->nx);
         printf("y: %i %i\n",this->inputReference->ny, this->maskImage->ny);
         printf("z: %i %i\n",this->inputReference->nz, this->maskImage->nz);
         fprintf(stderr,"[NiftyReg ERROR] The mask image has different x, y or z dimension than the reference image.\n");
         reg_exit(1);
      }
   }

   // CHECK THE NUMBER OF LEVEL TO PERFORM
   if(this->levelToPerform>0)
   {
      this->levelToPerform=this->levelToPerform<this->levelNumber?this->levelToPerform:this->levelNumber;
   }
   else this->levelToPerform=this->levelNumber;
   if(this->levelToPerform==0 || this->levelToPerform>this->levelNumber)
      this->levelToPerform=this->levelNumber;
686

687
#ifndef NDEBUG
688
   reg_print_fct_debug("reg_base<T>::CheckParameters");
689 690 691 692 693 694
#endif
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::InitialiseSimilarity()
{
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
   // SET THE DEFAULT MEASURE OF SIMILARITY IF NONE HAS BEEN SET
   if(this->measure_nmi==NULL &&
         this->measure_ssd==NULL &&
         this->measure_dti==NULL &&
         this->measure_lncc==NULL &&
         this->measure_lncc==NULL)
   {
      this->measure_nmi=new reg_nmi;
      for(int i=0; i<this->inputReference->nt; ++i)
         this->measure_nmi->SetActiveTimepoint(i);
   }
   if(this->measure_nmi!=NULL)
      this->measure_nmi->InitialiseMeasure(this->currentReference,
                                           this->currentFloating,
                                           this->currentMask,
                                           this->warped,
                                           this->warpedGradientImage,
                                           this->voxelBasedMeasureGradientImage
                                          );

   if(this->measure_multichannel_nmi!=NULL)
      this->measure_multichannel_nmi->InitialiseMeasure(this->currentReference,
            this->currentFloating,
            this->currentMask,
            this->warped,
            this->warpedGradientImage,
            this->voxelBasedMeasureGradientImage
                                                       );

   if(this->measure_ssd!=NULL)
      this->measure_ssd->InitialiseMeasure(this->currentReference,
                                           this->currentFloating,
                                           this->currentMask,
                                           this->warped,
                                           this->warpedGradientImage,
                                           this->voxelBasedMeasureGradientImage
                                          );

   if(this->measure_kld!=NULL)
      this->measure_kld->InitialiseMeasure(this->currentReference,
                                           this->currentFloating,
                                           this->currentMask,
                                           this->warped,
                                           this->warpedGradientImage,
                                           this->voxelBasedMeasureGradientImage
                                          );

   if(this->measure_lncc!=NULL)
      this->measure_lncc->InitialiseMeasure(this->currentReference,
                                            this->currentFloating,
                                            this->currentMask,
                                            this->warped,
                                            this->warpedGradientImage,
                                            this->voxelBasedMeasureGradientImage
                                           );

   if(this->measure_dti!=NULL)
      this->measure_dti->InitialiseMeasure(this->currentReference,
                                           this->currentFloating,
                                           this->currentMask,
                                           this->warped,
                                           this->warpedGradientImage,
                                           this->voxelBasedMeasureGradientImage
                                          );
759 760

#ifndef NDEBUG
761
   reg_print_fct_debug("reg_base<T>::InitialiseSimilarity");
762
#endif
763
   return;
764 765 766
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
767
void reg_base<T>::Initialise()
768
{
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 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
   if(this->initialised) return;

   this->CheckParameters();

   // CREATE THE PYRAMIDE IMAGES
   if(this->usePyramid)
   {
      this->referencePyramid = (nifti_image **)malloc(this->levelToPerform*sizeof(nifti_image *));
      this->floatingPyramid = (nifti_image **)malloc(this->levelToPerform*sizeof(nifti_image *));
      this->maskPyramid = (int **)malloc(this->levelToPerform*sizeof(int *));
      this->activeVoxelNumber= (int *)malloc(this->levelToPerform*sizeof(int));
   }
   else
   {
      this->referencePyramid = (nifti_image **)malloc(sizeof(nifti_image *));
      this->floatingPyramid = (nifti_image **)malloc(sizeof(nifti_image *));
      this->maskPyramid = (int **)malloc(sizeof(int *));
      this->activeVoxelNumber= (int *)malloc(sizeof(int));
   }

   // FINEST LEVEL OF REGISTRATION
   if(this->usePyramid)
   {
      reg_createImagePyramid<T>(this->inputReference, this->referencePyramid, this->levelNumber, this->levelToPerform);
      reg_createImagePyramid<T>(this->inputFloating, this->floatingPyramid, this->levelNumber, this->levelToPerform);
      if (this->maskImage!=NULL)
         reg_createMaskPyramid<T>(this->maskImage, this->maskPyramid, this->levelNumber, this->levelToPerform, this->activeVoxelNumber);
      else
      {
         for(unsigned int l=0; l<this->levelToPerform; ++l)
         {
            this->activeVoxelNumber[l]=this->referencePyramid[l]->nx*this->referencePyramid[l]->ny*this->referencePyramid[l]->nz;
            this->maskPyramid[l]=(int *)calloc(activeVoxelNumber[l],sizeof(int));
         }
      }
   }
   else
   {
      reg_createImagePyramid<T>(this->inputReference, this->referencePyramid, 1, 1);
      reg_createImagePyramid<T>(this->inputFloating, this->floatingPyramid, 1, 1);
      if (this->maskImage!=NULL)
         reg_createMaskPyramid<T>(this->maskImage, this->maskPyramid, 1, 1, this->activeVoxelNumber);
      else
      {
         this->activeVoxelNumber[0]=this->referencePyramid[0]->nx*this->referencePyramid[0]->ny*this->referencePyramid[0]->nz;
         this->maskPyramid[0]=(int *)calloc(activeVoxelNumber[0],sizeof(int));
      }
   }

   unsigned int pyramidalLevelNumber=1;
   if(this->usePyramid) pyramidalLevelNumber=this->levelToPerform;

   // SMOOTH THE INPUT IMAGES IF REQUIRED
   for(unsigned int l=0; l<this->levelToPerform; l++)
   {
      if(this->referenceSmoothingSigma!=0.0)
      {
         bool *active = new bool[this->referencePyramid[l]->nt];
         float *sigma = new float[this->referencePyramid[l]->nt];
         active[0]=true;
         for(int i=1; i<this->referencePyramid[l]->nt; ++i)
            active[i]=false;
         sigma[0]=this->referenceSmoothingSigma;
         reg_tools_kernelConvolution(this->referencePyramid[l], sigma, 0, NULL, active);
         delete []active;
         delete []sigma;
      }
      if(this->floatingSmoothingSigma!=0.0)
      {
         // Only the first image is smoothed
         bool *active = new bool[this->floatingPyramid[l]->nt];
         float *sigma = new float[this->floatingPyramid[l]->nt];
         active[0]=true;
         for(int i=1; i<this->floatingPyramid[l]->nt; ++i)
            active[i]=false;
         sigma[0]=this->floatingSmoothingSigma;
         reg_tools_kernelConvolution(this->floatingPyramid[l], sigma, 0, NULL, active);
         delete []active;
         delete []sigma;
      }
   }

   // THRESHOLD THE INPUT IMAGES IF REQUIRED
   for(unsigned int l=0; l<pyramidalLevelNumber; l++)
   {
      reg_thresholdImage<T>(this->referencePyramid[l],this->referenceThresholdLow[0], this->referenceThresholdUp[0]);
      reg_thresholdImage<T>(this->floatingPyramid[l],this->referenceThresholdLow[0], this->referenceThresholdUp[0]);
   }

   this->initialised=true;
859
#ifndef NDEBUG
860
   reg_print_fct_debug("reg_base<T>::Initialise");
861 862 863 864 865 866 867
#endif
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template <class T>
void reg_base<T>::SetOptimiser()
{
868 869 870
   if(this->useConjGradient)
      this->optimiser=new reg_conjugateGradient<T>();
   else this->optimiser=new reg_optimiser<T>();
871
#ifndef NDEBUG
872
   reg_print_fct_debug("reg_base<T>::SetOptimiser");
873
#endif
874 875 876 877 878 879
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template <class T>
double reg_base<T>::ComputeSimilarityMeasure()
{
880 881 882
   double measure=0.;
   if(this->measure_nmi!=NULL)
      measure += this->measure_nmi->GetSimilarityMeasureValue();
883

884 885
   if(this->measure_multichannel_nmi!=NULL)
      measure += this->measure_multichannel_nmi->GetSimilarityMeasureValue();
886

887 888
   if(this->measure_ssd!=NULL)
      measure += this->measure_ssd->GetSimilarityMeasureValue();
889

890 891
   if(this->measure_kld!=NULL)
      measure += this->measure_kld->GetSimilarityMeasureValue();
892

893 894
   if(this->measure_lncc!=NULL)
      measure += this->measure_lncc->GetSimilarityMeasureValue();
895

896 897
   if(this->measure_dti!=NULL)
      measure += this->measure_dti->GetSimilarityMeasureValue();
898

899
#ifndef NDEBUG
900
   reg_print_fct_debug("reg_base<T>::ComputeSimilarityMeasure");
901
#endif
902
   return double(this->similarityWeight) * measure;
903 904 905 906 907 908
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template <class T>
void reg_base<T>::GetVoxelBasedGradient()
{
909
   // The intensity gradient is first computed
910 911 912 913 914 915 916 917 918 919 920 921
//    if(this->measure_dti!=NULL){
//        reg_getImageGradient(this->currentFloating,
//                             this->warpedGradientImage,
//                             this->deformationFieldImage,
//                             this->currentMask,
//                             this->interpolation,
//                             this->warpedPaddingValue,
//                             this->measure_dti->GetActiveTimepoints(),
//		 					   this->forwardJacobianMatrix,
//							   this->warped);
//    }
//    else{
922 923 924 925 926 927
   reg_getImageGradient(this->currentFloating,
                        this->warpedGradientImage,
                        this->deformationFieldImage,
                        this->currentMask,
                        this->interpolation,
                        this->warpedPaddingValue);
928
//    }
929

930 931 932 933 934 935 936
   // The voxel based gradient image is filled with zeros
   reg_tools_multiplyValueToImage(this->voxelBasedMeasureGradientImage,
                                  this->voxelBasedMeasureGradientImage,
                                  0.f);
   // The gradient of the various measures of similarity are computed
   if(this->measure_nmi!=NULL)
      this->measure_nmi->GetVoxelBasedSimilarityMeasureGradient();
937

938 939
   if(this->measure_multichannel_nmi!=NULL)
      this->measure_multichannel_nmi->GetVoxelBasedSimilarityMeasureGradient();
940

941 942
   if(this->measure_ssd!=NULL)
      this->measure_ssd->GetVoxelBasedSimilarityMeasureGradient();
943

944 945
   if(this->measure_kld!=NULL)
      this->measure_kld->GetVoxelBasedSimilarityMeasureGradient();
946

947 948
   if(this->measure_lncc!=NULL)
      this->measure_lncc->GetVoxelBasedSimilarityMeasureGradient();
949

950 951
   if(this->measure_dti!=NULL)
      this->measure_dti->GetVoxelBasedSimilarityMeasureGradient();
952

953
#ifndef NDEBUG
954
   reg_print_fct_debug("reg_base<T>::GetVoxelBasedGradient");
955
#endif
956 957 958
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
//template<class T>
//void reg_base<T>::ApproximateParzenWindow()
//{
//    if(this->measure_nmi==NULL)
//        this->measure_nmi=new reg_nmi;
//    this->measure_nmi=approxParzenWindow = true;
//    return;
//}
///* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
//template<class T>
//void reg_base<T>::DoNotApproximateParzenWindow()
//{
//    if(this->measure_nmi==NULL)
//        this->measure_nmi=new reg_nmi;
//    this->measure_nmi=approxParzenWindow = false;
//    return;
//}
976 977 978
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
979
void reg_base<T>::UseNMISetFloatingBinNumber(int timepoint, int floBinNumber)
980
{
981 982 983 984 985 986
   if(this->measure_nmi==NULL)
      this->measure_nmi=new reg_nmi;
   this->measure_nmi->SetActiveTimepoint(timepoint);
   // I am here adding 4 to the specified bin number to accomodate for
   // the spline support
   this->measure_nmi->SetFloatingBinNumber(floBinNumber+4, timepoint);
987
#ifndef NDEBUG
988
   reg_print_fct_debug("reg_base<T>::UseNMISetFloatingBinNumber");
989
#endif
990 991 992
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
993
void reg_base<T>::UseNMISetReferenceBinNumber(int timepoint, int refBinNumber)
994
{
995 996 997 998 999 1000
   if(this->measure_nmi==NULL)
      this->measure_nmi=new reg_nmi;
   this->measure_nmi->SetActiveTimepoint(timepoint);
   // I am here adding 4 to the specified bin number to accomodate for
   // the spline support
   this->measure_nmi->SetReferenceBinNumber(refBinNumber+4, timepoint);
1001
#ifndef NDEBUG
1002
   reg_print_fct_debug("reg_base<T>::UseNMISetReferenceBinNumber");
1003
#endif
1004 1005 1006
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
1007
void reg_base<T>::UseMultiChannelNMI(int timepointNumber, int *timepoint)
1008
{
1009 1010 1011 1012
   if(this->measure_multichannel_nmi==NULL)
      this->measure_multichannel_nmi=new reg_multichannel_nmi;
   for(int i=0; i<timepointNumber; ++i)
      this->measure_multichannel_nmi->SetActiveTimepoint(i);
1013
#ifndef NDEBUG
1014
   reg_print_fct_debug("reg_base<T>::UseMultiChannelNMI");
1015
#endif
1016 1017 1018
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
1019
void reg_base<T>::UseSSD(int timepoint)
1020
{
1021 1022 1023
   if(this->measure_ssd==NULL)
      this->measure_ssd=new reg_ssd;
   this->measure_ssd->SetActiveTimepoint(timepoint);
1024
#ifndef NDEBUG
1025
   reg_print_fct_debug("reg_base<T>::UseSSD");
1026
#endif
1027 1028 1029
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
1030
void reg_base<T>::UseKLDivergence(int timepoint)
1031
{
1032 1033 1034
   if(this->measure_kld==NULL)
      this->measure_kld=new reg_kld;
   this->measure_kld->SetActiveTimepoint(timepoint);
1035
#ifndef NDEBUG
1036
   reg_print_fct_debug("reg_base<T>::UseKLDivergence");
1037
#endif
1038 1039
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
Marc Modat's avatar
Marc Modat committed
1040
template<class T>
1041
void reg_base<T>::UseLNCC(int timepoint, float stddev)
Marc Modat's avatar
Marc Modat committed
1042
{
1043 1044 1045 1046
   if(this->measure_lncc==NULL)
      this->measure_lncc=new reg_lncc;
   this->measure_lncc->SetKernelStandardDeviation(timepoint,
         stddev);
1047
#ifndef NDEBUG
1048
   reg_print_fct_debug("reg_base<T>::UseLNCC");
1049 1050 1051 1052 1053 1054
#endif
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
void reg_base<T>::SetLNCCKernelType(int type)
{
1055 1056 1057 1058 1059 1060 1061
   if(this->measure_lncc==NULL)
   {
      reg_print_fct_error("reg_base<T>::SetLNCCKernelType");
      reg_print_msg_error("The LNCC object has to be created first");
      reg_exit(1);
   }
   this->measure_lncc->SetKernelType(type);
1062
#ifndef NDEBUG
1063
   reg_print_fct_debug("reg_base<T>::SetLNCCKernelType");
1064
#endif
Marc Modat's avatar
Marc Modat committed
1065 1066 1067
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template<class T>
1068
void reg_base<T>::UseDTI(bool *timepoint)
Marc Modat's avatar
Marc Modat committed
1069
{
1070 1071 1072 1073 1074 1075 1076
   if(this->measure_dti==NULL)
      this->measure_dti=new reg_dti;
   for(int i=0; i<this->inputReference->nt; ++i)
   {
      if(timepoint[i]==true)
         this->measure_dti->SetActiveTimepoint(i);
   }
1077
#ifndef NDEBUG
1078
   reg_print_fct_debug("reg_base<T>::UseDTI");
1079
#endif
Marc Modat's avatar
Marc Modat committed
1080 1081
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
1082 1083 1084 1085
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template <class T>
void reg_base<T>::WarpFloatingImage(int inter)
{
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
   // Compute the deformation field
   this->GetDeformationField();

   if(this->measure_dti==NULL)
   {
      // Resample the floating image
      reg_resampleImage(this->currentFloating,
                        this->warped,
                        this->deformationFieldImage,
                        this->currentMask,
                        inter,
                        this->warpedPaddingValue);
   }
   else
   {
      reg_defField_getJacobianMatrix(this->deformationFieldImage,
                                     this->forwardJacobianMatrix);
      reg_resampleImage(this->currentFloating,
                        this->warped,
                        this->deformationFieldImage,
                        this->currentMask,
                        inter,
                        this->warpedPaddingValue,
                        this->measure_dti->GetActiveTimepoints(),
                        this->forwardJacobianMatrix);
   }
1112
#ifndef NDEBUG
1113
   reg_print_fct_debug("reg_base<T>::WarpFloatingImage");
1114
#endif
1115 1116 1117 1118 1119 1120 1121
}
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
/* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ */
template <class T>
void reg_base<T>::Run()
{
#ifndef NDEBUG
1122
   printf("[NiftyReg DEBUG] %s::Run() called\n", this->executableName);
1123 1124
#endif

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 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
   if(!this->initialised) this->Initialise();

   // Compute the resolution of the progress bar
   float iProgressStep=1, nProgressSteps;
   nProgressSteps = this->levelToPerform*this->maxiterationNumber;

   // Loop over the different resolution level to perform
   for(this->currentLevel=0;
         this->currentLevel<this->levelToPerform;
         this->currentLevel++)
   {

      // Set the current input images
      if(this->usePyramid)
      {
         this->currentReference = this->referencePyramid[this->currentLevel];
         this->currentFloating = this->floatingPyramid[this->currentLevel];
         this->currentMask = this->maskPyramid[this->currentLevel];
      }
      else
      {
         this->currentReference = this->referencePyramid[0];
         this->currentFloating = this->floatingPyramid[0];
         this->currentMask = this->maskPyramid[0];
      }

      // Allocate image that depends on the reference image
      this->AllocateWarped();
      this->AllocateDeformationField();
      this->AllocateWarpedGradient();