go home Home | Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals | Related Pages
elxConversion.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright UMC Utrecht and contributors
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef elxConversion_h
19#define elxConversion_h
20
21#include "itkMatrix.h"
22
23#include <iterator>
24#include <map>
25#include <string>
26#include <type_traits> // For is_integral and is_same.
27#include <vector>
28
29namespace itk
30{
31// Forward declaration from ITK header <itkOptimizerParameters.h>.
32template <typename>
33class ITK_TEMPLATE_EXPORT OptimizerParameters;
34} // namespace itk
35
36namespace elastix
37{
45{
46public:
48 using ParameterValuesType = std::vector<std::string>;
49 using ParameterMapType = std::map<std::string, ParameterValuesType>;
51
53 static std::string
54 SecondsToDHMS(const double totalSeconds, const unsigned int precision);
55
57 static constexpr const char *
58 BoolToString(const bool arg)
59 {
60 return arg ? "true" : "false";
61 }
62
64 static std::string
65 ObjectPtrToString(itk::Object *);
66
69 ToOptimizerParameters(const std::vector<double> &);
70
72 static std::string
74
76 static std::string
77 ToString(const bool arg)
78 {
79 return BoolToString(arg);
80 }
81
83 static std::string
84 ToString(double);
85
87 static std::string
88 ToString(float);
89
91 template <typename TInteger>
92 static std::string
93 ToString(const TInteger integerValue)
94 {
95 static_assert(std::is_integral_v<TInteger>, "An integer type expected!");
96 static_assert(!std::is_same_v<TInteger, bool>, "No bool expected!");
97 return std::to_string(integerValue);
98 }
99
100
109 template <typename TContainer, typename SFINAE = typename TContainer::iterator>
110 static std::vector<std::string>
111 ToVectorOfStrings(const TContainer & container)
112 {
113 std::vector<std::string> result;
114
115 result.reserve(container.size());
116
117 for (const auto element : container)
118 {
119 result.push_back(Conversion::ToString(element));
120 }
121 return result;
122 }
123
127 template <typename T, unsigned int NRows, unsigned int NColumns>
128 static std::vector<std::string>
129 ToVectorOfStrings(const itk::Matrix<T, NRows, NColumns> & matrix)
130 {
131 std::vector<std::string> result;
132 result.reserve(NColumns * NRows);
133
134 for (unsigned column{}; column < NColumns; ++column)
135 {
136 for (unsigned row{}; row < NRows; ++row)
137 {
138 result.push_back(Conversion::ToString(matrix(row, column)));
139 }
140 }
141 return result;
142 }
143
144
146 template <typename TValue>
147 static std::vector<TValue>
148 ConcatenateVectors(std::vector<TValue> vector1, std::vector<TValue> vector2)
149 {
150 vector1.insert(end(vector1), std::make_move_iterator(begin(vector2)), std::make_move_iterator(end(vector2)));
151 return vector1;
152 }
153
154
159 static bool
160 IsNumber(const std::string &);
161
162
165 static std::string
166 ToNativePathNameSeparators(const std::string &);
167
172 template <class T>
173 static bool
174 StringToValue(const std::string & str, T & value)
175 {
176 // Conversion to bool is supported by another StringToValue overload.
177 static_assert(!std::is_same_v<T, bool>, "This StringToValue<T> overload does not support bool!");
178
179 // 8-bits (signed/unsigned) char types are supported by other StringToValue
180 // overloads.
181 static_assert(sizeof(T) > 1, "This StringToValue<T> overload does not support (signed/unsigned) char!");
182
183 if (std::is_unsigned_v<T> && (!str.empty()) && (str.front() == '-'))
184 {
185 // An unsigned value should not start with a minus sign!
186 return false;
187 }
188
189 auto inputStream = [&str] {
190 const auto decimalPointPos = str.find_first_of('.');
191 const bool hasDecimalPointAndTrailingZeros =
192 (decimalPointPos != std::string::npos) &&
193 (static_cast<std::uintmax_t>(std::count(str.cbegin() + decimalPointPos + 1, str.cend(), '0')) ==
194 (str.size() - decimalPointPos - 1));
195 return std::istringstream(
196 hasDecimalPointAndTrailingZeros ? std::string(str.cbegin(), str.cbegin() + decimalPointPos) : str);
197 }();
198
199 // Note: `inputStream >> value` evaluates to false when the `badbit` or the `failbit` is set.
200 return (inputStream >> value) && inputStream.eof();
201
202 } // end StringToValue()
203
204
208 static bool
209 StringToValue(const std::string & str, std::string & value);
210
212 static bool
213 StringToValue(const std::string & str, double & value);
214
215 static bool
216 StringToValue(const std::string & str, float & value);
220 static bool
221 StringToValue(const std::string & str, char & value);
222
223 static bool
224 StringToValue(const std::string & str, signed char & value);
225
226 static bool
227 StringToValue(const std::string & str, unsigned char & value);
231 static bool
232 StringToValue(const std::string & str, bool & value);
233
236 static bool
237 StringToValue(const std::string & str, itk::Object *& value);
238};
239
240} // end namespace elastix
241
242#endif // end #ifndef elxConversion_h
A class that contains utility functions for the conversion of number of seconds and parameter values ...
static std::string ToString(float)
static std::vector< TValue > ConcatenateVectors(std::vector< TValue > vector1, std::vector< TValue > vector2)
std::map< std::string, ParameterValuesType > ParameterMapType
static std::vector< std::string > ToVectorOfStrings(const TContainer &container)
static std::vector< std::string > ToVectorOfStrings(const itk::Matrix< T, NRows, NColumns > &matrix)
static bool StringToValue(const std::string &str, T &value)
static std::string ToString(double)
static std::string SecondsToDHMS(const double totalSeconds, const unsigned int precision)
static bool StringToValue(const std::string &str, std::string &value)
static std::string ObjectPtrToString(itk::Object *)
std::vector< std::string > ParameterValuesType
static bool StringToValue(const std::string &str, itk::Object *&value)
static bool StringToValue(const std::string &str, bool &value)
static std::string ToString(const bool arg)
static bool StringToValue(const std::string &str, float &value)
static bool StringToValue(const std::string &str, char &value)
static bool IsNumber(const std::string &)
static itk::OptimizerParameters< double > ToOptimizerParameters(const std::vector< double > &)
static std::string ToString(const TInteger integerValue)
static std::string ParameterMapToString(const ParameterMapType &)
static bool StringToValue(const std::string &str, unsigned char &value)
static bool StringToValue(const std::string &str, double &value)
static std::string ToNativePathNameSeparators(const std::string &)
static bool StringToValue(const std::string &str, signed char &value)
static constexpr const char * BoolToString(const bool arg)
class ITK_TEMPLATE_EXPORT OptimizerParameters


Generated on 2024-07-17 for elastix by doxygen 1.11.0 (9b424b03c9833626cd435af22a444888fbbb192d) elastix logo