-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMatrix.js
155 lines (141 loc) · 4.22 KB
/
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Owner: david@famo.us
* @license MPL 2.0
* @copyright Famous Industries, Inc. 2014
*/
define(function(require, exports, module) {
var Vector = require('./Vector');
/**
* A library for using a 3x3 numerical matrix, represented as a two-level array.
*
* @class Matrix
* @constructor
*
* @param {Array.Array} values array of rows
*/
function Matrix(values) {
this.values = values ||
[
[1,0,0],
[0,1,0],
[0,0,1]
];
return this;
}
var _register = new Matrix();
var _vectorRegister = new Vector();
/**
* Return the values in the matrix as an array of numerical row arrays
*
* @method get
*
* @return {Array.array} matrix values as array of rows.
*/
Matrix.prototype.get = function get() {
return this.values;
};
/**
* Set the nested array of rows in the matrix.
*
* @method set
*
* @param {Array.array} values matrix values as array of rows.
*/
Matrix.prototype.set = function set(values) {
this.values = values;
};
/**
* Take this matrix as A, input vector V as a column vector, and return matrix product (A)(V).
* Note: This sets the internal vector register. Current handles to the vector register
* will see values changed.
*
* @method vectorMultiply
*
* @param {Vector} v input vector V
* @return {Vector} result of multiplication, as a handle to the internal vector register
*/
Matrix.prototype.vectorMultiply = function vectorMultiply(v) {
var M = this.get();
var v0 = v.x;
var v1 = v.y;
var v2 = v.z;
var M0 = M[0];
var M1 = M[1];
var M2 = M[2];
var M00 = M0[0];
var M01 = M0[1];
var M02 = M0[2];
var M10 = M1[0];
var M11 = M1[1];
var M12 = M1[2];
var M20 = M2[0];
var M21 = M2[1];
var M22 = M2[2];
return _vectorRegister.setXYZ(
M00*v0 + M01*v1 + M02*v2,
M10*v0 + M11*v1 + M12*v2,
M20*v0 + M21*v1 + M22*v2
);
};
/**
* Multiply the provided matrix M2 with this matrix. Result is (this) * (M2).
* Note: This sets the internal matrix register. Current handles to the register
* will see values changed.
*
* @method multiply
*
* @param {Matrix} M2 input matrix to multiply on the right
* @return {Matrix} result of multiplication, as a handle to the internal register
*/
Matrix.prototype.multiply = function multiply(M2) {
var M1 = this.get();
var result = [[]];
for (var i = 0; i < 3; i++) {
result[i] = [];
for (var j = 0; j < 3; j++) {
var sum = 0;
for (var k = 0; k < 3; k++) {
sum += M1[i][k] * M2[k][j];
}
result[i][j] = sum;
}
}
return _register.set(result);
};
/**
* Creates a Matrix which is the transpose of this matrix.
* Note: This sets the internal matrix register. Current handles to the register
* will see values changed.
*
* @method transpose
*
* @return {Matrix} result of transpose, as a handle to the internal register
*/
Matrix.prototype.transpose = function transpose() {
var result = [];
var M = this.get();
for (var row = 0; row < 3; row++) {
for (var col = 0; col < 3; col++) {
result[row][col] = M[col][row];
}
}
return _register.set(result);
};
/**
* Clones the matrix
*
* @method clone
* @return {Matrix} New copy of the original matrix
*/
Matrix.prototype.clone = function clone() {
var values = this.get();
var M = [];
for (var row = 0; row < 3; row++)
M[row] = values[row].slice();
return new Matrix(M);
};
module.exports = Matrix;
});