Ray tracing primitives
Next: Bezier curves Up: Some Mathematics for Advanced Previous: Some Mathematics for Advanced

Subsections

# Ray tracing primitives

## Mathematical preliminaries

### Coordinate systems

To deal easily with the various primitive objects, you need to be able to work in all three of the standard 3D coordinate systems: rectangular (x,y,z), spherical polar ( ), and cylindrical polar ( ). To convert from one to another you use the following formulae.
Spherical polar to rectangular

 x = (1) y = (2) z = (3)

Cylindrical polar to rectangular
 x = (4) y = (5) z = z (6)

Rectangular to spherical polar1

 r = (7) = (8) = (9)

Rectangular to cylindrical polar

 r = (10) = (11) z = z (12)

### Vector algebra

It is helpful to remember your vector arithmetic. A 3D vector is represented thus:

 (13)

For ease of writing such definitions in text we may say , where we understand that this ordered triple is equivalent to the vector.
The magnitude of vector P is:

 (14)

The dot product of two vectors ( and ) is:

 (15)

which could also be written as a matrix multiplication:

 (16)

The dot product is a scalar value equal to:

 (17)

where is the angle between the two vectors. Note that this means that:

 (18)

and hence:

 (19)

Also note that the dot product between two mutually perpendicular vectors is always zero.
The cross product (vector product) of these two vectors is:

 (20)

The cross product is a vector which is perpendicular to both and . This is a very handy way to make a new vector which is guaranteed perpendicular to a given vector. It also means that:
 = 0 (21) = 0 (22)

though this may be getting a bit esoteric and so let's move on to something more interesting

## Equation of a ray

A ray is defined by an origin or eye point, , and an offset vector, . The equation for the ray is:

 (23)

This is obviously equivalent to the three equations:

 (24)

When finding a ray-object intersection point, we are looking for the intersection point with the lowest non-negative value of t.

## Equations for the primitives

### Sphere

The unit sphere, centred at the origin, has the implicit equation:

 x2+y2+z2=1 (25)

In spherical polar coordinates it is even simpler:

 r=1 (26)

In vector arthmetic, it becomes:

 (27)

To find the intersection between this sphere and an arbitrary ray, substitute the ray equation (Equation 24) in the sphere equation (Equation 25):
 (xE+txD)2+(yE+tyD)2+(zE+tzD)2=1 (28) t2(xD2+yD2+zD2)+t(2xExD+2yEyD+2zEzD) +(xE2+yE2+zE2-1)=0 (29) at2+bt+c=0 (30) (31)

where a=xD2+yD2+zD2, b=2xExD+2yEyD+2zEzD, and c=xE2+yE2+zE2-1. This gives zero, one, or two real values for t. If there are zero real values then there is no intersection between the ray and the sphere. If there are either one or two real values then chose the smallest, non-negative value, as the intersection point. If there is no non-negative value, then the line (of which the ray is a part) does intersect the sphere, but the intersection point is not on the part of the line which consistutes the ray. In this case there is again no intersection point between the ray and the sphere.

An alternative formulation is to use the vector versions of the equations (Equations 23 and 27):

 (32) (33) at2+bt+c=0 (34) (35)

Where , , and . In other words, exactly the same result, expressed in a more compact way. Graphics Gems I (p. 388) decribes yet another way of arriving at the same result.

### Cylinder

The infinite unit cylinder aligned along the z-axis is defined as:

 x2+y2=1 (36)

In cylindrical polar coordinates it is just:

 r=1 (37)

To intersect a ray with this, substitute Equation 24 in Equation 36.

 (xE+txD)2+(yE+tyD)2=1 (38) t2(xD2+yD2)+t(2xExD+2yEyD) +(xE2+yE2-1)=0 (39) at2+bt+c=0 (40) (41)

where a=xD2+yD2, b=2xExD+2yEyD, and c=xE2+yE2-1.

The finite open-ended unit cylinder aligned along the z-axis is defined as:

 (42)

The only difference between this and Equation 36 being the restriction on z. In cylindrical polar coordinates this is obviously:

 (43)

To handle this finite length cylinder, solve Equation 41 above. This gives, at most, two values of t. Call these t1 and t2. Calculate z1 and z2using Equation 24 ( z1 = zE + t1zD and z2 = zE + t2zD) and then check and . Whichever intersection point passes this test and, if both pass the test, has the smallest non-negative value of t, is the closest intersection point of the ray with the open-ended finite cylinder.

If we wish the finite length cylinder to be closed we must formulate an intersection calculation between the ray and the cylinder's end caps. The end caps have the formulae:

 (44) (45)

