-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathRay.js
80 lines (73 loc) · 2.43 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import Cartesian3 from "./Cartesian3.js";
import Check from "./Check.js";
import defaultValue from "./defaultValue.js";
import defined from "./defined.js";
/**
* 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.
*/
function Ray(origin, direction) {
direction = Cartesian3.clone(defaultValue(direction, Cartesian3.ZERO));
if (!Cartesian3.equals(direction, Cartesian3.ZERO)) {
Cartesian3.normalize(direction, direction);
}
/**
* The origin of the ray.
* @type {Cartesian3}
* @default {@link Cartesian3.ZERO}
*/
this.origin = Cartesian3.clone(defaultValue(origin, Cartesian3.ZERO));
/**
* The direction of the ray.
* @type {Cartesian3}
*/
this.direction = direction;
}
/**
* Duplicates a Ray instance.
*
* @param {Ray} ray The ray to duplicate.
* @param {Ray} [result] The object onto which to store the result.
* @returns {Ray} The modified result parameter or a new Ray instance if one was not provided. (Returns undefined if ray is undefined)
*/
Ray.clone = function (ray, result) {
if (!defined(ray)) {
return undefined;
}
if (!defined(result)) {
return new Ray(ray.origin, ray.direction);
}
result.origin = Cartesian3.clone(ray.origin);
result.direction = Cartesian3.clone(ray.direction);
return result;
};
/**
* 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.
*
* @param {Ray} ray The ray.
* @param {Number} t A scalar value.
* @param {Cartesian3} [result] The object in which the result will be stored.
* @returns {Cartesian3} The modified result parameter, or a new instance if none was provided.
*
* @example
* //Get the first intersection point of a ray and an ellipsoid.
* const intersection = Cesium.IntersectionTests.rayEllipsoid(ray, ellipsoid);
* const point = Cesium.Ray.getPoint(ray, intersection.start);
*/
Ray.getPoint = function (ray, t, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("ray", ray);
Check.typeOf.number("t", t);
//>>includeEnd('debug');
if (!defined(result)) {
result = new Cartesian3();
}
result = Cartesian3.multiplyByScalar(ray.direction, t, result);
return Cartesian3.add(ray.origin, result, result);
};
export default Ray;