www.gusucode.com > mbctools 工具箱 matlab 源码程序 > mbctools/xregcontours2patches.m

    function hPatch = xregcontours2patches( C, H, varargin )
%XREGCONTOURSTOPATCHES CONTOURS output to patch object conversion
%
%   xregcontours2patches(C,H) where C is the output from CONTOURS and H is
%   an axes handle, is a handles to a patch object that C describes. This
%   patch object will be attached to the axes H. It is assumed that each
%   line segment forms a closed patch. This can be assured from CONTOURS
%   by a suitable padding of the function evaluation matrix.
%
%   xregcontours2patches(C,H,<Parameter>,<Value>,...) sets additional
%   properties of the patch object.  In addition, the following non-patch
%   parameters are supported:
%
%   FaceColor: The color of the main faces.  Default is [0.5, 0.5, 1]
%   HoleColor: The color of the faces that are holes.  This defaults to the
%              color of the parent axes.
%   FaceLevel: The Z-value of the main faces.  Default is 0.
%   HoleLevel: The Z-value of the hole faces.  Default is 0.
%
%   The patches are colored according to the sign of their areas: patches
%   positive area are light blue [0.5, 0.5, 1] and patches with negative
%   area are colored the same as the background color. This means that C
%   should come from CONTOURS and not CONTOURC and that a binary value
%   function will have its patches correctly coloured (except possibly any
%   patches on infinite extent which aren't listed in the output anyway.
%
%   See also CONTOURS.

%  Copyright 2000-2008 The MathWorks, Inc. and Ford Global Technologies, Inc.

%     The contour matrix C is a two row matrix of contour lines. Each
%     contiguous drawing segment contains the value of the contour,
%     the number of (x,y) drawing pairs, and the pairs themselves.
%     The segments are appended end-to-end as
%
%        C = [level1 x1 x2 x3 ... level2 x2 x2 x3 ...;
%             pairs1 y1 y2 y3 ... pairs2 y2 y2 y3 ...]

% Check for face colour property
FACE_COLOUR = [0.5, 0.5, 1]; % light blue
HOLE_COLOUR = get(H, 'Color'); % Color of holes; defaults to axes color.
FACE_LEVEL = 0;  % Z value for patchs
HOLE_LEVEL = 0;   % Z value for holes
for i = length( varargin )-1:-2:1,
    switch upper(varargin{i})
        case 'FACECOLOR'
            FACE_COLOUR = varargin{i+1};
            varargin([i, i+1]) = [];
        case 'HOLECOLOR'
            HOLE_COLOUR = varargin{i+1};
            varargin([i, i+1]) = [];
        case 'FACELEVEL'
            FACE_LEVEL = varargin{i+1};
            varargin([i, i+1]) = [];
        case 'HOLELEVEL'
            HOLE_LEVEL = varargin{i+1};
            varargin([i, i+1]) = [];
    end
end

% First count the number of faces that C holds data for, and the max
% number of vertices in a single patch.
NumFaces = 0;
MaxVertices = 0;
n = 1;
while n<size(C, 2)
    npts = C(2,n);
    if npts>MaxVertices
        MaxVertices = npts;
    end
    NumFaces = NumFaces+1;
    n = n + npts + 1;
end

% Build up data for each face
XVert = cell(1, NumFaces);
YVert = cell(1, NumFaces);
ZVert = cell(1, NumFaces);
Faces = NaN(NumFaces, MaxVertices);
FaceColours = zeros(NumFaces, 3);
n = 1;
for i = 1:NumFaces
    npts = C(2,n);
    xp = C(1,(n+1):(n+npts));
    yp = C(2,(n+1):(n+npts));
    area  = sum( diff(xp).*(yp(1:npts-1)+yp(2:npts))/2 );
    
    XVert{i} = xp;
    YVert{i} = yp;
    Faces(i,1:npts) = (n+1-i):(n+npts-i);
    zp = yp;
    if area>0
        zp(:) = FACE_LEVEL;
        FaceColours(i,:) = FACE_COLOUR;
    else
        zp(:) = HOLE_LEVEL;
        FaceColours(i,:) = HOLE_COLOUR;
    end
    ZVert{i} = zp;
    
    n = n + npts + 1;
end

vertices = [[XVert{:}]; [YVert{:}]; [ZVert{:}]].';

hPatch = patch( ... ...
    'Parent', H, ...
    'Vertices', vertices, ...
    'Faces',    Faces, ...
    'FaceVertexCData', FaceColours, ...
    'FaceColor', 'Flat', ...
    varargin{:} );