function [hhh]=PlotPlane(T,world_ref,tam,varargin)
%
%   [hhh]=PlotPlane(T,world_ref,tam,varargin)
%     Plot the plane with the orientation/translation given by T
%     If the world_reference of the T is not reference to Matlab (0,0,0)
%
%   Input T, worlf_ref, tam, and othe option like string and color
%       PlotPlane(eye(4),eye(4),10,'color',[1 0 0],'string','something','color',[r g b])
%
%   If T is [n p] a normak vector and a point a plane orthogonal to n passing 
%        through p will be drawn
%
%
%   All the handlers are returned.

i=1;
color=[0.6 0.6 0.6];str='';
rate=[1 1];
while i<length(varargin)
  if isequal(varargin{i},'string')
    i=i+1;
    str=varargin{i};
  end
  if isequal(varargin{i},'color')
    i=i+1;
    color=varargin{i};
    if isnumeric(color)
      color=color*norm(color)^-1;
    end
  end
  if isequal(varargin{i},'rate')
   i=i+1;
   rate=varargin{i};
  end
   i=i+1; 
end

if ~exist('world_ref')
    world_ref=eye(4);
else
    [l c]=size(world_ref);
    if (l==c) && (l==1)
        tam=world_ref;
        world_ref=eye(4);
    end
    
end

if ~exist('tam')
    tam=150;
end

[l c]=size(T);

if (l==c) && (l==4)
    
    BASE = [ 0   tam     0     0     0     0
             0     0     0   tam     0     0
             0     0     0     0     0   tam ];
    
    dX=tam/5;
    d=dX;
    tt=[-d -d -d 1]';
    
    n_sq=5;
    dim_x=n_sq+1;
    dim_y=n_sq+1;
    
    x=0:dX:dX*n_sq;
    xx=[x x x x x x];
    yy=sort(xx);
    
    % desenha o plano
    
    X=[ xx ; yy ; zeros(1, length(xx)) ];
    
    X= world_ref*T*[ X ; ones(1,length(X)) ];
    
    
    xx=zeros(dim_x,dim_y);
    yy=zeros(dim_x,dim_y);
    zz=zeros(dim_x,dim_y);
    
    xx(:)=X(1,:);
    yy(:)=X(2,:);
    zz(:)=X(3,:);
    
    hold on
    hhh(1)=mesh(xx,yy,zz,'edgecolor',color);
    local_base=world_ref*T*[BASE ; ones(1,length(BASE)) ];
    % ref so com uma cor
    % plot3(local_base(1,:),local_base(2,:),local_base(3,:),'b','linewidth',2);
    % text(local_base(1,2),local_base(2,2),local_base(3,2),'x','fontsize',8);
    %ref rgb -> xyz
    hhh(2)=plot3(local_base(1,1:2),local_base(2,1:2),local_base(3,1:2),'r','linewidth',2);
    hhh(3)=plot3(local_base(1,3:4),local_base(2,3:4),local_base(3,3:4),'g','linewidth',2);
    hhh(4)=plot3(local_base(1,5:6),local_base(2,5:6),local_base(3,5:6),'b','linewidth',2);
    tt=world_ref*T*tt;
    hhh(5)=text(tt(1),tt(2),tt(3),str,'fontsize',10);
    hold off
    
elseif (l==3) && (c==2)
    
    u=world_ref*[T(:,1);1];
    u(4)=[];
    u=u/norm(u);
    p=world_ref*[T(:,2);1];
    p(4)=[];
    P=u'*p;
    
    
    hold on
    
    xyz=p;
    [nhec in]=sort(abs(u));
    hold on
    quiver3(xyz(1),xyz(2),xyz(3),u(1),u(2),u(3),tam,'linewidth',2)
    tam=[rate(1) rate(2) 1]*tam;
    % draw mirror plane
    [ppp(:,:,in(1)) ppp(:,:,in(2))]=meshgrid(xyz(in(1))-tam(1):tam(1)/3:xyz(in(1))+tam(1),xyz(in(2))-tam(2):tam(2)/3:xyz(in(2))+tam(2));
    ppp(:,:,in(3))=(P-u(in(1))*ppp(:,:,in(1))-u(in(2))*ppp(:,:,in(2)))/u(in(3));
  global vert;    
%     h=surf(ppp(:,:,1),ppp(:,:,2),ppp(:,:,3));
%     set(h,'facecolor',color)
    vert(:,1)=[ppp(1,1,1);ppp(1,end,1);ppp(end,end,1);ppp(end,1,1)];
    vert(:,2)=[ppp(1,1,2);ppp(1,end,2);ppp(end,end,2);ppp(end,1,2)];
    vert(:,3)=[ppp(1,1,3);ppp(1,end,3);ppp(end,end,3);ppp(end,1,3)];
    patch('vertices',vert,'faces',[1 2 3 4],'FaceColor',color)%,'FaceAlpha',0.4,'edgecolor','none')
% mesh(ppp(:,:,1),ppp(:,:,2),ppp(:,:,3));
    hold on
    
else
    fprintf(1,'Input must be a rigid transformation or a vector and a point [v p ]\n')
end




