Commit 9d5a498d authored by Jean-Yves TINEVEZ's avatar Jean-Yves TINEVEZ
Browse files

Rework the ellipse plotting routines.

parent 9e532f94
...@@ -84,6 +84,16 @@ classdef epicell ...@@ -84,6 +84,16 @@ classdef epicell
'LineWidth', 1, ... 'LineWidth', 1, ...
'Marker', '.' ); 'Marker', '.' );
end end
% Plot a 2D ellipse in 3D.
h = plot_ellipse_3d( obj, npoints, ax )
% Plot an ellipse in XY plane.
h = plot_ellipse_2d( obj, npoints, ax )
% Generate 3D points on the ellipse fit.
p = get_ellipse_points( obj, npoints )
end end
%% Private static methods: compute final properties value. %% Private static methods: compute final properties value.
...@@ -203,12 +213,6 @@ classdef epicell ...@@ -203,12 +213,6 @@ classdef epicell
% Fit a 2D ellipse to a set of 3D points. % Fit a 2D ellipse to a set of 3D points.
[ f3d, v ] = fit_ellipse_3d( p, E, method ) [ f3d, v ] = fit_ellipse_3d( p, E, method )
% Plot a 2D ellipse in 3D.
h = plot_ellipse_3d( f3d, v, npoints )
% Plot an ellipse in XY plane.
h = plot_ellipse_2d( f, npoints )
end end
end end
function p = get_ellipse_points( obj, npoints )
%GET_ELLIPSE_POINTS Generate 3D points on the ellipse fit.
f = obj.ellipse_fit;
v = epicell.euleurZXZ2rot( obj.euler_angles );
x0 = f( 1 );
y0 = f( 2 );
z0 = f( 3 );
a = f( 4 );
b = f( 5 );
theta = f( 6 );
R = [ cos( theta ) sin( theta ) ;
-sin( theta ) cos( theta ) ] ;
t = linspace( 0.5 * pi, 2.5 * pi, npoints )';
XY0 = [ a * sin(t), b * cos(t) ];
XY1 = XY0 * R;
xr = XY1( :, 1 );
yr = XY1( :, 2 );
zr = zeros( numel( xr ), 1 );
pr = [ xr yr zr ];
% Transform back
pb = pr * v';
xb = pb( :, 1 ) + x0;
yb = pb( :, 2 ) + y0;
zb = pb( :, 3 ) + z0;
xb = [ xb ; xb(1,:) ];
yb = [ yb ; yb(1,:) ];
zb = [ zb ; zb(1,:) ];
p = [ xb, yb, zb ];
end
function h = plot_ellipse_2d( f, npoints) function h = plot_ellipse_2d( obj, npoints, ax )
%PLOT_ELLIPSE_2D Plot an ellipse in XY plane. %PLOT_ELLIPSE_2D Plot the ellipse projected on the XY plane.
if nargin < 2 p = get_ellipse_points( obj, npoints );
npoints = 20;
end
x0 = f(1); xb = p( :, 1 );
y0 = f(2); yb = p( :, 2 );
a = f(3);
b = f(4);
theta = f(5);
R = [ cos( theta ) sin( theta ) ;
-sin( theta ) cos( theta ) ] ;
t = linspace( 0 , 2 * pi, npoints )';
XY0 = [ a * sin(t), b * cos(t) ];
XY1 = XY0 * R;
xr = XY1( :, 1 ) + x0;
yr = XY1( :, 2 ) + y0;
xr = [ xr ; xr(1,:) ]; xb = [ xb ; xb(1,:) ];
yr = [ yr ; yr(1,:) ]; yb = [ yb ; yb(1,:) ];
h = line( xr, yr ); h = line( ax, xb, yb );
end end
function h = plot_ellipse_3d( f3d, v, npoints) function h = plot_ellipse_3d( obj, npoints, ax )
%PLOT_ELLIPSE_3D Plot a 2D ellipse in 3D. %PLOT_ELLIPSE_3D Plot a 2D ellipse in 3D.
if nargin < 3 p = get_ellipse_points( obj, npoints );
npoints = 20;
end
x0 = f3d(1); xb = p( :, 1 );
y0 = f3d(2); yb = p( :, 2 );
z0 = f3d(3); zb = p( :, 3 );
a = f3d(4);
b = f3d(5);
theta = f3d(6);
R = [ cos( theta ) sin( theta ) ;
-sin( theta ) cos( theta ) ] ;
t = linspace( 0 , 2 * pi, npoints )';
XY0 = [ a * sin(t), b * cos(t) ];
XY1 = XY0 * R;
xr = XY1( :, 1 );
yr = XY1( :, 2 );
zr = zeros( numel( xr ), 1 );
pr = [ xr yr zr ];
% Transform back
pb = pr * v';
xb = pb( :, 1 ) + x0;
yb = pb( :, 2 ) + y0;
zb = pb( :, 3 ) + z0;
xb = [ xb ; xb(1,:) ]; xb = [ xb ; xb(1,:) ];
yb = [ yb ; yb(1,:) ]; yb = [ yb ; yb(1,:) ];
zb = [ zb ; zb(1,:) ]; zb = [ zb ; zb(1,:) ];
h = line( xb, yb, zb ); h = line( ax, xb, yb, zb );
end end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment