File: itkGenericMultiResolutionPyramidImageFilter.hxx

package info (click to toggle)
elastix 5.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,480 kB
  • sloc: cpp: 68,403; lisp: 4,118; python: 1,013; xml: 182; sh: 177; makefile: 33
file content (901 lines) | stat: -rw-r--r-- 29,114 bytes parent folder | download
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
494
495
496
497
498
499
500
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
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
686
687
688
689
690
691
692
693
694
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
759
760
761
762
763
764
765
766
767
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
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
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
/*=========================================================================
 *
 *  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.
 *
 *=========================================================================*/
#ifndef itkGenericMultiResolutionPyramidImageFilter_hxx
#define itkGenericMultiResolutionPyramidImageFilter_hxx

#include "itkGenericMultiResolutionPyramidImageFilter.h"

#include "itkResampleImageFilter.h"
#include "itkShrinkImageFilter.h"
#include "itkImageAlgorithm.h"

namespace itk
{
/**
 * ******************* Constructor ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::
  GenericMultiResolutionPyramidImageFilter()
{
  this->m_CurrentLevel = 0;
  this->m_ComputeOnlyForCurrentLevel = false;
  this->m_SmoothingSchedule = SmoothingScheduleType(this->GetNumberOfLevels(), ImageDimension, ScalarRealType());
  this->m_SmoothingScheduleDefined = false;
} // end Constructor


/**
 * ******************* SetNumberOfLevels ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
void
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::SetNumberOfLevels(unsigned int num)
{
  if (this->m_NumberOfLevels == num)
  {
    return;
  }
  Superclass::SetNumberOfLevels(num);

  /** Resize the smoothing schedule too. */
  this->m_SmoothingSchedule = SmoothingScheduleType(this->GetNumberOfLevels(), ImageDimension, ScalarRealType());
  this->m_SmoothingScheduleDefined = false;

} // end SetNumberOfLevels()


/**
 * ******************* SetCurrentLevel ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
void
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::SetCurrentLevel(unsigned int level)
{
  itkDebugMacro("setting CurrentLevel to " << level);
  if (this->m_CurrentLevel != level)
  {
    // clamp value to be less then number of levels
    this->m_CurrentLevel = level;
    if (this->m_CurrentLevel >= this->m_NumberOfLevels)
    {
      // Safe this->m_NumberOfLevels always >= 1
      this->m_CurrentLevel = this->m_NumberOfLevels - 1;
    }
    this->ReleaseOutputs();

    /** Only set the modified flag for this filter if the output is computed per level. */
    if (this->m_ComputeOnlyForCurrentLevel)
    {
      this->Modified();
    }
  }
} // end SetCurrentLevel()


/**
 * ******************* SetComputeOnlyForCurrentLevel ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
void
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::SetComputeOnlyForCurrentLevel(
  const bool _arg)
{
  itkDebugMacro("setting ComputeOnlyForCurrentLevel to " << _arg);
  if (this->m_ComputeOnlyForCurrentLevel != _arg)
  {
    this->m_ComputeOnlyForCurrentLevel = _arg;
    this->ReleaseOutputs();
    this->Modified();
  }
} // end SetComputeOnlyForCurrentLevel()


/**
 * ******************* SetSchedule ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
void
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::SetSchedule(
  const ScheduleType & schedule)
{
  Superclass::SetSchedule(schedule);

  /** This part is to make sure that only combination of
   * SetRescaleSchedule and SetSmoothingSchedule or SetSchedule are used.
   * Only in GenerateData we use SetSmoothingScheduleToDefault, because only
   * there all required information is available.
   */
  this->m_SmoothingSchedule = SmoothingScheduleType(this->GetNumberOfLevels(), ImageDimension, ScalarRealType());
  this->m_SmoothingScheduleDefined = false;
} // end SetSchedule()


/**
 * ******************* SetRescaleSchedule ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
void
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::SetRescaleSchedule(
  const RescaleScheduleType & schedule)
{
  /** Here we would prefer to use m_RescaleSchedule.
   * Although it would require copying most of the methods
   * from MultiResolutionPyramidImageFilter and changing m_Schedule
   * to m_RescaleSchedule.
   */
  Superclass::SetSchedule(schedule);
} // end SetRescaleSchedule()


