source: ps/trunk/source/maths/MathUtil.h@ 25266

Last change on this file since 25266 was 25266, checked in by wraitii, 3 years ago

Use type_identity to simplify Clamp usage.

Differential Revision: https://code.wildfiregames.com/D3859

  • Property svn:eol-style set to native
File size: 1.5 KB
Line 
1/* Copyright (C) 2021 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_MATHUTIL
19#define INCLUDED_MATHUTIL
20
21#define DEGTORAD(a) ((a) * ((float)M_PI/180.0f))
22#define RADTODEG(a) ((a) * (180.0f/(float)M_PI))
23#define SQR(x) ((x) * (x))
24
25template <typename T>
26inline T Interpolate(const T& a, const T& b, float t)
27{
28 return a + (b - a) * t;
29}
30
31// TODO C++20: use the proper one.
32template<typename T>
33struct type_identity
34{
35 using type = T;
36};
37template<typename T>
38using type_identity_t = typename type_identity<T>::type;
39
40template <typename T>
41inline T Clamp(T value, type_identity_t<T> min, type_identity_t<T> max)
42{
43 if (value <= min)
44 return min;
45 else if (value >= max)
46 return max;
47 return value;
48}
49
50inline float sgn(float a)
51{
52 if (a > 0.0f)
53 return 1.0f;
54 if (a < 0.0f)
55 return -1.0f;
56 return 0.0f;
57}
58
59#endif // INCLUDED_MATHUTIL
Note: See TracBrowser for help on using the repository browser.