"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 Rendering
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.VertexIndices = void 0;
const core_bentley_1 = require("@itwin/core-bentley");
/**
 * Holds an array of indices into a VertexTable. Each index is a 24-bit unsigned integer.
 * The order of the indices specifies the order in which vertices are drawn.
 * @internal
 */
class VertexIndices {
    data;
    /**
     * Directly construct from an array of bytes in which each index occupies 3 contiguous bytes.
     * The length of the array must be a multiple of 3. This object takes ownership of the array.
     */
    constructor(data) {
        this.data = data;
        (0, core_bentley_1.assert)(0 === this.data.length % 3);
    }
    /** Get the number of 24-bit indices. */
    get length() { return this.data.length / 3; }
    /** Convert an array of 24-bit unsigned integer values into a VertexIndices object. */
    static fromArray(indices) {
        const bytes = new Uint8Array(indices.length * 3);
        for (let i = 0; i < indices.length; i++)
            this.encodeIndex(indices[i], bytes, i * 3);
        return new VertexIndices(bytes);
    }
    static encodeIndex(index, bytes, byteIndex) {
        (0, core_bentley_1.assert)(byteIndex + 2 < bytes.length);
        bytes[byteIndex + 0] = index & 0x000000ff;
        bytes[byteIndex + 1] = (index & 0x0000ff00) >> 8;
        bytes[byteIndex + 2] = (index & 0x00ff0000) >> 16;
    }
    setNthIndex(n, value) {
        VertexIndices.encodeIndex(value, this.data, n * 3);
    }
    decodeIndex(index) {
        (0, core_bentley_1.assert)(index < this.length);
        const byteIndex = index * 3;
        return this.data[byteIndex] | (this.data[byteIndex + 1] << 8) | (this.data[byteIndex + 2] << 16);
    }
    decodeIndices() {
        const indices = [];
        for (let i = 0; i < this.length; i++)
            indices.push(this.decodeIndex(i));
        return indices;
    }
    [Symbol.iterator]() {
        function* iterator(indices) {
            for (let i = 0; i < indices.length; i++)
                yield indices.decodeIndex(i);
        }
        return iterator(this);
    }
}
exports.VertexIndices = VertexIndices;
//# sourceMappingURL=VertexIndices.js.map