1 // =============================================================================
2 // PROJECT CHRONO - http://projectchrono.org
3 //
4 // Copyright (c) 2014 projectchrono.org
5 // All rights reserved.
6 //
7 // Use of this source code is governed by a BSD-style license that can be found
8 // in the LICENSE file at the top level of the distribution and at
9 // http://projectchrono.org/license-chrono.txt.
10 //
11 // =============================================================================
12
13 #ifndef CHMATHEMATICS_H
14 #define CHMATHEMATICS_H
15
16 #include <cmath>
17 #include <cfloat>
18 #include <cassert>
19
20 #include "chrono/core/ChApiCE.h"
21
22 namespace chrono {
23 // CONSTANTS
24
25 static const double CH_C_PI = 3.141592653589793238462643383279;
26 static const double CH_C_PI_2 = 1.570796326794896619231321691639;
27 static const double CH_C_PI_4 = 0.785398163397448309615660845819;
28 static const double CH_C_1_PI = 0.318309886183790671537767526745;
29 static const double CH_C_2PI = 6.283185307179586476925286766559;
30 static const double CH_C_RAD_TO_DEG = 180.0 / 3.1415926535897932384626433832795;
31 static const double CH_C_DEG_TO_RAD = 3.1415926535897932384626433832795 / 180.0;
32
33 static const double CH_C_SQRT_2 = 1.41421356237309504880;
34 static const double CH_C_SQRT_1_2 = 0.70710678118654752440;
35
36 static const double CH_C_E = 2.71828182845904523536;
37 static const double CH_C_LOG2E = 1.44269504088896340736;
38 static const double CH_C_LOG10E = 0.43429448190325182765;
39 static const double CH_C_LN2 = 0.69314718055994530941;
40 static const double CH_C_LN10 = 2.30258509299404568402;
41
42 static const double BDF_STEP_HIGH = 1e-4;
43 static const double BDF_STEP_LOW = 1e-7;
44
45 // ANGLE CONVERSIONS
46
47 /// Computes the atan2, returning angle given cosine and sine.
48 ChApi double ChAtan2(double mcos, double msin);
49
50 // OTHER
51
52 /// Returns random value in (0..1) interval with Park-Miller method
53 ChApi double ChRandom();
54
55 /// Sets the seed of the ChRandom function (Park-Miller method)
56 ChApi void ChSetRandomSeed(long newseed);
57
58 /// Computes a 1D harmonic multi-octave noise
59 ChApi double ChNoise(double x, double amp, double freq, int octaves, double amp_ratio);
60
61 /// Maximum between two values
ChMax(int a,int b)62 inline int ChMax(int a, int b) {
63 if (a > b)
64 return a;
65 return b;
66 }
67 /// Maximum between two values
ChMax(double a,double b)68 inline double ChMax(double a, double b) {
69 if (a > b)
70 return a;
71 return b;
72 }
73 /// Minimum between two values
ChMin(int a,int b)74 inline int ChMin(int a, int b) {
75 if (a < b)
76 return a;
77 return b;
78 }
79 /// Minimum between two values
ChMin(double a,double b)80 inline double ChMin(double a, double b) {
81 if (a < b)
82 return a;
83 return b;
84 }
85
86 /// Clamp and modify the specified value to lie within the given limits.
87 template <typename T>
ChClampValue(T & value,T limitMin,T limitMax)88 void ChClampValue(T& value, T limitMin, T limitMax) {
89 if (value < limitMin)
90 value = limitMin;
91 else if (value > limitMax)
92 value = limitMax;
93 }
94
95 /// Clamp the specified value to lie within the given limits.
96 template <typename T>
ChClamp(T value,T limitMin,T limitMax)97 T ChClamp(T value, T limitMin, T limitMax) {
98 if (value < limitMin)
99 return limitMin;
100 if (value > limitMax)
101 return limitMax;
102
103 return value;
104 }
105
106 /// Signum function.
107 template <typename T>
ChSignum(T x)108 int ChSignum(T x) {
109 return (x > T(0)) - (x < T(0));
110 }
111
112 /// Smooth (sinusoidal) ramp between y1 and y2.
113 /// Note: must have x1 < x2 (no check).
114 ChApi double ChSineStep(double x, double x1, double y1, double x2, double y2);
115
116 /// Parameter make periodic in 0..1
117 /// (using 0..1 modulus if closed, otherwise clamping in 0..1 range)
118 ChApi void ChPeriodicPar(double& u, int closed);
119
120 } // end namespace chrono
121
122 #endif
123