两个三维向量之间的夹角

4

我有一系列顶点(粉色)需要旋转,以便顶点的一条边与三角形的边缘(白色)匹配。

首先,我创建两个向量 floretAB 和 triangleAB(绿色)来表示这两个边。然后,我找到这两个向量的叉积,得到一个绕着它们旋转的轴(红色)。

接下来,我计算这两个向量之间的夹角,并使用该夹角和旋转轴创建四元数。最后,我将所有顶点绕着该四元数旋转。

enter image description here

旋转前

_

enter image description here

旋转后效果

_

但是,虽然旋转后的顶点正确绕着四元数旋转了,但夹角没有正确计算,如图所示:

enter image description here

以下是我用于计算两个向量之间夹角的代码。我不明白自己做错了什么:

double[] cross = new double[3];
crossProduct(floretAB.mX, floretAB.mY, floretAB.mZ, triangleAB.mX, triangleAB.mY, triangleAB.mZ, cross);
double dot = dotProduct(floretAB.mX, floretAB.mY, floretAB.mZ, triangleAB.mX, triangleAB.mY, triangleAB.mZ);
double crossMag = Math.sqrt(cross[0]*cross[0] + cross[1]*cross[1] + cross[2]*cross[2]);
double angle = Math.atan2(crossMag, dot);

public static double dotProduct(double vector1X,double vector1Y,double vector1Z,double vector2X,double vector2Y,double vector2Z){

    return vector1X*vector2X + vector1Y*vector2Y + vector1Z*vector2Z;

}

public static void crossProduct(double vector1X,double vector1Y,double vector1Z,double vector2X,double vector2Y,double vector2Z, double[] outputArray){

    outputArray[0] = vector1Y*vector2Z - vector1Z*vector2Y;     
    outputArray[1] = vector1Z*vector2X - vector1X*vector2Z;
    outputArray[2] = vector1X*vector2Y - vector1Y*vector2X;

}

任何帮助都将不胜感激,因为这个问题真的让我很烦恼。谢谢,詹姆斯。
编辑:以下是代码的其余部分:
        // get floret p1,p2 vector
    // get triangle p1,p2 vector
    Vector3D floretAB = new Vector3D(florets3D[0], florets3D[7]);
    // get triangle p1,p2 vector
    Vector3D triangleAB = new Vector3D(triangle[0], triangle[1]);

    // get rotation axis (cross) and angle (dot)

    /*
    double[] cross = new double[3];
    crossProduct(floretAB.mX, floretAB.mY, floretAB.mZ, triangleAB.mX, triangleAB.mY, triangleAB.mZ, cross);
    double dotMag = floretAB.getMagnitude() * triangleAB.getMagnitude();
    double dot = dotProduct(floretAB.mX, floretAB.mY, floretAB.mZ, triangleAB.mX, triangleAB.mY, triangleAB.mZ) / dotMag;
    double angle = Math.acos(dot);
    */

    double[] cross = new double[3];
    crossProduct(floretAB.mX, floretAB.mY, floretAB.mZ, triangleAB.mX, triangleAB.mY, triangleAB.mZ, cross);
    double dot = dotProduct(floretAB.mX, floretAB.mY, floretAB.mZ, triangleAB.mX, triangleAB.mY, triangleAB.mZ);
    double crossMag = Math.sqrt(cross[0]*cross[0] + cross[1]*cross[1] + cross[2]*cross[2]);
    double angle = Math.atan2(crossMag, dot);

    // rotate floret so p1,p2 vector matches with triangle p1,p2 vector     
    double[] newVerts = new double[3];
    Quaternion quat = new Quaternion(cross[0], cross[1], cross[2], angle);
    for(int i = 0;i<numfloretVerts;i++){
        Vertex3D vert = florets3D[i];
        quat.RotateVector(vert.getmX(), vert.getmY(), vert.getmZ(), newVerts);
        vert.setmX(newVerts[0]);
        vert.setmY(newVerts[1]);
        vert.setmZ(newVerts[2]);
    }

_

public class Vector3D {

public double mX;
public double mY;
public double mZ;

public Vertex3D point;

/**
 * Constructs a vector from two points. The new vector is normalised
 * 
 * @param point1
 * @param point2
 */
public Vector3D(Vertex3D point1, Vertex3D point2){
    mX = point2.getmX() - point1.getmX();
    mY = point2.getmY() - point1.getmY();
    mZ = point2.getmZ() - point1.getmZ();
    normalise();
    point = point1;
}

/**
 * Normalises the vector
 */
public void normalise(){
    double magnitude = Math.sqrt(mX*mX + mY*mY + mZ*mZ);
    if(magnitude!=0){
        mX /= magnitude;
        mY /= magnitude;
        mZ /= magnitude;
    }
}

/**
 * 
 * @return the magnitude of the vector
 */
public double getMagnitude(){
    return Math.sqrt(mX*mX + mY*mY + mZ*mZ);
}

}

_

