| 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 a Float32Array implementation that implements |
| 18 |
* most of the Float32Array spec and that can be used when a built-in |
| 19 |
* implementation is not available. |
| 20 |
* |
| 21 |
* Note that if no existing Float32Array implementation is found then |
| 22 |
* this class and all its public properties are exported as Float32Array. |
| 23 |
* |
| 24 |
* Adding support for the other TypedArray classes here does not make sense |
| 25 |
* since this vector math library only needs Float32Array. |
| 26 |
* |
| 27 |
*/ |
| 28 |
goog.provide('goog.vec.Float32Array'); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* Constructs a new Float32Array. The new array is initialized to all zeros. |
| 34 |
* |
| 35 |
* @param {goog.vec.Float32Array|Array|ArrayBuffer|number} p0 |
| 36 |
* The length of the array, or an array to initialize the contents of the |
| 37 |
* new Float32Array. |
| 38 |
* @constructor |
| 39 |
* @implements {IArrayLike<number>} |
| 40 |
* @final |
| 41 |
*/ |
| 42 |
goog.vec.Float32Array = function(p0) { |
| 43 |
/** @type {number} */ |
| 44 |
this.length = /** @type {number} */ (p0.length || p0); |
| 45 |
for (var i = 0; i < this.length; i++) { |
| 46 |
this[i] = p0[i] || 0; |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* The number of bytes in an element (as defined by the Typed Array |
| 53 |
* specification). |
| 54 |
* |
| 55 |
* @type {number} |
| 56 |
*/ |
| 57 |
goog.vec.Float32Array.BYTES_PER_ELEMENT = 4; |
| 58 |
|
| 59 |
|
| 60 |
/** |
| 61 |
* The number of bytes in an element (as defined by the Typed Array |
| 62 |
* specification). |
| 63 |
* |
| 64 |
* @type {number} |
| 65 |
*/ |
| 66 |
goog.vec.Float32Array.prototype.BYTES_PER_ELEMENT = 4; |
| 67 |
|
| 68 |
|
| 69 |
/** |
| 70 |
* Sets elements of the array. |
| 71 |
* @param {Array<number>|Float32Array} values The array of values. |
| 72 |
* @param {number=} opt_offset The offset in this array to start. |
| 73 |
*/ |
| 74 |
goog.vec.Float32Array.prototype.set = function(values, opt_offset) { |
| 75 |
opt_offset = opt_offset || 0; |
| 76 |
for (var i = 0; i < values.length && opt_offset + i < this.length; i++) { |
| 77 |
this[opt_offset + i] = values[i]; |
| 78 |
} |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
/** |
| 83 |
* Creates a string representation of this array. |
| 84 |
* @return {string} The string version of this array. |
| 85 |
* @override |
| 86 |
*/ |
| 87 |
goog.vec.Float32Array.prototype.toString = Array.prototype.join; |
| 88 |
|
| 89 |
|
| 90 |
/** |
| 91 |
* Note that we cannot implement the subarray() or (deprecated) slice() |
| 92 |
* methods properly since doing so would require being able to overload |
| 93 |
* the [] operator which is not possible in javascript. So we leave |
| 94 |
* them unimplemented. Any attempt to call these methods will just result |
| 95 |
* in a javascript error since we leave them undefined. |
| 96 |
*/ |
| 97 |
|
| 98 |
|
| 99 |
/** |
| 100 |
* If no existing Float32Array implementation is found then we export |
| 101 |
* goog.vec.Float32Array as Float32Array. |
| 102 |
*/ |
| 103 |
if (typeof Float32Array == 'undefined') { |
| 104 |
goog.exportProperty( |
| 105 |
goog.vec.Float32Array, 'BYTES_PER_ELEMENT', |
| 106 |
goog.vec.Float32Array.BYTES_PER_ELEMENT); |
| 107 |
goog.exportProperty( |
| 108 |
goog.vec.Float32Array.prototype, 'BYTES_PER_ELEMENT', |
| 109 |
goog.vec.Float32Array.prototype.BYTES_PER_ELEMENT); |
| 110 |
goog.exportProperty( |
| 111 |
goog.vec.Float32Array.prototype, 'set', |
| 112 |
goog.vec.Float32Array.prototype.set); |
| 113 |
goog.exportProperty( |
| 114 |
goog.vec.Float32Array.prototype, 'toString', |
| 115 |
goog.vec.Float32Array.prototype.toString); |
| 116 |
goog.exportSymbol('Float32Array', goog.vec.Float32Array); |
| 117 |
} |