forked from NIVANorge/Mobius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobius_math.h
72 lines (55 loc) · 1.78 KB
/
mobius_math.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
#if !defined(MOBIUS_MATH_H)
#define Min(A, B) ((A) < (B) ? (A) : (B))
#define Max(A, B) ((A) > (B) ? (A) : (B))
#define Square(A) ((A)*(A))
#define Pi 3.141592653589793238462643383279502884
inline double
LinearInterpolate(double X, double MinX, double MaxX, double MinY, double MaxY)
{
double XX = (X - MinX) / (MaxX - MinX);
return MinY + (MaxY - MinY)*XX;
}
inline double
LinearResponse(double X, double MinX, double MaxX, double MinY, double MaxY)
{
if(X <= MinX) return MinY;
if(X >= MaxX) return MaxY;
double XX = (X - MinX) / (MaxX - MinX);
return MinY + (MaxY - MinY)*XX;
}
inline double
SCurveResponse(double X, double MinX, double MaxX, double MinY, double MaxY)
{
if(X <= MinX) return MinY;
if(X >= MaxX) return MaxY;
double XX = (X - MinX) / (MaxX - MinX);
double T = (3.0 - 2.0*XX)*XX*XX;
return MinY + (MaxY - MinY)*T;
}
inline double
InverseGammaResponse(double X, double MinX, double MaxX, double MinY, double MaxY)
{
const double GammaMinX = 1.461632144968362341262;
const double GammaMin = 0.885603194410888;
if(X <= MinX) return MinY;
double XX = (X - MinX) / (MaxX - MinX);
double T = GammaMin / tgamma(XX * GammaMinX);
return MinY + (MaxY - MinY) * T;
}
inline double
SafeDivide(double A, double B)
{
//NOTE: This function is mostly meant for computing concentration = SafeDivide(mass, watervolume). If the watervolume is 0, it is safe to treat the concentration as 0 as there would be no flow taking the concentration to another compartment anyway. Moreover, all chemical processes involving concentrations are usually turned off if the watervolume is 0.
double Result = A / B;
if(std::isfinite(Result)) return Result;
return 0.0;
}
inline double
Clamp01(double A)
{
if(A < 0.0) return 0.0;
if(A > 1.0) return 1.0;
return A;
}
#define MOBIUS_MATH_H
#endif