ITK  6.0.0
Insight Toolkit
itkNumericTraitsStdVector.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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  * https://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 
19 #ifndef itkNumericTraitsStdVector_h
20 #define itkNumericTraitsStdVector_h
21 
22 #include "itkMath.h"
23 #include <vector>
24 
25 // This work is part of the National Alliance for Medical Image Computing
26 // (NAMIC), funded by the National Institutes of Health through the NIH Roadmap
27 // for Medical Research, Grant U54 EB005149.
28 
29 namespace itk
30 {
55 template <typename T>
56 class NumericTraits<std::vector<T>>
57 {
58 public:
64 
66  using ValueType = T;
67 
68  using Self = std::vector<T>;
69 
71  using AbsType = std::vector<ElementAbsType>;
72 
74  using AccumulateType = std::vector<ElementAccumulateType>;
75 
78  using FloatType = std::vector<ElementFloatType>;
79 
80  // TODO: this won't really print well, at least not without defining an operator
81  // to push to a stream.
83  using PrintType = std::vector<ElementPrintType>;
84 
86  using RealType = std::vector<ElementRealType>;
87 
90 
93 
99  static const Self
100  max(const Self & a)
101  {
102  Self b(a.Size(), NumericTraits<T>::max());
103  return b;
104  }
107  static const Self
108  min(const Self & a)
109  {
110  Self b(a.Size(), NumericTraits<T>::min());
111  return b;
112  }
113 
114  static const Self
115  ZeroValue(const Self & a)
116  {
117  Self b(a.Size(), T{});
118  return b;
119  }
120 
121  static const Self
122  OneValue(const Self & a)
123  {
124  Self b(a.Size(), NumericTraits<T>::OneValue());
125  return b;
126  }
127 
128  static const Self
129  NonpositiveMin(const Self & a)
130  {
131  Self b(a.Size(), NumericTraits<T>::NonpositiveMin());
132  return b;
133  }
134 
135  static constexpr bool IsSigned = std::is_signed_v<ValueType>;
136  static constexpr bool IsInteger = std::is_integral_v<ValueType>;
138 
140  static void
141  SetLength(std::vector<T> & m, const unsigned int s)
142  {
143  // since std::vector often holds types that have no NumericTraits::ZeroValue(),
144  // allow resize() to call the type's default constructor
145  m.clear();
146  m.resize(s);
147  }
151  static unsigned int
152  GetLength(const std::vector<T> & m)
153  {
154  return itk::Math::CastWithRangeCheck<unsigned int>(m.size());
155  }
156 
157  static void
159  {
160  mv = v;
161  }
162 
163  template <typename TArray>
164  static void
165  AssignToArray(const Self & v, TArray & mv)
166  {
167  for (unsigned int i = 0; i < GetLength(v); ++i)
168  {
169  mv[i] = v[i];
170  }
171  }
172 };
173 } // end namespace itk
174 
175 #endif // itkNumericTraitsStdVector_h
itk::NumericTraits< std::vector< T > >::FloatType
std::vector< ElementFloatType > FloatType
Definition: itkNumericTraitsStdVector.h:78
itk::NumericTraits< std::vector< T > >::ElementAbsType
typename NumericTraits< T >::AbsType ElementAbsType
Definition: itkNumericTraitsStdVector.h:59
itk::NumericTraits< std::vector< T > >::RealType
std::vector< ElementRealType > RealType
Definition: itkNumericTraitsStdVector.h:86
Self
AddImageFilter Self
Definition: itkAddImageFilter.h:89
itk::NumericTraits< std::vector< T > >::AbsType
std::vector< ElementAbsType > AbsType
Definition: itkNumericTraitsStdVector.h:71
itk::NumericTraits::NonpositiveMin
static constexpr T NonpositiveMin()
Definition: itkNumericTraits.h:99
itk::NumericTraits< std::vector< T > >::AccumulateType
std::vector< ElementAccumulateType > AccumulateType
Definition: itkNumericTraitsStdVector.h:74
itk::NumericTraits< std::vector< T > >::OneValue
static const Self OneValue(const Self &a)
Definition: itkNumericTraitsStdVector.h:122
itk::NumericTraits::min
static constexpr T min(const T &)
Definition: itkNumericTraits.h:174
itk::NumericTraits< std::vector< T > >::max
static const Self max(const Self &a)
Definition: itkNumericTraitsStdVector.h:100
itk::NumericTraits::AccumulateType
double AccumulateType
Definition: itkNumericTraits.h:76
itk::NumericTraits< std::vector< T > >::ValueType
T ValueType
Definition: itkNumericTraitsStdVector.h:66
itk::NumericTraits< std::vector< T > >::SetLength
static void SetLength(std::vector< T > &m, const unsigned int s)
Definition: itkNumericTraitsStdVector.h:141
itk::NumericTraits< std::vector< T > >::GetLength
static unsigned int GetLength(const std::vector< T > &m)
Definition: itkNumericTraitsStdVector.h:152
itk::NumericTraits< std::vector< T > >::Self
std::vector< T > Self
Definition: itkNumericTraitsStdVector.h:68
itk::NumericTraits< std::vector< T > >::ElementRealType
typename NumericTraits< T >::RealType ElementRealType
Definition: itkNumericTraitsStdVector.h:63
itk::NumericTraits::IsSigned
static constexpr bool IsSigned
Definition: itkNumericTraits.h:135
itk::NumericTraits< std::vector< T > >::AssignToArray
static void AssignToArray(const Self &v, MeasurementVectorType &mv)
Definition: itkNumericTraitsStdVector.h:158
itk::NumericTraits::IsInteger
static constexpr bool IsInteger
Definition: itkNumericTraits.h:140
itk::NumericTraits::OneValue
static T OneValue()
Definition: itkNumericTraits.h:158
itk::NumericTraits::PrintType
T PrintType
Definition: itkNumericTraits.h:70
itk::NumericTraits< std::vector< T > >::NonpositiveMin
static const Self NonpositiveMin(const Self &a)
Definition: itkNumericTraitsStdVector.h:129
itk::NumericTraits< std::vector< T > >::ElementAccumulateType
typename NumericTraits< T >::AccumulateType ElementAccumulateType
Definition: itkNumericTraitsStdVector.h:60
itk::NumericTraits< std::vector< T > >::ElementFloatType
typename NumericTraits< T >::FloatType ElementFloatType
Definition: itkNumericTraitsStdVector.h:61
itk::NumericTraits< std::vector< T > >::min
static const Self min(const Self &a)
Definition: itkNumericTraitsStdVector.h:108
itk::NumericTraits< std::vector< T > >::ElementPrintType
typename NumericTraits< T >::PrintType ElementPrintType
Definition: itkNumericTraitsStdVector.h:62
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:60
itk::NumericTraits::max
static constexpr T max(const T &)
Definition: itkNumericTraits.h:169
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::NumericTraits::GetLength
static unsigned int GetLength()
Definition: itkNumericTraits.h:210
itk::NumericTraits< std::vector< T > >::AssignToArray
static void AssignToArray(const Self &v, TArray &mv)
Definition: itkNumericTraitsStdVector.h:165
itk::NumericTraits::IsComplex
static constexpr bool IsComplex
Definition: itkNumericTraits.h:145
itk::NumericTraits< std::vector< T > >::ZeroValue
static const Self ZeroValue(const Self &a)
Definition: itkNumericTraitsStdVector.h:115
itk::NumericTraits< std::vector< T > >::ScalarRealType
ElementRealType ScalarRealType
Definition: itkNumericTraitsStdVector.h:89
itk::NumericTraits::RealType
double RealType
Definition: itkNumericTraits.h:86
AddImageFilter
Definition: itkAddImageFilter.h:81
itk::NumericTraits::FloatType
float FloatType
Definition: itkNumericTraits.h:83
itk::NumericTraits< std::vector< T > >::PrintType
std::vector< ElementPrintType > PrintType
Definition: itkNumericTraitsStdVector.h:83
itkMath.h
itk::NumericTraits::AbsType
T AbsType
Definition: itkNumericTraits.h:73
itk::NumericTraits< std::vector< T > >::MeasurementVectorType
Self MeasurementVectorType
Definition: itkNumericTraitsStdVector.h:92