-
Notifications
You must be signed in to change notification settings - Fork 0
/
3jsbot_matrix.js
171 lines (136 loc) · 3.02 KB
/
3jsbot_matrix.js
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
//////////////////////////////////////////////////
///// MATRIX ALGEBRA AND GEOMETRIC TRANSFORMS
//////////////////////////////////////////////////
/*
CS148: reference code has functions for:
matrix_multiply
matrix_transpose
vector_normalize
vector_cross
generate_identity
generate_translation_matrix
generate_rotation_matrix_X
generate_rotation_matrix_Y
generate_rotation_matrix_Z
*/
function matrix_multiply( mat1, mat2 )
{
var mat1cols = mat1[0].length;
var mat1rows = mat1.length;
var mat2cols = mat2[0].length;
var mat2rows = mat2.length;
var outmat = init_matrix(mat1rows, mat2cols);
var accum = 0;
if( mat1cols != mat2rows )
{
console.log("matrix multiply error\n");
}
for( var r = 0; r < mat1rows; r++ )
{
for( var c = 0; c < mat2cols; c++ )
{
accum = 0;
for( var sop = 0; sop < mat2rows; sop++)
{
accum += mat1[r][sop] * mat2[sop][c];
}
outmat[r][c] = accum;
}
}
return outmat;
}
function matrix_transpose( mat )
{
var rows = mat.length;
var cols = mat[0].length;
var outmat = init_matrix(cols, rows);
for( var r = 0; r < rows; r++ )
{
for( var c = 0; c < cols; c++ )
{
outmat[c][r] = mat[r][c];
}
}
return outmat;
}
function vector_normalize( v )
{
var sum = 0;
for( var i = 0; i < v.length; i++)
sum += v[i]*v[i];
var length = Math.sqrt(sum);
for( i = 0; i < v.length; i++ )
v[i] = v[i]/length;
return v;
}
function vector_cross(v1, v2)
{
if( v1.length != 3 || v2.length != 3 )
console.log("incorrect vector dimension");
var outv = new Array(3);
outv[0] = v1[1]*v2[2] - v1[2]*v2[1];
outv[1] = v1[2]*v2[0] - v1[0]*v2[2];
outv[2] = v1[0]*v2[1] - v1[1]*v2[0];
return outv;
}
function generate_identity(dim)
{
var outmat = init_matrix(dim, dim);
for(var r = 0; r < dim; r++)
{
for(var c = 0; c < dim; c++)
{
if(r == c)
outmat[r][c] = 1;
else
outmat[r][c] = 0;
}
}
return outmat;
}
function init_matrix(rows, cols)
{
outmat = new Array(rows);
for(var r = 0; r < rows; r++)
outmat[r] = new Array(cols);
return outmat;
}
function generate_translation_matrix( x, y, z )
{
var outmat = generate_identity(4);
outmat[0][3] = x;
outmat[1][3] = y;
outmat[2][3] = z;
return outmat;
}
function generate_rotation_matrix( x, y, z )
{
return matrix_multiply( matrix_multiply( generate_x_rotation(x), generate_y_rotation(y) ), generate_z_rotation(z) );
}
function generate_x_rotation( angle )
{
var outmat = generate_identity(4);
outmat[1][1] = Math.cos(angle);
outmat[1][2] = -Math.sin(angle);
outmat[2][2] = Math.cos(angle);
outmat[2][1] = Math.sin(angle);
return outmat;
}
function generate_y_rotation( angle )
{
var outmat = generate_identity(4);
outmat[0][0] = Math.cos(angle);
outmat[0][2] = Math.sin(angle);
outmat[2][2] = Math.cos(angle);
outmat[2][0] = -Math.sin(angle);
return outmat;
}
function generate_z_rotation( angle )
{
var outmat = generate_identity(4);
outmat[0][0] = Math.cos(angle);
outmat[0][1] = -Math.sin(angle);
outmat[1][1] = Math.cos(angle);
outmat[1][0] = Math.sin(angle);
return outmat;
}