ITK  5.1.0
Insight Toolkit
itkNumericTraits.h
Go to the documentation of this file.
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 itkNumericTraits_h
19 #define itkNumericTraits_h
20 
21 #include "itkMacro.h"
22 
23 #undef min
24 #undef max
25 
26 #define itkNUMERIC_TRAITS_MIN_MAX_MACRO() \
27  static constexpr ValueType min(ValueType) { return std::numeric_limits<ValueType>::min(); } \
28  static constexpr ValueType max(ValueType) { return std::numeric_limits<ValueType>::max(); } \
29  static constexpr ValueType min() { return std::numeric_limits<ValueType>::min(); } \
30  static constexpr ValueType max() { return std::numeric_limits<ValueType>::max(); }
31 
32 #include <limits> // for std::numeric_limits
33 #include <complex>
34 
35 namespace itk
36 {
37 
38 // forward decare to avoid circular dependencies
39 template <typename TValue, unsigned int VLength>
40 class FixedArray;
41 
57 template <typename T>
58 class NumericTraits : public std::numeric_limits<T>
59 {
60 public:
62  using TraitsType = std::numeric_limits<T>;
63 
65  using ValueType = T;
66 
68  using PrintType = T;
69 
71  using AbsType = T;
72 
74  using AccumulateType = double;
75 
78 
81  using FloatType = float;
82 
84  using RealType = double;
85 
88 
90  static const T ITKCommon_EXPORT Zero;
91 
93  static const T ITKCommon_EXPORT One;
94 
96  static constexpr T
98  {
99  return TraitsType::lowest();
100  }
101 
103  static bool
104  IsPositive(T val)
105  {
106  return val > Zero;
107  }
108 
110  static bool
112  {
113  return val <= Zero;
114  }
115 
117  static bool
118  IsNegative(T val)
119  {
120  return val < Zero;
121  }
122 
124  static bool
126  {
127  return val >= Zero;
128  }
129 
133  static constexpr bool IsSigned = false;
134 
138  static constexpr bool IsInteger = false;
139 
143  static constexpr bool IsComplex = false;
144 
147  static T
149  {
150  return Zero;
151  }
152 
155  static T
157  {
158  return One;
159  }
160 
161  /* Provide a default implementation of the max() method with
162  * argument. This API is needed for VariableLengthVector because
163  * its length is only known at run-time. Specializations of the
164  * VariableLengthVector will provide a different implementation
165  * where a vector of the correct size is built. */
166  static constexpr T
167  max(const T &)
168  {
169  return TraitsType::max();
170  }
171  static constexpr T
172  min(const T &)
173  {
174  return TraitsType::min();
175  }
176 
184  static void
185  SetLength(T & m, const unsigned int s)
186  {
187  if (s != 1)
188  {
189  itkGenericExceptionMacro(<< "Cannot set the size of a scalar to " << s);
190  }
192  }
193 
200  static unsigned int
201  GetLength(const T &)
202  {
203  return GetLength();
204  }
205 
207  static unsigned int
209  {
210  return 1;
211  }
212 
216  static T
217  NonpositiveMin(const T &)
218  {
219  return NonpositiveMin();
220  }
221 
225  static T
226  ZeroValue(const T &)
227  {
228  return ZeroValue();
229  }
230 
234  static T
235  OneValue(const T &)
236  {
237  return OneValue();
238  }
239 
241  template <typename TArray>
242  static void
243  AssignToArray(const T & v, TArray & mv)
244  {
245  mv[0] = v;
246  }
247 };
248 
250 
258 template <>
259 class NumericTraits<bool> : public std::numeric_limits<bool>
260 {
261 public:
262  using ValueType = bool;
263  using PrintType = bool;
264  using AbsType = unsigned char;
265  using AccumulateType = unsigned char;
266  using RealType = double;
267  using ScalarRealType = RealType;
268  using FloatType = float;
269  using MeasurementVectorType = FixedArray<ValueType, 1>;
270 
271  static constexpr bool ITKCommon_EXPORT Zero = false;
272  static constexpr bool ITKCommon_EXPORT One = true;
273 
274  static constexpr bool
275  min()
276  {
277  return false;
278  }
279  static constexpr bool
280  max()
281  {
282  return true;
283  }
284  static constexpr bool
285  min(bool)
286  {
287  return min();
288  }
289  static constexpr bool
290  max(bool)
291  {
292  return max();
293  }
294  static constexpr bool
296  {
297  return false;
298  }
299  static constexpr bool
300  IsPositive(bool val)
301  {
302  return val;
303  }
304  static constexpr bool
305  IsNonpositive(bool val)
306  {
307  return !val;
308  }
309  static constexpr bool
310  IsNegative(bool val)
311  {
312  return val ? false : false;
313  }
314  static constexpr bool
315  IsNonnegative(bool val)
316  {
317  return val ? true : true;
318  }
319  static constexpr bool IsSigned = false;
320  static constexpr bool IsInteger = true;
321  static constexpr bool IsComplex = false;
322  static constexpr bool
323  ZeroValue()
324  {
325  return Zero;
326  }
327  static constexpr bool
328  OneValue()
329  {
330  return One;
331  }
332  static constexpr unsigned int
333  GetLength(const ValueType &)
334  {
335  return 1;
336  }
337  static constexpr unsigned int
338  GetLength()
339  {
340  return 1;
341  }
342  static constexpr ValueType
343  NonpositiveMin(const ValueType &)
344  {
345  return NonpositiveMin();
346  }
347  static constexpr ValueType
348  ZeroValue(const ValueType &)
349  {
350  return ZeroValue();
351  }
352  static constexpr ValueType
353  OneValue(const ValueType &)
354  {
355  return OneValue();
356  }
357 
358  template <typename TArray>
359  static void
360  AssignToArray(const ValueType & v, TArray & mv)
361  {
362  mv[0] = v;
363  }
364  static void
365  SetLength(ValueType & m, const unsigned int s)
366  {
367  if (s != 1)
368  {
369  itkGenericExceptionMacro(<< "Cannot set the size of a scalar to " << s);
370  }
372  }
373 };
374 
380 template <>
381 class NumericTraits<char> : public std::numeric_limits<char>
382 {
383 public:
384  using ValueType = char;
385  using PrintType = int;
386  using AbsType = unsigned char;
387  using AccumulateType = short;
388  using RealType = double;
389  using ScalarRealType = RealType;
390  using FloatType = float;
391  using MeasurementVectorType = FixedArray<ValueType, 1>;
392 
393  static constexpr char ITKCommon_EXPORT Zero = 0;
394  static constexpr char ITKCommon_EXPORT One = 1;
395 
397 
398  static constexpr char
400  {
401  return lowest();
402  }
403  static constexpr bool
404  IsPositive(char val)
405  {
406  return val > Zero;
407  }
408  static constexpr bool
409  IsNonpositive(char val)
410  {
411  return val <= Zero;
412  }
413 
414  static constexpr bool
415  IsNegative(char)
416  {
417  return false;
418  }
419  static constexpr bool
420  IsNonnegative(char)
421  {
422  return true;
423  }
424  static constexpr bool IsSigned = std::numeric_limits<char>::is_signed;
425 
426  static constexpr bool IsInteger = std::numeric_limits<char>::is_integer;
427  static constexpr bool IsComplex = false;
428  static constexpr char
429  ZeroValue()
430  {
431  return Zero;
432  }
433  static constexpr char
434  OneValue()
435  {
436  return One;
437  }
438  static constexpr unsigned int
439  GetLength(const ValueType &)
440  {
441  return 1;
442  }
443  static constexpr unsigned int
444  GetLength()
445  {
446  return 1;
447  }
448  static constexpr ValueType
449  NonpositiveMin(const ValueType &)
450  {
451  return NonpositiveMin();
452  }
453  static constexpr ValueType
454  ZeroValue(const ValueType &)
455  {
456  return ZeroValue();
457  }
458  static constexpr ValueType
459  OneValue(const ValueType &)
460  {
461  return OneValue();
462  }
463 
464  template <typename TArray>
465  static void
466  AssignToArray(const ValueType & v, TArray & mv)
467  {
468  mv[0] = v;
469  }
470  static void
471  SetLength(ValueType & m, const unsigned int s)
472  {
473  if (s != 1)
474  {
475  itkGenericExceptionMacro(<< "Cannot set the size of a scalar to " << s);
476  }
478  }
479 };
480 
486 template <>
487 class NumericTraits<signed char> : public std::numeric_limits<signed char>
488 {
489 public:
490  using ValueType = signed char;
491  using PrintType = int;
492  using AbsType = unsigned char;
493  using AccumulateType = short;
494  using RealType = double;
495  using ScalarRealType = RealType;
496  using FloatType = float;
497  using MeasurementVectorType = FixedArray<ValueType, 1>;
498 
499  static constexpr signed char ITKCommon_EXPORT Zero = 0;
500  static constexpr signed char ITKCommon_EXPORT One = 1;
501 
502  static constexpr signed char
503  min()
504  {
505  return -128;
506  }
507  static constexpr signed char
508  max()
509  {
510  return 127;
511  }
512  static constexpr signed char
513  min(signed char)
514  {
515  return min();
516  }
517  static constexpr signed char
518  max(signed char)
519  {
520  return max();
521  }
522  static constexpr signed char
524  {
525  return lowest();
526  }
527  static constexpr bool
528  IsPositive(signed char val)
529  {
530  return val > Zero;
531  }
532  static constexpr bool
533  IsNonpositive(signed char val)
534  {
535  return val <= Zero;
536  }
537  static constexpr bool
538  IsNegative(signed char val)
539  {
540  return val < Zero;
541  }
542  static constexpr bool
543  IsNonnegative(signed char val)
544  {
545  return val >= Zero;
546  }
547  static constexpr bool IsSigned = true;
548  static constexpr bool IsInteger = true;
549  static constexpr bool IsComplex = false;
550  static constexpr signed char
551  ZeroValue()
552  {
553  return Zero;
554  }
555  static constexpr signed char
556  OneValue()
557  {
558  return One;
559  }
560  static constexpr unsigned int
561  GetLength(const ValueType &)
562  {
563  return 1;
564  }
565  static constexpr unsigned int
566  GetLength()
567  {
568  return 1;
569  }
570  static constexpr ValueType
571  NonpositiveMin(const ValueType &)
572  {
573  return NonpositiveMin();
574  }
575  static constexpr ValueType
576  ZeroValue(const ValueType &)
577  {
578  return ZeroValue();
579  }
580  static constexpr ValueType
581  OneValue(const ValueType &)
582  {
583  return OneValue();
584  }
585 
586  template <typename TArray>
587  static void
588  AssignToArray(const ValueType & v, TArray & mv)
589  {
590  mv[0] = v;
591  }
592  static void
593  SetLength(ValueType & m, const unsigned int s)
594  {
595  if (s != 1)
596  {
597  itkGenericExceptionMacro(<< "Cannot set the size of a scalar to " << s);
598  }
600  }
601 };
602 
608 template <>
609 class NumericTraits<unsigned char> : public std::numeric_limits<unsigned char>
610 {
611 public:
612  using ValueType = unsigned char;
613  using PrintType = int;
614  using AbsType = unsigned char;
615  using AccumulateType = unsigned short;
616  using RealType = double;
617  using ScalarRealType = RealType;
618  using FloatType = float;
619  using MeasurementVectorType = FixedArray<ValueType, 1>;
620 
621  static constexpr unsigned char ITKCommon_EXPORT Zero = 0;
622  static constexpr unsigned char ITKCommon_EXPORT One = 1;
623 
625 
626  static constexpr unsigned char
628  {
629  return std::numeric_limits<ValueType>::lowest();
630  }
631  static constexpr bool
632  IsPositive(unsigned char val)
633  {
634  return val != Zero;
635  }
636  static constexpr bool
637  IsNonpositive(unsigned char val)
638  {
639  return val == Zero;
640  }
641  static constexpr bool
642  IsNegative(unsigned char val)
643  {
644  return val ? false : false;
645  }
646  static constexpr bool
647  IsNonnegative(unsigned char val)
648  {
649  return val ? true : true;
650  }
651  static constexpr bool IsSigned = false;
652  static constexpr bool IsInteger = true;
653  static constexpr bool IsComplex = false;
654  static constexpr unsigned char
655  ZeroValue()
656  {
657  return Zero;
658  }
659  static constexpr unsigned char
660  OneValue()
661  {
662  return One;
663  }
664  static constexpr unsigned int
665  GetLength(const ValueType &)
666  {
667  return 1;
668  }
669  static constexpr unsigned int
670  GetLength()
671  {
672  return 1;
673  }
674  static constexpr ValueType
675  NonpositiveMin(const ValueType &)
676  {
677  return NonpositiveMin();
678  }
679  static constexpr ValueType
680  ZeroValue(const ValueType &)
681  {
682  return ZeroValue();
683  }
684  static constexpr ValueType
685  OneValue(const ValueType &)
686  {
687  return OneValue();
688  }
689 
690  template <typename TArray>
691  static void
692  AssignToArray(const ValueType & v, TArray & mv)
693  {
694  mv[0] = v;
695  }
696  static void
697  SetLength(ValueType & m, const unsigned int s)
698  {
699  if (s != 1)
700  {
701  itkGenericExceptionMacro(<< "Cannot set the size of a scalar to " << s);
702  }
704  }
705 };
706 
711 template <>
712 class NumericTraits<short> : public std::numeric_limits<short>
713 {
714 public:
715  using ValueType = short;
716  using PrintType = short;
717  using AbsType = unsigned short;
718  using AccumulateType = int;
719  using RealType = double;
720  using ScalarRealType = RealType;
721  using FloatType = float;
722  using MeasurementVectorType = FixedArray<ValueType, 1>;
723 
724  static constexpr short ITKCommon_EXPORT Zero = 0;
725  static constexpr short ITKCommon_EXPORT One = 1;
726 
728  static constexpr short
730  {
731  return std::numeric_limits<ValueType>::lowest();
732  }
733  static constexpr bool
734  IsPositive(short val)
735  {
736  return val > Zero;
737  }
738  static constexpr bool
739  IsNonpositive(short val)
740  {
741  return val <= Zero;
742  }
743  static constexpr bool
744  IsNegative(short val)
745  {
746  return val < Zero;
747  }
748  static constexpr bool
749  IsNonnegative(short val)
750  {
751  return val >= Zero;
752  }
753  static constexpr bool IsSigned = true;
754  static constexpr bool IsInteger = true;
755  static constexpr bool IsComplex = false;
756  static constexpr short
757  ZeroValue()
758  {
759  return Zero;
760  }
761  static constexpr short
762  OneValue()
763  {
764  return One;
765  }
766  static constexpr unsigned int
767  GetLength(const ValueType &)
768  {
769  return 1;
770  }
771  static constexpr unsigned int
772  GetLength()
773  {
774  return 1;
775  }
776  static constexpr ValueType
777  NonpositiveMin(const ValueType &)
778  {
779  return NonpositiveMin();
780  }
781  static constexpr ValueType
782  ZeroValue(const ValueType &)
783  {
784  return ZeroValue();
785  }
786  static constexpr ValueType
787  OneValue(const ValueType &)
788  {
789  return OneValue();
790  }
791 
792  template <typename TArray>
793  static void
794  AssignToArray(const ValueType & v, TArray & mv)
795  {
796  mv[0] = v;
797  }
798  static void
799  SetLength(ValueType & m, const unsigned int s)
800  {
801  if (s != 1)
802  {
803  itkGenericExceptionMacro(<< "Cannot set the size of a scalar to " << s);
804  }
806  }
807 };
808 
814 template <>
815 class NumericTraits<unsigned short> : public std::numeric_limits<unsigned short>
816 {
817 public:
818  using ValueType = unsigned short;
819  using PrintType = unsigned short;
820  using AbsType = unsigned short;
821  using AccumulateType = unsigned int;
822  using RealType = double;
823  using ScalarRealType = RealType;
824  using FloatType = float;
825  using MeasurementVectorType = FixedArray<ValueType, 1>;
826 
827  static constexpr unsigned short ITKCommon_EXPORT Zero = 0;
828  static constexpr unsigned short ITKCommon_EXPORT One = 1;
829 
831  static constexpr unsigned short
833  {
834  return std::numeric_limits<ValueType>::lowest();
835  }
836  static constexpr bool
837  IsPositive(unsigned short val)
838  {
839  return val != Zero;
840  }
841  static constexpr bool
842  IsNonpositive(unsigned short val)
843  {
844  return val == Zero;
845  }
846  static constexpr bool
847  IsNegative(unsigned short val)
848  {
849  return val ? false : false;
850  }
851  static constexpr bool
852  IsNonnegative(unsigned short val)
853  {
854  return val ? true : true;
855  }
856  static constexpr bool IsSigned = false;
857  static constexpr bool IsInteger = true;
858  static constexpr bool IsComplex = false;
859  static constexpr unsigned short
860  ZeroValue()
861  {
862  return Zero;
863  }
864  static constexpr unsigned short
865  OneValue()
866  {
867  return One;
868  }
869  static constexpr unsigned int
870  GetLength(const ValueType &)
871  {
872  return 1;
873  }
874  static constexpr unsigned int
875  GetLength()
876  {
877  return 1;
878  }
879  static constexpr ValueType
880  NonpositiveMin(const ValueType &)
881  {
882  return NonpositiveMin();
883  }
884  static constexpr ValueType
885  ZeroValue(const ValueType &)
886  {
887  return ZeroValue();
888  }
889  static constexpr ValueType
890  OneValue(const ValueType &)
891  {
892  return OneValue();
893  }
894 
895  template <typename TArray>
896  static void
897  AssignToArray(const ValueType & v, TArray & mv)
898  {
899  mv[0] = v;
900  }
901  static void
902  SetLength(ValueType & m, const unsigned int s)
903  {
904  if (s != 1)
905  {
906  itkGenericExceptionMacro(<< "Cannot set the size of a scalar to " << s);
907  }
909  }
910 };
911 
916 template <>
917 class NumericTraits<int> : public std::numeric_limits<int>
918 {
919 public:
920  using ValueType = int;
921  using PrintType = int;
922  using AbsType = unsigned int;
923  using AccumulateType = long;
924  using RealType = double;
925  using ScalarRealType = RealType;
926  using FloatType = float;
927  using MeasurementVectorType = FixedArray<ValueType, 1>;
928 
929  static constexpr int ITKCommon_EXPORT Zero = 0;
930  static constexpr int ITKCommon_EXPORT One = 1;
931 
933  static constexpr int
935  {
936  return std::numeric_limits<ValueType>::lowest();
937  }
938  static constexpr bool
939  IsPositive(int val)
940  {
941  return val > Zero;
942  }
943  static constexpr bool
944  IsNonpositive(int val)
945  {
946  return val <= Zero;
947  }
948  static constexpr bool
949  IsNegative(int val)
950  {
951  return val < Zero;
952  }
953  static constexpr bool
954  IsNonnegative(int val)
955  {
956  return val >= Zero;
957  }
958  static constexpr bool IsSigned = true;
959  static constexpr bool IsInteger = true;
960  static constexpr bool IsComplex = false;
961  static constexpr int
962  ZeroValue()
963  {
964  return Zero;
965  }
966  static constexpr int
967  OneValue()
968  {
969  return One;
970  }
971  static constexpr unsigned int
972  GetLength(const ValueType &)
973  {
974  return 1;
975  }
976  static constexpr unsigned int
977  GetLength()
978  {
979  return 1;
980  }
981  static constexpr ValueType
982  NonpositiveMin(const ValueType &)
983  {
984  return NonpositiveMin();
985  }
986  static constexpr ValueType
987  ZeroValue(const ValueType &)
988  {
989  return ZeroValue();
990  }
991  static constexpr ValueType
992  OneValue(const ValueType &)
993  {
994  return OneValue();
995  }
996 
997  template <typename TArray>
998  static void
999  AssignToArray(const ValueType & v, TArray & mv)
1000  {
1001  mv[0] = v;
1002  }
1003  static void
1004  SetLength(ValueType & m, const unsigned int s)
1005  {
1006  if (s != 1)
1007  {
1008  itkGenericExceptionMacro(<< "Cannot set the size of a scalar to " << s);
1009  }
1011  }
1012 };
1013 
1019 template <>
1020 class NumericTraits<unsigned int> : public std::numeric_limits<unsigned int>
1021 {
1022 public:
1023  using ValueType = unsigned int;
1024  using PrintType = unsigned int;
1025  using AbsType = unsigned int;
1026  using AccumulateType = unsigned int;
1027  using RealType = double;
1028  using ScalarRealType = RealType;
1029  using FloatType = float;
1030  using MeasurementVectorType = FixedArray<ValueType, 1>;
1031 
1032  static constexpr unsigned int ITKCommon_EXPORT Zero = 0;
1033  static constexpr unsigned int ITKCommon_EXPORT One = 1;
1034 
1035  static constexpr unsigned int
1036  min()
1037  {
1038  return 0;
1039  }
1040  static constexpr unsigned int
1041  max()
1042  {
1043  return static_cast<unsigned int>(-1);
1044  }
1045  static constexpr unsigned int
1046  min(unsigned int)
1047  {
1048  return std::numeric_limits<ValueType>::min();
1049  }
1050  static constexpr unsigned int
1051  max(unsigned int)
1052  {
1053  return std::numeric_limits<ValueType>::max();
1054  }
1055  static constexpr unsigned int
1056  NonpositiveMin()
1057  {
1058  return 0;
1059  }
1060  static constexpr bool
1061  IsPositive(unsigned int val)
1062  {
1063  return val != Zero;
1064  }
1065  static constexpr bool
1066  IsNonpositive(unsigned int val)
1067  {
1068  return val == Zero;
1069  }
1070  static constexpr bool
1071  IsNegative(unsigned int val)
1072  {
1073  return val ? false : false;
1074  }
1075  static constexpr bool
1076  IsNonnegative(unsigned int val)
1077  {
1078  return val ? true : true;
1079  }
1080  static constexpr bool IsSigned = false;
1081  static constexpr bool IsInteger = true;
1082  static constexpr bool IsComplex = false;
1083  static constexpr unsigned int
1084  ZeroValue()
1085  {
1086  return Zero;
1087  }
1088  static constexpr unsigned int
1089  OneValue()
1090  {
1091  return One;
1092  }
1093  static constexpr unsigned int
1094  GetLength(const ValueType &)
1095  {
1096  return 1;
1097  }
1098  static constexpr unsigned int
1099  GetLength()
1100  {
1101  return 1;
1102  }
1103  static constexpr ValueType
1104  NonpositiveMin(const ValueType &)
1105  {
1106  return NonpositiveMin();
1107  }
1108  static constexpr ValueType
1109  ZeroValue(const ValueType &)
1110  {
1111  return ZeroValue();
1112  }
1113  static constexpr ValueType
1114  OneValue(const ValueType &)
1115  {
1116  return OneValue();
1117  }
1118 
1119  template <typename TArray>
1120  static void
1121  AssignToArray(const ValueType & v, TArray & mv)
1122  {
1123  mv[0] = v;
1124  }
1125  static void
1126  SetLength(ValueType & m, const unsigned int s)
1127  {
1128  if (s != 1)
1129  {
1130  itkGenericExceptionMacro(<< "Cannot set the size of a scalar to " << s);
1131  }
1133  }
1134 };
1135 
1141 template <>
1142 class NumericTraits<long> : public std::numeric_limits<long>
1143 {
1144 public:
1145  using ValueType = long;
1146  using PrintType = long;
1147  using AbsType = unsigned long;
1148  using AccumulateType = long;
1149  using RealType = double;
1150  using ScalarRealType = RealType;
1151  using FloatType = float;
1152  using MeasurementVectorType = FixedArray<ValueType, 1>;
1153 
1154  static constexpr long ITKCommon_EXPORT Zero = 0L;
1155  static constexpr long ITKCommon_EXPORT One = 1L;
1156 
1158  static constexpr long
1159  NonpositiveMin()
1160  {
1161  return std::numeric_limits<ValueType>::lowest();
1162  }
1163  static constexpr bool
1164  IsPositive(long val)
1165  {
1166  return val > Zero;
1167  }
1168  static constexpr bool
1169  IsNonpositive(long val)
1170  {
1171  return val <= Zero;
1172  }
1173  static constexpr bool
1174  IsNegative(long val)
1175  {
1176  return val < Zero;
1177  }
1178  static constexpr bool
1179  IsNonnegative(long val)
1180  {
1181  return val >= Zero;
1182  }
1183  static constexpr bool IsSigned = true;
1184  static constexpr bool IsInteger = true;
1185  static constexpr bool IsComplex = false;
1186  static constexpr long
1187  ZeroValue()
1188  {
1189  return Zero;
1190  }
1191  static constexpr long
1192  OneValue()
1193  {
1194  return One;
1195  }
1196  static constexpr unsigned int
1197  GetLength(const ValueType &)
1198  {
1199  return 1;
1200  }
1201  static constexpr unsigned int
1202  GetLength()
1203  {
1204  return 1;
1205  }
1206  static constexpr ValueType
1207  NonpositiveMin(const ValueType &)
1208  {
1209  return NonpositiveMin();
1210  }
1211  static constexpr ValueType
1212  ZeroValue(const ValueType &)
1213  {
1214  return ZeroValue();
1215  }
1216  static constexpr ValueType
1217  OneValue(const ValueType &)
1218  {
1219  return OneValue();
1220  }
1221 
1222  template <typename TArray>
1223  static void
1224  AssignToArray(const ValueType & v, TArray & mv)
1225  {
1226  mv[0] = v;
1227  }
1228  static void
1229  SetLength(ValueType & m, const unsigned int s)
1230  {
1231  if (s != 1)
1232  {
1233  itkGenericExceptionMacro(<< "Cannot set the size of a scalar to " << s);
1234  }
1236  }
1237 };
1238 
1244 template <>
1245 class NumericTraits<unsigned long> : public std::numeric_limits<unsigned long>
1246 {
1247 public:
1248  using ValueType = unsigned long;
1249  using PrintType = unsigned long;
1250  using AbsType = unsigned long;
1251  using AccumulateType = unsigned long;
1252  using RealType = double;
1253  using ScalarRealType = RealType;
1254  using FloatType = float;
1255  using MeasurementVectorType = FixedArray<ValueType, 1>;
1256 
1257  static constexpr unsigned long ITKCommon_EXPORT Zero = 0UL;
1258  static constexpr unsigned long ITKCommon_EXPORT One = 1UL;
1259 
1261  static constexpr unsigned long
1262  NonpositiveMin()
1263  {
1264  return std::numeric_limits<ValueType>::lowest();
1265  }
1266  static constexpr bool
1267  IsPositive(unsigned long val)
1268  {
1269  return val != Zero;
1270  }
1271  static constexpr bool
1272  IsNonpositive(unsigned long val)
1273  {
1274  return val == Zero;
1275  }
1276  static constexpr bool
1277  IsNegative(unsigned long)
1278  {
1279  return false;
1280  }
1281  static constexpr bool
1282  IsNonnegative(unsigned long)
1283  {
1284  return true;
1285  }
1286  static constexpr bool IsSigned = false;
1287  static constexpr bool IsInteger = true;
1288  static constexpr bool IsComplex = false;
1289  static constexpr unsigned long
1290  ZeroValue()
1291  {
1292  return Zero;
1293  }
1294  static constexpr unsigned long
1295  OneValue()
1296  {
1297  return One;
1298  }
1299  static constexpr unsigned int
1300  GetLength(const ValueType &)
1301  {
1302  return 1;
1303  }
1304  static constexpr unsigned int
1305  GetLength()
1306  {
1307  return 1;
1308  }
1309  static constexpr ValueType
1310  NonpositiveMin(const ValueType &)
1311  {
1312  return NonpositiveMin();
1313  }
1314  static constexpr ValueType
1315  ZeroValue(const ValueType &)
1316  {
1317  return ZeroValue();
1318  }
1319  static constexpr ValueType
1320  OneValue(const ValueType &)
1321  {
1322  return OneValue();
1323  }
1324 
1325  template <typename TArray>
1326  static void
1327  AssignToArray(const ValueType & v, TArray & mv)
1328  {
1329  mv[0] = v;
1330  }
1331  static void
1332  SetLength(ValueType & m, const unsigned int s)
1333  {
1334  if (s != 1)
1335  {
1336  itkGenericExceptionMacro(<< "Cannot set the size of a scalar to " << s);
1337  }
1339  }
1340 };
1341 
1347 template <>
1348 class NumericTraits<float> : public std::numeric_limits<float>
1349 {
1350 public:
1351  using ValueType = float;
1352  using PrintType = float;
1353  using AbsType = float;
1354  using AccumulateType = double;
1355  using RealType = double;
1356  using ScalarRealType = RealType;
1357  using FloatType = float;
1358  using MeasurementVectorType = FixedArray<ValueType, 1>;
1359 
1360 
1361  static constexpr float ITKCommon_EXPORT Zero = 0.0f;
1362  static constexpr float ITKCommon_EXPORT One = 1.0f;
1363 
1365  static constexpr float
1366  NonpositiveMin()
1367  {
1368  return std::numeric_limits<ValueType>::lowest();
1369  }
1370  static constexpr bool
1371  IsPositive(float val)
1372  {
1373  return val > Zero;
1374  }
1375  static constexpr bool
1376  IsNonpositive(float val)
1377  {
1378  return val <= Zero;
1379  }
1380  static constexpr bool
1381  IsNegative(float val)
1382  {
1383  return val < Zero;
1384  }
1385  static constexpr bool
1386  IsNonnegative(float val)
1387  {
1388  return val >= Zero;
1389  }
1390  static constexpr bool IsSigned = true;
1391  static constexpr bool IsInteger = false;
1392  static constexpr bool IsComplex = false;
1393  static constexpr float
1394  ZeroValue()
1395  {
1396  return Zero;
1397  }
1398  static constexpr float
1399  OneValue()
1400  {
1401  return One;
1402  }
1403  static constexpr unsigned int
1404  GetLength(const ValueType &)
1405  {
1406  return 1;
1407  }
1408  static constexpr unsigned int
1409  GetLength()
1410  {
1411  return 1;
1412  }
1413  static constexpr ValueType
1414  NonpositiveMin(const ValueType &)
1415  {
1416  return NonpositiveMin();
1417  }
1418  static constexpr ValueType
1419  ZeroValue(const ValueType &)
1420  {
1421  return ZeroValue();
1422  }
1423  static constexpr ValueType
1424  OneValue(const ValueType &)
1425  {
1426  return OneValue();
1427  }
1428 
1429  template <typename TArray>
1430  static void
1431  AssignToArray(const ValueType & v, TArray & mv)
1432  {
1433  mv[0] = v;
1434  }
1435  static void
1436  SetLength(ValueType & m, const unsigned int s)
1437  {
1438  if (s != 1)
1439  {
1440  itkGenericExceptionMacro(<< "Cannot set the size of a scalar to " << s);
1441  }
1443  }
1444 };
1445 
1451 template <>
1452 class NumericTraits<double> : public std::numeric_limits<double>
1453 {
1454 public:
1455  using ValueType = double;
1456  using PrintType = double;
1457  using AbsType = double;
1458  using AccumulateType = double;
1459  using RealType = double;
1460  using ScalarRealType = RealType;
1461  using FloatType = float;
1462  using MeasurementVectorType = FixedArray<ValueType, 1>;
1463 
1464  static constexpr double ITKCommon_EXPORT Zero = 0.0;
1465  static constexpr double ITKCommon_EXPORT One = 1.0;
1466 
1468  static constexpr double
1469  NonpositiveMin()
1470  {
1471  return std::numeric_limits<ValueType>::lowest();
1472  }
1473  static constexpr bool
1474  IsPositive(double val)
1475  {
1476  return val > Zero;
1477  }
1478  static constexpr bool
1479  IsNonpositive(double val)
1480  {
1481  return val <= Zero;
1482  }
1483  static constexpr bool
1484  IsNegative(double val)
1485  {
1486  return val < Zero;
1487  }
1488  static constexpr bool
1489  IsNonnegative(double val)
1490  {
1491  return val >= Zero;
1492  }
1493  static constexpr bool IsSigned = true;
1494  static constexpr bool IsInteger = false;
1495  static constexpr bool IsComplex = false;
1496  static constexpr double
1497  ZeroValue()
1498  {
1499  return Zero;
1500  }
1501  static constexpr double
1502  OneValue()
1503  {
1504  return One;
1505  }
1506  static constexpr unsigned int
1507  GetLength(const ValueType &)
1508  {
1509  return 1;
1510  }
1511  static constexpr unsigned int
1512  GetLength()
1513  {
1514  return 1;
1515  }
1516  static constexpr ValueType
1517  NonpositiveMin(const ValueType &)
1518  {
1519  return NonpositiveMin();
1520  }
1521  static constexpr ValueType
1522  ZeroValue(const ValueType &)
1523  {
1524  return ZeroValue();
1525  }
1526  static constexpr ValueType
1527  OneValue(const ValueType &)
1528  {
1529  return OneValue();
1530  }
1531 
1532  template <typename TArray>
1533  static void
1534  AssignToArray(const ValueType & v, TArray & mv)
1535  {
1536  mv[0] = v;
1537  }
1538  static void
1539  SetLength(ValueType & m, const unsigned int s)
1540  {
1541  if (s != 1)
1542  {
1543  itkGenericExceptionMacro(<< "Cannot set the size of a scalar to " << s);
1544  }
1546  }
1547 };
1548 
1554 template <>
1555 class NumericTraits<long double> : public std::numeric_limits<long double>
1556 {
1557 public:
1558  using ValueType = long double;
1559 #if defined(__SUNPRO_CC) && defined(_ILP32)
1560  // sun studio in 32 bit mode is unable to print long double values: it
1561  // segfaults.
1562  // conversion to double will give usable results if the value is in the double
1563  // range - better than nothing.
1564  using PrintType = double;
1565 #else
1566  using PrintType = long double;
1567 #endif
1568  using AbsType = long double;
1569  using AccumulateType = long double;
1570  using RealType = long double;
1571  using ScalarRealType = RealType;
1572  using FloatType = float;
1573  using MeasurementVectorType = FixedArray<ValueType, 1>;
1574 
1575  static constexpr long double ITKCommon_EXPORT Zero = 0.0;
1576  static constexpr long double ITKCommon_EXPORT One = 1.0;
1577 
1579  static constexpr long double
1580  NonpositiveMin()
1581  {
1582  return std::numeric_limits<ValueType>::lowest();
1583  }
1584  static constexpr bool
1585  IsPositive(long double val)
1586  {
1587  return val > Zero;
1588  }
1589  static constexpr bool
1590  IsNonpositive(long double val)
1591  {
1592  return val <= Zero;
1593  }
1594  static constexpr bool
1595  IsNegative(long double val)
1596  {
1597  return val < Zero;
1598  }
1599  static constexpr bool
1600  IsNonnegative(long double val)
1601  {
1602  return val >= Zero;
1603  }
1604  static constexpr bool IsSigned = true;
1605  static constexpr bool IsInteger = false;
1606  static constexpr bool IsComplex = false;
1607  static constexpr long double
1608  ZeroValue()
1609  {
1610  return Zero;
1611  }
1612  static constexpr long double
1613  OneValue()
1614  {
1615  return One;
1616  }
1617  static constexpr unsigned int
1618  GetLength(const ValueType &)
1619  {
1620  return 1;
1621  }
1622  static constexpr unsigned int
1623  GetLength()
1624  {
1625  return 1;
1626  }
1627  static constexpr ValueType
1628  NonpositiveMin(const ValueType &)
1629  {
1630  return NonpositiveMin();
1631  }
1632  static constexpr ValueType
1633  ZeroValue(const ValueType &)
1634  {
1635  return ZeroValue();
1636  }
1637  static constexpr ValueType
1638  OneValue(const ValueType &)
1639  {
1640  return OneValue();
1641  }
1642 
1643  template <typename TArray>
1644  static void
1645  AssignToArray(const ValueType & v, TArray & mv)
1646  {
1647  mv[0] = v;
1648  }
1649  static void
1650  SetLength(ValueType & m, const unsigned int s)
1651  {
1652  if (s != 1)
1653  {
1654  itkGenericExceptionMacro(<< "Cannot set the size of a scalar to " << s);
1655  }
1657  }
1658 };
1659 
1660 
1666 template <>
1667 class NumericTraits<long long> : public std::numeric_limits<long long>
1668 {
1669 public:
1670  using ValueType = long long;
1671  using PrintType = long long;
1672  using AbsType = long long;
1673  using AccumulateType = long long;
1674  using RealType = double;
1675  using ScalarRealType = RealType;
1676  using FloatType = float;
1677  using MeasurementVectorType = FixedArray<ValueType, 1>;
1678 
1679  static constexpr ValueType ITKCommon_EXPORT Zero = 0LL;
1680  static constexpr ValueType ITKCommon_EXPORT One = 1LL;
1681 
1683  static constexpr ValueType
1684  NonpositiveMin()
1685  {
1686  return std::numeric_limits<ValueType>::lowest();
1687  }
1688  static constexpr bool
1689  IsPositive(ValueType val)
1690  {
1691  return val > Zero;
1692  }
1693  static constexpr bool
1695  {
1696  return val <= Zero;
1697  }
1698  static constexpr bool
1699  IsNegative(ValueType val)
1700  {
1701  return val < Zero;
1702  }
1703  static constexpr bool
1705  {
1706  return val >= Zero;
1707  }
1708  static constexpr bool IsSigned = true;
1709  static constexpr bool IsInteger = true;
1710  static constexpr bool IsComplex = false;
1711  static constexpr ValueType
1712  ZeroValue()
1713  {
1714  return Zero;
1715  }
1716  static constexpr ValueType
1717  OneValue()
1718  {
1719  return One;
1720  }
1721  static constexpr unsigned int
1722  GetLength(const ValueType &)
1723  {
1724  return 1;
1725  }
1726  static constexpr unsigned int
1727  GetLength()
1728  {
1729  return 1;
1730  }
1731  static constexpr ValueType
1732  NonpositiveMin(const ValueType &)
1733  {
1734  return NonpositiveMin();
1735  }
1736  static constexpr ValueType
1737  ZeroValue(const ValueType &)
1738  {
1739  return ZeroValue();
1740  }
1741  static constexpr ValueType
1742  OneValue(const ValueType &)
1743  {
1744  return OneValue();
1745  }
1746 
1747  template <typename TArray>
1748  static void
1749  AssignToArray(const ValueType & v, TArray & mv)
1750  {
1751  mv[0] = v;
1752  }
1753  static void
1754  SetLength(ValueType & m, const unsigned int s)
1755  {
1756  if (s != 1)
1757  {
1758  itkGenericExceptionMacro(<< "Cannot set the size of a scalar to " << s);
1759  }
1761  }
1762 };
1763 
1769 template <>
1770 class NumericTraits<unsigned long long> : public std::numeric_limits<unsigned long long>
1771 {
1772 public:
1773  using ValueType = unsigned long long;
1774  using PrintType = unsigned long long;
1775  using AbsType = unsigned long long;
1776  using AccumulateType = unsigned long long;
1777  using RealType = double;
1778  using ScalarRealType = RealType;
1779  using FloatType = float;
1780  using MeasurementVectorType = FixedArray<ValueType, 1>;
1781 
1782  static constexpr ValueType ITKCommon_EXPORT Zero = 0ULL;
1783  static constexpr ValueType ITKCommon_EXPORT One = 1ULL;
1784 
1786  static constexpr ValueType
1787  NonpositiveMin()
1788  {
1789  return std::numeric_limits<ValueType>::lowest();
1790  }
1791  static constexpr bool
1792  IsPositive(ValueType val)
1793  {
1794  return val != Zero;
1795  }
1796  static constexpr bool
1798  {
1799  return val == Zero;
1800  }
1801  static constexpr bool IsNegative(ValueType) { return false; }
1802  static constexpr bool IsNonnegative(ValueType) { return true; }
1803  static constexpr bool IsSigned = false;
1804  static constexpr bool IsInteger = true;
1805  static constexpr bool IsComplex = false;
1806  static constexpr ValueType
1807  ZeroValue()
1808  {
1809  return Zero;
1810  }
1811  static constexpr ValueType
1812  OneValue()
1813  {
1814  return One;
1815  }
1816  static constexpr unsigned int
1817  GetLength(const ValueType &)
1818  {
1819  return 1;
1820  }
1821  static constexpr unsigned int
1822  GetLength()
1823  {
1824  return 1;
1825  }
1826  static constexpr ValueType
1827  NonpositiveMin(const ValueType &)
1828  {
1829  return NonpositiveMin();
1830  }
1831  static constexpr ValueType
1832  ZeroValue(const ValueType &)
1833  {
1834  return ZeroValue();
1835  }
1836  static constexpr ValueType
1837  OneValue(const ValueType &)
1838  {
1839  return OneValue();
1840  }
1841 
1842  template <typename TArray>
1843  static void
1844  AssignToArray(const ValueType & v, TArray & mv)
1845  {
1846  mv[0] = v;
1847  }
1848  static void
1849  SetLength(ValueType & m, const unsigned int s)
1850  {
1851  if (s != 1)
1852  {
1853  itkGenericExceptionMacro(<< "Cannot set the size of a scalar to " << s);
1854  }
1856  }
1857 };
1858 
1859 
1865 template <typename TComponent>
1866 class NumericTraits<std::complex<TComponent>>
1867 {
1868 public:
1869  using Self = std::complex<TComponent>;
1870  // for backward compatibility
1871  using TheType = Self;
1872  using ValueType = TComponent;
1873  using PrintType = Self;
1874  using AbsType = double;
1875  using AccumulateType = Self;
1876  using RealType = std::complex<double>;
1877  using ScalarRealType = double;
1878  using FloatType = std::complex<float>;
1879  using MeasurementVectorType = FixedArray<ValueType, 2>;
1880 
1881  static const Self ITKCommon_EXPORT Zero;
1882  static const Self ITKCommon_EXPORT One;
1883 
1884  static constexpr Self
1885  min()
1886  {
1887  return std::numeric_limits<ValueType>::min();
1888  }
1889  static constexpr Self
1890  max()
1891  {
1892  return std::numeric_limits<ValueType>::max();
1893  }
1894  static constexpr Self min(Self) { return min(); }
1895  static constexpr Self max(Self) { return max(); }
1896  static constexpr ValueType
1897  epsilon()
1898  {
1899  return std::numeric_limits<ValueType>::epsilon();
1900  }
1901  static constexpr Self
1902  NonpositiveMin()
1903  {
1904  return Self(NumericTraits<ValueType>::NonpositiveMin(), 0);
1905  }
1906 
1907  static constexpr bool
1908  IsPositive(Self val)
1909  {
1910  return val.real() > 0;
1911  }
1912  static constexpr bool
1913  IsNonpositive(Self val)
1914  {
1915  return val.real() <= 0;
1916  }
1917  static constexpr bool
1918  IsNegative(Self val)
1919  {
1920  return val.real() < 0;
1921  }
1922  static constexpr bool
1923  IsNonnegative(Self val)
1924  {
1925  return val.real() >= 0;
1926  }
1927 
1928  static constexpr bool IsSigned = NumericTraits<ValueType>::IsSigned;
1929  static constexpr bool IsInteger = false;
1930  static constexpr bool IsComplex = true;
1931  static Self
1932  ZeroValue()
1933  {
1934  return Self(0, 0);
1935  }
1936  static Self
1937  OneValue()
1938  {
1939  return Self(1, 0);
1940  }
1941  static constexpr unsigned int
1942  GetLength(const Self &)
1943  {
1944  return 2;
1945  }
1946  static constexpr unsigned int
1947  GetLength()
1948  {
1949  return 2;
1950  }
1951  static constexpr Self
1952  NonpositiveMin(const Self &)
1953  {
1954  return NonpositiveMin();
1955  }
1956  static Self
1957  ZeroValue(const Self &)
1958  {
1959  return ZeroValue();
1960  }
1961  static Self
1962  OneValue(const Self &)
1963  {
1964  return OneValue();
1965  }
1966 
1967  template <typename TArray>
1968  static void
1969  AssignToArray(const Self & v, TArray & mv)
1970  {
1971  mv[0] = v.real();
1972  mv[1] = v.imag();
1973  }
1974  static void
1975  SetLength(Self & m, const unsigned int s)
1976  {
1977  if (s != 2)
1978  {
1979  itkGenericExceptionMacro(<< "Cannot set the size of a complex to " << s);
1980  }
1982  }
1983 };
1985 } // end namespace itk
1986 
1987 #include "itkFixedArray.h"
1988 
1989 #endif // itkNumericTraits_h
itk::NumericTraits::NonpositiveMin
static T NonpositiveMin(const T &)
Definition: itkNumericTraits.h:217
itk::NumericTraits::IsPositive
static bool IsPositive(T val)
Definition: itkNumericTraits.h:104
itk::NumericTraits::One
static const T ITKCommon_EXPORT One
Definition: itkNumericTraits.h:93
itk::NumericTraits< InputImagePixelType >::ValueType
InputImagePixelType ValueType
Definition: itkNumericTraits.h:65
itk::NumericTraits::NonpositiveMin
static constexpr T NonpositiveMin()
Definition: itkNumericTraits.h:97
itk::NumericTraits::SetLength
static void SetLength(T &m, const unsigned int s)
Definition: itkNumericTraits.h:185
itk::NumericTraits::min
static constexpr T min(const T &)
Definition: itkNumericTraits.h:172
itk::NumericTraits< InputImagePixelType >::AccumulateType
double AccumulateType
Definition: itkNumericTraits.h:74
itk::NumericTraits::IsSigned
static constexpr bool IsSigned
Definition: itkNumericTraits.h:133
itk::NumericTraits::MeasurementVectorType
FixedArray< ValueType, 1 > MeasurementVectorType
Definition: itkNumericTraits.h:77
itk::NumericTraits::Zero
static const T ITKCommon_EXPORT Zero
Definition: itkNumericTraits.h:90
itkMacro.h
itk::NumericTraits::IsInteger
static constexpr bool IsInteger
Definition: itkNumericTraits.h:138
itkNUMERIC_TRAITS_MIN_MAX_MACRO
#define itkNUMERIC_TRAITS_MIN_MAX_MACRO()
Definition: itkNumericTraits.h:26
itk::NumericTraits::OneValue
static T OneValue()
Definition: itkNumericTraits.h:156
itk::NumericTraits::AssignToArray
static void AssignToArray(const T &v, TArray &mv)
Definition: itkNumericTraits.h:243
itk::NumericTraits< InputImagePixelType >::PrintType
InputImagePixelType PrintType
Definition: itkNumericTraits.h:68
itkFixedArray.h
itk::NumericTraits::OneValue
static T OneValue(const T &)
Definition: itkNumericTraits.h:235
itk::FixedArray
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:52
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:58
itk::NumericTraits::max
static constexpr T max(const T &)
Definition: itkNumericTraits.h:167
itk::NumericTraits::ZeroValue
static T ZeroValue()
Definition: itkNumericTraits.h:148
itk::NumericTraits::GetLength
static unsigned int GetLength(const T &)
Definition: itkNumericTraits.h:201
itk::NumericTraits::IsNonnegative
static bool IsNonnegative(T val)
Definition: itkNumericTraits.h:125
itk::NumericTraits< InputImagePixelType >::ScalarRealType
RealType ScalarRealType
Definition: itkNumericTraits.h:87
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
itk::NumericTraits< InputImagePixelType >::TraitsType
std::numeric_limits< InputImagePixelType > TraitsType
Definition: itkNumericTraits.h:62
itk::NumericTraits::GetLength
static unsigned int GetLength()
Definition: itkNumericTraits.h:208
itk::NumericTraits::IsComplex
static constexpr bool IsComplex
Definition: itkNumericTraits.h:143
itk::NumericTraits::IsNonpositive
static bool IsNonpositive(T val)
Definition: itkNumericTraits.h:111
itk::NumericTraits< InputImagePixelType >::RealType
double RealType
Definition: itkNumericTraits.h:84
itk::NumericTraits::ZeroValue
static T ZeroValue(const T &)
Definition: itkNumericTraits.h:226
itk::NumericTraits< InputImagePixelType >::FloatType
float FloatType
Definition: itkNumericTraits.h:81
itk::NumericTraits< InputImagePixelType >::AbsType
InputImagePixelType AbsType
Definition: itkNumericTraits.h:71
itk::NumericTraits::IsNegative
static bool IsNegative(T val)
Definition: itkNumericTraits.h:118