-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransform.cpp
More file actions
103 lines (85 loc) · 2.83 KB
/
Transform.cpp
File metadata and controls
103 lines (85 loc) · 2.83 KB
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
#include <cmath>
#include "Transform.h"
//
// Constructors
//
Transform::Transform()
{
fwd.identity();
rev.identity();
}
Transform::Transform( const Matrix4x4 & f, const Matrix4x4 & r)
: fwd(f),
rev(r)
{
}
// Destructors
Transform::~Transform()
{
}
//
// Helpers
//
Transform compose( const Transform & t1, const Transform & t2)
{
Transform c;
mult( t1.fwd, t2.fwd, c.fwd);
mult( t2.rev, t1.rev, c.rev);
return c;
}
// Create a rotation Transform from angle and axis
Transform makeRotation( float angle, const Vector4 & axis)
{
float ca = std::cos( angle);
float omca = 1.0f - ca;
float sa = std::sin( angle);
Transform t;
Vector4 u = axis;
u.normalize();
t.fwd = Matrix4x4( ca + u.x * u.x * omca, u.x * u.y * omca - u.z * sa, u.x * u.z * omca + u.y * sa, 0.0,
u.y * u.x * omca + u.z * sa, ca + u.y * u.y * omca, u.y * u.z * omca - u.x * sa, 0.0,
u.z * u.x * omca - u.y * sa, u.z * u.y * omca + u.x * sa, ca + u.z * u.z * omca, 0.0,
0.0, 0.0, 0.0, 1.0);
inverse( t.fwd, t.rev);
return t;
}
// Create a translation Transform
Transform makeTranslation( const Vector4 & d)
{
return Transform(Matrix4x4(1.0, 0.0, 0.0, d.x,
0.0, 1.0, 0.0, d.y,
0.0, 0.0, 1.0, d.z,
0.0, 0.0, 0.0, 1.0),
Matrix4x4(1.0, 0.0, 0.0, -d.x,
0.0, 1.0, 0.0, -d.y,
0.0, 0.0, 1.0, -d.z,
0.0, 0.0, 0.0, 1.0));
}
// Create a translation Transform
Transform makeTranslation( float dx, float dy, float dz)
{
return Transform(Matrix4x4(1.0, 0.0, 0.0, dx,
0.0, 1.0, 0.0, dy,
0.0, 0.0, 1.0, dz,
0.0, 0.0, 0.0, 1.0),
Matrix4x4(1.0, 0.0, 0.0, -dx,
0.0, 1.0, 0.0, -dy,
0.0, 0.0, 1.0, -dz,
0.0, 0.0, 0.0, 1.0));
}
// Create a scaling Transform
Transform makeScaling( float xScale, float yScale, float zScale)
{
return Transform(Matrix4x4(xScale, 0.0, 0.0, 0.0,
0.0, yScale, 0.0, 0.0,
0.0, 0.0, zScale, 0.0,
0.0, 0.0, 0.0, 1.0),
Matrix4x4(1.0 / xScale, 0.0, 0.0, 0.0,
0.0, 1.0 / yScale, 0.0, 0.0,
0.0, 0.0, 1.0 / zScale, 0.0,
0.0, 0.0, 0.0, 1.0));
}
Transform makeScaling( float scale)
{
return makeScaling( scale, scale, scale);
}