float32array.js 3.5 KB

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