OTB  9.0.0
Orfeo Toolbox
otbMath.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 
22 #ifndef otbMath_h
23 #define otbMath_h
24 
31 #include <cmath>
32 
33 namespace otb
34 {
35 
36 // For Windows MSVC, defines these macros.
37 #ifdef _USE_MATH_DEFINES
38 #define _MATH_DEFINES_DEFINED
39 #endif
40 
43 /* Some useful constants. */
44 constexpr double CONST_E = 2.7182818284590452354; /* e */
45 constexpr double CONST_LOG2E = 1.4426950408889634074; /* log_2 e */
46 constexpr double CONST_LOG10E = 0.43429448190325182765; /* log_10 e */
47 constexpr double CONST_LN2 = 0.69314718055994530942; /* log_e 2 */
48 constexpr double CONST_LN10 = 2.30258509299404568402; /* log_e 10 */
49 constexpr double CONST_PI = 3.14159265358979323846; /* pi */
50 constexpr double CONST_PI_2 = CONST_PI / 2.0; /* pi/2 */
51 constexpr double CONST_PI_4 = CONST_PI / 4.0; /* pi/4 */
52 constexpr double CONST_PI_8 = CONST_PI / 8.0; /* pi/8 */
53 constexpr double CONST_1_PI = 1.0 / CONST_PI; /* 1/pi */
54 constexpr double CONST_2_PI = 2.0 / CONST_PI; /* 2/pi */
55 constexpr double CONST_2PI = 2.0 * CONST_PI; /* 2*pi */
56 constexpr double CONST_PI_180 = CONST_PI / 180.0; /* pi/180 */
57 constexpr double CONST_180_PI = 180.0 / CONST_PI; /* 180/pi */
58 const double CONST_LNPI = std::log(CONST_PI); /* ln(pi)*/
59 const double CONST_SQRTPI = std::sqrt(CONST_PI); /* sqrt(pi)*/
60 constexpr double CONST_2_SQRTPI = 1.12837916709551257390; /* 2/sqrt(pi) */
61 constexpr double CONST_SQRT2 = 1.41421356237309504880; /* sqrt(2) */
62 constexpr double CONST_SQRT1_2 = 0.70710678118654752440; /* 1/sqrt(2) */
63 constexpr double CONST_SQRT3 = 1.73205080756887729353; /* sqrt(3) */
64 constexpr double CONST_EULER = 0.57721566490153286060; /* euler constant*/
65 
66 /* The above constants are not adequate for computation using `long double's.
67  Therefore we provide as an extension constants with similar names as a
68  GNU extension. Provide enough digits for the 128-bit IEEE quad. */
69 #ifdef __USE_GNU
70 
71 constexpr long double CONST_El = 2.7182818284590452353602874713526625L; /* e */
72 constexpr long double CONST_LOG2El = 1.4426950408889634073599246810018921L; /* log_2 e */
73 constexpr long double CONST_LOG10El = 0.4342944819032518276511289189166051L; /* log_10 e */
74 constexpr long double CONST_LN2l = 0.6931471805599453094172321214581766L; /* log_e 2 */
75 constexpr long double CONST_LN10l = 2.3025850929940456840179914546843642L; /* log_e 10 */
76 constexpr long double CONST_PIl = 3.1415926535897932384626433832795029L; /* pi */
77 constexpr long double CONST_PI_2l = CONST_PI / 2.0L; /* pi/2 */
78 constexpr long double CONST_PI_4l = CONST_PI / 4.0L; /* pi/4 */
79 constexpr long double CONST_PI_8l = CONST_PI / 8.0L; /* pi/8 */
80 constexpr long double CONST_2_PIl = 2.0L / CONST_PI; /* 2/pi */
81 constexpr long double CONST_2_SQRTPIl = 1.1283791670955125738961589031215452L; /* 2/sqrt(pi) */
82 constexpr long double CONST_2PIl = 2.0L * CONST_PI; /* 2*pi */
83 constexpr long double CONST_PI_180l = CONST_PI / 180.0L; /* pi/180 */
84 constexpr long double CONST_180_PIl = 180.0L / CONST_PI; /* 180/pi */
85 // Defined but not used?
86 // const long double CONST_LNPIl = std::log(CONST_PIl); /* ln(pi)*/
87 // const long double CONST_SQRTPIl = std::sqrt(CONST_PIl); /* sqrt(pi)*/
88 constexpr long double CONST_SQRT2l = 1.4142135623730950488016887242096981L; /* sqrt(2) */
89 constexpr long double CONST_SQRT1_2l = 0.7071067811865475244008443621048490L; /* 1/sqrt(2) */
90 constexpr long double CONST_SQRT3l = 1.7320508075688772935274463415058724L; /* sqrt(3) */
91 constexpr long double CONST_EULERl = 0.5772156649015328606065120900824024L; /* euler constant*/
92 
93 #endif
94 
95 template <typename T>
96 T InverseValue(const T& value)
97 {
98  return ((value > 0 || value < 0) ? 1 / value : 0);
99 }
100 
101 template <typename T>
102 T SignOfValue(const T& value)
103 {
104  return (value > 0) ? 1 : ((value < 0) ? -1 : 0);
105 }
106 }
107 
108 #endif
otb::CONST_PI
constexpr double CONST_PI
Definition: otbMath.h:49
otb::CONST_SQRTPI
const double CONST_SQRTPI
Definition: otbMath.h:59
otb::CONST_SQRT2
constexpr double CONST_SQRT2
Definition: otbMath.h:61
otb::CONST_SQRT1_2
constexpr double CONST_SQRT1_2
Definition: otbMath.h:62
otb::CONST_LOG2E
constexpr double CONST_LOG2E
Definition: otbMath.h:45
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::CONST_PI_4
constexpr double CONST_PI_4
Definition: otbMath.h:51
otb::CONST_1_PI
constexpr double CONST_1_PI
Definition: otbMath.h:53
otb::InverseValue
T InverseValue(const T &value)
Definition: otbMath.h:96
otb::CONST_2_PI
constexpr double CONST_2_PI
Definition: otbMath.h:54
otb::CONST_PI_180
constexpr double CONST_PI_180
Definition: otbMath.h:56
otb::CONST_2PI
constexpr double CONST_2PI
Definition: otbMath.h:55
otb::CONST_PI_8
constexpr double CONST_PI_8
Definition: otbMath.h:52
otb::CONST_2_SQRTPI
constexpr double CONST_2_SQRTPI
Definition: otbMath.h:60
otb::CONST_PI_2
constexpr double CONST_PI_2
Definition: otbMath.h:50
otb::CONST_EULER
constexpr double CONST_EULER
Definition: otbMath.h:64
otb::CONST_SQRT3
constexpr double CONST_SQRT3
Definition: otbMath.h:63
otb::CONST_LN10
constexpr double CONST_LN10
Definition: otbMath.h:48
otb::CONST_E
constexpr double CONST_E
Definition: otbMath.h:44
otb::CONST_LOG10E
constexpr double CONST_LOG10E
Definition: otbMath.h:46
otb::CONST_180_PI
constexpr double CONST_180_PI
Definition: otbMath.h:57
otb::CONST_LN2
constexpr double CONST_LN2
Definition: otbMath.h:47
otb::CONST_LNPI
const double CONST_LNPI
Definition: otbMath.h:58
otb::SignOfValue
T SignOfValue(const T &value)
Definition: otbMath.h:102