Ray.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import Cartesian3 from "./Cartesian3.js";
  2. import Check from "./Check.js";
  3. import defaultValue from "./defaultValue.js";
  4. import defined from "./defined.js";
  5. /**
  6. * Represents a ray that extends infinitely from the provided origin in the provided direction.
  7. * @alias Ray
  8. * @constructor
  9. *
  10. * @param {Cartesian3} [origin=Cartesian3.ZERO] The origin of the ray.
  11. * @param {Cartesian3} [direction=Cartesian3.ZERO] The direction of the ray.
  12. */
  13. function Ray(origin, direction) {
  14. direction = Cartesian3.clone(defaultValue(direction, Cartesian3.ZERO));
  15. if (!Cartesian3.equals(direction, Cartesian3.ZERO)) {
  16. Cartesian3.normalize(direction, direction);
  17. }
  18. /**
  19. * The origin of the ray.
  20. * @type {Cartesian3}
  21. * @default {@link Cartesian3.ZERO}
  22. */
  23. this.origin = Cartesian3.clone(defaultValue(origin, Cartesian3.ZERO));
  24. /**
  25. * The direction of the ray.
  26. * @type {Cartesian3}
  27. */
  28. this.direction = direction;
  29. }
  30. /**
  31. * Duplicates a Ray instance.
  32. *
  33. * @param {Ray} ray The ray to duplicate.
  34. * @param {Ray} [result] The object onto which to store the result.
  35. * @returns {Ray} The modified result parameter or a new Ray instance if one was not provided. (Returns undefined if ray is undefined)
  36. */
  37. Ray.clone = function (ray, result) {
  38. if (!defined(ray)) {
  39. return undefined;
  40. }
  41. if (!defined(result)) {
  42. return new Ray(ray.origin, ray.direction);
  43. }
  44. result.origin = Cartesian3.clone(ray.origin);
  45. result.direction = Cartesian3.clone(ray.direction);
  46. return result;
  47. };
  48. /**
  49. * Computes the point along the ray given by r(t) = o + t*d,
  50. * where o is the origin of the ray and d is the direction.
  51. *
  52. * @param {Ray} ray The ray.
  53. * @param {number} t A scalar value.
  54. * @param {Cartesian3} [result] The object in which the result will be stored.
  55. * @returns {Cartesian3} The modified result parameter, or a new instance if none was provided.
  56. *
  57. * @example
  58. * //Get the first intersection point of a ray and an ellipsoid.
  59. * const intersection = Cesium.IntersectionTests.rayEllipsoid(ray, ellipsoid);
  60. * const point = Cesium.Ray.getPoint(ray, intersection.start);
  61. */
  62. Ray.getPoint = function (ray, t, result) {
  63. //>>includeStart('debug', pragmas.debug);
  64. Check.typeOf.object("ray", ray);
  65. Check.typeOf.number("t", t);
  66. //>>includeEnd('debug');
  67. if (!defined(result)) {
  68. result = new Cartesian3();
  69. }
  70. result = Cartesian3.multiplyByScalar(ray.direction, t, result);
  71. return Cartesian3.add(ray.origin, result, result);
  72. };
  73. export default Ray;