Clear Filters
Clear Filters

Two methods to check if a point exists within an ellipsoid

12 views (last 30 days)
I have a need to determine if a point, measured in [x, y, z] from the origin, is within an ellipsoid also centered at the origin with semi-axes [a1, a2, a3] that is rotated around x/y/z by tilt/roll/yaw angles. Most answers I have found suggest it is easier to rotate the point backward into a non-rotated ellipsoid rather than rotating the equation of the ellipsoid. Then after the point is rotated, I can use the new x'/y'/z' in the standard equation of an ellipsoid: (x'/a1)^2+(y'/a2)^2+(z'/a3)^2=ans. If ans is less than or equal to 1, then the point is inside or on the ellipsoid. I am using a right handed system, for reference.
This is where I started and I believe I have the solution. However, I would like to also solve the problem by rotating the ellipoid, for verification and personal curiosity reasons.
What I have now is:
% point vector measured at x/y/z
p0=[-70, -100, 300]';
% ellipsoid axes a1/a2/a3
axes=[1300, 100, 700];
% rotation angles tilt, roll, yaw degrees
r_angles=[4, -1, 120];
% rotation matrices for the ellipsoid
% rotations are intrinsic and in the order roll, tilt, yaw
Rx=rotx(r_angles(1))
Rx = 3x3
1.0000 0 0 0 0.9976 -0.0698 0 0.0698 0.9976
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Ry=roty(r_angles(2))
Ry = 3x3
0.9998 0 -0.0175 0 1.0000 0 0.0175 0 0.9998
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Rz=rotz(r_angles(3))
Rz = 3x3
-0.5000 -0.8660 0 0.8660 -0.5000 0 0 0 1.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% total rotation matrix
R=Rz*Rx*Ry
R = 3x3
-0.4989 -0.8639 0.0691 0.8665 -0.4988 0.0198 0.0174 0.0698 0.9974
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% since rotation matrices inverses are the same as their transpose and
% the inverse of each matrix is the same as rotating in the opposite
% direction, i can apply the transpose rotation matrices in the reverse
% order to rotate the point backward.
% R'=Ry'*Rx'*Rz'
p1=R'*p0
p1 = 3x1
-46.5064 131.2793 292.4088
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% now I can check if the rotated point is inside the origin centered and
% aligned ellipsoid
% point in ellipsoid <=1; yes
pie=(p1(1)/axes(1))^2+(p1(2)/axes(2))^2+(p1(3)/axes(3))^2
pie = 1.8992
% an answer above 1 suggests this point is not inside the ellipsoid
If I attempt to rotate the ellipsoid instead, I can start with the general equation for an ellipsoid using matrix notation.
(p-v)'*A*(p-v)=1
v is a vector with the center of the ellipsoid. Since my ellipsoid is at the origin, we can elimiante v.
p'*A*p=1
This is where my confusion comes in. I have seen some resources say that the general equation can be rotated in two ways:
1: By applying the rotation matrices as follows to A, which is defined as a square symmetric matrix with the inverse square of each axis on its diagonals.
(p-v)' * R' * A * R * (p-v) = 1
or
p' * R' * A * R * p = 1
2: Another method defines A differently. It has A defined as a positive definite matrix with eigen spaces(vectors?) as the principal axes of the ellipsoid and the eigenvalues are the squared inverses of the semiaxes. Which means that A is defined as: A=Q * D * Q'
D is the diagonal matrix with the inverse square of each axis on the diagonal. Q is the matrix with columns that represent the axes direction of the ellipsoid. I interpret this to mean that Q = R, since the ellipsoid started along the x/y/z axes. Which means:
p' * R * D * R' * p = 1
% continuing from here I can attempt both methods to see which works
% we can use p0 since it is not being rotated
% for method 1 we need to define the diagonal matrix
D=diag(axes.^(-2))
D = 3x3
1.0e-04 * 0.0059 0 0 0 1.0000 0 0 0 0.0204
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
pie2=p0'*R'*D*R*p0
pie2 = 0.1871
% for method 2 we just switch the locations of R' and R
pie3=p0'*R*D*R'*p0
pie3 = 1.8992
Since method 2 agrees with the backward rotation method above, I am inclined to say that it is the correct method. However, I am unsure if I simply implemented the first method incorrectly. I would like some confirmation or correction of my methods. Where did I go wrong, or what am I missing to prove I did it right?

Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!