Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
MathUtil.h
Go to the documentation of this file.
1/* Copyright (C) 2026 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#include <numbers>
22
23#define DEGTORAD(a) ((a) * (std::numbers::pi_v<float> / 180.0f))
24#define RADTODEG(a) ((a) * (180.0f / std::numbers::pi_v<float>))
25#define SQR(x) ((x) * (x))
26
27template<typename T>
28inline T Interpolate(const T& a, const T& b, float t)
29{
30 return a + (b - a) * t;
31}
32
33template<typename T>
34inline T Clamp(T value, T min, T max)
35{
36 if (value <= min)
37 return min;
38 else if (value >= max)
39 return max;
40 return value;
41}
42
43template<typename T>
44inline T SmoothStep(T edge0, T edge1, T value)
45{
46 value = Clamp<T>((value - edge0) / (edge1 - edge0), 0, 1);
47 return value * value * (3 - 2 * value);
48}
49
50template<typename T>
51inline T Sign(const T value)
52{
53 if (value > T(0))
54 return T(1);
55 if (value < T(0))
56 return T(-1);
57 return T(0);
58}
59
60#endif // INCLUDED_MATHUTIL
T Sign(const T value)
Definition: MathUtil.h:51
T Interpolate(const T &a, const T &b, float t)
Definition: MathUtil.h:28
T Clamp(T value, T min, T max)
Definition: MathUtil.h:34
T SmoothStep(T edge0, T edge1, T value)
Definition: MathUtil.h:44
#define T(string_literal)
Definition: secure_crt.cpp:81