/**
 * Copyright (c) 2018 Bitbloq (BQ)
 *
 * License: MIT
 *
 * long description for the file
 *
 * @summary short description for the file
 * @author David García <https://github.com/empoalp>, Alberto Valero <https://github.com/avalero>
 *
 * Created at     : 2018-11-09 09:29:49
 * Last modified  : 2018-11-28 18:11:22
 */
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    }
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
import * as THREE from 'three';
import ThreeBSP from './threeCSG';
import './custom.d';
var ctx = self;
var CompoundWorker = /** @class */ (function (_super) {
    __extends(CompoundWorker, _super);
    function CompoundWorker() {
        return _super.call(this, 'http://bitbloq.bq.com') || this;
    }
    return CompoundWorker;
}(Worker));
export default CompoundWorker;
var getUnionFromGeometries = function (geometries) {
    var geomBSP = new ThreeBSP(geometries[0]);
    // Union with the rest
    for (var i = 1; i < geometries.length; i += 1) {
        var bspGeom = new ThreeBSP(geometries[i]);
        geomBSP = geomBSP.union(bspGeom);
    }
    var geom = geomBSP.toGeometry();
    return geom;
};
var getDifferenceFromGeometries = function (geometries) {
    var geomBSP = new ThreeBSP(geometries[0]);
    // Union with the rest
    for (var i = 1; i < geometries.length; i += 1) {
        var bspGeom = new ThreeBSP(geometries[i]);
        geomBSP = geomBSP.subtract(bspGeom);
    }
    var geom = geomBSP.toGeometry();
    return geom;
};
var getIntersectionFromGeometries = function (geometries) {
    var geomBSP = new ThreeBSP(geometries[0]);
    // Union with the rest
    for (var i = 1; i < geometries.length; i += 1) {
        var bspGeom = new ThreeBSP(geometries[i]);
        geomBSP = geomBSP.intersect(bspGeom);
    }
    var geom = geomBSP.toGeometry();
    return geom;
};
ctx.addEventListener('message', function (e) {
    var geometries = [];
    var bufferArray = e.data.bufferArray;
    var firstGeomMatrix = undefined;
    //add all children to geometries array
    for (var i = 0; i < bufferArray.length; i += 3) {
        //recompute object form vertices and normals
        var verticesBuffer = e.data.bufferArray[i];
        var normalsBuffer = e.data.bufferArray[i + 1];
        var positionBuffer = e.data.bufferArray[i + 2];
        var _vertices = new Float32Array(verticesBuffer, 0, verticesBuffer.byteLength / Float32Array.BYTES_PER_ELEMENT);
        var _normals = new Float32Array(normalsBuffer, 0, normalsBuffer.byteLength / Float32Array.BYTES_PER_ELEMENT);
        var _positions = new Float32Array(positionBuffer, 0, positionBuffer.byteLength / Float32Array.BYTES_PER_ELEMENT);
        var matrixWorld = new THREE.Matrix4();
        matrixWorld.elements = new Float32Array(_positions);
        if (i == 0)
            firstGeomMatrix = matrixWorld.clone();
        var buffGeometry = new THREE.BufferGeometry();
        buffGeometry.addAttribute('position', new THREE.BufferAttribute(_vertices, 3));
        buffGeometry.addAttribute('normal', new THREE.BufferAttribute(_normals, 3));
        var geometry_1 = new THREE.Geometry().fromBufferGeometry(buffGeometry);
        geometry_1.applyMatrix(matrixWorld);
        geometries.push(geometry_1);
    }
    //compute action
    var geometry = new THREE.Geometry();
    if (e.data.type === 'Union') {
        geometry = getUnionFromGeometries(geometries);
    }
    else if (e.data.type === 'Difference') {
        geometry = getDifferenceFromGeometries(geometries);
    }
    else if (e.data.type === 'Intersection') {
        geometry = getIntersectionFromGeometries(geometries);
    }
    else {
        var message_1 = {
            status: 'error',
        };
        ctx.postMessage(message_1);
    }
    //move resulting geometry to origin of coordinates (center on first child on origin)
    var invMatrix = new THREE.Matrix4();
    if (firstGeomMatrix)
        invMatrix.getInverse(firstGeomMatrix);
    geometry.applyMatrix(invMatrix);
    // get buffer data
    var bufferGeom = new THREE.BufferGeometry().fromGeometry(geometry);
    var vertices = new Float32Array(bufferGeom.getAttribute('position').array);
    var normals = new Float32Array(bufferGeom.getAttribute('normal').array);
    var message = {
        status: 'ok',
        vertices: vertices,
        normals: normals,
    };
    ctx.postMessage(message, [message.vertices.buffer, message.normals.buffer]);
}, false);
//# sourceMappingURL=compound.worker.js.map