Mathematical Disciplines


When working in MEL, there are three mathematical disciplines we often use. Algebra, trigonometry, and vector mathematics form the basis for the majority of all calculations carried out within MEL. Although there are situations where advanced mathematics such as analytical geometry and complex matrix calculations might be necessary, these are few and far between for the average scripter.

Algebra

Algebra is a subject with multiple layers of complexity, and while most of the calculations and equations we deal with in the realm of computer graphics rely on geometry and its associated laws, algebra provides a foundation for the calculations carried out within geometric equations. Once we achieve an understanding of algebra, we will be able to approach any equation with confidence.

One of the basic concepts of algebra is the order of operations. Within algebra, these are defined by the associative , commutative , and distributive laws. We do not want to have an extended discussion of algebraic theory and will limit our discussion to how it affects MEL. In MEL, operators have certain precedence over others. For example, multiplication and division are carried out before addition and subtraction. If there are multiple operations of the same precedence in one equation, we do the operations from left to right. See Example 2.2.

Example 2.2: Operator precedence in action.

click to expand

There are times, of course, when we might want to carry out an addition operation before using the results in a multiplication operation. To do this, we use a concept known as nesting or grouping . We use parentheses, (), to enclose functions we want to calculate first. We can even enclose one grouped operation inside of another, hence the term nesting . If you think of this almost as a hierarchy, you calculate all the operations on the same level at the same point in the calculation, again from left to right. See Example 2.3.

Example 2.3: Grouping different functions together.

click to expand

It is also important to note that within mathematical notation, it is possible to imply multiplication, as seen in Example 2.4.

Example 2.4 : Implied multiplication.

click to expand

This shorthand is not possible within MEL, but we will use it when appropriate in written formulae.

Algebra is most often used to solve for an unknown quantity. The idea of having a number as unknown is the basis for most algebraic equations. What we try to accomplish to solve an algebra problem is to get all the known information on one side of the =. We use letters to represent these unknown quantities , such as x , a , or i . There are some guidelines as to which letters are supposed to represent what, but it is really up to you. Most often, an unknown quantity is given the letter x. Whenever you do any math equation, you are essentially solving for an unknown, as seen in Example 2.5.

Example 2.5: Solving for the unknown quantity, represented by x.

click to expand

Once we start solving for more complex unknown quantities, we need to begin flexing some of our algebraic muscles . This process is shown in Example 2.6.

Example 2.6: Solving for x by using the inverse operations.

click to expand

What is handy about these notations is they are used across disciplines, and even into other disciplines that require mathematical equations, such as physics. A tenet of Newtonian physics is that force=mass acceleration , or f = ma . Once two of these factors have been determined, we can solve for the missing value.

As we have seen, algebra finds its way into multiple math disciplines, and having at least a basic understanding of the algebraic processes will come in handy as we explore trigonometry, vectors, and analytical geometry.

Trigonometry

Trigonometry is the study of the mathematical relationships that exist within triangles. As triangles are the basic foundation of all geometry that exists in 3D computer graphics, an understanding of trigonometry is a good thing for a scripter to have.

The basic foundation of trigonometry is that there exists within all triangles a relationship between the length of their sides and the angles of the triangle. The relation of each of the three sides and the three angles of a triangle is an exact and nearly magical science.

A triangle containing a 90 , or right , angle is referred to as a Right Triangle . The side opposite the right angle is called the hypotenuse . The other sides of the triangle do not have specific names, but names given based on the angle we are either trying to find, or that we know if we are trying to determine the length of a side. In Figure 2.1, we use the symbol ²to represent the angle used in the equations to follow.

click to expand
Figure 2.1: A triangle for use in the following equations.

The three basic trigonometric functions are sine , cosine , and tangent . The trigonometric ratios given by these three functions are shown in Example 2.7. Note that we have used the common mathematical abbreviations for each of these functions. Coincidently, these common abbreviations are also the MEL commands for these same functions.

Example 2.7: The three basic trigonometric functions.

click to expand

Sine, cosine, and tangent values are known. Therefore, as long as we know the length of one side and any other piece of information on a right triangle, be it the length of another side or any angle in that triangle, we can use algebra as seen in Example 2.8 to solve for the unknown quantities for the triangle shown in Figure 2.2.

click to expand
Figure 2.2: A triangle in need of solving.

Example 2.8: Finding the length of the unknown side.

click to expand

When the triangle is not a right triangle, we employ the sometimes more useful sine rule . The sine rule states:

click to expand

Where side a is opposite angle A, side b is opposite angle B, and side c is opposite angle C. We can use the sine rule to find the dimensions of nearly any arbitrary triangle.

Luckily, because Maya essentially acts as a operating system specialized for doing 3D graphics when you re working in MEL, finding many of these values is a simple process, as are the functions to calculate those you can t simply query from your scene.

