3Д принтеры и всё что с ними связано

ЗД принтеры АБС пластик

Converting Between Matrices and Quaternions

One of the most important aspects of quaternions is their ability to be converted into rotation matrices. This is useful because graphics APIs such as OpenGL and Direct3D cannot accept quaternions directly rather, they rely on matrices to perform transformations. Because of this, without being able to convert between quaternions, your graphics would become useless because you would not be able to use the infor­mation they contain along with OpenGL or other graphics APIs. For example, if you have two sets of rotations for an object, the first set is

the starting rotation and the second is the ending rotation. Without being able to convert these matrices to quaternions, it would be impossible to take advantage of the ease of interpolation between quaternions. Keep in mind that all matrices will be referenced in the manner mij where i is the row and j is the column.

This section first investigates how to convert a quaternion to a matrix. Because there is much more to cover in the later chapters of the book, there is not room to go through the “whys” of the conversion. Instead, this section looks at the formula for the conversion. Figure 2.5 shows how to generate a 3×3 rotation matrix from a quaternion. Keep in mind that when writing code to do the conversion, it is helpful to calculate some of the multiplication ahead of time to avoid unnecessary overhead.

w2 + x2 — y2 — z2 2xy-2wz 2xz+2yn

2xy+2wz w2 — x2 + y2 — z2 2yz-2wx 2xz-2wy 2yz-2wx w2-x2-y2 + z2

Figure 2.5 Converting a quaternion to a rotation matrix. W represents the scalar value and X, Y, and Z represent the three parts of the vector component.

Converting back to a quaternion is a little bit harder, but not much. The first thing you do is calculate the trace of the matrix using the formula (tr = m11+m22+m33). If that value comes out to be greater than zero, you can perform an “instant calculation” using the follow­ing formula:

temp = 1

2 /tr+ 1 0.25

Qw =


Qx = (тгэ ~ mзг) x temp qy = (m31 — m13) x temp

qz = (m12 — m21) x temp

If it is less than or equal to zero, you have to take a different approach that depends on which element of the major diagonal is the largest. If the upper-left corner is the largest, you use this formula:

temp =———- 1

2/l + — m22 + m33


Qw —


qx = (m2, + m12) x temp qy = (m13 + m31) x temp qz = (m32 — m23) x temp

If the middle element outshines the others, use this formula: 1

temp =

ff?22 — /П33

Qw = (/ттгг + m12) x temp 0.25

Qx =


Qy = (m32 + m23) x temp qz=(m13-m31)xtemp

And finally, if the lower-right corner takes the cake, use this one: temp = 1

+ ГП33 /77 / у Л?22

Qw= (m13+ m31)xtemp

Qx = (m32 + m23) x temp

_ 0.25 temp

Qz = (m2) — m12) x temp

These formulas convert your rotation matrix into a quaternion so you can take advantage of the many advantages they have, such as easy interpolation, cheaper multiplication, and less storage space.

Your email address will not be published. Required fields are marked *