/**
 * ******************* SetRescaleScheduleToUnity ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
void
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::SetRescaleScheduleToUnity()
{
  RescaleScheduleType schedule;
  schedule.Fill(NumericTraits<ScalarRealType>::OneValue());
  Superclass::SetSchedule(schedule);
} // end SetRescaleScheduleToUnity()


/**
 * ******************* SetSmoothingSchedule ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
void
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::SetSmoothingSchedule(
  const SmoothingScheduleType & schedule)
{
  if (schedule == this->m_SmoothingSchedule)
  {
    return;
  }

  if (schedule.rows() != this->m_NumberOfLevels || schedule.columns() != ImageDimension)
  {
    itkDebugMacro("Smoothing schedule has wrong dimensions");
    return;
  }

  for (unsigned int level = 0; level < this->m_NumberOfLevels; ++level)
  {
    for (unsigned int dim = 0; dim < ImageDimension; ++dim)
    {
      this->m_SmoothingSchedule[level][dim] = schedule[level][dim];

      /** Similar to Superclass::SetSchedule, set smoothing schedule to
       * max( 0, min(schedule[level], schedule[level-1] ).
       */
      if (level > 0)
      {
        this->m_SmoothingSchedule[level][dim] =
          std::min(this->m_SmoothingSchedule[level][dim], this->m_SmoothingSchedule[level - 1][dim]);
      }
      if (this->m_SmoothingSchedule[level][dim] < 0.0)
      {
        this->m_SmoothingSchedule[level][dim] = 0.0;
      }
    }
  }

  this->m_SmoothingScheduleDefined = true;
  this->Modified();
} // end SetSmoothingSchedule()


