ImplicitTileset.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import Check from "../Core/Check.js";
  2. import clone from "../Core/clone.js";
  3. import defined from "../Core/defined.js";
  4. import Resource from "../Core/Resource.js";
  5. import RuntimeError from "../Core/RuntimeError.js";
  6. import hasExtension from "./hasExtension.js";
  7. import ImplicitSubdivisionScheme from "./ImplicitSubdivisionScheme.js";
  8. /**
  9. * An ImplicitTileset is a simple struct that stores information about the
  10. * structure of a single implicit tileset. This includes template URIs for
  11. * locating resources, details from the implicit root tile (bounding volume,
  12. * geometricError, etc.), and details about the subtrees (e.g. subtreeLevels,
  13. * subdivisionScheme).
  14. *
  15. * @alias ImplicitTileset
  16. * @constructor
  17. *
  18. * @param {Resource} baseResource The base resource for the tileset
  19. * @param {object} tileJson The JSON header of the tile with either implicit tiling (3D Tiles 1.1) or the 3DTILES_implicit_tiling extension.
  20. * @param {MetadataSchema} [metadataSchema] The metadata schema containing the implicit tile metadata class.
  21. * @private
  22. * @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.
  23. */
  24. function ImplicitTileset(baseResource, tileJson, metadataSchema) {
  25. const implicitTiling = hasExtension(tileJson, "3DTILES_implicit_tiling")
  26. ? tileJson.extensions["3DTILES_implicit_tiling"]
  27. : tileJson.implicitTiling;
  28. //>>includeStart('debug', pragmas.debug);
  29. Check.typeOf.object("implicitTiling", implicitTiling);
  30. //>>includeEnd('debug');
  31. /**
  32. * The base resource for the tileset. This is stored here as it is needed
  33. * later when expanding Implicit3DTileContents so tile URLs are relative
  34. * to the tileset, not the subtree file.
  35. *
  36. * @type {Resource}
  37. * @readonly
  38. * @private
  39. */
  40. this.baseResource = baseResource;
  41. /**
  42. * The geometric error of the root tile
  43. *
  44. * @type {number}
  45. * @readonly
  46. * @private
  47. */
  48. this.geometricError = tileJson.geometricError;
  49. /**
  50. * The metadata schema containing the implicit tile metadata class.
  51. *
  52. * @type {MetadataSchema|undefined}
  53. * @readonly
  54. * @private
  55. */
  56. this.metadataSchema = metadataSchema;
  57. const boundingVolume = tileJson.boundingVolume;
  58. if (
  59. !defined(boundingVolume.box) &&
  60. !defined(boundingVolume.region) &&
  61. !hasExtension(boundingVolume, "3DTILES_bounding_volume_S2") &&
  62. !hasExtension(boundingVolume, "3DTILES_bounding_volume_cylinder")
  63. ) {
  64. throw new RuntimeError(
  65. "Only box, region, 3DTILES_bounding_volume_S2, and 3DTILES_bounding_volume_cylinder are supported for implicit tiling"
  66. );
  67. }
  68. /**
  69. * The JSON representation of a bounding volume. This is either a box or a
  70. * region.
  71. *
  72. * @type {object}
  73. * @readonly
  74. * @private
  75. */
  76. this.boundingVolume = boundingVolume;
  77. /**
  78. * The refine strategy as a string, either 'ADD' or 'REPLACE'
  79. *
  80. * @type {string}
  81. * @readonly
  82. * @private
  83. */
  84. this.refine = tileJson.refine;
  85. /**
  86. * Template URI for the subtree resources, e.g.
  87. * <code>https://example.com/{level}/{x}/{y}.subtree</code>
  88. *
  89. * @type {Resource}
  90. * @readonly
  91. * @private
  92. */
  93. this.subtreeUriTemplate = new Resource({ url: implicitTiling.subtrees.uri });
  94. /**
  95. * Template URIs for locating content resources, e.g.
  96. * <code>https://example.com/{level}/{x}/{y}.b3dm</code>.
  97. * <p>
  98. * This is an array to support multiple contents.
  99. * </p>
  100. *
  101. * @type {Resource[]}
  102. * @readonly
  103. * @private
  104. */
  105. this.contentUriTemplates = [];
  106. /**
  107. * Store a copy of the content headers, so properties such as
  108. * <code>extras</code> or <code>extensions</code> are preserved when
  109. * {@link Cesium3DTile}s are created for each tile.
  110. * <p>
  111. * This is an array to support multiple contents.
  112. * </p>
  113. *
  114. * @type {Object[]}
  115. * @readonly
  116. * @private
  117. */
  118. this.contentHeaders = [];
  119. const contentHeaders = gatherContentHeaders(tileJson);
  120. for (let i = 0; i < contentHeaders.length; i++) {
  121. const contentHeader = contentHeaders[i];
  122. this.contentHeaders.push(clone(contentHeader, true));
  123. const contentResource = new Resource({ url: contentHeader.uri });
  124. this.contentUriTemplates.push(contentResource);
  125. }
  126. /**
  127. * The maximum number of contents as well as content availability bitstreams.
  128. * This is used for loop bounds when checking content availability.
  129. *
  130. * @type {number}
  131. * @readonly
  132. * @private
  133. */
  134. this.contentCount = this.contentHeaders.length;
  135. /**
  136. * Stores a copy of the root implicit tile's JSON header. This is used
  137. * as a template for creating {@link Cesium3DTile}s. The following properties
  138. * are removed:
  139. *
  140. * <ul>
  141. * <li><code>tile.implicitTiling</code> to prevent infinite loops of implicit tiling</li>
  142. * <li><code>tile.extensions["3DTILES_implicit_tiling"]</code>, if used instead of tile.implicitTiling</li>
  143. * <li><code>tile.contents</code>, since contents are handled separately</li>
  144. * <li><code>tile.content</code>, if used instead of tile.contents</li>
  145. * <li><code>tile.extensions["3DTILES_multiple_contents"]</code>, if used instead of tile.contents or tile.content</li>
  146. * </ul>
  147. *
  148. * @type {object}
  149. * @readonly
  150. * @private
  151. */
  152. this.tileHeader = makeTileHeaderTemplate(tileJson);
  153. /**
  154. * The subdivision scheme for this implicit tileset; either OCTREE or QUADTREE
  155. *
  156. * @type {ImplicitSubdivisionScheme}
  157. * @readonly
  158. * @private
  159. */
  160. this.subdivisionScheme =
  161. ImplicitSubdivisionScheme[implicitTiling.subdivisionScheme];
  162. /**
  163. * The branching factor for this tileset. Either 4 for quadtrees or 8 for
  164. * octrees.
  165. *
  166. * @type {number}
  167. * @readonly
  168. * @private
  169. */
  170. this.branchingFactor = ImplicitSubdivisionScheme.getBranchingFactor(
  171. this.subdivisionScheme
  172. );
  173. /**
  174. * How many distinct levels within each subtree. For example, a quadtree
  175. * with subtreeLevels = 2 will have 5 nodes per quadtree (1 root + 4 children)
  176. *
  177. * @type {number}
  178. * @readonly
  179. * @private
  180. */
  181. this.subtreeLevels = implicitTiling.subtreeLevels;
  182. /**
  183. * The number of levels containing available tiles in the tileset.
  184. *
  185. * @type {number}
  186. * @readonly
  187. * @private
  188. */
  189. if (defined(implicitTiling.availableLevels)) {
  190. this.availableLevels = implicitTiling.availableLevels;
  191. } else {
  192. this.availableLevels = implicitTiling.maximumLevel + 1;
  193. }
  194. }
  195. /**
  196. * Gather JSON headers for all contents in the tile.
  197. * This handles both regular tiles and tiles with multiple contents, either
  198. * in the contents array (3D Tiles 1.1) or the `3DTILES_multiple_contents` extension
  199. *
  200. * @param {object} tileJson The JSON header of the tile with either implicit tiling (3D Tiles 1.1) or the 3DTILES_implicit_tiling extension.
  201. * @return {Object[]} An array of JSON headers for the contents of each tile
  202. * @private
  203. */
  204. function gatherContentHeaders(tileJson) {
  205. if (hasExtension(tileJson, "3DTILES_multiple_contents")) {
  206. const extension = tileJson.extensions["3DTILES_multiple_contents"];
  207. return defined(extension.contents) ? extension.contents : extension.content;
  208. }
  209. if (defined(tileJson.contents)) {
  210. return tileJson.contents;
  211. }
  212. if (defined(tileJson.content)) {
  213. return [tileJson.content];
  214. }
  215. return [];
  216. }
  217. function makeTileHeaderTemplate(tileJson) {
  218. const template = clone(tileJson, true);
  219. // Remove the implicit tiling extension to prevent infinite loops,
  220. // as well as content-related properties since content is handled separately
  221. if (defined(template.extensions)) {
  222. delete template.extensions["3DTILES_implicit_tiling"];
  223. delete template.extensions["3DTILES_multiple_contents"];
  224. // if there are no other extensions, remove the extensions property to
  225. // keep each tile simple
  226. if (Object.keys(template.extensions).length === 0) {
  227. delete template.extensions;
  228. }
  229. }
  230. delete template.implicitTiling;
  231. delete template.contents;
  232. delete template.content;
  233. return template;
  234. }
  235. export default ImplicitTileset;