go home Home | Main Page | Topics | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals | Related Pages
Loading...
Searching...
No Matches
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{
43
51{
52public:
54 using ParameterValuesType = std::vector<std::string>;
55 using ParameterMapType = std::map<std::string, ParameterValuesType>;
57
59 static std::string
60 SecondsToDHMS(const double totalSeconds, const unsigned int precision);
61
63 static constexpr const char *
64 BoolToString(const bool arg)
65 {
66 return arg ? "true" : "false";
67 }
68
70 static std::string
71 ObjectPtrToString(itk::Object *);
72
75 ToOptimizerParameters(const std::vector<double> &);
76
78 static std::string
80
82 static std::string_view
84 {
85 return format == ParameterMapStringFormat::Toml ? "#" : "//";
86 }
87
89 static std::string
90 ToString(const bool arg)
91 {
92 return BoolToString(arg);
93 }
94
96 static std::string
97 ToString(double);
98
100 static std::string
101 ToString(float);
102
104 template <typename TInteger>
105 static std::string
106 ToString(const TInteger integerValue)
107 {
108 static_assert(std::is_integral_v<TInteger>, "An integer type expected!");
109 static_assert(!std::is_same_v<TInteger, bool>, "No bool expected!");
110 return std::to_string(integerValue);
111 }
112
113
122 template <typename TContainer, typename SFINAE = typename TContainer::iterator>
123 static std::vector<std::string>
124 ToVectorOfStrings(const TContainer & container)
125 {
126 std::vector<std::string> result;
127
128 result.reserve(container.size());
129
130 for (const auto element : container)
131 {
132 result.push_back(Conversion::ToString(element));
133 }
134 return result;
135 }
136
140 template <typename T, unsigned int NRows, unsigned int NColumns>
141 static std::vector<std::string>
142 ToVectorOfStrings(const itk::Matrix<T, NRows, NColumns> & matrix)
143 {
144 std::vector<std::string> result;
145 result.reserve(NColumns * NRows);
146
147 for (unsigned column{}; column < NColumns; ++column)
148 {
149 for (unsigned row{}; row < NRows; ++row)
150 {
151 result.push_back(Conversion::ToString(matrix(row, column)));
152 }
153 }
154 return result;
155 }
156
157
159 template <typename TValue>
160 static std::vector<TValue>
161 ConcatenateVectors(std::vector<TValue> vector1, std::vector<TValue> vector2)
162 {
163 vector1.insert(end(vector1), std::make_move_iterator(begin(vector2)), std::make_move_iterator(end(vector2)));
164 return vector1;
165 }
166
167
172 static bool
173 IsNumber(const std::string &);
174
175
178 static std::string
179 ToNativePathNameSeparators(const std::string &);
180
185 template <typename T>
186 static bool
187 StringToValue(const std::string & str, T & value)
188 {
189 // Conversion to bool is supported by another StringToValue overload.
190 static_assert(!std::is_same_v<T, bool>, "This StringToValue<T> overload does not support bool!");
191
192 // 8-bits (signed/unsigned) char types are supported by other StringToValue
193 // overloads.
194 static_assert(sizeof(T) > 1, "This StringToValue<T> overload does not support (signed/unsigned) char!");
195
196 if (std::is_unsigned_v<T> && (!str.empty()) && (str.front() == '-'))
197 {
198 // An unsigned value should not start with a minus sign!
199 return false;
200 }
201
202 auto inputStream = [&str] {
203 const auto decimalPointPos = str.find_first_of('.');
204 const bool hasDecimalPointAndTrailingZeros =
205 (decimalPointPos != std::string::npos) &&
206 (static_cast<std::uintmax_t>(std::count(str.cbegin() + decimalPointPos + 1, str.cend(), '0')) ==
207 (str.size() - decimalPointPos - 1));
208 return std::istringstream(
209 hasDecimalPointAndTrailingZeros ? std::string(str.cbegin(), str.cbegin() + decimalPointPos) : str);
210 }();
211
212 // Note: `inputStream >> value` evaluates to false when the `badbit` or the `failbit` is set.
213 return (inputStream >> value) && inputStream.eof();
214
215 } // end StringToValue()
216
217
221 static bool
222 StringToValue(const std::string & str, std::string & value);
223
225 static bool
226 StringToValue(const std::string & str, double & value);
227
228 static bool
229 StringToValue(const std::string & str, float & value);
231
233 static bool
234 StringToValue(const std::string & str, char & value);
235
236 static bool
237 StringToValue(const std::string & str, signed char & value);
238
239 static bool
240 StringToValue(const std::string & str, unsigned char & value);
242
244 static bool
245 StringToValue(const std::string & str, bool & value);
246
249 static bool
250 StringToValue(const std::string & str, itk::Object *& value);
251
255 StringToParameterMapStringFormat(const std::string & str);
256
258 static std::string
260};
261
262} // end namespace elastix
263
264#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::string_view ParameterMapStartOfCommentString(const ParameterMapStringFormat format)
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 ParameterMapStringFormat StringToParameterMapStringFormat(const std::string &str)
static bool StringToValue(const std::string &str, float &value)
static bool StringToValue(const std::string &str, char &value)
static std::string CreateParameterMapFileNameExtension(const ParameterMapStringFormat format)
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 &, const ParameterMapStringFormat)
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)
ParameterMapStringFormat
class ITK_TEMPLATE_EXPORT OptimizerParameters


Generated on 1774142652 for elastix by doxygen 1.15.0 elastix logo