"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var THREE = _interopRequireWildcard(require("three"));
var _C3DTBoundingVolume = _interopRequireDefault(require("./C3DTBoundingVolume"));
var _C3DTilesEnums = require("./C3DTilesEnums");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
// Inverse transform of a tile, computed from the tile transform and used when parsing the bounding volume of a tile
// if the bounding volume is a region (https://github.com/CesiumGS/3d-tiles/tree/main/specification#region) which is
// in global coordinates and other bounding volumes are not. To harmonize, we transform back the bounding volume region
// to a reference local to the tile.
const tileMatrixInverse = new THREE.Matrix4();

/** @classdesc
 * A 3D Tiles
 *  [Tileset](https://github.com/AnalyticalGraphicsInc/3d-tiles/blob/master/specification/schema/tileset.schema.json).
 * @property {C3DTilesTypes} type - Used by 3D Tiles extensions
 * (e.g. {@link C3DTBatchTableHierarchyExtension}) to know in which context
 * (i.e. for which 3D Tiles class) the parsing of the extension should be done.
 *  @property {object} asset - Generic information about the tileset, see
 *  [asset specification]https://github.com/CesiumGS/3d-tiles/blob/master/specification/schema/asset.schema.json
 *  @property {object} properties - Properties associated with the tileset, see
 *  [tileset specification](https://github.com/CesiumGS/3d-tiles/blob/master/specification/schema/tileset.schema.json#L11)
 *  @property {number} geometricError - see [tileset
 *  specification](https://github.com/CesiumGS/3d-tiles/blob/master/specification/schema/tileset.schema.json#L18)
 *  @property {string[]} extensionsUsed - see [tileset
 *  specification](https://github.com/CesiumGS/3d-tiles/blob/master/specification/schema/tileset.schema.json#L27)
 *  @property {array} extensionsRequired - see [tileset specification](https://github.com/CesiumGS/3d-tiles/blob/master/specification/schema/tileset.schema.json#L36)
 *  @property {object[]} tiles - an array holding all the
 *  [tiles](https://github.com/AnalyticalGraphicsInc/3d-tiles/blob/master/specification/schema/tile.schema.json)
 *  (not their content which is stored in the attribute object3d of the
 *  layer). This list represents a flattened tileset.
 *  @property {object} extensions - Extensions of the tileset in the form:
 * {extensioName1: extensionObject1, extensioName2: extensionObject2, ...}
 */
class C3DTileset {
  constructor(json, baseURL, registeredExtensions) {
    this.type = _C3DTilesEnums.C3DTilesTypes.tileset;
    this.asset = json.asset;
    this.properties = json.properties;
    this.geometricError = json.geometricError;
    this.extensionsUsed = json.extensionsUsed;
    this.extensionsRequired = json.extensionsRequired;
    this.tiles = [];
    this.parseTiles(json.root, baseURL, undefined, registeredExtensions);
    if (json.extensions) {
      this.extensions = registeredExtensions.parseExtensions(json.extensions, this.type);
    }
  }

  /**
   * Recursion on the 3DTiles tileset (which is a tree) to parse the tiles
   * (nodes of the tree).
   * @param {object} tile - current tile
   * @param {string} baseURL - url of the source tileset and tiles
   * @param {object} parent - parent tile (used for computing the transform
   * matrix from local to global coordinates)
   * @param {object} registeredExtensions - 3D Tiles extensions registered
   * in the C3DTilesLayer (see {@link C3DTExtensions}
   */
  parseTiles(tile, baseURL, parent, registeredExtensions) {
    // compute transform (will become Object3D.matrix when the object is
    // downloaded)
    tile.transform = tile.transform ? new THREE.Matrix4().fromArray(tile.transform) : undefined;

    // The only reason to store _worldFromLocalTransform is because of
    // extendTileset where we need the transform chain for one tile.
    tile._worldFromLocalTransform = tile.transform;
    if (parent && parent._worldFromLocalTransform) {
      if (tile.transform) {
        tile._worldFromLocalTransform = new THREE.Matrix4().multiplyMatrices(parent._worldFromLocalTransform, tile.transform);
      } else {
        tile._worldFromLocalTransform = parent._worldFromLocalTransform;
      }
    }

    // tileMatrixInverse is only used for volume.region
    if (tile.viewerRequestVolume && tile.viewerRequestVolume.region || tile.boundingVolume && tile.boundingVolume.region) {
      if (tile._worldFromLocalTransform) {
        tileMatrixInverse.copy(tile._worldFromLocalTransform).invert();
      } else {
        tileMatrixInverse.identity();
      }
    }
    tile.viewerRequestVolume = tile.viewerRequestVolume ? new _C3DTBoundingVolume.default(tile.viewerRequestVolume, tileMatrixInverse, registeredExtensions) : null;
    tile.boundingVolume = tile.boundingVolume ? new _C3DTBoundingVolume.default(tile.boundingVolume, tileMatrixInverse, registeredExtensions) : null;
    this.tiles.push(tile);
    tile.tileId = this.tiles.length - 1;
    tile.baseURL = baseURL;
    if (tile.children) {
      for (const child of tile.children) {
        this.parseTiles(child, baseURL, tile, registeredExtensions);
      }
    }
  }
  extendTileset(tileset, nodeId, baseURL, registeredExtensions) {
    this.parseTiles(tileset.root, baseURL, this.tiles[nodeId], registeredExtensions);
    this.tiles[nodeId].children = [tileset.root];
    this.tiles[nodeId].isTileset = true;
  }
}
var _default = C3DTileset;
exports.default = _default;