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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import Cartesian3 from "../Core/Cartesian3.js";
  2. import Check from "../Core/Check.js";
  3. import defined from "../Core/defined.js";
  4. import MetadataType from "./MetadataType.js";
  5. import OrientedBoundingBox from "../Core/OrientedBoundingBox.js";
  6. /**
  7. * A cell from a {@link VoxelPrimitive}.
  8. * <p>
  9. * Provides access to properties associated with one cell of a voxel primitive.
  10. * </p>
  11. * <p>
  12. * Do not construct this directly. Access it through picking using {@link Scene#pickVoxel}.
  13. * </p>
  14. *
  15. * @alias VoxelCell
  16. * @constructor
  17. *
  18. * @param {VoxelPrimitive} primitive The voxel primitive containing the cell
  19. * @param {number} tileIndex The index of the tile
  20. * @param {number} sampleIndex The index of the sample within the tile, containing metadata for this cell
  21. *
  22. * @example
  23. * // On left click, display all the properties for a voxel cell in the console log.
  24. * handler.setInputAction(function(movement) {
  25. * const voxelCell = scene.pickVoxel(movement.position);
  26. * if (voxelCell instanceof Cesium.VoxelCell) {
  27. * const propertyIds = voxelCell.getPropertyIds();
  28. * const length = propertyIds.length;
  29. * for (let i = 0; i < length; ++i) {
  30. * const propertyId = propertyIds[i];
  31. * console.log(`{propertyId}: ${voxelCell.getProperty(propertyId)}`);
  32. * }
  33. * }
  34. * }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  35. *
  36. * @experimental This feature is not final and is subject to change without Cesium's standard deprecation policy.
  37. */
  38. function VoxelCell(primitive, tileIndex, sampleIndex) {
  39. this._primitive = primitive;
  40. this._tileIndex = tileIndex;
  41. this._sampleIndex = sampleIndex;
  42. this._metadata = {};
  43. this._orientedBoundingBox = new OrientedBoundingBox();
  44. }
  45. /**
  46. * Construct a VoxelCell, and update the metadata and bounding box using the properties
  47. * of a supplied keyframe node.
  48. *
  49. * @private
  50. * @param {VoxelPrimitive} primitive The voxel primitive containing the cell.
  51. * @param {number} tileIndex The index of the tile.
  52. * @param {number} sampleIndex The index of the sample within the tile, containing metadata for this cell.
  53. * @param {KeyframeNode} keyframeNode The keyframe node containing information about the tile.
  54. * @returns {VoxelCell}
  55. *
  56. * @experimental This feature is not final and is subject to change without Cesium's standard deprecation policy.
  57. */
  58. VoxelCell.fromKeyframeNode = function (
  59. primitive,
  60. tileIndex,
  61. sampleIndex,
  62. keyframeNode,
  63. ) {
  64. //>>includeStart('debug', pragmas.debug);
  65. Check.typeOf.object("primitive", primitive);
  66. Check.typeOf.number("tileIndex", tileIndex);
  67. Check.typeOf.number("sampleIndex", sampleIndex);
  68. Check.typeOf.object("keyframeNode", keyframeNode);
  69. //>>includeEnd('debug');
  70. const voxelCell = new VoxelCell(primitive, tileIndex, sampleIndex);
  71. const { spatialNode, content } = keyframeNode;
  72. voxelCell._metadata = getMetadataForSample(primitive, content, sampleIndex);
  73. voxelCell._orientedBoundingBox = getOrientedBoundingBox(
  74. primitive,
  75. spatialNode,
  76. sampleIndex,
  77. voxelCell._orientedBoundingBox,
  78. );
  79. return voxelCell;
  80. };
  81. /**
  82. * @private
  83. * @param {VoxelPrimitive} primitive
  84. * @param {VoxelContent} content
  85. * @param {number} sampleIndex
  86. * @returns {object}
  87. */
  88. function getMetadataForSample(primitive, content, sampleIndex) {
  89. if (!defined(content) || !defined(content.metadata)) {
  90. return undefined;
  91. }
  92. const { names, types } = primitive.provider;
  93. const { metadata } = content;
  94. const metadataMap = {};
  95. for (let i = 0; i < names.length; i++) {
  96. const name = names[i];
  97. const componentCount = MetadataType.getComponentCount(types[i]);
  98. const samples = metadata[i].slice(
  99. sampleIndex * componentCount,
  100. (sampleIndex + 1) * componentCount,
  101. );
  102. metadataMap[name] = samples;
  103. }
  104. return metadataMap;
  105. }
  106. const tileCoordinateScratch = new Cartesian3();
  107. const tileUvScratch = new Cartesian3();
  108. /**
  109. * @private
  110. * @param {VoxelPrimitive} primitive
  111. * @param {SpatialNode} spatialNode
  112. * @param {OrientedBoundingBox} result
  113. * @returns {OrientedBoundingBox}
  114. */
  115. function getOrientedBoundingBox(primitive, spatialNode, sampleIndex, result) {
  116. // Convert the sample index into a 3D tile coordinate
  117. // Note: dimensions from the spatialNode include padding
  118. const paddedDimensions = spatialNode.dimensions;
  119. const sliceSize = paddedDimensions.x * paddedDimensions.y;
  120. const zIndex = Math.floor(sampleIndex / sliceSize);
  121. const indexInSlice = sampleIndex - zIndex * sliceSize;
  122. const yIndex = Math.floor(indexInSlice / paddedDimensions.x);
  123. const xIndex = indexInSlice - yIndex * paddedDimensions.x;
  124. const tileCoordinate = Cartesian3.fromElements(
  125. xIndex,
  126. yIndex,
  127. zIndex,
  128. tileCoordinateScratch,
  129. );
  130. // Remove padding, and convert to a fraction in [0, 1], where the limits are
  131. // the unpadded bounds of the tile
  132. const tileUv = Cartesian3.divideComponents(
  133. Cartesian3.subtract(
  134. tileCoordinate,
  135. primitive._paddingBefore,
  136. tileCoordinateScratch,
  137. ),
  138. primitive.dimensions,
  139. tileUvScratch,
  140. );
  141. const shape = primitive._shape;
  142. return shape.computeOrientedBoundingBoxForSample(
  143. spatialNode,
  144. primitive.dimensions,
  145. tileUv,
  146. result,
  147. );
  148. }
  149. Object.defineProperties(VoxelCell.prototype, {
  150. /**
  151. * Gets an object of the metadata values for this cell. The object's keys are the metadata names.
  152. *
  153. * @memberof VoxelCell.prototype
  154. *
  155. * @type {object}
  156. *
  157. * @readonly
  158. * @private
  159. */
  160. metadata: {
  161. get: function () {
  162. return this._metadata;
  163. },
  164. },
  165. /**
  166. * All objects returned by {@link Scene#pick} have a <code>primitive</code> property. This returns
  167. * the VoxelPrimitive containing the cell.
  168. *
  169. * @memberof VoxelCell.prototype
  170. *
  171. * @type {VoxelPrimitive}
  172. *
  173. * @readonly
  174. */
  175. primitive: {
  176. get: function () {
  177. return this._primitive;
  178. },
  179. },
  180. /**
  181. * Get the sample index of the cell.
  182. *
  183. * @memberof VoxelCell.prototype
  184. *
  185. * @type {number}
  186. *
  187. * @readonly
  188. */
  189. sampleIndex: {
  190. get: function () {
  191. return this._sampleIndex;
  192. },
  193. },
  194. /**
  195. * Get the index of the tile containing the cell.
  196. *
  197. * @memberof VoxelCell.prototype
  198. *
  199. * @type {number}
  200. *
  201. * @readonly
  202. */
  203. tileIndex: {
  204. get: function () {
  205. return this._tileIndex;
  206. },
  207. },
  208. /**
  209. * Get a copy of the oriented bounding box containing the cell.
  210. *
  211. * @memberof VoxelCell.prototype
  212. *
  213. * @type {OrientedBoundingBox}
  214. *
  215. * @readonly
  216. */
  217. orientedBoundingBox: {
  218. get: function () {
  219. return this._orientedBoundingBox.clone();
  220. },
  221. },
  222. });
  223. /**
  224. * Returns <code>true</code> if the feature contains this property.
  225. *
  226. * @param {string} name The case-sensitive name of the property.
  227. * @returns {boolean} Whether the feature contains this property.
  228. */
  229. VoxelCell.prototype.hasProperty = function (name) {
  230. return defined(this._metadata[name]);
  231. };
  232. /**
  233. * Returns an array of metadata property names for the feature.
  234. *
  235. * @returns {string[]} The IDs of the feature's properties.
  236. */
  237. VoxelCell.prototype.getNames = function () {
  238. return Object.keys(this._metadata);
  239. };
  240. /**
  241. * Returns a copy of the value of the metadata in the cell with the given name.
  242. *
  243. * @param {string} name The case-sensitive name of the property.
  244. * @returns {*} The value of the property or <code>undefined</code> if the feature does not have this property.
  245. *
  246. * @example
  247. * // Display all the properties for a voxel cell in the console log.
  248. * const names = voxelCell.getNames();
  249. * for (let i = 0; i < names.length; ++i) {
  250. * const name = names[i];
  251. * console.log(`{name}: ${voxelCell.getProperty(name)}`);
  252. * }
  253. */
  254. VoxelCell.prototype.getProperty = function (name) {
  255. return this._metadata[name];
  256. };
  257. export default VoxelCell;