Changeset View
Changeset View
Standalone View
Standalone View
source/maths/MathUtil.h
/* Copyright (C) 2010 Wildfire Games. | /* Copyright (C) 2019 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
* GNU General Public License for more details. | * GNU General Public License for more details. | ||||
* | * | ||||
* You should have received a copy of the GNU General Public License | * You should have received a copy of the GNU General Public License | ||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
#ifndef INCLUDED_MATHUTIL | #ifndef INCLUDED_MATHUTIL | ||||
#define INCLUDED_MATHUTIL | #define INCLUDED_MATHUTIL | ||||
#define DEGTORAD(a) ((a) * ((float)M_PI/180.0f)) | #define DEGTORAD(a) ((a) * ((float)M_PI/180.0f)) | ||||
#define RADTODEG(a) ((a) * (180.0f/(float)M_PI)) | #define RADTODEG(a) ((a) * (180.0f/(float)M_PI)) | ||||
#define SQR(x) ((x) * (x)) | #define SQR(x) ((x) * (x)) | ||||
template <typename T> | template <typename T> | ||||
inline T Interpolate(const T& a, const T& b, float l) | inline T Interpolate(T a, T b, float t) | ||||
Itms: why did you remove const here? | |||||
Not Done Inline ActionsThe syntax looks like pass-by-copy so it may be redundant in most cases, but I suppose we could use pointers as T so I think it'd be worth keeping this const for the sake of clarity. wraitii: The syntax looks like pass-by-copy so it may be redundant in most cases, but I suppose we could… | |||||
{ | { | ||||
return a + (b - a) * l; | return a + (b - a) * t; | ||||
} | } | ||||
template <typename T> | template <typename T> | ||||
inline T clamp(T value, T min, T max) | inline T Clamp(T value, T min, T max) | ||||
Not Done Inline ActionsLikewise here. wraitii: Likewise here. | |||||
Done Inline ActionsI tried to make it more optimized, but it looks more complicated, than someone may expect: #include <type_traits> namespace internal { template <typename T> struct MathArgType { // We check a size of the type to be sure that it might fit in machine // register to pass it by value (currently not all compilers inline // functions with template arguments optimally). using type = typename std::conditional< std::is_arithmetic<T>::value && sizeof(T) <= sizeof(void*), T, const T&>::type; }; template <typename T, typename Arg> inline T Clamp(Arg value, Arg min, Arg max) { if (value <= min) return min; else if (value >= max) return max; return value; } } // namespace internal template <typename T> inline T Clamp(T&& value, T&& min, T&& max) { return internal::Clamp<typename std::decay<T>::type, typename internal::MathArgType<T>::type>(value, min, max); } vladislavbelov: I tried to make it more optimized, but it looks more complicated, than someone may expect… | |||||
{ | { | ||||
if (value <= min) return min; | ASSERT(min <= max); | ||||
else if (value >= max) return max; | if (value < min) | ||||
else return value; | return min; | ||||
else if (value > max) | |||||
return max; | |||||
return value; | |||||
} | } | ||||
Not Done Inline ActionsI heard we avoid else before return. elexis: I heard we avoid else before return.
(If there is code, people think it has a use, but these… | |||||
Not Done Inline Actions+1 to elexis' remark. Itms: +1 to elexis' remark. | |||||
Not Done Inline ActionsAdding +1 as well because why not. wraitii: Adding +1 as well because why not. | |||||
Done Inline ActionsIt's been fixed already ^^ Stan: It's been fixed already ^^ | |||||
inline float sgn(float a) | inline float sgn(float a) | ||||
{ | { | ||||
if (a > 0.0f) return 1.0f; | if (a > 0.0f) return 1.0f; | ||||
if (a < 0.0f) return -1.0f; | if (a < 0.0f) return -1.0f; | ||||
return 0.0f; | return 0.0f; | ||||
} | } | ||||
#endif | #endif |
Wildfire Games · Phabricator
why did you remove const here?