Once you have calculated the solutions to Equation 41 you will either know that there are no intersections with the infinite cylinder or you will know that there are one or two real intersection points (t1 and t2). The previous paragraph explained how to ascertain whether these correspond to points on the finite length open-ended cylinder. Now, if z1 and z2 lie either side of we know that the ray intersects the end cap, and can calculate the intersection point as:

 (46)

A similar equation holds for the end cap. Note that the ray may intersect both end caps, for example when and .

### Cone

The infinite double cone2 aligned along the z-axis is defined as:

 x2+y2=z2 (47)

In cylindrical polar coordinates it is:

 r2=z2 (48)

To intersect a ray with this, substitute Equation 24 in Equation 47.

 (xE+txD)2+(yE+tyD)2=(zE+tzD)2 (49) t2(xD2+yD2-zD2)+t(2xExD+2yEyD-2zEzD) +(xE2+yE2-zE2)=0 (50) at2+bt+c=0 (51) (52)

where a=xD2+yD2-zD2, b=2xExD+2yEyD-2zEzD, and c=xE2+yE2-zE2.

The finite open-ended cone aligned along the z-axis is defined as:

 (53)

The only difference between this and Equation 47 being the restriction on z. Note that if and are both positive or both negative then you get a single cone with its top truncated. If either or is zero you get a single cone with its apex at the origin.

To handle this finite length cone you proceed as for the finite length cylinder, with the obvious simple modifications.

### Torus

