User:Nominal animal

Line-cylinder intersection
I'd like to create an article Line-cylinder intersection describing a simple algorithm to calculate the intersection between a line and a right circular cylinder. The algorithm is similar to Line–sphere intersection.

I have not found a complete reference for these equations, only partial ones, although the method is more or less described in various discussion forums. The equations below I solved using Maple. Early testing (in a C program) indicates the algorithms are correct, but I intend to do some further testing.

Cylinder equation, end caps excluded
Let $${\bar b} = ( b_x, b_y , b_z )$$ be the cylinder base (or one endpoint), $${\hat a} = ( a_x, a_y ,  a_z )$$ be the cylinder axis unit vector, cylinder radius $$r$$, and height (or axis length) $$h$$. The cylinder may be in any orientation.

The equation for an infinite cylinder can be written as
 * $$\lVert {\hat a} \times ({\bar p} - {\bar b}) \rVert = r$$

where $${\bar p} = (x, \ y , \  z )$$ is any point on the cylinder surface. The equation simply states that points $${\bar p}$$ are exactly at Euclidean distance $$r$$ from the axis $${\hat a}$$ starting from point $${\bar b}$$, where $$r$$ is measured in units of $$\lVert{\hat a}\rVert$$. Note that $$\lVert{\hat a}\rVert = 1$$ if $${\hat a}$$ is a unit vector. Because both sides of the equation are always positive or zero, we can square it, and eliminate the square root operation in the Euclidean norm on the left side:
 * $${\lVert {\hat a} \times ({\bar p} - {\bar b}) \rVert}^2 = r^2$$

Point $${\bar p}$$ is at signed distance
 * $$t = {\hat a} \cdot ({\bar p} - {\bar b})$$

from the base along the axis. Therefore, the two equations defining the cylinder, excluding the end caps, is
 * $${\lVert {\hat a} \times ({\bar p} - {\bar b}) \rVert}^2 = r^2$$
 * $$0 \leq {\hat a} \cdot ({\bar p} - {\bar b}) \leq h$$

The line
Let $${\bar p} = {\hat n} d$$ be a line thorough origin, $${\hat n}$$ being the unit vector, and $$d$$ the distance from origin.

If your line does not pass thorough origin but point $${\bar o}$$, i.e. your line is $${\bar o} + {\hat n} d$$, replace $${\bar b}$$ with $$({\bar b}-{\bar o})$$ everywhere; distance $$d$$ is then the distance from $${\bar o}$$.

The intersection problem
The intersection between the line and the cylinder is
 * $${\lVert {\hat a} \times ({\hat n} d - {\bar b}) \rVert}^2 = r^2$$
 * $$0 \leq t \leq h$$

where the signed distance along the axis $$t$$ is
 * $$t = {\hat a} \cdot ({\hat n} d - {\bar b})$$

Solution
Rearranging the first equation gives a quadratic equation for $$d$$. Solving that for $$d$$ gives
 * $$d = \frac{({\hat n}\times{\hat a})\cdot({\bar b}\times{\hat a}) \pm\sqrt{ ({\hat n}\times{\hat a})\cdot({\hat n}\times{\hat a}) r^2 - ({\hat a}\cdot{\hat a})({\bar b}\cdot({\hat n}\times{\hat a}))^2}}{({\hat n}\times{\hat a})\cdot({\hat n}\times{\hat a})}$$

where $$({\hat a}\cdot{\hat a}) = 1$$ if $${\hat a}$$ is a unit vector. If
 * $$\lVert {\hat n} \times {\hat a} \rVert = 0$$

the line is parallel to the axis, and there is no intersection, or the intersection is a line. If
 * $$({\hat n}\times{\hat a})\cdot({\hat n}\times{\hat a}) r^2 - ({\hat a}\cdot{\hat a})({\bar b}\cdot({\hat n}\times{\hat a}))^2 < 0$$

the line does not intersect the cylinder.

