VelocityVectorProperty.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import Cartesian3 from "../Core/Cartesian3.js";
  2. import defaultValue from "../Core/defaultValue.js";
  3. import defined from "../Core/defined.js";
  4. import DeveloperError from "../Core/DeveloperError.js";
  5. import Event from "../Core/Event.js";
  6. import JulianDate from "../Core/JulianDate.js";
  7. import Property from "./Property.js";
  8. /**
  9. * A {@link Property} which evaluates to a {@link Cartesian3} vector
  10. * based on the velocity of the provided {@link PositionProperty}.
  11. *
  12. * @alias VelocityVectorProperty
  13. * @constructor
  14. *
  15. * @param {PositionProperty} [position] The position property used to compute the velocity.
  16. * @param {boolean} [normalize=true] Whether to normalize the computed velocity vector.
  17. *
  18. * @example
  19. * //Create an entity with a billboard rotated to match its velocity.
  20. * const position = new Cesium.SampledProperty();
  21. * position.addSamples(...);
  22. * const entity = viewer.entities.add({
  23. * position : position,
  24. * billboard : {
  25. * image : 'image.png',
  26. * alignedAxis : new Cesium.VelocityVectorProperty(position, true) // alignedAxis must be a unit vector
  27. * }
  28. * }));
  29. */
  30. function VelocityVectorProperty(position, normalize) {
  31. this._position = undefined;
  32. this._subscription = undefined;
  33. this._definitionChanged = new Event();
  34. this._normalize = defaultValue(normalize, true);
  35. this.position = position;
  36. }
  37. Object.defineProperties(VelocityVectorProperty.prototype, {
  38. /**
  39. * Gets a value indicating if this property is constant.
  40. * @memberof VelocityVectorProperty.prototype
  41. *
  42. * @type {boolean}
  43. * @readonly
  44. */
  45. isConstant: {
  46. get: function () {
  47. return Property.isConstant(this._position);
  48. },
  49. },
  50. /**
  51. * Gets the event that is raised whenever the definition of this property changes.
  52. * @memberof VelocityVectorProperty.prototype
  53. *
  54. * @type {Event}
  55. * @readonly
  56. */
  57. definitionChanged: {
  58. get: function () {
  59. return this._definitionChanged;
  60. },
  61. },
  62. /**
  63. * Gets or sets the position property used to compute the velocity vector.
  64. * @memberof VelocityVectorProperty.prototype
  65. *
  66. * @type {Property|undefined}
  67. */
  68. position: {
  69. get: function () {
  70. return this._position;
  71. },
  72. set: function (value) {
  73. const oldValue = this._position;
  74. if (oldValue !== value) {
  75. if (defined(oldValue)) {
  76. this._subscription();
  77. }
  78. this._position = value;
  79. if (defined(value)) {
  80. this._subscription = value._definitionChanged.addEventListener(
  81. function () {
  82. this._definitionChanged.raiseEvent(this);
  83. },
  84. this
  85. );
  86. }
  87. this._definitionChanged.raiseEvent(this);
  88. }
  89. },
  90. },
  91. /**
  92. * Gets or sets whether the vector produced by this property
  93. * will be normalized or not.
  94. * @memberof VelocityVectorProperty.prototype
  95. *
  96. * @type {boolean}
  97. */
  98. normalize: {
  99. get: function () {
  100. return this._normalize;
  101. },
  102. set: function (value) {
  103. if (this._normalize === value) {
  104. return;
  105. }
  106. this._normalize = value;
  107. this._definitionChanged.raiseEvent(this);
  108. },
  109. },
  110. });
  111. const position1Scratch = new Cartesian3();
  112. const position2Scratch = new Cartesian3();
  113. const timeScratch = new JulianDate();
  114. const step = 1.0 / 60.0;
  115. /**
  116. * Gets the value of the property at the provided time.
  117. *
  118. * @param {JulianDate} [time] The time for which to retrieve the value.
  119. * @param {Cartesian3} [result] The object to store the value into, if omitted, a new instance is created and returned.
  120. * @returns {Cartesian3} The modified result parameter or a new instance if the result parameter was not supplied.
  121. */
  122. VelocityVectorProperty.prototype.getValue = function (time, result) {
  123. return this._getValue(time, result);
  124. };
  125. /**
  126. * @private
  127. */
  128. VelocityVectorProperty.prototype._getValue = function (
  129. time,
  130. velocityResult,
  131. positionResult
  132. ) {
  133. //>>includeStart('debug', pragmas.debug);
  134. if (!defined(time)) {
  135. throw new DeveloperError("time is required");
  136. }
  137. //>>includeEnd('debug');
  138. if (!defined(velocityResult)) {
  139. velocityResult = new Cartesian3();
  140. }
  141. const property = this._position;
  142. if (Property.isConstant(property)) {
  143. return this._normalize
  144. ? undefined
  145. : Cartesian3.clone(Cartesian3.ZERO, velocityResult);
  146. }
  147. let position1 = property.getValue(time, position1Scratch);
  148. let position2 = property.getValue(
  149. JulianDate.addSeconds(time, step, timeScratch),
  150. position2Scratch
  151. );
  152. //If we don't have a position for now, return undefined.
  153. if (!defined(position1)) {
  154. return undefined;
  155. }
  156. //If we don't have a position for now + step, see if we have a position for now - step.
  157. if (!defined(position2)) {
  158. position2 = position1;
  159. position1 = property.getValue(
  160. JulianDate.addSeconds(time, -step, timeScratch),
  161. position2Scratch
  162. );
  163. if (!defined(position1)) {
  164. return undefined;
  165. }
  166. }
  167. if (Cartesian3.equals(position1, position2)) {
  168. return this._normalize
  169. ? undefined
  170. : Cartesian3.clone(Cartesian3.ZERO, velocityResult);
  171. }
  172. if (defined(positionResult)) {
  173. position1.clone(positionResult);
  174. }
  175. const velocity = Cartesian3.subtract(position2, position1, velocityResult);
  176. if (this._normalize) {
  177. return Cartesian3.normalize(velocity, velocityResult);
  178. }
  179. return Cartesian3.divideByScalar(velocity, step, velocityResult);
  180. };
  181. /**
  182. * Compares this property to the provided property and returns
  183. * <code>true</code> if they are equal, <code>false</code> otherwise.
  184. *
  185. * @param {Property} [other] The other property.
  186. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  187. */
  188. VelocityVectorProperty.prototype.equals = function (other) {
  189. return (
  190. this === other || //
  191. (other instanceof VelocityVectorProperty &&
  192. Property.equals(this._position, other._position))
  193. );
  194. };
  195. export default VelocityVectorProperty;