Line-cylinder intersection

Line-cylinder intersection is the calculation of any points of intersection, given an analytic geometry description of a line and a cylinder in 3d space.

Four cases for an intersection of a line and a cylinder. Intersections with the gray cylinder are highlighted in red. Global axes are black. The magenta line has zero intersections with the cylinder. Cyan line has a single point of intersection. Green line has two intersections. Yellow line lies tangent to the cylinder, so has infinitely many points of intersection.

An arbitrary line and cylinder may have no intersection at all. Or there may be one or two points of intersection.[1] Or a line may lie along the surface of a cylinder, parallel to its axis, resulting in infinitely many points of intersection. The method described here distinguishes between these cases, and when intersections exist, computes their positions.

The term “cylinder” can refer to a three-dimensional solid or, as in this article, only the curved external surface of the solid. This is why a line piercing a cylinder's volume is considered to have two points of intersection: the surface point where it enters and the one where it leaves. See § end caps.

A key intuition of this sort of intersection problem is to represent each shape as an equation which is true for all points on the shape. Solving them as a system of two simultaneous equations finds the points which belong to both shapes, which is the intersection. The equations below were solved using Maple.

This method has applications in computational geometry, graphics rendering, shape modeling, physics-based modeling, and related types of computational 3d simulations. This has led to various implementations.[2][3][4] This method is closely related to Line–sphere intersection.

Cylinder equation, end caps excluded

edit

Let   be the cylinder base (or one endpoint),   be the cylinder axis unit vector, cylinder radius  , and height (or axis length)  . The cylinder may be in any orientation.

The equation for an infinite cylinder can be written as

 

where   is any point on the cylinder surface. The equation simply states that points   are exactly at Euclidean distance   from the axis   starting from point  , where   is measured in units of  . Note that   if   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:

 

Point   is at signed distance

 

from the base along the axis. Therefore, the two equations defining the cylinder, excluding the end caps, is

 
 

The line

edit

Let   be a line through origin,   being the unit vector, and   the distance from origin.

If your line does not pass through origin but point  , i.e. your line is  , replace   with   everywhere; distance   is then the distance from  .

The intersection problem

edit

The intersection between the line and the cylinder is

 
 

where the signed distance along the axis   is

 

Solution

edit

Rearranging the first equation gives a quadratic equation for  . Solving that for   gives

 

where   if   is a unit vector. If

 

the line is parallel to the axis, and there is no intersection, or the intersection is a line. If

 

the line does not intersect the cylinder.

Solving   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   from the cylinder base   along the axis   to the intersection   is within zero and the length of the cylinder:

 

where   is still

 

End caps

edit

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

edit

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   does not include the end caps. If   is the cylinder height including both hemispherical end caps, then  .

Check if the line   intersects either sphere: center   or   and radius  :

 

If

 

the line does not intersect the end cap sphere.

If there are solutions  , accept only those that hit the actual end cap hemisphere:

    or    

where, once again,

 

Planar end caps

edit

Planar end caps are circular regions, radius  , in planes centered at   and  , with unit normal vectors   and  , respectively. The line   intersects the plane if and only if

 

Solving d is simple,

 

Note that if

 

the line is parallel to the end cap plane (and also perpendicular to the cylinder axis). Finally, if and only if

 

the intersection point   is within the actual end cap (the circular region in the plane).

Unit normal vector at an intersection point

edit

One of the many applications for this algorithm is in ray tracing, where the cylinder unit normal vector   at the intersection   is needed for refracted and reflected rays and lighting.

The equations below use the signed distance   to the intersection point   from base   along the axis  , which is always

 

For the cylinder surface (excluding the end caps, but including the seam),  :

 

For a spherical end cap at the base,  :

 

for a spherical end cap at the other end,  :

 

For a planar end cap at the base,  :

 

for a planar end cap at the other end,  :

 

See also

edit

References

edit
  1. ^ Narrien, John (1846). "Chap. VI". Analytical Geometry with the Properties of Conic Sections. London: Longman, Brown, Green, and Longmans. p. 156. Retrieved December 12, 2023. ...Thus a straight line can cut a curve surface of the second order in only two points...
  2. ^ Shene, Ching-Kuang (1994). "Computing the intersection of a line and a cylinder (Chapter V.1)". In Heckbert, Paul (ed.). Graphics Gems IV. AP Professional, Boston. pp. 353–355. ISBN 9780123361561. Retrieved December 12, 2023.
  3. ^ Cychosz, Joseph M.; Waggenspack, Warren N. Jr. (1994). "Intersecting a Ray with a Cylinder (Chapter V.2)". In Heckbert, Paul (ed.). Graphics Gems IV. AP Professional, Boston. pp. 356–365. ISBN 9780123361561. Retrieved December 12, 2023.
  4. ^ Held, Martin (1997). "ERIT—A Collection of Efficient and Reliable Intersection Tests". Journal of Graphics Tools. 2 (4): 25–44. doi:10.1080/10867651.1997.10487482.
edit