ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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 ITK_MACROEND_NOOP_STATEMENT
32
33#include <limits> // for std::numeric_limits
34#include <complex>
35#include <type_traits> // for std::is_floating_point
36
37namespace itk
38{
39
40// forward declare to avoid circular dependencies
41template <typename TValue, unsigned int VLength>
42class FixedArray;
43
59template <typename T>
60class NumericTraits : public std::numeric_limits<T>
61{
62public:
64 using TraitsType = std::numeric_limits<T>;
65
67 using ValueType = T;
68
70 using PrintType = T;
71
73 using AbsType = T;
74
76 using AccumulateType = double;
77
80
83 using FloatType = float;
84
86 using RealType = double;
87
90
92 static const T ITKCommon_EXPORT Zero;
93
95 static const T ITKCommon_EXPORT One;
96
98 static constexpr T
100 {
101 return TraitsType::lowest();
102 }
103
105 static bool
107 {
108 return val > Zero;
109 }
110
112 static bool
114 {
115 return val <= Zero;
116 }
117
119 static bool
121 {
122 return val < Zero;
123 }
124
126 static bool
128 {
129 return val >= Zero;
130 }
131
135 static constexpr bool IsSigned = false;
136
140 static constexpr bool IsInteger = false;
141
145 static constexpr bool IsComplex = false;
146
149 static T
151 {
152 return Zero;
153 }
154
157 static T
159 {
160 return One;
161 }
162
163 /* Provide a default implementation of the max() method with
164 * argument. This API is needed for VariableLengthVector because
165 * its length is only known at run-time. Specializations of the
166 * VariableLengthVector will provide a different implementation
167 * where a vector of the correct size is built. */
168 static constexpr T
169 max(const T &)
170 {
171 return TraitsType::max();
172 }
173 static constexpr T
174 min(const T &)
175 {
176 return TraitsType::min();
177 }
178
186 static void
187 SetLength(T & m, const unsigned int s)
188 {
189 if (s != 1)
190 {
191 itkGenericExceptionMacro("Cannot set the size of a scalar to " << s);
192 }
193 m = ValueType{};
194 }
195
201 static constexpr unsigned int
202 GetLength(const T &)
203 {
204 return GetLength();
205 }
206
208 static constexpr 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
258
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 itkNotUsed(val))
312 {
313 return false;
314 }
315 static constexpr bool
316 IsNonnegative(bool itkNotUsed(val))
317 {
318 return 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 itkNotUsed(val))
644 {
645 return false;
646 }
647 static constexpr bool
648 IsNonnegative(unsigned char itkNotUsed(val))
649 {
650 return 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 itkNotUsed(val))
849 {
850 return false;
851 }
852 static constexpr bool
853 IsNonnegative(unsigned short itkNotUsed(val))
854 {
855 return 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 itkNotUsed(val))
1073 {
1074 return false;
1075 }
1076 static constexpr bool
1077 IsNonnegative(unsigned int itkNotUsed(val))
1078 {
1079 return 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 using PrintType = long double;
1561 using AbsType = long double;
1562 using AccumulateType = long double;
1563 using RealType = long double;
1564 using ScalarRealType = RealType;
1565 using FloatType = float;
1566 using MeasurementVectorType = FixedArray<ValueType, 1>;
1567
1568 static constexpr long double ITKCommon_EXPORT Zero = 0.0;
1569 static constexpr long double ITKCommon_EXPORT One = 1.0;
1570
1572 static constexpr long double
1574 {
1575 return std::numeric_limits<ValueType>::lowest();
1576 }
1577 static constexpr bool
1578 IsPositive(long double val)
1579 {
1580 return val > Zero;
1581 }
1582 static constexpr bool
1583 IsNonpositive(long double val)
1584 {
1585 return val <= Zero;
1586 }
1587 static constexpr bool
1588 IsNegative(long double val)
1589 {
1590 return val < Zero;
1591 }
1592 static constexpr bool
1593 IsNonnegative(long double val)
1594 {
1595 return val >= Zero;
1596 }
1597 static constexpr bool IsSigned = true;
1598 static constexpr bool IsInteger = false;
1599 static constexpr bool IsComplex = false;
1600 static constexpr long double
1601 ZeroValue()
1602 {
1603 return Zero;
1604 }
1605 static constexpr long double
1606 OneValue()
1607 {
1608 return One;
1609 }
1610 static constexpr unsigned int
1611 GetLength(const ValueType &)
1612 {
1613 return 1;
1614 }
1615 static constexpr unsigned int
1616 GetLength()
1617 {
1618 return 1;
1619 }
1620 static constexpr ValueType
1621 NonpositiveMin(const ValueType &)
1622 {
1623 return NonpositiveMin();
1624 }
1625 static constexpr ValueType
1626 ZeroValue(const ValueType &)
1627 {
1628 return ZeroValue();
1629 }
1630 static constexpr ValueType
1631 OneValue(const ValueType &)
1632 {
1633 return OneValue();
1634 }
1635
1636 template <typename TArray>
1637 static void
1638 AssignToArray(const ValueType & v, TArray & mv)
1639 {
1640 mv[0] = v;
1641 }
1642 static void
1643 SetLength(ValueType & m, const unsigned int s)
1644 {
1645 if (s != 1)
1646 {
1647 itkGenericExceptionMacro("Cannot set the size of a scalar to " << s);
1648 }
1649 m = ValueType{};
1650 }
1651};
1652
1653
1659template <>
1660class NumericTraits<long long> : public std::numeric_limits<long long>
1661{
1662public:
1663 using ValueType = long long;
1664 using PrintType = long long;
1665 using AbsType = long long;
1666 using AccumulateType = long long;
1667 using RealType = double;
1668 using ScalarRealType = RealType;
1669 using FloatType = float;
1670 using MeasurementVectorType = FixedArray<ValueType, 1>;
1671
1672 static constexpr ValueType ITKCommon_EXPORT Zero = 0LL;
1673 static constexpr ValueType ITKCommon_EXPORT One = 1LL;
1674
1676 static constexpr ValueType
1678 {
1679 return std::numeric_limits<ValueType>::lowest();
1680 }
1681 static constexpr bool
1683 {
1684 return val > Zero;
1685 }
1686 static constexpr bool
1688 {
1689 return val <= Zero;
1690 }
1691 static constexpr bool
1693 {
1694 return val < Zero;
1695 }
1696 static constexpr bool
1698 {
1699 return val >= Zero;
1700 }
1701 static constexpr bool IsSigned = true;
1702 static constexpr bool IsInteger = true;
1703 static constexpr bool IsComplex = false;
1704 static constexpr ValueType
1705 ZeroValue()
1706 {
1707 return Zero;
1708 }
1709 static constexpr ValueType
1710 OneValue()
1711 {
1712 return One;
1713 }
1714 static constexpr unsigned int
1715 GetLength(const ValueType &)
1716 {
1717 return 1;
1718 }
1719 static constexpr unsigned int
1720 GetLength()
1721 {
1722 return 1;
1723 }
1724 static constexpr ValueType
1725 NonpositiveMin(const ValueType &)
1726 {
1727 return NonpositiveMin();
1728 }
1729 static constexpr ValueType
1730 ZeroValue(const ValueType &)
1731 {
1732 return ZeroValue();
1733 }
1734 static constexpr ValueType
1735 OneValue(const ValueType &)
1736 {
1737 return OneValue();
1738 }
1739
1740 template <typename TArray>
1741 static void
1742 AssignToArray(const ValueType & v, TArray & mv)
1743 {
1744 mv[0] = v;
1745 }
1746 static void
1747 SetLength(ValueType & m, const unsigned int s)
1748 {
1749 if (s != 1)
1750 {
1751 itkGenericExceptionMacro("Cannot set the size of a scalar to " << s);
1752 }
1753 m = ValueType{};
1754 }
1755};
1756
1762template <>
1763class NumericTraits<unsigned long long> : public std::numeric_limits<unsigned long long>
1764{
1765public:
1766 using ValueType = unsigned long long;
1767 using PrintType = unsigned long long;
1768 using AbsType = unsigned long long;
1769 using AccumulateType = unsigned long long;
1770 using RealType = double;
1771 using ScalarRealType = RealType;
1772 using FloatType = float;
1773 using MeasurementVectorType = FixedArray<ValueType, 1>;
1774
1775 static constexpr ValueType ITKCommon_EXPORT Zero = 0ULL;
1776 static constexpr ValueType ITKCommon_EXPORT One = 1ULL;
1777
1779 static constexpr ValueType
1781 {
1782 return std::numeric_limits<ValueType>::lowest();
1783 }
1784 static constexpr bool
1786 {
1787 return val != Zero;
1788 }
1789 static constexpr bool
1791 {
1792 return val == Zero;
1793 }
1794 static constexpr bool
1796 {
1797 return false;
1798 }
1799 static constexpr bool
1801 {
1802 return true;
1803 }
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
1896 min(Self)
1897 {
1898 return min();
1899 }
1900 static constexpr Self
1901 max(Self)
1902 {
1903 return max();
1904 }
1905 static constexpr ValueType
1906 epsilon()
1907 {
1908 return std::numeric_limits<ValueType>::epsilon();
1909 }
1910 static constexpr Self
1912 {
1914 }
1915
1916 static constexpr bool
1917 IsPositive(Self val)
1918 {
1919 return val.real() > 0;
1920 }
1921 static constexpr bool
1922 IsNonpositive(Self val)
1923 {
1924 return val.real() <= 0;
1925 }
1926 static constexpr bool
1927 IsNegative(Self val)
1928 {
1929 return val.real() < 0;
1930 }
1931 static constexpr bool
1932 IsNonnegative(Self val)
1933 {
1934 return val.real() >= 0;
1935 }
1936
1937 static constexpr bool IsSigned = std::is_signed_v<ValueType>;
1938 static constexpr bool IsInteger = false;
1939 static constexpr bool IsComplex = true;
1940 static Self
1941 ZeroValue()
1942 {
1943 return Self(0, 0);
1944 }
1945 static Self
1946 OneValue()
1947 {
1948 return Self(1, 0);
1949 }
1950 static constexpr unsigned int
1951 GetLength(const Self &)
1952 {
1953 return 2;
1954 }
1955 static constexpr unsigned int
1956 GetLength()
1957 {
1958 return 2;
1959 }
1960 static constexpr Self
1961 NonpositiveMin(const Self &)
1962 {
1963 return NonpositiveMin();
1964 }
1965 static Self
1966 ZeroValue(const Self &)
1967 {
1968 return ZeroValue();
1969 }
1970 static Self
1971 OneValue(const Self &)
1972 {
1973 return OneValue();
1974 }
1975
1976 template <typename TArray>
1977 static void
1978 AssignToArray(const Self & v, TArray & mv)
1979 {
1980 mv[0] = v.real();
1981 mv[1] = v.imag();
1982 }
1983 static void
1984 SetLength(Self & m, const unsigned int s)
1985 {
1986 if (s != 2)
1987 {
1988 itkGenericExceptionMacro("Cannot set the size of a complex to " << s);
1989 }
1990 m = ValueType{};
1991 }
1992
1993#if defined(ITK_LEGACY_REMOVE)
1994 static_assert(std::is_floating_point_v<TComponent>,
1995 "As per https://en.cppreference.com/w/cpp/numeric/complex the behavior is unspecified and may fail to "
1996 "compile if TComponent is not float, double, or long double and undefined if T is not NumericType.");
1997#endif // defined(ITK_LEGACY_REMOVE)
1998};
2000} // end namespace itk
2001
2002#include "itkFixedArray.h"
2003
2004#endif // itkNumericTraits_h
Simulate a standard C array with copy semantics.
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 const AbsoluteFrequencyType ITKCommon_EXPORT Zero
static bool IsNonpositive(T val)
static constexpr unsigned int GetLength()
static T NonpositiveMin(const T &)
static const AbsoluteFrequencyType ITKCommon_EXPORT One
static constexpr unsigned int GetLength(const T &)
static void SetLength(T &m, const unsigned int s)
static bool IsNonnegative(T val)
FixedArray< ValueType, 1 > MeasurementVectorType
static constexpr bool IsComplex
#define itkNUMERIC_TRAITS_MIN_MAX_MACRO()
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....