/**
 * 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-10-02 18:56:46
 * Last modified  : 2018-11-29 19:05:25
 */
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 __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
import * as THREE from 'three';
import ObjectsCommon from './ObjectsCommon';
var Object3D = /** @class */ (function (_super) {
    __extends(Object3D, _super);
    function Object3D(viewOptions, operations) {
        if (viewOptions === void 0) { viewOptions = ObjectsCommon.createViewOptions(); }
        if (operations === void 0) { operations = []; }
        var _this = this;
        var vO = __assign({}, ObjectsCommon.createViewOptions(), viewOptions);
        _this = _super.call(this, vO, operations) || this;
        return _this;
    }
    Object3D.getVerticesFromGeom = function (geometry) {
        var bufferGeometry = new THREE.BufferGeometry().fromGeometry(geometry);
        var attribute = bufferGeometry.getAttribute('position');
        return attribute.array;
    };
    Object3D.getNormalsFromGeom = function (geometry) {
        var bufferGeometry = new THREE.BufferGeometry().fromGeometry(geometry);
        var attribute = bufferGeometry.getAttribute('normal');
        return attribute.array;
    };
    Object3D.getMeshFromVerticesNormals = function (vertices, normals, material) {
        var geometry = new THREE.BufferGeometry();
        geometry.addAttribute('position', new THREE.BufferAttribute(vertices, 3));
        geometry.addAttribute('normal', new THREE.BufferAttribute(normals, 3));
        var mesh = new THREE.Mesh(geometry, material);
        return mesh;
    };
    // protected getMaterial(): THREE.MeshLambertMaterial {
    //   return new THREE.MeshLambertMaterial({
    //     color: this.viewOptions.color,
    //   });
    // }
    Object3D.prototype.getGeometry = function () {
        throw new Error('ERROR. Pure Virtual Function implemented in children');
    };
    Object3D.prototype.getBufferGeometry = function () {
        throw new Error('ERROR. Pure Virtual Function implemented in children');
    };
    Object3D.prototype.applyViewOptions = function (mesh) {
        if (!this.mesh && !mesh)
            throw new Error('ApplyViewOptions - Mesh not defined');
        var matParams = {
            color: this.viewOptions.color,
            transparent: false,
        };
        if (this.viewOptions.opacity < 1) {
            matParams = __assign({}, matParams, { opacity: this.viewOptions.opacity, transparent: true });
        }
        var material = new THREE.MeshLambertMaterial(matParams);
        if (mesh)
            mesh.material = material;
        else
            this.mesh.material = material;
        this._viewOptionsUpdateRequired = false;
    };
    Object3D.prototype.applyOperationsAsync = function () {
        return __awaiter(this, void 0, void 0, function () {
            var _this = this;
            return __generator(this, function (_a) {
                this.mesh.position.set(0, 0, 0);
                this.mesh.quaternion.setFromEuler(new THREE.Euler(0, 0, 0), true);
                this.mesh.scale.x = 1;
                this.mesh.scale.y = 1;
                this.mesh.scale.y = 1;
                this.operations.forEach(function (operation) {
                    // Translate operation
                    if (operation.type === Object3D.createTranslateOperation().type) {
                        _this.applyTranslateOperation(operation);
                    }
                    else if (operation.type === Object3D.createRotateOperation().type) {
                        _this.applyRotateOperation(operation);
                    }
                    else if (operation.type === Object3D.createScaleOperation().type) {
                        _this.applyScaleOperation(operation);
                    }
                    else if (operation.type === Object3D.createMirrorOperation().type) {
                        _this.applyMirrorOperation(operation);
                    }
                    else {
                        throw Error('ERROR: Unknown Operation');
                    }
                });
                this._pendingOperation = false;
                this.mesh.updateMatrixWorld(true);
                this.mesh.updateMatrix();
                return [2 /*return*/];
            });
        });
    };
    Object3D.prototype.applyMirrorOperation = function (operation) {
        if (operation.plane === 'xy') {
            this.applyScaleOperation(Object3D.createScaleOperation(1, 1, -1));
        }
        else if (operation.plane === 'yz') {
            this.applyScaleOperation(Object3D.createScaleOperation(-1, 1, 1));
        }
        else if (operation.plane === 'zx') {
            this.applyScaleOperation(Object3D.createScaleOperation(1, -1, 1));
        }
    };
    Object3D.prototype.applyTranslateOperation = function (operation) {
        if (operation.relative) {
            this.mesh.translateX(operation.x);
            this.mesh.translateY(operation.y);
            this.mesh.translateZ(operation.z);
        }
        else {
            //absolute x,y,z axis.
            this.mesh.position.x += Number(operation.x);
            this.mesh.position.y += Number(operation.y);
            this.mesh.position.z += Number(operation.z);
        }
    };
    Object3D.prototype.applyRotateOperation = function (operation) {
        var x = THREE.Math.degToRad(Number(operation.x));
        var y = THREE.Math.degToRad(Number(operation.y));
        var z = THREE.Math.degToRad(Number(operation.z));
        if (operation.relative) {
            this.mesh.rotateX(x);
            this.mesh.rotateY(y);
            this.mesh.rotateZ(z);
        }
        else {
            this.mesh.rotateOnWorldAxis(new THREE.Vector3(1, 0, 0), x);
            this.mesh.rotateOnWorldAxis(new THREE.Vector3(0, 1, 0), y);
            this.mesh.rotateOnWorldAxis(new THREE.Vector3(0, 0, 1), z);
        }
    };
    Object3D.prototype.applyScaleOperation = function (operation) {
        if (Number(operation.x) > 0 &&
            Number(operation.y) > 0 &&
            Number(operation.z) > 0)
            this.mesh.scale.set(this.mesh.scale.x * Number(operation.x), this.mesh.scale.y * Number(operation.y), this.mesh.scale.z * Number(operation.z));
    };
    Object3D.prototype.setMesh = function (mesh) {
        this.mesh = mesh;
        this._meshUpdateRequired = false;
        this._pendingOperation = false;
        this.mesh.updateMatrixWorld(true);
        this.mesh.updateMatrix();
    };
    Object3D.prototype.clone = function () {
        throw new Error('Object3D.clone() Implemented in children');
    };
    return Object3D;
}(ObjectsCommon));
export default Object3D;
//# sourceMappingURL=Object3D.js.map