(serial) Drinking coffee and Learning unity--chapter II Preparatory Knowledge System (2)

Source: Internet
Author: User
Tags svm

What is a matrix?

A rectangular table formed by the number of MN rows n columns is called a MXN matrix.


Row vector and column vector

Because vectors we can be seen as a special matrix, that is, only one row or only a single column of the Special matrix. So we call a vector of only one row, and a single column of vectors is called a column vector.

Note that our program actually stores the MN number in a linear table when the matrix is stored, so getting the elements in the first few rows of a matrix has a precedence principle. Here, in particular, unity has such a word in the Handbook of the Matrix4x4 class:

Matrices in Unity is column major. Data is accessed as:row+ (column*4). Matrices can indexed like 2D arrays but in an expression likemat[a, b], a refers to the row index, while B refers to th E column index (Notethat This is the opposite-round to Cartesian coordinates).

Unity The matrix in is used for column precedence. Data is obtained by row + ( column ) . Matrices can be indexed in the form of a two-dimensional array, but with an expression like mat[a,b] . Where a represents the line number andb represents the column number (note that this is exactly the same as the Cartesian coordinate system).


Mathematical representations of line vectors and column vectors:




0 matrix

All elements are a matrix of 0.

Unit matrix

A matrix of the following form is called the Unit matrix



Its characteristics are obvious: all elements of the line number and column number equal to the element is 1, otherwise 0, that is, from the top left to the right of the diagonal is 1, the other elements are 0 of the matrix. About the specificity of the unit matrix is mentioned later.

Matrix Equality

Two matrices with the same dimension and equal all elements we consider to be two equal matrices.

Matrix addition, matrix subtraction, matrix multiplication

These 3 operations are relatively simple and therefore written together. They represent the matrix of the number added and subtracted from the corresponding rows and columns respectively. Multiplying a number from a matrix is the matrix in which all the elements are multiplied by this number.

Transpose matrix



Matrix multiplication

Matrix multiplication is special, and we first make sure that the process is meaningful when we multiply the matrix. The number of rows of the left multiplicative matrix of a matrix multiplication with meaningful preconditions is equal to that of the right multiplicative matrix.

Mathematical process: The number of columns of matrix A is equal to the number of rows in matrix B, then the elements of the J column of row I in matrix C are multiplied


Since only the number of columns of a is equal to the number of rows B, we can imagine that a 4-tuple row vector has 4 columns, so he can multiply the left by a 4x4 matrix. A 4-tuple-column vector has 4 rows, so he can right-multiply a 4x4 matrix. Because the row vector and the column vector appear to be just the difference between the writing form, the line vector and the column vector have a transpose relationship.

Matrix multiplication does not satisfy the commutative law, but in cases where multiplication is meaningful, there are:

In matrix operations, the multiplication of vectors and matrices is divided into two types: the row vector left multiply and the column vector right multiply.

The line vector left multiplication and the column vector right are not much different from what is essentially expressed, just because the row vector and the column vector exist the transpose relation, each game engine has his writing habit, as well as the esthetic preference. The line vector is used in DirectX with the left multiply, while the column vectors used in OpenGL are right-multiplying.


Inverse matrix

A matrix with inverse matrix first needs to satisfy a condition: The matrix is a non-singular matrix or a full rank matrix, or the determinant of the Matrix ≠0.

The matrix A satisfies the condition exists a inverse matrix B, which makes the ab= unit matrix I.

We remember it as


Spatial transformations

Understanding the coordinate system, vectors, matrices, we can begin to understand the spatial transformation, and this part of the knowledge basically covers the most of the current areas related to graphic image development.

We have a preliminary understanding of the definition, characteristics and algorithms of the vector matrix, but the linear algebra does not tell us the geometrical meaning of the vector and matrix operations. This book will lead the reader to explore the spatial geometric meanings of vector matrix operations.

Matrices can perform any kind Ofliner transform.

Generally speaking, a matrix can describe arbitrary linear transformations. A spatial transformation that satisfies two points is called a linear transformation: The origin does not move, and the parallel relationship between the line segment and the line remains unchanged. In other words, a linear transformation might stretch the coordinate system, but not distort it.

The rotation (rotation) and scaling (scaling) of any graph belongs to the linear transformation: the parallel relationship between the segments has not changed and the origin has not moved. and the translation (translation) transformation is not a linear transformation, no matter which point before the transformation is the original point of the coordinate system, the transformed origin must have shifted.

So we say that linear transformation is a change of the length of the segment, the angle between the segments, the area of the graph, and the volume.

