Deriving the distance of closest approach between ellipsoid and line (prev. "equation of a 3-dimensional line in spherical coordinates") Currently trying to solve a problem of calculating the smallest distance between a given ellipsoid centered on the coordinate system starting point and a given line (located... somewhere). After chasing a few promising but non-functional methods I have settled on trying to use spherical coordintates - to determine the formula for said distance by determining the formula of the distance of the ellipsoid from the center and doing the same for the line, consequently subtracting the two, and using gradient descent on the resulting function to approach a minimum (hopefully the global one). However, while that makes the ellipsoid calculations easy, I have found

Zackary Duffy

Zackary Duffy

Answered question

2022-09-04

Deriving the distance of closest approach between ellipsoid and line (prev. "equation of a 3-dimensional line in spherical coordinates")
Currently trying to solve a problem of calculating the smallest distance between a given ellipsoid centered on the coordinate system starting point and a given line (located... somewhere).
After chasing a few promising but non-functional methods I have settled on trying to use spherical coordintates - to determine the formula for said distance by determining the formula of the distance of the ellipsoid from the center and doing the same for the line, consequently subtracting the two, and using gradient descent on the resulting function to approach a minimum (hopefully the global one).
However, while that makes the ellipsoid calculations easy, I have found no consise way of determining a line in spherical coordinates in equation form. I have found the old questions on similar topics proposing use of Euler angles and the like, but that does not seem to be the solution (possibly because I haven't managed to appreciate it). So, asking here - is there any way to derive an equation for a line in 3-dimensional space in spherical coordinates?
Alternate methods for the task at hand are appreciated too - for the record, my previous lead was using a cyllindrical coordinate system with the line as its X axis, but the resulting formula for the ellipsoid turned out to be bogus.
Edit: may have figured out a solution for the bigger problem that does not rely on the spherical equation - see my own answer to the question. Title changed accordingly.
Edit 2: Scratch that. Fell into the same trap again; that solution is not going to work.

Answer & Explanation

gasskadeu7

gasskadeu7

Beginner2022-09-05Added 21 answers

Summary:
Rotate the ellipsoid and the line so that the line is parallel to the z axis. Translate the line so that the ellipsoid is centered at origin.
Projecting the ellipsoid to the xy plane yields an ellipse. This 2D ellipse is at the same distance from the line (the 2D point where the line projects to the xy plane), as the original ellipsoid is from the original line.
I shall assume the methods for finding the 2D distance between a point and an ellipse are known.
Let's say you have an ellipsoid with semi-axis vectors
p 1 = [ x 1 y 1 z 1 ] , p 2 = [ x 2 y 2 z 2 ] , p 3 = [ x 3 y 3 z 3 ]
where the three semi-axis vectors are perpendicular ( p 1 p 2 , p 1 p 3 , and p 2 p 3 ; i.e. p 1 p 2 = 0, p 1 p 3 = 0, and p 2 p 3 = 0). You also have a line (or line segment),
p ( t ) = p 0 + t p Δ
where
p 0 = [ x 0 y 0 z 0 ] , p Δ = [ x Δ y Δ z Δ ] , 0 t 1
First, construct a rotation matrix R which rotates the line parallel to the z axis. Let
R = [ r 11 r 12 r 13 r 21 r 22 r 23 r 31 r 32 r 33 ] = [ r ^ 1 r ^ 2 r ^ 3 ]
where r ^ 1 , r ^ 2 , and r ^ 3 are row vectors. The last one corresponds to the direction of the line:
r ^ 3 = [ r 31 r 32 r 33 ] = p Δ p Δ = p Δ p Δ p Δ
The two other vectors need to be perpendicular to the line, and to each other. You can for example construct three temporary vectors,
t 1 = p 1 × r ^ 3 p 1 , t 2 = p 2 × r ^ 3 p 2 , t 3 = p 3 × r ^ 3 p 3
and pick two longest ones, normalizing them to unit length. (Because vector cross product is perpendicular to both vectors, and the three semi-axis vectors are perpendicular to each other, at least two of the temporary unit vectors have positive length.)
Andreasihf

Andreasihf

Beginner2022-09-06Added 3 answers

From this point on, p 1 , p 2 , and p 3 are assumed to be the ellipsoid semi-axis vectors, rotated by R so that p Δ is parallel to the z axis z Δ > 0, with the center of the ellipsoid at origin.
Let
p = [ x y z ] , A = [ a 11 a 12 a 13 a 21 a 22 a 23 a 31 a 32 a 33 ]
where
a 11 = x 1 2 ( x 1 2 + y 1 2 + z 1 2 ) 2 + x 2 2 ( x 2 2 + y 2 2 + z 2 2 ) 2 + x 3 2 ( x 3 2 + y 3 2 + z 3 2 ) 2 a 22 = y 1 2 ( x 1 2 + y 1 2 + z 1 2 ) 2 + y 2 2 ( x 2 2 + y 2 2 + z 2 2 ) 2 + y 3 2 ( x 3 2 + y 3 2 + z 3 2 ) 2 a 33 = z 1 2 ( x 1 2 + y 1 2 + z 1 2 ) 2 + z 2 2 ( x 2 2 + y 2 2 + z 2 2 ) 2 + z 3 2 ( x 3 2 + y 3 2 + z 3 2 ) 2 a 12 = a 21 = x 1 y 1 ( x 1 2 + y 1 2 + z 1 2 ) 2 + x 2 y 2 ( x 2 2 + y 2 2 + z 2 2 ) 2 + x 3 y 3 ( x 3 2 + y 3 2 + z 3 2 ) 2 a 13 = a 31 = x 1 z 1 ( x 1 2 + y 1 2 + z 1 2 ) 2 + x 2 z 2 ( x 2 2 + y 2 2 + z 2 2 ) 2 + x 3 z 3 ( x 3 2 + y 3 2 + z 3 2 ) 2 a 23 = a 32 = y 1 z 1 ( x 1 2 + y 1 2 + z 1 2 ) 2 + y 2 z 2 ( x 2 2 + y 2 2 + z 2 2 ) 2 + y 3 z 3 ( x 3 2 + y 3 2 + z 3 2 ) 2
The ellipsoid can now be described in matrix form using
(1) p T M p 1 = 0
If we project the ellipsoid to the xy plane, the resulting curve (shadow) is also an ellipse. The distance between this ellipse and point ( x 0 , y 0 ) is the minimum distance between the original ellipsoid and the original line.
The z component of the gradient of the ellipsoid is zero at the projected ellipsoid:
(2) d d z ( p T M p 1 ) = 0
Solving (2) for z yields
(3) z = ( a 13 + a 31 ) x + ( a 23 + a 32 ) y a 33
Substituting (3) into (1) gives us the equation for the projected ellipse on the xy plane,
(4a) x 2 ( a 11 ( a 13 + a 31 ) 2 4 a 33 ) + x y ( a 12 + a 21 ( a 13 + a 31 ) ( a 23 + a 32 ) 2 a 33 ) + y 2 ( a 22 ( a 23 + a 32 ) 2 4 a 33 ) 1 = 0
That is, if we use
C 1 = a 11 ( a 13 + a 31 ) 2 4 a 33 C 2 = a 12 + a 21 ( a 13 + a 31 ) ( a 23 + a 32 ) 2 a 33 C 3 = a 22 ( a 23 + a 32 ) 2 4 a 33
the equation of the projected ellipse is
(4) C 1 x 2 + C 2 x y + C 3 y 2 1 = 0
Note that solving (4) for x yields
x = y C 2 2 C 1 ± y 2 ( C 2 2 4 C 1 C 3 ) + 4 C 1 2 C 1
or for y,
y = x C 2 2 C 3 ± x 2 ( C 2 2 4 C 1 C 3 ) + 4 C 3 2 C 3
These can be used to find the axis-aligned bounding box, i.e. the ranges for the x and y coordinates for the ellipse, by setting their derivative to zero. That yields
x b b = ± C 2 4 C 1 2 C 3 C 1 C 2 2 y b b = ± C 2 4 C 1 C 3 2 C 3 C 2 2

Do you have a similar question?

Recalculate according to your conditions!

New Questions in Linear algebra

Ask your question.
Get an expert answer.

Let our experts help you. Answer in as fast as 15 minutes.

Didn't find what you were looking for?