forked from TomohikoMukai/ssdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRigidTransform.h
133 lines (125 loc) · 3.67 KB
/
RigidTransform.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifndef RIGID_TRANSFORM_H
#define RIGID_TRANSFORM_H
#pragma once
#include <DirectXMath.h>
class RigidTransform
{
public:
DirectX::XMFLOAT4A& Rotation()
{
return rotation;
}
const DirectX::XMFLOAT4A& Rotation() const
{
return rotation;
}
DirectX::XMFLOAT3A& Translation()
{
return translation;
}
const DirectX::XMFLOAT3A& Translation() const
{
return translation;
}
public:
RigidTransform()
: rotation(0, 0, 0, 1.0f),
translation(0, 0, 0)
{
}
RigidTransform(const RigidTransform& src)
{
rotation = src.rotation;
translation = src.translation;
}
RigidTransform(const DirectX::XMFLOAT4A& r, const DirectX::XMFLOAT3A& t)
{
rotation = r;
translation = t;
}
explicit RigidTransform(const DirectX::XMFLOAT4X4A& m)
{
translation = DirectX::XMFLOAT3A(m._41, m._42, m._43);
DirectX::XMMATRIX xm = DirectX::XMLoadFloat4x4A(&m);
DirectX::XMVECTOR rv = DirectX::XMQuaternionRotationMatrix(xm);
if (DirectX::XMVectorGetW(rv) < 0)
{
rv = DirectX::XMVectorNegate(rv);
}
DirectX::XMStoreFloat4A(&rotation, rv);
}
~RigidTransform()
{
}
public:
RigidTransform& operator =(const RigidTransform& src)
{
rotation = src.rotation;
translation = src.translation;
return *this;
}
RigidTransform& operator =(const DirectX::XMFLOAT4X4A& m)
{
translation = DirectX::XMFLOAT3A(m._41, m._42, m._43);
DirectX::XMMATRIX xm = DirectX::XMLoadFloat4x4A(&m);
DirectX::XMVECTOR rv = DirectX::XMQuaternionRotationMatrix(xm);
if (DirectX::XMVectorGetW(rv) < 0)
{
rv = DirectX::XMVectorNegate(rv);
}
DirectX::XMStoreFloat4A(&rotation, rv);
return *this;
}
void Set(const DirectX::XMFLOAT4A& r, const DirectX::XMFLOAT3A& t)
{
rotation = r;
translation = t;
}
DirectX::XMVECTOR TransformCoord(DirectX::CXMVECTOR v) const
{
DirectX::XMVECTOR u = DirectX::XMVector3Rotate(v, DirectX::XMLoadFloat4A(&rotation));
return DirectX::XMVectorAdd(DirectX::XMLoadFloat3A(&translation), u);
}
public:
static RigidTransform Identity()
{
return RigidTransform();
}
static RigidTransform Inverse(const RigidTransform& src)
{
RigidTransform is;
DirectX::XMVECTOR xv = DirectX::XMLoadFloat4A(&src.rotation);
DirectX::XMStoreFloat4A(&is.rotation, DirectX::XMQuaternionConjugate(xv));
xv = DirectX::XMLoadFloat3A(&src.translation);
DirectX::XMStoreFloat3A(&is.translation, DirectX::XMVectorNegate(xv));
return is;
}
public:
DirectX::XMFLOAT4X4A ToMatrix4x4() const
{
DirectX::XMMATRIX m = DirectX::XMMatrixAffineTransformation(DirectX::XMVectorSet(1.0f, 1.0f, 1.0f, 1.0f),
DirectX::XMVectorZero(),
XMLoadFloat4A(&rotation),
XMLoadFloat3A(&translation));
DirectX::XMFLOAT4X4A r;
DirectX::XMStoreFloat4x4A(&r, m);
return r;
}
static RigidTransform FromMatrix4x4(const DirectX::XMFLOAT4X4A& m)
{
RigidTransform at;
at.translation = DirectX::XMFLOAT3A(m._41, m._42, m._43);
DirectX::XMMATRIX xm = DirectX::XMLoadFloat4x4A(&m);
DirectX::XMVECTOR rv = DirectX::XMQuaternionRotationMatrix(xm);
if (DirectX::XMVectorGetW(rv) < 0)
{
rv = DirectX::XMVectorNegate(rv);
}
DirectX::XMStoreFloat4A(&at.rotation, rv);
return at;
}
private:
DirectX::XMFLOAT4A rotation;
DirectX::XMFLOAT3A translation;
};
#endif //RIGID_TRANSFORM_H