4

この質問には回答済みです。以下は、それを機能させるために必要なコードのほとんどです! それが他の人に役立つことを願っています。

@Aki Suihkonen、@David Hammen、@MBo に感謝します。

どちらの角度関数も正しい答えを返します。

私は3つのポイントがあります:

A: 12 4 5
B: 6 8 -10
C: 5 6 7

クォータニオンを実装しました。Angle( A, B, C ) が以前より 40 度高くなるように点 C を回転させたいと思います。

私の質問は次のとおりです。どの軸に従って回転する必要がありますか? A、B、および C が平面を作成するため、ベクトル BA および BC に垂直な軸に従って点 C を回転させる必要があると想像しました。単位ベクトルの CrossProduct で取得しましたが、Angle( A, B, C ) を取得しようとすると、正しい結果が得られません。

これが角度を取得する方法です:(古い方法)

results:
Angle of ABC before rotation = 67.3895.
Angle of ABC after rotation = 107.389.

float Angle( float x1, float y1, float z1,
             float x2, float y2, float z2 )
{
  float x, y, z;
  CrossProduct( x1, y1, z1, x2, y2, z2, &x, &y, &z );

  float result = atan2 ( L2Norm( x, y, z ),
                         DotProduct( x1, y1, z1, x2, y2, z2 ) );

  return result;
}

ここで、x1、y1、z1、x2、y2、z2 は、単位ベクトル BA および BC からの結果です。

更新された角度関数:

results:
Angle of ABC before rotation = 67.3895.
Angle of ABC after rotation = 107.389.

Angle( Atom &atom1, Atom &atom2, Atom &atom3 )
{
float px1, py1, pz1;
    float px2, py2, pz2;

    UnitVector( atom1.X(), atom1.Y(), atom1.Z(),
            atom2.X(), atom2.Y(), atom2.Z(),
            &px1, &py1, &pz1 );


    UnitVector( atom3.X(), atom3.Y(), atom3.Z(),
            atom2.X(), atom2.Y(), atom2.Z(),
            &px2, &py2, &pz2 );

  float dot_product = DotProduct( px1, py1, pz1, px2, py2, pz2 );
  float length_BA = sqrt( px1*px1 + py1*py1 + pz1*pz1 );
  float length_BC = sqrt( px2*px2 + py2*py2 + pz2*pz2 );

  return acos( dot_product / ( length_BA * length_BC ) );
}

float DotProduct( float x1, float y1, float z1,
                  float x2, float y2, float z2 )
{
  return x1*x2 + y1*y2 + z1*z2;
}

void CrossProduct( float x1, float y1, float z1,
                   float x2, float y2, float z2,
                   float *ox, float *oy, float *oz )
{
  *ox = (y1*z2) -(z1*y2);
  *oy = -((x1*z2) -(z1*x2));
  *oz = (x1*y2) -(y1*x2);
}

だから私の質問は次のとおりです。角度(A、B、C)が以前よりも40度大きくなるように、どの軸に従って点Cを回転させる必要がありますか?

  // The 3 points.
  Atom A( "A", -4, 2 , 8 );
  Atom B( "B", -1, 3 , 4 );
  Atom C( "C", -2, -4 , 5 );

  float x1, y1, z1;
  float x2, y2, z2;
  float x, y, z;

  // Get the cross product. Create the perpendicular vector to the BA and BC vectors.
  PointVector( A.X(), A.Y(), A.Z(), B.X(), B.Y(), B.Z(), &x1, &y1, &z1 );
  PointVector( C.X(), C.Y(), C.Z(), B.X(), B.Y(), B.Z(), &x2, &y2, &z2 );

  CrossProduct( x1, y1, z1, x2, y2, z2, &x, &y, &z );

  // Normalize the coordinates.
  float length = sqrt( x*x + y*y + z*z );
  length = 1.0 / length;

  x *= length;
  y *= length;
  z *= length;

  // Create the 40 degrees angle. It is supposed to increment the current ABC angle by 40 degrees.
  float angle = 40*M_PI/180;
  float sinAngleOver2 = sin(angle/2);
  float w = cos(angle/2);

  // Create the axis quat.
  Quatd q(w, x * sinAngleOver2, y * sinAngleOver2, z * sinAngleOver2);

  // Create the point quaternal. The angle of it equals to the current ABC angle.
  angle = Angle( A, B, C ) *180/M_PI;
  angle *= M_PI/180;
  sinAngleOver2 = sin(angle/2);
  w = cos(angle/2);

  // Normalize the coordinates. The coordinates are the C point coordinates.


    x = C.X() - B.X();
    y = C.Y() - B.Y();
    z = C.Z() - B.Z();

    length = sqrt( x*x + y*y + z*z );
    length = 1.0 / length;

    x *= length;
    y *= length;
    z *= length;


  Quatd qpt(w, x * sinAngleOver2, y * sinAngleOver2, z * sinAngleOver2);

  // Rotate.
  qpt = q*qpt*q.unitInverse();

  Atom new_C;
  new_C.X( qpt.x + B.X() );
  new_C.Y( qpt.y + B.Y() );
  new_C.Z( qpt.z + B.Z() );

  cout << "Angle: " << Angle( A, B, new_C )*180/M_PI << '\n';
4

1 に答える 1

3

このコードは正しくありません:

// Normalize the coordinates. The coordinates are the C point coordinates.
length = sqrt( C.X()*C.X() + C.Y()*C.Y() + C.Z()*C.Z() );
length = 1.0 / length;

x = C.X() / length;
y = C.Y() / length;
z = C.Z() / length;

length変数を storeに再利用することで混乱しました1 / length。ここでベクトルを正規化するにCは、のコンポーネントを掛ける必要があります。length

最後に何をしようとしているのかを理解するには、四元数の数学に精通していませんがC、原点を中心に回転しているように見えます。を中心に回転させたいとします。これは、 から減算し、原点を中心に回転を実行し、結果に加算して元の空間に戻すことをB意味します。BCB

個人的には、何らかの四元数とベクトルの乗算を実装するか、変換を実行するために四元数を行列に変換します。

于 2013-01-29T14:54:24.007 に答える