Quaternary element and Rotation

Source: Internet
Author: User
I. Basis of four tuples

Q (x, y, z, W), where X, Y, Z is used to determine the rotation axis, W is the Rotation Angle

Q = W + Xi + YJ + ZK, I, j, and K are the unit components of the three virtual axes.

I * j = K

J * k = I;

K * I = J;

Cross multiplication:

C = A × B =

| I j k |
| A1 B1 C1 |
| A2 B2 C2 |
= (B1c2-b2c1, c1a2-a1c2, a1b2-a2b1)

C is also a vector, and the length of C is | A | B | sin (theta), perpendicular to the plane where A and B are located. The direction is determined by the right hand rule, use the four fingers in the right hand to first represent the direction of vector A, and then the fingers swing to the direction of vector B in the direction of the palm, the direction pointed by the thumb is the direction of vector C

 

 

1. Multiply the four tuples:

Q1 = W1 + x1i + y1j + z1k = (W1, V1)

Q2 = W2 + x2i + y2j + z2k = (W2, V2)

Q1 * q2 = (W1 * W2-<V1, V2>, W1 * V2 + W2 * V1 + v1xv2)

(W1 + x1i + y1j + z1k) * (W2 + x2i + y2j + z2k)

= W1 * w2-x1 * x2-y1 * y2-z1 * Z2 +

(W1 * X2 + X1 * W2 + Y1 * z2-z1-y2) I +

(Y1 * W2 + W1 * y2 + z1 * x2-x1 * Z2) J +

(W1 * Z2 + z1 * W2 + X1 * y2-y1 * x2) k

For the axis part, If V1/V2, V1 x v2 = 0 (the cross multiplication result of the parallel vector is 0)

2. The dot multiplication of the four tuples. The product of the vertex is a numerical value:

Q1. * q2 = W1 * W2 + <V1, V2> = W1 * W2 + X1 * X2 + Y1 * y2 + z1 * Z2;

3. Multiplication

S is a real number, and Q is a quad-tuple, then there is sq = Qs

4. concatenation

P = (W, V), p * = (W,-V)

 

(PQ) * = Q * p *

 

N (q) = W2 + X2 + y2 + Z2

Q-1 = Q */N (q) --------------- à apparently available qq-1 =)

 

 

Ii. Use a four-element Rotation Vector

If there is a four-tuple q = (W, V) representing the vector, the result after applying the rotation P to it is:

Q' = pqp-1 = (W, V ')

From the above, we can see that the actual part of the calculated Q 'is equal to the actual part of Q, and N (v) = N (v ')

 

If n (q) = 1, q = (COSA, usina) can be set, and U is also a unit vector, thenQ' is the 2a radians that Q rotates around uResult

 

If S (q) represents the real part of Q, there are 2 S (q) = q + Q *

 

2 S (pqp-1) = pqp-1 + (pqp-1) * = pqp * + (pqp *) * = pqp * + PQ * p * = P (q + Q *) p * = 2 S (q)

(P is a unit of four elements, P-1 equals P *)

 

 

 

Conversion from a euclidean angle to a quaternary number

Define pitch, yaw, and roll to rotate radians around the X axis, Y axis, and Z axis respectively.

Float P = pitch * piover180/2.0;

Float y = yaw * piover180/2.0;

Float r = roll * piover180/2.0;

 

Float sinp = sin (P );

Float Siny = sin (y );

Float SINR = sin (R );

Float COSP = cos (P );

Float cosy = cos (y );

Float cosr = cos (R );

 

This-> X = SINR * COSP * cosy-cosr * sinp * Siny;

This-> Y = cosr * sinp * cosy + SINR * COSP * Siny;

This-> Z = cosr * COSP * Siny-SINR * sinp * cosy;

This-> W = cosr * COSP * cosy + SINR * sinp * Siny;

 

Normalise ();

 

3. Use MATLAB for related computing

 

Calculates the rotation quantity of the two vectors V1 and V2, and enables V1 to reach V2 after applying p.

 

If the rotation axis from V1 to V2 is V and the rotation angle is Theta, then q = [V * Cos (theta/2) sin (theta/2)]

Matlab code:

Function q = vector2q (V1, V2)

%... Normalize ....

