1 /*========================================================================= 2 * 3 * Copyright Insight Software Consortium 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 itkNumericTraitsVectorPixel_h 19 #define itkNumericTraitsVectorPixel_h 20 21 #include "itkNumericTraits.h" 22 #include "itkVector.h" 23 24 namespace itk 25 { 26 /** \brief NumericTraits for Vector 27 * \tparam T Component type for Vector 28 * \tparam D Space dimension (Dimension of Vector) 29 */ 30 template< typename T, unsigned int D > 31 class NumericTraits< Vector< T, D > > 32 { 33 private: 34 35 using ElementAbsType = typename NumericTraits< T >::AbsType; 36 using ElementAccumulateType = typename NumericTraits< T >::AccumulateType; 37 using ElementFloatType = typename NumericTraits< T >::FloatType; 38 using ElementPrintType = typename NumericTraits< T >::PrintType; 39 using ElementRealType = typename NumericTraits< T >::RealType; 40 41 public: 42 43 /** Return the type of the native component type. */ 44 using ValueType = T; 45 using Self = Vector< T, D >; 46 47 /** Unsigned component type */ 48 using AbsType = Vector< ElementAbsType, D >; 49 50 /** Accumulation of addition and multiplication. */ 51 using AccumulateType = Vector< ElementAccumulateType, D >; 52 53 /** Typedef for operations that use floating point instead of real precision 54 */ 55 using FloatType = Vector< ElementFloatType, D >; 56 57 /** Return the type that can be printed. */ 58 using PrintType = Vector< ElementPrintType, D >; 59 60 /** Type for real-valued scalar operations. */ 61 using RealType = Vector< ElementRealType, D >; 62 63 /** Type for real-valued scalar operations. */ 64 using ScalarRealType = ElementRealType; 65 66 /** Measurement vector type */ 67 using MeasurementVectorType = Self; 68 69 /** Component wise defined element 70 * 71 * \note minimum value for floating pointer types is defined as 72 * minimum positive normalize value. 73 */ max(const Self &)74 static const Self max(const Self &) 75 { 76 return Self( NumericTraits< T >::max() ); 77 } 78 min(const Self &)79 static const Self min(const Self &) 80 { 81 return Self( NumericTraits< T >::min() ); 82 } 83 max()84 static const Self max() 85 { 86 return Self( NumericTraits< T >::max() ); 87 } 88 min()89 static const Self min() 90 { 91 return Self( NumericTraits< T >::min() ); 92 } 93 NonpositiveMin()94 static const Self NonpositiveMin() 95 { 96 return Self( NumericTraits< T >::NonpositiveMin() ); 97 } 98 ZeroValue()99 static const Self ZeroValue() 100 { 101 return Self( NumericTraits< T >::ZeroValue() ); 102 } 103 OneValue()104 static const Self OneValue() 105 { 106 return Self( NumericTraits< T >::OneValue() ); 107 } 108 NonpositiveMin(const Self &)109 static const Self NonpositiveMin(const Self &) 110 { 111 return NonpositiveMin(); 112 } 113 ZeroValue(const Self &)114 static const Self ZeroValue(const Self &) 115 { 116 return ZeroValue(); 117 } 118 OneValue(const Self &)119 static const Self OneValue(const Self &) 120 { 121 return OneValue(); 122 } 123 IsPositive(const Self & a)124 static bool IsPositive( const Self & a) 125 { 126 bool flag = false; 127 for (unsigned int i=0; i < GetLength( a ); i++) 128 { 129 if ( a[i] > NumericTraits< ValueType >::ZeroValue() ) 130 { 131 flag = true; 132 } 133 } 134 return flag; 135 } 136 IsNonpositive(const Self & a)137 static bool IsNonpositive( const Self & a) 138 { 139 bool flag = false; 140 for (unsigned int i=0; i < GetLength( a ); i++) 141 { 142 if ( ! (a[i] > 0.0 ) ) 143 { 144 flag = true; 145 } 146 } 147 return flag; 148 } 149 IsNegative(const Self & a)150 static bool IsNegative( const Self & a) 151 { 152 bool flag = false; 153 for (unsigned int i=0; i < GetLength( a ); i++) 154 { 155 if ( a[i] < 0.0 ) 156 { 157 flag = true; 158 } 159 } 160 return flag; 161 } 162 IsNonnegative(const Self & a)163 static bool IsNonnegative( const Self & a) 164 { 165 bool flag = false; 166 for (unsigned int i=0; i < GetLength( a ); i++) 167 { 168 if ( ! (a[i] < 0.0 )) 169 { 170 flag = true; 171 } 172 } 173 return flag; 174 } 175 176 static constexpr bool IsSigned = NumericTraits< ValueType >::IsSigned; 177 static constexpr bool IsInteger = NumericTraits< ValueType >::IsInteger; 178 static constexpr bool IsComplex = NumericTraits< ValueType >::IsComplex; 179 180 /** Fixed length vectors cannot be resized, so an exception will 181 * be thrown if the input size is not valid. If the size is valid 182 * the vector will be filled with zeros. */ SetLength(Vector<T,D> & m,const unsigned int s)183 static void SetLength(Vector< T, D > & m, const unsigned int s) 184 { 185 if ( s != D ) 186 { 187 itkGenericExceptionMacro(<< "Cannot set the size of a Vector of length " 188 << D << " to " << s); 189 } 190 m.Fill(NumericTraits< T >::ZeroValue()); 191 } 192 193 /** Return the size of the vector. */ GetLength(const Vector<T,D> &)194 static unsigned int GetLength(const Vector< T, D > &) 195 { 196 return D; 197 } 198 199 /** Return the size of the vector. */ GetLength()200 static unsigned int GetLength() 201 { 202 return D; 203 } 204 AssignToArray(const Self & v,MeasurementVectorType & mv)205 static void AssignToArray( const Self & v, MeasurementVectorType & mv ) 206 { 207 mv = v; 208 } 209 210 template<typename TArray> AssignToArray(const Self & v,TArray & mv)211 static void AssignToArray( const Self & v, TArray & mv ) 212 { 213 for( unsigned int i=0; i<D; i++ ) 214 { 215 mv[i] = v[i]; 216 } 217 } 218 219 /** \note: the functions are preferred over the member variables as 220 * they are defined for all partial specialization 221 */ 222 static const Self ITKCommon_EXPORT Zero; 223 static const Self ITKCommon_EXPORT One; 224 }; 225 } // end namespace itk 226 227 #endif // itkNumericTraitsVectorPixel_h 228