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

Implicit3DTileContent.js 38KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  1. import Cartesian3 from "../Core/Cartesian3.js";
  2. import Check from "../Core/Check.js";
  3. import clone from "../Core/clone.js";
  4. import combine from "../Core/combine.js";
  5. import defined from "../Core/defined.js";
  6. import destroyObject from "../Core/destroyObject.js";
  7. import DeveloperError from "../Core/DeveloperError.js";
  8. import CesiumMath from "../Core/Math.js";
  9. import HilbertOrder from "../Core/HilbertOrder.js";
  10. import Matrix3 from "../Core/Matrix3.js";
  11. import Rectangle from "../Core/Rectangle.js";
  12. import S2Cell from "../Core/S2Cell.js";
  13. import ImplicitSubtree from "./ImplicitSubtree.js";
  14. import hasExtension from "./hasExtension.js";
  15. import MetadataSemantic from "./MetadataSemantic.js";
  16. import BoundingVolumeSemantics from "./BoundingVolumeSemantics.js";
  17. /**
  18. * A specialized {@link Cesium3DTileContent} that lazily evaluates an implicit
  19. * tileset. It is somewhat similar in operation to a
  20. * {@link Tileset3DTileContent} in that once the content is constructed, it
  21. * updates the tileset tree with more tiles. However, unlike external tilesets,
  22. * child subtrees are represented as additional placeholder nodes with
  23. * Implicit3DTileContent.
  24. * <p>
  25. * Implements the {@link Cesium3DTileContent} interface.
  26. * </p>
  27. * This object is normally not instantiated directly, use {@link Implicit3DTileContent.fromSubtreeJson}.
  28. *
  29. * @implements Cesium3DTileContent
  30. * @private
  31. * @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.
  32. */
  33. class Implicit3DTileContent {
  34. /**
  35. * @param {Cesium3DTileset} tileset The tileset this content belongs to
  36. * @param {Cesium3DTile} tile The tile this content belongs to.
  37. * @param {Resource} resource The resource for the tileset
  38. * @param {object} [json] The JSON object containing the subtree. Mutually exclusive with arrayBuffer.
  39. * @param {ArrayBuffer} [arrayBuffer] The array buffer that stores the content payload. Mutually exclusive with json.
  40. * @param {number} [byteOffset=0] The offset into the array buffer, if one was provided
  41. *
  42. * @exception {DeveloperError} One of json and arrayBuffer must be defined.
  43. */
  44. constructor(tileset, tile, resource) {
  45. //>>includeStart('debug', pragmas.debug);
  46. Check.defined("tile.implicitTileset", tile.implicitTileset);
  47. Check.defined("tile.implicitCoordinates", tile.implicitCoordinates);
  48. //>>includeEnd('debug');
  49. const implicitTileset = tile.implicitTileset;
  50. const implicitCoordinates = tile.implicitCoordinates;
  51. this._implicitTileset = implicitTileset;
  52. this._implicitCoordinates = implicitCoordinates;
  53. this._implicitSubtree = undefined;
  54. this._tileset = tileset;
  55. this._tile = tile;
  56. this._resource = resource;
  57. this._metadata = undefined;
  58. this.featurePropertiesDirty = false;
  59. this._group = undefined;
  60. const templateValues = implicitCoordinates.getTemplateValues();
  61. const subtreeResource =
  62. implicitTileset.subtreeUriTemplate.getDerivedResource({
  63. templateValues: templateValues,
  64. });
  65. this._url = subtreeResource.getUrlComponent(true);
  66. this._ready = false;
  67. }
  68. get featuresLength() {
  69. return 0;
  70. }
  71. get pointsLength() {
  72. return 0;
  73. }
  74. get trianglesLength() {
  75. return 0;
  76. }
  77. get geometryByteLength() {
  78. return 0;
  79. }
  80. get texturesByteLength() {
  81. return 0;
  82. }
  83. get batchTableByteLength() {
  84. return 0;
  85. }
  86. get innerContents() {
  87. return undefined;
  88. }
  89. /**
  90. * Returns true when the tile's content is ready to render; otherwise false
  91. *
  92. *
  93. * @type {boolean}
  94. * @readonly
  95. * @private
  96. */
  97. get ready() {
  98. return this._ready;
  99. }
  100. get tileset() {
  101. return this._tileset;
  102. }
  103. get tile() {
  104. return this._tile;
  105. }
  106. get url() {
  107. return this._url;
  108. }
  109. /**
  110. * Part of the {@link Cesium3DTileContent} interface. <code>Implicit3DTileContent</code>
  111. * always returns <code>undefined</code>. Only transcoded tiles have content metadata.
  112. * @private
  113. */
  114. get metadata() {
  115. return undefined;
  116. }
  117. set metadata(_) {
  118. //>>includeStart('debug', pragmas.debug);
  119. throw new DeveloperError("Implicit3DTileContent cannot have metadata");
  120. //>>includeEnd('debug');
  121. }
  122. get batchTable() {
  123. return undefined;
  124. }
  125. get group() {
  126. return this._group;
  127. }
  128. set group(value) {
  129. this._group = value;
  130. }
  131. /**
  132. * Initialize the implicit content by parsing the subtree resource and setting
  133. * up a promise chain to expand the immediate subtree.
  134. *
  135. * @param {Cesium3DTileset} tileset The tileset this content belongs to
  136. * @param {Cesium3DTile} tile The tile this content belongs to.
  137. * @param {Resource} resource The resource for the tileset
  138. * @param {object} [json] The JSON containing the subtree. Mutually exclusive with arrayBuffer.
  139. * @param {ArrayBuffer} [arrayBuffer] The ArrayBuffer containing a subtree binary. Mutually exclusive with json.
  140. * @param {number} [byteOffset=0] The byte offset into the arrayBuffer
  141. * @return {Promise<Implicit3DTileContent>}
  142. *
  143. * @exception {DeveloperError} One of json and arrayBuffer must be defined.
  144. *
  145. * @private
  146. */
  147. static async fromSubtreeJson(
  148. tileset,
  149. tile,
  150. resource,
  151. json,
  152. arrayBuffer,
  153. byteOffset,
  154. ) {
  155. //>>includeStart('debug', pragmas.debug);
  156. Check.defined("tile.implicitTileset", tile.implicitTileset);
  157. Check.defined("tile.implicitCoordinates", tile.implicitCoordinates);
  158. if (defined(json) === defined(arrayBuffer)) {
  159. throw new DeveloperError("One of json and arrayBuffer must be defined.");
  160. }
  161. //>>includeEnd('debug');
  162. byteOffset = byteOffset ?? 0;
  163. let uint8Array;
  164. if (defined(arrayBuffer)) {
  165. uint8Array = new Uint8Array(arrayBuffer, byteOffset);
  166. }
  167. const implicitTileset = tile.implicitTileset;
  168. const implicitCoordinates = tile.implicitCoordinates;
  169. const subtree = await ImplicitSubtree.fromSubtreeJson(
  170. resource,
  171. json,
  172. uint8Array,
  173. implicitTileset,
  174. implicitCoordinates,
  175. );
  176. const content = new Implicit3DTileContent(tileset, tile, resource);
  177. content._implicitSubtree = subtree;
  178. expandSubtree(content, subtree);
  179. content._ready = true;
  180. return content;
  181. }
  182. /**
  183. * Part of the {@link Cesium3DTileContent} interface. <code>Implicit3DTileContent</code>
  184. * always returns <code>false</code> since a tile of this type does not have any features.
  185. * @private
  186. */
  187. hasProperty(batchId, name) {
  188. return false;
  189. }
  190. /**
  191. * Part of the {@link Cesium3DTileContent} interface. <code>Implicit3DTileContent</code>
  192. * always returns <code>undefined</code> since a tile of this type does not have any features.
  193. * @private
  194. */
  195. getFeature(batchId) {
  196. return undefined;
  197. }
  198. applyDebugSettings(enabled, color) {}
  199. applyStyle(style) {}
  200. update(tileset, frameState) {}
  201. pick(ray, frameState, result) {
  202. return undefined;
  203. }
  204. isDestroyed() {
  205. return false;
  206. }
  207. destroy() {
  208. this._implicitSubtree =
  209. this._implicitSubtree && this._implicitSubtree.destroy();
  210. return destroyObject(this);
  211. }
  212. }
  213. /**
  214. * Expand a single subtree placeholder tile. This transcodes the subtree into
  215. * a tree of {@link Cesium3DTile}. The root of this tree is stored in
  216. * the placeholder tile's children array. This method also creates placeholder
  217. * tiles for the child subtrees to be lazily expanded as needed.
  218. *
  219. * @param {Implicit3DTileContent} content The content
  220. * @param {ImplicitSubtree} subtree The parsed subtree
  221. * @private
  222. */
  223. function expandSubtree(content, subtree) {
  224. const placeholderTile = content._tile;
  225. // Parse the tiles inside this immediate subtree
  226. const childIndex = content._implicitCoordinates.childIndex;
  227. const results = transcodeSubtreeTiles(
  228. content,
  229. subtree,
  230. placeholderTile,
  231. childIndex,
  232. );
  233. const statistics = content._tileset.statistics;
  234. // Link the new subtree to the existing placeholder tile.
  235. placeholderTile.children.push(results.rootTile);
  236. statistics.numberOfTilesTotal++;
  237. // for each child subtree, make new placeholder tiles
  238. const childSubtrees = listChildSubtrees(content, subtree, results.bottomRow);
  239. for (let i = 0; i < childSubtrees.length; i++) {
  240. const subtreeLocator = childSubtrees[i];
  241. const leafTile = subtreeLocator.tile;
  242. const implicitChildTile = makePlaceholderChildSubtree(
  243. content,
  244. leafTile,
  245. subtreeLocator.childIndex,
  246. );
  247. leafTile.children.push(implicitChildTile);
  248. statistics.numberOfTilesTotal++;
  249. }
  250. }
  251. /**
  252. * A pair of (tile, childIndex) used for finding child subtrees.
  253. *
  254. * @typedef {object} ChildSubtreeLocator
  255. * @property {Cesium3DTile} tile One of the tiles in the bottommost row of the subtree.
  256. * @property {number} childIndex The morton index of the child tile relative to its parent
  257. * @private
  258. */
  259. /**
  260. * Determine what child subtrees exist and return a list of information
  261. *
  262. * @param {Implicit3DTileContent} content The implicit content
  263. * @param {ImplicitSubtree} subtree The subtree for looking up availability
  264. * @param {Array<Cesium3DTile|undefined>} bottomRow The bottom row of tiles in a transcoded subtree
  265. * @returns {ChildSubtreeLocator[]} A list of identifiers for the child subtrees.
  266. * @private
  267. */
  268. function listChildSubtrees(content, subtree, bottomRow) {
  269. const results = [];
  270. const branchingFactor = content._implicitTileset.branchingFactor;
  271. for (let i = 0; i < bottomRow.length; i++) {
  272. const leafTile = bottomRow[i];
  273. if (!defined(leafTile)) {
  274. continue;
  275. }
  276. for (let j = 0; j < branchingFactor; j++) {
  277. const index = i * branchingFactor + j;
  278. if (subtree.childSubtreeIsAvailableAtIndex(index)) {
  279. results.push({
  280. tile: leafTile,
  281. childIndex: j,
  282. });
  283. }
  284. }
  285. }
  286. return results;
  287. }
  288. /**
  289. * Results of transcodeSubtreeTiles, containing the root tile of the
  290. * subtree and the bottom row of nodes for further processing.
  291. *
  292. * @typedef {object} TranscodedSubtree
  293. * @property {Cesium3DTile} rootTile The transcoded root tile of the subtree
  294. * @property {Array<Cesium3DTile|undefined>} bottomRow The bottom row of transcoded tiles. This is helpful for processing child subtrees
  295. * @private
  296. */
  297. /**
  298. * Transcode the implicitly-defined tiles within this subtree and generate
  299. * explicit {@link Cesium3DTile} objects. This function only transcode tiles,
  300. * child subtrees are handled separately.
  301. *
  302. * @param {Implicit3DTileContent} content The implicit content
  303. * @param {ImplicitSubtree} subtree The subtree to get availability information
  304. * @param {Cesium3DTile} placeholderTile The placeholder tile, used for constructing the subtree root tile
  305. * @param {number} childIndex The Morton index of the root tile relative to parentOfRootTile
  306. * @returns {TranscodedSubtree} The newly created subtree of tiles
  307. * @private
  308. */
  309. function transcodeSubtreeTiles(content, subtree, placeholderTile, childIndex) {
  310. const rootBitIndex = 0;
  311. const rootParentIsPlaceholder = true;
  312. const rootTile = deriveChildTile(
  313. content,
  314. subtree,
  315. placeholderTile,
  316. childIndex,
  317. rootBitIndex,
  318. rootParentIsPlaceholder,
  319. );
  320. const statistics = content._tileset.statistics;
  321. // Sliding window over the levels of the tree.
  322. // Each row is branchingFactor * length of previous row
  323. // Tiles within a row are ordered by Morton index.
  324. let parentRow = [rootTile];
  325. let currentRow = [];
  326. const implicitTileset = content._implicitTileset;
  327. for (let level = 1; level < implicitTileset.subtreeLevels; level++) {
  328. const levelOffset = subtree.getLevelOffset(level);
  329. const numberOfChildren = implicitTileset.branchingFactor * parentRow.length;
  330. for (
  331. let childMortonIndex = 0;
  332. childMortonIndex < numberOfChildren;
  333. childMortonIndex++
  334. ) {
  335. const childBitIndex = levelOffset + childMortonIndex;
  336. if (!subtree.tileIsAvailableAtIndex(childBitIndex)) {
  337. currentRow.push(undefined);
  338. continue;
  339. }
  340. const parentMortonIndex = subtree.getParentMortonIndex(childMortonIndex);
  341. const parentTile = parentRow[parentMortonIndex];
  342. const childChildIndex =
  343. childMortonIndex % implicitTileset.branchingFactor;
  344. const childTile = deriveChildTile(
  345. content,
  346. subtree,
  347. parentTile,
  348. childChildIndex,
  349. childBitIndex,
  350. );
  351. parentTile.children.push(childTile);
  352. statistics.numberOfTilesTotal++;
  353. currentRow.push(childTile);
  354. }
  355. parentRow = currentRow;
  356. currentRow = [];
  357. }
  358. return {
  359. rootTile: rootTile,
  360. // At the end of the last loop, bottomRow was moved to parentRow
  361. bottomRow: parentRow,
  362. };
  363. }
  364. function getGeometricError(tileMetadata, implicitTileset, implicitCoordinates) {
  365. const semantic = MetadataSemantic.TILE_GEOMETRIC_ERROR;
  366. if (defined(tileMetadata) && tileMetadata.hasPropertyBySemantic(semantic)) {
  367. return tileMetadata.getPropertyBySemantic(semantic);
  368. }
  369. return (
  370. implicitTileset.geometricError / Math.pow(2, implicitCoordinates.level)
  371. );
  372. }
  373. /**
  374. * Given a parent tile and information about which child to create, derive
  375. * the properties of the child tile implicitly.
  376. * <p>
  377. * This creates a real tile for rendering, not a placeholder tile like some of
  378. * the other methods of ImplicitTileset.
  379. * </p>
  380. *
  381. * @param {Implicit3DTileContent} implicitContent The implicit content
  382. * @param {ImplicitSubtree} subtree The subtree the child tile belongs to
  383. * @param {Cesium3DTile} parentTile The parent of the new child tile
  384. * @param {number} childIndex The morton index of the child tile relative to its parent
  385. * @param {number} childBitIndex The index of the child tile within the tile's availability information.
  386. * @param {boolean} [parentIsPlaceholderTile=false] True if parentTile is a placeholder tile. This is true for the root of each subtree.
  387. * @returns {Cesium3DTile} The new child tile.
  388. * @private
  389. */
  390. function deriveChildTile(
  391. implicitContent,
  392. subtree,
  393. parentTile,
  394. childIndex,
  395. childBitIndex,
  396. parentIsPlaceholderTile,
  397. ) {
  398. const implicitTileset = implicitContent._implicitTileset;
  399. let implicitCoordinates;
  400. if (parentIsPlaceholderTile ?? false) {
  401. implicitCoordinates = parentTile.implicitCoordinates;
  402. } else {
  403. implicitCoordinates =
  404. parentTile.implicitCoordinates.getChildCoordinates(childIndex);
  405. }
  406. // Parse metadata and bounding volume semantics at the beginning
  407. // as the bounding volumes are needed below.
  408. let tileMetadata;
  409. let tileBounds;
  410. if (defined(subtree.tilePropertyTableJson)) {
  411. tileMetadata = subtree.getTileMetadataView(implicitCoordinates);
  412. tileBounds = BoundingVolumeSemantics.parseAllBoundingVolumeSemantics(
  413. "TILE",
  414. tileMetadata,
  415. );
  416. }
  417. // Content is not loaded at this point, so this flag is set for future reference.
  418. const contentPropertyTableJsons = subtree.contentPropertyTableJsons;
  419. const length = contentPropertyTableJsons.length;
  420. let hasImplicitContentMetadata = false;
  421. for (let i = 0; i < length; i++) {
  422. if (subtree.contentIsAvailableAtCoordinates(implicitCoordinates, i)) {
  423. hasImplicitContentMetadata = true;
  424. break;
  425. }
  426. }
  427. const boundingVolume = getTileBoundingVolume(
  428. implicitTileset,
  429. implicitCoordinates,
  430. childIndex,
  431. parentIsPlaceholderTile,
  432. parentTile,
  433. tileBounds,
  434. );
  435. const contentJsons = [];
  436. for (let i = 0; i < implicitTileset.contentCount; i++) {
  437. if (!subtree.contentIsAvailableAtIndex(childBitIndex, i)) {
  438. continue;
  439. }
  440. const childContentTemplate = implicitTileset.contentUriTemplates[i];
  441. const childContentUri = childContentTemplate.getDerivedResource({
  442. templateValues: implicitCoordinates.getTemplateValues(),
  443. }).url;
  444. const contentJson = {
  445. uri: childContentUri,
  446. };
  447. let contentBounds;
  448. if (subtree.contentPropertyTableJsons.length > 0) {
  449. const contentMetadata = subtree.getContentMetadataView(
  450. implicitCoordinates,
  451. i,
  452. );
  453. contentBounds = BoundingVolumeSemantics.parseAllBoundingVolumeSemantics(
  454. "CONTENT",
  455. contentMetadata,
  456. );
  457. }
  458. const contentBoundingVolume = getContentBoundingVolume(
  459. boundingVolume,
  460. contentBounds,
  461. );
  462. if (defined(contentBoundingVolume)) {
  463. contentJson.boundingVolume = contentBoundingVolume;
  464. }
  465. // combine() is used to pass through any additional properties the
  466. // user specified such as extras or extensions
  467. contentJsons.push(combine(contentJson, implicitTileset.contentHeaders[i]));
  468. }
  469. const childGeometricError = getGeometricError(
  470. tileMetadata,
  471. implicitTileset,
  472. implicitCoordinates,
  473. );
  474. const tileJson = {
  475. boundingVolume: boundingVolume,
  476. geometricError: childGeometricError,
  477. refine: implicitTileset.refine,
  478. contents: contentJsons,
  479. };
  480. // combine() is used to pass through any additional properties the
  481. // user specified such as extras or extensions.
  482. const deep = true;
  483. const rootHeader = clone(implicitTileset.tileHeader, deep);
  484. // The bounding volume was computed above since it may come from metadata
  485. // in the subtree file.
  486. delete rootHeader.boundingVolume;
  487. // Copying the transform to all the transcoded tiles would cause the transform
  488. // to be applied multiple times. Removing it from the header avoids this issue.
  489. delete rootHeader.transform;
  490. // The implicit tiling spec does not specify what should happen if explicit
  491. // tile metadata is added to the placeholder tile. Since implicit tile
  492. // metadata comes from the subtree file, ignore the explicit version.
  493. //
  494. // Also, when a property with the semantic TILE_BOUNDING_VOLUME is added to
  495. // the placeholder tile to set a tight bounding volume (See Cesium3DTile.js)
  496. // propagating it to transcoded tiles causes transcoded tiles to use the
  497. // wrong bounding volume, this can lead to loading far too many tiles.
  498. delete rootHeader.metadata;
  499. const combinedTileJson = combine(tileJson, rootHeader, deep);
  500. const childTile = makeTile(
  501. implicitContent,
  502. implicitTileset.baseResource,
  503. combinedTileJson,
  504. parentTile,
  505. );
  506. childTile.implicitCoordinates = implicitCoordinates;
  507. childTile.implicitSubtree = subtree;
  508. childTile.metadata = tileMetadata;
  509. childTile.hasImplicitContentMetadata = hasImplicitContentMetadata;
  510. return childTile;
  511. }
  512. /**
  513. * Checks whether the bounding volume's heights can be updated.
  514. * Returns true if the minimumHeight/maximumHeight parameter
  515. * is defined and the bounding volume is a region or S2 cell.
  516. *
  517. * @param {object} [boundingVolume] The bounding volume
  518. * @param {object} [tileBounds] The tile bounds
  519. * @param {number} [tileBounds.minimumHeight] The minimum height
  520. * @param {number} [tileBounds.maximumHeight] The maximum height
  521. * @returns {boolean} Whether the bounding volume's heights can be updated
  522. * @private
  523. */
  524. function canUpdateHeights(boundingVolume, tileBounds) {
  525. return (
  526. defined(boundingVolume) &&
  527. defined(tileBounds) &&
  528. (defined(tileBounds.minimumHeight) || defined(tileBounds.maximumHeight)) &&
  529. (hasExtension(boundingVolume, "3DTILES_bounding_volume_S2") ||
  530. defined(boundingVolume.region))
  531. );
  532. }
  533. /**
  534. * Update the minimum and maximum height of the bounding volume.
  535. * This is typically used to tighten a bounding volume using the
  536. * <code>TILE_MINIMUM_HEIGHT</code> and <code>TILE_MAXIMUM_HEIGHT</code>
  537. * semantics. Heights are only updated if the respective
  538. * minimumHeight/maximumHeight parameter is defined and the
  539. * bounding volume is a region or S2 cell.
  540. *
  541. * @param {object} boundingVolume The bounding volume
  542. * @param {object} [tileBounds] The tile bounds
  543. * @param {number} [tileBounds.minimumHeight] The new minimum height
  544. * @param {number} [tileBounds.maximumHeight] The new maximum height
  545. * @private
  546. */
  547. function updateHeights(boundingVolume, tileBounds) {
  548. if (!defined(tileBounds)) {
  549. return;
  550. }
  551. if (hasExtension(boundingVolume, "3DTILES_bounding_volume_S2")) {
  552. updateS2CellHeights(
  553. boundingVolume.extensions["3DTILES_bounding_volume_S2"],
  554. tileBounds.minimumHeight,
  555. tileBounds.maximumHeight,
  556. );
  557. } else if (defined(boundingVolume.region)) {
  558. updateRegionHeights(
  559. boundingVolume.region,
  560. tileBounds.minimumHeight,
  561. tileBounds.maximumHeight,
  562. );
  563. }
  564. }
  565. /**
  566. * For a bounding region, update the minimum and maximum height. This
  567. * is typically used to tighten a bounding volume using the
  568. * <code>TILE_MINIMUM_HEIGHT</code> and <code>TILE_MAXIMUM_HEIGHT</code>
  569. * semantics. Heights are only updated if the respective
  570. * minimumHeight/maximumHeight parameter is defined.
  571. *
  572. * @param {Array} region A 6-element array describing the bounding region
  573. * @param {number} [minimumHeight] The new minimum height
  574. * @param {number} [maximumHeight] The new maximum height
  575. * @private
  576. */
  577. function updateRegionHeights(region, minimumHeight, maximumHeight) {
  578. if (defined(minimumHeight)) {
  579. region[4] = minimumHeight;
  580. }
  581. if (defined(maximumHeight)) {
  582. region[5] = maximumHeight;
  583. }
  584. }
  585. /**
  586. * For a bounding S2 cell, update the minimum and maximum height. This
  587. * is typically used to tighten a bounding volume using the
  588. * <code>TILE_MINIMUM_HEIGHT</code> and <code>TILE_MAXIMUM_HEIGHT</code>
  589. * semantics. Heights are only updated if the respective
  590. * minimumHeight/maximumHeight parameter is defined.
  591. *
  592. * @param {object} s2CellVolume An object describing the S2 cell
  593. * @param {number} [minimumHeight] The new minimum height
  594. * @param {number} [maximumHeight] The new maximum height
  595. * @private
  596. */
  597. function updateS2CellHeights(s2CellVolume, minimumHeight, maximumHeight) {
  598. if (defined(minimumHeight)) {
  599. s2CellVolume.minimumHeight = minimumHeight;
  600. }
  601. if (defined(maximumHeight)) {
  602. s2CellVolume.maximumHeight = maximumHeight;
  603. }
  604. }
  605. /**
  606. * Gets the tile's bounding volume, which may be specified via
  607. * metadata semantics such as TILE_BOUNDING_BOX or implicitly
  608. * derived from the implicit root tile's bounding volume.
  609. * <p>
  610. * Priority of bounding volume types:
  611. * <ol>
  612. * <li>Explicit min/max height
  613. * <ol>
  614. * <li>With explicit region</li>
  615. * <li>With implicit S2</li>
  616. * <li>With implicit region</li>
  617. * </ol>
  618. * </li>
  619. * <li>Explicit box</li>
  620. * <li>Explicit region</li>
  621. * <li>Explicit sphere</li>
  622. * <li>Implicit S2</li>
  623. * <li>Implicit box</li>
  624. * <li>Implicit region</li>
  625. * </ol>
  626. * </p>
  627. *
  628. * @param {ImplicitTileset} implicitTileset The implicit tileset struct which holds the root bounding volume
  629. * @param {ImplicitTileCoordinates} implicitCoordinates The coordinates of the child tile
  630. * @param {number} childIndex The morton index of the child tile relative to its parent
  631. * @param {boolean} parentIsPlaceholderTile True if parentTile is a placeholder tile. This is true for the root of each subtree.
  632. * @param {Cesium3DTile} parentTile The parent of the new child tile
  633. * @param {object} [tileBounds] The tile bounds
  634. * @returns {object} An object containing the JSON for a bounding volume
  635. * @private
  636. */
  637. function getTileBoundingVolume(
  638. implicitTileset,
  639. implicitCoordinates,
  640. childIndex,
  641. parentIsPlaceholderTile,
  642. parentTile,
  643. tileBounds,
  644. ) {
  645. let boundingVolume;
  646. if (
  647. !defined(tileBounds) ||
  648. !defined(tileBounds.boundingVolume) ||
  649. (!canUpdateHeights(tileBounds.boundingVolume, tileBounds) &&
  650. canUpdateHeights(implicitTileset.boundingVolume, tileBounds))
  651. ) {
  652. boundingVolume = deriveBoundingVolume(
  653. implicitTileset,
  654. implicitCoordinates,
  655. childIndex,
  656. parentIsPlaceholderTile ?? false,
  657. parentTile,
  658. );
  659. } else {
  660. boundingVolume = tileBounds.boundingVolume;
  661. }
  662. // The TILE_MINIMUM_HEIGHT and TILE_MAXIMUM_HEIGHT metadata semantics
  663. // can be used to tighten the bounding volume
  664. updateHeights(boundingVolume, tileBounds);
  665. return boundingVolume;
  666. }
  667. /**
  668. * Gets the content bounding volume, which may be specified via
  669. * metadata semantics such as CONTENT_BOUNDING_BOX.
  670. * <p>
  671. * Priority of bounding volume types:
  672. * <ol>
  673. * <li>Explicit min/max height
  674. * <ol>
  675. * <li>With explicit region</li>
  676. * <li>With tile bounding volume (S2 or region)</li>
  677. * </ol>
  678. * </li>
  679. * <li>Explicit box</li>
  680. * <li>Explicit region</li>
  681. * <li>Explicit sphere</li>
  682. * <li>Tile bounding volume (when content.boundingVolume is undefined)</li>
  683. * </ol>
  684. * </p>
  685. *
  686. * @param {object} tileBoundingVolume An object containing the JSON for the tile's bounding volume
  687. * @param {object} [contentBounds] The content bounds
  688. * @returns {object|undefined} An object containing the JSON for a bounding volume, or <code>undefined</code> if there is no bounding volume
  689. * @private
  690. */
  691. function getContentBoundingVolume(tileBoundingVolume, contentBounds) {
  692. // content bounding volumes can only be specified via
  693. // metadata semantics such as CONTENT_BOUNDING_BOX
  694. let contentBoundingVolume;
  695. if (defined(contentBounds)) {
  696. contentBoundingVolume = contentBounds.boundingVolume;
  697. }
  698. // The CONTENT_MINIMUM_HEIGHT and CONTENT_MAXIMUM_HEIGHT metadata semantics
  699. // can be used to tighten the bounding volume
  700. if (canUpdateHeights(contentBoundingVolume, contentBounds)) {
  701. updateHeights(contentBoundingVolume, contentBounds);
  702. } else if (canUpdateHeights(tileBoundingVolume, contentBounds)) {
  703. contentBoundingVolume = clone(tileBoundingVolume, true);
  704. updateHeights(contentBoundingVolume, contentBounds);
  705. }
  706. return contentBoundingVolume;
  707. }
  708. /**
  709. * Given the coordinates of a tile, derive its bounding volume from the root.
  710. *
  711. * @param {ImplicitTileset} implicitTileset The implicit tileset struct which holds the root bounding volume
  712. * @param {ImplicitTileCoordinates} implicitCoordinates The coordinates of the child tile
  713. * @param {number} childIndex The morton index of the child tile relative to its parent
  714. * @param {boolean} parentIsPlaceholderTile True if parentTile is a placeholder tile. This is true for the root of each subtree.
  715. * @param {Cesium3DTile} parentTile The parent of the new child tile
  716. * @returns {object} An object containing the JSON for a bounding volume
  717. * @private
  718. */
  719. function deriveBoundingVolume(
  720. implicitTileset,
  721. implicitCoordinates,
  722. childIndex,
  723. parentIsPlaceholderTile,
  724. parentTile,
  725. ) {
  726. const rootBoundingVolume = implicitTileset.boundingVolume;
  727. if (hasExtension(rootBoundingVolume, "3DTILES_bounding_volume_S2")) {
  728. return deriveBoundingVolumeS2(
  729. parentIsPlaceholderTile,
  730. parentTile,
  731. childIndex,
  732. implicitCoordinates.level,
  733. implicitCoordinates.x,
  734. implicitCoordinates.y,
  735. implicitCoordinates.z,
  736. );
  737. }
  738. if (defined(rootBoundingVolume.region)) {
  739. const childRegion = deriveBoundingRegion(
  740. rootBoundingVolume.region,
  741. implicitCoordinates.level,
  742. implicitCoordinates.x,
  743. implicitCoordinates.y,
  744. implicitCoordinates.z,
  745. );
  746. return {
  747. region: childRegion,
  748. };
  749. }
  750. const childBox = deriveBoundingBox(
  751. rootBoundingVolume.box,
  752. implicitCoordinates.level,
  753. implicitCoordinates.x,
  754. implicitCoordinates.y,
  755. implicitCoordinates.z,
  756. );
  757. return {
  758. box: childBox,
  759. };
  760. }
  761. /**
  762. * Derive a bounding volume for a descendant tile (child, grandchild, etc.),
  763. * assuming a quadtree or octree implicit tiling scheme. The (level, x, y, [z])
  764. * coordinates are given to select the descendant tile and compute its position
  765. * and dimensions.
  766. * <p>
  767. * If z is present, octree subdivision is used. Otherwise, quadtree subdivision
  768. * is used. Quadtrees are always divided at the midpoint of the the horizontal
  769. * dimensions, i.e. (x, y), leaving the z axis unchanged.
  770. * </p>
  771. *
  772. * @param {boolean} parentIsPlaceholderTile True if parentTile is a placeholder tile. This is true for the root of each subtree.
  773. * @param {Cesium3DTile} parentTile The parent of the new child tile
  774. * @param {number} childIndex The morton index of the child tile relative to its parent
  775. * @param {number} level The level of the descendant tile relative to the root implicit tile
  776. * @param {number} x The x coordinate of the descendant tile
  777. * @param {number} y The y coordinate of the descendant tile
  778. * @param {number} [z] The z coordinate of the descendant tile (octree only)
  779. * @returns {object} An object with the 3DTILES_bounding_volume_S2 extension.
  780. * @private
  781. */
  782. function deriveBoundingVolumeS2(
  783. parentIsPlaceholderTile,
  784. parentTile,
  785. childIndex,
  786. level,
  787. x,
  788. y,
  789. z,
  790. ) {
  791. //>>includeStart('debug', pragmas.debug);
  792. Check.typeOf.bool("parentIsPlaceholderTile", parentIsPlaceholderTile);
  793. Check.typeOf.object("parentTile", parentTile);
  794. Check.typeOf.number("childIndex", childIndex);
  795. Check.typeOf.number("level", level);
  796. Check.typeOf.number("x", x);
  797. Check.typeOf.number("y", y);
  798. if (defined(z)) {
  799. Check.typeOf.number("z", z);
  800. }
  801. //>>includeEnd('debug');
  802. const boundingVolumeS2 = parentTile._boundingVolume;
  803. // Handle the placeholder tile case, where we just duplicate the placeholder's bounding volume.
  804. if (parentIsPlaceholderTile) {
  805. return {
  806. extensions: {
  807. "3DTILES_bounding_volume_S2": {
  808. token: S2Cell.getTokenFromId(boundingVolumeS2.s2Cell._cellId),
  809. minimumHeight: boundingVolumeS2.minimumHeight,
  810. maximumHeight: boundingVolumeS2.maximumHeight,
  811. },
  812. },
  813. };
  814. }
  815. // Extract the first 3 face bits from the 64-bit S2 cell ID.
  816. const face = Number(parentTile._boundingVolume.s2Cell._cellId >> BigInt(61));
  817. // The Hilbert curve is rotated for the "odd" faces on the S2 Earthcube.
  818. // See http://s2geometry.io/devguide/img/s2cell_global.jpg
  819. const position =
  820. face % 2 === 0
  821. ? HilbertOrder.encode2D(level, x, y)
  822. : HilbertOrder.encode2D(level, y, x);
  823. const cell = S2Cell.fromFacePositionLevel(face, BigInt(position), level);
  824. let minHeight, maxHeight;
  825. if (defined(z)) {
  826. const midpointHeight =
  827. (boundingVolumeS2.maximumHeight + boundingVolumeS2.minimumHeight) / 2;
  828. minHeight =
  829. childIndex < 4 ? boundingVolumeS2.minimumHeight : midpointHeight;
  830. maxHeight =
  831. childIndex < 4 ? midpointHeight : boundingVolumeS2.maximumHeight;
  832. } else {
  833. minHeight = boundingVolumeS2.minimumHeight;
  834. maxHeight = boundingVolumeS2.maximumHeight;
  835. }
  836. return {
  837. extensions: {
  838. "3DTILES_bounding_volume_S2": {
  839. token: S2Cell.getTokenFromId(cell._cellId),
  840. minimumHeight: minHeight,
  841. maximumHeight: maxHeight,
  842. },
  843. },
  844. };
  845. }
  846. const scratchScaleFactors = new Cartesian3();
  847. const scratchRootCenter = new Cartesian3();
  848. const scratchCenter = new Cartesian3();
  849. const scratchHalfAxes = new Matrix3();
  850. /**
  851. * Derive a bounding volume for a descendant tile (child, grandchild, etc.),
  852. * assuming a quadtree or octree implicit tiling scheme. The (level, x, y, [z])
  853. * coordinates are given to select the descendant tile and compute its position
  854. * and dimensions.
  855. * <p>
  856. * If z is present, octree subdivision is used. Otherwise, quadtree subdivision
  857. * is used. Quadtrees are always divided at the midpoint of the the horizontal
  858. * dimensions, i.e. (x, y), leaving the z axis unchanged.
  859. * </p>
  860. * <p>
  861. * This computes the child volume directly from the root bounding volume rather
  862. * than recursively subdividing to minimize floating point error.
  863. * </p>
  864. *
  865. * @param {number[]} rootBox An array of 12 numbers representing the bounding box of the root tile
  866. * @param {number} level The level of the descendant tile relative to the root implicit tile
  867. * @param {number} x The x coordinate of the descendant tile
  868. * @param {number} y The y coordinate of the descendant tile
  869. * @param {number} [z] The z coordinate of the descendant tile (octree only)
  870. * @returns {number[]} An array of 12 numbers representing the bounding box of the descendant tile.
  871. * @private
  872. */
  873. function deriveBoundingBox(rootBox, level, x, y, z) {
  874. //>>includeStart('debug', pragmas.debug);
  875. Check.typeOf.object("rootBox", rootBox);
  876. Check.typeOf.number("level", level);
  877. Check.typeOf.number("x", x);
  878. Check.typeOf.number("y", y);
  879. if (defined(z)) {
  880. Check.typeOf.number("z", z);
  881. }
  882. //>>includeEnd('debug');
  883. if (level === 0) {
  884. return rootBox;
  885. }
  886. const rootCenter = Cartesian3.unpack(rootBox, 0, scratchRootCenter);
  887. const rootHalfAxes = Matrix3.unpack(rootBox, 3, scratchHalfAxes);
  888. const tileScale = Math.pow(2, -level);
  889. const modelSpaceX = -1 + (2 * x + 1) * tileScale;
  890. const modelSpaceY = -1 + (2 * y + 1) * tileScale;
  891. let modelSpaceZ = 0;
  892. const scaleFactors = Cartesian3.fromElements(
  893. tileScale,
  894. tileScale,
  895. 1,
  896. scratchScaleFactors,
  897. );
  898. if (defined(z)) {
  899. modelSpaceZ = -1 + (2 * z + 1) * tileScale;
  900. scaleFactors.z = tileScale;
  901. }
  902. let center = Cartesian3.fromElements(
  903. modelSpaceX,
  904. modelSpaceY,
  905. modelSpaceZ,
  906. scratchCenter,
  907. );
  908. center = Matrix3.multiplyByVector(rootHalfAxes, center, scratchCenter);
  909. center = Cartesian3.add(center, rootCenter, scratchCenter);
  910. let halfAxes = Matrix3.clone(rootHalfAxes);
  911. halfAxes = Matrix3.multiplyByScale(halfAxes, scaleFactors, halfAxes);
  912. const childBox = new Array(12);
  913. Cartesian3.pack(center, childBox);
  914. Matrix3.pack(halfAxes, childBox, 3);
  915. return childBox;
  916. }
  917. const scratchRectangle = new Rectangle();
  918. /**
  919. * Derive a bounding volume for a descendant tile (child, grandchild, etc.),
  920. * assuming a quadtree or octree implicit tiling scheme. The (level, x, y, [z])
  921. * coordinates are given to select the descendant tile and compute its position
  922. * and dimensions.
  923. * <p>
  924. * If z is present, octree subdivision is used. Otherwise, quadtree subdivision
  925. * is used. Quadtrees are always divided at the midpoint of the the horizontal
  926. * dimensions, i.e. (mid_longitude, mid_latitude), leaving the height values
  927. * unchanged.
  928. * </p>
  929. * <p>
  930. * This computes the child volume directly from the root bounding volume rather
  931. * than recursively subdividing to minimize floating point error.
  932. * </p>
  933. * @param {number[]} rootRegion An array of 6 numbers representing the root implicit tile
  934. * @param {number} level The level of the descendant tile relative to the root implicit tile
  935. * @param {number} x The x coordinate of the descendant tile
  936. * @param {number} y The x coordinate of the descendant tile
  937. * @param {number} [z] The z coordinate of the descendant tile (octree only)
  938. * @returns {number[]} An array of 6 numbers representing the bounding region of the descendant tile
  939. * @private
  940. */
  941. function deriveBoundingRegion(rootRegion, level, x, y, z) {
  942. //>>includeStart('debug', pragmas.debug);
  943. Check.typeOf.object("rootRegion", rootRegion);
  944. Check.typeOf.number("level", level);
  945. Check.typeOf.number("x", x);
  946. Check.typeOf.number("y", y);
  947. if (defined(z)) {
  948. Check.typeOf.number("z", z);
  949. }
  950. //>>includeEnd('debug');
  951. if (level === 0) {
  952. return rootRegion.slice();
  953. }
  954. const rectangle = Rectangle.unpack(rootRegion, 0, scratchRectangle);
  955. const rootMinimumHeight = rootRegion[4];
  956. const rootMaximumHeight = rootRegion[5];
  957. const tileScale = Math.pow(2, -level);
  958. const childWidth = tileScale * rectangle.width;
  959. const west = CesiumMath.negativePiToPi(rectangle.west + x * childWidth);
  960. const east = CesiumMath.negativePiToPi(west + childWidth);
  961. const childHeight = tileScale * rectangle.height;
  962. const south = CesiumMath.negativePiToPi(rectangle.south + y * childHeight);
  963. const north = CesiumMath.negativePiToPi(south + childHeight);
  964. // Height is only subdivided for octrees; It remains constant for quadtrees.
  965. let minimumHeight = rootMinimumHeight;
  966. let maximumHeight = rootMaximumHeight;
  967. if (defined(z)) {
  968. const childThickness = tileScale * (rootMaximumHeight - rootMinimumHeight);
  969. minimumHeight += z * childThickness;
  970. maximumHeight = minimumHeight + childThickness;
  971. }
  972. return [west, south, east, north, minimumHeight, maximumHeight];
  973. }
  974. /**
  975. * Create a placeholder 3D Tile whose content will be an Implicit3DTileContent
  976. * for lazy evaluation of a child subtree.
  977. *
  978. * @param {Implicit3DTileContent} content The content object.
  979. * @param {Cesium3DTile} parentTile The parent of the new child subtree.
  980. * @param {number} childIndex The morton index of the child tile relative to its parent
  981. * @returns {Cesium3DTile} The new placeholder tile
  982. * @private
  983. */
  984. function makePlaceholderChildSubtree(content, parentTile, childIndex) {
  985. const implicitTileset = content._implicitTileset;
  986. const implicitCoordinates =
  987. parentTile.implicitCoordinates.getChildCoordinates(childIndex);
  988. const childBoundingVolume = deriveBoundingVolume(
  989. implicitTileset,
  990. implicitCoordinates,
  991. childIndex,
  992. false,
  993. parentTile,
  994. );
  995. // Ignore tile metadata when computing geometric error for the placeholder tile
  996. // since the child subtree's metadata hasn't been loaded yet.
  997. // The actual geometric error will be computed in deriveChildTile.
  998. const childGeometricError = getGeometricError(
  999. undefined,
  1000. implicitTileset,
  1001. implicitCoordinates,
  1002. );
  1003. const childContentUri = implicitTileset.subtreeUriTemplate.getDerivedResource(
  1004. {
  1005. templateValues: implicitCoordinates.getTemplateValues(),
  1006. },
  1007. ).url;
  1008. const tileJson = {
  1009. boundingVolume: childBoundingVolume,
  1010. geometricError: childGeometricError,
  1011. refine: implicitTileset.refine,
  1012. contents: [
  1013. {
  1014. uri: childContentUri,
  1015. },
  1016. ],
  1017. };
  1018. const tile = makeTile(
  1019. content,
  1020. implicitTileset.baseResource,
  1021. tileJson,
  1022. parentTile,
  1023. );
  1024. tile.implicitTileset = implicitTileset;
  1025. tile.implicitCoordinates = implicitCoordinates;
  1026. return tile;
  1027. }
  1028. /**
  1029. * Make a {@link Cesium3DTile}. This uses the content's tile's constructor instead
  1030. * of importing Cesium3DTile. This is to avoid a circular dependency between
  1031. * this file and Cesium3DTile.js
  1032. * @param {Implicit3DTileContent} content The implicit content
  1033. * @param {Resource} baseResource The base resource for the tileset
  1034. * @param {object} tileJson The JSON header for the tile
  1035. * @param {Cesium3DTile} parentTile The parent of the new tile
  1036. * @returns {Cesium3DTile} The newly created tile.
  1037. * @private
  1038. */
  1039. function makeTile(content, baseResource, tileJson, parentTile) {
  1040. const Cesium3DTile = content._tile.constructor;
  1041. return new Cesium3DTile(content._tileset, baseResource, tileJson, parentTile);
  1042. }
  1043. // Exposed for testing
  1044. Implicit3DTileContent._deriveBoundingBox = deriveBoundingBox;
  1045. Implicit3DTileContent._deriveBoundingRegion = deriveBoundingRegion;
  1046. Implicit3DTileContent._deriveBoundingVolumeS2 = deriveBoundingVolumeS2;
  1047. export default Implicit3DTileContent;