first_stack = 20;
last_stack= 89;
new_frame= [351 251 93];

z_multiplier = 3.077;

optreg.z_multiplier = 3.077;

optreg.img_width = 351;
img_width = optreg.img_width;

optreg.img_height = 251;
img_height = optreg.img_height;

optreg.num_slices = 93;
num_slices = optreg.num_slices;

optreg.new_num_slices = 287;
new_num_slices = optreg.new_num_slices;
optreg.new_frame = [351 251 287];
% new_frame = optreg.new_frame;

optreg.center_position = (optreg.new_frame - [1 1 1])/2;
center_position = optreg.center_position;
optreg.strain_name = '';
strain_name = '';
position = 4;
dataset = 2;


stack_index =11;



% if is_moving_dataset
%     strain_name =optreg.moving_strain_name;
% else
%     strain_name = optreg.target_strain_name;
% end


% 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([strain_name num2str(dataset) '_' num2str(position) '_emb1_fullmatlabresult.mat']);

% moments = zeros(last_stack - first_stack + 1,3); 
% principal_axes_over_time = zeros(last_stack-first_stack+1,3,3);
% centroids = zeros(last_stack - first_stack + 1,3);
    
%     stack_index
%     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_index}.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(121,:) = [0 0 0];
    
    [~,points_stretched_corrected] = GenerateNewSkeleton(points_stretched_corrected,optreg);
    
%     
%     mean(points_stretched_corrected(:,1))
%     hull = convhulln(double(points_stretched_corrected));
%     list = reshape(hull,[],1);
%     list2 = unique(list);
%     
%     points_on_hull = zeros(size(list2,1),3);
%     for i = 1:size(list2,1)
%         points_on_hull(i,:) = points_stretched_corrected(list2(i),:);
%     end
%     
    % find all points in convex hull.  Represent them by list, no need probably
% to put them in a 3d image.

% find bounding rectangle


figh = figure;
figh.Color = [1 1 1];
% 
% surfh = trisurf(hull,points_stretched_corrected(:,1),points_stretched_corrected(:,2),points_stretched_corrected(:,3));
% 
axis([1 351 1 251 1 287])
% az = 0;
% el = 270;
% view(az, el);
view([-1 -1.5 -1.5])
% 
% 
these_axes =gca;
these_axes.Box = 'off';
% these_axes.BoxStyle
% these_axes.XGrid = 'off';
% these_axes.YGrid = 'off';
% these_axes.ZGrid = 'off';
these_axes.XTick = [];
these_axes.YTick = [];
these_axes.DataAspectRatio = [1 1 1];
these_axes.Color = [1 1 1];

axis off;
% 
% surfh.FaceAlpha = 1;


% for showing convex hull straightening:

view([-1 -1.5 -1.5])

% new_c = zeros(64,3);
% for i= 1:64
% % new_c(i,:) = [.2 .8*rand 1];
% new_c(i,:) = [.2 rand 1];
% 
% % new_c(i,:) = [0 .4 1.2];
% end

% new_c = zeros(64,3);
% for i= 1:64
% new_c(i,:) = [.2 min((i/64)+.2,1) 1];
% % new_c(i,:) = [0 .4 1.2];
% % new_c(i,:) = [.2 min(sin((i/64)*(pi/2)),1) 1];y
% new_c(i,:) = [.2 min(tan(i/64)/tan(1),1) 1];
% new_c(i,:) = [.2 min(tan(i/64)/tan(1),1) 1];
% 
% 
% end
% colormap(new_c)

%  new_c = zeros(64,3);
% for i= 1:64
% new_c(i,:) = [.2 min((i/64)+.1,1) 1];
% % new_c(i,:) = [0 .4 1.2];
% end
% colormap(new_c)
% view([-1 -1.5 -1.5])

%  new_c = zeros(64,3);
% for i= 1:64
% new_c(i,:) = [min((i/64)+.1,1) min((i/64)+.1,1) min((i/64)+.1,1)];
% % new_c(i,:) = [0 .4 1.2];
% end
% colormap(new_c)
% view([-1 -1.5 -1.5])
% 
% 
% colormap(new_c)


% xdata = esequence{stack_index}.finalpoints(:,1) + [ones(size(esequence{stack_index}.finalpoints,1),1)*ROIxmin];
% ydata = esequence{stack_index}.finalpoints(:,2) + ones(size(esequence{stack_index}.finalpoints,1),1)*ROIymin;

xdata = points_stretched_corrected(:,1);
ydata = points_stretched_corrected(:,2);
zdata = points_stretched_corrected(:,3);

% xdata = points_on_hull(:,1);
% ydata = points_on_hull(:,2);
% zdata = points_on_hull(:,3);

% 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)];



hold on; scatterh = scatter3(xdata, ydata, zdata,144,[.2 .2 .2]);

