"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Tiles
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.TileGeometryCollector = void 0;
const IModelApp_1 = require("../IModelApp");
/** Collects geoemtry from a [[GeometryTileTreeReference]] within a specified volume at a specified level of detail.
 * Subclasses can refine the collection criterion.
 * The tile geometry is obtained asynchronously, so successive collections over multiple frames may be required before all of the geometry
 * is collected.
 * @public
 */
class TileGeometryCollector {
    /** Create a new collector. */
    constructor(options) {
        /** The list of accumulated polyfaces, populated during [[GeometryTileTreeReference.collectTileGeometry]].
         * The polyfaces belong to the [[Tile]]s - they should not be modified.
         * If [[isAllGeometryLoaded]] is `false`, then this list is incomplete - another geometry collection should be performed with a new collector on a subsequent frame.
         */
        this.polyfaces = [];
        this._missing = new Set();
        this._loading = false;
        this._options = options;
    }
    /** Allows an implementation of [[GeometryTileTreeReference.collectTileGeoemtry]] to indicate that further loading is required before
     * the collection can be completed.
     * This will cause [[isAllGeometryLoaded]] to return `false`.
     */
    markLoading() {
        this._loading = true;
    }
    /** Enqueues requests to obtain the content of any tiles whose content is required to complete the geometry collection.
     * @see [[isAllGeometryLoaded]] to determine if geometry collection is complete.
     */
    requestMissingTiles() {
        IModelApp_1.IModelApp.tileAdmin.requestTiles(this._options.user, this._missing);
    }
    /** Allows an implementation of [[GeometryTileTreeReference.collectTileGeometry]] to indicate that the specified tile's content must be loaded
     * before geometry collection can be completed.
     * This will cause [[isAllGeometryLoaded]] to return `false`.
     */
    addMissingTile(tile) {
        this._missing.add(tile);
    }
    /** Returns true if [[polyfaces]] has been fully populated with all the geometry satisfying this collector's criteria.
     * If it returns false, another collection using a new collector should be performed on a subsequent frame to load more geometry.
     */
    get isAllGeometryLoaded() {
        return !this._loading && this._missing.size === 0;
    }
    /** Determine whether to collect the specified tile's geometry, reject it, or to evaluate the geometry of its child tiles for collection.
     * This base implementation makes that determination based on the collector's range and chord tolerance. Subclasses should typically call `super.collectTile` and, if
     * it returns "accept" or "continue", apply their own criteria to the tile.
     */
    collectTile(tile) {
        const range = this._options.transform ? this._options.transform.multiplyRange(tile.range) : tile.range;
        if (!range.intersectsRange(this._options.range))
            return "reject";
        if (tile.maximumSize === 0 || !tile.isDisplayable)
            return "continue";
        const tolerance = tile.radius / tile.maximumSize;
        return tolerance < this._options.chordTolerance ? "accept" : "continue";
    }
}
exports.TileGeometryCollector = TileGeometryCollector;
//# sourceMappingURL=TileGeometryCollector.js.map