forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmat3.js
More file actions
274 lines (242 loc) · 7.04 KB
/
mat3.js
File metadata and controls
274 lines (242 loc) · 7.04 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { Vec3 } from './vec3.js';
/**
* @class
* @name Mat3
* @classdesc A 3x3 matrix.
* @description Creates a new identity Mat3 object.
* @property {Float32Array} data Matrix elements in the form of a flat array.
*/
class Mat3 {
constructor() {
// Create an identity matrix. Note that a new Float32Array has all elements set
// to zero by default, so we only need to set the relevant elements to one.
const data = new Float32Array(9);
data[0] = data[4] = data[8] = 1;
this.data = data;
}
/**
* @function
* @name Mat3#clone
* @description Creates a duplicate of the specified matrix.
* @returns {Mat3} A duplicate matrix.
* @example
* var src = new pc.Mat3().translate(10, 20, 30);
* var dst = src.clone();
* console.log("The two matrices are " + (src.equals(dst) ? "equal" : "different"));
*/
clone() {
return new Mat3().copy(this);
}
/**
* @function
* @name Mat3#copy
* @description Copies the contents of a source 3x3 matrix to a destination 3x3 matrix.
* @param {Mat3} rhs - A 3x3 matrix to be copied.
* @returns {Mat3} Self for chaining.
* @example
* var src = new pc.Mat3().translate(10, 20, 30);
* var dst = new pc.Mat3();
* dst.copy(src);
* console.log("The two matrices are " + (src.equals(dst) ? "equal" : "different"));
*/
copy(rhs) {
const src = rhs.data;
const dst = this.data;
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
dst[3] = src[3];
dst[4] = src[4];
dst[5] = src[5];
dst[6] = src[6];
dst[7] = src[7];
dst[8] = src[8];
return this;
}
/**
* @function
* @name Mat3#set
* @description Copies the contents of a source array[9] to a destination 3x3 matrix.
* @param {number[]} src - An array[9] to be copied.
* @returns {Mat3} Self for chaining.
* @example
* var dst = new pc.Mat3();
* dst.set([0, 1, 2, 3, 4, 5, 6, 7, 8]);
*/
set(src) {
const dst = this.data;
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
dst[3] = src[3];
dst[4] = src[4];
dst[5] = src[5];
dst[6] = src[6];
dst[7] = src[7];
dst[8] = src[8];
return this;
}
/**
* @function
* @name Mat3#equals
* @param {Mat3} rhs - The other matrix.
* @description Reports whether two matrices are equal.
* @returns {boolean} True if the matrices are equal and false otherwise.
* @example
* var a = new pc.Mat3().translate(10, 20, 30);
* var b = new pc.Mat3();
* console.log("The two matrices are " + (a.equals(b) ? "equal" : "different"));
*/
equals(rhs) {
const l = this.data;
const r = rhs.data;
return ((l[0] === r[0]) &&
(l[1] === r[1]) &&
(l[2] === r[2]) &&
(l[3] === r[3]) &&
(l[4] === r[4]) &&
(l[5] === r[5]) &&
(l[6] === r[6]) &&
(l[7] === r[7]) &&
(l[8] === r[8]));
}
/**
* @function
* @name Mat3#isIdentity
* @description Reports whether the specified matrix is the identity matrix.
* @returns {boolean} True if the matrix is identity and false otherwise.
* @example
* var m = new pc.Mat3();
* console.log("The matrix is " + (m.isIdentity() ? "identity" : "not identity"));
*/
isIdentity() {
const m = this.data;
return ((m[0] === 1) &&
(m[1] === 0) &&
(m[2] === 0) &&
(m[3] === 0) &&
(m[4] === 1) &&
(m[5] === 0) &&
(m[6] === 0) &&
(m[7] === 0) &&
(m[8] === 1));
}
/**
* @function
* @name Mat3#setIdentity
* @description Sets the matrix to the identity matrix.
* @returns {Mat3} Self for chaining.
* @example
* m.setIdentity();
* console.log("The matrix is " + (m.isIdentity() ? "identity" : "not identity"));
*/
setIdentity() {
const m = this.data;
m[0] = 1;
m[1] = 0;
m[2] = 0;
m[3] = 0;
m[4] = 1;
m[5] = 0;
m[6] = 0;
m[7] = 0;
m[8] = 1;
return this;
}
/**
* @function
* @name Mat3#toString
* @description Converts the matrix to string form.
* @returns {string} The matrix in string form.
* @example
* var m = new pc.Mat3();
* // Outputs [1, 0, 0, 0, 1, 0, 0, 0, 1]
* console.log(m.toString());
*/
toString() {
let t = '[';
for (let i = 0; i < 9; i++) {
t += this.data[i];
t += (i !== 8) ? ', ' : '';
}
t += ']';
return t;
}
/**
* @function
* @name Mat3#transpose
* @description Generates the transpose of the specified 3x3 matrix.
* @returns {Mat3} Self for chaining.
* @example
* var m = new pc.Mat3();
*
* // Transpose in place
* m.transpose();
*/
transpose() {
const m = this.data;
let tmp;
tmp = m[1]; m[1] = m[3]; m[3] = tmp;
tmp = m[2]; m[2] = m[6]; m[6] = tmp;
tmp = m[5]; m[5] = m[7]; m[7] = tmp;
return this;
}
/**
* @function
* @name Mat3#setFromMat4
* @description Converts the specified 4x4 matrix to a Mat3.
* @param {Mat4} m - The 4x4 matrix to convert.
* @returns {Mat3} Self for chaining.
*/
setFromMat4(m) {
const src = m.data;
const dst = this.data;
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
dst[3] = src[4];
dst[4] = src[5];
dst[5] = src[6];
dst[6] = src[8];
dst[7] = src[9];
dst[8] = src[10];
return this;
}
/**
* @function
* @name Mat3#transformVector
* @description Transforms a 3-dimensional vector by a 3x3 matrix.
* @param {Vec3} vec - The 3-dimensional vector to be transformed.
* @param {Vec3} [res] - An optional 3-dimensional vector to receive the result of the transformation.
* @returns {Vec3} The input vector v transformed by the current instance.
*/
transformVector(vec, res = new Vec3()) {
const m = this.data;
const x = vec.x;
const y = vec.y;
const z = vec.z;
res.x = x * m[0] + y * m[3] + z * m[6];
res.y = x * m[1] + y * m[4] + z * m[7];
res.z = x * m[2] + y * m[5] + z * m[8];
return res;
}
/**
* @field
* @static
* @readonly
* @name Mat3.IDENTITY
* @type {Mat3}
* @description A constant matrix set to the identity.
*/
static IDENTITY = Object.freeze(new Mat3());
/**
* @field
* @static
* @readonly
* @name Mat3.ZERO
* @type {Mat3}
* @description A constant matrix with all elements set to 0.
*/
static ZERO = Object.freeze(new Mat3().set([0, 0, 0, 0, 0, 0, 0, 0, 0]));
}
export { Mat3 };