"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var THREE = _interopRequireWildcard(require("three"));
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; }
// See the different constants holding ordinal, name, numElements, byteSize in PointAttributes.cpp in PotreeConverter
// elementByteSize is byteSize / numElements
const POINT_ATTTRIBUTES = {
  POSITION_CARTESIAN: {
    numElements: 3,
    arrayType: Float32Array,
    attributeName: 'position'
  },
  COLOR_PACKED: {
    numElements: 4,
    arrayType: Uint8Array,
    attributeName: 'color',
    normalized: true
  },
  INTENSITY: {
    numElements: 1,
    numByte: 2,
    // using Float32Array because Float16Array doesn't exist
    arrayType: Uint16Array,
    attributeName: 'intensity',
    normalized: true
  },
  CLASSIFICATION: {
    numElements: 1,
    arrayType: Uint8Array,
    attributeName: 'classification',
    normalized: true
  },
  // Note: at the time of writing, PotreeConverter will only generate normals in Oct16 format
  // see PotreeConverter.cpp:121
  // we keep all the historical value to still supports old conversion
  NORMAL_SPHEREMAPPED: {
    numElements: 2,
    arrayType: Uint8Array,
    attributeName: 'sphereMappedNormal'
  },
  // see https://web.archive.org/web/20150303053317/http://lgdv.cs.fau.de/get/1602
  NORMAL_OCT16: {
    numElements: 2,
    arrayType: Uint8Array,
    attributeName: 'oct16Normal'
  },
  NORMAL: {
    numElements: 3,
    arrayType: Float32Array,
    attributeName: 'normal'
  }
};
for (const potreeName of Object.keys(POINT_ATTTRIBUTES)) {
  const attr = POINT_ATTTRIBUTES[potreeName];
  attr.potreeName = potreeName;
  attr.numByte = attr.numByte || attr.arrayType.BYTES_PER_ELEMENT;
  attr.byteSize = attr.numElements * attr.numByte;
  attr.normalized = attr.normalized || false;
  // chrome is known to perform badly when we call a method without respecting its arity
  const fnName = `getUint${attr.numByte * 8}`;
  attr.getValue = attr.numByte === 1 ? function (view, offset) {
    return view[fnName](offset);
  } : function (view, offset) {
    return view[fnName](offset, true);
  };
}
var _default = {
  /** @module PotreeBinParser */
  /** Parse .bin PotreeConverter format and convert to a THREE.BufferGeometry
   * @function parse
   * @param {ArrayBuffer} buffer - the bin buffer.
   * @param {Object} options
   * @param {string[]} options.in.pointAttributes - the point attributes information contained in cloud.js
   * @return {Promise} - a promise that resolves with a THREE.BufferGeometry.
   *
   */
  parse: function (buffer, options) {
    if (!buffer) {
      throw new Error('No array buffer provided.');
    }
    const view = new DataView(buffer);
    // Format: X1,Y1,Z1,R1,G1,B1,A1,[...],XN,YN,ZN,RN,GN,BN,AN
    let pointByteSize = 0;
    for (const potreeName of options.in.pointAttributes) {
      pointByteSize += POINT_ATTTRIBUTES[potreeName].byteSize;
    }
    const numPoints = Math.floor(buffer.byteLength / pointByteSize);
    const geometry = new THREE.BufferGeometry();
    let elemOffset = 0;
    let attrOffset = 0;
    for (const potreeName of options.in.pointAttributes) {
      const attr = POINT_ATTTRIBUTES[potreeName];
      const arrayLength = attr.numElements * numPoints;
      const array = new attr.arrayType(arrayLength);
      for (let arrayOffset = 0; arrayOffset < arrayLength; arrayOffset += attr.numElements) {
        for (let elemIdx = 0; elemIdx < attr.numElements; elemIdx++) {
          array[arrayOffset + elemIdx] = attr.getValue(view, attrOffset + elemIdx * attr.numByte);
        }
        attrOffset += pointByteSize;
      }
      elemOffset += attr.byteSize;
      attrOffset = elemOffset;
      geometry.setAttribute(attr.attributeName, new THREE.BufferAttribute(array, attr.numElements, attr.normalized));
    }
    geometry.computeBoundingBox();
    return Promise.resolve(geometry);
  }
};
exports.default = _default;