viewPolytope

PURPOSE ^

plots a polytope in H-representation or V-representation

SYNOPSIS ^

function h = viewPolytope(poly, varargin)

DESCRIPTION ^

 plots a polytope in H-representation or V-representation

 h = viewPolytope returns the handles to the patch objects, which
   represent the facets of the polytope.

 viewPolytope(Pt, 'optionname', option) applies the following options:
    option   - a structure containing fields with options as below
                (additional values are preferred compared to options given
                here)
    color     - sets default color {'r' or [1 1 1]}
    shade     - level of transparency (0 - transparent, 1 - opaque)
    colormap  - sets a different colormap (default: 'hsv')
    edgecolor - color of edges (default is black)
    linewidth - width of the border of polytope (default is 1)
    linestyle - style of the border of each polytope (could be either '-',
                ':', ';' or 'none') (default is '-')
    flatness  - for degenerate objects; look for: help convertVrepToHrep.
                (default is 0)
    zerotol   - zerotol is used to identify values that are close to zero 
                by zero. (default is 1e-7)
    infinityBoxSize - look for: help convertHrepToVrep (default is 1e3)

 See also: obtainVrep, paintFacet

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function h = viewPolytope(poly, varargin)
0002 % plots a polytope in H-representation or V-representation
0003 %
0004 % h = viewPolytope returns the handles to the patch objects, which
0005 %   represent the facets of the polytope.
0006 %
0007 % viewPolytope(Pt, 'optionname', option) applies the following options:
0008 %    option   - a structure containing fields with options as below
0009 %                (additional values are preferred compared to options given
0010 %                here)
0011 %    color     - sets default color {'r' or [1 1 1]}
0012 %    shade     - level of transparency (0 - transparent, 1 - opaque)
0013 %    colormap  - sets a different colormap (default: 'hsv')
0014 %    edgecolor - color of edges (default is black)
0015 %    linewidth - width of the border of polytope (default is 1)
0016 %    linestyle - style of the border of each polytope (could be either '-',
0017 %                ':', ';' or 'none') (default is '-')
0018 %    flatness  - for degenerate objects; look for: help convertVrepToHrep.
0019 %                (default is 0)
0020 %    zerotol   - zerotol is used to identify values that are close to zero
0021 %                by zero. (default is 1e-7)
0022 %    infinityBoxSize - look for: help convertHrepToVrep (default is 1e3)
0023 %
0024 % See also: obtainVrep, paintFacet
0025 
0026 % The elk-library: convex geometry applied to crystallization modeling.
0027 %   Copyright (C) 2012 Alexander Reinhold
0028 %
0029 % This program is free software: you can redistribute it and/or modify it
0030 %   under the terms of the GNU General Public License as published by the
0031 %   Free Software Foundation, either version 3 of the License, or (at your
0032 %   option) any later version.
0033 %
0034 % This program is distributed in the hope that it will be useful, but
0035 %   WITHOUT ANY WARRANTY; without even the implied warranty of
0036 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0037 %   General Public License for more details.
0038 %
0039 % You should have received a copy of the GNU General Public License along
0040 %   with this program.  If not, see <http://www.gnu.org/licenses/>.
0041 
0042 %% map options
0043 % [opt.option, opt.shade, opt.color,...
0044 %  opt.linestyle, opt.linewidth, opt.edgecolor,...
0045 %  opt.flatness, opt.infinityBoxSize, opt.zerotol] = ...
0046 % mapOption(varargin,...
0047 %     'option', struct([]),...
0048 %     'shade', 0.8,...
0049 %     'color', [1 1 1]*0.5,...
0050 %     'linestyle', '-',...
0051 %     'linewidth', 1.5,...
0052 %     'edgecolor', [0 0 0],...
0053 %     'flatness', 0, ...
0054 %     'infinityBoxSize', 1e2, ...
0055 %     'zerotol', elkZerotol ...
0056 %     );
0057 opt = mapOptionStruct(varargin,...
0058     'shade', 0.8,...    
0059     'color', [1 1 1]*0.5,...
0060     'linestyle', '-',...
0061     'linewidth', 1.5,...
0062     'edgecolor', [0 0 0],...
0063     'flatness', 0, ...
0064     'infinityBoxSize', 1e2, ...
0065     'zerotol', elkZerotol ...
0066     );
0067 
0068 % opt = mergeStruct(opt.option, opt);
0069 % opt = rmfield(opt, 'option');
0070 
0071 %% check input dimension
0072 infoPoly = identifyPolytope(poly);
0073 if infoPoly.dim > 3 || infoPoly.dim < 2
0074     error('elk:polytope:wrongType', ...
0075         'viewPolytope can only plot 2D and 3D polytopes');
0076 end
0077 
0078 %% convert input to plottable polytope
0079 [vrep, hrep] = makePlottablePolytope(poly, opt.zerotol, opt.flatness, ...
0080     opt.infinityBoxSize);
0081 % empty polytope
0082 if isempty(vrep.V)
0083     h = [];
0084     return
0085 end
0086 
0087 %% Plot
0088 switch infoPoly.dim
0089     case 2
0090         % all at once - a single patch
0091         if size(vrep.V, 1) > 2
0092             K = convhull(vrep.V(:, 1), vrep.V(:, 2)); 
0093         elseif size(vrep.V, 1) == 2
0094             K = 1:2;
0095         elseif size(vrep.V, 1) == 1
0096             K = 1;
0097         end
0098         h = patch(vrep.V(K, 1), vrep.V(K, 2), vrep.V(K, 2)*0+1);
0099     case 3
0100         % cycle through facet normals - each facet a patch
0101         h = zeros(size(hrep.A, 1), 1);
0102         for iFacet = 1:size(hrep.A, 1)
0103             % filter vertices
0104             thisFacetVrep.V = ...
0105                 vrep.V(isZero(...
0106                   hrep.A(iFacet, :) * vrep.V' - hrep.h(iFacet), ...
0107                   opt.zerotol), :);
0108             
0109             % calculate facet in 2d
0110             thisFacetVrep2d = rotateVrep3d(...
0111                 thisFacetVrep, 'vector', hrep.A(iFacet, :), [0 0 0]);
0112             thisFacetVrep2d = moveVrep(...
0113                 thisFacetVrep2d,  hrep.h(iFacet)*[0 0 -1]);
0114             thisFacetVrep2d.V(:, 3) = [];
0115             
0116             % resort vertices for facet
0117             if size(thisFacetVrep2d.V, 1) > 2
0118                 K = convhull(thisFacetVrep2d.V(:, 1), ...
0119                     thisFacetVrep2d.V(:, 2));
0120             elseif size(thisFacetVrep2d.V, 1) == 2
0121                 K = 1:2;
0122             elseif size(thisFacetVrep2d.V, 1) == 1
0123                 K = 1;
0124             else
0125                 continue
0126                 error('elk:polytope:NumericalProblem', ...
0127                     'something is wrong with the dimension of the provided polytope');
0128             end
0129             thisFacetVrep.V = thisFacetVrep.V(K, :);
0130             
0131             % plot vertices
0132             h(iFacet) = patch(thisFacetVrep.V(:, 1), ...
0133                               thisFacetVrep.V(:, 2), ...
0134                               thisFacetVrep.V(:, 3), ...
0135                               ones(1,3));
0136             view([42, 23]);
0137         end
0138 end
0139 
0140 % filter empty patches - should this happen??
0141 h(h==0) = [];
0142 
0143 %% apply options
0144 set(h, 'facealpha', opt.shade, ...
0145        'facecolor', opt.color, ...
0146        'linewidth', opt.linewidth, ...
0147        'linestyle', opt.linestyle, ...
0148        'edgecolor', opt.edgecolor);
0149 
0150 if opt.shade == 0
0151     set(h, 'facecolor', 'none')
0152 end
0153 
0154 
0155 set(gca, 'PlotBoxAspectRatio', [1 1 1], ...
0156          'DataAspectRatio', [1 1 1],...
0157          'XGrid', 'off', 'YGrid', 'off', 'ZGrid', 'off');
0158 
0159 if nargout < 1
0160     clear h
0161 end
0162 
0163

Generated on Sat 18-Jul-2015 16:45:31 by m2html © 2005