Line-cylinder intersection

From HandWiki
Short description: Geometry calculation
Four cases for an intersection of a line and a cylinder. Intersections with the gray cylinder are highlighted in red. Global axes are back. 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.

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.

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

Let [math]\displaystyle{ {\bar b} = ( b_x , b_y , b_z ) }[/math] be the cylinder base (or one endpoint), [math]\displaystyle{ {\hat a} = ( a_x , a_y , a_z ) }[/math] be the cylinder axis unit vector, cylinder radius [math]\displaystyle{ r }[/math], and height (or axis length) [math]\displaystyle{ h }[/math]. The cylinder may be in any orientation.

The equation for an infinite cylinder can be written as

[math]\displaystyle{ \lVert {\hat a} \times ({\bar p} - {\bar b}) \rVert = r }[/math]

where [math]\displaystyle{ {\bar p} = (x , \ y , \ z ) }[/math] is any point on the cylinder surface. The equation simply states that points [math]\displaystyle{ {\bar p} }[/math] are exactly at Euclidean distance [math]\displaystyle{ r }[/math] from the axis [math]\displaystyle{ {\hat a} }[/math] starting from point [math]\displaystyle{ {\bar b} }[/math], where [math]\displaystyle{ r }[/math] is measured in units of [math]\displaystyle{ \lVert{\hat a}\rVert }[/math]. Note that [math]\displaystyle{ \lVert{\hat a}\rVert = 1 }[/math] if [math]\displaystyle{ {\hat a} }[/math] 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:

[math]\displaystyle{ {\lVert {\hat a} \times ({\bar p} - {\bar b}) \rVert}^2 = r^2 }[/math]

Point [math]\displaystyle{ {\bar p} }[/math] is at signed distance

[math]\displaystyle{ t = {\hat a} \cdot ({\bar p} - {\bar b}) }[/math]

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

[math]\displaystyle{ {\lVert {\hat a} \times ({\bar p} - {\bar b}) \rVert}^2 = r^2 }[/math]
[math]\displaystyle{ 0 \leq {\hat a} \cdot ({\bar p} - {\bar b}) \leq h }[/math]

The line

Let [math]\displaystyle{ {\bar p} = {\hat n} d }[/math] be a line through origin, [math]\displaystyle{ {\hat n} }[/math] being the unit vector, and [math]\displaystyle{ d }[/math] the distance from origin.

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

The intersection problem

The intersection between the line and the cylinder is

[math]\displaystyle{ {\lVert {\hat a} \times ({\hat n} d - {\bar b}) \rVert}^2 = r^2 }[/math]
[math]\displaystyle{ 0 \leq t \leq h }[/math]

where the signed distance along the axis [math]\displaystyle{ t }[/math] is

[math]\displaystyle{ t = {\hat a} \cdot ({\hat n} d - {\bar b}) }[/math]

Solution

Rearranging the first equation gives a quadratic equation for [math]\displaystyle{ d }[/math]. Solving that for [math]\displaystyle{ d }[/math] gives

[math]\displaystyle{ 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})} }[/math]

where [math]\displaystyle{ ({\hat a}\cdot{\hat a}) = 1 }[/math] if [math]\displaystyle{ {\hat a} }[/math] is a unit vector. If

[math]\displaystyle{ \lVert {\hat n} \times {\hat a} \rVert = 0 }[/math]

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

