ImplicitTileset.js 7.8 KB

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