-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCS123Matrix.cpp
161 lines (116 loc) · 4.91 KB
/
CS123Matrix.cpp
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*!
@file CS123Matrix.cpp
@author Travis Fischer ([email protected])
@date Fall 2008
@brief
Provides basic functionality for a templated, arbitrarily-sized matrix.
You will need to fill this file in for the Camtrans assignment.
**/
#include "CS123Algebra.h"
#include <iostream>
#include <cmath>
//@name Routines which generate specific-purpose transformation matrices
//@{---------------------------------------------------------------------
// @returns the scale matrix described by the vector
Matrix4x4 getScaleMat(const Vector4 &v) {
return Matrix4x4(v.x, 0, 0, 0,
0, v.y, 0, 0,
0, 0, v.z, 0,
0, 0, 0, 1);
}
// @returns the translation matrix described by the vector
Matrix4x4 getTransMat(const Vector4 &v) {
return Matrix4x4(1,0,0,v.x,
0,1,0,v.y,
0,0,1,v.z,
0,0,0,1);
}
// @returns the rotation matrix about the x axis by the specified angle
Matrix4x4 getRotXMat (const REAL radians) {
return Matrix4x4(1,0,0,0,
0, cos(radians), -sin(radians), 0,
0, sin(radians), cos(radians), 0,
0, 0, 0, 1);
}
// @returns the rotation matrix about the y axis by the specified angle
Matrix4x4 getRotYMat (const REAL radians) {
return Matrix4x4(cos(radians), 0, sin(radians), 0,
0,1,0,0,
-sin(radians), 0, cos(radians), 0,
0, 0, 0, 1);
}
// @returns the rotation matrix about the z axis by the specified angle
Matrix4x4 getRotZMat (const REAL radians) {
return Matrix4x4(cos(radians), -sin(radians),0,0,
sin(radians), cos(radians), 0,0,
0,0,1,0,
0,0,0,1);
}
// @returns the rotation matrix around the vector and point by the specified angle
Matrix4x4 getRotMat (const Vector4 &p, const Vector4 &v, const REAL a) {
// @TODO: [CAMTRANS] Fill this in...
REAL theta = atan2(v.z, v.x);
REAL otherAngle = -atan2(v.y, sqrt(v.x*v.x+v.z*v.z));
Matrix4x4 trans = Matrix4x4(1,0,0,p.x,
0,1,0, p.y,
0,0,1, p.z,
0,0,0,1);
Matrix4x4 invTrans = Matrix4x4(1,0,0, -p.x,
0,1,0, -p.y,
0,0,1, -p.z,
0,0,0,1);
Matrix4x4 final = trans*getInvRotYMat(theta)*getInvRotZMat(otherAngle)*getRotXMat(a)*getRotZMat(otherAngle)*getRotYMat(theta)*invTrans;
return final;
}
// @returns the inverse scale matrix described by the vector
Matrix4x4 getInvScaleMat(const Vector4 &v) {
return Matrix4x4(1/v.x, 0, 0, 0,
0, 1/v.y, 0, 0,
0, 0, 1/v.z, 0,
0, 0, 0, 1);
}
// @returns the inverse translation matrix described by the vector
Matrix4x4 getInvTransMat(const Vector4 &v) {
return Matrix4x4(1,0,0,-v.x,
0,1,0,-v.y,
0,0,1,-v.z,
0,0,0,1);
}
// @returns the inverse rotation matrix about the x axis by the specified angle
Matrix4x4 getInvRotXMat (const REAL radians) {
return Matrix4x4(1,0,0,0,
0, cos(radians), sin(radians), 0,
0, -sin(radians), cos(radians), 0,
0, 0, 0, 1);
}
// @returns the inverse rotation matrix about the y axis by the specified angle
Matrix4x4 getInvRotYMat (const REAL radians) {
return Matrix4x4(cos(radians), 0, -sin(radians), 0,
0,1,0,0,
sin(radians), 0, cos(radians), 0,
0, 0, 0, 1);
}
// @returns the inverse rotation matrix about the z axis by the specified angle
Matrix4x4 getInvRotZMat (const REAL radians) {
return Matrix4x4(cos(radians), sin(radians),0,0,
-sin(radians), cos(radians), 0,0,
0,0,1,0,
0,0,0,1);
}
// @returns the inverse rotation matrix around the vector and point by the specified angle
Matrix4x4 getInvRotMat (const Vector4 &p, const Vector4 &v, const REAL a) {
// @TODO: [CAMTRANS] Fill this in...
REAL theta = atan2(v.z, v.x);
REAL otherAngle = -atan2(v.y, sqrt(v.x*v.x+v.z*v.z));
Matrix4x4 trans = Matrix4x4(1,0,0,p.x,
0,1,0, p.y,
0,0,1, p.z,
0,0,0,1);
Matrix4x4 invTrans = Matrix4x4(1,0,0, -p.x,
0,1,0, -p.y,
0,0,1, -p.z,
0,0,0,1);
Matrix4x4 final = trans*getInvRotYMat(theta)*getInvRotZMat(otherAngle)*getInvRotXMat(a)*getRotZMat(otherAngle)*getRotYMat(theta)*invTrans;
return final;
}
//@}---------------------------------------------------------------------