/usr/include/ITK-4.5/itkMath.h is in libinsighttoolkit4-dev 4.5.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | /*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
/*=========================================================================
*
* Portions of this file are subject to the VTK Toolkit Version 3 copyright.
*
* Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
*
* For complete copyright, license and disclaimer of warranty information
* please refer to the NOTICE file at the top of the ITK source tree.
*
*=========================================================================*/
#ifndef __itkMath_h
#define __itkMath_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\f] 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 \f] */
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 integer 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 integer 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 integer 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
// integer to an unsigned integer 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;
}
/** \brief Return the signed distance in ULPs (units in the last place) between two floats.
*
* This is the signed distance, i.e., if x1 > x2, then the result is positive.
*
* \sa FloatAlmostEqual
*/
template <typename T>
inline typename Detail::FloatIEEE<T>::IntType
FloatDifferenceULP( T x1, T x2 )
{
Detail::FloatIEEE<T> x1f(x1);
Detail::FloatIEEE<T> x2f(x2);
return x1f.AsULP() - x2f.AsULP();
}
/** \brief Compare two floats and return if they are effectively equal.
*
* Determining when floats are almost equal is difficult because of their
* IEEE bit representation. This function uses the integer representation of
* the float to determine if they are almost equal.
*
* The implementation is based off the explanation in the white papers:
*
* - http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
* - http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
*
* This function is not a cure-all, and reading those articles is important
* to understand its appropriate use in the context of ULPs, zeros, subnormals,
* infinities, and NANs. For example, it is preferable to use this function on
* two floats directly instead of subtracting them and comparing them to zero.
*
* The tolerance is specified in ULPs (units in the last place), i.e. how many
* floats there are in between the numbers. Therefore, the tolerance depends on
* the magnitude of the values that are being compared. A second tolerance is
* a maximum difference allowed, which is important when comparing numbers close to
* zero.
*
* A NAN compares as not equal to a number, but two NAN's may compare as equal
* to each other.
*
* \param x1 first floating value to compare
* \param x2 second floating values to compare
* \param maxUlps maximum units in the last place to be considered equal
* \param maxAbsoluteDifference maximum absolute difference to be considered equal
*/
template <typename T>
inline bool
FloatAlmostEqual( T x1, T x2,
typename Detail::FloatIEEE<T>::IntType maxUlps = 4,
typename Detail::FloatIEEE<T>::FloatType maxAbsoluteDifference = 0.1*NumericTraits<T>::epsilon() )
{
// Check if the numbers are really close -- needed
// when comparing numbers near zero.
const T absDifference = vcl_abs(x1 - x2);
if ( absDifference <= maxAbsoluteDifference )
{
return true;
}
typename Detail::FloatIEEE<T>::IntType
ulps = FloatDifferenceULP(x1, x2);
if(ulps < 0)
{
ulps = -ulps;
}
return ulps <= maxUlps;
}
} // end namespace Math
} // end namespace itk
#endif // end of itkMath.h
|