Nv = handles.Nv;
Nc = handles.Nc;
AP_ver_id = handles.AP_ver_id;
BS_ver_id = handles.BS_ver_id;
V_cell = handles.V_cell;
% Current time
Cell_id = handles.Cell_id;
Slice_id = handles.slice;

X_min_array = zeros(length(handles.V_cell), 1);
Y_min_array = zeros(length(handles.V_cell), 1);
Z_min_array = zeros(length(handles.V_cell), 1);

X_max_array = zeros(length(handles.V_cell), 1);
Y_max_array = zeros(length(handles.V_cell), 1);
Z_max_array = zeros(length(handles.V_cell), 1);

% Determine the window size
t_measure = 0; tic;

for i_tmp = 1:length(handles.V_cell),
    Cell_id_tmp = i_tmp;
    X_min_array(i_tmp) = min(V_cell{i_tmp}(1, :));
    X_max_array(i_tmp) = max(V_cell{i_tmp}(1, :));
    Y_min_array(i_tmp) = min(V_cell{i_tmp}(2, :));
    Y_max_array(i_tmp) = max(V_cell{i_tmp}(2, :));
    Z_min_array(i_tmp) = min(V_cell{i_tmp}(3, :));
    Z_max_array(i_tmp) = max(V_cell{i_tmp}(3, :));
end


%{
handles.X_min = min(V_cell{Cell_id}(1, :)) - .5;
handles.X_max = max(V_cell{Cell_id}(1, :)) + .5;
handles.Y_min = min(V_cell{Cell_id}(2, :)) - .5;
handles.Y_max = max(V_cell{Cell_id}(2, :)) + .5;
handles.Z_min = min(V_cell{Cell_id}(3, :)) - 2;
handles.Z_max = max(V_cell{Cell_id}(3, :)) + 1.5;
%}

handles.X_min = min(X_min_array) - .5;
handles.X_max = max(X_max_array) + .5;
handles.Y_min = min(Y_min_array) - .5;
handles.Y_max = max(Y_max_array) + .5;
handles.Z_min = min(Z_min_array) - .5;
handles.Z_max = max(Z_max_array) + .5;

%{
if handles.X_max > handles.Y_max,
    handles.Y_max = handles.X_max;
else
    handles.X_max = handles.Y_max;
end

if handles.X_min < handles.Y_min,
    handles.Y_min = handles.X_min;
else
    handles.X_min = handles.Y_min;
end
%}

axes(handles.axes1);

%Get the id of the constricting and the non-constricting verticies
id1 = zeros(Nv, 1);
for i=1:(handles.Nx-1),
    for j=1:(handles.Ny-1),
        id_tmp = i + (j-1)*(handles.Nx-1);
        if i >= handles.x_cons(1) && i <= handles.x_cons(2) && j >= handles.y_cons(1) && j <= handles.y_cons(2),
            id1(AP_ver_id{id_tmp}(1)+1) = 1;
            id1(AP_ver_id{id_tmp}(2)+1) = 1;
            id1(AP_ver_id{id_tmp}(3)+1) = 1;
            id1(AP_ver_id{id_tmp}(4)+1) = 1;
        else
            id1(AP_ver_id{id_tmp}(1)+1) = 2;
            id1(AP_ver_id{id_tmp}(2)+1) = 2;
            id1(AP_ver_id{id_tmp}(3)+1) = 2;
            id1(AP_ver_id{id_tmp}(4)+1) = 2;
        end
    end
end

