Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing whether a line segment intersects a sphere

I am trying to determine whether a line segment (i.e. between two points) intersects a sphere. I am not interested in the position of the intersection, just whether or not the segment intersects the sphere surface. Does anyone have any suggestions as to what the most efficient algorithm for this would be? (I'm wondering if there are any algorithms that are simpler than the usual ray-sphere intersection algorithms, since I'm not interested in the intersection position)

like image 327
astrofrog Avatar asked Jan 14 '10 05:01

astrofrog


People also ask

What is the line of intersection of a sphere and a plane which does not pass through the center?

Sphere-plane intersection When the intersection of a sphere and a plane is not empty or a single point, it is a circle. This can be seen as follows: Let S be a sphere with center O, P a plane which intersects S.

What is the intersection of two spheres?

The intersection curve of two sphere always degenerates into the absolute conic and a circle. Therefore, the real intersection of two spheres is a circle. The plane determined by this circle is perpendicular to the line connecting the centers of the spheres and this line passes through the center of this circle.

What is the intersection of any plane and a sphere?

It is a circle. Such a circle formed by the intersection of a plane and a sphere is called the circle of a sphere.

How many points can a line and a sphere in 3d space have in common?

In analytic geometry, a line and a sphere can intersect in three ways: No intersection at all. Intersection in exactly one point. Intersection in two points.


2 Answers

If you are only interested if knowing if it intersects or not then your basic algorithm will look like this...

Consider you have the vector of your ray line, A -> B.

You know that the shortest distance between this vector and the centre of the sphere occurs at the intersection of your ray vector and a vector which is at 90 degrees to this which passes through the centre of the sphere.

You hence have two vectors, the equations of which fully completely defined. You can work out the intersection point of the vectors using linear algebra, and hence the length of the line (or more efficiently the square of the length of the line) and test if this is less than the radius (or the square of the radius) of your sphere.

like image 53
Cruachan Avatar answered Oct 29 '22 05:10

Cruachan


I don't know what the standard way of doing it is, but if you only want to know IF it intersects, here is what I would do.

General rule ... avoid doing sqrt() or other costly operations. When possible, deal with the square of the radius.

  1. Determine if the starting point is inside the radius of the sphere. If you know that this is never the case, then skip this step. If you are inside, your ray will intersect the sphere.

From here on, your starting point is outside the sphere.

  1. Now, imagine the small box that will fit sphere. If you are outside that box, check the x-direction, y-direction and z-direction of the ray to see if it will intersect the side of the box that your ray starts at. This should be a simple sign check, or comparison against zero. If you are outside the and moving away from it, you will never intersect it.

From here on, you are in the more complicated phase. Your starting point is between the imaginary box and the sphere. You can get a simplified expression using calculus and geometry.

The gist of what you want to do is determine if the shortest distance between your ray and the sphere is less than radius of the sphere.

Let your ray be represented by (x0 + it, y0 + jt, z0 + kt), and the centre of your sphere be at (xS, yS, zS). So, we want to find t such that it would give the shortest of (xS - x0 - it, yS - y0 - jt, zS - z0 - kt).

Let x = xS - x0, y = yX - y0, z = zS - z0, D = magnitude of the vector squared

D = x^2 -2*xit + (i*t)^2 + y^2 - 2*yjt + (j*t)^2 + z^2 - 2*zkt + (k*t)^2

D = (i^2 + j^2 + k^2)t^2 - (xi + yj + zk)*2*t + (x^2 + y^2 + z^2)

dD/dt = 0 = 2*t*(i^2 + j^2 + k^2) - 2*(xi + yj + z*k)

t = (xi + yj + z*k) / (i^2 + j^2 + k^2)

Plug t back into the equation for D = .... If the result is less than or equal the square of the sphere's radius, you have an intersection. If it is greater, then there is no intersection.

like image 36
Sparky Avatar answered Oct 29 '22 07:10

Sparky