ray.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 Implements a 3D ray that are compatible with WebGL.
  16. * Each element is a float64 in case high precision is required.
  17. * The API is structured to avoid unnecessary memory allocations.
  18. * The last parameter will typically be the output vector and an
  19. * object can be both an input and output parameter to all methods
  20. * except where noted.
  21. *
  22. */
  23. goog.provide('goog.vec.Ray');
  24. goog.require('goog.vec.Vec3');
  25. /**
  26. * Constructs a new ray with an optional origin and direction. If not specified,
  27. * the default is [0, 0, 0].
  28. * @param {goog.vec.Vec3.AnyType=} opt_origin The optional origin.
  29. * @param {goog.vec.Vec3.AnyType=} opt_dir The optional direction.
  30. * @constructor
  31. * @final
  32. */
  33. goog.vec.Ray = function(opt_origin, opt_dir) {
  34. /**
  35. * @type {goog.vec.Vec3.Float64}
  36. */
  37. this.origin = goog.vec.Vec3.createFloat64();
  38. if (opt_origin) {
  39. goog.vec.Vec3.setFromArray(this.origin, opt_origin);
  40. }
  41. /**
  42. * @type {goog.vec.Vec3.Float64}
  43. */
  44. this.dir = goog.vec.Vec3.createFloat64();
  45. if (opt_dir) {
  46. goog.vec.Vec3.setFromArray(this.dir, opt_dir);
  47. }
  48. };
  49. /**
  50. * Sets the origin and direction of the ray.
  51. * @param {goog.vec.AnyType} origin The new origin.
  52. * @param {goog.vec.AnyType} dir The new direction.
  53. */
  54. goog.vec.Ray.prototype.set = function(origin, dir) {
  55. goog.vec.Vec3.setFromArray(this.origin, origin);
  56. goog.vec.Vec3.setFromArray(this.dir, dir);
  57. };
  58. /**
  59. * Sets the origin of the ray.
  60. * @param {goog.vec.AnyType} origin the new origin.
  61. */
  62. goog.vec.Ray.prototype.setOrigin = function(origin) {
  63. goog.vec.Vec3.setFromArray(this.origin, origin);
  64. };
  65. /**
  66. * Sets the direction of the ray.
  67. * @param {goog.vec.AnyType} dir The new direction.
  68. */
  69. goog.vec.Ray.prototype.setDir = function(dir) {
  70. goog.vec.Vec3.setFromArray(this.dir, dir);
  71. };
  72. /**
  73. * Returns true if this ray is equal to the other ray.
  74. * @param {goog.vec.Ray} other The other ray.
  75. * @return {boolean} True if this ray is equal to the other ray.
  76. */
  77. goog.vec.Ray.prototype.equals = function(other) {
  78. return other != null && goog.vec.Vec3.equals(this.origin, other.origin) &&
  79. goog.vec.Vec3.equals(this.dir, other.dir);
  80. };