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

    function Y = bringInside(con, X)
%BRINGINSIDE Bring points inside a constraint
%
%  Y = BRINGINSIDE(CON, X) is the data set X but with any points outside the
%  constraint CON projected into the constraint.
%  If X(i,:) is inside the constraint, then Y(i,:) = X(i,:).
%  If X(i,:) is outside the constriant, then Y(:,i) will be the closest point to
%  X(i,:) that is inside (on the boundary of) the constraint.
%
%  See also CONSTAR, CONSTAR/CONSTRAINTDISTANCE, CONBASE/ISINSIDE, 
%     CONBASE/BRINGINSIDE.

%  Copyright 2000-2005 The MathWorks, Inc.

% By default we don't move any points
Y = X;

% Now need to process only the active factors ...
X = pFilterFactors( con, X );
% ... and in scaled units
X = pCodeData( con, X );

% Project onto sphere and find radii
%  -- "r" is distance of the points from the center
[YY, r] = map_to_sphere( X );

% Evaluate sphereical based interpolant
%  -- "R" is the distance to the boundary from the center for each of the data
%     points
R = eval( con.Model, YY );
R = transform_radius( R, con.Transform, 'Inverse' );

% Only want to move points that are outside the constraint, i.e., where the
% actual distance is greater than that to the boundary
out = r > R;

if any( out ),
    % For the points outside, take their projection onto the sphere (at unit
    % distance) and move this onto the boundary, i.e., multiply by the distance
    % to the boundary.
    % This then needs to be coded back into natural units
    X = i_SparseDiagonal( R(out) ) * YY(out,:);
    % This then needs to be coded back into natural units
    X = pInverseCodeData( con, X );

    % For the active factors and the points that are outside, we need to update the
    % dataset.
    ai = getActiveIndices( con );
    Y(out,ai) = X;
end

%------------------------------------------------------------------------------|
function D = i_SparseDiagonal( d )
n = length( d );
D = sparse( 1:n, 1:n, d, n, n );

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