四元数与反符号相乘

时间:2019-07-18 21:24:22

标签: java quaternions

假设我有两个四元数a和b表示为(标量,Vector3(x,y,z))。 如果-a =(-0.01664f,-0.26258f,0.409833f,-0.87339f)和b =(0.017495f,-0.45169f,0.336854f,-0.8495f)。 然后是p0 = slerp(-a,b)和p1 = slerp(a,-b)其中a =(0.01664f,0.26258f,-0.409833f,0.87339f)和-b =(-0.017159792,0.44303548,-0.33039978 ,0.83322334)。 p0和p1相等吗?如果没有,为什么?

   public Quaternion slerp (Quaternion q) {

    // Calculate exponents and multiply everything from left to right
    Quaternion temp = new Quaternion(1.0f, 0.0f, 0.0f, 0.0f);
    Quaternion fin = new Quaternion(1.0f, 0.0f, 0.0f, 0.0f);
    float t = 0.5f;
    float dot = 0.0f;
    dot = this.dot(q);
    if (dot < 0.0f) {
    q.x = -q.x;
            q.y = -q.y;
            q.z = -q.z;
            q.w = -q.w;
    }
    this.exp(t).mul(q.exp(t));
    return this;
}
   public Quaternion exp (float alpha) {
    if (this.w > .9999999999){
        this.w = (float)Math.pow(this.w, alpha);
        return this;
    }
    // Calculate |q|^alpha
    float norm = len();
    float normExp = (float)Math.pow(norm, alpha);

    // Calculate theta
    float theta = (float)Math.acos(w / norm);

    // Calculate coefficient of basis elements
    float coeff = 0;
    if (Math.abs(theta) < 0.001) // If theta is small enough, use the limit of sin(alpha*theta) / sin(theta) instead of actual
    // value
        coeff = normExp * alpha / norm;
    else
        coeff = (float)(normExp * Math.sin(alpha * theta) / (norm * Math.sin(theta)));

    // Write results
    w = (float)(normExp * Math.cos(alpha * theta));
    x *= coeff;
    y *= coeff;
    z *= coeff;

    // Fix any possible discrepancies
    nor();

    return this;
}

    public Quaternion mul (final Quaternion other) {
    final float newX = this.w * other.x + this.x * other.w + this.y * other.z - this.z * other.y;
    final float newY = this.w * other.y + this.y * other.w + this.z * other.x - this.x * other.z;
    final float newZ = this.w * other.z + this.z * other.w + this.x * other.y - this.y * other.x;
    final float newW = this.w * other.w - this.x * other.x - this.y * other.y - this.z * other.z;
    this.x = newX;
    this.y = newY;
    this.z = newZ;
    this.w = newW;
    return this;
}

0 个答案:

没有答案