forked from InsightSoftwareConsortium/ITK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itkMath.h
214 lines (188 loc) · 7.95 KB
/
itkMath.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkMath.h
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
Portions of this code are covered under the VTK copyright.
See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __itkMath_h
#define __itkMath_h
#include "itkConfigure.h"
#include "itkIntTypes.h"
#include "itkMathDetail.h"
#include "itkConceptChecking.h"
namespace itk
{
namespace Math
{
// These constants originate from VXL's vnl_math.h. They have been
// moved here to improve visibility, and to ensure that the constants
// are available during compile time ( as opposed to static const
// member vaiables ).
/** \brief \f[e] The base of the natural logarithm or Euler's number */
static const double e = 2.7182818284590452354;
/** \brief \f[ \log_2 e \f] */
static const double log2e = 1.4426950408889634074;
/** \brief \f[ \log_10 e \f] */
static const double log10e = 0.43429448190325182765;
/** \brief \f[ \log_e 2 \f] */
static const double ln2 = 0.69314718055994530942;
/** \brief \f[ \log_e 10 \f] */
static const double ln10 = 2.30258509299404568402;
/** \brief \f[ \pi ] */
static const double pi = 3.14159265358979323846;
/** \brief \f[ \frac{\pi}{2} \f] */
static const double pi_over_2 = 1.57079632679489661923;
/** \brief \f[ \frac{\pi}{4} \f] */
static const double pi_over_4 = 0.78539816339744830962;
/** \brief \f[ \frac{1}{\pi} \f] */
static const double one_over_pi = 0.31830988618379067154;
/** \brief \f[ \frac{2}{\pi} \f] */
static const double two_over_pi = 0.63661977236758134308;
/** \brief \f[ \frac{2}{\sqrt{\pi}} \f] */
static const double two_over_sqrtpi = 1.12837916709551257390;
/** \brief \f[ \frac{2}{\sqrt{2\pi}} \f] */
static const double one_over_sqrt2pi = 0.39894228040143267794;
/** \brief \f[ \sqrt{2} \f] */
static const double sqrt2 = 1.41421356237309504880;
/** \brief \f[ \sqrt{ \frac{1}{2}} \f] */
static const double sqrt1_2 = 0.70710678118654752440;
/** A useful macro to generate a template floating point to integer
* conversion templated on the return type and using either the 32
* bit, the 64 bit or the vanilla version */
#define itkTemplateFloatingToIntegerMacro(name) \
template <typename TReturn,typename TInput> \
inline TReturn name(TInput x) \
{ \
\
if (sizeof(TReturn) <= 4) \
{ \
return static_cast<TReturn>(Detail::name##_32(x)); \
} \
else if (sizeof(TReturn) <= 8) \
{ \
return static_cast<TReturn>(Detail::name##_64(x)); \
} \
else \
{ \
return static_cast<TReturn>(Detail::name##_base<TReturn,TInput>(x)); \
} \
}
/** \brief Round towards nearest integer
*
* \tparam TReturn must be an interger type
* \tparam TInput must be float or double
*
* halfway cases are rounded towards the nearest even
* integer, e.g.
* \code
* RoundHalfIntegerToEven( 1.5) == 2
* RoundHalfIntegerToEven(-1.5) == -2
* RoundHalfIntegerToEven( 2.5) == 2
* RoundHalfIntegerToEven( 3.5) == 4
* \endcode
*
* The behavior of overflow is undefined due to numerous implementations.
*
* \warning We assume that the rounding mode is not changed from the default
* one (or at least that it is always restored to the default one).
*/
itkTemplateFloatingToIntegerMacro(RoundHalfIntegerToEven);
/** \brief Round towards nearest integer
*
* \tparam TReturn must be an interger type
* \tparam TInput must be float or double
*
* halfway cases are rounded upward, e.g.
* \code
* RoundHalfIntegerUp( 1.5) == 2
* RoundHalfIntegerUp(-1.5) == -1
* RoundHalfIntegerUp( 2.5) == 3
* \endcode
*
* The behavior of overflow is undefined due to numerous implementations.
*
* \warning The argument absolute value must be less than
* NumbericTraits<TReturn>::max()/2 for RoundHalfIntegerUp to be
* guaranteed to work.
*
* \warning We also assume that the rounding mode is not changed from
* the default one (or at least that it is always restored to the
* default one).
*/
itkTemplateFloatingToIntegerMacro(RoundHalfIntegerUp);
/** \brief Round towards nearest integer (This is a synonym for RoundHalfIntegerUp)
*
* \tparam TReturn must be an interger type
* \tparam TInput must be float or double
*
* \sa RoundHalfIntegerUp<TReturn, TInput>()
*/
template <typename TReturn, typename TInput>
inline TReturn Round(TInput x) { return RoundHalfIntegerUp<TReturn,TInput>(x); }
/** \brief Round towards minus infinity
*
* The behavior of overflow is undefined due to numerous implementations.
*
* \warning argument absolute value must be less than
* NumbericTraits<TReturn>::max()/2 for vnl_math_floor to be
* guaranteed to work.
*
* \warning We also assume that the rounding mode is not changed from
* the default one (or at least that it is always restored to the
* default one).
*/
itkTemplateFloatingToIntegerMacro(Floor);
/** \brief Round towards plus infinity
*
* The behavior of overflow is undefined due to numerous implementations.
*
* \warning argument absolute value must be less than INT_MAX/2
* for vnl_math_ceil to be guaranteed to work.
* \warning We also assume that the rounding mode is not changed from
* the default one (or at least that it is always restored to the
* default one).
*/
itkTemplateFloatingToIntegerMacro(Ceil);
#undef itkTemplateFloatingToIntegerMacro
template <typename TReturn,typename TInput>
inline TReturn CastWithRangeCheck(TInput x)
{
#ifdef ITK_USE_CONCEPT_CHECKING
itkConceptMacro( OnlyDefinedForIntegerTypes1, (itk::Concept::IsInteger<TReturn>) );
itkConceptMacro( OnlyDefinedForIntegerTypes2, (itk::Concept::IsInteger<TInput>) );
#endif // ITK_USE_CONCEPT_CHECKING
TReturn ret = static_cast<TReturn>(x);
if ( sizeof (TReturn) > sizeof(TInput) &&
!( !itk::NumericTraits<TReturn>::is_signed && itk::NumericTraits<TInput>::is_signed ) )
{
// if the output type is bigger and we are not converting a signed
// interger to an unsigned interger then we have no problems
return ret;
}
else if ( sizeof (TReturn) >= sizeof(TInput) )
{
if ( itk::NumericTraits<TInput>::IsPositive(x) != itk::NumericTraits<TReturn>::IsPositive(ret) )
{
itk::RangeError _e(__FILE__, __LINE__);
throw _e;
}
}
else if ( static_cast<TInput>(ret) != x ||
( itk::NumericTraits<TInput>::IsPositive(x) != itk::NumericTraits<TReturn>::IsPositive(ret) ) )
{
itk::RangeError _e(__FILE__, __LINE__);
throw _e;
}
return ret;
}
} // end namespace Math
} // end namespace itk
#endif // end of itkMath.h