SampledPositionProperty.js 11 KB

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