Vector Mathematics

The term vector refers not to a method of mathematics, but rather to a type of number. Of course, the math related to vectors has its own set of specific rules and some specific operations, which we ll cover.

The numbers we have used so far are actually rather simple compared to vectors. A vector contains multiple pieces of information, and not just because it contains three numbers . For example, a single vector can contain the direction an object is traveling and its current speed.

Note  

In the wider world of math and physics, not all vectors have three components . Within computer graphics, however, this is both the most common type, as well as the type MEL uses.

A vector is a very special type of number in 3D space, and possibly the most confusing to artists. As 3D artists , we actually deal with vectors all the time and never realize it. In creating a hierarchy of objects, assuming the transforms of those objects haven t been reset, the translate values of each object represent a vector between that object and its parent, even if that parent is the world. In Figure 2.3, we see a visualization of a vector from the origin.

click to expand
Figure 2.3: A vector visualized in 3D space.

As we learned, vectors contain multiple pieces of information, the obvious being a point in Cartesian space. However, that vector also has a length. We call this value the magnitude of the vector. A vector s magnitude can be used to represent not just distance, but speed, the intensity of a physical force such as a gravity field, or any item that contains both direction and intensity. It is no coincidence that the attribute on dynamic fields in Maya that controls the intensity is called magnitude.

Mathematical vector notation can be done in column or row formatting. Each number in the vector is referred to as a component . For purposes of clarity, we refer to these components as x , y , z . We use bold letters to signify a vector within standard mathematical notation ” please refer to Example 2.9.

Example 2.9: Vector notation.

click to expand

If we want to represent the magnitude of a vector, we enclose the vector representation in vertical braces, as in

To determine the magnitude of a vector by hand, we use the formula

To visualize a simple vector, we assume that the vector s tail , or beginning, is at the world origin. The head is placed at the coordinates of the vector, depicted as an arrowhead in Figure 2.4.

click to expand
Figure 2.4: The standard depiction of the vector.

Vectors, like any type of number, can be used in mathematical equations. Adding and subtracting vectors from each other is a rather simple matter of adding or subtracting each of the components together, as seen in Examples 2.10 and 2.11. These two equations are visualized in Figures 2.5 and 2.6.

Example 2.10: Addition of two vectors.

click to expand

Example 2.11: Subtraction of two vectors.

click to expand
click to expand
Figure 2.5: Vector addition.
click to expand
Figure 2.6: Vector subtraction.

We can see how the simple addition of multiple vectors can be used to determine the effects of multiple forces on a particle, or where a vehicle traveling 90 m.p.h. will be during a 20 left turn after 3 seconds.

If we need to determine the magnitude of a vector that does not begin at the origin, we first build a new vector by subtracting the tail from the head, and treat it as a vector starting at the origin. We refer to the value of the new components as the delta , signified by the Greek letter . In Example 2.12, we see how we find the magnitude of a vector that does not begin at the origin.

Example 2.12: Finding the magnitude of the delta vector. This magnitude is the distance between the two points defined by the vectors a and b.

click to expand

While you work within MEL, many of these functions are greatly simplified. However, it is important to know the origins of these calculations if, for example, we have a bug in our code and we need to track down where that erroneous number is coming into our calculations.

To add, subtract, or multiply a simple number, called a scalar number, to a vector, is straightforward: simply multiply the scalar by each of the components of the vector, as seen in Examples 2.13, 2.14, and 2.15.

Example 2.13: Adding a scalar and a vector.

click to expand

Example 2.14: Subtracting a scalar and a vector.

click to expand

Example 2.15: Multiplying a scalar and a vector.

click to expand

Now that we know what is involved in multiplying a vector by a scalar, we can introduce the concept of a unit vector . A unit vector is simply a vector with a magnitude of 1. The process of converting a vector to unit form is called normalization . To normalize a vector, we apply the formula shown in Example 2.16.

Example 2.16: The formula for normalizing a vector.

click to expand

By dividing 1 by the magnitude of our vector, we find the number we must multiply each of that vector s components by to normalize the vector. Because unit vectors, having a magnitude of 1, evaluate quicker when involved in many calculations, they are often used to speed up and simplify vector calculations. For example, let us say that we have an object at co-ordinates 5, 14, “3 in Cartesian space. Those co-ordinates create a vector from the origin to that object. Let us further assume that we want that object to be 10 units away from the origin ”in this example a completely arbitrary choice, but it is a real-world calculation done quite often in 3D programming. In Example 2.17, we find its magnitude quite easily.

Example 2.17 Finding the magnitude of our vector.

click to expand

Example 2.18 Normalizing the vector from Example 2.17.

click to expand

With our newfound unitized vector, we simply multiply that by our proposed distance, 10, we find the point along the same vector that is 10 units long. This calculation, along with its proof, is seen in Example 2.19.

