
function [XV,YV,ZV,n]=maptopolygon3(xv,yv,zv,R10,RFM)

%--------------------------------------------------------------------------
% Object:
% This routine takes a polygon in the 3D space whose vertices are (XV,YV,ZV)
% and maps it to a flat polygon with vertices (xv,yv,0) by a "rigid
% motion".
% The vector "zv" and the rotation matrix "R10" allow to pull back the
% points.
%--------------------------------------------------------------------------
% Input:
%
% xv,yv: coordinates of the vertices of a polygon in the two
% dimensional space that is obtained by a "rigid motion" of a polygon with
% vertices (XV,YV,ZV); last vertex may not be repeated.
%
% R10,RFM: rotation and reflection matrices involved in the process.
%     It is "P=R10'*(RFM*P2+PS)" where "PS" is a certain shift, "P2" a 
%     the plane xy and "P" is a point in the 3D plane.
%
%--------------------------------------------------------------------------
% Output:
%
% XV,YV,ZV: coordinates of the vertices of a polygon in the three
% dimensional space (e.g. the vertices of a facet of a tetrahedra); last
% vertex may not be repeated.
%
% n: versor of outer normal to the plane containing [XV YV ZV].
%     
%--------------------------------------------------------------------------


P2=[xv'; yv'; zeros(size(zv'))];
PS=[zeros(size(zv')); zeros(size(zv')); zv' ];

P=R10'*(RFM*P2+PS);

XV=(P(1,:))';
YV=(P(2,:))';
ZV=(P(3,:))';

if nargout >= 4
    ee=[0;0;1];
    n=R10'*(RFM*ee); % outer normal to the facet
    n=n/norm(n); % versor of outer normal
end