/
Ray.js
64 lines (59 loc) · 2.02 KB
/
Ray.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*global define*/
define([
'./DeveloperError',
'./defaultValue',
'./Cartesian3'
], function(
DeveloperError,
defaultValue,
Cartesian3) {
"use strict";
/**
* Represents a ray that extends infinitely from the provided origin in the provided direction.
* @alias Ray
* @constructor
*
* @param {Cartesian3} [origin=Cartesian3.ZERO] The origin of the ray.
* @param {Cartesian3} [direction=Cartesian3.ZERO] The direction of the ray.
*/
var Ray = function(origin, direction) {
direction = Cartesian3.clone(defaultValue(direction, Cartesian3.ZERO));
if (!direction.equals(Cartesian3.ZERO)) {
Cartesian3.normalize(direction, direction);
}
/**
* The origin of the ray.
* @type {Cartesian3}
*/
this.origin = Cartesian3.clone(defaultValue(origin, Cartesian3.ZERO));
/**
* The direction of the ray.
* @type {Cartesian3}
*/
this.direction = direction;
};
/**
* Computes the point along the ray given by r(t) = o + t*d,
* where o is the origin of the ray and d is the direction.
* @memberof Ray
*
* @param {Number} t A scalar value.
* @param {Cartesian3} [result] The object in which the result will be stored.
* @returns The modified result parameter, or a new instance if none was provided.
*
* @exception {DeveloperError} t is a required number
*
* @example
* //Get the first intersection point of a ray and an ellipsoid.
* var intersection = IntersectionTests.rayEllipsoid(ray, ellipsoid);
* var point = ray.getPoint(intersection.start);
*/
Ray.prototype.getPoint = function(t, result) {
if (typeof t !== 'number') {
throw new DeveloperError('t is a required number');
}
result = Cartesian3.multiplyByScalar(this.direction, t, result);
return Cartesian3.add(this.origin, result, result);
};
return Ray;
});