rbmatlab 0.10.01
grid/@cubegrid/plot.m
00001 function p = plot(grid,params)
00002 %function p = plot(grid,params)
00003 % plot method for a cubegrid.
00004 %
00005 % A line plot is performed. Currently implementation for more than 3D not
00006 % implemented, as this would be a line-mess
00007 %
00008 % Return values:
00009 %  p: this is the list of handles to the graphics primitives
00010 %
00011 % optional fields of params:
00012 %   color : RGB vector
00013 %   shrink_factor: if this flag is given, the elements are plotted shrinked
00014 %   plot_level:   if this flag is nonzero, a level plot is performed
00015 %   level  : this integer must be specified in case of level-plot    
00016 %   plot_patch    : if this flag is set (only 2D and 3D) the plot is done
00017 %                   by colored patches
00018 %   axis_equal    : if this flag is set, set axis to equal scale
00019 %
00020 % Bernard Haasdonk 1.3.2007
00021 
00022 if nargin==1
00023   params = [];
00024 end;
00025 
00026 if ~(isfield(params,'shrink_factor'))
00027   params.shrink_factor = 1.0;
00028 end;
00029 
00030 if ~(isfield(params,'plot_level'))
00031   params.plot_level = 0;
00032 end;
00033 
00034 if ~(isfield(params,'level'))
00035   params.level = 0;
00036 end;
00037 
00038 if ~(isfield(params,'plot_patch'))
00039   params.plot_patch = 0;
00040 end;
00041 
00042 if ~(isfield(params,'axis_equal'))
00043   params.axis_equal = 0;
00044 end;
00045 
00046 if ~(isfield(params,'color'))
00047   params.color = [0,0,1];
00048 end;
00049 
00050 if ~(isfield(params,'LineStyle'))
00051     params.LineStyle = '-'
00052 end;
00053 
00054 dim = grid.dimension;
00055 
00056 % determine elements to be plotted
00057 if (params.plot_level)
00058   ids = find(grid.level==params.level);
00059 else
00060   ids = find(grid.isleaf);  
00061 end;
00062 
00063 if isempty(ids)
00064   disp('no elements on current level or grid empty')
00065 end;
00066 
00067 if ~params.plot_patch % i.e. line mode required
00068   % generate list of local coordinates to be connected
00069   % idea: plot n-1 d bottom cube, its shifted top version and the 
00070   % connecting lines:
00071   % dim = 1 =>    [1 2]
00072   % dim = 2 =>    [1 2; 3,4; 1 3; 2 4]
00073   % dim = 3 =>    [1 2; 3,4; 1 3; 2 4, 5 6; 7,8; 5 7; 6 8; 1 5 ; 2 6 ;
00074   %                3 7 ; 4 8]
00075   li = [1 2];
00076   for i = 2:dim
00077     % assume li is local coordinate li of lower n-1 d patch
00078     li = [li; li + 2^(i-1)];
00079     % now li is local coordinate li of lower and upper n-1 d patch
00080     li = [li; (1:2^(i-1))' ,(1:2^(i-1))'+2^(i-1)];
00081   end;
00082 else % generate patch-list in local coordinates
00083   switch dim
00084    case 2
00085     li = [1 2 4 3];
00086    case 3
00087     li = [1 2 4 3; ...
00088           5 6 8 7; ...
00089           1 2 6 5; ...
00090           2 4 8 6; ...
00091           4 3 7 8; ...
00092           3 1 5 7 ];
00093    otherwise
00094     error('patch mode requires dim= 2 or 3!');
00095   end;
00096 end;
00097 
00098 Xtotal = [];
00099 Ytotal = [];
00100 Ztotal = [];
00101 
00102 X = zeros(size(li,2),size(li,1));
00103 Y = zeros(size(li,2),size(li,1));
00104 if dim >= 3
00105   Z = zeros(size(li,2),size(li,1));
00106 else
00107   Z = [];
00108 end;
00109 
00110 % stupid plotting: loop over all elements. should be vectorized sometime 
00111 
00112 for nel = ids(:)'
00113   % collect globalcoordinates of points for element
00114   ve = grid.vertexindex(nel,:);
00115   co = grid.vertex(ve,:); % => co is nlocalpoints x dimension matrix
00116                           % midpoint of element as matrix
00117   cog = repmat(mean(co),size(co,1),1); 
00118   % scale coordinates
00119   co = (co - cog)*params.shrink_factor + cog;
00120   
00121   % generate coordinate lists to be plotted
00122   % X,Y,Z: 2 x nlines matrix
00123   switch grid.dimension
00124    case 1
00125     % only x-coord varying
00126     X(1,:) = co(li(:,1),1);
00127     X(2,:) = co(li(:,2),1);    
00128    case 2
00129     % x and y-coord varying
00130     X = reshape(co(li',1),size(X));
00131     Y = reshape(co(li',2),size(Y));
00132    case 3
00133     % x and y- and z-coord varying
00134     X = reshape(co(li',1),size(X));
00135     Y = reshape(co(li',2),size(Y));
00136     Z = reshape(co(li',3),size(Z));
00137   end;      
00138   Xtotal = [Xtotal, X];
00139   Ytotal = [Ytotal, Y];
00140   Ztotal = [Ztotal, Z];  
00141 end;
00142 
00143 switch grid.dimension
00144  case 1
00145   p = line(Xtotal,Ytotal,'Color',params.color,'LineStyle',params.LineStyle);  
00146  case 2
00147   if params.plot_patch
00148     p = patch(Xtotal,Ytotal,params.color);
00149   else
00150     p = line(Xtotal,Ytotal,'Color',params.color,'LineStyle',params.LineStyle);
00151   end;
00152  case 3
00153   if params.plot_patch
00154     p = patch(Xtotal,Ytotal,Ztotal,params.color);
00155   else
00156     p = line(Xtotal,Ytotal,Ztotal,'Color',params.color,LineStyle,params.LineStyle);
00157   end;
00158   view(3);
00159  otherwise
00160   disp('plot for dimension>3 not yet implemented!')
00161   return;
00162 end;
00163 
00164 if params.axis_equal
00165   axis equal;
00166 end;
00167 
00168 end
00169 
 All Classes Namespaces Files Functions Variables