智慧水务管理系统 - 精河县供水工程综合管理平台

CallbackPositionProperty.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import defined from "../Core/defined.js";
  2. import DeveloperError from "../Core/DeveloperError.js";
  3. import Event from "../Core/Event.js";
  4. import JulianDate from "../Core/JulianDate.js";
  5. import ReferenceFrame from "../Core/ReferenceFrame.js";
  6. import PositionProperty from "./PositionProperty.js";
  7. /**
  8. * A {@link PositionProperty} whose value is lazily evaluated by a callback function.
  9. *
  10. * @alias CallbackPositionProperty
  11. * @constructor
  12. *
  13. * @param {CallbackPositionProperty.Callback} callback The function to be called when the position property is evaluated.
  14. * @param {boolean} isConstant <code>true</code> when the callback function returns the same value every time, <code>false</code> if the value will change.
  15. * @param {ReferenceFrame} [referenceFrame=ReferenceFrame.FIXED] The reference frame in which the position is defined.
  16. *
  17. * @demo {@link https://sandcastle.cesium.com/index.html?id=callback-position-property|Cesium Sandcastle Callback Position Property Demo}
  18. */
  19. function CallbackPositionProperty(callback, isConstant, referenceFrame) {
  20. this._callback = undefined;
  21. this._isConstant = undefined;
  22. this._referenceFrame = referenceFrame ?? ReferenceFrame.FIXED;
  23. this._definitionChanged = new Event();
  24. this.setCallback(callback, isConstant);
  25. }
  26. Object.defineProperties(CallbackPositionProperty.prototype, {
  27. /**
  28. * Gets a value indicating if this property is constant.
  29. * @memberof CallbackPositionProperty.prototype
  30. *
  31. * @type {boolean}
  32. * @readonly
  33. */
  34. isConstant: {
  35. get: function () {
  36. return this._isConstant;
  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 CallbackPositionProperty.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 CallbackPositionProperty.prototype
  56. * @type {ReferenceFrame}
  57. * @default ReferenceFrame.FIXED;
  58. */
  59. referenceFrame: {
  60. get: function () {
  61. return this._referenceFrame;
  62. },
  63. },
  64. });
  65. const timeScratch = new JulianDate();
  66. /**
  67. * Gets the value of the property at the provided time in the fixed frame.
  68. *
  69. * @param {JulianDate} [time=JulianDate.now()] The time for which to retrieve the value. If omitted, the current system time is used.
  70. * @param {Cartesian3} [result] The object to store the value into, if omitted, a new instance is created and returned.
  71. * @returns {Cartesian3 | undefined} The modified result parameter or a new instance if the result parameter was not supplied.
  72. */
  73. CallbackPositionProperty.prototype.getValue = function (time, result) {
  74. if (!defined(time)) {
  75. time = JulianDate.now(timeScratch);
  76. }
  77. return this.getValueInReferenceFrame(time, ReferenceFrame.FIXED, result);
  78. };
  79. /**
  80. * Sets the callback to be used.
  81. *
  82. * @param {CallbackPositionProperty.Callback} callback The function to be called when the property is evaluated.
  83. * @param {boolean} isConstant <code>true</code> when the callback function returns the same value every time, <code>false</code> if the value will change.
  84. */
  85. CallbackPositionProperty.prototype.setCallback = function (
  86. callback,
  87. isConstant,
  88. ) {
  89. //>>includeStart('debug', pragmas.debug);
  90. if (!defined(callback)) {
  91. throw new DeveloperError("callback is required.");
  92. }
  93. if (!defined(isConstant)) {
  94. throw new DeveloperError("isConstant is required.");
  95. }
  96. //>>includeEnd('debug');
  97. const changed =
  98. this._callback !== callback || this._isConstant !== isConstant;
  99. this._callback = callback;
  100. this._isConstant = isConstant;
  101. if (changed) {
  102. this._definitionChanged.raiseEvent(this);
  103. }
  104. };
  105. /**
  106. * Gets the value of the property at the provided time and in the provided reference frame.
  107. *
  108. * @param {JulianDate} time The time for which to retrieve the value.
  109. * @param {ReferenceFrame} referenceFrame The desired referenceFrame of the result.
  110. * @param {Cartesian3} [result] The object to store the value into, if omitted, a new instance is created and returned.
  111. * @returns {Cartesian3 | undefined} The modified result parameter or a new instance if the result parameter was not supplied.
  112. */
  113. CallbackPositionProperty.prototype.getValueInReferenceFrame = function (
  114. time,
  115. referenceFrame,
  116. result,
  117. ) {
  118. //>>includeStart('debug', pragmas.debug);
  119. if (!defined(time)) {
  120. throw new DeveloperError("time is required.");
  121. }
  122. if (!defined(referenceFrame)) {
  123. throw new DeveloperError("referenceFrame is required.");
  124. }
  125. //>>includeEnd('debug');
  126. const value = this._callback(time, result);
  127. return PositionProperty.convertToReferenceFrame(
  128. time,
  129. value,
  130. this._referenceFrame,
  131. referenceFrame,
  132. result,
  133. );
  134. };
  135. /**
  136. * Compares this property to the provided property and returns
  137. * <code>true</code> if they are equal, <code>false</code> otherwise.
  138. *
  139. * @param {Property} [other] The other property.
  140. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  141. */
  142. CallbackPositionProperty.prototype.equals = function (other) {
  143. return (
  144. this === other ||
  145. (other instanceof CallbackPositionProperty &&
  146. this._callback === other._callback &&
  147. this._isConstant === other._isConstant &&
  148. this._referenceFrame === other._referenceFrame)
  149. );
  150. };
  151. /**
  152. * A function that returns the value of the position property.
  153. * @callback CallbackPositionProperty.Callback
  154. *
  155. * @param {JulianDate} [time=JulianDate.now()] The time for which to retrieve the value. If omitted, the current system time is used.
  156. * @param {Cartesian3} [result] The object to store the value into. If omitted, the function must create and return a new instance.
  157. * @returns {Cartesian3 | undefined} The modified result parameter, or a new instance if the result parameter was not supplied or is unsupported.
  158. */
  159. export default CallbackPositionProperty;