"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var VertexTrianglesMap_1 = require("./VertexTrianglesMap");
var CoordinateCalculator = tslib_1.__importStar(require("./CoordinateCalculator"));
var ControlLattice = tslib_1.__importStar(require("../ControlLattice"));
var Vector_1 = require("../../Math/Vector");
exports.create = function (controlLattice) { return ({
    controlLattice: controlLattice,
    trianglesByVertex: VertexTrianglesMap_1.create(controlLattice),
}); };
/**
 * calculate weightings for a linear combination of surrounding vectors
 *
 * @param {MeanValueCoordinates} meanValueCoordinates
 * @param {vec3} inputVertex find a linear combination to represent this vector by the surrounding vectors
 */
exports.getCoordinates = function (meanValueCoordinates, inputVertex) {
    var tLength = VertexTrianglesMap_1.getLength(meanValueCoordinates.trianglesByVertex);
    var tResult = new Array(tLength);
    var tSum = 0;
    for (var vertexIndex = 0; vertexIndex < tLength; vertexIndex++) {
        var boundaryVertex = ControlLattice.getVertex(meanValueCoordinates.controlLattice, vertexIndex);
        var triangles = VertexTrianglesMap_1.getTriangles(meanValueCoordinates.trianglesByVertex, vertexIndex);
        var weighting = CoordinateCalculator.getCoordinate(inputVertex, boundaryVertex, triangles);
        tResult[vertexIndex] = weighting;
        tSum += weighting;
    }
    for (var i = 0; i < tLength; i++) {
        tResult[i] /= tSum;
    }
    return tResult;
};
exports.evaluate = function (meanValueCoordinates, pWeightings, outVector) {
    if (outVector === void 0) { outVector = Vector_1.Vector.CreateVec3(); }
    var result = outVector;
    result.x = 0;
    result.y = 0;
    result.z = 0;
    var x = 0;
    var y = 0;
    var z = 0;
    var total = 0;
    var length = pWeightings.length;
    var boundaryVertex;
    for (var vertexIndex = 0; vertexIndex < length; vertexIndex++) {
        var coefficient = pWeightings[vertexIndex];
        if (coefficient > 0) {
            boundaryVertex = ControlLattice.getVertex(meanValueCoordinates.controlLattice, vertexIndex);
            x += coefficient * boundaryVertex.x;
            y += coefficient * boundaryVertex.y;
            z += coefficient * boundaryVertex.z;
            total += coefficient;
        }
    }
    result.x = x / total;
    result.y = y / total;
    result.z = z / total;
    return result;
};