t_measure = toc;disp(['To initialize the plot takes ', num2str(t_measure), ' seconds']);

    set(handles.axes1, 'nextplot' ,'replace');
   
    if handles.T_show,
        t_measure = 0; tic;
        %scatter3(handles.axes1, V_cell{Cell_id}(1, 1:(Nv/2)), V_cell{Cell_id}(2, 1:(Nv/2)), V_cell{Cell_id}(3, 1:(Nv/2)), 30, 'r', 'filled');
        scatter3(handles.axes1, V_cell{Cell_id}(1, id1 == 2), V_cell{Cell_id}(2, id1 == 2), V_cell{Cell_id}(3, id1 == 2), 30, 'r', 'filled');
        set(handles.axes1, 'nextplot' ,'add');
        scatter3(handles.axes1, V_cell{Cell_id}(1, id1 == 1), V_cell{Cell_id}(2, id1 == 1), V_cell{Cell_id}(3, id1 == 1), 30, 'g', 'filled');
        %set(handles.axes1, 'nextplot' ,'add');
        t_measure = toc;disp(['To plot top took ', num2str(t_measure), ' seconds']);
    end
    
    
    
    if handles.B_show,
        t_measure = 0; tic;
        scatter3(handles.axes1, V_cell{Cell_id}(1, id1 == 0), V_cell{Cell_id}(2, id1 == 0), V_cell{Cell_id}(3, id1 == 0), 30, 'b', 'filled');
        set(handles.axes1, 'nextplot' ,'add');
        t_measure = toc;disp(['To plot bottom took ', num2str(t_measure), ' seconds']);
    end
    
        %%{
    if handles.S_show,
        t_measure = 0; tic;
        for j=1:Nv/3,
            if handles.multilayer,
                plot3(handles.axes1, [V_cell{Cell_id}(1, j) V_cell{Cell_id}(1, j+Nv/2)], [V_cell{Cell_id}(2, j), V_cell{Cell_id}(2, j+Nv/2)], [V_cell{Cell_id}(3, j), V_cell{Cell_id}(3, j+Nv/2)], 'k', 'linewidth', 3);
            else
                plot3(handles.axes1, [V_cell{Cell_id}(1, j) V_cell{Cell_id}(1, j+Nv/3) V_cell{Cell_id}(1, j+2*Nv/3)], [V_cell{Cell_id}(2, j) V_cell{Cell_id}(2, j+Nv/3) V_cell{Cell_id}(2, j+2*Nv/3)], [V_cell{Cell_id}(3, j) V_cell{Cell_id}(3, j+Nv/3) V_cell{Cell_id}(3, j+2*Nv/3)], 'k', 'linewidth', 3);
            end
                set(handles.axes1, 'nextplot' ,'add');
        end
        t_measure = toc; disp(['To plot sides took ', num2str(t_measure), ' seconds']);
    end
    
    %}
    
    V_x = V_cell{Cell_id}(1, :); V_y = V_cell{Cell_id}(2, :); V_z = V_cell{Cell_id}(3, :);
    t_measure = 0; tic;
    for j=1:Nc,
    
        AP_ver = cell(4, 1);
        AP_ver{1} = [V_x(AP_ver_id{j}(1)+1), V_y(AP_ver_id{j}(1)+1), V_z(AP_ver_id{j}(1)+1)];
        AP_ver{2} = [V_x(AP_ver_id{j}(2)+1), V_y(AP_ver_id{j}(2)+1), V_z(AP_ver_id{j}(2)+1)];
        AP_ver{3} = [V_x(AP_ver_id{j}(3)+1), V_y(AP_ver_id{j}(3)+1), V_z(AP_ver_id{j}(3)+1)];
        AP_ver{4} = [V_x(AP_ver_id{j}(4)+1), V_y(AP_ver_id{j}(4)+1), V_z(AP_ver_id{j}(4)+1)];
        
        X1 = [AP_ver{1}(1);AP_ver{2}(1);AP_ver{4}(1);AP_ver{3}(1)];
        Y1 = [AP_ver{1}(2);AP_ver{2}(2);AP_ver{4}(2);AP_ver{3}(2)];
        Z1 = [AP_ver{1}(3);AP_ver{2}(3);AP_ver{4}(3);AP_ver{3}(3)];
        
        if handles.T_show,
            fill3(X1, Y1, Z1, 'r');%shading interp;
        end
        
        BS_ver = cell(4, 1);
        BS_ver{1} = [V_x(BS_ver_id{j}(1)+1), V_y(BS_ver_id{j}(1)+1), V_z(BS_ver_id{j}(1)+1)];
        BS_ver{2} = [V_x(BS_ver_id{j}(2)+1), V_y(BS_ver_id{j}(2)+1), V_z(BS_ver_id{j}(2)+1)];
        BS_ver{3} = [V_x(BS_ver_id{j}(3)+1), V_y(BS_ver_id{j}(3)+1), V_z(BS_ver_id{j}(3)+1)];
        BS_ver{4} = [V_x(BS_ver_id{j}(4)+1), V_y(BS_ver_id{j}(4)+1), V_z(BS_ver_id{j}(4)+1)];
        
        X1 = [BS_ver{1}(1);BS_ver{2}(1);BS_ver{4}(1);BS_ver{3}(1)];
        Y1 = [BS_ver{1}(2);BS_ver{2}(2);BS_ver{4}(2);BS_ver{3}(2)];
        Z1 = [BS_ver{1}(3);BS_ver{2}(3);BS_ver{4}(3);BS_ver{3}(3)];
        
        if handles.B_show,
            fill3(X1, Y1, Z1, 'b');%shading interp;
        end
        
    end
    t_measure = toc; disp(['To plot the fills took ', num2str(t_measure), ' seconds']);
    
    
    hold off; view(handles.view);
    %axis([-2 10 0 12 -2 6]);
    %axis([-.5 (handles.Nx - .5) -.5 (handles.Ny - .5) -.5 (abs(V_cell{1}(3,end) - V_cell{1}(3,1)) + .5)]);
    %axis([handles.X_min handles.X_max handles.Y_min handles.Y_max handles.Z_min - 18 handles.Z_max + 2]);
    %axis([-.7 1.7 -.7 1.7 -2.5 5]);
    %axis([handles.X_min handles.X_max handles.Y_min handles.Y_max -10 10]);
    %axis([handles.X_min handles.X_max handles.Y_min handles.Y_max handles.Z_min handles.Z_max]);
   axis([-20 20 -20 20 -20 20]);
    %axis([handles.X_min handles.X_max handles.Y_min handles.Y_max -ceil(handles.X_max/2) ceil(handles.X_max/2)]);
   
    title(['t = ', num2str(Cell_id)]);
    plot_selected_cell;
   
