CompositePositionProperty.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import defaultValue from "../Core/defaultValue.js";
  2. import defined from "../Core/defined.js";
  3. import DeveloperError from "../Core/DeveloperError.js";
  4. import Event from "../Core/Event.js";
  5. import ReferenceFrame from "../Core/ReferenceFrame.js";
  6. import CompositeProperty from "./CompositeProperty.js";
  7. import Property from "./Property.js";
  8. /**
  9. * A {@link CompositeProperty} which is also a {@link PositionProperty}.
  10. *
  11. * @alias CompositePositionProperty
  12. * @constructor
  13. *
  14. * @param {ReferenceFrame} [referenceFrame=ReferenceFrame.FIXED] The reference frame in which the position is defined.
  15. */
  16. function CompositePositionProperty(referenceFrame) {
  17. this._referenceFrame = defaultValue(referenceFrame, ReferenceFrame.FIXED);
  18. this._definitionChanged = new Event();
  19. this._composite = new CompositeProperty();
  20. this._composite.definitionChanged.addEventListener(
  21. CompositePositionProperty.prototype._raiseDefinitionChanged,
  22. this
  23. );
  24. }
  25. Object.defineProperties(CompositePositionProperty.prototype, {
  26. /**
  27. * Gets a value indicating if this property is constant. A property is considered
  28. * constant if getValue always returns the same result for the current definition.
  29. * @memberof CompositePositionProperty.prototype
  30. *
  31. * @type {boolean}
  32. * @readonly
  33. */
  34. isConstant: {
  35. get: function () {
  36. return this._composite.isConstant;
  37. },
  38. },
  39. /**
  40. * Gets the event that is raised whenever the definition of this property changes.
  41. * The definition is changed whenever setValue is called with data different
  42. * than the current value.
  43. * @memberof CompositePositionProperty.prototype
  44. *
  45. * @type {Event}
  46. * @readonly
  47. */
  48. definitionChanged: {
  49. get: function () {
  50. return this._definitionChanged;
  51. },
  52. },
  53. /**
  54. * Gets the interval collection.
  55. * @memberof CompositePositionProperty.prototype
  56. *
  57. * @type {TimeIntervalCollection}
  58. */
  59. intervals: {
  60. get: function () {
  61. return this._composite.intervals;
  62. },
  63. },
  64. /**
  65. * Gets or sets the reference frame which this position presents itself as.
  66. * Each PositionProperty making up this object has it's own reference frame,
  67. * so this property merely exposes a "preferred" reference frame for clients
  68. * to use.
  69. * @memberof CompositePositionProperty.prototype
  70. *
  71. * @type {ReferenceFrame}
  72. */
  73. referenceFrame: {
  74. get: function () {
  75. return this._referenceFrame;
  76. },
  77. set: function (value) {
  78. this._referenceFrame = value;
  79. },
  80. },
  81. });
  82. /**
  83. * Gets the value of the property at the provided time in the fixed frame.
  84. *
  85. * @param {JulianDate} time The time for which to retrieve the value.
  86. * @param {Cartesian3} [result] The object to store the value into, if omitted, a new instance is created and returned.
  87. * @returns {Cartesian3 | undefined} The modified result parameter or a new instance if the result parameter was not supplied.
  88. */
  89. CompositePositionProperty.prototype.getValue = function (time, result) {
  90. return this.getValueInReferenceFrame(time, ReferenceFrame.FIXED, result);
  91. };
  92. /**
  93. * Gets the value of the property at the provided time and in the provided reference frame.
  94. *
  95. * @param {JulianDate} time The time for which to retrieve the value.
  96. * @param {ReferenceFrame} referenceFrame The desired referenceFrame of the result.
  97. * @param {Cartesian3} [result] The object to store the value into, if omitted, a new instance is created and returned.
  98. * @returns {Cartesian3 | undefined} The modified result parameter or a new instance if the result parameter was not supplied.
  99. */
  100. CompositePositionProperty.prototype.getValueInReferenceFrame = function (
  101. time,
  102. referenceFrame,
  103. result
  104. ) {
  105. //>>includeStart('debug', pragmas.debug);
  106. if (!defined(time)) {
  107. throw new DeveloperError("time is required.");
  108. }
  109. if (!defined(referenceFrame)) {
  110. throw new DeveloperError("referenceFrame is required.");
  111. }
  112. //>>includeEnd('debug');
  113. const innerProperty = this._composite._intervals.findDataForIntervalContainingDate(
  114. time
  115. );
  116. if (defined(innerProperty)) {
  117. return innerProperty.getValueInReferenceFrame(time, referenceFrame, result);
  118. }
  119. return undefined;
  120. };
  121. /**
  122. * Compares this property to the provided property and returns
  123. * <code>true</code> if they are equal, <code>false</code> otherwise.
  124. *
  125. * @param {Property} [other] The other property.
  126. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  127. */
  128. CompositePositionProperty.prototype.equals = function (other) {
  129. return (
  130. this === other || //
  131. (other instanceof CompositePositionProperty && //
  132. this._referenceFrame === other._referenceFrame && //
  133. this._composite.equals(other._composite, Property.equals))
  134. );
  135. };
  136. /**
  137. * @private
  138. */
  139. CompositePositionProperty.prototype._raiseDefinitionChanged = function () {
  140. this._definitionChanged.raiseEvent(this);
  141. };
  142. export default CompositePositionProperty;