/**
 * ******************* SetSmoothingScheduleToZero ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
void
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::SetSmoothingScheduleToZero()
{
  this->SetSmoothingSchedule(SmoothingScheduleType());
} // end SetSmoothingScheduleToZero()


/**
 * ******************* GenerateData ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
void
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::GenerateData()
{
  // Depending on user setting of the SetUseMultiResolutionRescaleSchedule() and
  // SetUseMultiResolutionSmoothingSchedule()
  // in combination with SetUseShrinkImageFilter() different pipelines will be
  // created below. The most common is:
  // 1. m_UseMultiResolutionSmoothingSchedule = true
  //    m_UseMultiResolutionRescaleSchedule = true
  //    Then pipeline is: input -> smoother -> shrinker/resample -> output
  // 2. m_UseMultiResolutionSmoothingSchedule = false
  //    m_UseMultiResolutionRescaleSchedule = true
  //    Then pipeline is: input -> shrinker/resample -> output
  // 3. m_UseMultiResolutionSmoothingSchedule = true
  //    m_UseMultiResolutionRescaleSchedule = false
  //    Then pipeline is: input -> smoother -> output
  // 4. m_UseMultiResolutionSmoothingSchedule = false
  //    m_UseMultiResolutionRescaleSchedule = false
  //    Then pipeline is: input -> copy -> output
  //
  // 1.a) The smoother can be skipped if AreSigmasAllZeros(...)
  //      returns true for the current level.
  // 1.b) The shrinker/resampler can be skipped if AreRescaleFactorsAllOnes(...)
  //      returns true for the current level.
  //
  // Then pipeline 1 may transforms for the current level to:
  // 1.a) input -> shrinker/resample -> output
  // 1.b) input -> smoother -> output
  //
  // Pipeline also takes care of memory allocation for N'th output if
  // SetComputeOnlyForCurrentLevel has been set to true.

  // Get the input and output pointers
  InputImageConstPointer input = this->GetInput();

  // Check if we have to do anything at all
  if (!this->IsSmoothingUsed() && !this->IsRescaleUsed())
  {
    // This is a special case we just allocate output images and copy input
    for (unsigned int level = 0; level < this->m_NumberOfLevels; ++level)
    {
      if (!this->m_ComputeOnlyForCurrentLevel)
      {
        this->UpdateProgress(static_cast<float>(level) / static_cast<float>(this->m_NumberOfLevels));
      }

      if (this->ComputeForCurrentLevel(level))
      {
        OutputImagePointer outputPtr = this->GetOutput(level);
        outputPtr->SetBufferedRegion(input->GetLargestPossibleRegion());
        outputPtr->Allocate();

        ImageAlgorithm::Copy(input.GetPointer(),
                             outputPtr.GetPointer(),
                             input->GetLargestPossibleRegion(),
                             outputPtr->GetLargestPossibleRegion());
      }
    }
    return; // We are done, return
  }

  // First check if smoothing schedule has been set
  if (!this->m_SmoothingScheduleDefined)
  {
    this->SetSmoothingScheduleToDefault();
  }

  typename SmootherType::Pointer                     smoother;
  typename ImageToImageFilterSameTypes::Pointer      rescaleSameTypes;
  typename ImageToImageFilterDifferentTypes::Pointer rescaleDifferentTypes;

  for (unsigned int level = 0; level < this->m_NumberOfLevels; ++level)
  {
    if (!this->m_ComputeOnlyForCurrentLevel)
    {
      this->UpdateProgress(static_cast<float>(level) / static_cast<float>(this->m_NumberOfLevels));
    }

    if (this->ComputeForCurrentLevel(level))
    {
      // Allocate memory for each output
      const OutputImagePointer outputPtr = this->GetOutput(level);
      outputPtr->SetBufferedRegion(outputPtr->GetRequestedRegion());
      outputPtr->Allocate();

      // Setup the smoother
      const bool smootherIsUsed = this->SetupSmoother(level, smoother, input);

      // Setup the shrinker or resampler
      const int shrinkerOrResamplerIsUsed = this->SetupShrinkerOrResampler(
        level, smoother, smootherIsUsed, input, outputPtr, rescaleSameTypes, rescaleDifferentTypes);

      const auto updateAndGraft = [this, level, outputPtr](auto & filter) {
        filter.GraftOutput(outputPtr);

        // force to always update in case shrink factors are the same
        filter.Modified();
        filter.UpdateLargestPossibleRegion();
        this->GraftNthOutput(level, filter.GetOutput());
      };

      // Update the pipeline and graft or copy results to this filters output
      if (shrinkerOrResamplerIsUsed == 0 && smootherIsUsed)
      {
        updateAndGraft(*smoother);
      }
      else if (shrinkerOrResamplerIsUsed == 0)
      {
        ImageAlgorithm::Copy(input.GetPointer(),
                             outputPtr.GetPointer(),
                             input->GetLargestPossibleRegion(),
                             outputPtr->GetLargestPossibleRegion());
      }
      else if (shrinkerOrResamplerIsUsed == 1)
      {
        updateAndGraft(*rescaleSameTypes);
      }
      else if (shrinkerOrResamplerIsUsed == 2)
      {
        updateAndGraft(*rescaleDifferentTypes);
      }
      // no else needed
    }
  } // end for ilevel
} // end GenerateData()


/**
 * ******************* SetupSmoother ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
bool
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::SetupSmoother(
  const unsigned int               level,
  typename SmootherType::Pointer & smoother,
  const InputImageConstPointer &   input)
{
  SigmaArrayType sigmaArray;
  this->GetSigma(level, sigmaArray);
  const bool sigmasAllZeros = this->AreSigmasAllZeros(sigmaArray);
  if (!sigmasAllZeros)
  {
    // First construct the smoother if has not been created and set input.
    if (smoother.IsNull())
    {
      smoother = SmootherType::New();
    }

    smoother->SetInput(input);
    smoother->SetSigmaArray(sigmaArray);
    return true;
  }

  return false;
} // end SetupSmoother()


/**
 * ******************* SetupShrinkerOrResampler ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
int
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::SetupShrinkerOrResampler(
  const unsigned int                                   level,
  typename SmootherType::Pointer &                     smoother,
  const bool                                           sameType,
  const InputImageConstPointer &                       inputPtr,
  const OutputImagePointer &                           outputPtr,
  typename ImageToImageFilterSameTypes::Pointer &      rescaleSameTypes,
  typename ImageToImageFilterDifferentTypes::Pointer & rescaleDifferentTypes)
{
  RescaleFactorArrayType shrinkFactors;
  this->GetShrinkFactors(level, shrinkFactors);
  const bool rescaleFactorsAllOnes = this->AreRescaleFactorsAllOnes(shrinkFactors);

  // No shrinking or resampling needed: return 0
  if (rescaleFactorsAllOnes)
  {
    return 0;
  }

  // Choose between shrinker or resampler
  this->DefineShrinkerOrResampler(sameType, shrinkFactors, outputPtr, rescaleSameTypes, rescaleDifferentTypes);

  // Rescaling is done with input and output type being equal: return 1
  // Input and output are equal only if the smoother was used previously.
  if (sameType)
  {
    rescaleSameTypes->SetInput(smoother->GetOutput());
    return 1;
  }

  // Rescaling is done with input and output type being different: return 2
  // Input and output are different only if the smoother was skipped.
  rescaleDifferentTypes->SetInput(inputPtr);
  return 2;

} // end SetupShrinkerOrResampler()


/**
 * ******************* DefineShrinkerOrResampler ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
void
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::DefineShrinkerOrResampler(
  const bool                                           sameType,
  const RescaleFactorArrayType &                       shrinkFactors,
  const OutputImagePointer &                           outputPtr,
  typename ImageToImageFilterSameTypes::Pointer &      rescaleSameTypes,
  typename ImageToImageFilterDifferentTypes::Pointer & rescaleDifferentTypes)
{
  // Typedefs
  using TransformType = IdentityTransform<TPrecisionType, OutputImageType::ImageDimension>;
  using ShrinkerSameType = ShrinkImageFilter<OutputImageType, OutputImageType>;
  using ResamplerSameType = ResampleImageFilter<OutputImageType, OutputImageType, TPrecisionType>;
  using ShrinkerDifferentType = ShrinkImageFilter<InputImageType, OutputImageType>;
  using ResamplerDifferentType = ResampleImageFilter<InputImageType, OutputImageType, TPrecisionType>;
  using InterpolatorForSameType = LinearInterpolateImageFunction<OutputImageType, TPrecisionType>;
  using InterpolatorForDifferentType = LinearInterpolateImageFunction<InputImageType, TPrecisionType>;

  /**
   * Define pipeline in case input and output types are THE SAME.
   */

  if (sameType)
  {
    // A pipeline version that newly constructs the required filters:
    if (rescaleSameTypes.IsNull())
    {
      if (this->GetUseShrinkImageFilter())
      {
        // Define and setup shrinker
        auto shrinker = ShrinkerSameType::New();
        shrinker->SetShrinkFactors(shrinkFactors);

        // Assign
        rescaleSameTypes = shrinker.GetPointer();
      }
      else
      {
        // Define and setup resampler
        auto resampler = ResamplerSameType::New();
        resampler->SetOutputParametersFromImage(outputPtr);
        resampler->SetDefaultPixelValue(0);

        // Define and set interpolator
        auto interpolator = InterpolatorForSameType::New();
        resampler->SetInterpolator(interpolator);

        // Define and set transform
        auto transform = TransformType::New();
        resampler->SetTransform(transform);

        // Assign
        rescaleSameTypes = resampler.GetPointer();
      }
    }
    // A pipeline version that re-uses previously constructed filters:
    else
    {
      if (this->GetUseShrinkImageFilter())
      {
        // Setup shrinker
        typename ShrinkerSameType::Pointer shrinker = dynamic_cast<ShrinkerSameType *>(rescaleSameTypes.GetPointer());
        shrinker->SetShrinkFactors(shrinkFactors);
      }
      else
      {
        // Setup resampler
        typename ResamplerSameType::Pointer resampler =
          dynamic_cast<ResamplerSameType *>(rescaleSameTypes.GetPointer());
        resampler->SetOutputParametersFromImage(outputPtr);
      }
    }

    return;
  }

  /**
   * Define pipeline in case input and output types are DIFFERENT.
   */

  // A pipeline version that newly constructs the required filters:
  if (rescaleDifferentTypes.IsNull())
  {
    if (this->GetUseShrinkImageFilter())
    {
      // Define and setup shrinker
      auto shrinker = ShrinkerDifferentType::New();
      shrinker->SetShrinkFactors(shrinkFactors);

      // Assign
      rescaleDifferentTypes = shrinker.GetPointer();
    }
    else
    {
      // Define and setup resampler
      auto resampler = ResamplerDifferentType::New();
      resampler->SetOutputParametersFromImage(outputPtr);
      resampler->SetDefaultPixelValue(0);

      // Define and set interpolator
      auto interpolator = InterpolatorForDifferentType::New();
      resampler->SetInterpolator(interpolator);

      // Define and set transform
      auto transform = TransformType::New();
      resampler->SetTransform(transform);

      // Assign
      rescaleDifferentTypes = resampler.GetPointer();
    }
  }
  // A pipeline version that re-uses previously constructed filters:
  else
  {
    if (this->GetUseShrinkImageFilter())
    {
      typename ShrinkerDifferentType::Pointer shrinker =
        dynamic_cast<ShrinkerDifferentType *>(rescaleDifferentTypes.GetPointer());
      shrinker->SetShrinkFactors(shrinkFactors);
    }
    else
    {
      typename ResamplerDifferentType::Pointer resampler =
        dynamic_cast<ResamplerDifferentType *>(rescaleDifferentTypes.GetPointer());
      resampler->SetOutputParametersFromImage(outputPtr);
    }
  }

} // end DefineShrinkerOrResampler()


