-
Notifications
You must be signed in to change notification settings - Fork 16
/
MathHelper.h
100 lines (80 loc) · 2.33 KB
/
MathHelper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//***************************************************************************************
// MathHelper.h by Frank Luna (C) 2011 All Rights Reserved.
//
// Helper math class.
//***************************************************************************************
#pragma once
#include <Windows.h>
#include <DirectXMath.h>
#include <cstdint>
class MathHelper
{
public:
// Returns random float in [0, 1).
static float RandF()
{
return (float)(rand()) / (float)RAND_MAX;
}
// Returns random float in [a, b).
static float RandF(float a, float b)
{
return a + RandF()*(b-a);
}
static int Rand(int a, int b)
{
return a + rand() % ((b - a) + 1);
}
template<typename T>
static T Min(const T& a, const T& b)
{
return a < b ? a : b;
}
template<typename T>
static T Max(const T& a, const T& b)
{
return a > b ? a : b;
}
template<typename T>
static T Lerp(const T& a, const T& b, float t)
{
return a + (b-a)*t;
}
template<typename T>
static T Clamp(const T& x, const T& low, const T& high)
{
return x < low ? low : (x > high ? high : x);
}
// Returns the polar angle of the point (x,y) in [0, 2*PI).
static float AngleFromXY(float x, float y);
static DirectX::XMVECTOR SphericalToCartesian(float radius, float theta, float phi)
{
return DirectX::XMVectorSet(
radius*sinf(phi)*cosf(theta),
radius*cosf(phi),
radius*sinf(phi)*sinf(theta),
1.0f);
}
static DirectX::XMMATRIX InverseTranspose(DirectX::CXMMATRIX M)
{
// Inverse-transpose is just applied to normals. So zero out
// translation row so that it doesn't get into our inverse-transpose
// calculation--we don't want the inverse-transpose of the translation.
DirectX::XMMATRIX A = M;
A.r[3] = DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f);
DirectX::XMVECTOR det = DirectX::XMMatrixDeterminant(A);
return DirectX::XMMatrixTranspose(DirectX::XMMatrixInverse(&det, A));
}
static DirectX::XMFLOAT4X4 Identity4x4()
{
static DirectX::XMFLOAT4X4 I(
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
return I;
}
static DirectX::XMVECTOR RandUnitVec3();
static DirectX::XMVECTOR RandHemisphereUnitVec3(DirectX::XMVECTOR n);
static const float Infinity;
static const float Pi;
};