Solving $$d$$ only gives you the distance at which the line intersects the infinite cylinder. To see if the intersection occurs within the part we consider the actual cylinder, we need to check if the signed distance $$t$$ from the cylinder base $${\bar b}$$ along the axis $${\hat a}$$ to the intersection $${\bar p} = {\hat n} d$$ is within zero and the length of the cylinder:
 * $$0 \le t \le h$$

where $$t$$ is still
 * $$t = {\hat a} \cdot ({\hat n} d - {\bar b})$$

End caps
The above assumes that the cylinder does not have end caps; they must be checked for separately. The seam where the end cap meets the cylinder is assumed to belong to the cylinder, and is excluded from the end cap.

Hemispherical end caps
Hemispherical end caps are just half-spheres at both ends of the cylinder. This object is sometimes called a capsule, or possibly fixed-radius linearly-swept sphere.

Cylinder height $$h$$ does not include the end caps. If $$H$$ is the cylinder height including both hemispherical end caps, then $$h = H - 2 r$$.

Check if the line $${\bar p} = {\hat n} d$$ intersects either sphere: center $${\bar c} = {\bar b}$$ or $${\bar c} = {\bar b} + {\hat a} h$$ and radius $$r$$:
 * $$d = {\hat n}\cdot{\bar c} \pm \sqrt{({\hat n}\cdot{\bar c})^2 + r^2 - ({\bar c}\cdot{\bar c})}$$

If
 * $$({\hat n}\cdot{\bar c})^2 + r^2 - ({\bar c}\cdot{\bar c}) < 0$$

the line does not intersect the end cap sphere.

If there are solutions $$d$$, accept only those that hit the actual end cap hemisphere:
 * $$-r \le t < 0$$  or   $$h < t \le h+r$$

where, once again,
 * $$t = {\hat a} \cdot ({\hat n} d - {\bar b})$$

Planar end caps
Planar end caps are circular regions, radius $$r$$, in planes centered at $${\bar c} = {\bar b}$$ and $${\bar c} = {\bar b} + {\hat a}h$$, with unit normal vectors $$-{\hat a}$$ and $${\hat a}$$, respectively. The line $${\hat n}d$$ intersects the plane if and only if
 * $$({\hat n} d - {\bar c}) \cdot {\hat a} = 0$$

Solving d is simple,
 * $$d = \frac{ {\hat a} \cdot {\bar c} }{ {\hat a} \cdot {\hat n} }$$

Note that if
 * $${\hat a} \cdot {\hat n} = 0$$

the line is parallel to the end cap plane (and also perpendicular to the cylinder axis). Finally, if and only if
 * $$({\hat n} d - {\bar c}) \cdot ({\hat n} d - {\bar c}) < r^2$$

the intersection point $${\hat n}d$$ is within the actual end cap (the circular region in the plane).

Unit normal vector at an intersection point
One of the many applications for this algorithm is in ray tracing, where the cylinder unit normal vector $${\hat v}$$ at the intersection $${\hat n} d$$ is needed for refracted and reflected rays and lighting.

The equations below use the signed distance $$t$$ to the intersection point $${\hat n} d$$ from base $${\bar b}$$ along the axis $${\hat a}$$, which is always
 * $$t = {\hat a} \cdot ({\hat n} d - {\bar b})$$

For the cylinder surface (excluding the end caps, but including the seam), $$0 \le t \le h$$:
 * $${\hat v} = \frac{ {\hat n} d - {\hat a} t - {\bar b} }{ \lVert {\hat n} d - {\hat a} t - {\bar b} \rVert }$$

For a spherical end cap at the base, $$-r \le t < 0$$:
 * $${\hat v} = \frac{ {\hat n} d - {\bar b} }{ r }$$

for a spherical end cap at the other end, $$h < t \le h + r$$:
 * $${\hat v} = \frac{ {\hat n} d - {\bar b} - {\hat a} h }{ r }$$

For a planar end cap at the base, $$t = 0$$:
 * $${\hat v} = -{\hat a}$$

for a planar end cap at the other end, $$t = h$$:
 * $${\hat v} = {\hat a}$$

Nominal animal (talk) 23:38, 26 June 2010 (UTC)