CMSC 425: Lecture 4 Geometry and Geometric Programming Geometry for Game Programming and Grahics: For the next few lectures, we will discuss some of the basic elements of geometry. There are many areas of comuter science that involve comutation with geometric entities. This includes not only comuter grahics, but also areas like comuter-aided design, robotics, comuter vision, and geograhic information systems. While software systems like Unity rovide suort to do geometry for you, there are good reasons for learning this material. First, for those of you who will go on to design the successor to Unity, it is imortant to understand the fundamentals underlying gometric rogramming. Second, as a game rogrammer, you will find that there things that Unity cannot hel with. In such cases, you will need to write scrits to do the geometry yourself. Comuter grahics deals largely with the geometry of lines and linear objects in 3-sace, because light travels in straight lines. For examle, here are some tyical geometric roblems that arise in designing rograms for comuter grahics. Transformations: You are asked to render a twirling boomerang flying through the air. How would you reresent the boomerang s rotation and translation over time in 3-dimensional sace? How would you comute its exact osition at a articular time? Geometric Intersections: Given the same boomerang, how would you determine whether it has hit another object? Orientation: You have been asked to design the AI for a non-layer agent in a flight combat simulator. You detect the resence of a enemy aircraft in a certain direction. How should you rotate your aircraft to either attack (or escae from) this threat? Change of coordinates: We know the osition of an object on a table with resect to a coordinate system associated with the table. We know the osition of the table with resect to a coordinate system associated with the room. What is the osition of the object with resect to the coordinate system associated with the room? Reflection and refraction: We would like to simulate the way that light reflects off of shiny objects and refracts through transarent objects. Such basic geometric roblems are fundamental to comuter grahics, and over the next few lectures, our goal will be to resent the tools needed to answer these sorts of uestions. There are various formal geometric systems that arise naturally in game rogramming and comuter grahics. The rincial ones are: Affine Geometry: The geometry of simle flat things : oints, lines, lanes, line segments, triangles, etc. There is no defined notion of distance, angles, or orientations, however. Euclidean Geometry: The geometric system that is most familiar to us. It enhances affine geometry by adding notions such as distances, angles, and orientations (such as clockwise and counterclockwise). Projective Geometry: In Euclidean geometry, there is no notion of infinity (in the same way that in standard arithmetic, you cannot divide by zero). But in grahics, we often need to deal with infinity. (For examle, two arallel lines in 3-dimensional sace can Lecture 4 1 Sring 2018
meet at a common vanishing oint in a ersective rendering. Think of the oint in the distance where two erfectly straight train tracks aear to meet. Comuting this vanishing oint involves oints at infinity.) Projective geometry ermits this. Affine Geometry: Affine geometry is basic to all geometric rocessing. It s basic elements are: Scalars, which we can just think of as being real numbers Points, which define locations in sace Free vectors (or simly vectors), which are used to secify direction and magnitude, but have no fixed osition. The term free means that vectors do not necessarily emanate from some osition (like the origin), but float freely about in sace. There is a secial vector called the zero vector, 0, that has no magnitude, such that v + 0 = 0 + v = v. Note that we did not define a zero oint or origin for affine sace. This is an intentional omission. No oint secial comared to any other oint. (We will eventually have to break down and define an origin in order to have a coordinate system for our oints, but this is a urely reresentational necessity, not an intrinsic feature of affine sace.) You might ask, why make a distinction between oints and vectors? 1 Although both can be reresented in the same way as a list of coordinates, they reresent very different concets. For examle, oints would be aroriate for reresenting a vertex of a mesh, the center of mass of an object, the oint of contact between two colliding objects. In contrast, a vector would be aroriate for reresenting the velocity of a moving object, the vector normal to a surface, the axis about which a rotating object is sinning. (As comuter scientists the idea of different abstract objects sharing a common reresentation should be familiar. For examle, stacks and ueues are two different abstract data tyes, but they can both be reresented as a 1-dimensional array.) Because oints and vectors are concetually different, it is not surrising that the oerations that can be alied to them are different. For examle, it makes erfect sense to multily a vector and a scalar. Geometrically, this corresonds to stretching the vector by this amount. It also makes sense to add two vectors together. This involves the usual head-to-tail rule, which you learn in linear algebra. It is not so clear, however, what it means to multily a oint by a scalar. (For examle, the to of the Washington monument is a oint. What would it mean to multily this oint by 2?) On the other hand, it does make sense to add a vector to a oint. For examle, if a vector oints straight u and is three meters long, then adding this to the to of the Washington monument would naturally give you a oint that is three meters above the to of the monument. We will use the following notational conventions. Points will usually be denoted by lower-case Roman letters such as,, and r. Vectors will usually be denoted with lower-case Roman letters, such as u, v, and w, and often to emhasize this we will add an arrow (e.g., u, v, w). Scalars will be reresented as lower case Greek letters (e.g., α, β, γ). In our rograms, scalars will be translated to Roman (e.g., a, b, c). (We will sometimes violate these conventions, 1 Unity does not distinguish between them. The data tye Vector3 is used to reresent both oints and vectors. Lecture 4 2 Sring 2018
however. For examle, we may use c to denote the center oint of a circle or r to denote the scalar radius of a circle.) Affine Oerations: The table below lists the valid combinations of scalars, oints, and vectors. The formal definitions are retty much what you would exect. Vector oerations are alied in the same way that you learned in linear algebra. For examle, vectors are added in the usual tail-to-head manner (see Fig. 1). The difference of two oints results in a free vector directed from to. Point-vector addition r + v is defined to be the translation of r by dislacement v. Note that some oerations (e.g. scalar-oint multilication, and addition of oints) are exlicitly not defined. vector scalar vector, vector vector/scalar scalar-vector multilication vector vector + vector, vector vector vector vector-vector addition vector oint oint oint-oint difference oint oint + vector, oint oint vector oint-vector addition u u + v v r v r + v Vector addition Point subtraction Point-vector addition Fig. 1: Affine oerations. Affine Combinations: Although the algebra of affine geometry has been careful to disallow oint addition and scalar multilication of oints, there is a articular combination of two oints that we will consider legal. The oeration is called an affine combination. Let s say that we have two oints and and want to comute their midoint r, or more generally a oint r that subdivides the line segment into the roortions α and 1 α, for some α [0, 1]. (The case α = 1/2 is the case of the midoint). This could be done by taking the vector, scaling it by α, and then adding the result to. That is, r = + α( ), (see Fig. 2(a)). Another way to think of this oint r is as a weighted average of the endoints and. Thinking of r in these terms, we might be temted to rewrite the above formula in the following (technically illegal) manner: r = (1 α) + α, (see Fig. 2(b)). Observe that as α ranges from 0 to 1, the oint r ranges along the line segment from to. In fact, we may allow to become negative in which case r lies to the left of, and if α > 1, then r lies to the right of (see Fig. 2(c)). The secial case when 0 α 1, this is called a convex combination. In general, we define the following two oerations for oints in affine sace. Lecture 4 3 Sring 2018
r = + 2 3 ( ) 1 3 + 2 3 α < 0 1 2 + 1 4 r + 1 4 0 < α < 1 (1 α) + α α > 1 (a) (b) (c) (d) Fig. 2: Affine combinations. r 0 + 1 2 r + 1 2 Affine combination: Given a seuence of oints 1, 2,..., n, an affine combination is any sum of the form α 1 1 + α 2 2 +... + α n n, where α 1, α 2,..., α n are scalars satisfying i α i = 1. Convex combination: Is an affine combination, where in addition we have α i 0 for 1 i n. Affine and convex combinations have a number of nice uses in grahics. For examle, any three noncollinear oints determine a lane. There is a 1 1 corresondence between the oints on this lane and the affine combinations of these three oints. Similarly, there is a 1 1 corresondence between the oints in the triangle determined by the these oints and the convex combinations of the oints (see Fig. 2(d)). In articular, the oint 1 3 + 1 3 + 1 3 r is the centroid of the triangle. We will sometimes be sloy, and write exressions like 1 2 ( + ), which really means 1 2 + 1 2. We will allow this sort of abuse of notation rovided that it is clear that there is a legal affine combination that underlies this oeration. To see whether you understand the notation, consider the following uestions. Given three oints in the 3-sace, what is the union of all their affine combinations? (Ans: the lane containing the 3 oints.) What is the union of all their convex combinations? (Ans: The triangle defined by the three oints and its interior.) Euclidean Geometry: In affine geometry we have rovided no way to talk about angles or distances. Euclidean geometry is an extension of affine geometry which includes one additional oeration, called the inner roduct. The inner roduct is an oerator that mas two vectors to a scalar. The roduct of u and v is denoted commonly denoted ( u, v). There are many ways of defining the inner roduct, but any legal definition should satisfy the following reuirements Positiveness: ( u, u) 0 and ( u, u) = 0 if and only if u = 0. Symmetry: ( u, v) = ( v, u). Bilinearity: ( u, v + w) = ( u, v) + ( u, w), and ( u, α v) = α( u, v). (Notice that the symmetric forms follow by symmetry.) See a book on linear algebra for more information. We will focus on a the most familiar inner roduct, called the dot roduct. To define this, we will need to get our hands dirty with Lecture 4 4 Sring 2018
coordinates. Suose that the d-dimensional vector u is reresented by the coordinate vector (u 0, u 1,..., u d 1 ). Then define d 1 u v = u i v i, Note that inner (and hence dot) roduct is defined only for vectors, not for oints. i=0 Using the dot roduct we may define a number of concets, which are not defined in regular affine geometry (see Fig. 3). Note that these concets generalize to all dimensions. Length: of a vector v is defined to be v v, and is denoted by v (or as v ). Normalization: Given any nonzero vector v, define the normalization to be a vector of unit length that oints in the same direction as v, that is, v/ v. We will denote this by v. Distance between oints: dist(, ) =. Angle: between two nonzero vectors u and v (ranging from 0 to π) is ( ) u v ang( u, v) = cos 1 = cos 1 (û v). u v This is easy to derive from the law of cosines. Note that this does not rovide us with a signed angle. We cannot tell whether u is clockwise our counterclockwise relative to v. We will discuss signed angles when we consider the cross-roduct. Orthogonality: u and v are orthogonal (or erendicular) if u v = 0. Orthogonal rojection: Given a vector u and a nonzero vector v, it is often convenient to decomose u into the sum of two vectors u = u 1 + u 2, such that u 1 is arallel to v and u 2 is orthogonal to v. u 1 ( u v) ( v v) v, u 2 u u 1. (As an exercise, verify that u 2 is orthogonal to v.) Note that we can ignore the denominator if we know that v is already normalized to unit length. The vector u 1 is called the orthogonal rojection of u onto v. If we think of v as being a normal vector to a lane, then the rojection of u onto this lane is called orthogonal comlement of u with resect to v, and is given by u 2 u u 1. u θ v Angle between vectors u u 2 v u 1 Orthogonal rojection and its comlement Fig. 3: The dot roduct and its uses. Lecture 4 5 Sring 2018
Doing it with Unity: Unity does not distinguish between oints and vectors. Both are reresented using Vector3. Unity suorts many of the vector oerations by overloading oerators. Given vectors u, v, and w, all of tye Vector3, the following oerators are suorted: u = v + w; // vector addition u = v - w; // vector subtraction if (u == v u!= w) {... } // vector comarison u = v * 2.0 f; // scalar multilication v = w / 2.0 f; // scalar division You can access the comonents of a Vector3 using as either using axis names, such as, u.x, u.y, and u.z, or through indexing, such as u[0], u[1], and u[2]. The Vector3 class also has the following members and static functions. float x = v. magnitude ; // length of v Vector3 u = v. normalize ; // unit vector in v s direction float a = Vector3. Angle ( u, v); // angle ( degrees ) between u and v float b = Vector3. Dot ( u, v); // dot roduct between u and v Vector3 u1 = Vector3. Project ( u, v); // orthog roj of u onto v Vector3 u2 = Vector3. ProjectOnPlane ( u, v); // orthogonal comlement Some of the Vector3 functions aly when the objects are interreted as oints. Let and be oints declared to be of tye Vector3. The function Vector3.Ler is short for linear interolation. It is essentially a two-oint secial case of a convex combination. (The combination arameter is assumed to lie between 0 and 1.) float b = Vector3. Distance (, ); // distance between and Vector3 midoint = Vector3. Ler (,, 0.5 f); // convex combination Lecture 4 6 Sring 2018