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

DebugModelMatrixPrimitive.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import ArcType from "../Core/ArcType.js";
  2. import Cartesian3 from "../Core/Cartesian3.js";
  3. import Color from "../Core/Color.js";
  4. import Frozen from "../Core/Frozen.js";
  5. import defined from "../Core/defined.js";
  6. import destroyObject from "../Core/destroyObject.js";
  7. import GeometryInstance from "../Core/GeometryInstance.js";
  8. import Matrix4 from "../Core/Matrix4.js";
  9. import PolylineGeometry from "../Core/PolylineGeometry.js";
  10. import PolylineColorAppearance from "./PolylineColorAppearance.js";
  11. import Primitive from "./Primitive.js";
  12. /**
  13. * Draws the axes of a reference frame defined by a matrix that transforms to world
  14. * coordinates, i.e., Earth's WGS84 coordinates. The most prominent example is
  15. * a primitives <code>modelMatrix</code>.
  16. * <p>
  17. * The X axis is red; Y is green; and Z is blue.
  18. * </p>
  19. * <p>
  20. * This is for debugging only; it is not optimized for production use.
  21. * </p>
  22. *
  23. * @alias DebugModelMatrixPrimitive
  24. * @constructor
  25. *
  26. * @param {object} [options] Object with the following properties:
  27. * @param {number} [options.length=10000000.0] The length of the axes in meters.
  28. * @param {number} [options.width=2.0] The width of the axes in pixels.
  29. * @param {Matrix4} [options.modelMatrix=Matrix4.IDENTITY] The 4x4 matrix that defines the reference frame, i.e., origin plus axes, to visualize.
  30. * @param {boolean} [options.show=true] Determines if this primitive will be shown.
  31. * @param {object} [options.id] A user-defined object to return when the instance is picked with {@link Scene#pick}
  32. *
  33. * @example
  34. * primitives.add(new Cesium.DebugModelMatrixPrimitive({
  35. * modelMatrix : primitive.modelMatrix, // primitive to debug
  36. * length : 100000.0,
  37. * width : 10.0
  38. * }));
  39. */
  40. function DebugModelMatrixPrimitive(options) {
  41. options = options ?? Frozen.EMPTY_OBJECT;
  42. /**
  43. * The length of the axes in meters.
  44. *
  45. * @type {number}
  46. * @default 10000000.0
  47. */
  48. this.length = options.length ?? 10000000.0;
  49. this._length = undefined;
  50. /**
  51. * The width of the axes in pixels.
  52. *
  53. * @type {number}
  54. * @default 2.0
  55. */
  56. this.width = options.width ?? 2.0;
  57. this._width = undefined;
  58. /**
  59. * Determines if this primitive will be shown.
  60. *
  61. * @type {boolean}
  62. * @default true
  63. */
  64. this.show = options.show ?? true;
  65. /**
  66. * The 4x4 matrix that defines the reference frame, i.e., origin plus axes, to visualize.
  67. *
  68. * @type {Matrix4}
  69. * @default {@link Matrix4.IDENTITY}
  70. */
  71. this.modelMatrix = Matrix4.clone(options.modelMatrix ?? Matrix4.IDENTITY);
  72. this._modelMatrix = new Matrix4();
  73. /**
  74. * User-defined value returned when the primitive is picked.
  75. *
  76. * @type {*}
  77. * @default undefined
  78. *
  79. * @see Scene#pick
  80. */
  81. this.id = options.id;
  82. this._id = undefined;
  83. this._primitive = undefined;
  84. }
  85. /**
  86. * @private
  87. */
  88. DebugModelMatrixPrimitive.prototype.update = function (frameState) {
  89. if (!this.show) {
  90. return;
  91. }
  92. if (
  93. !defined(this._primitive) ||
  94. !Matrix4.equals(this._modelMatrix, this.modelMatrix) ||
  95. this._length !== this.length ||
  96. this._width !== this.width ||
  97. this._id !== this.id
  98. ) {
  99. this._modelMatrix = Matrix4.clone(this.modelMatrix, this._modelMatrix);
  100. this._length = this.length;
  101. this._width = this.width;
  102. this._id = this.id;
  103. if (defined(this._primitive)) {
  104. this._primitive.destroy();
  105. }
  106. // Workaround projecting (0, 0, 0)
  107. if (
  108. this.modelMatrix[12] === 0.0 &&
  109. this.modelMatrix[13] === 0.0 &&
  110. this.modelMatrix[14] === 0.0
  111. ) {
  112. this.modelMatrix[14] = 0.01;
  113. }
  114. const x = new GeometryInstance({
  115. geometry: new PolylineGeometry({
  116. positions: [Cartesian3.ZERO, Cartesian3.UNIT_X],
  117. width: this.width,
  118. vertexFormat: PolylineColorAppearance.VERTEX_FORMAT,
  119. colors: [Color.RED, Color.RED],
  120. arcType: ArcType.NONE,
  121. }),
  122. modelMatrix: Matrix4.multiplyByUniformScale(
  123. this.modelMatrix,
  124. this.length,
  125. new Matrix4(),
  126. ),
  127. id: this.id,
  128. pickPrimitive: this,
  129. });
  130. const y = new GeometryInstance({
  131. geometry: new PolylineGeometry({
  132. positions: [Cartesian3.ZERO, Cartesian3.UNIT_Y],
  133. width: this.width,
  134. vertexFormat: PolylineColorAppearance.VERTEX_FORMAT,
  135. colors: [Color.GREEN, Color.GREEN],
  136. arcType: ArcType.NONE,
  137. }),
  138. modelMatrix: Matrix4.multiplyByUniformScale(
  139. this.modelMatrix,
  140. this.length,
  141. new Matrix4(),
  142. ),
  143. id: this.id,
  144. pickPrimitive: this,
  145. });
  146. const z = new GeometryInstance({
  147. geometry: new PolylineGeometry({
  148. positions: [Cartesian3.ZERO, Cartesian3.UNIT_Z],
  149. width: this.width,
  150. vertexFormat: PolylineColorAppearance.VERTEX_FORMAT,
  151. colors: [Color.BLUE, Color.BLUE],
  152. arcType: ArcType.NONE,
  153. }),
  154. modelMatrix: Matrix4.multiplyByUniformScale(
  155. this.modelMatrix,
  156. this.length,
  157. new Matrix4(),
  158. ),
  159. id: this.id,
  160. pickPrimitive: this,
  161. });
  162. this._primitive = new Primitive({
  163. geometryInstances: [x, y, z],
  164. appearance: new PolylineColorAppearance(),
  165. asynchronous: false,
  166. });
  167. }
  168. this._primitive.update(frameState);
  169. };
  170. /**
  171. * Returns true if this object was destroyed; otherwise, false.
  172. * <p>
  173. * If this object was destroyed, it should not be used; calling any function other than
  174. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  175. * </p>
  176. *
  177. * @returns {boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  178. *
  179. * @see DebugModelMatrixPrimitive#destroy
  180. */
  181. DebugModelMatrixPrimitive.prototype.isDestroyed = function () {
  182. return false;
  183. };
  184. /**
  185. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  186. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  187. * <p>
  188. * Once an object is destroyed, it should not be used; calling any function other than
  189. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  190. * assign the return value (<code>undefined</code>) to the object as done in the example.
  191. * </p>
  192. *
  193. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  194. *
  195. * @example
  196. * p = p && p.destroy();
  197. *
  198. * @see DebugModelMatrixPrimitive#isDestroyed
  199. */
  200. DebugModelMatrixPrimitive.prototype.destroy = function () {
  201. this._primitive = this._primitive && this._primitive.destroy();
  202. return destroyObject(this);
  203. };
  204. export default DebugModelMatrixPrimitive;