"use strict";
/*
 * @Description  :
 * @Author       : 赵耀圣
 * @QQ           : 549184003
 * @Date         : 2021-03-22 17:27:16
 * @LastEditTime : 2021-03-23 18:01:49
 * @FilePath     : \cga.js\src\IKanimation\CCDSolver.ts
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.CCDSolver = void 0;
var Math_1 = require("../math/Math");
var Plane_1 = require("../struct/3d/Plane");
var Quat_1 = require("../math/Quat");
var Vec3_1 = require("../math/Vec3");
var _quat = new Quat_1.Quat();
var _plane = new Plane_1.Plane();
/**
 * @description :
 * @param        {*}
 * @return       {*}
 * @example     :
 */
var CCDSolver = /** @class */ (function () {
    function CCDSolver() {
    }
    CCDSolver.prototype.updateTarget = function () {
    };
    CCDSolver.prototype.projectPlane = function (v, normal) {
        return v.clone().projectOnPlaneNormal(normal);
    };
    CCDSolver.prototype.calcAngle = function (v1, v2, dir) {
        var vc1 = v1.clone();
        var vc2 = v2.clone();
        var angle = (0, Math_1.clamp)(vc1.normalize().dot(vc2.normalize()), -1, 1);
        angle = Math.acos(angle);
        if (vc1.cross(vc2).dot(dir) < 0)
            return -angle;
        return angle;
    };
    /**
     * @description :
     * @param        {Vec3} target 目标点
     * @param        {Vec3} zeroPos 关节原点
     * @param        {Vec3} effector 末端效应点
     * @return       {*}
     * @example     :
     */
    CCDSolver.prototype.solveTranslate = function (target, zeroPos, effector) {
    };
    /**
     * @description :
     * @param        {Vec3} target 目标点
     * @param        {Vec3} zeroPos 关节原点
     * @param        {Vec3} effector 末端效应点
     * @return       {*}
     * @example     :
     */
    CCDSolver.prototype.solveRotation = function (target, zeroPos, effector, contraints) {
        var dir0 = effector.clone().sub(zeroPos).normalize();
        var dir1 = target.clone().sub(zeroPos).normalize();
        var rotationAxis = (0, Vec3_1.v3)().cross(dir0, dir1).normalize();
        _plane.setFromPointNormal(zeroPos, rotationAxis);
        //最近点
        var closestDir = this.projectPlane(target, rotationAxis);
        closestDir.sub(zeroPos);
        var angle = 0;
        _quat.setFromUnitVecs(dir0, closestDir);
        return _quat.clone();
    };
    CCDSolver.prototype.solveIK = function (skeleton, target) {
        var i = 0;
        while (i++ < skeleton.boneNum) {
        }
    };
    CCDSolver.prototype.solve = function (bones) {
        // for (var i = bones.Length - 2; i > -1; i--) {
        //     // Slerp if weight is < 0
        //     //CCD tends to overemphasise the rotations of the bones closer to the target position. Reducing bone weight down the hierarchy will compensate for this effect.
        //     var w = bones[i].weight * IKPositionWeight;
        //     if (w > 0) {
        //         Vec3 toLastBone = bones[bones.Length - 1].transform.position - bones[i].transform.position;
        //         Vec3 toTarget = targetPosition - bones[i].transform.position;
        //         // Get the rotation to direct the last bone to the target
        //         Quat targetRotation = Quat.FromToRotation(toLastBone, toTarget) * bones[i].transform.rotation;
        //         if (w >= 1) bones[i].transform.rotation = targetRotation;
        //         else bones[i].transform.rotation = Quaternion.Lerp(bones[i].transform.rotation, targetRotation, w);
        //     }
        // }
    };
    return CCDSolver;
}());
exports.CCDSolver = CCDSolver;
