-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransformMatrix.cpp
199 lines (168 loc) · 3.74 KB
/
TransformMatrix.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
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
/*
* CUDARadiosity 0.87a
* Copyright 2012 by David Sargeant, Patrick Gradie, Michael Rogers
*
* Based on code from rrv (Radiosity Renderer and Visualizer) by TODO
* Distributed under GPL (see <http://www.gnu.org/licenses/>)
*
*/
#include "TransformMatrix.h"
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
SpatialVector::operator float() const {
return sqrt(dx*dx + dy*dy + dz*dz);
}
/**
* Create matrix of identity
*/
void TransformMatrix::setIdentity(TTransformMatrix dest) {
for(int i=0; i<4; i++)
for(int j=0; j<4; j++)
dest[i][j] = (i==j)?
1.0f:
0.0f;
}
TransformMatrix::TransformMatrix() {
setIdentity(matrix_);
}
/**
* FIXME: ad hoc algoritmus
* @param angle
* @param vect
*/
void TransformMatrix::rotate (float angle, SpatialVector vect) {
// Convert angle to radians
angle *= M_PI/180.0;
// Normalize spatial vector and
// compute angle for all axes
float vectSize = static_cast<float>(vect);
vect*= angle/vectSize;
/* std::cout << "Rotation: " << vect << std::endl;
std::abort();*/
// Working variables
TTransformMatrix mm;
float sinAlpha;
float cosAlpha;
// X-axis rotation
setIdentity(mm);
sinAlpha = sinf(vect.dx);
cosAlpha = cosf(vect.dx);
mm[1][1] = cosAlpha;
mm[1][2] = sinAlpha;
mm[2][1] = -sinAlpha;
mm[2][2] = cosAlpha;
this->mulMatrixFromLeft(mm);
// Y-axis rotation
setIdentity(mm);
sinAlpha = sinf(vect.dy);
cosAlpha = cosf(vect.dy);
mm[0][0] = cosAlpha;
mm[0][2] = sinAlpha;
mm[2][0] = -sinAlpha;
mm[2][2] = cosAlpha;
this->mulMatrixFromLeft(mm);
// Z-axis rotation
setIdentity(mm);
sinAlpha = sinf(vect.dz);
cosAlpha = cosf(vect.dz);
mm[0][0] = cosAlpha;
mm[0][1] = sinAlpha;
mm[1][0] = -sinAlpha;
mm[1][1] = cosAlpha;
this->mulMatrixFromLeft(mm);
}
/**
* @param vect
*/
void TransformMatrix::translate (SpatialVector vect) {
TTransformMatrix mm;
setIdentity(mm);
mm[3][0] = vect.dx;
mm[3][1] = vect.dy;
mm[3][2] = vect.dz;
// FIXME: mulMatrixFromRight??
this->mulMatrixFromLeft(mm);
}
/**
* @param vect
*/
void TransformMatrix::scale (SpatialVector vect ) {
TTransformMatrix mm;
setIdentity(mm);
mm[0][0] = vect.dx;
mm[1][1] = vect.dy;
mm[2][2] = vect.dz;
// FIXME: mulMatrixFromRight??
this->mulMatrixFromLeft(mm);
}
/**
* @param vect
*/
void TransformMatrix::shear (SpatialVector) {
// TODO: Implement
}
/**
* @return Vertex
* @param vertex
*/
Vertex TransformMatrix::transform (Vertex in) {
float x=
matrix_[0][0]*in.x +
matrix_[1][0]*in.y +
matrix_[2][0]*in.z +
matrix_[3][0];
float y=
matrix_[0][1]*in.x +
matrix_[1][1]*in.y +
matrix_[2][1]*in.z +
matrix_[3][1];
float z=
matrix_[0][2]*in.x +
matrix_[1][2]*in.y +
matrix_[2][2]*in.z +
matrix_[3][2];
float w=
matrix_[0][3]*in.x +
matrix_[1][3]*in.y +
matrix_[2][3]*in.z +
matrix_[3][3];
float factor= 1/w;
return Vertex(
x/factor,
y/factor,
z/factor
);
}
void TransformMatrix::mulMatrixFromLeft(TTransformMatrix mm) {
TTransformMatrix tmp;
copyMatrix(tmp, matrix_);
mulMatrix(matrix_, mm, tmp);
}
void TransformMatrix::mulMatrixFromRight(TTransformMatrix mm) {
TTransformMatrix tmp;
copyMatrix(tmp, matrix_);
mulMatrix(matrix_, tmp, mm);
}
void TransformMatrix::copyMatrix(
TTransformMatrix dest,
TTransformMatrix src)
{
for(int i=0; i< MATRIX_DIM; i++)
for(int j=0; j< MATRIX_DIM; j++)
dest[i][j] = src[i][j];
}
void TransformMatrix::mulMatrix(
TTransformMatrix dest,
TTransformMatrix a,
TTransformMatrix b)
{
for(int i=0; i< MATRIX_DIM; i++)
for(int j=0; j< MATRIX_DIM; j++) {
float &destItem = dest[i][j];
destItem = 0.0;
for (int n=0; n< MATRIX_DIM; n++)
destItem+= a[i][n] * b[n][j];
}
}