File: elxElastixBase.cxx

package info (click to toggle)
elastix 4.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 55,600 kB
  • sloc: cpp: 58,067; lisp: 4,121; python: 777; sh: 227; xml: 182; makefile: 34
file content (493 lines) | stat: -rw-r--r-- 15,845 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
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
284
285
286
287
288
289
290
291
292
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
375
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*=========================================================================
 *
 *  Copyright UMC Utrecht and contributors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
#include "elxElastixBase.h"
#include <sstream>
#include "itkMersenneTwisterRandomVariateGenerator.h"

namespace elastix
{

/**
 * ********************* Constructor ****************************
 */

ElastixBase::ElastixBase()
{
  /** Initialize. */
  this->m_Configuration     = 0;
  this->m_ComponentDatabase = 0;
  this->m_DBIndex           = 0;

  /** The default output precision of elxout is set to 6. */
  this->m_DefaultOutputPrecision = 6;

  /** Create the component containers. */
  this->m_FixedImagePyramidContainer    = ObjectContainerType::New();
  this->m_MovingImagePyramidContainer   = ObjectContainerType::New();
  this->m_InterpolatorContainer         = ObjectContainerType::New();
  this->m_ImageSamplerContainer         = ObjectContainerType::New();
  this->m_MetricContainer               = ObjectContainerType::New();
  this->m_OptimizerContainer            = ObjectContainerType::New();
  this->m_RegistrationContainer         = ObjectContainerType::New();
  this->m_ResamplerContainer            = ObjectContainerType::New();
  this->m_ResampleInterpolatorContainer = ObjectContainerType::New();
  this->m_TransformContainer            = ObjectContainerType::New();

  /** Create image and mask containers. */
  this->m_FixedImageContainer          = DataObjectContainerType::New();
  this->m_MovingImageContainer         = DataObjectContainerType::New();
  this->m_FixedImageFileNameContainer  = FileNameContainerType::New();
  this->m_MovingImageFileNameContainer = FileNameContainerType::New();

  this->m_FixedMaskContainer          = DataObjectContainerType::New();
  this->m_MovingMaskContainer         = DataObjectContainerType::New();
  this->m_FixedMaskFileNameContainer  = FileNameContainerType::New();
  this->m_MovingMaskFileNameContainer = FileNameContainerType::New();

  this->m_ResultImageContainer = DataObjectContainerType::New();

  /** Initialize initialTransform and final transform. */
  this->m_InitialTransform = 0;
  this->m_FinalTransform   = 0;

  /** From Elastix 4.3 to 4.7: Ignore direction cosines by default, for
   * backward compatability. From Elastix 4.8: set it to true by default.*/
  this->m_UseDirectionCosines = true;

} // end Constructor


/**
 * ********************* SetDBIndex ***********************
 */

void
ElastixBase::SetDBIndex( DBIndexType _arg )
{
  /** If m_DBIndex is not set, set it. */
  if( this->m_DBIndex != _arg )
  {
    this->m_DBIndex = _arg;

    itk::Object * thisasobject = dynamic_cast< itk::Object * >( this );
    if( thisasobject )
    {
      thisasobject->Modified();
    }
  }

} // end SetDBIndex()


/**
 * ************************ BeforeAllBase ***************************
 */

int
ElastixBase::BeforeAllBase( void )
{
  /** Declare the return value and initialize it. */
  int returndummy = 0;

  /** Set the default precision of floating values in the output. */
  this->m_Configuration->ReadParameter(
    this->m_DefaultOutputPrecision, "DefaultOutputPrecision", 0, false );
  elxout << std::setprecision( this->m_DefaultOutputPrecision );

  /** Print to log file. */
  elxout << std::fixed;
  elxout << std::showpoint;
  elxout << std::setprecision( 3 );
  elxout << "ELASTIX version: " << __ELASTIX_VERSION << std::endl;
  elxout << std::setprecision( this->GetDefaultOutputPrecision() );

  /** Check Command line options and print them to the logfile. */
  elxout << "Command line options from ElastixBase:" << std::endl;
  std::string check = "";

  /** Read the fixed and moving image filenames. These are obliged options,
   * so print an error if they are not present.
   * Print also some info (second boolean = true).
   */
#ifndef _ELASTIX_BUILD_LIBRARY
  this->m_FixedImageFileNameContainer = this->GenerateFileNameContainer(
    "-f", returndummy, true, true );
  this->m_MovingImageFileNameContainer = this->GenerateFileNameContainer(
    "-m", returndummy, true, true );
#endif
  /** Read the fixed and moving mask filenames. These are not obliged options,
   * so do not print any errors if they are not present.
   * Do print some info (second boolean = true).
   */
  int maskreturndummy = 0;
  this->m_FixedMaskFileNameContainer = this->GenerateFileNameContainer(
    "-fMask", maskreturndummy, false, true );
  if( maskreturndummy != 0 )
  {
    elxout << "-fMask    unspecified, so no fixed mask used" << std::endl;
  }
  maskreturndummy                     = 0;
  this->m_MovingMaskFileNameContainer = this->GenerateFileNameContainer(
    "-mMask", maskreturndummy, false, true );
  if( maskreturndummy != 0 )
  {
    elxout << "-mMask    unspecified, so no moving mask used" << std::endl;
  }

  /** Check for appearance of "-out".
   * This check has already been performed in elastix.cxx,
   * Here we do it again. MS: WHY?
   */
  check = this->GetConfiguration()->GetCommandLineArgument( "-out" );
  if( check == "" )
  {
    xl::xout[ "error" ] << "ERROR: No CommandLine option \"-out\" given!" << std::endl;
    returndummy |= 1;
  }
  else
  {
    /** Make sure that last character of the output folder equals a '/' or '\'. */
    std::string folder( check );
    const char  last = folder[ folder.size() - 1 ];
    if( last != '/' && last != '\\' )
    {
      folder.append( "/" );
      folder = itksys::SystemTools::ConvertToOutputPath( folder.c_str() );

      /** Note that on Windows, in case the output folder contains a space,
       * the path name is double quoted by ConvertToOutputPath, which is undesirable.
       * So, we remove these quotes again.
       */
      if( itksys::SystemTools::StringStartsWith( folder.c_str(), "\"" )
        && itksys::SystemTools::StringEndsWith(   folder.c_str(), "\"" ) )
      {
        folder = folder.substr( 1, folder.length() - 2 );
      }

      this->GetConfiguration()->SetCommandLineArgument( "-out", folder.c_str() );
    }
    elxout << "-out      " << check << std::endl;
  }

  /** Print all "-p". */
  unsigned int i    = 1;
  bool         loop = true;
  while( loop )
  {
    std::ostringstream tempPname( "" );
    tempPname << "-p(" << i << ")";
    check = this->GetConfiguration()->GetCommandLineArgument( tempPname.str().c_str() );
    if( check == "" ) { loop = false; }
    else { elxout << "-p        " << check << std::endl; }
    ++i;
  }

  /** Check for appearance of "-priority", if this is a Windows station. */
#ifdef _WIN32
  check = this->GetConfiguration()->GetCommandLineArgument( "-priority" );
  if( check == "" )
  {
    elxout << "-priority unspecified, so NORMAL process priority" << std::endl;
  }
  else
  {
    elxout << "-priority " << check << std::endl;
  }
#endif

  /** Check for appearance of -threads, which specifies the maximum number of threads. */
  check = this->GetConfiguration()->GetCommandLineArgument( "-threads" );
  if( check == "" )
  {
    elxout << "-threads  unspecified, so all available threads are used" << std::endl;
  }
  else
  {
    elxout << "-threads  " << check << std::endl;
  }

  /** Check the very important UseDirectionCosines parameter. */
  this->m_UseDirectionCosines = true;
  bool retudc = this->GetConfiguration()->ReadParameter( this->m_UseDirectionCosines,
    "UseDirectionCosines", 0 );
  if( !retudc )
  {
    xl::xout[ "warning" ]
      << "\nWARNING: The option \"UseDirectionCosines\" was not found in your parameter file.\n"
      << "  From elastix 4.8 it defaults to true!\n"
      << "This may change the behavior of your registrations considerably.\n"
      << std::endl;
  }

  /** Set the random seed. Use 121212 as a default, which is the same as
   * the default in the MersenneTwister code.
   * Use silent parameter file readout, to avoid annoying warning when
   * starting elastix */
  typedef itk::Statistics::MersenneTwisterRandomVariateGenerator RandomGeneratorType;
  typedef RandomGeneratorType::IntegerType                       SeedType;
  unsigned int randomSeed = 121212;
  this->GetConfiguration()->ReadParameter( randomSeed, "RandomSeed", 0, false );
  RandomGeneratorType::Pointer randomGenerator = RandomGeneratorType::GetInstance();
  randomGenerator->SetSeed( static_cast< SeedType >( randomSeed ) );

  /** Return a value. */
  return returndummy;

} // end BeforeAllBase()


/**
 * ************************ BeforeAllTransformixBase ***************************
 */

int
ElastixBase::BeforeAllTransformixBase( void )
{
  /** Declare the return value and initialize it. */
  int returndummy = 0;

  /** Print to log file. */
  elxout << std::fixed;
  elxout << std::showpoint;
  elxout << std::setprecision( 3 );
  elxout << "ELASTIX version: " << __ELASTIX_VERSION << std::endl;
  elxout << std::setprecision( this->GetDefaultOutputPrecision() );

  /** Check Command line options and print them to the logfile. */
  elxout << "Command line options from ElastixBase:" << std::endl;
#ifndef _ELASTIX_BUILD_LIBRARY
  /** Read the input image filenames. These are not obliged options,
   * so do not print an error if they are not present.
   * Print also some info (second boolean = true)
   * Save the result in the moving image file name container.
   */
  int inreturndummy = 0;
  this->m_MovingImageFileNameContainer = this->GenerateFileNameContainer(
    "-in", inreturndummy, false, true );
  if( inreturndummy != 0 )
  {
    elxout << "-in       unspecified, so no input image specified" << std::endl;
  }
#endif
  /** Check for appearance of "-out". */
  std::string check = this->GetConfiguration()->GetCommandLineArgument( "-out" );
  if( check == "" )
  {
    xl::xout[ "error" ] << "ERROR: No CommandLine option \"-out\" given!" << std::endl;
    returndummy |= 1;
  }
  else
  {
    /** Make sure that last character of -out equals a '/'. */
    std::string folder( check );
    if( folder.find_last_of( "/" ) != folder.size() - 1 )
    {
      folder.append( "/" );
      this->GetConfiguration()->SetCommandLineArgument( "-out", folder.c_str() );
    }
    elxout << "-out      " << check << std::endl;
  }

  /** Check for appearance of -threads, which specifies the maximum number of threads. */
  check = this->GetConfiguration()->GetCommandLineArgument( "-threads" );
  if( check == "" )
  {
    elxout << "-threads  unspecified, so all available threads are used" << std::endl;
  }
  else
  {
    elxout << "-threads  " << check << std::endl;
  }
#ifndef _ELASTIX_BUILD_LIBRARY
  /** Print "-tp". */
  check = this->GetConfiguration()->GetCommandLineArgument( "-tp" );
  elxout << "-tp       " << check << std::endl;
#endif
  /** Check the very important UseDirectionCosines parameter. */
  this->m_UseDirectionCosines = false;
  bool retudc = this->GetConfiguration()->ReadParameter( this->m_UseDirectionCosines,
    "UseDirectionCosines", 0 );
  if( !retudc )
  {
    xl::xout[ "warning" ]
      << "\nWARNING: From elastix 4.3 it is highly recommended to add\n"
      << "the UseDirectionCosines option to your parameter file! See\n"
      << "http://elastix.isi.uu.nl/whatsnew_04_3.php for more information.\n"
      << std::endl;
  }

  return returndummy;

} // end BeforeAllTransformixBase()


/**
 * ************************ BeforeRegistrationBase ******************
 */

void
ElastixBase::BeforeRegistrationBase( void )
{
  using namespace xl;

  /** Set up the "iteration" writing field. */
  this->m_IterationInfo.SetOutputs( xout.GetCOutputs() );
  this->m_IterationInfo.SetOutputs( xout.GetXOutputs() );

  xout.AddTargetCell( "iteration", &this->m_IterationInfo );

} // end BeforeRegistrationBase()


/**
 * **************** AfterRegistrationBase ***********************
 */

void
ElastixBase::AfterRegistrationBase( void )
{
  /** Remove the "iteration" writing field. */
  xl::xout.RemoveTargetCell( "iteration" );

} // end AfterRegistrationBase()


/**
 * ********************* GenerateFileNameContainer ******************
 */

ElastixBase::FileNameContainerPointer
ElastixBase::GenerateFileNameContainer(
  const std::string & optionkey, int & errorcode,
  bool printerrors, bool printinfo ) const
{
  FileNameContainerPointer fileNameContainer = FileNameContainerType::New();

  /** Try optionkey0. */
  std::ostringstream argusedss( "" );
  argusedss << optionkey << 0;
  std::string argused = argusedss.str();
  std::string check   = this->GetConfiguration()->GetCommandLineArgument( argused.c_str() );
  if( check == "" )
  {
    /** Try optionkey. */
    std::ostringstream argusedss2( "" );
    argusedss2 << optionkey;
    argused = argusedss2.str();
    check   = this->GetConfiguration()->GetCommandLineArgument( argused.c_str() );
    if( check == "" )
    {
      /** Both failed; return an error message, if desired. */
      if( printerrors )
      {
        xl::xout[ "error" ]
          << "ERROR: No CommandLine option \""
          << optionkey << "\" or \""
          << optionkey << 0 << "\" given!" << std::endl;
      }
      errorcode |= 1;

      return fileNameContainer;
    }
  }

  /** Optionkey or optionkey0 is found. */
  if( check != "" )
  {
    /** Print info, if desired. */
    if( printinfo )
    {
      /** Print the option, with some spaces, followed by the value. */
      int          nrSpaces0 = 10 - argused.length();
      unsigned int nrSpaces  = nrSpaces0 > 1 ? nrSpaces0 : 1;
      std::string  spaces    = "";
      spaces.resize( nrSpaces, ' ' );
      elxout << argused << spaces << check << std::endl;
    }
    fileNameContainer->CreateElementAt( 0 ) = check;

    /** Loop over all optionkey<i> options given with i > 0. */
    unsigned int i           = 1;
    bool         readsuccess = true;
    while( readsuccess )
    {
      std::ostringstream argusedss2( "" );
      argusedss2 << optionkey << i;
      argused = argusedss2.str();
      check   = this->GetConfiguration()->GetCommandLineArgument( argused.c_str() );
      if( check == "" )
      {
        readsuccess = false;
      }
      else
      {
        if( printinfo )
        {
          /** Print the option, with some spaces, followed by the value. */
          int          nrSpaces0 = 10 - argused.length();
          unsigned int nrSpaces  = nrSpaces0 > 1 ? nrSpaces0 : 1;
          std::string  spaces    = "";
          spaces.resize( nrSpaces, ' ' );
          elxout << argused << spaces << check << std::endl;
        }
        fileNameContainer->CreateElementAt( i ) = check;
        ++i;
      }
    } // end while
  }   // end if

  return fileNameContainer;

} // end GenerateFileNameContainer()


/**
 * ******************** GetUseDirectionCosines ********************
 */

bool
ElastixBase::GetUseDirectionCosines( void ) const
{
  return this->m_UseDirectionCosines;
}


/**
 * ******************** SetOriginalFixedImageDirectionFlat ********************
 */

void
ElastixBase::SetOriginalFixedImageDirectionFlat(
  const FlatDirectionCosinesType & arg )
{
  this->m_OriginalFixedImageDirection = arg;
}


/**
 * ******************** GetOriginalFixedImageDirectionFlat ********************
 */

const ElastixBase::FlatDirectionCosinesType &
ElastixBase::GetOriginalFixedImageDirectionFlat( void ) const
{
  return this->m_OriginalFixedImageDirection;
}


} // end namespace elastix