BRE12
MathUtils.h
1 #pragma once
2 
3 #include <Windows.h>
4 #include <DirectXMath.h>
5 #include <cstdint>
6 
7 #include <Utils\DebugUtils.h>
8 
9 namespace BRE {
10 class MathUtils {
11 public:
12  MathUtils() = delete;
13  ~MathUtils() = delete;
14  MathUtils(const MathUtils&) = delete;
15  const MathUtils& operator=(const MathUtils&) = delete;
16  MathUtils(MathUtils&&) = delete;
17  MathUtils& operator=(MathUtils&&) = delete;
18 
19  static float RandomFloatInInverval(const float bottomValue,
20  const float topValue) noexcept
21  {
22  BRE_ASSERT(bottomValue < topValue);
23  const float randomBetweenZeroAndOne = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
24  return bottomValue + randomBetweenZeroAndOne * (topValue - bottomValue);
25  }
26 
27  static int RandomIntegerInInterval(const int32_t a,
28  const int32_t b) noexcept
29  {
30  return a + rand() % ((b - a) + 1);
31  }
32 
33  template<typename T>
34  static T Min(const T& a,
35  const T& b) noexcept
36  {
37  return a < b ? a : b;
38  }
39 
40  template<typename T>
41  static T Max(const T& a,
42  const T& b) noexcept
43  {
44  return a > b ? a : b;
45  }
46 
47  template<typename T>
48  static T Lerp(const T& a,
49  const T& b,
50  const float t) noexcept
51  {
52  return a + (b - a) * t;
53  }
54 
55  template<typename T>
56  static T Clamp(const T& x,
57  const T& low,
58  const T& high) noexcept
59  {
60  return x < low ? low : (x > high ? high : x);
61  }
62 
63  static DirectX::XMMATRIX GetTransposeMatrix(const DirectX::XMFLOAT4X4& matrix) noexcept
64  {
65  const DirectX::XMMATRIX xmMatrix = XMLoadFloat4x4(&matrix);
66  return DirectX::XMMatrixTranspose(xmMatrix);
67  }
68 
69  static void StoreTransposeMatrix(const DirectX::XMFLOAT4X4& sourceMatrix,
70  DirectX::XMFLOAT4X4& destinationMatrix) noexcept
71  {
72  const DirectX::XMMATRIX xmMatrix = GetTransposeMatrix(sourceMatrix);
73  DirectX::XMStoreFloat4x4(&destinationMatrix, xmMatrix);
74  }
75 
76  static DirectX::XMMATRIX GetInverseMatrix(const DirectX::XMFLOAT4X4& matrix) noexcept
77  {
78  const DirectX::XMMATRIX xmMatrix = XMLoadFloat4x4(&matrix);
79  return DirectX::XMMatrixInverse(nullptr, xmMatrix);
80  }
81 
82  static void StoreInverseMatrix(const DirectX::XMFLOAT4X4& sourceMatrix,
83  DirectX::XMFLOAT4X4& destinationMatrix) noexcept
84  {
85  const DirectX::XMMATRIX xmMatrix = GetInverseMatrix(sourceMatrix);
86  DirectX::XMStoreFloat4x4(&destinationMatrix, xmMatrix);
87  }
88 
89  static DirectX::XMMATRIX GetInverseTransposeMatrix(const DirectX::XMFLOAT4X4& matrix) noexcept
90  {
91  const DirectX::XMMATRIX xmInverseMatrix = GetInverseMatrix(matrix);
92  DirectX::XMFLOAT4X4 inverseMatrix;
93  DirectX::XMStoreFloat4x4(&inverseMatrix, xmInverseMatrix);
94  return GetTransposeMatrix(inverseMatrix);
95  }
96 
97  static void StoreInverseTransposeMatrix(const DirectX::XMFLOAT4X4& sourceMatrix,
98  DirectX::XMFLOAT4X4& destinationMatrix) noexcept
99  {
100  const DirectX::XMMATRIX xmMatrix = GetInverseTransposeMatrix(sourceMatrix);
101  DirectX::XMStoreFloat4x4(&destinationMatrix, xmMatrix);
102  }
103 
104  static void ComputeMatrix(DirectX::XMFLOAT4X4& m,
105  const float tx,
106  const float ty,
107  const float tz,
108  const float sx = 1.0f,
109  const float sy = 1.0f,
110  const float sz = 1.0f,
111  const float rx = 0.0f,
112  const float ry = 0.0f,
113  const float rz = 0.0f) noexcept;
114 
115  static DirectX::XMFLOAT4X4 GetIdentity4x4Matrix() noexcept
116  {
117  DirectX::XMFLOAT4X4 identityMatrix(
118  1.0f, 0.0f, 0.0f, 0.0f,
119  0.0f, 1.0f, 0.0f, 0.0f,
120  0.0f, 0.0f, 1.0f, 0.0f,
121  0.0f, 0.0f, 0.0f, 1.0f);
122 
123  return identityMatrix;
124  }
125 
126  static const float Infinity;
127  static const float Pi;
128 };
129 
130 
131 }
132 
Definition: Camera.cpp:8
Definition: MathUtils.h:10