/**
 * ******************* GenerateOutputInformation ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
void
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::GenerateOutputInformation()
{
  if (this->IsRescaleUsed())
  {
    Superclass::GenerateOutputInformation();
  }
  else
  {
    // call the SuperSuperclass implementation of this method
    SuperSuperclass::GenerateOutputInformation();
  }
} // end GenerateOutputInformation()


/**
 * ******************* GenerateOutputRequestedRegion ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
void
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::GenerateOutputRequestedRegion(
  DataObject * refOutput)
{
  if (this->IsRescaleUsed())
  {
    Superclass::GenerateOutputRequestedRegion(refOutput);
  }
  else
  {
    // call the supersuperclass's implementation of this method
    SuperSuperclass::GenerateOutputRequestedRegion(refOutput);
  }

  // We have to set requestedRegion properly
  for (unsigned int level = 0; level < this->m_NumberOfLevels; ++level)
  {
    this->GetOutput(level)->SetRequestedRegionToLargestPossibleRegion();
  }
} // end GenerateOutputRequestedRegion()


/**
 * ******************* GenerateInputRequestedRegion ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
void
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::GenerateInputRequestedRegion()
{
  if (this->IsRescaleUsed())
  {
    /** GenericMultiResolutionPyramidImageFilter requires a larger input requested
     * region than the output requested regions to accommodate the shrinkage and
     * smoothing operations. Therefore Superclass provides this implementation.
     */
    Superclass::GenerateInputRequestedRegion();
  }
  else
  {
    /** call the SuperSuperclass implementation of this method. This should
     * copy the output requested region to the input requested region
     */
    SuperSuperclass::GenerateInputRequestedRegion();

    /** This filter needs all of the input, because it uses the
     * GausianRecursiveFilter.
     */
    InputImagePointer image = const_cast<InputImageType *>(this->GetInput());

    if (!image)
    {
      itkExceptionMacro("Input has not been set.");
    }
    else
    {
      image->SetRequestedRegion(this->GetInput()->GetLargestPossibleRegion());
    }
  }
} // end GenerateInputRequestedRegion()


