Disance Beween Two Ellipses in 3D David Eberly Magic Sofware 6006 Meadow Run Cour Chapel Hill, NC 27516 eberly@magic-sofware.com 1 Inroducion An ellipse in 3D is represened by a cener C, uni lengh axes U and V wih corresponding axis lenghs a and b, and a plane conaining he ellipse, N ( X C) = 0 where N is a uni lengh normal o he plane. The vecors U, V, and N form a righ handed orhonormal coordinae sysem (he marix wih hese vecors as columns is orhonormal wih deerminan 1). The ellipse is parameerized as X = C + a cos(θ) U + b sin(θ) V for angles θ [0, 2π). The ellipse is also defined by he wo polynomial equaions N ( X C) = 0 ( X C) ( U U a 2 + V V b 2 ) ( X C) = 1 where he las equaion is wrien as a quadraic form. The firs equaion defines a plane and he second equaion defines an ellipsoid. The inersecion of plane and ellipsoid is an ellipse. 2 Soluion as Polynomial Sysem The wo ellipses are N 0 ( X C 0 ) = 0 and ( X C 0 ) A 0 ( X C 0 ) = 1 where A 0 = U 0U 0 /a 2 0 + V 0V 0 /b 2 0 and N 1 ( Y C 1 ) = 0 and ( Y C 1 ) A 1 ( Y C 1 ) = 1 where A 1 = U 1U 1 /a 2 1 + V 1V 1 /b 2 1. The problem is o minimize he squared disance Y Y 2 subjec o he four consrains menioned above. The problem can be solved wih he mehod of Lagrange mulipliers. Inroduce four new parameers, α, β, γ, and δ and minimize F ( X, Y ; α, β, γ, δ) = X Y 2 + α(( X C 0 ) A 0 ( X C 0 ) 1) + β( N 0 ( X C 0 ) 0) +γ(( Y C 1 ) A 1 ( Y C 1 ) 1) + δ( N 1 ( Y C 1 ) 0). 1
Taking derivaives yields F X = 2( X Y ) + 2αA 0 ( X C 0 ) + β N 0 F Y = 2( X Y ) + 2γA 1 ( Y C 1 ) + δ N 1 F α = ( X C 0 ) A 0 ( X C 0 ) 1 F β = N 0 ( X C 0 ) F γ = ( Y C 1 ) A 1 ( Y C 1 ) 1 F δ = N 1 ( Y C 1 ) Seing he las four equaions o zero yields he four original consrains. Seing he firs equaion o he zero vecor and muliplying by ( X C 0 ) yields α = 2( X C 0 ) ( X Y ). Seing he firs equaion o he zero vecor and muliplying by N 0 yields Similar manipulaions of he second equaion yield and The firs wo derivaive equaions become β = 2 N 0( X Y ). γ = 2( Y C 1 ) ( X Y ) δ = 2 N 1( X Y ). M 0 ( X Y ( ) = N0 N 0 + A 0 ( X C 0 )( X C ) 0 ) I ( X Y ) = 0 M 1 ( X Y ( ) = N1 N 1 + A 1 ( Y C 1 )( Y C ) 1 ) I ( X Y ) = 0 Observe ha M 0 N0 = 0, M 0 A 0 ( X C 0 ) = 0, and M 0 ( N 0 ( X C 0 )) = N 0 ( X C 0 ). Therefore, M 0 = W 0 W 0 / W 0 2 where W 0 = N 0 ( X C 0 ). Similarly, M 1 = W 1 W 1 / W 1 2 where W 1 = N 1 ( Y C 1 ). The previous displayed equaions are equivalen o W 0( X Y ) = 0 and W 1( X Y ) = 0. The poins X = (x 0, x 1, x 2 ) and Y = (y 0, y 1, y 2 ) ha aain minimum disance beween he wo ellipses are 2
soluions o he six quadraic equaions in six unknowns, p 0 (x 0, x 1, x 2 ) = N 0 ( X C 0 ) = 0, p 1 (x 0, x 1, x 2 ) = ( X C 0 ) A 0 ( X C 0 ) = 1, p 2 (x 0, x 1, x 2, y 0, y 1, y 2 ) = ( X Y ) N 0 ( X C 0 ) = 0, q 0 (y 0, y 1, y 2 ) = N 1 ( Y C 1 ) = 0, q 1 (y 0, y 1, y 2 ) = ( Y C 1 ) A 1 ( Y C 1 ) = 1, q 2 (x 0, x 1, x 2, y 0, y 1, y 2 ) = ( X Y ) N 1 ( Y C 1 ) = 0. On a compuer algebra sysem ha suppors he resulan operaion for eliminaing polynomial variables, he following se of operaions leads o a polynomial in one variable. Le Resulan[P, Q, z] denoe he resulan of polynomials P and Q where he variable z is eliminaed, r 0 (x 0, x 1, y 0, y 1, y 2 ) = Resulan[p 0, p 2, x 2 ] r 1 (x 0, x 1 ) = Resulan[p 1, p 2, x 2 ] r 2 (x 0, x 1, y 0, y 1 ) = Resulan[r 0, q 2, y 2 ] s 0 (x 0, x 1, x 2, y 0, y 1 ) = Resulan[q 0, q 2, y 2 ] s 1 (y 0, y 1 ) = Resulan[q 1, q 2, y 2 ] s 2 (x 0, x 1, y 0, y 1 ) = Resulan[s 0, p 2, x 2 ] r 3 (x 0, y 0, x 1 ) = Resulan[r 2, r 1, x 1 ] r 4 (x 0, y 0 ) = Resulan[r 3, s 1, y 1 ] s 3 (x 0, x 1, y 0 ) = Resulan[s 2, s 1, y 1 ] s 4 (x 0, y 0 ) = Resulan[s 3, r 1, x 1 ] φ(x 0 ) = Resulan[r 4, s 4, y 0 ] For wo circles, he degree of φ is 8. For a circle and an ellipse, he degree of φ is 12. For wo ellipses, he degree of φ is 16. 3
3 Soluion using Trigonomeric Approach Le he wo ellipses be X = C 0 + a 0 cos(θ) U 0 + b 0 sin(θ) V 0 Y = C 1 + a 1 cos(φ) U 1 + b 1 sin(φ) V 1 for θ [0, 2π) and φ [0, 2π). The squared disance beween any wo poins on he ellipses is F (θ, φ) = X(θ) Y (φ) 2. The problem is o minimize F (θ, φ). Define c 0 = cos(θ), s 0 = sin(θ), c 1 = cos(φ), and s 1 = sin(φ). Compue derivaives, F θ = ( X(θ) Y (φ)) X (θ) and F φ = ( X(θ) Y (φ)) Y (φ). Seing hese equal o zero leads o he wo polynomial equaions in c 0, s 0, c 1, and s 1. The wo polynomial consrains for he sines and cosines are also lised. p 0 = (a 2 0 b 2 0)s 0 c 0 + a 0 (α 00 + α 01 s 1 + α 02 c 1 )s 0 + b 0 (β 00 + β 01 s 1 + β 02 c 1 )c 0 = 0 p 1 = (a 2 1 b 2 1)s 1 c 1 + a 1 (α 10 + α 11 s 0 + α 12 c 0 )s 1 + b 1 (β 10 + β 11 s 0 + β 12 c 0 )c 1 = 0 q 0 = s 2 0 + c 2 0 1 = 0 q 1 = s 2 1 + c 2 1 1 = 0 This is a sysem of four quadraic polynomial equaions in four unknowns and can be solved wih resulans: r 0 (s 0, s 1, c 1 ) = Resulan[p 0, q 0, c 0 ] r 1 (s 0, s 1, c 0 ) = Resulan[p 1, q 1, c 1 ] r 2 (s 0, s 1 ) = Resulan[r 0, q 1, c 1 ] r 3 (s 0, s 1 ) = Resulan[r 1, q 0, c 0 ] φ(s 0 ) = Resulan[r 2, r 3, s 1 ] Alernaively, we can use he simple naure of q 0 and q 1 o do some of he eliminaion. Le p 0 = α 0 s 0 + β 0 c 0 + γ 0 s 0 c 0 where α 0 and β 0 are linear polynomials in s 1 and c 1. Similarly, p 1 = α 1 s 1 + β 1 c 1 + γ 1 s 1 c 1 where α 1 and β 1 are linear polynomials in s 0 and c 0. Solving for c 0 in p 0 = 0 and c 1 in p 1 = 0, squaring, and using he q i consrains leads o r 0 = (1 s 2 0)(γ 0 s 0 + β 0 ) 2 α 2 0s 2 0 = 0 r 1 = (1 s 2 1)(γ 1 s 1 + β 1 ) 2 α 2 1s 2 1 = 0 Using he q i consrains, wrie r i = r i 0 + r i 1c 1 i, i = 0, 1, where he r ij are polynomials in s 0 and s 1. The erms r i 0 are degree 4 and he erms r i1 is degree 3. Solving for c 0 in r 1 = 0 and c 1 in r 0 = 0, squaring, and 4
using he q i consrains leads o w 0 = (1 s 2 1)r 2 01 r 2 00 = 8 j=0 w 0js j 0 = 0 w 1 = (1 s 2 0)r 2 11 r 2 10 = 4 j=0 w 1js j 0 = 0 The coefficiens w ij are polynomials in s 1. The degrees of w 00 hrough w 08 respecively are 4, 3, 4, 3, 4, 3, 2, 1, and 0. The degree of w 1j is 8 j. Toal degree for each of w i is 8. The final eliminaion can be compued using a Bezou deerminan, φ(s 1 ) = de[e ij ], where he underlying marix is 8x8 and he enry is e ij = min(8,17 i j) k=max(9 i,9 j) v k,17 i j k where v i,j = w 0i w 1j w 0j w 1i. If he i or j index is ou of range in he w erms, hen he erm is assumed o be zero. The soluions o φ = 0 are he candidae poins for s 1. For each s 1, wo c 1 values are compued using s 2 1 + c 2 1 = 1. For each s 1, he roos of he polynomial w 1 (s 0 ) are compued. For each s 0, wo c 0 values are compued using s 2 0 + c 2 0 = 1. Ou of all such candidaes, X Y 2 can be compued and he minimum value is seleced. 4 Numerical Soluion Neiher algebraic mehod above seems reasonable. Each looks very slow o compue and you have he usual numerical problems wih polynomials of large degree. I have no implemened he following, bu my guess is i is an alernaive o consider. Implemen a disance calculaor for poin o ellipse (in 3D). This is a funcion of a single parameer, say F (θ) for θ [0, 2π]. Use a numerical minimizer ha does no require derivaive calculaion (Powell s mehod for example) and minimize F on he inerval [0, 2π]. The scheme is ieraive and hopefully converges rapidly o he soluion. 5