1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2013-2015 Imperial College London
5  * Copyright 2013-2015 Andreas Schuh
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef MIRTK_MirrorExtrapolateImageFunction_H
21 #define MIRTK_MirrorExtrapolateImageFunction_H
22 
23 #include "mirtk/ExtrapolateImageFunction.h"
24 #include "mirtk/BaseImage.h"
25 
26 
27 namespace mirtk {
28 
29 
30 /**
31  * Extrapolation of generic image by mirroring along the boundaries
32  */
33 template <class TImage>
34 class GenericMirrorExtrapolateImageFunction
35 : public IndexExtrapolateImageFunction<TImage>
36 {
37   mirtkExtrapolatorMacro(
38     GenericMirrorExtrapolateImageFunction,
39     Extrapolation_Mirror
40   );
41 
42 public:
43 
44   /// Constructor
GenericMirrorExtrapolateImageFunction()45   GenericMirrorExtrapolateImageFunction() {}
46 
47   /// Destructor
~GenericMirrorExtrapolateImageFunction()48   virtual ~GenericMirrorExtrapolateImageFunction() {}
49 
50   /// Mirror index at boundary such that it is inside the range [0, max]
51   /// \note Use static function as MirrorExtrapolateImageFunction::Apply.
Apply(int & index,int max)52   static void Apply(int &index, int max)
53   {
54     if (max == 0) {
55       index = 0;
56     } else if (index < 0) {
57       index = -index;
58       int n = index / max;
59       int m = index - n * max;
60       if (n & 1) index = max - m;
61       else       index = m;
62     } else if (index > max) {
63       index -= max;
64       int n = index / max;
65       int m = index - n * max;
66       if (n & 1) index = m;
67       else       index = max - m;
68     }
69   }
70 
71   /// Mirror index at boundary such that it is inside the range [0, max]
TransformIndex(int & index,int max)72   virtual void TransformIndex(int &index, int max) const
73   {
74     Apply(index, max);
75   }
76 
77 };
78 
79 
80 /**
81  * Extrapolation of any image by mirroring along the boundaries
82  */
83 class MirrorExtrapolateImageFunction
84 : public GenericMirrorExtrapolateImageFunction<BaseImage>
85 {
86   mirtkObjectMacro(MirrorExtrapolateImageFunction);
87 
88 public:
89 
90   /// Constructor
MirrorExtrapolateImageFunction()91   MirrorExtrapolateImageFunction() {}
92 
93   /// Destructor
~MirrorExtrapolateImageFunction()94   virtual ~MirrorExtrapolateImageFunction() {}
95 
96 };
97 
98 
99 } // namespace mirtk
100 
101 #endif // MIRTK_MirrorExtrapolateImageFunction_H
102