Debian Codegraph @

closure-library/closure/goog/vec/vec4.js

raw
1 // Copyright 2011 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15
16 /**
17 * @fileoverview Supplies 4 element vectors that are compatible with WebGL.
18 * Each element is a float32 since that is typically the desired size of a
19 * 4-vector in the GPU. The API is structured to avoid unnecessary memory
20 * allocations. The last parameter will typically be the output vector and
21 * an object can be both an input and output parameter to all methods except
22 * where noted.
23 *
24 */
25 goog.provide('goog.vec.Vec4');
26
27 /** @suppress {extraRequire} */
28 goog.require('goog.vec');
29
30 /** @typedef {goog.vec.Float32} */ goog.vec.Vec4.Float32;
31 /** @typedef {goog.vec.Float64} */ goog.vec.Vec4.Float64;
32 /** @typedef {goog.vec.Number} */ goog.vec.Vec4.Number;
33 /** @typedef {goog.vec.AnyType} */ goog.vec.Vec4.AnyType;
34
35 // The following two types are deprecated - use the above types instead.
36 /** @typedef {Float32Array} */ goog.vec.Vec4.Type;
37 /** @typedef {goog.vec.ArrayType} */ goog.vec.Vec4.Vec4Like;
38
39
40 /**
41 * Creates a 4 element vector of Float32. The array is initialized to zero.
42 *
43 * @return {!goog.vec.Vec4.Float32} The new 3 element array.
44 */
45 goog.vec.Vec4.createFloat32 = function() {
46 return new Float32Array(4);
47 };
48
49
50 /**
51 * Creates a 4 element vector of Float64. The array is initialized to zero.
52 *
53 * @return {!goog.vec.Vec4.Float64} The new 4 element array.
54 */
55 goog.vec.Vec4.createFloat64 = function() {
56 return new Float64Array(4);
57 };
58
59
60 /**
61 * Creates a 4 element vector of Number. The array is initialized to zero.
62 *
63 * @return {!goog.vec.Vec4.Number} The new 4 element array.
64 */
65 goog.vec.Vec4.createNumber = function() {
66 var v = new Array(4);
67 goog.vec.Vec4.setFromValues(v, 0, 0, 0, 0);
68 return v;
69 };
70
71
72 /**
73 * Creates a 4 element vector of Float32Array. The array is initialized to zero.
74 *
75 * @deprecated Use createFloat32.
76 * @return {!goog.vec.Vec4.Type} The new 4 element array.
77 */
78 goog.vec.Vec4.create = function() {
79 return new Float32Array(4);
80 };
81
82
83 /**
84 * Creates a new 4 element vector initialized with the value from the given
85 * array.
86 *
87 * @deprecated Use createFloat32FromArray.
88 * @param {goog.vec.Vec4.Vec4Like} vec The source 4 element array.
89 * @return {!goog.vec.Vec4.Type} The new 4 element array.
90 */
91 goog.vec.Vec4.createFromArray = function(vec) {
92 var newVec = goog.vec.Vec4.create();
93 goog.vec.Vec4.setFromArray(newVec, vec);
94 return newVec;
95 };
96
97
98 /**
99 * Creates a new 4 element FLoat32 vector initialized with the value from the
100 * given array.
101 *
102 * @param {goog.vec.Vec4.AnyType} vec The source 3 element array.
103 * @return {!goog.vec.Vec4.Float32} The new 3 element array.
104 */
105 goog.vec.Vec4.createFloat32FromArray = function(vec) {
106 var newVec = goog.vec.Vec4.createFloat32();
107 goog.vec.Vec4.setFromArray(newVec, vec);
108 return newVec;
109 };
110
111
112 /**
113 * Creates a new 4 element Float32 vector initialized with the supplied values.
114 *
115 * @param {number} v0 The value for element at index 0.
116 * @param {number} v1 The value for element at index 1.
117 * @param {number} v2 The value for element at index 2.
118 * @param {number} v3 The value for element at index 3.
119 * @return {!goog.vec.Vec4.Float32} The new vector.
120 */
121 goog.vec.Vec4.createFloat32FromValues = function(v0, v1, v2, v3) {
122 var vec = goog.vec.Vec4.createFloat32();
123 goog.vec.Vec4.setFromValues(vec, v0, v1, v2, v3);
124 return vec;
125 };
126
127
128 /**
129 * Creates a clone of the given 4 element Float32 vector.
130 *
131 * @param {goog.vec.Vec4.Float32} vec The source 3 element vector.
132 * @return {!goog.vec.Vec4.Float32} The new cloned vector.
133 */
134 goog.vec.Vec4.cloneFloat32 = goog.vec.Vec4.createFloat32FromArray;
135
136
137 /**
138 * Creates a new 4 element Float64 vector initialized with the value from the
139 * given array.
140 *
141 * @param {goog.vec.Vec4.AnyType} vec The source 4 element array.
142 * @return {!goog.vec.Vec4.Float64} The new 4 element array.
143 */
144 goog.vec.Vec4.createFloat64FromArray = function(vec) {
145 var newVec = goog.vec.Vec4.createFloat64();
146 goog.vec.Vec4.setFromArray(newVec, vec);
147 return newVec;
148 };
149
150
151 /**
152 * Creates a new 4 element Float64 vector initialized with the supplied values.
153 *
154 * @param {number} v0 The value for element at index 0.
155 * @param {number} v1 The value for element at index 1.
156 * @param {number} v2 The value for element at index 2.
157 * @param {number} v3 The value for element at index 3.
158 * @return {!goog.vec.Vec4.Float64} The new vector.
159 */
160 goog.vec.Vec4.createFloat64FromValues = function(v0, v1, v2, v3) {
161 var vec = goog.vec.Vec4.createFloat64();
162 goog.vec.Vec4.setFromValues(vec, v0, v1, v2, v3);
163 return vec;
164 };
165
166
167 /**
168 * Creates a clone of the given 4 element vector.
169 *
170 * @param {goog.vec.Vec4.Float64} vec The source 4 element vector.
171 * @return {!goog.vec.Vec4.Float64} The new cloned vector.
172 */
173 goog.vec.Vec4.cloneFloat64 = goog.vec.Vec4.createFloat64FromArray;
174
175
176 /**
177 * Creates a new 4 element vector initialized with the supplied values.
178 *
179 * @deprecated Use createFloat32FromValues.
180 * @param {number} v0 The value for element at index 0.
181 * @param {number} v1 The value for element at index 1.
182 * @param {number} v2 The value for element at index 2.
183 * @param {number} v3 The value for element at index 3.
184 * @return {!goog.vec.Vec4.Type} The new vector.
185 */
186 goog.vec.Vec4.createFromValues = function(v0, v1, v2, v3) {
187 var vec = goog.vec.Vec4.create();
188 goog.vec.Vec4.setFromValues(vec, v0, v1, v2, v3);
189 return vec;
190 };
191
192
193 /**
194 * Creates a clone of the given 4 element vector.
195 *
196 * @deprecated Use cloneFloat32.
197 * @param {goog.vec.Vec4.Vec4Like} vec The source 4 element vector.
198 * @return {!goog.vec.Vec4.Type} The new cloned vector.
199 */
200 goog.vec.Vec4.clone = goog.vec.Vec4.createFromArray;
201
202
203 /**
204 * Initializes the vector with the given values.
205 *
206 * @param {goog.vec.Vec4.AnyType} vec The vector to receive the values.
207 * @param {number} v0 The value for element at index 0.
208 * @param {number} v1 The value for element at index 1.
209 * @param {number} v2 The value for element at index 2.
210 * @param {number} v3 The value for element at index 3.
211 * @return {!goog.vec.Vec4.AnyType} Return vec so that operations can be
212 * chained together.
213 */
214 goog.vec.Vec4.setFromValues = function(vec, v0, v1, v2, v3) {
215 vec[0] = v0;
216 vec[1] = v1;
217 vec[2] = v2;
218 vec[3] = v3;
219 return vec;
220 };
221
222
223 /**
224 * Initializes the vector with the given array of values.
225 *
226 * @param {goog.vec.Vec4.AnyType} vec The vector to receive the
227 * values.
228 * @param {goog.vec.Vec4.AnyType} values The array of values.
229 * @return {!goog.vec.Vec4.AnyType} Return vec so that operations can be
230 * chained together.
231 */
232 goog.vec.Vec4.setFromArray = function(vec, values) {
233 vec[0] = values[0];
234 vec[1] = values[1];
235 vec[2] = values[2];
236 vec[3] = values[3];
237 return vec;
238 };
239
240
241 /**
242 * Performs a component-wise addition of vec0 and vec1 together storing the
243 * result into resultVec.
244 *
245 * @param {goog.vec.Vec4.AnyType} vec0 The first addend.
246 * @param {goog.vec.Vec4.AnyType} vec1 The second addend.
247 * @param {goog.vec.Vec4.AnyType} resultVec The vector to
248 * receive the result. May be vec0 or vec1.
249 * @return {!goog.vec.Vec4.AnyType} Return resultVec so that operations can be
250 * chained together.
251 */
252 goog.vec.Vec4.add = function(vec0, vec1, resultVec) {
253 resultVec[0] = vec0[0] + vec1[0];
254 resultVec[1] = vec0[1] + vec1[1];
255 resultVec[2] = vec0[2] + vec1[2];
256 resultVec[3] = vec0[3] + vec1[3];
257 return resultVec;
258 };
259
260
261 /**
262 * Performs a component-wise subtraction of vec1 from vec0 storing the
263 * result into resultVec.
264 *
265 * @param {goog.vec.Vec4.AnyType} vec0 The minuend.
266 * @param {goog.vec.Vec4.AnyType} vec1 The subtrahend.
267 * @param {goog.vec.Vec4.AnyType} resultVec The vector to
268 * receive the result. May be vec0 or vec1.
269 * @return {!goog.vec.Vec4.AnyType} Return resultVec so that operations can be
270 * chained together.
271 */
272 goog.vec.Vec4.subtract = function(vec0, vec1, resultVec) {
273 resultVec[0] = vec0[0] - vec1[0];
274 resultVec[1] = vec0[1] - vec1[1];
275 resultVec[2] = vec0[2] - vec1[2];
276 resultVec[3] = vec0[3] - vec1[3];
277 return resultVec;
278 };
279
280
281 /**
282 * Negates vec0, storing the result into resultVec.
283 *
284 * @param {goog.vec.Vec4.AnyType} vec0 The vector to negate.
285 * @param {goog.vec.Vec4.AnyType} resultVec The vector to
286 * receive the result. May be vec0.
287 * @return {!goog.vec.Vec4.AnyType} Return resultVec so that operations can be
288 * chained together.
289 */
290 goog.vec.Vec4.negate = function(vec0, resultVec) {
291 resultVec[0] = -vec0[0];
292 resultVec[1] = -vec0[1];
293 resultVec[2] = -vec0[2];
294 resultVec[3] = -vec0[3];
295 return resultVec;
296 };
297
298
299 /**
300 * Takes the absolute value of each component of vec0 storing the result in
301 * resultVec.
302 *
303 * @param {goog.vec.Vec4.AnyType} vec0 The source vector.
304 * @param {goog.vec.Vec4.AnyType} resultVec The vector to receive the result.
305 * May be vec0.
306 * @return {!goog.vec.Vec4.AnyType} Return resultVec so that operations can be
307 * chained together.
308 */
309 goog.vec.Vec4.abs = function(vec0, resultVec) {
310 resultVec[0] = Math.abs(vec0[0]);
311 resultVec[1] = Math.abs(vec0[1]);
312 resultVec[2] = Math.abs(vec0[2]);
313 resultVec[3] = Math.abs(vec0[3]);
314 return resultVec;
315 };
316
317
318 /**
319 * Multiplies each component of vec0 with scalar storing the product into
320 * resultVec.
321 *
322 * @param {goog.vec.Vec4.AnyType} vec0 The source vector.
323 * @param {number} scalar The value to multiply with each component of vec0.
324 * @param {goog.vec.Vec4.AnyType} resultVec The vector to
325 * receive the result. May be vec0.
326 * @return {!goog.vec.Vec4.AnyType} Return resultVec so that operations can be
327 * chained together.
328 */
329 goog.vec.Vec4.scale = function(vec0, scalar, resultVec) {
330 resultVec[0] = vec0[0] * scalar;
331 resultVec[1] = vec0[1] * scalar;
332 resultVec[2] = vec0[2] * scalar;
333 resultVec[3] = vec0[3] * scalar;
334 return resultVec;
335 };
336
337
338 /**
339 * Returns the magnitudeSquared of the given vector.
340 *
341 * @param {goog.vec.Vec4.AnyType} vec0 The vector.
342 * @return {number} The magnitude of the vector.
343 */
344 goog.vec.Vec4.magnitudeSquared = function(vec0) {
345 var x = vec0[0], y = vec0[1], z = vec0[2], w = vec0[3];
346 return x * x + y * y + z * z + w * w;
347 };
348
349
350 /**
351 * Returns the magnitude of the given vector.
352 *
353 * @param {goog.vec.Vec4.AnyType} vec0 The vector.
354 * @return {number} The magnitude of the vector.
355 */
356 goog.vec.Vec4.magnitude = function(vec0) {
357 var x = vec0[0], y = vec0[1], z = vec0[2], w = vec0[3];
358 return Math.sqrt(x * x + y * y + z * z + w * w);
359 };
360
361
362 /**
363 * Normalizes the given vector storing the result into resultVec.
364 *
365 * @param {goog.vec.Vec4.AnyType} vec0 The vector to normalize.
366 * @param {goog.vec.Vec4.AnyType} resultVec The vector to
367 * receive the result. May be vec0.
368 * @return {!goog.vec.Vec4.AnyType} Return resultVec so that operations can be
369 * chained together.
370 */
371 goog.vec.Vec4.normalize = function(vec0, resultVec) {
372 var ilen = 1 / goog.vec.Vec4.magnitude(vec0);
373 resultVec[0] = vec0[0] * ilen;
374 resultVec[1] = vec0[1] * ilen;
375 resultVec[2] = vec0[2] * ilen;
376 resultVec[3] = vec0[3] * ilen;
377 return resultVec;
378 };
379
380
381 /**
382 * Returns the scalar product of vectors v0 and v1.
383 *
384 * @param {goog.vec.Vec4.AnyType} v0 The first vector.
385 * @param {goog.vec.Vec4.AnyType} v1 The second vector.
386 * @return {number} The scalar product.
387 */
388 goog.vec.Vec4.dot = function(v0, v1) {
389 return v0[0] * v1[0] + v0[1] * v1[1] + v0[2] * v1[2] + v0[3] * v1[3];
390 };
391
392
393 /**
394 * Linearly interpolate from v0 to v1 according to f. The value of f should be
395 * in the range [0..1] otherwise the results are undefined.
396 *
397 * @param {goog.vec.Vec4.AnyType} v0 The first vector.
398 * @param {goog.vec.Vec4.AnyType} v1 The second vector.
399 * @param {number} f The interpolation factor.
400 * @param {goog.vec.Vec4.AnyType} resultVec The vector to receive the
401 * results (may be v0 or v1).
402 * @return {!goog.vec.Vec4.AnyType} Return resultVec so that operations can be
403 * chained together.
404 */
405 goog.vec.Vec4.lerp = function(v0, v1, f, resultVec) {
406 var x = v0[0], y = v0[1], z = v0[2], w = v0[3];
407 resultVec[0] = (v1[0] - x) * f + x;
408 resultVec[1] = (v1[1] - y) * f + y;
409 resultVec[2] = (v1[2] - z) * f + z;
410 resultVec[3] = (v1[3] - w) * f + w;
411 return resultVec;
412 };
413
414
415 /**
416 * Compares the components of vec0 with the components of another vector or
417 * scalar, storing the larger values in resultVec.
418 *
419 * @param {goog.vec.Vec4.AnyType} vec0 The source vector.
420 * @param {goog.vec.Vec4.AnyType|number} limit The limit vector or scalar.
421 * @param {goog.vec.Vec4.AnyType} resultVec The vector to receive the
422 * results (may be vec0 or limit).
423 * @return {!goog.vec.Vec4.AnyType} Return resultVec so that operations can be
424 * chained together.
425 */
426 goog.vec.Vec4.max = function(vec0, limit, resultVec) {
427 if (goog.isNumber(limit)) {
428 resultVec[0] = Math.max(vec0[0], limit);
429 resultVec[1] = Math.max(vec0[1], limit);
430 resultVec[2] = Math.max(vec0[2], limit);
431 resultVec[3] = Math.max(vec0[3], limit);
432 } else {
433 resultVec[0] = Math.max(vec0[0], limit[0]);
434 resultVec[1] = Math.max(vec0[1], limit[1]);
435 resultVec[2] = Math.max(vec0[2], limit[2]);
436 resultVec[3] = Math.max(vec0[3], limit[3]);
437 }
438 return resultVec;
439 };
440
441
442 /**
443 * Compares the components of vec0 with the components of another vector or
444 * scalar, storing the smaller values in resultVec.
445 *
446 * @param {goog.vec.Vec4.AnyType} vec0 The source vector.
447 * @param {goog.vec.Vec4.AnyType|number} limit The limit vector or scalar.
448 * @param {goog.vec.Vec4.AnyType} resultVec The vector to receive the
449 * results (may be vec0 or limit).
450 * @return {!goog.vec.Vec4.AnyType} Return resultVec so that operations can be
451 * chained together.
452 */
453 goog.vec.Vec4.min = function(vec0, limit, resultVec) {
454 if (goog.isNumber(limit)) {
455 resultVec[0] = Math.min(vec0[0], limit);
456 resultVec[1] = Math.min(vec0[1], limit);
457 resultVec[2] = Math.min(vec0[2], limit);
458 resultVec[3] = Math.min(vec0[3], limit);
459 } else {
460 resultVec[0] = Math.min(vec0[0], limit[0]);
461 resultVec[1] = Math.min(vec0[1], limit[1]);
462 resultVec[2] = Math.min(vec0[2], limit[2]);
463 resultVec[3] = Math.min(vec0[3], limit[3]);
464 }
465 return resultVec;
466 };
467
468
469 /**
470 * Returns true if the components of v0 are equal to the components of v1.
471 *
472 * @param {goog.vec.Vec4.AnyType} v0 The first vector.
473 * @param {goog.vec.Vec4.AnyType} v1 The second vector.
474 * @return {boolean} True if the vectors are equal, false otherwise.
475 */
476 goog.vec.Vec4.equals = function(v0, v1) {
477 return v0.length == v1.length && v0[0] == v1[0] && v0[1] == v1[1] &&
478 v0[2] == v1[2] && v0[3] == v1[3];
479 };