public class Quaternion {

private static final double TOLERANCE = 0.00001f;

double w;
double x;
double y;
double z;

public Quaternion(double axisX, double axisY, double axisZ, double angleInRadians){
    setAxisAngle(axisX, axisY, axisZ, angleInRadians);      
}

public void Normalise(){

    // Don't normalize if we don't have to
    double mag2 = w * w + x * x + y * y + z * z;
    if (Math.abs(mag2) > TOLERANCE && Math.abs(mag2 - 1.0f) > TOLERANCE) {
        double mag = (double) Math.sqrt(mag2);
        w /= mag;
        x /= mag;
        y /= mag;
        z /= mag;
    }

}

public void getConjugate(double[] outputArray){

    outputArray[0] = w;
    outputArray[1] = -x;
    outputArray[2] = -y;
    outputArray[3] = -z;

}

public void Multiply(double[] aq, double[] rq, double[] outputArray){

    outputArray[0] = aq[0] * rq[0] - aq[1] * rq[1] - aq[2] * rq[2] - aq[3] * rq[3];
    outputArray[1] = aq[0] * rq[1] + aq[1] * rq[0] + aq[2] * rq[3] - aq[3] * rq[2];
    outputArray[2] = aq[0] * rq[2] + aq[2] * rq[0] + aq[3] * rq[1] - aq[1] * rq[3];
    outputArray[3] = aq[0] * rq[3] + aq[3] * rq[0] + aq[1] * rq[2] - aq[2] * rq[1];

}

private double[] vecQuat = new double[4];
private double[] resQuat = new double[4];
private double[] thisQuat = new double[4];

private double[] conj = new double[4];

/**
 * Rotates a vector (or point) around this axis-angle
 * 
 * @param vectorX the x component of the vector (or point)
 * @param vectorY the y component of the vector (or point)
 * @param vectorZ the z component of the vector (or point)
 * @param outputArray the array in which the results will be stored
 */
public void RotateVector(double vectorX, double vectorY, double vectorZ, double[] outputArray){

    vecQuat[0] = 0.0f;
    vecQuat[1] = vectorX;
    vecQuat[2] = vectorY;
    vecQuat[3] = vectorZ;

    thisQuat[0] = w;
    thisQuat[1] = x;
    thisQuat[2] = y;
    thisQuat[3] = z;

    getConjugate(conj);
    Multiply(vecQuat,conj,resQuat);
    Multiply(thisQuat,resQuat,vecQuat);

    outputArray[0] = vecQuat[1];
    outputArray[1] = vecQuat[2];
    outputArray[2] = vecQuat[3];

}

/**
 * set Quaternion by providing axis-angle form
 */
public void setAxisAngle(double axisX, double axisY, double axisZ, double angleInRadians){
    w = (double) Math.cos( angleInRadians/2);
    x = (double) (axisX * Math.sin( angleInRadians/2 ));
    y = (double) (axisY * Math.sin( angleInRadians/2 ));
    z = (double) (axisZ * Math.sin( angleInRadians/2 ));

    Normalise();
}
}

尝试使用 Math.atan2(dot, crossMag); - John Dvorak
没有产生任何影响/解决问题 :( - James Coote
3个回答

1

我认为你在数学上过于复杂了。

给定两个单位向量(你说它们已经被标准化了),那么叉积的大小等于 sin(theta)。不应该需要调用点积或者 atan2

在创建四元数之前,你可能还需要将叉积向量结果归一化,这取决于你对 new Quaternion(x, y, z, theta) 的实现以及是否需要将 [x, y, z] 标准化。


数学部分没有问题。在将向量放入四元数之前,我需要对叉乘向量进行归一化处理。 - James Coote
@JamesCoote 那就是猜对了的运气 :) - Alnitak

1
我认为问题在于您以错误的方式评估角度。如果我正确理解您试图实现的内容,那么您需要计算2条绿线之间的夹角。您可以使用定义来正确计算2条绿线之间的点积。
(a, b) = a1*b1 + a2*b2 + a3*b3.

但是点积也可以这样计算:

(a, b) = |a|*|b|*cos(theta)

因此,您可以像这样评估cos(theta)-即2条绿线之间夹角的余弦:

cos(theta) = (a1*b1 + a2*b2 + a3*b3) / (|a|*|b|)

但我会采用另一种方法。首先,我会将两个向量归一化(即将它们转换为单位向量)。您可以通过将每个向量的分量除以向量的长度(sqrt(x1*x1 + y1*y1 + z1*z1))来实现这一点。然后您将得到以下结果:

(aa, bb) = cos(theta)

其中aa是规范化后的a,bb是规范化后的b。

希望这能帮到您。


实际上,这些向量已经被归一化了。我尝试使用您的方法,但结果完全相同。这表明我的代码其他地方可能有问题,但如果我手动将角度设置为Pi或Pi/2(即90或180度),它会正确旋转这些角度。 - James Coote
@James Coote:你的意思是,如果硬编码Pi或Pi/2并且除此之外使用完全相同的代码,它将正确旋转? - Andrii Polunin
是的,这就是我推断出生成角度的代码肯定有问题的原因。我可能是错的,所以我会把剩下的代码也发布出来。 - James Coote
@James Coote:是的,如果您发布其余代码,可能会很有用。我现在可以建议的另一件事是检查角度的测量单位。如果您的代码适用于硬编码值,则可能会出现此问题。顺便问一下,您是以弧度还是以度数进行硬编码的?偶尔可能会发生这样的情况,即一个变量中的值为度数,而另一个变量中的值为弧度。如果您在某些计算中未事先转换为相同的测量单位,则会得到不正确的结果。 - Andrii Polunin

1
所述答案对于实数是正确的,但在使用浮点数计算时,接近某些角度时可能会失去精度。 对于arccos(),当角度接近零或PI时,以及对于arcsin()接近pi / 2和-pi / 2时,可能会丢失多达一半的有效数字。 一种更健壮的方法,并且只在整个范围从零到包括PI,假设输入向量为单位长度时遭受少量舍入误差。
public double AngleBetween(Vector3D a, Vector3D b)
{
    return 2.0d * Math.atan((a-b).Length/(a+b).Length);
}

注意,这给出了两个向量之间的无向角度。关于此的参考文献并归因于Kahan,可以在http://www.cs.berkeley.edu/~wkahan/MathH110/Cross.pdf找到。

@user151496,这段代码是用C#编写的,“d”告诉编译器常量2.0是一个双精度值。这可能有些过度,但可以加强表达式以双精度执行的要求。 - dmbaker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接