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