SampledPositionProperty.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import Cartesian3 from '../Core/Cartesian3.js';
  2. import Check from '../Core/Check.js';
  3. import defaultValue from '../Core/defaultValue.js';
  4. import defined from '../Core/defined.js';
  5. import defineProperties from '../Core/defineProperties.js';
  6. import DeveloperError from '../Core/DeveloperError.js';
  7. import Event from '../Core/Event.js';
  8. import ReferenceFrame from '../Core/ReferenceFrame.js';
  9. import PositionProperty from './PositionProperty.js';
  10. import Property from './Property.js';
  11. import SampledProperty from './SampledProperty.js';
  12. /**
  13. * A {@link SampledProperty} which is also a {@link PositionProperty}.
  14. *
  15. * @alias SampledPositionProperty
  16. * @constructor
  17. *
  18. * @param {ReferenceFrame} [referenceFrame=ReferenceFrame.FIXED] The reference frame in which the position is defined.
  19. * @param {Number} [numberOfDerivatives=0] The number of derivatives that accompany each position; i.e. velocity, acceleration, etc...
  20. */
  21. function SampledPositionProperty(referenceFrame, numberOfDerivatives) {
  22. numberOfDerivatives = defaultValue(numberOfDerivatives, 0);
  23. var derivativeTypes;
  24. if (numberOfDerivatives > 0) {
  25. derivativeTypes = new Array(numberOfDerivatives);
  26. for (var i = 0; i < numberOfDerivatives; i++) {
  27. derivativeTypes[i] = Cartesian3;
  28. }
  29. }
  30. this._numberOfDerivatives = numberOfDerivatives;
  31. this._property = new SampledProperty(Cartesian3, derivativeTypes);
  32. this._definitionChanged = new Event();
  33. this._referenceFrame = defaultValue(referenceFrame, ReferenceFrame.FIXED);
  34. this._property._definitionChanged.addEventListener(function() {
  35. this._definitionChanged.raiseEvent(this);
  36. }, this);
  37. }
  38. defineProperties(SampledPositionProperty.prototype, {
  39. /**
  40. * Gets a value indicating if this property is constant. A property is considered
  41. * constant if getValue always returns the same result for the current definition.
  42. * @memberof SampledPositionProperty.prototype
  43. *
  44. * @type {Boolean}
  45. * @readonly
  46. */
  47. isConstant : {
  48. get : function() {
  49. return this._property.isConstant;
  50. }
  51. },
  52. /**
  53. * Gets the event that is raised whenever the definition of this property changes.
  54. * The definition is considered to have changed if a call to getValue would return
  55. * a different result for the same time.
  56. * @memberof SampledPositionProperty.prototype
  57. *
  58. * @type {Event}
  59. * @readonly
  60. */
  61. definitionChanged : {
  62. get : function() {
  63. return this._definitionChanged;
  64. }
  65. },
  66. /**
  67. * Gets the reference frame in which the position is defined.
  68. * @memberof SampledPositionProperty.prototype
  69. * @type {ReferenceFrame}
  70. * @default ReferenceFrame.FIXED;
  71. */
  72. referenceFrame : {
  73. get : function() {
  74. return this._referenceFrame;
  75. }
  76. },
  77. /**
  78. * Gets the degree of interpolation to perform when retrieving a value.
  79. * @memberof SampledPositionProperty.prototype
  80. *
  81. * @type {Number}
  82. * @default 1
  83. */
  84. interpolationDegree : {
  85. get : function() {
  86. return this._property.interpolationDegree;
  87. }
  88. },
  89. /**
  90. * Gets the interpolation algorithm to use when retrieving a value.
  91. * @memberof SampledPositionProperty.prototype
  92. *
  93. * @type {InterpolationAlgorithm}
  94. * @default LinearApproximation
  95. */
  96. interpolationAlgorithm : {
  97. get : function() {
  98. return this._property.interpolationAlgorithm;
  99. }
  100. },
  101. /**
  102. * The number of derivatives contained by this property; i.e. 0 for just position, 1 for velocity, etc.
  103. * @memberof SampledPositionProperty.prototype
  104. *
  105. * @type {Boolean}
  106. * @default false
  107. */
  108. numberOfDerivatives : {
  109. get : function() {
  110. return this._numberOfDerivatives;
  111. }
  112. },
  113. /**
  114. * Gets or sets the type of extrapolation to perform when a value
  115. * is requested at a time after any available samples.
  116. * @memberof SampledPositionProperty.prototype
  117. * @type {ExtrapolationType}
  118. * @default ExtrapolationType.NONE
  119. */
  120. forwardExtrapolationType : {
  121. get : function() {
  122. return this._property.forwardExtrapolationType;
  123. },
  124. set : function(value) {
  125. this._property.forwardExtrapolationType = value;
  126. }
  127. },
  128. /**
  129. * Gets or sets the amount of time to extrapolate forward before
  130. * the property becomes undefined. A value of 0 will extrapolate forever.
  131. * @memberof SampledPositionProperty.prototype
  132. * @type {Number}
  133. * @default 0
  134. */
  135. forwardExtrapolationDuration : {
  136. get : function() {
  137. return this._property.forwardExtrapolationDuration;
  138. },
  139. set : function(value) {
  140. this._property.forwardExtrapolationDuration = value;
  141. }
  142. },
  143. /**
  144. * Gets or sets the type of extrapolation to perform when a value
  145. * is requested at a time before any available samples.
  146. * @memberof SampledPositionProperty.prototype
  147. * @type {ExtrapolationType}
  148. * @default ExtrapolationType.NONE
  149. */
  150. backwardExtrapolationType : {
  151. get : function() {
  152. return this._property.backwardExtrapolationType;
  153. },
  154. set : function(value) {
  155. this._property.backwardExtrapolationType = value;
  156. }
  157. },
  158. /**
  159. * Gets or sets the amount of time to extrapolate backward
  160. * before the property becomes undefined. A value of 0 will extrapolate forever.
  161. * @memberof SampledPositionProperty.prototype
  162. * @type {Number}
  163. * @default 0
  164. */
  165. backwardExtrapolationDuration : {
  166. get : function() {
  167. return this._property.backwardExtrapolationDuration;
  168. },
  169. set : function(value) {
  170. this._property.backwardExtrapolationDuration = value;
  171. }
  172. }
  173. });
  174. /**
  175. * Gets the position at the provided time.
  176. *
  177. * @param {JulianDate} time The time for which to retrieve the value.
  178. * @param {Cartesian3} [result] The object to store the value into, if omitted, a new instance is created and returned.
  179. * @returns {Cartesian3} The modified result parameter or a new instance if the result parameter was not supplied.
  180. */
  181. SampledPositionProperty.prototype.getValue = function(time, result) {
  182. return this.getValueInReferenceFrame(time, ReferenceFrame.FIXED, result);
  183. };
  184. /**
  185. * Gets the position at the provided time and in the provided reference frame.
  186. *
  187. * @param {JulianDate} time The time for which to retrieve the value.
  188. * @param {ReferenceFrame} referenceFrame The desired referenceFrame of the result.
  189. * @param {Cartesian3} [result] The object to store the value into, if omitted, a new instance is created and returned.
  190. * @returns {Cartesian3} The modified result parameter or a new instance if the result parameter was not supplied.
  191. */
  192. SampledPositionProperty.prototype.getValueInReferenceFrame = function(time, referenceFrame, result) {
  193. //>>includeStart('debug', pragmas.debug);
  194. Check.defined('time', time);
  195. Check.defined('referenceFrame', referenceFrame);
  196. //>>includeEnd('debug');
  197. result = this._property.getValue(time, result);
  198. if (defined(result)) {
  199. return PositionProperty.convertToReferenceFrame(time, result, this._referenceFrame, referenceFrame, result);
  200. }
  201. return undefined;
  202. };
  203. /**
  204. * Sets the algorithm and degree to use when interpolating a position.
  205. *
  206. * @param {Object} [options] Object with the following properties:
  207. * @param {InterpolationAlgorithm} [options.interpolationAlgorithm] The new interpolation algorithm. If undefined, the existing property will be unchanged.
  208. * @param {Number} [options.interpolationDegree] The new interpolation degree. If undefined, the existing property will be unchanged.
  209. */
  210. SampledPositionProperty.prototype.setInterpolationOptions = function(options) {
  211. this._property.setInterpolationOptions(options);
  212. };
  213. /**
  214. * Adds a new sample.
  215. *
  216. * @param {JulianDate} time The sample time.
  217. * @param {Cartesian3} position The position at the provided time.
  218. * @param {Cartesian3[]} [derivatives] The array of derivative values at the provided time.
  219. */
  220. SampledPositionProperty.prototype.addSample = function(time, position, derivatives) {
  221. var numberOfDerivatives = this._numberOfDerivatives;
  222. //>>includeStart('debug', pragmas.debug);
  223. if (numberOfDerivatives > 0 && (!defined(derivatives) || derivatives.length !== numberOfDerivatives)) {
  224. throw new DeveloperError('derivatives length must be equal to the number of derivatives.');
  225. }
  226. //>>includeEnd('debug');
  227. this._property.addSample(time, position, derivatives);
  228. };
  229. /**
  230. * Adds multiple samples via parallel arrays.
  231. *
  232. * @param {JulianDate[]} times An array of JulianDate instances where each index is a sample time.
  233. * @param {Cartesian3[]} positions An array of Cartesian3 position instances, where each value corresponds to the provided time index.
  234. * @param {Array[]} [derivatives] An array where each value is another array containing derivatives for the corresponding time index.
  235. *
  236. * @exception {DeveloperError} All arrays must be the same length.
  237. */
  238. SampledPositionProperty.prototype.addSamples = function(times, positions, derivatives) {
  239. this._property.addSamples(times, positions, derivatives);
  240. };
  241. /**
  242. * Adds samples as a single packed array where each new sample is represented as a date,
  243. * followed by the packed representation of the corresponding value and derivatives.
  244. *
  245. * @param {Number[]} packedSamples The array of packed samples.
  246. * @param {JulianDate} [epoch] If any of the dates in packedSamples are numbers, they are considered an offset from this epoch, in seconds.
  247. */
  248. SampledPositionProperty.prototype.addSamplesPackedArray = function(packedSamples, epoch) {
  249. this._property.addSamplesPackedArray(packedSamples, epoch);
  250. };
  251. /**
  252. * Removes a sample at the given time, if present.
  253. *
  254. * @param {JulianDate} time The sample time.
  255. * @returns {Boolean} <code>true</code> if a sample at time was removed, <code>false</code> otherwise.
  256. */
  257. SampledPositionProperty.prototype.removeSample = function(time) {
  258. this._property.removeSample(time);
  259. };
  260. /**
  261. * Removes all samples for the given time interval.
  262. *
  263. * @param {TimeInterval} time The time interval for which to remove all samples.
  264. */
  265. SampledPositionProperty.prototype.removeSamples = function(timeInterval) {
  266. this._property.removeSamples(timeInterval);
  267. };
  268. /**
  269. * Compares this property to the provided property and returns
  270. * <code>true</code> if they are equal, <code>false</code> otherwise.
  271. *
  272. * @param {Property} [other] The other property.
  273. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  274. */
  275. SampledPositionProperty.prototype.equals = function(other) {
  276. return this === other || //
  277. (other instanceof SampledPositionProperty &&
  278. Property.equals(this._property, other._property) && //
  279. this._referenceFrame === other._referenceFrame);
  280. };
  281. export default SampledPositionProperty;