summaryrefslogtreecommitdiffstats
path: root/src/math/func.h
blob: 78bfeb429112f2b400d168e19ae78e5d24bb749f (plain)
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
/*
 * This file is part of the Colobot: Gold Edition source code
 * Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
 * http://epsiteс.ch; http://colobot.info; http://github.com/colobot
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://gnu.org/licenses
 */

/**
 * \file math/func.h
 * \brief Common math functions
 */

#pragma once


#include "math/const.h"


#include <cmath>
#include <cstdlib>


// Math module namespace
namespace Math {


//! Compares \a a and \a b within \a tolerance
inline bool IsEqual(float a, float b, float tolerance = Math::TOLERANCE)
{
    return fabs(a - b) < tolerance;
}

//! Compares \a a to zero within \a tolerance
inline bool IsZero(float a, float tolerance = Math::TOLERANCE)
{
    return Math::IsEqual(a, 0.0f, tolerance);
}

//! Minimum
inline float Min(float a, float b)
{
    if ( a <= b ) return a;
    else          return b;
}

inline float Min(float a, float b, float c)
{
    return Min( Min(a, b), c );
}

inline float Min(float a, float b, float c, float d)
{
    return Math::Min( Math::Min(a, b), Math::Min(c, d) );
}

inline float Min(float a, float b, float c, float d, float e)
{
    return Math::Min( Math::Min(a, b), Math::Min(c, d), e );
}

//! Maximum
inline float Max(float a, float b)
{
    if ( a >= b ) return a;
    else          return b;
}

inline float Max(float a, float b, float c)
{
    return Math::Max( Math::Max(a, b), c );
}

inline float Max(float a, float b, float c, float d)
{
    return Math::Max( Math::Max(a, b), Math::Max(c, d) );
}

inline float Max(float a, float b, float c, float d, float e)
{
    return Math::Max( Math::Max(a, b), Math::Max(c, d), e );
}

//! Returns the normalized value (0 .. 1)
inline float Norm(float a)
{
    if ( a < 0.0f ) return 0.0f;
    if ( a > 1.0f ) return 1.0f;
    return a;
}

//! Swaps two integers
inline void Swap(int &a, int &b)
{
    int c = a;
    a = b;
    b = c;
}

//! Swaps two real numbers
inline void Swap(float &a, float &b)
{
    float c = a;
    a = b;
    b = c;
}

//! Returns the modulo of a floating point number
/** Mod(8.1, 4) = 0.1
        Mod(n, 1) = fractional part of n */
inline float Mod(float a, float m)
{
    return a - ( static_cast<int>(a / m) ) * m;
}

//! Returns a random value between 0 and 1.
inline float Rand()
{
    return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
}

//! Returns whether \a x is an even power of 2
inline bool IsPowerOfTwo(unsigned int x)
{
   return x && !(x & (x - 1));
}

//! Returns the next nearest power of two to \a x
inline int NextPowerOfTwo(int x)
{
  double logbase2 = log(static_cast<float>(x)) / Math::LOG_2;
  return static_cast<int>(pow(2, ceil(logbase2)) + 0.5);
}


//! Returns a normalized angle, that is in other words between 0 and 2 * PI
inline float NormAngle(float angle)
{
    angle = Math::Mod(angle, PI*2.0f);
    if ( angle < 0.0f )
        return PI*2.0f + angle;

    return angle;
}

//! Test if a angle is between two terminals
inline bool TestAngle(float angle, float min, float max)
{
    angle = Math::NormAngle(angle);
    min   = Math::NormAngle(min);
    max   = Math::NormAngle(max);

    if ( min > max )
        return ( angle <= max || angle >= min );

    return ( angle >= min && angle <= max );
}

//! Calculates a value (radians) proportional between a and b (degrees)
inline float PropAngle(int a, int b, float p)
{
    float aa = static_cast<float>(a) * DEG_TO_RAD;
    float bb = static_cast<float>(b) * DEG_TO_RAD;

    return aa + p * (bb - aa);
}

//! Calculates the angle to rotate the angle \a a to the angle \a g
/** A positive angle is counterclockwise (CCW). */
inline float Direction(float a, float g)
{
    a = Math::NormAngle(a);
    g = Math::NormAngle(g);

    if ( a < g )
    {
        if ( a+PI*2.0f-g < g-a ) a += PI*2.0f;
    }
    else
    {
        if ( g+PI*2.0f-a < a-g ) g += PI*2.0f;
    }

    return g-a;
}

//! Managing the dead zone of a joystick.
/**
\verbatim
in:   -1            0            1
--|-------|----o----|-------|-->
             <---->
              dead
out:  -1       0         0       1
\endverbatim */
inline float Neutral(float value, float dead)
{
    if ( fabs(value) <= dead )
    {
        return 0.0f;
    }
    else
    {
        if ( value > 0.0f ) return (value-dead)/(1.0f-dead);
        else                return (value+dead)/(1.0f-dead);
    }
}

//! Gently advances a desired value from its current value
/** Over time, the progression is more rapid. */
inline float Smooth(float actual, float hope, float time)
{
    float future = actual + (hope-actual)*time;

    if ( hope > actual )
    {
        if ( future > hope ) future = hope;
    }
    if ( hope < actual )
    {
        if ( future < hope ) future = hope;
    }

    return future;
}

//! Bounces any movement
/**
\verbatim
out
 |
1+------o-------o---
 |    o | o   o |  | bounce
 |   o  |   o---|---
 |  o   |       |
 | o    |       |
-o------|-------+----> progress
0|      |       1
 |<---->|middle
\endverbatim */
inline float Bounce(float progress, float middle = 0.3f, float bounce = 0.4f)
{
    if ( progress < middle )
    {
        progress = progress/middle;    // 0..1
        return 0.5f+sinf(progress*PI-PI/2.0f)/2.0f;
    }
    else
    {
        progress = (progress-middle)/(1.0f-middle);    // 0..1
        return (1.0f-bounce/2.0f)+sinf((0.5f+progress*2.0f)*PI)*(bounce/2.0f);
    }
}


} // namespace Math