ConstantPositionProperty.js 4.8 KB

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