[math]\displaystyle{ ({\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 \lt 0 }[/math]

the line does not intersect the cylinder.

Solving [math]\displaystyle{ d }[/math] 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 [math]\displaystyle{ t }[/math] from the cylinder base [math]\displaystyle{ {\bar b} }[/math] along the axis [math]\displaystyle{ {\hat a} }[/math] to the intersection [math]\displaystyle{ {\bar p} = {\hat n} d }[/math] is within zero and the length of the cylinder:

[math]\displaystyle{ 0 \le t \le h }[/math]

where [math]\displaystyle{ t }[/math] is still

[math]\displaystyle{ t = {\hat a} \cdot ({\hat n} d - {\bar b}) }[/math]

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 [math]\displaystyle{ h }[/math] does not include the end caps. If [math]\displaystyle{ H }[/math] is the cylinder height including both hemispherical end caps, then [math]\displaystyle{ h = H - 2 r }[/math].

Check if the line [math]\displaystyle{ {\bar p} = {\hat n} d }[/math] intersects either sphere: center [math]\displaystyle{ {\bar c} = {\bar b} }[/math] or [math]\displaystyle{ {\bar c} = {\bar b} + {\hat a} h }[/math] and radius [math]\displaystyle{ r }[/math]:

[math]\displaystyle{ d = {\hat n}\cdot{\bar c} \pm \sqrt{({\hat n}\cdot{\bar c})^2 + r^2 - ({\bar c}\cdot{\bar c})} }[/math]

If

[math]\displaystyle{ ({\hat n}\cdot{\bar c})^2 + r^2 - ({\bar c}\cdot{\bar c}) \lt 0 }[/math]

the line does not intersect the end cap sphere.

If there are solutions [math]\displaystyle{ d }[/math], accept only those that hit the actual end cap hemisphere:

[math]\displaystyle{ -r \le t \lt 0 }[/math]   or   [math]\displaystyle{ h \lt t \le h+r }[/math]

where, once again,

[math]\displaystyle{ t = {\hat a} \cdot ({\hat n} d - {\bar b}) }[/math]

Planar end caps

Planar end caps are circular regions, radius [math]\displaystyle{ r }[/math], in planes centered at [math]\displaystyle{ {\bar c} = {\bar b} }[/math] and [math]\displaystyle{ {\bar c} = {\bar b} + {\hat a}h }[/math], with unit normal vectors [math]\displaystyle{ -{\hat a} }[/math] and [math]\displaystyle{ {\hat a} }[/math], respectively. The line [math]\displaystyle{ {\hat n}d }[/math] intersects the plane if and only if

[math]\displaystyle{ ({\hat n} d - {\bar c}) \cdot {\hat a} = 0 }[/math]

Solving d is simple,

[math]\displaystyle{ d = \frac{ {\hat a} \cdot {\bar c} }{ {\hat a} \cdot {\hat n} } }[/math]

Note that if

[math]\displaystyle{ {\hat a} \cdot {\hat n} = 0 }[/math]

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

[math]\displaystyle{ ({\hat n} d - {\bar c}) \cdot ({\hat n} d - {\bar c}) \lt r^2 }[/math]

the intersection point [math]\displaystyle{ {\hat n}d }[/math] 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 [math]\displaystyle{ {\hat v} }[/math] at the intersection [math]\displaystyle{ {\hat n} d }[/math] is needed for refracted and reflected rays and lighting.

The equations below use the signed distance [math]\displaystyle{ t }[/math] to the intersection point [math]\displaystyle{ {\hat n} d }[/math] from base [math]\displaystyle{ {\bar b} }[/math] along the axis [math]\displaystyle{ {\hat a} }[/math], which is always

[math]\displaystyle{ t = {\hat a} \cdot ({\hat n} d - {\bar b}) }[/math]

For the cylinder surface (excluding the end caps, but including the seam), [math]\displaystyle{ 0 \le t \le h }[/math]:

[math]\displaystyle{ {\hat v} = \frac{ {\hat n} d - {\hat a} t - {\bar b} }{ \lVert {\hat n} d - {\hat a} t - {\bar b} \rVert } }[/math]

For a spherical end cap at the base, [math]\displaystyle{ -r \le t \lt 0 }[/math]:

[math]\displaystyle{ {\hat v} = \frac{ {\hat n} d - {\bar b} }{ r } }[/math]

for a spherical end cap at the other end, [math]\displaystyle{ h \lt t \le h + r }[/math]:

[math]\displaystyle{ {\hat v} = \frac{ {\hat n} d - {\bar b} - {\hat a} h }{ r } }[/math]

For a planar end cap at the base, [math]\displaystyle{ t = 0 }[/math]:

[math]\displaystyle{ {\hat v} = -{\hat a} }[/math]

for a planar end cap at the other end, [math]\displaystyle{ t = h }[/math]:

[math]\displaystyle{ {\hat v} = {\hat a} }[/math]

See also

References

  1. Narrien, John (1846). "Chap. VI". Analytical Geometry with the Properties of Conic Sections. London: Longman, Brown, Green, and Longmans. p. 156. https://www.google.com/books/edition/Analytical_Geometry_with_the_properties/6h9kAAAAcAAJ?hl=en&gbpv=1&dq=%22Thus+a+straight+line+can+cut+a+curve+surface+of+the+second+order+in+only+two+points%22&pg=PA156&printsec=frontcover. 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. Graphics Gems IV. AP Professional, Boston. pp. 353–355. ISBN 9780123361561. https://www.google.com/books/edition/Graphics_Gems/brDfBAAAQBAJ?hl=en&gbpv=1&dq=%22Computing%20the%20intersection%20of%20a%20line%20and%20a%20cylinder%22&pg=PA354&printsec=frontcover. 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. Graphics Gems IV. AP Professional, Boston. pp. 356–365. ISBN 9780123361561. https://www.google.com/books/edition/Graphics_Gems_IV/CCqzMm_-WucC?hl=en&gbpv=1&dq=%22Intersecting%20a%20Ray%20with%20a%20Cylinder%22&pg=PA356&printsec=frontcover. 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. 

External links