InterpolationMode.h
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_InterpolationMode_H
21 #define MIRTK_InterpolationMode_H
22 
23 #include "mirtk/String.h"
24 
25 
26 namespace mirtk {
27 
28 
29 // ----------------------------------------------------------------------------
30 /// Image interpolation modes
32  Interpolation_Default,
33  Interpolation_NN,
34  Interpolation_Linear,
35  Interpolation_FastLinear,
36  Interpolation_BSpline,
37  Interpolation_CubicBSpline,
38  Interpolation_FastCubicBSpline,
39  Interpolation_CSpline,
40  Interpolation_SBased,
41  Interpolation_Sinc,
42  Interpolation_Gaussian,
43  Interpolation_NNWithPadding,
44  Interpolation_LinearWithPadding,
45  Interpolation_FastLinearWithPadding,
46  Interpolation_BSplineWithPadding,
47  Interpolation_CubicBSplineWithPadding,
48  Interpolation_FastCubicBSplineWithPadding,
49  Interpolation_CSplineWithPadding,
50  Interpolation_SBasedWithPadding,
51  Interpolation_SincWithPadding,
52  Interpolation_GaussianWithPadding,
53  // Add new enumeration values above
54  Interpolation_Last
55 };
56 
57 // ----------------------------------------------------------------------------
58 /// Get default interpolation mode
60 {
61  return Interpolation_FastLinear;
62 }
63 
64 // ----------------------------------------------------------------------------
65 /// Get corresponding interpolation with padding
67 {
68  switch(m) {
69  case Interpolation_NN: return Interpolation_NNWithPadding;
70  case Interpolation_Linear: return Interpolation_LinearWithPadding;
71  case Interpolation_FastLinear: return Interpolation_FastLinearWithPadding;
72  case Interpolation_BSpline: return Interpolation_BSplineWithPadding;
73  case Interpolation_CubicBSpline: return Interpolation_CubicBSplineWithPadding;
74  case Interpolation_FastCubicBSpline: return Interpolation_FastCubicBSplineWithPadding;
75  case Interpolation_CSpline: return Interpolation_CSplineWithPadding;
76  case Interpolation_SBased: return Interpolation_SBasedWithPadding;
77  case Interpolation_Sinc: return Interpolation_SincWithPadding;
78  case Interpolation_Gaussian: return Interpolation_GaussianWithPadding;
79  default: return m;
80  }
81 }
82 
83 // ----------------------------------------------------------------------------
84 /// Get corresponding interpolation without padding
86 {
87  switch(m) {
88  case Interpolation_NNWithPadding: return Interpolation_NN;
89  case Interpolation_LinearWithPadding: return Interpolation_Linear;
90  case Interpolation_FastLinearWithPadding: return Interpolation_FastLinear;
91  case Interpolation_BSplineWithPadding: return Interpolation_BSpline;
92  case Interpolation_CubicBSplineWithPadding: return Interpolation_CubicBSpline;
93  case Interpolation_FastCubicBSplineWithPadding: return Interpolation_FastCubicBSpline;
94  case Interpolation_CSplineWithPadding: return Interpolation_CSpline;
95  case Interpolation_SBasedWithPadding: return Interpolation_SBased;
96  case Interpolation_SincWithPadding: return Interpolation_Sinc;
97  case Interpolation_GaussianWithPadding: return Interpolation_Gaussian;
98  default: return m;
99  }
100 }
101 
102 // ----------------------------------------------------------------------------
103 /// Whether interpolation mode is "with padding"
105 {
106  return InterpolationWithPadding(m) == m;
107 }
108 
109 // ----------------------------------------------------------------------------
110 /// Whether interpolation mode is "without padding"
112 {
113  return InterpolationWithoutPadding(m) == m;
114 }
115 
116 // ----------------------------------------------------------------------------
117 template <>
118 inline string ToString(const InterpolationMode &m, int w, char c, bool left)
119 {
120  string str;
121  const auto mode = InterpolationWithoutPadding(m);
122  switch(mode) {
123  case Interpolation_Default: str = "Default"; break;
124  case Interpolation_NN: str = "NN"; break;
125  case Interpolation_Linear: str = "Linear"; break;
126  case Interpolation_FastLinear: str = "Fast linear"; break;
127  case Interpolation_BSpline: str = "BSpline"; break;
128  case Interpolation_CSpline: str = "CSpline"; break;
129  case Interpolation_CubicBSpline: str = "Cubic BSpline"; break;
130  case Interpolation_FastCubicBSpline: str = "Fast cubic BSpline"; break;
131  case Interpolation_SBased: str = "SBased"; break;
132  case Interpolation_Sinc: str = "Sinc"; break;
133  case Interpolation_Gaussian: str = "Gaussian"; break;
134  default: str = "Unknown"; break;
135  }
136  if (mode != m) str += " with padding";
137  return ToString(str, w, c, left);
138 }
139 
140 // ----------------------------------------------------------------------------
141 template <>
142 inline bool FromString(const char *str, InterpolationMode &m)
143 {
144  string lstr = ToLower(str);
145  bool with_padding = false;
146  auto pos = lstr.find(" (with padding)");
147  if (pos != string::npos) {
148  lstr.erase(pos, 15);
149  with_padding = true;
150  } else {
151  pos = lstr.find(" with padding");
152  if (pos != string::npos) {
153  lstr.erase(pos, 13);
154  with_padding = true;
155  } else {
156  pos = lstr.find(" (without padding)");
157  if (pos != string::npos) {
158  lstr.erase(pos, 18);
159  } else {
160  pos = lstr.find(" without padding");
161  if (pos != string::npos) lstr.erase(pos, 16);
162  }
163  }
164  }
165  if (lstr == "default") m = Interpolation_Default;
166  else if (lstr == "nn") m = Interpolation_NN;
167  else if (lstr == "linear") m = Interpolation_Linear;
168  else if (lstr == "fast linear") m = Interpolation_FastLinear;
169  else if (lstr == "bspline") m = Interpolation_BSpline;
170  else if (lstr == "b-spline") m = Interpolation_BSpline;
171  else if (lstr == "cspline") m = Interpolation_CSpline;
172  else if (lstr == "c-spline") m = Interpolation_CSpline;
173  else if (lstr == "cubic bspline") m = Interpolation_CubicBSpline;
174  else if (lstr == "cubic b-spline") m = Interpolation_CubicBSpline;
175  else if (lstr == "fast cubic bspline") m = Interpolation_FastCubicBSpline;
176  else if (lstr == "fast cubic b-spline") m = Interpolation_FastCubicBSpline;
177  else if (lstr == "sbased") m = Interpolation_SBased;
178  else if (lstr == "shapebased") m = Interpolation_SBased;
179  else if (lstr == "shape-based") m = Interpolation_SBased;
180  else if (lstr == "sinc") m = Interpolation_Sinc;
181  else if (lstr == "gaussian") m = Interpolation_Gaussian;
182  else return false;
183  if (with_padding) {
185  }
186  return true;
187 }
188 
189 
190 } // namespace mirtk
191 
192 #endif // MIRTK_InterpolationMode_H
bool IsInterpolationWithPadding(InterpolationMode m)
Whether interpolation mode is "with padding".
InterpolationMode DefaultInterpolationMode()
Get default interpolation mode.
InterpolationMode
Image interpolation modes.
InterpolationMode InterpolationWithPadding(InterpolationMode m)
Get corresponding interpolation with padding.
string ToLower(const string &)
Convert string to lowercase letters.
Definition: IOConfig.h:41
InterpolationMode InterpolationWithoutPadding(InterpolationMode m)
Get corresponding interpolation without padding.
bool IsInterpolationWithoutPadding(InterpolationMode m)
Whether interpolation mode is "without padding".
string ToString(const EnergyMeasure &value, int w, char c, bool left)
Convert energy measure enumeration value to string.
bool FromString(const char *str, EnergyMeasure &value)
Convert energy measure string to enumeration value.