A torus is defined by two parameters: the radius of the torus (that is the radius of the torus's defining circle, measured from the origin) and the radius of the tube (the perpendicular distance from the defining circle to the surface of the torus). These are R and rrespectively. Normally R > r.

An implicit definition of the torus is:

 (54)

The torus can also be definied parametrically in terms of two angles, and , where can be thought of as the angle around the defining circle and the angle around the inside of the tube:

 x = (55) y = (56) z = (57)

Substituting these three equations into Equation 54 will show that they are correct and is a useful exercise in algebraic manipulation.

To find the intersection points of a ray with a torus you need to substitute Equation 24 in Equation 54. Equation 58 is that substitution with the term expanded, the resulting square root term placed on one side of the equals sign, and all other terms placed on the other side:

 = R2 + xE2+2txExD+t2xD2 + yE2+2tyEyD+t2yD2 + zE2+2tzEzD+t2zD2 - r2 (58)

If we now square both sides we will get a quartic equation in t. This can be solved using a standard quartic root finder to find the four roots of the equation3 (there are up to four intersection points between a torus and an arbitrary ray). A quartic root finder is described in Graphics Gems V (p. 3).

### Plane

A plane can be defined by a normal vector, and a point on the plane, . A point, , is on the plane if:

 (59)

To find the ray/plane intersection substitute Equation 23 in Equation 59:

 (60) (61)

If t<0 then the plane is behind the eye point and there is no intersection. If then the intersection point is . If then the ray is parallel to the plane, and there is no intersection point.

### Polygon

A polygon can be defined by an ordered set of vertices: . To find the intersection point between a polygon and a ray, we first find the intersection point between the polygon's plane and the ray, and then ascertain whether this intersection point lies inside the polygon or not.

The normal of the polygon's plane can be found by the simple cross product: . A point on the plane's surface is even easier to find: . The intersection calculation then proceeds as above.

If an intersection point between the ray and the plane is found then we can check whether or not the point lies inside the polygon in the following manner. First we project the intersection point and the polygon to two dimensions by simply throwing away one coordinate. Obviously we will throw away the coordinate in which the polygon has the smallest extent. We then test to see if the intersection point lies inside the two dimensional polygon using the odd/even test.

The odd/even test checks to see whether or not an arbitrary point lies inside an arbitrary polygon in two dimensions. This is done by drawing a horizontal ray from the point to infinity. If the ray crosses an even number of polygon edges then the point lies outside the polygon. Contrariwise, if the ray crosses an odd number of polygon edges then the point lies inside the polygon. A full discussion of the implementation details of this, and other point-in-polygon algorithms, can be found in Graphics Gems IV pp. 24-46.

### Disc

The disc is similar to the polygon. Both are planar objects. A disc can be defined by its centre, , its radius, r, and a normal vector, . Finding the intersection point, , of the ray with the disc's plane proceeds as for a plane/ray or polygon/ray intersection. Discovering whether lies inside the disc requires you to simply check that:

 (62)

## Intersections with arbitrarily positioned primitives

Of the above primitives, only the plane and polygon are arbitrarily defined4. The sphere, cylinder, cone, and torus are all defined as being centred at the origin, and all have other restrictions on their definition5. In order to ray trace one of these primitives in an arbitrary location we have two alternatives: (1) find general intersection algorithms between a ray and the arbitrarily located versions of the primitives; or (2) use geometric transforms to scale, rotate, and translate these primitives into the desired locations. This second option will be followed here.

### Transforming the primitives

The basic idea here is a very simple. We specify a scaling, a rotation, and a translation which, between them, transform the primitive from its standard position to the desired location. You will remember that this was covered in the IB Computer Graphics & Image Processing course. To perform the intersection we take the inverse transform of the ray, intersect this with the primitive in its standard position, and then transform the resulting intersection point to its correct location.

We now need to take a small diversion in order to explain the difference between a vector representing a point and a vector representing a displacement. A displacement can be thought of as the difference between two points. When transforming objects, displacements are scaled and rotated but not translated. Think of it this way: if you translate two points, the displacement between them stays exactly the same. But if you scale or rotate the two points, the displacement between them scales or rotates accordingly.

All this is by way of explaining how we transform a ray in order to intersect the appropriate ray with the primitive in its standard position. Let us assume that the primitive object in its standard position, , undergoes transformation6 to get into the desired position . To intersect ray, , with we transform the point, , and the displacement as follows:

 = (63) = (64)

The point is translasted, rotated and scaled but the displacement is only rotated and scaled.

Now intersect ray, , with the object in its standard position, , as described in the previous sections. This gives the value of t and consequently allows you to directly calculate .

In addition to scaling, rotating and translating the standard primitives, this mechanism allows us to stretch and squash them by scaling them by different factors in the different dimensions. For example, an ellipoid is simply a stretched sphere. However, such anisotropic scaling causes interesting problems with the normal vectors, as discussed in the following section.

### Transforming normal vectors

In ray tracing we need to know not just the intersection point but also the normal vector to the surface at the intersection point. This normal vector is used in the illumination calculations. However, if we scale an object anisotropically, the normal vector scales as the inverse of the object scaling, although it rotates in the same way as the object.

This can be mathematically explained by remembering that the normal vector is related to the derivative of the surface. The normal vector is perpendicular to the surface, which means that it is perpendicular to any derivative vector. As an example, consider the ellipsoid created by scaling the unit sphere by the matrix:

 (65)

The intersection point between the unit sphere and the inverse transformed ray will be at some point . This equates to the spherical polar coordinate where7 . By virtue of the fact that the normal to every point on the sphere passes through the centre of the sphere, it is easy to see that the normal vector, , is also .

Transforming to the true intersection point is simply a matter of applying . Thus the intersection point of the ray with the ellipsoid is at rectangular coordinate .

To find the normal vector to the ellipsoid at this intersection point, we need to find two non-parallel derivative vectors to the surface at the intersection point, and take their cross product to give the normal vector, . Two derivative vectors are:

 = (66) = (67)

This leads to the normal vector being8:
 = (68) = (69)

Thus, we conclude that while:
 = (70) = (71)

This leaves open the question of what to do about the rotation and translation components of the transformation. It is intuitively obvious that rotating an object causes the normal vector to rotate by the same amount and, because normal vectors are displacements rather than points, they should not be translated. So the final analysis is that:
 = (72) = (73)

This analysis has been applied to the unit sphere, but the same result holds for any object.

## Converting the primitives to polygons

We may be in a situation where we define objects in terms of the various primitives, but where we wish to draw the objects using polygon scan conversion. In this case we need to convert primitives into polygons. Some polygon scan conversion algorithms can only deal with triangles; in these cases we may need to do a little bit of extra work to ensure that all of the generated polygons are triangles.

Converting the curved primitives (sphere, cylinder, cone, torus, disc) involves approximating a curved profile by a series of straight line segments. The simplest example is the disc. This can be approximated by a regular n-gon, where n is chosen to give an adequate approximation to the disc. Adequate'' in this case will depend on the desired rendering resolution, the desired speed of rendering, and the desired quality of the final image. If necessary, this n-gon can be converted to triangles in one of a number of ways: (1) define a central vertex and connect every edge to this vertex to make nisosceles triangles; (2) select one vertex on the n-gon, and make a triangle fan emanating from this vertex; or (3) start at one edge of the n-gon and make a triangle strip set which proceeds from this edge of the polygon to the opposite edge.

A cylinder or cone can be converted to polygons by polygonising the discs at either end into n-gons, and then connecting corresponding vertices on the two n-gons. In the special case of a cone with a point, one of the n-gons obviously degenerates to that point.

Spheres and tori can be most easily converted to polygons by considering their parameterisation in terms of and (for a sphere these are Equations 1-3; for a torus they are Equations 55-57). By selecting appropriate steps in the two parameters we can generate a set of quadrilaterals which approximates the curved primitive.

It should be noted that spheres can be polygonised with more uniform polygons by starting with one of the five Platonic solids, and subdividing its faces accordingly. The details of this are left as an exercise to the reader.

Next: Bezier curves Up: Some Mathematics for Advanced Previous: Some Mathematics for Advanced