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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. import Check from "../Core/Check.js";
  2. import ComponentDatatype from "../Core/ComponentDatatype.js";
  3. import defined from "../Core/defined.js";
  4. import deprecationWarning from "../Core/deprecationWarning.js";
  5. import DeveloperError from "../Core/DeveloperError.js";
  6. import RuntimeError from "../Core/RuntimeError.js";
  7. import BatchTableHierarchy from "./BatchTableHierarchy.js";
  8. import StructuralMetadata from "./StructuralMetadata.js";
  9. import PropertyAttribute from "./PropertyAttribute.js";
  10. import PropertyTable from "./PropertyTable.js";
  11. import getBinaryAccessor from "./getBinaryAccessor.js";
  12. import JsonMetadataTable from "./JsonMetadataTable.js";
  13. import MetadataClass from "./MetadataClass.js";
  14. import MetadataSchema from "./MetadataSchema.js";
  15. import MetadataTable from "./MetadataTable.js";
  16. import ModelComponents from "./ModelComponents.js";
  17. import ModelUtility from "./Model/ModelUtility.js";
  18. import oneTimeWarning from "../Core/oneTimeWarning.js";
  19. /**
  20. * An object that parses the the 3D Tiles 1.0 batch table and transcodes it to
  21. * be compatible with structural metadata from the <code>EXT_structural_metadata</code> glTF extension
  22. * <p>
  23. * See the {@link https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_structural_metadata|EXT_structural_metadata Extension} for glTF.
  24. * </p>
  25. *
  26. * @param {object} options Object with the following properties:
  27. * @param {number} options.count The number of features in the batch table.
  28. * @param {object} options.batchTable The batch table JSON
  29. * @param {Uint8Array} [options.binaryBody] The batch table binary body
  30. * @param {boolean} [options.parseAsPropertyAttributes=false] If true, binary properties are parsed as property attributes instead of a property table. This is used for .pnts models for GPU styling.
  31. * @param {ModelComponents.Attribute[]} [options.customAttributeOutput] Pass in an empty array here and this method will populate it with a list of custom attributes that will store the values of the property attributes. The attributes will be created with typed arrays, the caller is responsible for uploading them to the GPU. This option is required when options.parseAsPropertyAttributes is true.
  32. * @return {StructuralMetadata} A transcoded structural metadata object
  33. *
  34. * @private
  35. * @experimental This feature is using part of the 3D Tiles spec that is not final and is subject to change without Cesium's standard deprecation policy.
  36. */
  37. function parseBatchTable(options) {
  38. //>>includeStart('debug', pragmas.debug);
  39. Check.typeOf.number("options.count", options.count);
  40. Check.typeOf.object("options.batchTable", options.batchTable);
  41. //>>includeEnd('debug');
  42. const featureCount = options.count;
  43. const batchTable = options.batchTable;
  44. const binaryBody = options.binaryBody;
  45. const parseAsPropertyAttributes = options.parseAsPropertyAttributes ?? false;
  46. const customAttributeOutput = options.customAttributeOutput;
  47. //>>includeStart('debug', pragmas.debug);
  48. if (parseAsPropertyAttributes && !defined(customAttributeOutput)) {
  49. throw new DeveloperError(
  50. "customAttributeOutput is required when parsing batch table as property attributes",
  51. );
  52. }
  53. //>>includeEnd('debug');
  54. // divide properties into binary, json and hierarchy
  55. const partitionResults = partitionProperties(batchTable);
  56. let jsonMetadataTable;
  57. if (defined(partitionResults.jsonProperties)) {
  58. jsonMetadataTable = new JsonMetadataTable({
  59. count: featureCount,
  60. properties: partitionResults.jsonProperties,
  61. });
  62. }
  63. let hierarchy;
  64. if (defined(partitionResults.hierarchy)) {
  65. hierarchy = new BatchTableHierarchy({
  66. extension: partitionResults.hierarchy,
  67. binaryBody: binaryBody,
  68. });
  69. }
  70. const className = MetadataClass.BATCH_TABLE_CLASS_NAME;
  71. const binaryProperties = partitionResults.binaryProperties;
  72. let metadataTable;
  73. let propertyAttributes;
  74. let transcodedSchema;
  75. if (parseAsPropertyAttributes) {
  76. const attributeResults = transcodeBinaryPropertiesAsPropertyAttributes(
  77. featureCount,
  78. className,
  79. binaryProperties,
  80. binaryBody,
  81. customAttributeOutput,
  82. );
  83. transcodedSchema = attributeResults.transcodedSchema;
  84. const propertyAttribute = new PropertyAttribute({
  85. propertyAttribute: attributeResults.propertyAttributeJson,
  86. class: attributeResults.transcodedClass,
  87. });
  88. propertyAttributes = [propertyAttribute];
  89. } else {
  90. const binaryResults = transcodeBinaryProperties(
  91. featureCount,
  92. className,
  93. binaryProperties,
  94. binaryBody,
  95. );
  96. transcodedSchema = binaryResults.transcodedSchema;
  97. const featureTableJson = binaryResults.featureTableJson;
  98. metadataTable = new MetadataTable({
  99. count: featureTableJson.count,
  100. properties: featureTableJson.properties,
  101. class: binaryResults.transcodedClass,
  102. bufferViews: binaryResults.bufferViewsTypedArrays,
  103. });
  104. propertyAttributes = [];
  105. }
  106. const propertyTables = [];
  107. if (
  108. defined(metadataTable) ||
  109. defined(jsonMetadataTable) ||
  110. defined(hierarchy)
  111. ) {
  112. const propertyTable = new PropertyTable({
  113. id: 0,
  114. name: "Batch Table",
  115. count: featureCount,
  116. metadataTable: metadataTable,
  117. jsonMetadataTable: jsonMetadataTable,
  118. batchTableHierarchy: hierarchy,
  119. });
  120. propertyTables.push(propertyTable);
  121. }
  122. const metadataOptions = {
  123. schema: transcodedSchema,
  124. propertyTables: propertyTables,
  125. propertyAttributes: propertyAttributes,
  126. extensions: partitionResults.extensions,
  127. extras: partitionResults.extras,
  128. };
  129. return new StructuralMetadata(metadataOptions);
  130. }
  131. /**
  132. * Divide the batch table's properties into binary, JSON and hierarchy
  133. * extension as each is handled separately
  134. *
  135. * @param {object} batchTable The batch table JSON
  136. * @returns {object} The batch table divided into binary, JSON and hierarchy portions. Extras and extensions are also divided out for ease of processing.
  137. *
  138. * @private
  139. */
  140. function partitionProperties(batchTable) {
  141. const legacyHierarchy = batchTable.HIERARCHY;
  142. const extras = batchTable.extras;
  143. const extensions = batchTable.extensions;
  144. let hierarchyExtension;
  145. if (defined(legacyHierarchy)) {
  146. parseBatchTable._deprecationWarning(
  147. "batchTableHierarchyExtension",
  148. "The batch table HIERARCHY property has been moved to an extension. Use extensions.3DTILES_batch_table_hierarchy instead.",
  149. );
  150. hierarchyExtension = legacyHierarchy;
  151. } else if (defined(extensions)) {
  152. hierarchyExtension = extensions["3DTILES_batch_table_hierarchy"];
  153. }
  154. // A JsonMetadataTable is only allocated as needed.
  155. let jsonProperties;
  156. // A MetadataTable or PropertyAttribute will always be created, even if
  157. // there are no properties.
  158. const binaryProperties = {};
  159. for (const propertyId in batchTable) {
  160. if (
  161. !batchTable.hasOwnProperty(propertyId) ||
  162. // these cases were handled above;
  163. propertyId === "HIERARCHY" ||
  164. propertyId === "extensions" ||
  165. propertyId === "extras"
  166. ) {
  167. continue;
  168. }
  169. const property = batchTable[propertyId];
  170. if (Array.isArray(property)) {
  171. jsonProperties = defined(jsonProperties) ? jsonProperties : {};
  172. jsonProperties[propertyId] = property;
  173. } else {
  174. binaryProperties[propertyId] = property;
  175. }
  176. }
  177. return {
  178. binaryProperties: binaryProperties,
  179. jsonProperties: jsonProperties,
  180. hierarchy: hierarchyExtension,
  181. extras: extras,
  182. extensions: extensions,
  183. };
  184. }
  185. /**
  186. * Transcode the binary properties of the batch table to be compatible with
  187. * <code>EXT_structural_metadata</code>
  188. *
  189. * @param {number} featureCount The number of features in the batch table
  190. * @param {string} className The name of the metadata class to be created.
  191. * @param {Object<string, Object>} binaryProperties A dictionary of property ID to property definition
  192. * @param {Uint8Array} [binaryBody] The binary body of the batch table
  193. * @return {object} Transcoded data needed for constructing a {@link StructuralMetadata} object.
  194. *
  195. * @private
  196. */
  197. function transcodeBinaryProperties(
  198. featureCount,
  199. className,
  200. binaryProperties,
  201. binaryBody,
  202. ) {
  203. const classProperties = {};
  204. const featureTableProperties = {};
  205. const bufferViewsTypedArrays = {};
  206. let bufferViewCount = 0;
  207. for (const propertyId in binaryProperties) {
  208. if (!binaryProperties.hasOwnProperty(propertyId)) {
  209. continue;
  210. }
  211. if (!defined(binaryBody)) {
  212. throw new RuntimeError(
  213. `Property ${propertyId} requires a batch table binary.`,
  214. );
  215. }
  216. const property = binaryProperties[propertyId];
  217. const binaryAccessor = getBinaryAccessor(property);
  218. featureTableProperties[propertyId] = {
  219. bufferView: bufferViewCount,
  220. };
  221. classProperties[propertyId] = transcodePropertyType(property);
  222. bufferViewsTypedArrays[bufferViewCount] =
  223. binaryAccessor.createArrayBufferView(
  224. binaryBody.buffer,
  225. binaryBody.byteOffset + property.byteOffset,
  226. featureCount,
  227. );
  228. bufferViewCount++;
  229. }
  230. const schemaJson = {
  231. classes: {},
  232. };
  233. schemaJson.classes[className] = {
  234. properties: classProperties,
  235. };
  236. const transcodedSchema = MetadataSchema.fromJson(schemaJson);
  237. const featureTableJson = {
  238. class: className,
  239. count: featureCount,
  240. properties: featureTableProperties,
  241. };
  242. return {
  243. featureTableJson: featureTableJson,
  244. bufferViewsTypedArrays: bufferViewsTypedArrays,
  245. transcodedSchema: transcodedSchema,
  246. transcodedClass: transcodedSchema.classes[className],
  247. };
  248. }
  249. function transcodeBinaryPropertiesAsPropertyAttributes(
  250. featureCount,
  251. className,
  252. binaryProperties,
  253. binaryBody,
  254. customAttributeOutput,
  255. ) {
  256. const classProperties = {};
  257. const propertyAttributeProperties = {};
  258. let nextPlaceholderId = 0;
  259. for (const propertyId in binaryProperties) {
  260. if (!binaryProperties.hasOwnProperty(propertyId)) {
  261. continue;
  262. }
  263. // For draco-compressed attributes from .pnts files, the results will be
  264. // stored in separate typed arrays. These will be used in place of the
  265. // binary body
  266. const property = binaryProperties[propertyId];
  267. if (!defined(binaryBody) && !defined(property.typedArray)) {
  268. throw new RuntimeError(
  269. `Property ${propertyId} requires a batch table binary.`,
  270. );
  271. }
  272. let sanitizedPropertyId = ModelUtility.sanitizeGlslIdentifier(propertyId);
  273. // If the sanitized string is empty or a duplicate, use a placeholder
  274. // name instead. This will work for styling, but it may lead to undefined
  275. // behavior in CustomShader, since
  276. // - different tiles may pick a different placeholder ID due to the
  277. // collection being unordered
  278. // - different tiles may have different number of properties.
  279. if (
  280. sanitizedPropertyId === "" ||
  281. classProperties.hasOwnProperty(sanitizedPropertyId)
  282. ) {
  283. sanitizedPropertyId = `property_${nextPlaceholderId}`;
  284. nextPlaceholderId++;
  285. }
  286. const classProperty = transcodePropertyType(property);
  287. classProperty.name = propertyId;
  288. classProperties[sanitizedPropertyId] = classProperty;
  289. // Extract the typed array and create a custom attribute as a typed array.
  290. // The caller must add the results to the ModelComponents, and upload the
  291. // typed array to the GPU. The attribute name is converted to all capitals
  292. // and underscores, like a glTF custom attribute.
  293. //
  294. // For example, if the original property ID was 'Temperature ℃', the result
  295. // is _TEMPERATURE
  296. let customAttributeName = sanitizedPropertyId.toUpperCase();
  297. if (!customAttributeName.startsWith("_")) {
  298. customAttributeName = `_${customAttributeName}`;
  299. }
  300. // for .pnts with draco compression, property.typedArray is used
  301. // instead of the binary body.
  302. let attributeTypedArray = property.typedArray;
  303. if (!defined(attributeTypedArray)) {
  304. const binaryAccessor = getBinaryAccessor(property);
  305. attributeTypedArray = binaryAccessor.createArrayBufferView(
  306. binaryBody.buffer,
  307. binaryBody.byteOffset + property.byteOffset,
  308. featureCount,
  309. );
  310. }
  311. const attribute = new ModelComponents.Attribute();
  312. attribute.name = customAttributeName;
  313. attribute.count = featureCount;
  314. attribute.type = property.type;
  315. const componentDatatype =
  316. ComponentDatatype.fromTypedArray(attributeTypedArray);
  317. if (
  318. componentDatatype === ComponentDatatype.INT ||
  319. componentDatatype === ComponentDatatype.UNSIGNED_INT ||
  320. componentDatatype === ComponentDatatype.DOUBLE
  321. ) {
  322. parseBatchTable._oneTimeWarning(
  323. "Cast pnts property to floats",
  324. `Point cloud property "${customAttributeName}" will be cast to a float array because INT, UNSIGNED_INT, and DOUBLE are not valid WebGL vertex attribute types. Some precision may be lost.`,
  325. );
  326. attributeTypedArray = new Float32Array(attributeTypedArray);
  327. }
  328. attribute.componentDatatype =
  329. ComponentDatatype.fromTypedArray(attributeTypedArray);
  330. attribute.typedArray = attributeTypedArray;
  331. customAttributeOutput.push(attribute);
  332. // Refer to the custom attribute name from the property attribute
  333. propertyAttributeProperties[sanitizedPropertyId] = {
  334. attribute: customAttributeName,
  335. };
  336. }
  337. const schemaJson = {
  338. classes: {},
  339. };
  340. schemaJson.classes[className] = {
  341. properties: classProperties,
  342. };
  343. const transcodedSchema = MetadataSchema.fromJson(schemaJson);
  344. const propertyAttributeJson = {
  345. properties: propertyAttributeProperties,
  346. };
  347. return {
  348. class: className,
  349. propertyAttributeJson: propertyAttributeJson,
  350. transcodedSchema: transcodedSchema,
  351. transcodedClass: transcodedSchema.classes[className],
  352. };
  353. }
  354. /**
  355. * Given a property definition from the batch table, compute the equivalent
  356. * <code>EXT_structural_metadata</code> type definition
  357. *
  358. * @param {object} property The batch table property definition
  359. * @return {object} The corresponding structural metadata property definition
  360. * @private
  361. */
  362. function transcodePropertyType(property) {
  363. const componentType = transcodeComponentType(property.componentType);
  364. return {
  365. type: property.type,
  366. componentType: componentType,
  367. };
  368. }
  369. /**
  370. * Convert the component type of a batch table property to the corresponding
  371. * type used with structural metadata
  372. *
  373. * @property {string} componentType the batch table's component type
  374. * @return {string} The corresponding structural metadata data type
  375. *
  376. * @private
  377. */
  378. function transcodeComponentType(componentType) {
  379. switch (componentType) {
  380. case "BYTE":
  381. return "INT8";
  382. case "UNSIGNED_BYTE":
  383. return "UINT8";
  384. case "SHORT":
  385. return "INT16";
  386. case "UNSIGNED_SHORT":
  387. return "UINT16";
  388. case "INT":
  389. return "INT32";
  390. case "UNSIGNED_INT":
  391. return "UINT32";
  392. case "FLOAT":
  393. return "FLOAT32";
  394. case "DOUBLE":
  395. return "FLOAT64";
  396. }
  397. }
  398. // exposed for testing
  399. parseBatchTable._deprecationWarning = deprecationWarning;
  400. parseBatchTable._oneTimeWarning = oneTimeWarning;
  401. export default parseBatchTable;