Len1 = SQRT (V1 * V1 ');

Len2 = SQRT (V2 * V2 ');

V1 = V1/len1;

V2 = v2/len2;

Angle = V1 * V2 ';

Axis = cross (V1, V2 );

Alen = SQRT (axis * axis ');

Axis = Axis/Alen;

T = ACOs (angle );

T = T/2;

Q (1) = Axis (1) * sin (t );

Q (2) = Axis (2) * sin (t );

Q (3) = Axis (3) * sin (t );

Q (4) = cos (t );

 

End

 

 

 

 

After Q is calculated, the corresponding rotation matrix and rotation matrix can be obtained.

The matrix in MATLAB is column-oriented

Function r = q2rot (q)

W = Q (4 );

X = Q (1 );

Y = Q (2 );

Z = Q (3 );

 

R = zeros (3, 3 );

R () = 1-2 * y * y-2 * z;

R (1, 2) = 2 * x * Y + 2 * w * z;

R () = 2 * x * Z-2 * w * Y;

 

R () = 2 * x * y-2 * w * z;

R () = 1-2 * x * X-2 * z;

R (2, 3) = 2 * z * Y + 2 * w * X;

 

R (3, 1) = 2 * x * z + 2 * w * Y;

R () = 2 * y * Z-2 * w * X;

R (3, 3) = 1-2 * x * X-2 * y * Y;

R = R ';

End

At the same time, you can also calculate the Euler's Point Based on the Quaternary element.

 

Function r = q2euler (q)

W = Q (4 );

X = Q (1 );

Y = Q (2 );

Z = Q (3 );

 

T11 = 2 * (w * x + y * z );

T12 = 1-2 * (x * x + y * y );

R (1) = atan2 (T11, T12 );

 

T2 = 2 * (w * Y-z * X );

R (2) = Asin (T2 );

 

T31 = 2 * (w * z + x * y );

T32 = 1-2 * (y * Y + z * z );

R (3) = atan2 (t31, T32 );

 

End

The calculated x-ray RX, Ry, and RZ are the rotation angles around the x-axis, Y-axis, and Z-axis respectively. For example:

Rotq = q2rot (q)

R = q2euler (q)

[Rotx roty Rotz] = rotation (r)

 

It can be found that rotq = Rotz * roty * rotx

From this we can see that the rotation sequence of the rotation matrix calculated by using the four elements above is the X axis, Y axis, and Z axis respectively.

 

Ra = PI/4;

Qz = [0 0-sin (RA) Cos (RA)] % rotate around Z-90 degrees

Qy = [0 sin (RA) 0 cos (RA)] % rotate around y 90 degrees

 

Qyz = qmult (QY, qz)

R = q2euler (qyz)

The result obtained by R is

R =-1.5708 0.0000-1.5708

That is to say, the geometric meaning is to first rotate around the X axis-90 degrees, then rotate around the Z axis-90 degrees, based on the multiplication of QY and qz, the actual operation is to first rotate around the Z axis-90 degrees, and then rotate around the Y axis 90 degrees, but the result is that these two operations are equivalent,This indicates that there can be multiple solutions from the Quaternary element to the ouarla corner.

 

There are two semicolons. If they are in the opposite direction, the values of the new vectors obtained by applying them to the vectors are still equal.

Q1 = [0.024666-0.023954 0.504727 0.862594];

Arm = [-8.881719 6.037597-2.36776];

Q2 =-Q1;

Rot1 = q2rot (Q1 );

Rot2 = q2rot (Q2 );

V1 = rot1 * arm'

V2 = rot2 * arm'

 

The V1 calculated above is equal to V2.

 

 

The cosine value of the element is their inner product.

If the cosine value is less than 0, we need to reverse one of them, because we know above that a ry is the same as its inverse ry for a vector.

 

 

The multiplication of the four elements indicates the accumulation of rotation.

PQ = p * q;

Rotp = q2rot (P );

Rotq = q2rot (Q );

Rotpq = q2rot (PQ );

Rotmul = rotp * rotq;

 

Here rotpq is equal to rotmul

 

 

Iv. Functions of the quaternion class in ogre

 

1. From a quaternary element to a rotating vector

Void quaternion: torotationmatrix (matrix3 & krot) const

1-2 * qy2-2 * QZ2

2 * QX * QY-2 * qz * QW

2 * QX * qz + 2 * QY * QW

2 * QX * Qy + 2 * qz * QW

1-2 * qx2-2 * QZ2

2 * QY * qz-2 * QX * QW

2 * QX * qz-2 * QY * QW

2 * QY * qz + 2 * QX * QW

1-2 * qx2-2 * qy2

 

2. Rotate the number to the Quaternary number.

According to the table in 1, there are:

4 * (1-qx2-qy2-qz2) = 1 + m00 + M11 + m22

Qw2 = 1-qx2-qy2-qz2, available

4 * qw2 = 1 + m00 + M11 + m22

Here, QW must ensure that 1 + m00 + M11 + m22> = 0. If not, construct other equations for calculation. There are two cases in ogre, one is m00 + M11 + m22> = 0, and QW can be obtained first. Otherwise, another equation is used for calculation.

 

3. Local axis

Vector3 xaixs (void) const;

Obtain the first column of the rotation matrix. If the rotation matrix is multiplied by a vector, the data in the first column is multiplied by the X component of the vector.

Vecotr3 yaxis (void) const;

Obtain the second column of the rotation matrix. If the rotation matrix is multiplied by a vector, the data in the second column is multiplied by the Y component of the vector.

Vecotr3 zaxis (void) const;

Obtain the third column of the rotation matrix. If the rotation matrix is multiplied by a vector, the data in the third column is multiplied by the Z component of the vector.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.