/**
 * ******************* ReleaseOutputs ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
void
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::ReleaseOutputs()
{
  // release the memories if already has been allocated
  for (unsigned int level = 0; level < this->m_NumberOfLevels; ++level)
  {
    if (this->m_ComputeOnlyForCurrentLevel && level != this->m_CurrentLevel)
    {
      this->GetOutput(level)->Initialize();
    }
  }
} // end ReleaseOutputs()


/**
 * ******************* ComputeForCurrentLevel ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
bool
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::ComputeForCurrentLevel(
  const unsigned int level) const
{
  if (!this->m_ComputeOnlyForCurrentLevel || (this->m_ComputeOnlyForCurrentLevel && level == this->m_CurrentLevel))
  {
    return true;
  }
  else
  {
    return false;
  }
} // end ComputeOnlyForCurrentLevel()


/**
 * ******************* GetDefaultSigma ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
double
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::GetDefaultSigma(
  const unsigned int   level,
  const unsigned int   dim,
  const unsigned int * factors,
  const SpacingType &  spacing) const
{
  /** Compute the standard deviation: 0.5 * factor * spacing
   * This is exactly like in the Superclass.
   * In the superclass, the DiscreteGaussianImageFilter is used, which
   * requires the variance, and has the option to ignore the image spacing.
   * That's why the formula looks maybe different at first sight.
   */
  if (factors[dim] == 1 && (level == this->m_NumberOfLevels - 1))
  {
    return 0.0;
  }
  return 0.5 * static_cast<double>(factors[dim]) * spacing[dim];
} // end GetDefaultSigma()


