function [thetaw,xw]=cubature_cap(x,gamma,n,fig)

[temp,xwt]=cubature_2_sphere([0,2*pi;0,gamma],n,0);

[x_theta,x_phi,r]=cart_2_sph(x);

R1=[cos(x_phi),0,sin(x_phi);0,1,0;-sin(x_phi),0,cos(x_phi)];

R2=[cos(x_theta),-sin(x_theta),0;sin(x_theta),cos(x_theta),0;0,0,1];

xw=R1*R2*xwt(:,1:3)';
xw=xw';
xw(:,4)=xwt(:,4);

thetaw=zeros(size(xw,1),3);
[thetaw(:,1),thetaw(:,2),r]=cart_2_sph(xw(:,1:3));
thetaw(:,3)=xw(:,4);

if fig==1
    pos=find(xw(:,3)>=0);
    neg=find(xw(:,3)<0);
    destra=find(xw(:,2)>=0);
    sinistra=find(xw(:,2)<0);
    anteriore=find(xw(:,1)>=0);
    posteriore=find(xw(:,1)<0);
    if size(pos,1)~=0
       xwpos=xw(pos,:);
       tri1=delaunay(xwpos(:,1),xwpos(:,2));
       colore1=(xwpos(tri1(:,1),4)+xwpos(tri1(:,2),4)+xwpos(tri1(:,3),4))/3;
    end
    if size(neg,1)~=0
       xwneg=xw(neg,:);
       tri2=delaunay(xwneg(:,1),xwneg(:,2));
       colore2=(xwneg(tri2(:,1),4)+xwneg(tri2(:,2),4)+xwneg(tri2(:,3),4))/3;
    end
    if size(destra,1)~=0
       xwd=xw(destra,:);
       trid=delaunay(xwd(:,1),xwd(:,3));
       colored=(xwd(trid(:,1),4)+xwd(trid(:,2),4)+xwd(trid(:,3),4))/3;
    end
    if size(sinistra,1)~=0
       xws=xw(sinistra,:);
       tris=delaunay(xws(:,1),xws(:,3));
       colores=(xws(tris(:,1),4)+xws(tris(:,2),4)+xws(tris(:,3),4))/3;
    end
    if size(anteriore,1)~=0
       xwa=xw(anteriore,:);
       tria=delaunay(xwa(:,2),xwa(:,3));
       colorea=(xwa(tria(:,1),4)+xwa(tria(:,2),4)+xwa(tria(:,3),4))/3;
    end
    if size(posteriore,1)~=0
       xwp=xw(posteriore,:);
       trip=delaunay(xwp(:,2),xwp(:,3));
       colorep=(xwp(trip(:,1),4)+xwp(trip(:,2),4)+xwp(trip(:,3),4))/3;
    end
    figure(1)
    hold on
    if size(pos,1)~=0
       trisurf(tri1,xwpos(:,1),xwpos(:,2),xwpos(:,3),colore1)
    end
    if size(neg,1)~=0
       trisurf(tri2,xwneg(:,1),xwneg(:,2),xwneg(:,3),colore2)
    end
    if size(destra,1)~=0
       trisurf(trid,xwd(:,1),xwd(:,2),xwd(:,3),colored)
    end
    if size(sinistra,1)~=0
       trisurf(tris,xws(:,1),xws(:,2),xws(:,3),colores)
    end
    if size(anteriore,1)~=0
       trisurf(tria,xwa(:,1),xwa(:,2),xwa(:,3),colorea)
    end
    if size(posteriore,1)~=0
       trisurf(trip,xwp(:,1),xwp(:,2),xwp(:,3),colorep)
    end
    hold off
end