import { create as createVertexTrianglesMap, getLength as getVertexTrianglesLength, getTriangles as getVertexTriangles, } from "./VertexTrianglesMap";
import * as CoordinateCalculator from "./CoordinateCalculator";
import * as ControlLattice from "../ControlLattice";
import { Vector } from "../../Math/Vector";
export var create = function (controlLattice) { return ({
    controlLattice: controlLattice,
    trianglesByVertex: createVertexTrianglesMap(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
 */
export var getCoordinates = function (meanValueCoordinates, inputVertex) {
    var tLength = getVertexTrianglesLength(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 = getVertexTriangles(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;
};
export var evaluate = function (meanValueCoordinates, pWeightings, outVector) {
    if (outVector === void 0) { outVector = 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;
};
