www.gusucode.com > mbcdesign 工具箱 matlab 源码程序 > mbcdesign/@conboolean/constraintDistance.m

    function G = constraintDistance(c, X)
%CONSTRAINTDISTANCE Signed distance to constraint boundary for a list of points
%  
%  D = CONSTRAINTDISTANCE(CON, X) is the signed distance to the boundary of the
%  constraint CON for the list of points X. 
%  
%  X should be an array with nFactors( CON ) columns. D will be a vector with
%  the same number of rows as X.
%
%  Boolean constraints combine the constraint distances of the aggregated
%  constraints in ways suitable for the boolean operation:
%
%   AND constraints: maximum distance returned from all constraints
%   OR constraints : minimum distance returned from all constraints
%   XOR constraints: product of the distance returned from the two constraints
%   NOT constraints: negative of the distance returned by the constraint
%
%  Note that the NOT operation is done last. This means that if a constraint is
%  both a NOT and an AND constraint then it is NOT( AND( c1, c2, ... ) ) and the
%  negation is performed after the maximum value has been taken.
%
%  See also CONBOOLEAN.

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

% Setup default output
%   By default all points are inside and since on the boundary is inside
%   and not( 0 ) is also inside, we use default values of zero.
G = zeros( size( X, 1 ), 1 );

% Number of constraints
nc = length( c.Constraints ); 
if nc == 0,
    return
end
% filter factors for ActiveFactors
% Normally a conboolean won't have active factors but when the boundary
% model is imported into the design editor it can set ActiveFactors to
% reorder the inputs
X = pFilterFactors( c, X );

switch lower( c.Op ),
    case 'none',
        if nc ~= 1,
            warning(message('mbc:conboolean:InvalidState'));
        end
        G = constraintDistance( c.Constraints{1}, X );
        
    case 'and',
        G = constraintDistance( c.Constraints{1}, X );
        for i = 2:nc,
            G = max( G, constraintDistance( c.Constraints{i}, X ) );
        end
        
    case 'or',
        G = constraintDistance( c.Constraints{1}, X );
        for i = 2:nc,
            G = min( G, constraintDistance( c.Constraints{i}, X ) );
        end
        
    case 'xor',
        if nc ~= 2,
            warning(message('mbc:conboolean:InvalidState'));
        end
        G = constraintDistance( c.Constraints{1}, X ) .* ...
            constraintDistance( c.Constraints{2}, X );
    otherwise
        warning(message('mbc:conboolean:InvalidState'));
end

% 'not' the constraints
if c.Not,
    G = -G;
end

%------------------------------------------------------------------------------|
% EOF
%------------------------------------------------------------------------------|