PositionPropertyArray.js 6.5 KB

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