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

TerrainOffsetProperty.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import Cartesian3 from "../Core/Cartesian3.js";
  2. import Cartographic from "../Core/Cartographic.js";
  3. import Check from "../Core/Check.js";
  4. import defined from "../Core/defined.js";
  5. import destroyObject from "../Core/destroyObject.js";
  6. import Event from "../Core/Event.js";
  7. import Iso8601 from "../Core/Iso8601.js";
  8. import JulianDate from "../Core/JulianDate.js";
  9. import CesiumMath from "../Core/Math.js";
  10. import HeightReference, {
  11. isHeightReferenceRelative,
  12. } from "../Scene/HeightReference.js";
  13. import Property from "./Property.js";
  14. const scratchPosition = new Cartesian3();
  15. /**
  16. * @private
  17. */
  18. function TerrainOffsetProperty(
  19. scene,
  20. positionProperty,
  21. heightReferenceProperty,
  22. extrudedHeightReferenceProperty,
  23. ) {
  24. //>>includeStart('debug', pragmas.debug);
  25. Check.defined("scene", scene);
  26. Check.defined("positionProperty", positionProperty);
  27. //>>includeEnd('debug');
  28. this._scene = scene;
  29. this._heightReference = heightReferenceProperty;
  30. this._extrudedHeightReference = extrudedHeightReferenceProperty;
  31. this._positionProperty = positionProperty;
  32. this._position = new Cartesian3();
  33. this._cartographicPosition = new Cartographic();
  34. this._normal = new Cartesian3();
  35. this._definitionChanged = new Event();
  36. this._terrainHeight = 0;
  37. this._removeCallbackFunc = undefined;
  38. this._removeEventListener = undefined;
  39. this._removeModeListener = undefined;
  40. const that = this;
  41. if (defined(scene.globe)) {
  42. this._removeEventListener = scene.terrainProviderChanged.addEventListener(
  43. function () {
  44. that._updateClamping();
  45. },
  46. );
  47. this._removeModeListener = scene.morphComplete.addEventListener(
  48. function () {
  49. that._updateClamping();
  50. },
  51. );
  52. }
  53. if (positionProperty.isConstant) {
  54. const position = positionProperty.getValue(
  55. Iso8601.MINIMUM_VALUE,
  56. scratchPosition,
  57. );
  58. if (
  59. !defined(position) ||
  60. Cartesian3.equals(position, Cartesian3.ZERO) ||
  61. !defined(scene.globe)
  62. ) {
  63. return;
  64. }
  65. this._position = Cartesian3.clone(position, this._position);
  66. this._updateClamping();
  67. this._normal = scene.ellipsoid.geodeticSurfaceNormal(
  68. position,
  69. this._normal,
  70. );
  71. }
  72. }
  73. Object.defineProperties(TerrainOffsetProperty.prototype, {
  74. /**
  75. * Gets a value indicating if this property is constant.
  76. * @memberof TerrainOffsetProperty.prototype
  77. *
  78. * @type {boolean}
  79. * @readonly
  80. */
  81. isConstant: {
  82. get: function () {
  83. return false;
  84. },
  85. },
  86. /**
  87. * Gets the event that is raised whenever the definition of this property changes.
  88. * @memberof TerrainOffsetProperty.prototype
  89. *
  90. * @type {Event}
  91. * @readonly
  92. */
  93. definitionChanged: {
  94. get: function () {
  95. return this._definitionChanged;
  96. },
  97. },
  98. });
  99. /**
  100. * @private
  101. */
  102. TerrainOffsetProperty.prototype._updateClamping = function () {
  103. if (defined(this._removeCallbackFunc)) {
  104. this._removeCallbackFunc();
  105. }
  106. const scene = this._scene;
  107. const position = this._position;
  108. if (Cartesian3.equals(position, Cartesian3.ZERO)) {
  109. this._terrainHeight = 0;
  110. return;
  111. }
  112. const ellipsoid = scene.ellipsoid;
  113. const cartographicPosition = ellipsoid.cartesianToCartographic(
  114. position,
  115. this._cartographicPosition,
  116. );
  117. const height = scene.getHeight(cartographicPosition, this._heightReference);
  118. if (defined(height)) {
  119. this._terrainHeight = height;
  120. } else {
  121. this._terrainHeight = 0;
  122. }
  123. const updateFunction = (clampedPosition) => {
  124. this._terrainHeight = clampedPosition.height;
  125. this.definitionChanged.raiseEvent();
  126. };
  127. this._removeCallbackFunc = scene.updateHeight(
  128. cartographicPosition,
  129. updateFunction,
  130. this._heightReference,
  131. );
  132. };
  133. const timeScratch = new JulianDate();
  134. /**
  135. * Gets the height relative to the terrain based on the positions.
  136. *
  137. * @param {JulianDate} [time=JulianDate.now()] The time for which to retrieve the value. If omitted, the current system time is used.
  138. * @param {object} [result] The object to store the value into, if omitted, a new instance is created and returned.
  139. * @returns {Cartesian3} The offset
  140. */
  141. TerrainOffsetProperty.prototype.getValue = function (time, result) {
  142. if (!defined(time)) {
  143. time = JulianDate.now(timeScratch);
  144. }
  145. const heightReference = Property.getValueOrDefault(
  146. this._heightReference,
  147. time,
  148. HeightReference.NONE,
  149. );
  150. const extrudedHeightReference = Property.getValueOrDefault(
  151. this._extrudedHeightReference,
  152. time,
  153. HeightReference.NONE,
  154. );
  155. if (
  156. heightReference === HeightReference.NONE &&
  157. !isHeightReferenceRelative(extrudedHeightReference)
  158. ) {
  159. this._position = Cartesian3.clone(Cartesian3.ZERO, this._position);
  160. return Cartesian3.clone(Cartesian3.ZERO, result);
  161. }
  162. if (this._positionProperty.isConstant) {
  163. return Cartesian3.multiplyByScalar(
  164. this._normal,
  165. this._terrainHeight,
  166. result,
  167. );
  168. }
  169. const scene = this._scene;
  170. const position = this._positionProperty.getValue(time, scratchPosition);
  171. if (
  172. !defined(position) ||
  173. Cartesian3.equals(position, Cartesian3.ZERO) ||
  174. !defined(scene.globe)
  175. ) {
  176. return Cartesian3.clone(Cartesian3.ZERO, result);
  177. }
  178. if (
  179. Cartesian3.equalsEpsilon(this._position, position, CesiumMath.EPSILON10)
  180. ) {
  181. return Cartesian3.multiplyByScalar(
  182. this._normal,
  183. this._terrainHeight,
  184. result,
  185. );
  186. }
  187. this._position = Cartesian3.clone(position, this._position);
  188. this._updateClamping();
  189. const normal = scene.ellipsoid.geodeticSurfaceNormal(position, this._normal);
  190. return Cartesian3.multiplyByScalar(normal, this._terrainHeight, result);
  191. };
  192. TerrainOffsetProperty.prototype.isDestroyed = function () {
  193. return false;
  194. };
  195. TerrainOffsetProperty.prototype.destroy = function () {
  196. if (defined(this._removeEventListener)) {
  197. this._removeEventListener();
  198. }
  199. if (defined(this._removeModeListener)) {
  200. this._removeModeListener();
  201. }
  202. if (defined(this._removeCallbackFunc)) {
  203. this._removeCallbackFunc();
  204. }
  205. return destroyObject(this);
  206. };
  207. /**
  208. * A function which creates one or more providers.
  209. * @callback TerrainOffsetProperty.PositionFunction
  210. * @param {JulianDate} time The clock time at which to retrieve the position
  211. * @param {Cartesian3} result The result position
  212. * @returns {Cartesian3} The position at which to do the terrain height check
  213. */
  214. export default TerrainOffsetProperty;