scatterh.Children
scatterh.Marker
scatterh.MarkerFaceColor  = [.9 0 0];
% scatterplot.SizeData = 72
scatterh.SizeData = 150;
scatterh.LineWidth = .1;
scatterh.MarkerEdgeColor = 'flat';


ilastik_filename = ['Btc_Ds2_Pos4_Co0_St' num2str(stack_index + 19) '_Probabilities.h5'];
h5input = h5read(ilastik_filename,'/exported_data');


% background = squeeze(h5input(:,:,:,1)>.5);
% background = imwarp(background,affine3d([1 0 0 0; 0 1 0 0; 0 0 3.077 0; 0 0 0 1]),
nuclei = squeeze(h5input(2,:,:,:)>.4);
nuclei = imwarp(nuclei,affine3d([0 1 0 0; 1 0 0 0; 0 0 1 0; 0 0 0 1]),'OutputView',imref3d([img_height img_width num_slices]));
nuclei_stretched = imwarp(nuclei,affine3d([1 0 0 0; 0 1 0 0; 0 0 3.077 0; 0 0 0 1]),'OutputView',imref3d([img_height img_width new_num_slices]));

unc_points = GeneratePoints(nuclei_stretched,optreg);

num_unc_points = size(unc_points,1);
unc_points_condensed = zeros(5000,3);
for q = 1:5000
    unc_points_condensed(q,:) = unc_points(randi([1 num_unc_points],1),:);
end

hold on; scatterunc = scatter3(unc_points_condensed(:,1),unc_points_condensed(:,2),unc_points_condensed(:,3),'r');
scatterunc.MarkerFaceColor = '[0 .8 0]';
scatterunc.MarkerEdgeColor = 'none';

% 
% 
% % unc_points = unc_points(:,[2 1 3]);
% % hold on; scatterunc=  scatter3(unc_points(:,1),unc_points(:,2),unc_points(:,3));
% 
% % embryo_background = squeeze(h5input(:,:,:,3)>.5);
% 
% cell_bodies = squeeze(h5input(4,:,:,:)>.5);
% cell_bodies = imwarp(cell_bodies,affine3d([0 1 0 0; 1 0 0 0; 0 0 1 0; 0 0 0 1]),'OutputView',imref3d([img_height img_width num_slices]));
% cb_stretched = imwarp(cell_bodies,affine3d([1 0 0 0; 0 1 0 0; 0 0 3.077 0; 0 0 0 1]),'OutputView',imref3d([img_height img_width new_num_slices]));
% 
% cb_stretched = bwareaopen(cb_stretched,100);
% 
% cb_points = GeneratePoints(cb_stretched,optreg);
% 
% num_cb_points = size(cb_points,1);
% cb_points_condensed = zeros(5000,3);
% for q = 1:5000
%     cb_points_condensed(q,:) = cb_points(randi([1 num_cb_points],1),:);
% end
% 
% hold on; scattercb = scatter3(cb_points_condensed(:,1),cb_points_condensed(:,2),cb_points_condensed(:,3),'r');
% scattercb.MarkerFaceColor = '[0 .3 0]';
% scattercb.MarkerEdgeColor = 'none';
% 
% 
% 
% % hold on; scattercb=  scatter3(cb_points(:,1),cb_points(:,2),cb_points(:,3));
% 
% 
% neurites = squeeze(h5input(5,:,:,:)>.5);
% neurites = imwarp(neurites,affine3d([0 1 0 0; 1 0 0 0; 0 0 1 0; 0 0 0 1]),'OutputView',imref3d([img_height img_width num_slices]));
% neurites_stretched = imwarp(neurites,affine3d([1 0 0 0; 0 1 0 0; 0 0 3.077 0; 0 0 0 1]),'OutputView',imref3d([img_height img_width new_num_slices]));
% 
% neurites_stretched = bwareaopen(neurites_stretched,1500);
% 
% % figure; imshow3d(neurites_stretched)
% % sph5 = strel3d(5);
% % imopen(neurites_stretched,sph5);
% 
% 
% neurites_points = GeneratePoints(neurites_stretched,optreg);
% 
% num_neurites_points = size(neurites_points,1);
% neurites_points_condensed = zeros(5000,3);
% for q = 1:5000
%     neurites_points_condensed(q,:) = neurites_points(randi([1 num_neurites_points],1),:);
% end
% 
% 
% hold on; scatterneur = scatter3(neurites_points_condensed(:,1),neurites_points_condensed(:,2),neurites_points_condensed(:,3));
% axis([1 351 1 251 1 287])
% scatterneur.SizeData = 15;
% scatterneur.MarkerFaceColor = [0 .5 .3];
% scatterneur.MarkerEdgeColor = 'none';


% hold on; scatterhi1 = 