方向插补

Direction interpolation

我有树向量 A、B 和 D。有没有办法确定向量 D 与 A、B 和 C 的接近程度,并用 0 到 1 的值表示它?还要比较结果(A 到 D、B 到 D 和 C 到 D)总和应为 1。

所有向量都归一化结束表示方向。

如果我理解正确的话,我认为有两种选择

  • 你忘记了它们的总和 1 而是简单地问“每个单独的向量与给定的结果向量匹配多少 D?”

    为此,应该执行类似以下操作。

    • 针对 D
    • 获取每个向量的 Vector3.Dot
    • -1 | 1 范围内的这些点积映射到 0 | 1
    • 范围内

    类似

    public static float[] GetResults(Vector3[] inputVectors /*A,B,C*/, Vetcor3 resultVector /*D*/)
    {
        var results = new float[inputVectors.Length];
    
        // In a first iteration simply get all Dot products
        for(var i = 0; i < inputVector.Length; i++)
        {
            // 1 -> equal direction
            // -1 -> exactly contrary direction
            // 0 -> perpendicular
            var result = Vector3.Dot(resultVector, inputVectors[i]);
            // map to range 0 to 1
            result = (1f + result) / 2f;
            results[i] = result;
        }
    
        return results;
    }
    

    您可能会得到类似这样的结果[0.7, 0.5, 0.2]

  • 或者您实际上想要一个概率数组或简单的词:“哪个向量与给定向量 D 最匹配?”

    为此基本上和以前一样做,但另外

    • 将它们全部加起来并将结果用作一个因数,这样您总共得到的值为 100% (1)

    这意味着你得到的结果是概率而不是实际的平等程度,你只是得到“最接近”D

    的向量
    // Linq provides queries against IEnumerable types
    // I used it for a shorthand to sum up all numbers in an array see below
    using System.Linq;
    
    ...
    
    public static float[] GetResults(Vector3[] inputVectors /*A,B,C*/, Vetcor3 resultVector /*D*/)
    {
        ... // Same as above 
    
    
        // Additionally now get the sum of them all using Linq -> this is the inverse factor
        var sum = results.Sum();
        // or without Linq basically
        //var sum = 0f;
        //foreach(var result in results)
        //{
        //    sum += result;
        //}
    
        // in a second iteration now divide all results by the "sum"
        // => after this they will sum up to exactly 1
        for(var i = 0; i < inputVector.Length; i++)
        {
            results[i] = results[i] / sum;
        }
    
        return results;
    }
    

    对于与上面相同的值,您可能会得到类似于 [0.5, 0.357, 0.143]


我之前想说的是:

让我们假设您得到一些非常糟糕的匹配向量,例如第一种方法 return

0.02, 0.1, 0.01

第二种方法 return 例如

0.08, 0.77, 0.15

所以这会让你假设 B 是一个很好的匹配,而实际上它只是三个非常糟糕的匹配之间不太糟糕的匹配。