www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/+cageview/+optimoutput/ValuesContourView.m

    classdef ValuesContourView < cageview.optimoutput.Values2dView
    %cageview.optimoutput.ValuesContourView class
    %   cageview.optimoutput.ValuesContourView extends cageview.optimoutput.Values2dView.
    %
    %    cageview.optimoutput.ValuesContourView properties:
    %       Parent - Property is of type 'MATLAB array'
    %       Position - Property is of type 'rect'
    %       Enable - Property is of type 'on/off'
    %       Visible - Property is of type 'on/off'
    %       UserData - Property is of type 'MATLAB array'
    %       Tag - Property is of type 'string'
    %       MessageService - Property is of type 'handle'
    %       Options - Property is of type 'handle vector'
    %       Actions - Property is of type 'handle vector'
    %       UIContextMenu - Property is of type 'MATLAB array'
    %       DisplayData - Property is of type 'ustring'
    %       CanRotate - Property is of type 'bool'
    %       ExtrapolateAll - Property is of type 'bool'
    %       DisplayColorbar - Property is of type 'bool'
    %       ShowAxesGrid - Property is of type 'bool'
    %       HideContours - Property is of type 'bool'
    %
    %    cageview.optimoutput.ValuesContourView methods:
    %       canPrint - Check whether component can be printed
    %       gettitle - Return string to use as title for view
    %       printCopy - Create a printer-friendly copy of OutputObjContourView
    
    %  Copyright 2007-2016 The MathWorks, Inc.
    
    properties (AbortSet, SetObservable)
        %EXTRAPOLATEALL Property is of type 'bool'
        ExtrapolateAll = false;
        %DISPLAYCOLORBAR Property is of type 'bool'
        DisplayColorbar = true;
        %SHOWAXESGRID Property is of type 'bool'
        ShowAxesGrid = true;
        %HIDECONTOURS Property is of type 'bool'
        HideContours = false;
    end
    
    properties (Access=protected, AbortSet)
        %HCOLORBAR Property is of type 'handle'
        hColorbar = [];
        %HCONTOURS Property is of type 'MATLAB array'
        hContours = [];
        %HCONTOURACTION Property is of type 'handle'
        hContourAction = [];
    end
    
    methods  % constructor block
        function obj = ValuesContourView(varargin)
        %ValuesContourView Constructor for ValuesContourView
        %  OBJ = ValuesContourView(PROP, VALUE) constructs a class that
        %  displays the results of an optimization on a 2-d plot. A surface is
        %  fitted through the optimization results and is displayed as a contour.
        
        % Call the inherited constructor
        obj@cageview.optimoutput.Values2dView(varargin{ : }); % converted super class constructor call
        
        % Disallow rotation of the axes
        obj.CanRotate = false;
        
        % Default axis values
        set(obj.hSelector.Axes, 'Color', 'w', 'Box', 'on', ...
            'XGrid', 'on', 'YGrid', 'on');
        view(obj.hSelector.Axes, 2);
        
        % About to create the contour. Set hold on to ensure the parent plot does
        % not get blown away.
        hold(obj.hSelector.Axes, 'on');
        
        % Contour
        [~, cg] = contour(obj.hSelector.Axes, []);
        set(cg, 'HitTest', 'off');
        obj.hContours = cg;
        
        % Set hold off.
        hold(obj.hSelector.Axes, 'off');
        
        % Set the contour group to the bottom of the stack, to ensure that the
        % points are never hidden
        uistack(obj.hContours, 'bottom');
        
        % Colorbar
        obj.hColorbar = mbcwidgets.ColorBar(...
            'Parent',obj.Parent, ...
            'Visible', obj.Visible, ...
            'RangeEditable', false);
        cbarcard = xregcardlayout(obj.Parent, ...
            'Visible', obj.Visible, ...
            'border', [0 65 0 20], ...
            'NumCards', 2);
        attach(cbarcard, obj.hColorbar, 1);
        set(obj, 'UserData', obj.hColorbar);
        
        % Augment layout from parent with colorbar
        obj.ContentHandle = xreggridbaglayout(obj.Parent, ...
            'dimension', [1 2], ...
            'colsizes', [-1 60], ...
            'elements', {obj.ContentHandle, cbarcard}, ...
            'position', obj.Position);
        
        % Create option actions
        AG = obj.ViewAction;
        AExtrapAll = mbcgui.actions.ToggleAction({@i_extrapolateallaction, obj}, ...
            '&Extrapolate All');
        AExtrapAll.Selected = obj.ExtrapolateAll;
        
        AGContOpt = mbcgui.actions.ActionGroup('', '&Contour Options');
        AGContOpt.MenuType = 'submenu';
        obj.hContourAction = cageview.optimoutput.ContourAction(obj.hContours);
        obj.hContourAction.setContourOptions(true);
        
        AShowAxesGrid = mbcgui.actions.ToggleAction({@i_showaxesgridaction, obj}, ...
            '&Show Axes Grid');
        AShowAxesGrid.Selected = obj.ShowAxesGrid;
        AHideContour = mbcgui.actions.ToggleAction({@i_hidecontouraction, obj}, ...
            '&Hide Contour');
        AHideContour.Selected = obj.HideContours;
        ACol = mbcgui.actions.ToggleAction({@i_displaycolbar, obj}, 'Display &Colorbar');
        ACol.Selected = obj.DisplayColorbar;
        AGContOpt.Actions = [obj.hContourAction.Actions(:)' AShowAxesGrid AHideContour ACol];
        
        % Combine all actions
        AG.Actions = [AG.Actions(:)', AExtrapAll, AGContOpt];
        obj.ViewAction = AG;
        
        % Perform initialisation tasks - do not perform super's refresh actions as
        % the super class has already done this.
        obj.pRefreshContour;
        
        % Listeners to synchronise the constraint display property and action
        obj.addPropertyListeners(...
            {'ExtrapolateAll', ...
            'ShowAxesGrid', ...
            'HideContours', ...
            'DisplayColorbar'}, ...
            {{@i_extrapolateall, obj}, ...
            {@i_showaxesgrid, obj}, ...
            {@i_hidecontour, obj}, ...
            {@i_setdisplaycolorbar, ACol, obj.ContentHandle, cbarcard}});
        end  % ValuesContourView
        
        
    end  % constructor block
    
    methods  % public methods
        %----------------------------------------
        function out = canPrint(obj) %#ok<MANU>
        %CANPRINT Check whether component can be printed
        %  CANPRINT(OBJ) returns true for OutputValues2dView components.
        
        out = true;
        
        end  % canPrint
        
        %----------------------------------------
        function str = gettitle(obj) %#ok<MANU>
        %GETTITLE Return string to use as title for view
        %  STR = GETTITLE(OBJ) returns a string that should be used as a title for
        %  the container the view sits in.
        
        str = 'Results Contour';
        
        end  % gettitle
        
        %----------------------------------------
        function newobj = printCopy(obj, fig)
        %PRINTCOPY Create a printer-friendly copy of OutputObjContourView
        %  LYT = PRINTCOPY(OBJ,FIG)
        
        % Create new axes, position them in the standard location and copy settings
        % from the view axes to the export ones
        AxCopyProps = {'XLim', 'YLim', 'Box', 'Clipping','CLimMode','ZLimMode'};
        ExportAx = obj.pCreatePrintableAxes(fig, AxCopyProps);
        
        % Copy the visible optimization results to the new axes
        ExportAx = pCopyLinesToPrintAxes(obj, ExportAx);
        
        % Copy the contour objects to the new axes
        xregGui.HGPlotCopy(obj.hContours, ExportAx);
        
        % Title the graph
        title(ExportAx, obj.pGetSelector.ZFactorName);
        
        % Add axis labels
        xlabel(ExportAx, obj.pGetSelector.XFactorName, 'Interpreter', 'none');
        ylabel(ExportAx, obj.pGetSelector.YFactorName, 'Interpreter', 'none');
        
        % Create a standard colorbar for the exported axes
        if obj.DisplayColorbar
            cl = [obj.hColorbar.Min, obj.hColorbar.Max];
            colorbar('peer',ExportAx);
            set(ExportAx, 'CLim', cl);
            bdEast = 100;
        else
            bdEast = 40;
        end
        newobj = xreglayerlayout(fig, ...
            'border', [65 40 bdEast 30], ...
            'elements', {ExportAx});
        
        set(ExportAx,'CLimMode','Auto','ZLimMode','auto')
        
        end  % printcopy
        
    end  % public methods
    
    methods (Access=protected)
        %----------------------------------------
        function pRefresh(obj)
        %PREFRESH Refresh view
        %   PREFRESH(OBJ) Refresh view in OBJ.
        
        % Call super's refresh to refresh the points
        pRefresh@cageview.optimoutput.Values2dView(obj)
        
        % Now refresh the surface
        obj.pRefreshContour;
        
        end  % pRefresh
        
        %----------------------------------------
        function pRefreshContour(obj)
        %PREFRESHCONTOUR Replot the extrapolation contour
        %   PREFRESHCONTOUR(OBJ) replots the extrapolation contour.
        
        % Get the optimization results which are fixed for the current node
        data = pGetOutputData(obj);
        
        if obj.hasData && ~obj.HideContours && ...
                size(data, 2) > 2 && ~isempty(obj.hContours)
            
            % Get the selected indices
            idxX = obj.hSelector.XFactor;
            idxY = obj.hSelector.YFactor;
            idxZ = obj.hSelector.ZFactor;
            
            % Get the optimization results which the user can alter on the current
            % node
            bAcceptableIdx = pGetUserOutputData(obj);
            
            % Create a extrapolation surface from optimization data.
            axes = i_getAxes(data, idxX, idxY);
            if obj.ExtrapolateAll
                extrapXData = data(:, idxX);
                extrapYData = data(:, idxY);
                extrapZData = data(:, idxZ);
            else
                extrapXData = data(bAcceptableIdx, idxX);
                extrapYData = data(bAcceptableIdx, idxY);
                extrapZData = data(bAcceptableIdx, idxZ);
            end
            IsConstant = false;
            if isempty(extrapXData) || isempty(axes{1})
                zz = [];
            elseif all( abs(extrapZData-mean(extrapZData))< sqrt(max( eps(max(abs(extrapZData))), eps)) )
                % constant data so don't extrapolate
                IsConstant = true;
                sz=[numel(axes{1}),numel(axes{2})];
                av = mean(extrapZData);
                % put a small perturbation on data so contour draws
                zz = zeros(sz);
                zz(:)=av;
                zz(1)=av+eps(av)*2;
                clim = mbcmakelimits(av);
                % set levels based on
                
                ContourArgs = {'LevelList', linspace(clim(1),clim(2),3)};
            else
                zz = eval(cgmathsobject,'extrapolate_RBF', ...
                    extrapXData, extrapYData, extrapZData, axes{1}, axes{2})'; %#ok<GTARG>
                Lauto = obj.hContourAction.Actions(3).Actions(1);
                if Lauto.Selected
                    ContourArgs = {'LevelListMode','auto'};
                else
                    ContourArgs = {};
                end
            end
            
            % Update the contour.
            if ~isempty(zz)
                [xx, yy] = ndgrid(axes{:});
                set(obj.hSelector.Axes,'CLimMode','Auto','ZLimMode','auto')
                set(obj.hContours, ...
                    'XData', xx, 'YData', yy, 'ZData', zz, ...
                    ContourArgs{:},...
                    'Visible','on');
                obj.hColorbar.setRange(get(obj.hSelector.Axes,'CLim'))
                obj.hColorbar.ColorMap = get(ancestor(obj.hColorbar.Parent,'figure'), 'Colormap');
                if IsConstant
                    % manually set the color limit so the contour color is constant
                    set(obj.hSelector.Axes,'CLim',clim);
                end
            else
                set(obj.hContours, 'Visible','off');
            end
            
            % Update the levels to be appropriate for the new data
            if obj.hContourAction.ContourLevels > 0 && ~isempty(zz)
                obj.hContourAction.setContourLevels(obj.hContourAction.ContourLevels);
            end
        else
            set(obj.hContours, 'Visible','off');
        end
        end  % pRefreshContour
        
        %----------------------------------------
        function data = pTransformDataForPoints(obj, data)
        %PTRANSFORMDATAFORPOINTS Apply any data transformations before plotting
        %   DATA = PTRANSFORMDATAFORPOINTS(OBJ, DATA) is called on the
        %   NPTS-by-NFACS data array before it is used to plot the solution points.
        %
        %   See also pGetOutputData.
        
        % The contour view doesn't want true zdata: this leads to points being
        % below the contour graph, which is itself not at a particular Z value.
        Zidx =  obj.hSelector.ZFactor;
        if ~isempty(Zidx) && Zidx>0 && Zidx<=size(data,2)
            data(:, Zidx) = 1;
        end
        
        end  % pTransformDataForPoints
        
    end
    
end  % classdef

function i_extrapolateallaction(src, ~, obj)
obj.ExtrapolateAll = src.Selected;
end  % i_extrapolateallaction

function i_extrapolateall(~, ~, obj)
obj.pRefreshContour;
end  % i_extrapolateall

function i_showaxesgridaction(src, ~, obj)
obj.ShowAxesGrid = src.Selected;
end  % i_showaxesgridaction

function i_showaxesgrid(~, ~, obj)
hSelector = obj.pGetSelector;
if obj.ShowAxesGrid
    gridState = 'on';
else
    gridState = 'off';
end
set(hSelector.Axes, 'XGrid', gridState, 'YGrid', gridState);
end  % i_showaxesgrid

function i_hidecontouraction(src, ~, obj)
obj.HideContours = src.Selected;
end  % i_hidecontouraction

function i_hidecontour(~, ~, obj)
obj.pRefreshContour;
end  % i_hidecontour

%--- Colorbar. Could be commonized?
function i_setdisplaycolorbar(~, evt, AColBar, hLayout, hCbarCard)
AColBar.Selected = evt.AffectedObject.DisplayColorBar;
if evt.AffectedObject.DisplayColorBar
    set(hLayout, 'ColSizes', [-1 60]);
    set(hCbarCard, 'CurrentCard', 1);
else
    set(hCbarCard, 'CurrentCard', 2);
    set(hLayout, 'ColSizes', [-1 0]);
end
end  % i_setdisplaycolorbar

function i_displaycolbar(src, ~, obj)
obj.DisplayColorbar = src.Selected;
end  % i_displaycolbar

function axes = i_getAxes(data, idxX, idxY)
axes = cell(1, 2);

% Optimization results
xdata = data(:, idxX);
ydata = data(:, idxY);

% Filter out any infs and NaNs before we create the grid
xdata = xdata(isfinite(xdata));
ydata = ydata(isfinite(ydata));
xlim = [min(xdata), max(xdata)];
ylim = [min(ydata), max(ydata)];
if ~isempty(xlim) && xlim(1)<xlim(2) && ...
        ~isempty(ylim) && ylim(1)<ylim(2)
    % Define a 50-by-50 grid for the extrapolation
    axes{1} = linspace(xlim(1),xlim(2), 50);
    axes{2} = linspace(ylim(1),ylim(2), 50);
end
end  % i_getAxes