olp-cpp-sdk  1.22.0
Math.h
1 /*
2  * Copyright (C) 2019-2021 HERE Europe B.V.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  * License-Filename: LICENSE
18  */
19 
20 #pragma once
21 
22 #include <algorithm>
23 #include <cmath>
24 #include <functional>
25 #include <limits>
26 
27 namespace olp {
28 namespace math {
29 using std::isnan;
30 using std::max;
31 using std::min;
32 using std::pow;
33 
34 constexpr double one_over_two_pi = 0.159154943091895335768883763372514362;
35 constexpr double half_pi = 1.57079632679489661923132169163975144;
36 constexpr double pi = 3.14159265358979323846264338327950288;
37 constexpr double two_pi = 6.28318530717958647692528676655900576;
38 constexpr double epsilon = std::numeric_limits<double>::epsilon();
39 
40 CORE_API inline double Degrees(double radians) {
41  return radians * 57.295779513082320876798154814105;
42 }
43 
44 CORE_API inline double Radians(double degrees) {
45  return degrees * 0.01745329251994329576923690768489;
46 }
47 
48 CORE_API inline bool EpsilonEqual(double const& x, double const& y) {
49  return std::abs(x - y) < epsilon;
50 }
51 
52 CORE_API inline double Clamp(double const& x, double const& minVal,
53  double const& maxVal) {
54  return min(max(x, minVal), maxVal);
55 }
56 
58 CORE_API inline double Wrap(double value, double lower, double upper) {
59  // Returns the lower bound if the range is singular.
60  if (EpsilonEqual(lower, upper)) {
61  return lower;
62  }
63 
64  // Wraps around the range or returns the exact unmodified value.
65  if (value < lower) {
66  return upper - std::fmod((lower - value), (upper - lower));
67  }
68 
69  if (upper <= value) {
70  return lower + std::fmod((value - lower), (upper - lower));
71  }
72  return value;
73 }
74 
75 } // namespace math
76 } // namespace olp
Rules all the other namespaces.
Definition: AppleSignInProperties.h:24