function [camso voxelso] = space_partition2(cams,space,ang,d,min_v,max_v,lato)
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here

if nargin<5
    min_v=0.5;
    max_v=5;
end

if nargin<7
    lato=500;
end

a=space(1);
b=space(2);
c=space(3);

num_cameras=size(cams,2);           % angles=zeros(3,num_cameras);        %array that will contain the orientations
position=zeros(3,num_cameras);

theta=ang/2;

%divisione dello spazio in cubi
moda=mod(a,d);
xlength=a-moda;
a=xlength;
num_x=a/d;
modb=mod(b,d);
ylength=b-modb;
b=ylength;
num_y=b/d;
modc=mod(c,d);
zlength=c-modc;
c=zlength;
num_z=c/d;
num_cell=num_x*num_y*num_z;
space=zeros(num_cameras,num_cell);

%diamo ad ogni cubo un'etichetta che è la posizione del suo baricentro
voxelso(1).setcameras=[];
voxelso(1).position=[];

for i=1:num_cell
    voxelso(i).position=[mod((d/2)+d*(i-1),a);mod((d/2)+d*(floor((i-1)/num_x)),b);mod((d/2)+d*(floor((i-1)/(num_x*num_y))),c)];
end



h = waitbar(0,'Please wait...', 'BarColor', 'red');
for i=1:num_cameras                 %extraction of the orientations of the 
    
    cams(i).intrinsic=[1 0 lato/2;0 1 lato/2;0 0 1];
    position(:,i)=cams(i).position;
    cams(i).versore = cams(i).rotationmat*[0 0 1]';
    
    waitbar(i/num_cameras, h);
    Rca=cams(i).rotationmat;        %cameras expressed with Cardano angles
    vers(:,i)=Rca*[0 0 1]';         %versore che rappresenta la direzione dell'asse della telecamera nel sistema di riferimento assoluto
    Rac=Rca';
    space_temp=zeros(2,num_cell);
    
    xa=1; xd=d/2;
    ya=1; yd=d/2;
    za=1; zd=d/2;

    for j=1:num_cell
        vect=[xd,yd,zd]'-[position(1,i) position(2,i) position(3,i)]';
        if norm(vect)>= min_v && norm(vect)<= max_v
            vect=Rac*vect;
            %proiezione normalizzata sul piano x-z
            pxz=[vect(1);vect(3)];
            pxz=pxz/norm(pxz);
            %proiezione normalizzata sul piano y-z
            pyz=[vect(2);vect(3)];
            pyz=pyz/norm(pyz);
            csi1=acos([0 1]*pxz);
            csi2=acos([0 1]*pyz);

            if csi1<theta
                space_temp(1,j)=1;
            end
            if csi2<theta
                space_temp(2,j)=1;
            end
        end    
        xa=xa+1;
        if xa==num_x+1
            xa=1;
            ya=ya+1;
            if ya==num_y+1
                ya=1;
                za=za+1;
            end
        end
        xd=xa*d-d/2;
        yd=ya*d-d/2;
        zd=za*d-d/2;
          
        if (space_temp(1,j)==1) && (space_temp(2,j)==1)
            space(i,j)=1;        
        end
    end
%% %plot totale per visualizzare nello spazio l'area vista
%     figure
%     plot3(position(1,i),position(2,i),position(3,i),'bo')
%     hold on;
%     plot3(position(1,i)+vers(1,i),position(2,i)+vers(2,i),position(3,i)+vers(3,i),'ro')
%     
%        
%     xa=1; xd=d/2;
%     ya=1; yd=d/2;
%     za=1; zd=d/2;
%        
%     for j=1:num_cell
%         if space(i,j)==1
%             plot3(xd,yd,zd,'gx');
%             hold on;
%         end
%         xa=xa+1;
%         if xa==num_x+1
%             xa=1;
%             ya=ya+1;
%             if ya==num_y+1
%                 ya=1;
%                 za=za+1;
%             end
%         end
%         xd=xa*d-d/2;
%         yd=ya*d-d/2;
%         zd=za*d-d/2;
%     end
%     
end    
    
camso=cams;

    for i=1:num_cameras
        setv=[];
        for j=1:num_cell    
            if space(i,j)==1
                setv=[setv j];
            end
        end
        camso(i).voxels=setv;
    end

    for i=1:num_cell
        setc=[];
        for j=1:num_cameras
            if space(j,i)==1
                setc=[setc j];
            end
        end
        voxelso(i).setcameras=setc;
    end

    close(h);
end
