VelocityVectorProperty.js 6.8 KB

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