Fit ellipse in 3D space

35 views (last 30 days)
RoboTomo
RoboTomo on 27 Sep 2022
Commented: RoboTomo on 28 Sep 2022
I have xy coordinates of the ellipse in 2D space and its coordinate center (0.1427, -0.4418).
I would like to fit this ellipse xy coordinates in to the 3D space to replace the one which is not correct/perfect. The coordinate center in 3D space should be (0.1427, -0.4418, -0.0564) and the plane is defined in attachment.
How can I calculate the missing z axis data to draw the ellipse in 3D space on the desired plane?
  2 Comments
RoboTomo
RoboTomo on 27 Sep 2022
No, I did not perform any rotations before. I am searching for intersection of plane and ellipsoid with this function: https://www.mathworks.com/matlabcentral/fileexchange/48613-surface-intersection
The output of intersection is ellipse with xyz data, however in some cases the output does not return the perfect xyz for the ellipse curve (see Fig. bad 3d ellipse). If somebody knows why this happens, it would also solve the problem.
I put above xy (in attachment) data to other function to obtain ellipse semi axis. This function calculates good ellipse fit even though the curve was not perfect: https://www.mathworks.com/matlabcentral/fileexchange/3215-fit_ellipse
I wanna use xy data of the fitted ellipse curve back in 3D space.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 28 Sep 2022
Edited: Matt J on 28 Sep 2022
Another possibility is to use this FEX download,
to fit the 3D data directly.
load('xyz');
xyz=xyz';
fobj=planarFit(xyz); %Fit a plane to xyz data
R=fobj.R(:,[2,3,1])';
Rxyz=R*xyz; %Rotate xyz coordinates to be parallel to xy plane
z0=mean(Rxyz(3,:),2);
fobj=ellipticalFit(Rxyz(1:2,:)); %ellipse fit to the rotated data (ignoring z-coordinate)
Rxyz=cell2mat(fobj.sample(0:2:360)); %Generate 'evenly' spaced samples around the ellipse
Rxyz(3,:)=z0; %re-insert z-coordinate
xyzFit=R'*Rxyz; %rotate back to 3D
%Visualize
scatter3(xyz(1,:),xyz(2,:),xyz(3,:),'filled'); hold on
scatter3(xyzFit(1,:),xyzFit(2,:),xyzFit(3,:)); hold off
xlabel X; ylabel Y; zlabel Z
legend('Original','Fit')
view(50,30)
  1 Comment
RoboTomo
RoboTomo on 28 Sep 2022
Of course... I had incomplete xyz data of the original ellipse in 3D and I didn't know such function for direct 3D data fitting exist. This fit looks great! Thank you very much for your answer and the code.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 27 Sep 2022
Edited: Matt J on 27 Sep 2022
I am searching for intersection of plane and ellipsoid
If that is the ultimate goal, you should just rewrite the ellipsoid equation in a rotated coordinate system where the intersection plane is the xy plane. That will result in a 2D equation for the intersection ellipse directly.
The appropriate basis vectors xaxis, yaxis, and zaxis can be obtained as,
zaxis=[A,B,C]';
xy=null(zaxis');
xaxis=xy(:,1);
yaxis=cross(zaxis,xaxis);
and the 4x4 coordinate transform matrix needed to apply and invert the transformation is,
T=eye(4);
T(1:3,1:3)=[xaxis,yaxis,zaxis]';
T=T*makehgtform('translate',-[0.1427, -0.4418, -0.0564]);
  2 Comments
Matt J
Matt J on 27 Sep 2022
An alternative approach is to use the transform T that I derived above to map the 3D points that the surface intersection code gave you to 2D. In 2D, you can perform the ellipse fit and generate as many points on the ellipse as you wish. Then you can map back to 3D with inv(T).

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by