Now let's solve the question in our minds: How does the Matrix complete the transformation? How does a NXN number make our coordinate system shaking?

Let's start with the imagination in the two-dimensional space.

Suppose we have a picture in a planar Cartesian coordinate system whose 4 vertices are (0,0), (1,0), (0,1).

Then assume that there is such a 2x2 matrix:

What does it mean for us to transform this graph with such a 2x2 matrix?

In fact, this matrix can be regarded as 2 line vector p=[2,1],q=[-1,2]. We draw these 2 vectors in a planar Cartesian coordinate system:




In fact, this 2x2 matrix is meant to transform the original x-axis into the direction where the P-vector sits, transforming the original y-axis into the direction of the Q vector. Because the coordinate system origin does not move, this is a linear transformation. Essentially this is a rotation transformation, we can extend our left hand, thumb pointing to X, index finger to Y, and then imagine any shape placed in two fingers, then the thumb from the original direction to the P direction, the index finger from the original direction to the Q square image, this process is our matrix described by the transformation process. The original image rotates 25° counterclockwise.

-





Not only the rotation, but the length of our graph is changed from the original 1 to the change. So this matrix also stretches our graphics.

In this example, our p and q are exactly perpendicular tangent lengths, thus constituting a square, and in fact, more generally, they constitute a parallelogram. This parallelogram we call "deflection box". Two-dimensional deflection box we can use two fingers, three-dimensional deflection box we can use 3 fingers to gestures, so readers try to think of the 3x3 matrix in the linear transformation of the deflection box.

We'll look back at our definition of linear transformations: the parallel relationship between segments does not change; The origin does not move. It can be seen that 2x2 matrices and 3x3 matrices are good for expressing linear transformations in 2D and 3D space respectively.

Looking closely at the above transformations, let's say we rotate the paper on the right to make the image "back to the positive", we rotate the 25° for the paper, so we throw a concept:

We rotate a shape or object at a certain angle, which is equivalent to a negative rotation of this coordinate system at this angle.





See here in fact we can see a very very very obvious rule, that is, the transformation matrix can appear in the Cos of the place must be in the same row number (a diagonal line), any row number is not equal to the occurrence of sin.

The 3 coefficients in the scaling matrix correspond to the extent to which the 3 axes of the original image were scaled.

A special kind of linear transformation we call mirroring. For example, the transformation represented by the 2-D transformation Matrix, whose transformed image is folded like a mirror, appears on the other side of the y-axis.


Shear Transformations

at the end of the linear transformation, we mention the shear transformation, which is a special kind of transformation whose transformation process is the "twisted" coordinate system. The process expects the reader to consult the relevant information on their own.

So far we have studied the matrix representation and geometrical meaning of linear transformations. In spatial mathematics, we think that the multiplication of coordinates and matrices is equal to the transformation itself.

If there is doubt in the reader's mind, the translation transformation is not a linear transformation, can it be expressed by matrix?

The answer is yes, but there is one condition: translation transformations in n-dimensional space cannot be expressed by nxn or smaller matrices, at least (n+1) x (n+1) matrix. Let's see why?

For the linear transformation matrix in 2D 3D, we split it into SVM P,q/p,q,r, which is equivalent to the number of axes. We can understand that the process of linear transformation is the process of transforming the unit vectors on the corresponding axes into the SVM represented by the matrix. The process must not include panning.

So how can we express the translation?

We said before that the multiplication of coordinates and matrices equals the transformation itself, so we assume that the 2-coordinate (2,2) line consists of a segment that is written as a 2-row vector [1 2], which is left-multiplying by a 2x2 unit matrix (we think the 2x2 unit matrix is an X, A scaling matrix with a scale factor of 1 in the Y direction) completes the linear transformation:


[1 1] The result of the transformation is still [1 1],[2 2] in the same vein. The resulting segment (graphic) does not have any change of picture

We make any modification to this transformation matrix: the values on the [1 1] diagonal are modified to show that the transformation process produces equal-to-scale scaling. The values that are not on this diagonal are modified to show that the transformation process produces a rotation.

In addition to this 2x2 matrix there is no other place that allows us to modify the possibility of creating a translation transformation. So we have to come up with a bold idea: can we get a matrix that contains more numbers to complete the transformation, so that the matrix has a place where we can modify its value so that the original image has a nonlinear transformation?

The answer is yes, the change and sublimation of thinking in the process of cognition is essential. So the idea of genius is raised: we express transformations and points in 2-dimensional space with 3-dimensional matrices and vectors.

