Conversion of the two elements of a matrix based on the four elements

Source: Internet
Author: User
Conversion of the two elements of a matrix based on the four elements

Ry is a mathematical tool that can replace the matrix and the Euclidean angle. He was initially discovered by William Rowan Hamilton (see Wikipedia), and its biggest feature is that it does not meet the exchange rate. I also want to talk about my own experiences on this point. In discrete mathematics, the concepts of semi-groups, groups, rings, and domains are mentioned. The central definition of this concept is to have exchange rate and allocation rate (For details, refer to the mathematical definition of the ring ), the concept of domain is to add the exchange rate on the basis of the ring. Therefore, the Quaternary element cannot satisfy the definition of a domain. It is a division ring. What is a division ring? In fact, it is very simple. The divisor and divisor both satisfy the combination Law and the allocation law. However, to satisfy the exchange law, that is, the divisor and divisor exchange location, the result is different (to be accurate, if they are not 0, the result is reciprocal ). In addition, to obtain I3 =-1 in a four-dimensional solution space, I, J, and K can only be obtained without meeting the exchange rate.

The advantage of the Quaternary element in computer graphics lies in its small amount of computation and good for interpolation, and its rotation has no defect. However, a general definition of a quaternary element is difficult to understand, and OpenGL APIs do not contain functions with parameters of the Quaternary element. Therefore, we need to make a small conversion according to the formula.

Original article, opposed to unstated reference. Original blog address: http://blog.csdn.net/gamesdev/article/details/10036105

Assume that Q (x, y, z, W) is the result of the Alpha rotation of vector A (XA, ya, za, then X, Y, Z, and W are:

X = sin (α/2) · XA

Y = sin (α/2) · ya

Z = sin (α/2) · za

W = cos (α/2)

In my previous blog, I learned how to use a matrix to represent the rotation of the coordinate system along the vector. Here I directly give the formula:

Combine these two formulas. Combined with the halfwidth formula learned by senior high school:

Sin α = 2sin (α/2) · cos (α/2)

Cos α = cos2 (α/2)-sin2 (α/2)

Cos2 (α/2) = (1 + cos α)/2

Sin2 (α/2) = (1-Cos α)/2

We can solve the following problem:

We should verify the correctness of the formula. Similarly, we should use OpenGL-hosted matrices to test whether there is a gap between the glrotatef () function and the formula we wrote.

First, I wrote a simple quaternion class, which is defined as follows:

# Ifndef quaternion_h # define quaternion_h # include <qtglobal> # include <math. h> class quaternion {public: quaternion (float _ x, float _ y, float _ z, float _ w) {x = _ x; y = _ y; z = _ z; W = _ w;} void tomatrix (float matrix []) {q_assert (Matrix! = Q_nullptr); # ifndef matrix # define matrix (row, col) matrix [row * 4 + Col] # endif matrix (0, 0) = 2 * (x * x + W * w)-1; matrix (0, 1) = 2 * (x * Y + z * W); matrix (0, 2) = 2 * (x * z-y * w); matrix (0, 3) = 0.0f; matrix (1, 0) = 2 * (x * Y-z * W); matrix (1, 1) = 2 * (y * Y + W * w)-1; matrix (1, 2) = 2 * (y * z + x * w); matrix (1, 3) = 0.0f; matrix (2, 0) = 2 * (x * Z + y * w); matrix (2, 1) = 2 * (y * z-x * w); matrix (2, 2) = 2 * (z * z + W * w)-1; matrix (2, 3) = 0.0f; matrix (3, 0) = 0.0f; matrix (3, 1) = 0.0f; matrix (3, 2) = 0.0f; matrix (3, 3) = 1.0f; # UNDEF matrix} static quaternion fromrotation (float _ x, float _ y, float _ z, float angleindegree) {// vector unitization float length = SQRT (_ x * _ x + _ y * _ y + _ z * _ z); q_assert (! Qfuzzycompare (length, 0.0f); // do not want the length to be 0 _ x/= length; _ y/= length; _ z/= length; float alpha = angleindegree/180*3.1415926; // return quaternion (sin (alpha/2) * _ x, sin (alpha/2) * _ y, sin (alpha/2) * _ z, cos (alpha/2);} private: Float X, Y, Z, W ;}; # endif // quaternion_h

The following shows how to call this class.

#include <assert.h>#include <stdio.h>#include "GLWidget.h"#include "Quaternion.h"void PrintMatrix( float matrix[16] ){    Q_ASSERT( matrix != 0 );    printf( "%8.2f%8.2f%8.2f%8.2f\n"            "%8.2f%8.2f%8.2f%8.2f\n"            "%8.2f%8.2f%8.2f%8.2f\n"            "%8.2f%8.2f%8.2f%8.2f\n",            matrix[0], matrix[1], matrix[2], matrix[3],            matrix[4], matrix[5], matrix[6], matrix[7],            matrix[8], matrix[9], matrix[10], matrix[11],            matrix[12], matrix[13], matrix[14], matrix[15] );}GLWidget::GLWidget( QWidget* pParent ):    QGLWidget( pParent ){    setWindowTitle( "Test OpenGL Quaternion and matrix" );}void GLWidget::initializeGL( void ){    float angle = 30.0f;    float x = 12.0f;    float y = 8.0f;    float z = 3.0f;    float matrix1[16], matrix2[16];    glMatrixMode( GL_MODELVIEW );    glLoadIdentity( );    glGetFloatv( GL_MODELVIEW_MATRIX, matrix1 );    glGetFloatv( GL_MODELVIEW_MATRIX, matrix2 );    printf( "The initial identity matrix is:\n" );    PrintMatrix( matrix2 );    printf( "Now perform OpenGL glRotate function.\n" );    glRotatef( angle, x, y, z );    glGetFloatv( GL_MODELVIEW_MATRIX, matrix1 );    PrintMatrix( matrix1 );    printf( "Now using quaternion to perform rotation.\n" );    Quaternion::FromRotation( x, y, z, angle ).ToMatrix( matrix2 );    PrintMatrix( matrix2 );}void GLWidget::paintGL( void ){}

The following are the running results.

This indicates that the preceding operation is correct.

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.