www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@cgtabgradconstraint/private/pFilterGridPts.m

    function [D, g, idxReq, idxReqCon] = pFilterGridPts(obj, pts, D, g)
%PFILTERGRIDPTS Filter grid points using filter function
%
%   [D, G, IDXREQ, IDXREQCON] = PFILTERGRIDPTS(OBJ, PTS, D, G)
%   filters the grid points that lie outside the boundary of PTS from the
%   gradient operators (D, G). The boolean vector, IDXREQ,
%   indicates which points are inside the boundary. The boolean vector,
%   IDXREQCON, indicates which linear constraints remain after the filter
%   has been applied.
%
%   See also PCONVHULLGRIDFILTER, PIDENTITYGRIDFILTER

%   Copyright 2006-2007 The MathWorks, Inc.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                FILTERING CURRENTLY SWITCHED OFF                         %
%                                                                         %
%   This is due to an assumption made by cgoptimrunners that the number   %
%   of outputs that a cgoptimitem returns does not vary between runs and  %
%   is only dependent on the lengths of the object's inputs.              %
%   Furthermore, it is assumed that the number of outputs a cgoptimitems  %
%   produce is returned in getNumOutputs. Breaking these assumptions, can %
%   have bad consequences, for example, plotting in the optim output node %
%   breaks.                                                               %
%                                                                         %
%   With filtering turned on, cgtabgradconstraint violates these          %
%   assumptions                                                           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% Number of axes
NAXES = length(obj.pAxisVariables);

% Call the filter function
idxReq = obj.FilterGridFcn(obj.ScaledAxisBreakpoints, pts);

% Morphologically dilate the valid region - we trust the interpolation a
% cell away from the constraint boundary
switch NAXES
    case 1
        idxReq = i_1dMorphDilate(idxReq);
    case 2
        idxReq = i_2dMorphDilate(idxReq);
    otherwise
        error(message('mbc:cgtabgradconstraint:InvalidState2'));
end

% Convert to required index to vector'
idxReq = idxReq(:);

% Find the constraints that do not contain filtered grid points
unwantedD = D(:, ~idxReq);
idxReqCon = ~any(unwantedD, 2);

% Return the filtered operators
D = D(idxReqCon, idxReq);
g = g(idxReqCon);

%--------------------------------------------------------------------------
function idxReq = i_1dMorphDilate(idxReq)
%--------------------------------------------------------------------------

sz = numel(idxReq);
idx = find(idxReq);
if ~isempty(idx)
    idxStart = max(1, idx(1)-1);
    idxEnd = min(sz, idx(end)+1);
    idxReq([idxStart idxEnd]) = true;
end

%--------------------------------------------------------------------------
function mIdxReq = i_2dMorphDilate(idxReq)
%--------------------------------------------------------------------------

mIdxReq = idxReq;
sz = size(idxReq);
for i = 2:sz(1)-1
    for j = 2:sz(2)-1
        if idxReq(i, j)
            % Dilate using a 3*3 basis
            mIdxReq(i-1:i+1, j-1:j+1) = true;
        end
    end
end