-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransform.cpp
More file actions
208 lines (167 loc) · 3.76 KB
/
Transform.cpp
File metadata and controls
208 lines (167 loc) · 3.76 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* $Rev: 250 $ */
#include "Transform.h"
#include "utility.h"
Transform::Transform() :
T_(Matrix::identity(4,4)), Tinv_(Matrix::identity(4,4)) {
}
Transform::Transform(const Transform& transform) :
T_(transform.T_), Tinv_(transform.Tinv_) {
}
Transform::~Transform() {
}
Transform& Transform::operator=(const Transform& transform) {
if (this != &transform) {
T_ = transform.T_;
Tinv_ = transform.Tinv_;
}
return *this;
}
Point Transform::apply(const Point& point) const {
Vector v(4);
v(0) = point(0);
v(1) = point(1);
v(2) = point(2);
v(3) = 1;
v = T_*v;
Point result;
result(0) = v(0)/v(3);
result(1) = v(1)/v(3);
result(2) = v(2)/v(3);
return result;
}
Direction Transform::apply(const Direction& direction) const {
Vector v(4);
v(0) = direction(0);
v(1) = direction(1);
v(2) = direction(2);
v(3) = 0;
v = T_*v;
Direction result;
result(0) = v(0);
result(1) = v(1);
result(2) = v(2);
return result;
}
Normal Transform::apply(const Normal& normal) const {
Vector v(4);
v(0) = normal(0);
v(1) = normal(1);
v(2) = normal(2);
v(3) = 0;
v = Tinv_.transpose()*v;
Normal result;
result(0) = v(0);
result(1) = v(1);
result(2) = v(2);
return result;
}
Ray Transform::apply(const Ray& ray) const {
Ray result;
result.point = apply(ray.point);
result.direction = apply(ray.direction);
return result;
}
Point Transform::applyInverse(const Point& point) const {
Vector v(4);
v(0) = point(0);
v(1) = point(1);
v(2) = point(2);
v(3) = 1;
v = Tinv_*v;
Point result;
result(0) = v(0)/v(3);
result(1) = v(1)/v(3);
result(2) = v(2)/v(3);
return result;
}
Direction Transform::applyInverse(const Direction& direction) const {
Vector v(4);
v(0) = direction(0);
v(1) = direction(1);
v(2) = direction(2);
v(3) = 0;
v = Tinv_*v;
Direction result;
result(0) = v(0);
result(1) = v(1);
result(2) = v(2);
return result;
}
Normal Transform::applyInverse(const Normal& normal) const {
Vector v(4);
v(0) = normal(0);
v(1) = normal(1);
v(2) = normal(2);
v(3) = 0;
v = T_.transpose()*v;
Normal result;
result(0) = v(0);
result(1) = v(1);
result(2) = v(2);
return result;
}
Ray Transform::applyInverse(const Ray& ray) const {
Ray result;
result.point = applyInverse(ray.point);
result.direction = applyInverse(ray.direction);
return result;
}
void Transform::rotateX(double rx) {
Matrix R(Matrix::identity(4,4));
rx = deg2rad(rx);
R(1,1) = R(2,2) = cos(rx);
R(1,2) = -sin(rx);
R(2,1) = sin(rx);
T_ = R*T_;
Tinv_ = Tinv_*R.transpose();
}
void Transform::rotateY(double ry) {
Matrix R(Matrix::identity(4,4));
ry = deg2rad(ry);
R(0,0) = R(2,2) = cos(ry);
R(0,2) = sin(ry);
R(2,0) = -sin(ry);
T_ = R*T_;
Tinv_ = Tinv_*R.transpose();
}
void Transform::rotateZ(double rz) {
Matrix R(Matrix::identity(4,4));
rz = deg2rad(rz);
R(0,0) = R(1,1) = cos(rz);
R(0,1) = -sin(rz);
R(1,0) = sin(rz);
T_ = R*T_;
Tinv_ = Tinv_*R.transpose();
}
void Transform::scale(double s) {
Matrix S(Matrix::identity(4,4));
S(0,0) = S(1,1) = S(2,2) = s;
T_ = S*T_;
S(0,0) = S(1,1) = S(2,2) = 1/s;
Tinv_ = Tinv_*S;
}
void Transform::scale(double sx, double sy, double sz) {
Matrix S(Matrix::identity(4,4));
S(0,0) = sx;
S(1,1) = sy;
S(2,2) = sz;
T_ = S*T_;
S(0,0) = 1/sx;
S(1,1) = 1/sy;
S(2,2) = 1/sz;
Tinv_ = Tinv_*S;
}
void Transform::translate(double tx, double ty, double tz) {
Matrix T(Matrix::identity(4,4));
T(0,3) = tx;
T(1,3) = ty;
T(2,3) = tz;
T_ = T*T_;
T(0,3) = -tx;
T(1,3) = -ty;
T(2,3) = -tz;
Tinv_ = Tinv_*T;
}
void Transform::translate(const Direction& direction) {
translate(direction(0), direction(1), direction(2));
}