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
63
65 static itk::OptimizerParameters<double>
66 ToOptimizerParameters(const std::vector<double> &);
67
69 static std::string
71
73 static std::string
74 ToString(const bool arg)
75 {
76 return BoolToString(arg);
77 }
78
80 static std::string
81 ToString(double);
82
84 static std::string
85 ToString(float);
86
88 template <typename TInteger>
89 static std::string
90 ToString(const TInteger integerValue)
91 {
92 static_assert(std::is_integral<TInteger>::value, "An integer type expected!");
93 static_assert(!std::is_same<TInteger, bool>::value, "No bool expected!");
94 return std::to_string(integerValue);
95 }
96
97
106 template <typename TContainer, typename SFINAE = typename TContainer::iterator>
107 static std::vector<std::string>
108 ToVectorOfStrings(const TContainer & container)
109 {
110 std::vector<std::string> result;
111
112 result.reserve(container.size());
113
114 for (const auto element : container)
115 {
116 result.push_back(Conversion::ToString(element));
117 }
118 return result;
119 }
120
124 template <typename T, unsigned int NRows, unsigned int NColumns>
125 static std::vector<std::string>
126 ToVectorOfStrings(const itk::Matrix<T, NRows, NColumns> & matrix)
127 {
128 std::vector<std::string> result;
129 result.reserve(NColumns * NRows);
130
131 for (unsigned column{}; column < NColumns; ++column)
132 {
133 for (unsigned row{}; row < NRows; ++row)
134 {
135 result.push_back(Conversion::ToString(matrix(row, column)));
136 }
137 }
138 return result;
139 }
140
141
143 template <typename TValue>
144 static std::vector<TValue>
145 ConcatenateVectors(std::vector<TValue> vector1, std::vector<TValue> vector2)
146 {
147 vector1.insert(end(vector1), std::make_move_iterator(begin(vector2)), std::make_move_iterator(end(vector2)));
148 return vector1;
149 }
150
151
156 static bool
157 IsNumber(const std::string &);
158
159
162 static std::string
163 ToNativePathNameSeparators(const std::string &);
164
169 template <class T>
170 static bool
171 StringToValue(const std::string & str, T & value)
172 {
173 // Conversion to bool is supported by another StringToValue overload.
174 static_assert(!std::is_same<T, bool>::value, "This StringToValue<T> overload does not support bool!");
175
176 // 8-bits (signed/unsigned) char types are supported by other StringToValue
177 // overloads.
178 static_assert(sizeof(T) > 1, "This StringToValue<T> overload does not support (signed/unsigned) char!");
179
180 auto inputStream = [&str] {
181 const auto decimalPointPos = str.find_first_of('.');
182 const bool hasDecimalPointAndTrailingZeros =
183 (decimalPointPos != std::string::npos) &&
184 (std::count(str.cbegin() + decimalPointPos + 1, str.cend(), '0') == (str.size() - decimalPointPos - 1));
185 return std::istringstream(
186 hasDecimalPointAndTrailingZeros ? std::string(str.cbegin(), str.cbegin() + decimalPointPos) : str);
187 }();
188
189 // Note: `inputStream >> value` evaluates to false when the `badbit` or the `failbit` is set.
190 return (inputStream >> value) && inputStream.eof();
191
192 } // end StringToValue()
193
194
198 static bool
199 StringToValue(const std::string & str, std::string & value);
200
202 static bool
203 StringToValue(const std::string & str, double & value);
204
205 static bool
206 StringToValue(const std::string & str, float & value);
210 static bool
211 StringToValue(const std::string & str, char & value);
212
213 static bool
214 StringToValue(const std::string & str, signed char & value);
215
216 static bool
217 StringToValue(const std::string & str, unsigned char & value);
221 static bool
222 StringToValue(const std::string & str, bool & value);
223};
224
225} // end namespace elastix
226
227#endif // end #ifndef elxConversion_h
A class that contains utility functions for the conversion of number of seconds and parameter values ...
Definition: elxConversion.h:45
static std::string ToString(float)
static std::vector< TValue > ConcatenateVectors(std::vector< TValue > vector1, std::vector< TValue > vector2)
std::map< std::string, ParameterValuesType > ParameterMapType
Definition: elxConversion.h:49
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)
std::vector< std::string > ParameterValuesType
Definition: elxConversion.h:48
static bool StringToValue(const std::string &str, bool &value)
static std::string ToString(const bool arg)
Definition: elxConversion.h:74
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)
Definition: elxConversion.h:90
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)
Definition: elxConversion.h:58
class ITK_TEMPLATE_EXPORT OptimizerParameters
Definition: elxConversion.h:33


Generated on 2023-01-13 for elastix by doxygen 1.9.6 elastix logo