parseBatchTable.js 15 KB

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