ray.js 2.2 KB

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