PositionPropertyArray.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 EventHelper from "../Core/EventHelper.js";
  6. import ReferenceFrame from "../Core/ReferenceFrame.js";
  7. import Property from "./Property.js";
  8. /**
  9. * A {@link Property} whose value is an array whose items are the computed value
  10. * of other PositionProperty instances.
  11. *
  12. * @alias PositionPropertyArray
  13. * @constructor
  14. *
  15. * @param {Property[]} [value] An array of Property instances.
  16. * @param {ReferenceFrame} [referenceFrame=ReferenceFrame.FIXED] The reference frame in which the position is defined.
  17. */
  18. function PositionPropertyArray(value, referenceFrame) {
  19. this._value = undefined;
  20. this._definitionChanged = new Event();
  21. this._eventHelper = new EventHelper();
  22. this._referenceFrame = defaultValue(referenceFrame, ReferenceFrame.FIXED);
  23. this.setValue(value);
  24. }
  25. Object.defineProperties(PositionPropertyArray.prototype, {
  26. /**
  27. * Gets a value indicating if this property is constant. This property
  28. * is considered constant if all property items in the array are constant.
  29. * @memberof PositionPropertyArray.prototype
  30. *
  31. * @type {Boolean}
  32. * @readonly
  33. */
  34. isConstant: {
  35. get: function () {
  36. const value = this._value;
  37. if (!defined(value)) {
  38. return true;
  39. }
  40. const length = value.length;
  41. for (let i = 0; i < length; i++) {
  42. if (!Property.isConstant(value[i])) {
  43. return false;
  44. }
  45. }
  46. return true;
  47. },
  48. },
  49. /**
  50. * Gets the event that is raised whenever the definition of this property changes.
  51. * The definition is changed whenever setValue is called with data different
  52. * than the current value or one of the properties in the array also changes.
  53. * @memberof PositionPropertyArray.prototype
  54. *
  55. * @type {Event}
  56. * @readonly
  57. */
  58. definitionChanged: {
  59. get: function () {
  60. return this._definitionChanged;
  61. },
  62. },
  63. /**
  64. * Gets the reference frame in which the position is defined.
  65. * @memberof PositionPropertyArray.prototype
  66. * @type {ReferenceFrame}
  67. * @default ReferenceFrame.FIXED;
  68. */
  69. referenceFrame: {
  70. get: function () {
  71. return this._referenceFrame;
  72. },
  73. },
  74. });
  75. /**
  76. * Gets the value of the property.
  77. *
  78. * @param {JulianDate} time The time for which to retrieve the value.
  79. * @param {Cartesian3[]} [result] The object to store the value into, if omitted, a new instance is created and returned.
  80. * @returns {Cartesian3[]} The modified result parameter or a new instance if the result parameter was not supplied.
  81. */
  82. PositionPropertyArray.prototype.getValue = function (time, result) {
  83. return this.getValueInReferenceFrame(time, ReferenceFrame.FIXED, result);
  84. };
  85. /**
  86. * Gets the value of the property at the provided time and in the provided reference frame.
  87. *
  88. * @param {JulianDate} time The time for which to retrieve the value.
  89. * @param {ReferenceFrame} referenceFrame The desired referenceFrame of the result.
  90. * @param {Cartesian3[]} [result] The object to store the value into, if omitted, a new instance is created and returned.
  91. * @returns {Cartesian3[]} The modified result parameter or a new instance if the result parameter was not supplied.
  92. */
  93. PositionPropertyArray.prototype.getValueInReferenceFrame = function (
  94. time,
  95. referenceFrame,
  96. result
  97. ) {
  98. //>>includeStart('debug', pragmas.debug);
  99. if (!defined(time)) {
  100. throw new DeveloperError("time is required.");
  101. }
  102. if (!defined(referenceFrame)) {
  103. throw new DeveloperError("referenceFrame is required.");
  104. }
  105. //>>includeEnd('debug');
  106. const value = this._value;
  107. if (!defined(value)) {
  108. return undefined;
  109. }
  110. const length = value.length;
  111. if (!defined(result)) {
  112. result = new Array(length);
  113. }
  114. let i = 0;
  115. let x = 0;
  116. while (i < length) {
  117. const property = value[i];
  118. const itemValue = property.getValueInReferenceFrame(
  119. time,
  120. referenceFrame,
  121. result[i]
  122. );
  123. if (defined(itemValue)) {
  124. result[x] = itemValue;
  125. x++;
  126. }
  127. i++;
  128. }
  129. result.length = x;
  130. return result;
  131. };
  132. /**
  133. * Sets the value of the property.
  134. *
  135. * @param {Property[]} value An array of Property instances.
  136. */
  137. PositionPropertyArray.prototype.setValue = function (value) {
  138. const eventHelper = this._eventHelper;
  139. eventHelper.removeAll();
  140. if (defined(value)) {
  141. this._value = value.slice();
  142. const length = value.length;
  143. for (let i = 0; i < length; i++) {
  144. const property = value[i];
  145. if (defined(property)) {
  146. eventHelper.add(
  147. property.definitionChanged,
  148. PositionPropertyArray.prototype._raiseDefinitionChanged,
  149. this
  150. );
  151. }
  152. }
  153. } else {
  154. this._value = undefined;
  155. }
  156. this._definitionChanged.raiseEvent(this);
  157. };
  158. /**
  159. * Compares this property to the provided property and returns
  160. * <code>true</code> if they are equal, <code>false</code> otherwise.
  161. *
  162. * @param {Property} [other] The other property.
  163. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  164. */
  165. PositionPropertyArray.prototype.equals = function (other) {
  166. return (
  167. this === other || //
  168. (other instanceof PositionPropertyArray && //
  169. this._referenceFrame === other._referenceFrame && //
  170. Property.arrayEquals(this._value, other._value))
  171. );
  172. };
  173. PositionPropertyArray.prototype._raiseDefinitionChanged = function () {
  174. this._definitionChanged.raiseEvent(this);
  175. };
  176. export default PositionPropertyArray;