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

    classdef boundaryModelWrapper < handle & matlab.mixin.Copyable
    %BOUNDARYMODELWRAPPER wrapper class for sharing boundary models between CAGE models
    %
    %    See also conbase
    
    %  Copyright 2011-2013 The MathWorks, Inc. and Ford Global Technologies, Inc.
    
    properties
        %ConObject static con object 
        ConObject
    end
    
    methods
        
        function obj = boundaryModelWrapper(con)
        %boundaryModelWrapper constructor
        %   obj = boundaryModelWrapper(con)
        
        obj.ConObject = con;
        end
        
        function [obj, IN] = isInside(obj,varargin)
        %  isInside Check that points are inside the ALL the constraints
        %
        %  [C, IN] = isInside(C, X)
        %  [C, IN] = isInside(C, X, STARTINDEX)
        %  [C, IN] = isInside(C, X, STARTINDEX, KEEPPOINTS)
        
        [obj.ConObject,IN] = isInside(obj.ConObject,varargin{:});
        end
        
        function y = constraintDistance(obj,X)
        %CONSTRAINTDISTANCE evaluate constraint model
        %    y = constraintDistance(obj,X)
        
        y = constraintDistance(obj.ConObject,X);
        end
        
        
        function c = merge(varargin)
        %MERGE merge conswitch boundary models
        %   c = merge(c1,c2,...) c is the merged conswitch boundary model inside a
        %   boundaryModelWrapper if all boundary models are conswitch. Otherwise an [] is
        %   returned.
        
        
        m = varargin{end};
        ConModels = varargin(1:end-1);
        if all( cellfun(@isSwitchModel,ConModels) )
            % boundary models are merged if all are point-by-point
            s = getsymbols(m);
            con = cellfun(@(c) c.ConObject,ConModels,'UniformOutput',false);
            c = merge(con{:},s{end});
            % make sure the ranges are the same otherwise the tolerances will be
            % awry
            cif = getInputFactors(c);
            cif = setRange(cif,getranges(m));
            c = setInputFactors(c,cif);
            c = setTolerance(c,getTolerance(m));
            c = boundaryModelWrapper(c);
        else
            c = [];
        end
        end
        
        function OK = isSwitchModel(obj)
        %ISSWITCHMODEL true if internal constraint isa conswitch
        %   OK = isSwitchModel(obj)
        
        OK = isa(obj.ConObject,'conswitch');
        end
        
        function obj = upgrade(obj,mdl)
        %UPGRADE upgrade constraint object
        %    obj = upgrade(obj,mdl)
        obj.ConObject = upgrade(obj.ConObject,mdl);
        end
        
        
        function obj = syncToModel(obj,m,Xg)
        %syncToModel syncronhise conswitch constraint to model
        %  obj = syncToModel(obj,m,Xg)
        %      operating points and tolerances are of the conswitch are syncronhised with
        %      xregmodswitch model
            
            if isSwitchModel(obj)
                if nargin<3
                    Xg = getSwitchPoints(m);
                end
                % make sure boundary model is consistent
                cif = getInputFactors(obj.ConObject);
                % make sure the ranges are the same otherwise the tolerances will be
                % awry
                
                R = getranges(m);
                if ~isequal(R,getRange(cif));
                    % make a new copy of boundary model wrapper
                    obj = copy(obj);
                    cif = setRange(cif,R);
                    obj.ConObject = setInputFactors(obj.ConObject,cif);
                end
                obj.ConObject = setTolerance(obj.ConObject,getTolerance(m));
                % the boundary model operating points
                ConstraintOpPoints = getSwitchPoints(obj.ConObject);
                Xg = double(Xg);
                [~,KeepLast]= mbcUniqueFilters(Xg);
                if ~isequal(ConstraintOpPoints,Xg)
                    % make a new copy of boundary model wrapper
                    obj = copy(obj);
                    % the operating points can be different if some of the boundary models didn't fit
                    %  Note, ismember chooses the last item in ConstraintOpPoints if there are nonunique
                    %  points.
                    tol = getAbsoluteTolerance(obj.ConObject);
                    Xg = Xg(KeepLast,:);
                    [ok,loc] = curvefitlib.internal.isMemberWithinTol(Xg,ConstraintOpPoints,tol);
                    obj.ConObject = selectModels(obj.ConObject,loc(ok),Xg(ok,:) );
                elseif ~all(KeepLast)
                    % make a new copy of boundary model wrapper
                    obj = copy(obj);
                    obj.ConObject = selectModels(obj.ConObject,KeepLast);
                end
            end
        end
        
        function obj = SnapToGrid(obj,Grid)
        if isSwitchModel(obj)
            obj = copy(obj);
            obj.ConObject = SnapToGrid(m.constraints,Grid);
        end
        end
    end
    
end