VelocityOrientationProperty.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import Cartesian3 from "../Core/Cartesian3.js";
  2. import defaultValue from "../Core/defaultValue.js";
  3. import defined from "../Core/defined.js";
  4. import Ellipsoid from "../Core/Ellipsoid.js";
  5. import Event from "../Core/Event.js";
  6. import Matrix3 from "../Core/Matrix3.js";
  7. import Quaternion from "../Core/Quaternion.js";
  8. import Transforms from "../Core/Transforms.js";
  9. import Property from "./Property.js";
  10. import VelocityVectorProperty from "./VelocityVectorProperty.js";
  11. /**
  12. * A {@link Property} which evaluates to a {@link Quaternion} rotation
  13. * based on the velocity of the provided {@link PositionProperty}.
  14. *
  15. * @alias VelocityOrientationProperty
  16. * @constructor
  17. *
  18. * @param {PositionProperty} [position] The position property used to compute the orientation.
  19. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid used to determine which way is up.
  20. *
  21. * @example
  22. * //Create an entity with position and orientation.
  23. * const position = new Cesium.SampledProperty();
  24. * position.addSamples(...);
  25. * const entity = viewer.entities.add({
  26. * position : position,
  27. * orientation : new Cesium.VelocityOrientationProperty(position)
  28. * }));
  29. */
  30. function VelocityOrientationProperty(position, ellipsoid) {
  31. this._velocityVectorProperty = new VelocityVectorProperty(position, true);
  32. this._subscription = undefined;
  33. this._ellipsoid = undefined;
  34. this._definitionChanged = new Event();
  35. this.ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  36. const that = this;
  37. this._velocityVectorProperty.definitionChanged.addEventListener(function () {
  38. that._definitionChanged.raiseEvent(that);
  39. });
  40. }
  41. Object.defineProperties(VelocityOrientationProperty.prototype, {
  42. /**
  43. * Gets a value indicating if this property is constant.
  44. * @memberof VelocityOrientationProperty.prototype
  45. *
  46. * @type {Boolean}
  47. * @readonly
  48. */
  49. isConstant: {
  50. get: function () {
  51. return Property.isConstant(this._velocityVectorProperty);
  52. },
  53. },
  54. /**
  55. * Gets the event that is raised whenever the definition of this property changes.
  56. * @memberof VelocityOrientationProperty.prototype
  57. *
  58. * @type {Event}
  59. * @readonly
  60. */
  61. definitionChanged: {
  62. get: function () {
  63. return this._definitionChanged;
  64. },
  65. },
  66. /**
  67. * Gets or sets the position property used to compute orientation.
  68. * @memberof VelocityOrientationProperty.prototype
  69. *
  70. * @type {Property|undefined}
  71. */
  72. position: {
  73. get: function () {
  74. return this._velocityVectorProperty.position;
  75. },
  76. set: function (value) {
  77. this._velocityVectorProperty.position = value;
  78. },
  79. },
  80. /**
  81. * Gets or sets the ellipsoid used to determine which way is up.
  82. * @memberof VelocityOrientationProperty.prototype
  83. *
  84. * @type {Property|undefined}
  85. */
  86. ellipsoid: {
  87. get: function () {
  88. return this._ellipsoid;
  89. },
  90. set: function (value) {
  91. const oldValue = this._ellipsoid;
  92. if (oldValue !== value) {
  93. this._ellipsoid = value;
  94. this._definitionChanged.raiseEvent(this);
  95. }
  96. },
  97. },
  98. });
  99. const positionScratch = new Cartesian3();
  100. const velocityScratch = new Cartesian3();
  101. const rotationScratch = new Matrix3();
  102. /**
  103. * Gets the value of the property at the provided time.
  104. *
  105. * @param {JulianDate} [time] The time for which to retrieve the value.
  106. * @param {Quaternion} [result] The object to store the value into, if omitted, a new instance is created and returned.
  107. * @returns {Quaternion} The modified result parameter or a new instance if the result parameter was not supplied.
  108. */
  109. VelocityOrientationProperty.prototype.getValue = function (time, result) {
  110. const velocity = this._velocityVectorProperty._getValue(
  111. time,
  112. velocityScratch,
  113. positionScratch
  114. );
  115. if (!defined(velocity)) {
  116. return undefined;
  117. }
  118. Transforms.rotationMatrixFromPositionVelocity(
  119. positionScratch,
  120. velocity,
  121. this._ellipsoid,
  122. rotationScratch
  123. );
  124. return Quaternion.fromRotationMatrix(rotationScratch, result);
  125. };
  126. /**
  127. * Compares this property to the provided property and returns
  128. * <code>true</code> if they are equal, <code>false</code> otherwise.
  129. *
  130. * @param {Property} [other] The other property.
  131. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  132. */
  133. VelocityOrientationProperty.prototype.equals = function (other) {
  134. return (
  135. this === other || //
  136. (other instanceof VelocityOrientationProperty &&
  137. Property.equals(
  138. this._velocityVectorProperty,
  139. other._velocityVectorProperty
  140. ) &&
  141. (this._ellipsoid === other._ellipsoid ||
  142. this._ellipsoid.equals(other._ellipsoid)))
  143. );
  144. };
  145. export default VelocityOrientationProperty;