KWStyle - itkNumericTraits.h
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkNumericTraits.h.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:42 $
7   Version:   $Revision: 1.4 $
8
9   Copyright (c) Insight Software Consortium. All rights reserved.
10   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
12      This software is distributed WITHOUT ANY WARRANTY; without even 
13      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14      PURPOSE.  See the above copyright notices for more information.
15
16 =========================================================================*/
17 #ifndef __itkNumericTraits_h
18 #define __itkNumericTraits_h
19
20 #include "itkMacro.h"
21 #undef min
22 #undef max
23
24 #include "vcl_limits.h" // for vcl_numeric_limits
25 #include <complex>
26
27 namespace itk
28 {
29
30 /** \class NumericTraits
31  * \brief Define additional traits for native types such as int or float.
32  *
33  * NumericTraits is used to extend the traits associated with native types
34  * such as float, char, int, and so on. These traits are extensions of the
35  * standard <numeric_limits> defined by the C++ compilers. Some of the added
36  * traits include minimum and maximum value; accumulation type; etc.
37  *
38  * \ingroup DataRepresentation
39  */
40 template <class T>
41 class NumericTraits : public vcl_numeric_limits<T> {
42 public:
43   /** The type of this limits trait object. */
44   typedef vcl_numeric_limits<T> TraitsType;
45
46   /** Return the type of this native type. */
47   typedef T ValueType; 
48
49   /** Return the type that can be printed. */
50   typedef T PrintType; 
51
52   /** Return value of abs(). */
53   typedef T AbsType; 
54
55   /** Accumulation of addition and multiplication. */
56   typedef double AccumulateType; 
57
58   // This primary template is never used but we need this definition
59   // to avoid an ICE on VS 7.0.  This definition cannot be present for
60   // VS 7.1 though or it generates bogus errors.
61 #if defined(_MSC_VER) && _MSC_VER == 1300
62   /** Type for real-valued operations.  */
63   typedef double RealType;
64 TDA   typedef RealType ScalarRealType;
65 #endif
66
67   /** Typedef for operations that use floating point instead of real precision
68    *  to save memory */
69   typedef float FloatType;
70
71   /** Additive identity. */
72   static const T Zero;
73
74   /** Multiplicative identity. */
75   static const T One;
76
77   /** Smallest (most nonpositive) value **/
78   static T NonpositiveMin() { return TraitsType::min(); }
79
80   /** Is a given value positive? **/
81   static bool IsPositive(T val) { return val > Zero; }
82
83   /** Is a given value nonpositive? **/
84   static bool IsNonpositive(T val) { return val <= Zero; }
85
86   /** Is a given value negative? **/
87   static bool IsNegative(T val) { return val < Zero; }
88
89   /** Is a given value nonnegative? **/
90   static bool IsNonnegative(T val) { return val >= Zero; }
91
92   /** Return zero value. This function should be used to support
93    *  RGBPixel type and standard types (not vectors) */
94   static T ZeroValue() { return Zero; }
95 };
96
97 /** \class NumericTraits<bool>
98  * \brief Define traits for type bool.
99  * 
100  * \ingroup DataRepresentation
101  */
102
103 template <>
104 class NumericTraits<bool> : public vcl_numeric_limits<bool> {
105 public:
106   typedef bool ValueType;
107   typedef bool PrintType;
108 TDA   typedef unsigned char AbsType;
109 TDA   typedef unsigned char AccumulateType;
110 TDA   typedef double RealType;
111 TDA   typedef RealType ScalarRealType;
112 TDA   typedef float FloatType;
113   static const bool ITKCommon_EXPORT Zero;
114   static const bool ITKCommon_EXPORT One;
115
116   static bool min() { return false; }
117   static bool max() { return true; }
118   static bool NonpositiveMin() { return false; }
119   static bool IsPositive(bool val) { return val; }
120   static bool IsNonpositive(bool val) { return !val; }
121   static bool IsNegative(bool /* val */) { return false; }
122   static bool IsNonnegative(bool /*val*/) {return true; }
123   static bool ZeroValue() { return Zero; }
124 };
125
126 /** \class NumericTraits<char>
127  * \brief Define traits for type char.
128  * NOTE: char is not guarenteed to be signed. On SGI's, thge default is unsigned
129  */
130 template <>
131 class NumericTraits<char> : public vcl_numeric_limits<char> {
132 public:
133   typedef char ValueType;
134 TDA   typedef int PrintType;
135 TDA   typedef unsigned char AbsType;
136 TDA   typedef short AccumulateType;
137 TDA   typedef double RealType;
138 TDA   typedef RealType ScalarRealType;
139 TDA   typedef float FloatType;
140   static const char ITKCommon_EXPORT Zero;
141   static const char ITKCommon_EXPORT One;
142
143   static char min() { return char(255) < 0 ? -128 : 0; }
144   static char max() { return char(255) < 0 ? 127 : 255; }
145   static char NonpositiveMin() { return min(); }
146   static bool IsPositive(char val) { return val > Zero; }
147   static bool IsNonpositive(char val) { return val <= Zero; }
148   static bool IsNegative(char val) { return val < Zero; }
149   static bool IsNonnegative(char val) {return val >= Zero; }
150   static char ZeroValue() { return Zero; }
151 };
152
153 /** \class NumericTraits<char>
154  * \brief Define traits for type char.
155  * NOTE: char is not guarenteed to be signed. On SGI's, thge default is unsigned
156  */
157 template <>
158 class NumericTraits<signed char> : public vcl_numeric_limits<signed char> {
159 public:
160   typedef signed char ValueType;
161 TDA   typedef int PrintType;
162 TDA   typedef unsigned char AbsType;
163 TDA   typedef short AccumulateType;
164 TDA   typedef double RealType;
165 TDA   typedef RealType ScalarRealType;
166 TDA   typedef float FloatType;
167   static const signed char ITKCommon_EXPORT Zero;
168   static const signed char ITKCommon_EXPORT One;
169
170   static signed char min() { return -128; }
171   static signed char max() { return  127; }
172   static signed char NonpositiveMin() { return min(); }
173   static bool IsPositive(signed char val) { return val > Zero; }
174   static bool IsNonpositive(signed char val) { return val <= Zero; }
175   static bool IsNegative(signed char val) { return val < Zero; }
176   static bool IsNonnegative(signed char val) {return val >= Zero; }
177   static signed char  ZeroValue() { return Zero; }
178 };
179
180 /** \class NumericTraits<unsigned char>
181  * \brief Define traits for type unsigned char.
182  * \ingroup DataRepresentation
183  */
184 template <>
185 class NumericTraits<unsigned char> : public vcl_numeric_limits<unsigned char> {
186 public:
187   typedef unsigned char ValueType;
188 TDA   typedef int PrintType;
189   typedef unsigned char AbsType;
190 TDA   typedef unsigned short AccumulateType;
191 TDA   typedef double RealType;
192 TDA   typedef RealType ScalarRealType;
193 TDA   typedef float FloatType;
194   static const unsigned char ITKCommon_EXPORT Zero;
195   static const unsigned char ITKCommon_EXPORT One;
196
197   static unsigned char NonpositiveMin() { return min(); }
198   static bool IsPositive(unsigned char val) { return val != Zero; }
199   static bool IsNonpositive(unsigned char val) { return val == Zero; }
200   static bool IsNegative(unsigned char /* val */) { return false; }
201   static bool IsNonnegative(unsigned char /*val */) {return true; }
202   static unsigned char  ZeroValue() { return Zero; }
203 };
204
205 /** \class NumericTraits<short>
206  * \brief Define traits for type short.
207  */
208 template <>
209 class NumericTraits<short> : public vcl_numeric_limits<short> {
210 public:
211   typedef short ValueType;
212   typedef short PrintType;
213 TDA   typedef unsigned short AbsType;
214 TDA   typedef int AccumulateType;
215 TDA   typedef double RealType;
216 TDA   typedef RealType ScalarRealType;
217   typedef float FloatType;
218   static const short ITKCommon_EXPORT Zero;
219   static const short ITKCommon_EXPORT One;
220
221   static short NonpositiveMin() { return min(); }
222   static bool IsPositive(short val) { return val > Zero; }
223   static bool IsNonpositive(short val) { return val <= Zero; }
224   static bool IsNegative(short val) { return val < Zero; }
225   static bool IsNonnegative(short val) {return val >= Zero; }
226   static short  ZeroValue() { return Zero; }
227 };
228
229 /** \class NumericTraits<unsigned short>
230  * \brief Define traits for type unsigned short.
231  * \ingroup DataRepresentation
232  */
233 template <>
234 LEN class NumericTraits<unsigned short> : public vcl_numeric_limits<unsigned short> {
235 public:
236   typedef unsigned short ValueType;
237   typedef unsigned short PrintType;
238   typedef unsigned short AbsType;
239 TDA   typedef unsigned int AccumulateType;
240 TDA   typedef double RealType;
241 TDA   typedef RealType ScalarRealType;
242 TDA   typedef float FloatType;
243   static const unsigned short ITKCommon_EXPORT Zero;
244   static const unsigned short ITKCommon_EXPORT One;
245
246   static unsigned short NonpositiveMin() { return min(); }
247   static unsigned short IsPositive(unsigned short val) { return val != Zero; }
248   static bool IsNonpositive(unsigned short val) { return val == Zero; }
249   static bool IsNegative(unsigned short/* val*/) { return false; }
250   static bool IsNonnegative(unsigned short /*val*/) {return true; }
251   static unsigned short  ZeroValue() { return Zero; }
252 };
253
254 /** \class NumericTraits<int>
255  * \brief Define traits for type int.
256  */
257 template <>
258 class NumericTraits<int> : public vcl_numeric_limits<int> {
259 public:
260   typedef int ValueType;
261   typedef int PrintType;
262 TDA   typedef unsigned int AbsType;
263 TDA   typedef long AccumulateType;
264 TDA   typedef double RealType;
265 TDA   typedef RealType ScalarRealType;
266 TDA   typedef float FloatType;
267   static const int ITKCommon_EXPORT Zero;
268   static const int ITKCommon_EXPORT One;
269
270   static int NonpositiveMin() { return min(); }
271   static bool IsPositive(int val) { return val > Zero; }
272   static bool IsNonpositive(int val) { return val <= Zero; }
273   static bool IsNegative(int val) { return val < Zero; }
274   static bool IsNonnegative(int val) {return val >= Zero; }
275   static int  ZeroValue() { return Zero; }
276 };
277
278 /** \class NumericTraits<unsigned int>
279  * \brief Define traits for type unsigned int.
280  * \ingroup DataRepresentation
281  */
282 template <>
283 class NumericTraits<unsigned int> : public vcl_numeric_limits<unsigned int> {
284 public:
285   typedef unsigned int ValueType;
286   typedef unsigned int PrintType;
287   typedef unsigned int AbsType;
288   typedef unsigned int AccumulateType;
289 TDA   typedef double RealType;
290 TDA   typedef RealType ScalarRealType;
291 TDA   typedef float FloatType;
292   static const unsigned int ITKCommon_EXPORT Zero;
293   static const unsigned int ITKCommon_EXPORT One;
294
295   static unsigned int min(void) { return 0; }
296   static unsigned int max(void) { return static_cast<unsigned int>( -1 ); }
297   static unsigned int NonpositiveMin() { return 0; }
298   static bool IsPositive(unsigned int val) { return val != Zero; }
299   static bool IsNonpositive(unsigned int val) { return  val == Zero; }
300   static bool IsNegative(unsigned int /*val*/) { return false; }
301   static bool IsNonnegative(unsigned int /*val*/) {return true; }
302   static unsigned int  ZeroValue() { return Zero; }
303 };
304
305 /** \class NumericTraits<long>
306  * \brief Define traits for type long.
307  * \ingroup DataRepresentation
308  */
309 template <>
310 class NumericTraits<long> : public vcl_numeric_limits<long> {
311 public:
312   typedef long ValueType;
313   typedef long PrintType;
314 TDA   typedef unsigned long AbsType;
315   typedef long AccumulateType;
316 TDA   typedef double RealType;
317 TDA   typedef RealType ScalarRealType;
318 TDA   typedef float FloatType;
319   static const long ITKCommon_EXPORT Zero;
320   static const long ITKCommon_EXPORT One;
321
322   static long NonpositiveMin() { return min(); }
323   static bool IsPositive(long val) { return val > Zero; }
324   static bool IsNonpositive(long val) { return val <= Zero; }
325   static bool IsNegative(long val) { return val < Zero; }
326   static bool IsNonnegative(long val) {return val >= Zero; }
327   static long  ZeroValue() { return Zero; }
328 };
329
330 /** \class NumericTraits<unsigned long>
331  * \brief Define traits for type unsigned long.
332  * \ingroup DataRepresentation 
333  */
334 template <>
335 class NumericTraits<unsigned long> : public vcl_numeric_limits<unsigned long> {
336 public:
337   typedef unsigned long ValueType;
338   typedef unsigned long PrintType;
339   typedef unsigned long AbsType;
340   typedef unsigned long AccumulateType;
341 TDA   typedef double RealType;
342 TDA   typedef RealType ScalarRealType;
343 TDA   typedef float FloatType;
344   static const unsigned long ITKCommon_EXPORT Zero;
345   static const unsigned long ITKCommon_EXPORT One;
346
347   static unsigned long NonpositiveMin() { return min(); }
348   static bool IsPositive(unsigned long val) { return val != Zero; }
349   static bool IsNonpositive(unsigned long val) { return val == Zero; }
350   static bool IsNegative(unsigned long) { return false; }
351   static bool IsNonnegative(unsigned long) {return true; }
352   static unsigned long  ZeroValue() { return Zero; }
353 };
354
355 /** \class NumericTraits<float>
356  * \brief Define traits for type float.
357  * \ingroup DataRepresentation
358  */
359 template <>
360 class NumericTraits<float> : public vcl_numeric_limits<float> {
361 public:
362   typedef float ValueType;
363   typedef float PrintType;
364   typedef float AbsType;
365 TDA   typedef double AccumulateType;
366 TDA   typedef double RealType;
367 TDA   typedef RealType ScalarRealType;
368   typedef float FloatType;
369   static const float ITKCommon_EXPORT Zero;
370   static const float ITKCommon_EXPORT One;
371
372   static float NonpositiveMin() { return -max(); }
373   static bool IsPositive(float val) { return val > Zero; }
374   static bool IsNonpositive(float val) { return val <= Zero; }
375   static bool IsNegative(float val) { return val < Zero; }
376   static bool IsNonnegative(float val) {return val >= Zero; }
377   static float  ZeroValue() { return Zero; }
378 };
379
380 /** \class NumericTraits<double>
381  * \brief Define traits for type double.
382  * \ingroup DataRepresentation 
383  */
384 template <>
385 class NumericTraits<double> : public vcl_numeric_limits<double> {
386 public:
387   typedef double ValueType;
388   typedef double PrintType;
389   typedef double AbsType;
390   typedef double AccumulateType;
391   typedef double RealType;
392 TDA   typedef RealType ScalarRealType;
393 TDA   typedef float FloatType;
394   static const double ITKCommon_EXPORT Zero;
395   static const double ITKCommon_EXPORT One;
396
397   static double NonpositiveMin() { return -max(); }
398   static bool IsPositive(double val) { return val > Zero; }
399   static bool IsNonpositive(double val) { return val <= Zero; }
400   static bool IsNegative(double val) { return val < Zero; }
401   static bool IsNonnegative(double val) {return val >= Zero; }
402   static double  ZeroValue() { return Zero; }
403 };
404
405 /** \class NumericTraits<long double>
406  * \brief Define traits for type long double.
407  * \ingroup DataRepresentation 
408  */
409 template <>
410 class NumericTraits<long double> : public vcl_numeric_limits<long double> {
411 public:
412   typedef long double ValueType;
413   typedef long double PrintType;
414   typedef long double AbsType;
415   typedef long double AccumulateType;
416   typedef long double RealType;
417 TDA   typedef RealType ScalarRealType;
418 TDA   typedef float FloatType;
419   static const long double ITKCommon_EXPORT Zero;
420   static const long double ITKCommon_EXPORT One;
421
422   static long double NonpositiveMin() { return -max(); }
423   static bool IsPositive(long double val) { return val > Zero; }
424   static bool IsNonpositive(long double val) { return val <= Zero; }
425   static bool IsNegative(long double val) { return val < Zero; }
426   static bool IsNonnegative(long double val) {return val >= Zero; }
427   static long double ZeroValue() { return Zero; }
428 };
429
430 /** \class NumericTraits< std::complex<float> >
431  * \brief Define traits for type std::complex<float>.
432  * \ingroup DataRepresentation 
433  */
434 template <>
435 class NumericTraits< std::complex<float> >  {
436 public:
437   typedef std::complex<float> TheType;
438 TDA   typedef float ValueType;
439 TDA   typedef TheType PrintType;
440 TDA   typedef double AbsType;
441 TDA   typedef TheType AccumulateType;
442 TDA   typedef std::complex<double> RealType;
443 TDA   typedef double ScalarRealType;
444   typedef std::complex<float> FloatType;
445   static const TheType ITKCommon_EXPORT Zero;
446   static const TheType ITKCommon_EXPORT One;
447
448   static TheType NonpositiveMin() { 
449     return TheType(-NumericTraits<float>::NonpositiveMin(),0.0); }
450   static bool IsPositive(TheType val) { return val.real() > 0.0; }
451   static bool IsNonpositive(TheType val) { return val.real() <= 0.0; }
452   static bool IsNegative(TheType val) { return val.real() < 0.0; }
453   static bool IsNonnegative(TheType val) {return val.real() >= 0.0; }
454   static TheType ZeroValue() { return Zero; }
455 };
456
457
458 /** \class NumericTraits< std::complex<double> >
459  * \brief Define traits for type std::complex<double>.
460  * \ingroup DataRepresentation 
461  */
462 template <>
463 class NumericTraits< std::complex<double> >  {
464 public:
465   typedef std::complex<double> TheType;
466 TDA   typedef double ValueType;
467 TDA   typedef TheType PrintType;
468 TDA   typedef double AbsType;
469 TDA   typedef TheType AccumulateType;
470   typedef std::complex<double> RealType;
471 TDA   typedef double ScalarRealType;
472 TDA   typedef std::complex<float> FloatType;
473   static const TheType ITKCommon_EXPORT Zero;
474   static const TheType ITKCommon_EXPORT One;
475
476   static TheType NonpositiveMin() { 
477     return TheType(-NumericTraits<double>::NonpositiveMin(),0.0); }
478   static bool IsPositive(TheType val) { return val.real() > 0.0; }
479   static bool IsNonpositive(TheType val) { return val.real() <= 0.0; }
480   static bool IsNegative(TheType val) { return val.real() < 0.0; }
481   static bool IsNonnegative(TheType val) {return val.real() >= 0.0; }
482   static TheType ZeroValue() { return Zero; }
483 };
484
485
486 // end namespace itk
487
488 #endif // __itkNumericTraits_h
489

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:27PM
© Kitware Inc.