## Notes on ray tracing to volumes bounded by first and second degree surfaces

A bounded volume in three dimensions is given by an inequality of the form P(x,y,z) >= 0. Points for which P(x,y,z) == 0 are on the bounding surface; points for which P(x,y,z) > 0 are inside the volume; points for which P(x,y,z) < 0 are outside the volume.

At any point on the bounding surface, we can compute the derivative vector [a,b,c] of function P(x,y,z). The surface normal at point [x,y,z] can then be defined as the unit length vector:

normalize([-a,-b,-c]).

### Ray tracing to first degree surfaces

Half spaces:

A volume that is bound by a first degree surface (ie: by a plane) is a half space. A half space is defined by the equation of its bounding plane [a,b,c,d] as follows:

ax + by + cz + d >= 0

Example: The half space of all points such that z >= -1 can be written as:

z + 1 >= 0     or     0*x + 0*y + 1*z + 1 >= 0
The bounding plane of this half space is therefore given by the vector of coefficients: [0,0,1,1].

Half space surface normals:

At every point on the boundary of ax+by+cz+d >= 0, the derivative vector is [a,b,c]. So the surface normal at any point on the surface is the unit length vector:

normalize([-a,-b,-c])

Convex polyhedra:

You can always describe a convex polyhedron as an intersection of half spaces. For example, the cube-shaped volume such that -1 <= x <= 1, -1 <= y <= 1, and -1 <= z <= 1, is the intersection of the following six half spaces:

[1,0,0,1], [-1,0,0,1],
[0,1,0,1], [0,-1,0,1],
[0,0,1,1], [0,0,-1,1].

Ray tracing to a half space:

Tracing a ray to a half space consists of substituting [vx + t wx, vy + t wy, vz + t wz] for [x,y,z], to get:

a (vx + t wx) + b (vy + t wy) + c (vz + t wz) + d >= 0
or
(a wx + b wy + c wz) t + (a vx + b vy + c vz + d) >= 0
This is an equation of the form At + B >= 0, whose solution is t = -B / A. When A = 0, then the ray is parallel to the defining plane of the half space, and it suffices to check whether the ray origin is inside the half space:
a vx + b vy + c vz + d >= 0

### Ray tracing to second degree surfaces

Second degree surfaces:

A volume that is bounded by a second degree surface in three dimensions is described by:

ax2 + by2 + cz2 + dyz + ezx + fxy + gx + hy + iz + j >= 0
which can be expressed as the vector of coefficients: [a,b,c,d,e,f,g,h,i,j].

Example: The equation of a sphere

(x - cx)2 + (y - cy)2 + (z - cz)2 - r2 <= 0
can be expressed as:
- (x2 - 2 x cx + cx2) - (y2 - 2 y cy + cy2) - (z2 - 2 z cz + cz2) + r2 >= 0
which can be expressed as the vector of coefficients: [-1 , -1 , -1 , 0 , 0 , 0 , 2cx , 2cy , 2cz , r2-cx2-cy2-cz2].

Derivatives and normals:

The vector-valued derivative of

ax2 + by2 + cz2 + dyz + ezx + fxy + gx + hy + iz + j
is given by:
[2ax+ez+fy+g, 2by+dz+fx+h, 2cy+dy+ex+i]

Recall that for any volume bounded by a surface which is described by some inequality P(x,y,z) >= 0, the surface normal at point [x,y,z] is given by the unit length vector:

normalize([-a,-b,-c])
where [a,b,c] is the derivative of P at [x,y,z].

Therefore, the surface normal at a point [x,y,z] on the surface is given by the unit length vector:

normalize([-2ax-ez-fy-g, -2by-dz-fx-h, -2cy-dy-ex-i])

### Ray tracing to boolean combinations of bounded volumes

Various interesting shapes can be defined as boolean combinations of volumes bounded by a mixture of first and second degree surfaces. For example, we can define a cylinder as the intersection of the volumes bounded by the three inequalities:
x2 + y2 - r2 <= 0
z >= -1
z <= 1

These three volumes can be defined, respectively, by the vectors of coefficients:

[-1 , -1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , r2 ]
[ 0 , 0 , 1 , 1 ]
[ 0 , 0 , -1 , 1 ]

A ray traced to this shape will encounter three line segments. The line segment through the cylindrical tube may be null (ie: when the ray misses the cylindrical tube entirely). The line segments through each of the two half spaces will in general be semi-infinite: either of the form [ t ... ] or [ - ... t ]. Since the ray consists only of positive values of t, the latter case is handled as [ 0 ... t].

The intersection of a ray with the cylinder is the intersection the three respective line segments. If this intersection is non-null, then one of the three surfaces (either the cylindrical tube or one of the the planar faces) will correspond to the first t in the intersection. We must compute the surface normal for that surface at the intersection point.