Mathter
A configurable 3D math library for game developers.
Utility.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <type_traits>
4 #include <algorithm>
5 
6 namespace mathter {
7 
8 template <class T, int Dim, bool Packed>
9 class Vector;
10 
11 //------------------------------------------------------------------------------
12 // Public utility stuff
13 //------------------------------------------------------------------------------
14 
15 
16 // Common mathematical constants
17 
19 template <class Scalar>
20 class Constants {
21 public:
22  static constexpr Scalar Pi = (Scalar)3.1415926535897932384626433832795028841971693993751;
23  static constexpr Scalar PiHalf = (Scalar)1.5707963267948966192313216916397514420985846996876;
24  static constexpr Scalar PiFourth = (Scalar)0.78539816339744830961566084581987572104929234984378;
25  static constexpr Scalar E = (Scalar)2.7182818284590452353602874713526624977572470937;
26  static constexpr Scalar Sqrt2 = (Scalar)1.4142135623730950488016887242096980785696718753769;
27  static constexpr Scalar SqrtHalf = (Scalar)0.70710678118654752440084436210484903928483593768847;
28 };
29 
30 
31 // Radians and degrees
32 
34 template <class Scalar>
35 auto Rad2Deg(Scalar rad) {
36  using ComputeT = typename std::conditional<std::is_floating_point<Scalar>::value, Scalar, long double>::type;
37  return rad / Constants<ComputeT>::Pi * ComputeT(180);
38 }
39 
41 template <class Scalar>
42 auto Deg2Rad(Scalar deg) {
43  using ComputeT = typename std::conditional<std::is_floating_point<Scalar>::value, Scalar, long double>::type;
44  return deg / ComputeT(180) * Constants<ComputeT>::Pi;
45 }
46 
47 
48 // Clamp and saturate
49 
51 template <class Scalar>
52 Scalar Clamp(Scalar arg, Scalar lower, Scalar upper) {
53  return std::max(lower, std::min(upper, arg));
54 }
55 
57 template <class T, int Dim, bool Packed>
58 Vector<T, Dim, Packed> Clamp(const Vector<T, Dim, Packed>& arg, T lower, T upper);
59 
61 template <class Scalar>
62 Scalar Saturate(Scalar arg) {
63  return Clamp(arg, Scalar(0), Scalar(1));
64 }
65 
67 template <class T, int Dim, bool Packed>
69 
70 
71 } // namespace mathter
72 
73 
74 #include "Vector.hpp"
75 
76 // Implementations of vector clamp functions.
77 namespace mathter {
78 
79 template <class T, int Dim, bool Packed>
80 Vector<T, Dim, Packed> Clamp(const Vector<T, Dim, Packed>& arg, T lower, T upper) {
82  for (int i = 0; i < arg.Dimension(); ++i) {
83  ret(i) = Clamp(arg(i), lower, upper);
84  }
85  return ret;
86 }
87 
88 template <class T, int Dim, bool Packed>
90  return Clamp(arg, T(0), T(1));
91 }
92 
93 } // namespace mathter
static constexpr Scalar Pi
Definition: Utility.hpp:22
auto Rad2Deg(Scalar rad)
Converts radians to degrees.
Definition: Utility.hpp:35
Accurate mathematical constants.
Definition: Utility.hpp:20
Represents a vector in N-dimensional space.
Definition: Definitions.hpp:57
static constexpr Scalar E
Definition: Utility.hpp:25
Definition: Approx.hpp:11
static constexpr Scalar Sqrt2
Definition: Utility.hpp:26
Scalar Clamp(Scalar arg, Scalar lower, Scalar upper)
Limits arg to the range [lower, upper], making it either lower or upper if out of range...
Definition: Utility.hpp:52
static constexpr Scalar SqrtHalf
Definition: Utility.hpp:27
static constexpr Scalar PiHalf
Definition: Utility.hpp:23
static constexpr Scalar PiFourth
Definition: Utility.hpp:24
auto Deg2Rad(Scalar deg)
Converts degrees to radians.
Definition: Utility.hpp:42
constexpr int Dimension() const
Returns the number of dimensions of the vector.
Definition: VectorImpl.hpp:402
Scalar Saturate(Scalar arg)
Clamps argument into range [0,1].
Definition: Utility.hpp:62