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