SampledPositionProperty.js 11 KB

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