Loading...
Searching...
No Matches
elxConversion.h
Go to the documentation of this file.
/*=========================================================================
*
* Copyright UMC Utrecht and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef elxConversion_h
#define elxConversion_h
#include "itkMatrix.h"
#include <iterator>
#include <map>
#include <string>
#include <type_traits> // For is_integral and is_same.
#include <vector>
namespace itk
{
// Forward declaration from ITK header <itkOptimizerParameters.h>.
template <typename>
class ITK_TEMPLATE_EXPORT OptimizerParameters;
} // namespace itk
namespace elastix
{
class Conversion
{
public:
using ParameterValuesType = std::vector<std::string>;
using ParameterMapType = std::map<std::string, ParameterValuesType>;
using Self = Conversion;
static std::string
SecondsToDHMS(const double totalSeconds, const unsigned int precision);
static constexpr const char *
BoolToString(const bool arg)
{
return arg ? "true" : "false";
}
static std::string
ObjectPtrToString(itk::Object *);
static itk::OptimizerParameters<double>
ToOptimizerParameters(const std::vector<double> &);
static std::string
ParameterMapToString(const ParameterMapType &);
static std::string
ToString(const bool arg)
{
return BoolToString(arg);
}
static std::string
ToString(double);
static std::string
ToString(float);
template <typename TInteger>
static std::string
ToString(const TInteger integerValue)
{
static_assert(std::is_integral_v<TInteger>, "An integer type expected!");
static_assert(!std::is_same_v<TInteger, bool>, "No bool expected!");
return std::to_string(integerValue);
}
template <typename TContainer, typename SFINAE = typename TContainer::iterator>
static std::vector<std::string>
ToVectorOfStrings(const TContainer & container)
{
std::vector<std::string> result;
result.reserve(container.size());
for (const auto element : container)
{
result.push_back(Conversion::ToString(element));
}
return result;
}
template <typename T, unsigned int NRows, unsigned int NColumns>
static std::vector<std::string>
ToVectorOfStrings(const itk::Matrix<T, NRows, NColumns> & matrix)
{
std::vector<std::string> result;
result.reserve(NColumns * NRows);
for (unsigned column{}; column < NColumns; ++column)
{
for (unsigned row{}; row < NRows; ++row)
{
result.push_back(Conversion::ToString(matrix(row, column)));
}
}
return result;
}
template <typename TValue>
static std::vector<TValue>
ConcatenateVectors(std::vector<TValue> vector1, std::vector<TValue> vector2)
{
vector1.insert(end(vector1), std::make_move_iterator(begin(vector2)), std::make_move_iterator(end(vector2)));
return vector1;
}
static bool
IsNumber(const std::string &);
static std::string
ToNativePathNameSeparators(const std::string &);
template <class T>
static bool
StringToValue(const std::string & str, T & value)
{
// Conversion to bool is supported by another StringToValue overload.
static_assert(!std::is_same_v<T, bool>, "This StringToValue<T> overload does not support bool!");
// 8-bits (signed/unsigned) char types are supported by other StringToValue
// overloads.
static_assert(sizeof(T) > 1, "This StringToValue<T> overload does not support (signed/unsigned) char!");
if (std::is_unsigned_v<T> && (!str.empty()) && (str.front() == '-'))
{
// An unsigned value should not start with a minus sign!
return false;
}
auto inputStream = [&str] {
const auto decimalPointPos = str.find_first_of('.');
const bool hasDecimalPointAndTrailingZeros =
(decimalPointPos != std::string::npos) &&
(static_cast<std::uintmax_t>(std::count(str.cbegin() + decimalPointPos + 1, str.cend(), '0')) ==
(str.size() - decimalPointPos - 1));
return std::istringstream(
hasDecimalPointAndTrailingZeros ? std::string(str.cbegin(), str.cbegin() + decimalPointPos) : str);
}();
// Note: `inputStream >> value` evaluates to false when the `badbit` or the `failbit` is set.
return (inputStream >> value) && inputStream.eof();
} // end StringToValue()
static bool
StringToValue(const std::string & str, std::string & value);
static bool
StringToValue(const std::string & str, double & value);
static bool
StringToValue(const std::string & str, float & value);
static bool
StringToValue(const std::string & str, char & value);
static bool
StringToValue(const std::string & str, signed char & value);
static bool
StringToValue(const std::string & str, unsigned char & value);
static bool
StringToValue(const std::string & str, bool & value);
static bool
StringToValue(const std::string & str, itk::Object *& value);
};
} // end namespace elastix
#endif // end #ifndef elxConversion_h
elastix::Conversion
A class that contains utility functions for the conversion of number of seconds and parameter values ...
Definition elxConversion.h:45
elastix::Conversion::ToString
static std::string ToString(float)
elastix::Conversion::ConcatenateVectors
static std::vector< TValue > ConcatenateVectors(std::vector< TValue > vector1, std::vector< TValue > vector2)
Definition elxConversion.h:148
elastix::Conversion::ParameterMapType
std::map< std::string, ParameterValuesType > ParameterMapType
Definition elxConversion.h:49
elastix::Conversion::ToVectorOfStrings
static std::vector< std::string > ToVectorOfStrings(const TContainer &container)
Definition elxConversion.h:111
elastix::Conversion::ToVectorOfStrings
static std::vector< std::string > ToVectorOfStrings(const itk::Matrix< T, NRows, NColumns > &matrix)
Definition elxConversion.h:129
elastix::Conversion::StringToValue
static bool StringToValue(const std::string &str, T &value)
Definition elxConversion.h:174
elastix::Conversion::ToString
static std::string ToString(double)
elastix::Conversion::SecondsToDHMS
static std::string SecondsToDHMS(const double totalSeconds, const unsigned int precision)
elastix::Conversion::StringToValue
static bool StringToValue(const std::string &str, std::string &value)
elastix::Conversion::ObjectPtrToString
static std::string ObjectPtrToString(itk::Object *)
elastix::Conversion::ParameterValuesType
std::vector< std::string > ParameterValuesType
Definition elxConversion.h:48
elastix::Conversion::StringToValue
static bool StringToValue(const std::string &str, itk::Object *&value)
elastix::Conversion::StringToValue
static bool StringToValue(const std::string &str, bool &value)
elastix::Conversion::ToString
static std::string ToString(const bool arg)
Definition elxConversion.h:77
elastix::Conversion::StringToValue
static bool StringToValue(const std::string &str, float &value)
elastix::Conversion::StringToValue
static bool StringToValue(const std::string &str, char &value)
elastix::Conversion::Self
Conversion Self
Definition elxConversion.h:50
elastix::Conversion::IsNumber
static bool IsNumber(const std::string &)
elastix::Conversion::ToOptimizerParameters
static itk::OptimizerParameters< double > ToOptimizerParameters(const std::vector< double > &)
elastix::Conversion::ToString
static std::string ToString(const TInteger integerValue)
Definition elxConversion.h:93
elastix::Conversion::ParameterMapToString
static std::string ParameterMapToString(const ParameterMapType &)
elastix::Conversion::StringToValue
static bool StringToValue(const std::string &str, unsigned char &value)
elastix::Conversion::StringToValue
static bool StringToValue(const std::string &str, double &value)
elastix::Conversion::ToNativePathNameSeparators
static std::string ToNativePathNameSeparators(const std::string &)
elastix::Conversion::StringToValue
static bool StringToValue(const std::string &str, signed char &value)
elastix::Conversion::BoolToString
static constexpr const char * BoolToString(const bool arg)
Definition elxConversion.h:58
elastix
Definition elxDefaultConstruct.h:25
itk
Definition itkAdvancedImageToImageMetric.h:43
itk::OptimizerParameters
class ITK_TEMPLATE_EXPORT OptimizerParameters
Definition elxConversion.h:33

Generated on 1768596610 for elastix by 1.15.0 |