Example 2.19: Changing our vector from 2.14 to be 10 units long.

click to expand

Now that we have considered multiplying a vector by a scalar, there hopefully is some curiosity as to what happens when two vectors are multiplied together. Multiplying two vectors is both more difficult and more useful than adding and subtracting vectors. In fact, the multiplication of vectors is the foundation of rendering, polygonal shading, and 3D physics calculations. There are two ways to multiply two vectors together; one involves multiplying the magnitudes of the vectors together, leading to a scalar result, and the other is actually multiplying the vectors together. Although both are used in computer graphics, we will only discuss the calculations of the first type in depth.

To find the scalar product of two vectors, we first, of course, need the vectors themselves . In Figure 2.7, we have visualized two vectors. For convenience, we have drawn them with a common tail, although this quality is not a requirement of our calculations.

click to expand
Figure 2.7: The dot product is visualized.

While we could simply multiply the magnitudes together, this value is arbitrary and does not take into account the orientation of one vector to another. To bring this angle into the equation, we will now compute a projection of our first vector onto our second vector, to form a right triangle. If we think back to what we learned in the section Trigonometry, we realize our first vector, h , acts as the hypotenuse, and its projection on our second vector, v , is the adjacent side to the angle between them. That means that the length of that projection is h cos² Now we can define our scalar product to the formula seen in Example 2.20.

Example 2.20: The formula for finding the scalar product.

click to expand

In the formula, we use the symbol · to signify the multiplication of the two vectors, to differentiate our calculation from our vector product calculation. We call this the dot product of the two vectors. We could engage in a complicated mathematical proof of the why, but it is not the purpose of this book to fully examine the intricacies of vector calculations. Therefore, we ll cheat and just show the way the dot product works in Example 2.21.

Example 2.21: The dot product.

click to expand

Now that we have a formula for finding the same number in multiple ways, we can use simple algebra to find an unknown quantity. Here is one of those occasions where using unit vectors come in handy. In Example 2.22, we use the dot product to solve for the unknown angle between two vectors.

Example 2.22: Finding the angle between two vectors.

click to expand

Now that we know that the cosine of our unknown angle is 0.314970394, it is a simple matter to find our angle as 71.6409734729205 . Obviously, finding the angles between two points is extremely useful. The same formula can be used to find a point 25 and 15 units away from a given point, or to determine if an object is within a camera s field of view, or calculate the illumination of a Lambert shaded surface. Make note that using this method will always find the shorter of the two angles, the one less than 180 . Moreover, these calculations are made in the relative plane of the two vectors, not the rotation values for an object.

As we said, the vector product is used quite often in computer graphics. Because the vector product, called the cross product because in mathematical formula it is signified with the standard multiplication symbol —, is quite complicated to calculate longhand, we won t go into the specifics to avoid possible confusion. Those interested in the subject are encouraged to read specific material on the matter. MEL provides the capability to calculate the cross product, as well as the dot product, so we need only understand what it is. The cross product is a vector that is 90 to both the contributing vectors as seen in Figure 2.8.

click to expand
Figure 2.8: Finding the cross product.

Using the cross product of two vectors is how we determine such information as the normal of polygon, draw an offset of a line, and determine the area of a surface.

The other type of complex number used within MEL is the matrix . Math involving matrices is about as complicated as you can get. Matrices are a fundamental aspect of moving points around in space. In fact, there is a very special, very specific type of matrix computation in Maya called the transformation matrix . Almost every calculation that requires knowledge of the transformation matrix in normal 3D math is handled by MEL commands.

Addition and subtraction of matrices is simple but restrictive . Within the operation, each matrix has to have the same dimensions. The addition or subtraction is carried out as a simple scalar operation between the corresponding elements of each matrix. Example 2.23 shows matrix subtraction.

Example 2.23: Matrix subtraction.

click to expand

The complications with matrix math come when we begin to multiply two matrices together. The first rule to keep in mind is the sizing of each array. The number of columns in your first array must be equal to the number of rows in your second array. The resultant matrix has the same number of rows as the first matrix, and the same number of columns as the second matrix.

The actual calculation of the values in the resultant matrix is somewhat more complicated. The resultant matrix values are computed by multiplying and adding values together, as seen in Example 2.24.

Example 2.24: Matrix multiplication.

click to expand

Matrix calculations are an extremely complex form of mathematics, and are rarely calculated within MEL scripts. The main use of matrix multiplication is in calculating transformation information, which is largely handled internally by Maya. Again, those interested in exploring the complex world of matrix math are encouraged to explore it independently. Additionally, due to some major limitations in how MEL handles matrices, they are quite infrequently used in MEL scripts.




The MEL Companion
The MEL Companion: Maya Scripting for 3D Artists (Charles River Media Graphics)
ISBN: 1584502754
EAN: 2147483647
Year: 2003
Pages: 101

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net