/**
 * ******************* SetSmoothingScheduleToDefault ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
void
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::SetSmoothingScheduleToDefault()
{
  InputImageConstPointer input = this->GetInput();
  const SpacingType &    spacing = input->GetSpacing();

  // Resize the smoothing schedule
  this->m_SmoothingSchedule = SmoothingScheduleType(this->GetNumberOfLevels(), ImageDimension, ScalarRealType());

  unsigned int factors[ImageDimension];
  for (unsigned int level = 0; level < this->m_NumberOfLevels; ++level)
  {
    for (unsigned int dim = 0; dim < ImageDimension; ++dim)
    {
      factors[dim] = this->m_Schedule[level][dim];
      this->m_SmoothingSchedule[level][dim] = this->GetDefaultSigma(level, dim, factors, spacing);
    }
  }
} // end SetSmoothingScheduleToDefault()


/**
 * ******************* GetSigma ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
void
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::GetSigma(
  const unsigned int level,
  SigmaArrayType &   sigmaArray) const
{
  sigmaArray.Fill(0);
  for (unsigned int dim = 0; dim < ImageDimension; ++dim)
  {
    sigmaArray[dim] = this->m_SmoothingSchedule[level][dim];
  }
} // end GetSigma()


/**
 * ******************* GetShrinkFactors ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
void
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::GetShrinkFactors(
  const unsigned int       level,
  RescaleFactorArrayType & shrinkFactors) const
{
  shrinkFactors.Fill(0);
  for (unsigned int dim = 0; dim < ImageDimension; ++dim)
  {
    /** Here we would prefer to use m_RescaleSchedule.
     * Although it would require copying most of the methods
     * from MultiResolutionPyramidImageFilter and changing m_Schedule
     * to m_RescaleSchedule.
     */
    shrinkFactors[dim] = this->m_Schedule[level][dim];
  }
} // end GetShrinkFactors()


/**
 * ******************* AreSigmasAllZeros ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
bool
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::AreSigmasAllZeros(
  const SigmaArrayType & sigmaArray) const
{
  for (unsigned int dim = 0; dim < ImageDimension; ++dim)
  {
    if (sigmaArray[dim] != 0.0)
    {
      return false;
    }
  }

  return true;
} // end AreSigmasAllZeros()


/**
 * ******************* AreRescaleFactorsAllOnes ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
bool
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::AreRescaleFactorsAllOnes(
  const RescaleFactorArrayType & rescaleFactors) const
{
  const ScalarRealType one = NumericTraits<ScalarRealType>::One;
  for (unsigned int dim = 0; dim < ImageDimension; ++dim)
  {
    if (rescaleFactors[dim] != one)
    {
      return false;
    }
  }

  return true;
} // end AreRescaleFactorsAllOnes()


/**
 * ******************* IsSmoothingUsed ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
bool
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::IsSmoothingUsed() const
{
  // If for any level all sigma elements are not zeros then smooth are used in pipeline
  SigmaArrayType sigmaArray;
  for (unsigned int level = 0; level < this->m_NumberOfLevels; ++level)
  {
    this->GetSigma(level, sigmaArray);
    if (!this->AreSigmasAllZeros(sigmaArray))
    {
      return true;
    }
  }
  return false;
} // end IsSmoothingUsed()


/**
 * ******************* IsRescaleUsed ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
bool
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::IsRescaleUsed() const
{
  // If for any level all rescale factors are not ones then rescale are used in pipeline
  RescaleFactorArrayType rescaleFactors;
  for (unsigned int level = 0; level < this->m_NumberOfLevels; ++level)
  {
    this->GetShrinkFactors(level, rescaleFactors);
    if (!this->AreRescaleFactorsAllOnes(rescaleFactors))
    {
      return true;
    }
  }
  return false;
} // end IsRescaleUsed()


/**
 * ******************* PrintSelf ***********************
 */

template <class TInputImage, class TOutputImage, class TPrecisionType>
void
GenericMultiResolutionPyramidImageFilter<TInputImage, TOutputImage, TPrecisionType>::PrintSelf(std::ostream & os,
                                                                                               Indent indent) const
{
  Superclass::PrintSelf(os, indent);

  os << indent << "CurrentLevel: " << this->m_CurrentLevel << std::endl;
  os << indent << "ComputeOnlyForCurrentLevel: " << (this->m_ComputeOnlyForCurrentLevel ? "true" : "false")
     << std::endl;
  os << indent << "SmoothingScheduleDefined: " << (this->m_SmoothingScheduleDefined ? "true" : "false") << std::endl;
  os << indent << "Smoothing Schedule: ";
  if (this->m_SmoothingSchedule.empty())
  {
    os << "Not set" << std::endl;
  }
  else
  {
    os << '\n' << this->m_SmoothingSchedule << std::endl;
  }
} // end PrintSelf()


} // end namespace itk

#endif // end #ifndef itkGenericMultiResolutionPyramidImageFilter_hxx