Let's see, for the point [1 1] after extending a dimension [1 1 1], we give the following requirements for this point coordinate after the extension:

This is not a point in a 3-dimensional space, it is still the point in the 2-dimensional space [1 1]. The third dimension we call a W. We can not imagine the point of this [1 1 1] when its meaning, we only see this point in the 2-dimensional space of the shadow (projection), we have this shadow in turn to imagine it. for points [1 1 1],[2 2 2],[3 3 3], they are projected to be [1 1] At this point in the two-dimensional space, and the projection process will divide the preceding 2 dimensions by the third dimension W respectively. When w=0, division is meaningless, so we think that the point [x,y,0] in the two-dimensional space is the meaning of infinity .

This space we call the 3-dimensional homogeneous space of this 2-dimensional space. The coordinates of this [x,y,w] form, which we call the 3-dimensional homogeneous coordinates of the 2-dimensional coordinates. Similarly, our 2-dimensional unit matrix, when W=1, corresponds to a 3 Veasey sub-unit matrix


Again we transform the Zizhi vector with the left multiplication expression of the homogeneous unit matrix:

[x y W], at which point the number of left matrix columns is still equal to the number of right matrix rows, and multiplication still has meaning.

And we'll see if there's a place in the homogeneous matrix where we can translate the coordinate system to make it possible?




Data items of 2-dimensional matrices in homogeneous matrices we don't have to try, we try to modify the more ABCD.

[x y W] =[x+a,y+b,w] (in the result matrix:)

The incredible thing happened, because of our definition of homogeneous space, arbitrary point [x,y,w],x/w, y/w equal two points we think is the same point. So we can see that this transformation matrix makes any point in the original coordinate system [x Y] transform into a [X+a y+b]! that is, any shape, the segment is shifted in the x direction of a, and in the y direction is translated B.

Conclusion: for the graph (coordinate system) in n-dimensional space, the NXN matrix can express the linear transformation and introduce the N+1 Veasey sub-space, which can be used to express the translation transformation by n+1 Veasey sub-matrix multiplication. But the original linear transformation can still express the linear transformation in the original corresponding position in the homogeneous matrix.

So the homogeneous matrix unifies the linear transformation and the translation transformation. We call the transformation of the linear transformation and the translation transformation the affine transformation .

In other words, a nxn matrix can express arbitrary affine transformations in n-1-dimensional space.

Thus we get the translation matrix in three-dimensional space, which is obviously a 3+1=4-dimensional matrix:



We return to the geometrical level, we think that the position of a meaningful amount, the translation is meaningful, there is no position of the concept of the amount of our translation is meaningless. The most typical is the dot and the vector. Although we say that vectors can be expressed by the end point, the dots in the narrow sense are positional, and the vectors are only size and direction. Therefore, we cannot translate transformations on a narrow vector.

Perspective projection transformation and projection matrix

Let's simply mention the projection transformation. A projection transformation typically occurs when an object in a 3-dimensional space is projected onto a plane (a shadow that is illuminated by the sun is projected on the wall). And we express that the simplest 3 planes of a 3 dimensional plane face are x=d,y=d,z=d respectively. They are each a plane with a dot distance d.

We assume that we want to project a three-dimensional graph onto the z=d plane, so that any homogeneous point [x y z] is projected onto the Z=d plane, and the resulting point coordinates should be: [x y z]/(z/d) =[x y d] removes Z from the 2-dimensional space (graph). So we construct a homogeneous space with a W of 1 and another [x y z 1] to [x y z z/d], then the matrix is constructed as:





any point [x y z 1] is multiplied with the matrix to get [x y z z/d],[x y z z/d] back to the original space [x y d], which is the projection matrix of the plane projection to the z=d in three-dimensional space.

Azimuth and angular displacement

After mastering some basic knowledge of vectors and matrices, we ask ourselves a question:

Does a vector have angular displacements? Does an object have angular displacement?

We know that vectors are only size and direction, and even if you force a vector to revolve around itself, the vector still has no change in size and direction after the rotation, and we think the vector is still the vector. But if we let an object revolve around itself, like turning a person over, do we still think it's the same? Due to the gravity of the Earth's relationship, the upside-down person can clearly feel that their state has changed. And this change is not just about the displacement that we get from one place to another. In physics we call this man a angular shift.

We refer to the degree of rotation as angular displacement. Displacement is often used to express the position of an object, and angular displacement is often used to express an object's orientation .

