Surface Normals - CS32310 H. Holstein (Aberystwyth University) December 20, 2012 Abstract The representation of solid objects in Computer Graphics requires knowledge of the outward surface normals. This concept is explained. Formulae are derived for the case of triangular planar facets, that use as input the coordinate data of the vertices and a vertex enumeration sequence. The results are generalised for arbitrary planar polygonal facets. At the end of the article, the formulae are summarised for ready reference. Introduction In three-dimensions, a normal to a surface point is a vector that is perpendicular to the surface at that point. If the surface is planar, then the same normal is valid at every point on the facet. Being perpendicular to a surface does not prescribe the magnitude of the normal vector. In particular, the sense of the normal is arbitrary. Thus, Figure 1 shows for the same plane two normals n1 and n2 directed in opposite senses. More generally, if the surface is curved (Figure 2), then the normal at a surface point is the normal to the tangent plane at that point (if such a plane exists!). In practical computer graphics, curved surfaces are often approximated by a mesh of planar facets. In that case, we need only consider normals to plane surfaces. Note that facet boundaries (edges, vertices) belong to several facets, and a unique normal does not exist there. Figure 1: A plane facet and two of its normal vectors (ref:[1]) Figure 2: A normal to a point on a curved surface, obtained via the tangent plane (ref: [1]) In computer graphics, surface normals are needed in computations that determine surface visibility and surface illumination. In the common situation of modelling non-transparent solid objects, we need to be able to compute outward facing surface normals, that is, surface normals directed away from the interior of the solid. In the sections below, we give a convention for indicating which side of a surface is outward facing, and how to compute the outward facing normal. We first consider this for a triangular facet, and then generalise the procedure for plane polygonal facet with an arbitrary number of vertices. The formulae are repeated in the final section labelled Results and Conclusions, for ease of reference. The outward normal of a triangular facet Vertices A, B, C of a solid object form a facet ABC (see Figure 3) that has an external and an internal surface. If we only have the coordinates of the vertices, then we do not have sufficient information as to which is the external surface to be illuminated and to be rendered. Extra semantic information for the solid is required to make that distinction. The commonly used convention is to enumerate a facet’s vertices in an anticlockwise order when the facet is seen from an external view. Thus, in Figure 3, we would specify ABC, P RQ, ACP, AP Q, AQB, BQR, BRC, CRP. Figure 3: A plane faceted solid for which the exterior surface of each facet is to be indicated Figure 4: Convention: vertex enumeration (e.g. ABC, PRQ) is anticlockwise from an external facet view. Equivalently, this circulation order indicates the sense of the external facet normal by means of the right-hand twist rule. Figure 4 illustrates that the circulation A → B → C → A carried out with the right hand results in a direction that is directed from the inside to the outside of facet ABC, as indicated by the normal vector n1 . Similarly, the circulation P → R → Q → P carried out with the right hand results in a direction directed externally to facet P RQ, as indicated by the normal vector n2 . To calculate the outward normal to facet ABC, we note that the three edges AB, BC, CA are in the plane of the facet, perpendicular to the facet normal. The vector cross product ~ × AC ~ n1 = AB (1) then gives a vector perpendicular to facet ABC. By the rules of forming the vector product, a sweep from the first factor to the second, i.e. from AB to AC, accompanied by a right-handed twist with forward thrust, will give the sense of the resultant vector, and this will be a direction around which the sequence of vertices A, B, C is anticlockwise. By design, this is the sense of the outward normal n1 to the facet (see Figure 4). The corresponding result for facet P RQ is that the external normal is P~R × P~Q, denoted by n2 in Figure 4. This result can also be obtained by textual substitution of ABC with P RQ, since both vertex sets are ordered anticlockwise around their external normals. Vertex data for the vertices are normally given as coordinates relative to some origin O, such as shown in Figure 5. ~ = rB − rA , The edge vectors are then expressible as differences of position vectors: AB ~ AC = rC − rA . Substituting in equation (1), we obtain n1 = (rB − rA ) × (rC − rA ) (2) = (rB × rC ) − (rB × rA ) − (rA × rC ) − (rA × rA ) | {z } reverse, with sign change | {z } reverse, with sign change = (rA × rB ) + (rB × rC ) + (rC × rA ) . | {z =0 (3) } (4) THE OUTWARD NORMAL OF A PLANE POLYGONAL FACET Figure 5: Facets ABC, P RQ: position vectors of vertices relative to the coordinate origin O. Note that also n1 = (rA − rB ) × (rB − rC ) (5) because expansion of the right hand side leads to the right hand expression of equation (4). The subscript orderings in equations (4) and (5) correspond directly to the anticlockwise vertex ordering ABC, and this makes the results easily memorisable. For facet P RQ, the results corresponding to equations (4) and (5) are n2 = (rP × rR ) + (rR × rQ ) + (rQ × rP ) = (rP − rR ) × (rR − rQ ) . (6) (7) The magnitudes of the normals calculated as above are twice the corresponding facet areas (this is a consequence of the vector product definition). If unit normals are required, then the normals must be divided by their magnitudes, n̂1 = n1 /||n1 ||, n̂2 = n2 /||n2 || . (8) The outward normal of a plane polygonal facet When a facet has more than three vertices A, B, . . . N , enumerated in anticlockwise order as viewed from an external view point, then it is tempting to use any three vertices from the full set, retaining their relative order, and proceed as for the 3-vertex case above to compute the outward normal. Unfortunately, this can lead to the wrong sense being attributed to the normal vector, in cases when the polygonal facet is not convex. An example is shown in Figure 6. Although ABCD . . . N is an anticlockwise vertex enumeration for the external facet view, the local subsequence BCD forms a triangle whose vertex enumeration is clockwise in the external view, and if used for determining the outward normal, this sequence would yield a normal in the wrong sense (inward pointing). The only safe procedure is make use of all the vertex data for the facet. First assume that it is possible to find a point Z on the interior of the facet plane such that a triangulation of the facet becomes possible with Z as the common vertex, such as shown in Figure 7. We can find the normals to each triangle, and add them, as they are all directed in the same sense. Taking the vertices A, B, . . . to be specified by position vectors rA , rB , . . ., and the position vector of Z to be rZ , we obtain n = (rA − rZ ) × (rB − rZ ) + (rB − rZ ) × (rC − rZ ) + . . . + (rN − rZ ) × (rA − rZ ) . (9) This expression simplifies considerably, because all terms involving rZ cancel. For example, the terms involving vertex rB and rZ are (−rZ ) × (rB ) + (rB ) × (−rZ ) = rZ × (−rB + rB ) = 0, and similarly for every other vertex. As a result, n = (rA × rB ) + (rB × rC ) + . . . + (rN × rA ) . (10) Figure 6: An example of a nonconvex facet. The anticlockwise sequence AB . . . N has a subsequence BCD in which BCDB is orientated clockwise. Figure 7: The point Z is chosen on the plane of facet AB . . . N such that the facet can be subdivided into non-overlapping triangles. (Such a point exists here, but may not exist in general). The magnitude of n is twice the area of the polygon, and this value is clearly independent of the partition point Z. For this reason, the apparent restriction placed on the choice of Z in Figure 7, is not a real restriction. Formula (10) is the generalisation of formula (4), for arbitrary facets. Putting rZ = rA in formula (9) cancels the first and last terms, and gives the generalisation of result (2). A generalisation of equation (5) also holds (see equation (16) below), because it can be shown to be equivalent to result (10). Results and conclusions On the assumption that coordinate data is provided for the vertices of a planar polygonal facet, and that the vertex sequence is give in anticlockwise order when the facet is viewed externally, we have derived expressions for the outward normal vector to the facet surface. For a triangular facet with anticlockwise vertex sequence ABC, the outward normal vector n is given by of the expressions n = (rB − rA ) × (rC − rA ) (11) = (rA × rB ) + (rB × rC ) + (rC × rA ) (12) = (rA − rB ) × (rB − rC ) . (13) Here rA , rB , . . . are the position vectors to vertices A, B, . . .. The generalisations for the external normal to the anticlockwise vertex sequence ABC . . . M N are n = (rB − rA ) × (rC − rA ) + . . . + (rM − rA ) × (rN − rA ) (14) = (rA × rB ) + (rB × rC ) + . . . + (rN × rA ) (15) = (rA − rB ) × (rB − rC ) + . . . + (rM − rN ) × (rN − rA ) (16) The efficiencies of these formulae are slightly different. When there are more than 5 vertices, formula (15) requires fewer arithmetic operations than formulae (14) or (16). On the other hand, coordinate differences of neighbouring vertices (such as used in equations (13) and (16)) prevent large numbers being generated, and this will lead to less cancellation rounding error in the case of floating point coordinate data. Formulae (14)-(16) are alternative formulae for the case when a facet has more than 3 vertices. It should be noted, however, that a precondition for the calculation is that the facet is planar. The formulae will yield a result even if the facet vertices do not lie in a plane, in which case the calculation for the normal may be meaningless. Checking this precondition can be carried out by verifying that each of the edge vectors is perpendicular to the calculated normal vector. Thus, we would require n • (rA − rB ) = 0, n • (rB − rC ) = 0, . . . . (17) An equivalent test is to require all the vertices to lie on the same plane, that is, the projections of their position vectors on to the normal vector are equal, n • rA = n • rB = n • rC = . . . = n • rN . (18) If these calculations are carried out in floating point arithmetic, then equality will have to be replaced by near equality up to a tolerance. Such problems are largely avoided by using only triangular facets: the three vertices of a triangular facet always define a plane. Further reading A very brief description of calculation of surface normals, with worked example, is given in sections 4.1-4.2, slides 20-23 of the lecture notes on projections (section 5 on the course web site). Most of the material in this article was presented on the whiteboard during a CS32310 lecture on 29th October. References [ 1 ] Figures 1 and 2 are taken from: Normal(geometry). Wikipedia: http://en.wikipedia.org/wiki/Normal_(geometry)