PositionPropertyArray.js 6.4 KB

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