We know that displacements in n-dimensional spaces can be represented by n numbers. For example, a point in 2-D space moves toward X 5, moving toward Y 10, then the displacement is expressed by 5, 102 digits.

So how does angular displacement in 3-dimensional space be expressed?

Matrix expression Angular Displacement

We can construct a 3-dimensional rotational transformation matrix to express angular displacements in 3-dimensional space (a matrix we have already given). This expression is very straightforward, but not concise. We understand the advantages and disadvantages of the matrix expression angular displacement

Advantages: Immediate, more suitable for the underlying graphics API directly to use, multiple angular displacement connections only need to be continuously multiplied to obtain the final angular displacement, only need to simply transpose the matrix to obtain the inverse of the angular displacement;

Cons: Take up more space (3-dimensional space requires 9 numbers to express angular displacement); obscure (I think the reader should have some experience); There are redundant data, possibly matrix creep.

Euler angle (Euler Angle) expression angular displacement

God horse? We simply describe a situation where an object is rotated to move out a 3x3 matrix? We can't bear it! So the Euler angle mentioned at the beginning of this chapter comes up. Euler's horn is presented by the famous mathematician Euler (Eulerian) and named after him. Let's see what angle the Euler angle is, what's the point of Euler's horn:

The Euler angle decomposes the rotation of a three-dimensional object into a rotation of axes that revolve around 3 22 perpendicular to each other. As for which 3 axes, in what order decomposition, Euler did not tell us, but the most significant case is that we press the 3 axes of the 3-dimensional coordinate system in a certain order to decompose the rotation. There are many kinds of conventions in this order, so let's mention the "Heading-pitch-bank" convention here.

That is, in the left-handed coordinate system, we continue to extend the left hand, 3 fingers 22 Vertical, we have referred to the left hand coordinate system, the index finger pointing to the y+ direction of the rotation of the amount we call heading, the middle finger pointing to the z+ direction of rotation We call the bank, thumb pointing to the z+ The rotational amount of the direction we become pitch.

Thus, we decompose an object's arbitrary rotation into 3 rotations, respectively, and get 3 angles from each other. We can describe nearly all rotations by a sequence of 3 numbers! But Euler's angle is still unable to express all the rotations. The most famous is the deadlock phenomenon of universal joints. That is, when the pitch=+-90° heading and the bank are vertical thereby reducing a degree of rotation, this problem has not been any good solution.

Advantages: Easy to use, the smallest number of occupied, concise, all of the Euler angles expressed by the angular displacement is legal.

Cons: Not unique (360° and 0° Although the values have changed but the orientation of the expression has not changed); The most difficult interpolation (it is difficult to interpolate between the two Euler angles, which is very annoying for us to join the animation); Vientiane lock problem.

Next we recognize the last way to express angular displacement:

Four yuan (quaternion) expression angular displacement

About four yuan what is left to the reader to extend their reading, here only four yuan is essentially a geometric interpretation of the complex number (a,b,c,d).

The complex number (A,B,C,D) has a real part a,3 a imaginary part is b,c,d respectively. Complex numbers can be added, reduced, multiplied, and cross-multiply, can be obtained by its conjugate complex number, but also to calculate the modulus and inverse of complex numbers.

Four the number of dollars how to express angular displacement, we let n be the axis of rotation direction of the unit vector, then four Yuan q=[]

Expression of this angular displacement. where NX NY nz is the projection vector of the axis of rotation of the axes respectively.

We specifically proposed a four-dollar interpolation (SLERP). Four-dollar interpolation process we only need to know that there is a start state Q, the end state p, the interpolation parameter T (0<=t<=1), then the SLERP function can be very fast and smooth in the Q and P two states to insert a rotation state. Its mathematical and geometrical explanations are also for readers to extend their reading.

Advantages: The SLERP provides the most smooth interpolation between 2 bearings, the connection and inversion of angular displacements are more rapid (compared to the matrix), each angular displacement occupies 4 number of memory overhead, medium overhead;

Disadvantage: The most difficult to use (light understanding it has been compared to science fiction); There may be a group of four of dollars to express an illegal angular displacement.

We know 3 ways to express angular displacement, specifically in the development of which to express the need to understand their respective points of superiority, according to the actual situation of choice. The angular displacements of these 3 ways can be converted to each other, the mathematical process is more complex, we can directly invoke the existing API to complete.

Geometric entities

Continue tomorrow ....





(serial) Drinking coffee and Learning unity--chapter II Preparatory Knowledge System (2)

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.