Essential Mathematics for Computational Design, second edition by Robert McNeel & Associates is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.
ii
Essential Mathematics for Computational Design
Preface Essential Mathematics for Computational Design introduces design professionals to foundation mathematical concepts that are necessary for effective development of computational methods for 3D modeling and computer graphics. This is not meant to be a complete and comprehensive resource, but rather an overview of the basic and most commonly used concepts. The material is directed towards designers who have little or no background in mathematics beyond high school. All concepts are explained visually using Grasshopper® (GH), the generative modeling environment for Rhinoceros® (Rhino). For more information, go to www.rhino3d.com and www.grasshopper3d.com. The content is divided into three parts. The first discusses vector math including vector representation, vector operation, and line and plane equations. The second part reviews matrix operations and transformations. The third part includes a general review of parametric curves with special focus on NURBS curves and the concepts of continuity and curvature. It also quickly reviews NURBS surfaces and polysurfaces. I would like to acknowledge the excellent and thorough technical review of Dr. Greg Arden of Robert McNeel and Associates. His valuable comments were instrumental to produce this second edition. I would also like to acknowledge Ms. Margaret Becker of Robert McNeel and Associates for reviewing the technical writing and formatting the document. Finally, I’d like to point out that the material in this book is based partly on a workshop I held at the University of Texas at Arlington for the Tex-Fab event February, 2010. Rajaa Issa Robert McNeel & Associates
iii
Essential Mathematics for Computational Design
Table of Contents 1 Vector Mathematics ......................................................................................................... 1 Vector representation......................................................................................................... 1 Vector operations............................................................................................................... 3 Vector equation of line ..................................................................................................... 13 Vector equation of a plane ............................................................................................... 14 2 Matrices and Transformations ...................................................................................... 16 Introduction...................................................................................................................... 16 Matrix multiplication ......................................................................................................... 16 Affine transformations ...................................................................................................... 17 3 Parametric Curves and Surfaces .................................................................................. 22 Introduction...................................................................................................................... 22 Cubic polynomial curves .................................................................................................. 22 Geometric continuity ........................................................................................................ 25 Curvature......................................................................................................................... 26 Algorithms for evaluating parametric curves ..................................................................... 28 NURBS curves................................................................................................................. 31 Characteristics of NURBS curves..................................................................................... 33 NURBS surfaces.............................................................................................................. 36 Characteristics of NURBS surfaces.................................................................................. 37 Polysurfaces .................................................................................................................... 39 References .......................................................................................................................... 42
iv
Essential Mathematics for Computational Design
1 Vector Mathematics Vector representation Vectors indicate a quantity that has "direction" and "magnitude" such as velocity or force. Vectors in 2D coordinate systems are represented with two real numbers in the form: v = Similarly, in 3-D coordinate system, vectors are represented by three real numbers and would look like: v = We will use a lower case bold letters to represent vectors. Also vector components are enclosed by angle brackets. Points will use upper case letters. Points’ coordinates will always be enclosed by round brackets. Using a coordinate system and any set of anchor points in that system, we can represent or visualize these vectors using a line-segment representation. We usually put an arrowhead to show the direction of vectors. For example, if we have a vector that has a direction parallel to the x-axis of a given 3-D coordinate system and a magnitude equal to 5.18 units, then we can write the vector as follows: v = <5.18, 0, 0> To represent that vector, we need an anchor point in the coordinate system. For example, all of the red line segments in the following figure are equal representations of the same vector.
Figure (1): Vector representation in the 3D coordinate system. 1: Grasshopper unit x-axis component. 2: Grasshopper number slider component. 3: Grasshopper point components that is set to reference multiple points in Rhino (in this case referencing v1, v2, v3 and v4). 4: Grasshopper vector display component
Given a 3-D vector v = < a1, a2, a3 >, all vector components a1, a2, a3 are real numbers. Also ALL line segments from a point A(x,y,z) to point B(x+a1, y+a2, z+a3) are EQUIVALENT representation of vector v. So, how do we define the end points of a line segment that represent a given vector? Let us define an anchor point (P0) using Grasshopper “x,y,z point” component: P0 = (1,2,3)
1
Essential Mathematics for Computational Design
And a vector using Grasshopper xyz vector component that takes as an input three real numbers: v = <2,2,2> The tip point (P1) of the vector is calculated by adding the corresponding components from anchor point and vector v: P1 = (1+2, 2+2, 3+2) = (3,4,5) The following definition displays this vector using the Grasshopper vector display component, and marks the end of the displayed vector that expectantly coincides with point P1:
Figure (2): Relationship between a vector, vector anchor point and the point coinciding with vector tip location
Position vector There is one special vector representation that uses the origin P0 (0,0,0) as the vector anchor point. The position vector v = is represented with a line segment between two points P0 and P1 so that: P0 = (0,0,0) P1 = (a1,a2,a3) A position vector for a given vector v= < a1, a2, a3 > is a special line segment representation from the origin_point(0,0,0) to point (a1, a2, a3). It is very important not to confuse vectors with points that have equivalent components. They are two very different concepts. In the following Grasshopper definition, point P1 coordinates are equal to vector components.
2
Essential Mathematics for Computational Design
Figure (3): Position vector
Vector operations Vector addition We add vectors by adding corresponding components. That is, if we have two vectors, a and b, the sum a+b is a vector that is calculated as follows: a = b = a+b = For example, if we have a<1, 2, 0> and b<4, 1, 4> the sum a+b=<5, 3, 4> is shown in the following:
The following Grasshopper definition shows how to create the a+b vector by adding corresponding components of the two input vectors a and b.
3
Essential Mathematics for Computational Design
Figure (4): Adding vectors through adding their corresponding components
The resulting vector is the same as that resulting from using Grasshopper's built-in addition component:
Figure (5): Adding vectors using GH vector addition component
Add two vectors by adding their corresponding components. Vector addition is also useful for finding the average direction of multiple vectors. In this case, we usually use same-length vectors. Here is an example that shows the difference between using same-length vectors and different-length vectors on the resulting vector addition:
Figure (6): Adding vectors to find average direction
Input vectors are not likely to be same length. In order to find average direction, you need to use the “unit vector” of input vectors. As we will see later, a unit vector is a vector of that has a magnitude equal to one. Here is an example that solves adding vectors of different lengths to find average directions in Grasshopper.
4
Essential Mathematics for Computational Design
Figure (7): Use unit vectors to find average direction of two or more vectors
Vector length We will use |a| to notate the length of a given vector “a”. The magnitude or length of a vector a = is calculated by |a| = sqrt(a12 + a22 +a32) Here is an example of calculating vector magnitude using Grasshopper function component:
Figure (8): Calculate vector length
Note that Grasshopper vector component has an output “L” that represents the vector magnitude. Using the same vector in the above example, you’ll notice that length is equal.
Figure (9): Vector length as a parameter is GH vector component
Vector scalar operation Given vector a = , and factor t = some real number, a*t = Here is the equation implemented in Grasshopper:
Figure (10): Vector scalar operation
5
Essential Mathematics for Computational Design
Unit vector A unit vector is a vector with a magnitude equal to one unit. Unit vectors are commonly used to compare directions of vectors. A vector is called a unit vector when its length or magnitude is equal to one unit.
Vector properties There are eight properties of vectors. If a, b and c are vectors and s and t are scalar, then: 1. a + b = b + a 2. a + 0 = a 3. s(a+b) = sa + sb 4. st(a) = s(ta) 5. a+(b + c) = (a+b) + c 6. a + (-a) = 0 7. (s + t)a = sa + ta 8. 1 * a = a
Vector dot product The dot product of two vectors is defined as follows: Given: vector a = vector b = a.b = a1*b1 + a2*b2 + a3*b3 In the following illustrations, we will show that Grasshopper vector dot product component yields the same result as the above a.b equation:
Figure (11): The dot product of two vectors as a sum of multiplying corresponding components
Grasshopper has a built-in vector dot product component as shown in the following illustration:
6
Essential Mathematics for Computational Design
Figure (12): The dot product of two vectors using GH vector dot product component
When calculating the dot product of two unit vectors, the result is always between -1 and +1. The dot product of a vector with itself is that vector’s length to the power of two: a.a = |a|2 Proof: If vector a = then from the definition of dot product of two vectors: a.a = a1*a1 + a2*a2 +a3*a3 or a.a = a12 + a22 +a32 Since we know that: |a| = sqrt(a12 + a22 +a32) Therefore, a.a = |a|2 Here is a Grasshopper sample to demonstrate this property comparing result using dot product component with multiplying the vector length by itself:
Figure (13): The dot product of a vector with itself
Dot product and angle between vectors One important theorem for vector dot product is: a.b = |a||b|cos(ө), or cos(ө) = a.b / (|a||b|), where “ө” is the angle included between the position vectors. And if vectors a and b are unit vectors, we can simply say: cos(ө) = a.b
7
Essential Mathematics for Computational Design
The dot product of two unit vectors equals the cosine of the angle between them Proof: From the law of cosines on triangle ABC |AB|2 = |CA|2 + |CB|2 - 2|CA||CB|cos(ө) or: |a-b|2 = |a|2 + |b|2 - 2|a||b|cos(ө) --- (1) |AB|2 is the same as |a-b|2, so we can say: |a-b|2 = {a-b) . (a-b) = a.a - a.b -b.a + b.b = |a|2 - 2a.b +|b|2 --- (2) from (1) & (2) |a|2 - 2a.b +|b|2 = |a|2 + |b|2 2|a||b|cos(ө) then: 2a.b = 2|a||b|cos(ө) or: cos(ө) = a.b / (|a||b|) Vectors a and b are orthogonal if, and only if, a.b = 0. But what is the dot product of two unit vectors if they are parallel? In the most practical way, you can think of the dot product of two vectors to be the projection length of one vector on the other. Here is a demonstration of this concept using Grasshopper. In the first figure, we calculate the dot product of the x-axis unit vector with an input vector “v”. In the second figure, we project the end point of the position vector “v” onto a line along the x-axis and calculate the distance from origin to that projection point. You’ll notice that the dot product and projection length are equal.
8
Essential Mathematics for Computational Design
Figure (14): The dot product and angle between vectors
Dot product properties If a, b and c are vectors and s is scalar then: 1. a . a = | a |2 2. a . (b + c) = a . b + a . c 3. 0 . a = 0 4. a . b = b . a 5. (sa) . b = s(a . b) = a . (sb)
Vector cross product The cross product of two 3d-vectors produces a third 3d-vector that is orthogonal to both input vectors. Given: a = b = The cross product a X b is defined using determinants. Here is a quick illustration of how to calculate a determinant. We use the standard basis vectors i=<1,0,0>, j=<0,1,0> and k=<0,0,1>
a x b = i(a2*b3) + j(a3*b1) + k(a1*b2) - k(a2*b1) - i(a3*b2) - j(a1*b3)
9
Essential Mathematics for Computational Design
a x b = This is the Grasshopper definition for evaluating the cross product using these expressions and comparing it with the vector cross product built-in component. They both yield same result.
Figure (15): Calculating the cross product of two vectors
Figure (16): Calculating the cross product of two vectors using GH cross product component
The vector a x b is orthogonal to both a and b Theorem For any pair of 3d vectors a and b |a x b| = |a||b|sin(ө), Where “ө” is the angle included between the position vectors of a and b. Or if a and b are unit vectors, then the length of their cross product equals the sine of the angle between them. In other words: |a x b| = sin(ө)
10
Essential Mathematics for Computational Design
This is an example to calculate the length of the cross product of two vectors using GH built-in cross product component and compare it to the calculation using the equation mentioned above. As expected, they both yield same result.
Figure (17): Calculating the length of cross product vector using a function and also using GH built-in component for vectors cross product
In determining the cross product, the order of operands is important. For example: a = <1, 0, 0> b = <0, 1, 0> a x b = <0, 0, 1> b x a = <0, 0, -1> In Rhino's right-handed system, the direction of a X b is given by the right-hand rule (where a = index finger, b = middle finger, and result = thumb).
11
Essential Mathematics for Computational Design
Vectors a and b are parallel if, and only if, a x b = 0
Cross product properties If a, b, and c are vectors and s is scalar then: 1. a X b = -b X a 2. (sa) X b = s(a X b) = a X (sb) 3. a X (b + c) = a X b + a X c 4. (a + b) X c = a X c + b X c 5. a . (b X c) = (a X b) . c 6. a X (b X c) = (a . c)b – (a . b)c Example All the concepts we reviewed so far have direct application to solving geometry problems encountered when modeling. For example, given a point and a surface, how can we determine whether the point is facing the front or back of that surface? Here are the steps to solve the problem:
Here is the Grasshopper solution following the same steps. Note that in this case the dot product is greater than 0 which means the point is facing the front side of the surface. If the dot product were less than 0 then the point would be on the back.
Figure (18): Find point location relative to surface front and back faces
12
Essential Mathematics for Computational Design
Vector equation of line The vector line equation is used in 3-D modeling applications and computer graphics. Here is a description of that equation and how it might be used.
In the figure: L = line v = line direction vector P0 = line position point r = r0 + a --- (1) a = t * v --- (2) Therefore from 1 and 2: r = r0 + t*v --- (3) However, we can write (3) as follows: = + = Therefore: x = x0 + ta y = y0 + tb z = z0 + tc Which is the same as: P = P0 + tv This is a Grasshopper definition to get any point on a line:
Figure (19): Find points on a line
13
Essential Mathematics for Computational Design
Example In the following figure, given points P0 and P1, find mid point P.
Notice that: a is the position vector for point P0 b is the position vector for point P1 v is the vector going from P0 to P1 From vector addition property: a + v = b, or v=b-a However, the line equation is: P = P0 + t*v, and since t=0.5 and v=b-a (from the above), then we can say: P = P0 + 0.5(b-a) Use the above equation to create a Grasshopper definition:
Figure (20): Find mid point between two input points
In general, you can find any point between P0 and P1 by changing the t value between 0 and 1.