All files / engine/Source/Scene BoundingVolumeSemantics.js

100% Statements 39/39
100% Branches 22/22
100% Functions 4/4
100% Lines 39/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170                    1x                                                   1x         1614x 1613x 1x   1612x     1611x                                                             1x         1649x 1648x 1x   1647x     1646x 1646x   1646x 1175x         471x 471x   471x 67x         404x 404x   404x   117x         287x                         1x   1616x 1615x 1x   1614x     1613x 1613x                         1x   1616x 1615x 1x   1614x     1613x 1613x        
import Check from "../Core/Check.js";
import defined from "../Core/defined.js";
import DeveloperError from "../Core/DeveloperError.js";
 
/**
 * Utilities for parsing bounding volume semantics from 3D Tiles 1.1 metadata.
 *
 * @namespace BoundingVolumeSemantics
 * @private
 */
const BoundingVolumeSemantics = {};
 
/**
 * Parse the bounding volume-related semantics such as
 * <code>TILE_BOUNDING_BOX</code> and <code>CONTENT_BOUNDING_BOX</code> from
 * implicit tile or content metadata. Results are returned as a JSON object for
 * use when transcoding tiles (see {@link Implicit3DTileContent}).
 * <p>
 * Bounding volumes are checked in the order box, region, then sphere. Only
 * the first valid bounding volume is returned.
 * </p>
 * <p>
 * This handles both tile and content bounding volumes, as the only difference
 * is the prefix. e.g. <code>TILE_BOUNDING_BOX</code> and
 * <code>CONTENT_BOUNDING_BOX</code> have the same memory layout.
 * </p>
 *
 * @see {@link https://github.com/CesiumGS/3d-tiles/tree/main/specification/Metadata/Semantics|3D Metadata Semantic Reference} for the various bounding volumes and minimum/maximum heights.
 *
 * @param {string} prefix Either "TILE" or "CONTENT"
 * @param {TileMetadata|ContentMetadata} metadata The metadata object for looking up values by semantic. In practice, this will typically be a {@link ImplicitMetadataView}
 * @return {object} An object containing the bounding volume, and any minimum or maximum height.
 *
 * @private
 * @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.
 */
BoundingVolumeSemantics.parseAllBoundingVolumeSemantics = function (
  prefix,
  metadata,
) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.string("prefix", prefix);
  if (prefix !== "TILE" && prefix !== "CONTENT") {
    throw new DeveloperError("prefix must be either 'TILE' or 'CONTENT'");
  }
  Check.typeOf.object("metadata", metadata);
  //>>includeEnd('debug');
 
  return {
    boundingVolume: BoundingVolumeSemantics.parseBoundingVolumeSemantic(
      prefix,
      metadata,
    ),
    minimumHeight: BoundingVolumeSemantics._parseMinimumHeight(
      prefix,
      metadata,
    ),
    maximumHeight: BoundingVolumeSemantics._parseMaximumHeight(
      prefix,
      metadata,
    ),
  };
};
 
/**
 * Parse the bounding volume from tile or content metadata. If the metadata
 * specify multiple bounding volumes, only the first one is returned. Bounding
 * volumes are checked in the order box, region, then sphere.
 * <p>
 * This handles both tile and content bounding volumes, as the only difference
 * is the prefix. e.g. <code>TILE_BOUNDING_BOX</code> and
 * <code>CONTENT_BOUNDING_BOX</code> have the same memory layout.
 * </p>
 *
 * @param {string} prefix Either "TILE" or "CONTENT"
 * @param {TileMetadata|ContentMetadata} metadata The metadata for looking up values
 * @return {object} An object representing the JSON description of the tile or content bounding volume
 * @private
 */
BoundingVolumeSemantics.parseBoundingVolumeSemantic = function (
  prefix,
  metadata,
) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.string("prefix", prefix);
  if (prefix !== "TILE" && prefix !== "CONTENT") {
    throw new DeveloperError("prefix must be either 'TILE' or 'CONTENT'");
  }
  Check.typeOf.object("metadata", metadata);
  //>>includeEnd('debug');
 
  const boundingBoxSemantic = `${prefix}_BOUNDING_BOX`;
  const boundingBox = metadata.getPropertyBySemantic(boundingBoxSemantic);
 
  if (defined(boundingBox)) {
    return {
      box: boundingBox,
    };
  }
 
  const boundingRegionSemantic = `${prefix}_BOUNDING_REGION`;
  const boundingRegion = metadata.getPropertyBySemantic(boundingRegionSemantic);
 
  if (defined(boundingRegion)) {
    return {
      region: boundingRegion,
    };
  }
 
  const boundingSphereSemantic = `${prefix}_BOUNDING_SPHERE`;
  const boundingSphere = metadata.getPropertyBySemantic(boundingSphereSemantic);
 
  if (defined(boundingSphere)) {
    // ARRAY with 4 elements is automatically converted to a Cartesian4
    return {
      sphere: boundingSphere,
    };
  }
 
  return undefined;
};
 
/**
 * Parse the minimum height from tile or content metadata. This is used for making
 * tighter quadtree bounds for implicit tiling. This works for both
 * <code>TILE_MINIMUM_HEIGHT</code> and <code>CONTENT_MINIMUM_HEIGHT</code>
 *
 * @param {string} prefix Either "TILE" or "CONTENT"
 * @param {TileMetadata|ContentMetadata} metadata The metadata for looking up values
 * @return {number} The minimum height
 * @private
 */
BoundingVolumeSemantics._parseMinimumHeight = function (prefix, metadata) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.string("prefix", prefix);
  if (prefix !== "TILE" && prefix !== "CONTENT") {
    throw new DeveloperError("prefix must be either 'TILE' or 'CONTENT'");
  }
  Check.typeOf.object("metadata", metadata);
  //>>includeEnd('debug');
 
  const minimumHeightSemantic = `${prefix}_MINIMUM_HEIGHT`;
  return metadata.getPropertyBySemantic(minimumHeightSemantic);
};
 
/**
 * Parse the maximum height from tile or content metadata. This is used for
 * making tighter quadtree bounds for implicit tiling. This works for both
 * <code>TILE_MAXIMUM_HEIGHT</code> and <code>CONTENT_MAXIMUM_HEIGHT</code>
 *
 * @param {string} prefix Either "TILE" or "CONTENT"
 * @param {TileMetadata|ContentMetadata} metadata The metadata for looking up values
 * @return {number} The maximum height
 * @private
 */
BoundingVolumeSemantics._parseMaximumHeight = function (prefix, metadata) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.string("prefix", prefix);
  if (prefix !== "TILE" && prefix !== "CONTENT") {
    throw new DeveloperError("prefix must be either 'TILE' or 'CONTENT'");
  }
  Check.typeOf.object("metadata", metadata);
  //>>includeEnd('debug');
 
  const maximumHeightSemantic = `${prefix}_MAXIMUM_HEIGHT`;
  return metadata.getPropertyBySemantic(maximumHeightSemantic);
};
 
export default BoundingVolumeSemantics;