function [first_axis, second_axis, third_axis, V, center] = EmbryoOutline(optreg,is_moving_dataset,stack_number,do_plots)

% pre_transform = false;
% do_stretch = true;

tolerance = .001;


first_stack = optreg.first_moment_stack;
last_stack = optreg.last_moment_stack;
new_frame = optreg.new_frame;
img_width = optreg.img_width;
img_height = optreg.img_height;


if is_moving_dataset
    directory = optreg.moving_dataset_directory;
    run_number = optreg.moving_run_number;
    
    if optreg.offset_first_last_moment
        
        time_offset = optreg.time_offset;
        
        ftp = max((first_stack -20),0) + min(first_stack,20)*5;
        ftp_corrected = ftp - time_offset;
        first_stack = min(ftp_corrected,100)/5 + max(ftp_corrected-100,0);
        first_stack = round(first_stack);
        first_stack
        
        ltp = max((last_stack -20),0) + min(last_stack,20)*5;
        ltp_corrected = ltp - time_offset;
        last_stack = min(ltp_corrected,100)/5 + max(ltp_corrected-100,0);
        last_stack = round(last_stack);
        last_stack
        
    end
    
    dataset = optreg.moving_dataset;
    position = optreg.moving_position;
    
else
    directory = optreg.target_dataset_directory;
    dataset = optreg.target_dataset;
    position = optreg.target_position;
    run_number = optreg.target_run_number;
    
end

cd(directory)

center_position = optreg.center_position;
z_multiplier = optreg.z_multiplier;
start_time = optreg.start_time;

load([num2str(dataset) '_' num2str(position) '_emb1_fullmatlabresult.mat']);

if is_moving_dataset
    x_subtract = optreg.moving_x_bounds(1)-1;
    y_subtract = optreg.moving_y_bounds(1)-1;
else
    x_subtract = optreg.target_x_bounds(1) - 1;
    y_subtract = optreg.target_y_bounds(1)-1;
end
points = esequence{1,stack_number}.finalpoints;
points_first_offset_x = ones(size(points,1),1)*x_subtract;
%     points_first_offset_x(2,1)
points_first_offset_y = ones(size(points,1),1)*y_subtract;
%     points_first_offset_y(2,1)
points_first_offset = [points_first_offset_x points_first_offset_y zeros(size(points,1),1)];
points = points - points_first_offset;
points_stretched = points*[1 0 0; 0 1 0; 0 0 z_multiplier];
points_stretched = points_stretched + [ones(size(points_stretched,1),1)*ROIxmin ones(size(points_stretched,1),1)*ROIymin 0*ones(size(points_stretched,1),1)];
points_stretched_corrected = round(points_stretched);
for i = 1:size(points_stretched(:,1))
    if (points_stretched(i,1)<=0)||(points_stretched(i,2)<=0)||(points_stretched(i,3)<=0)
        points_stretched_corrected(i,:) = [0 0 0];
    end
end

[~,points_stretched_corrected] = GenerateNewSkeleton(points_stretched_corrected,optreg);
% 
% if pre_transform
%     num_points = size(points_transformed,1);
%     points_transformed = [points_transformed squeeze(ones(num_points,1))];
%     points_transformed = transform_matrix*points_transformed;
%     points_transformed = points_transformed(:,1:3);
% end

points_stretched_transp = points_stretched_corrected.';

[ellipse_matrix,center] = MinVolEllipse(points_stretched_transp,tolerance);

[U Q V] = svd(ellipse_matrix);
% tformed_ellipse_matrix = (U)*[1 0 0; 0 1 0; 0 0 1.55]*Q *(V.');

if do_plots
figure
title(['stack ' num2str(stack_number)]);
scatter3(points_stretched_corrected(:,1),points_stretched_corrected(:,2), points_stretched_corrected(:,3)); axis([0 300 0 250 0 250]); hold on;
title(['stack ' num2str(stack_number)]);

hold on;
Ellipse_plot(ellipse_matrix,center)
end


if do_plots
figure; new_points = points_stretched_corrected*V; [new_ellipse_matrix,new_center] = MinVolEllipse(new_points.',tolerance);
scatter3(new_points(:,1),new_points(:,2),new_points(:,3));hold on;Ellipse_plot(new_ellipse_matrix,new_center);
end

first_axis = 1/sqrt(Q(1,1));
second_axis = 1/sqrt(Q(2,2));
third_axis = 1/sqrt(Q(3,3));

end