rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
plot_polygon_grid.m
1 function p = plot_polygon_grid(grid,params)
2 %function p = plot_polygon_grid(grid[, params])
3 % plot method for a 2D polygonal grid. This routine can be used for triangular
4 % and rectangular grids.
5 %
6 % A line plot is performed as default.
7 %
8 % Parameters:
9 % params: optional structure holding fields controlling the plot output.
10 %
11 % Return values:
12 % p: This is the list of handles to the graphics primitives
13 %
14 % @todo For large grids, the routine can be slow. In these cases interestingly, the
15 % grid plotting should be implemented with patches, as that seems to be
16 % faster...
17 %
18 % optional fields of params:
19 % color : RGB vector of line/patch color
20 % shrink_factor : if this flag is given, the elements are plotted shrinked
21 % plot_patch : if this flag is set the plot is done
22 % by colored patches
23 % axis_equal : if this flag is set, set axis to equal scale
24 %
25 
26 % Bernard Haasdonk 9.5.2007
27 
28 if nargin==1
29  params = [];
30 end;
31 
32 if ~(isfield(params,'shrink_factor'))
33  params.shrink_factor = 1.0;
34 end;
35 
36 if ~(isfield(params,'plot_patch'))
37  params.plot_patch = 0;
38 end;
39 
40 if ~(isfield(params,'axis_equal'))
41  params.axis_equal = 0;
42 end;
43 
44 if ~(isfield(params,'axis_tight'))
45  params.axis_tight = 0;
46 end;
47 
48 if ~(isfield(params,'color'))
49  params.color = [0,0,1];
50 end;
51 
52 dim = 2;
53 nneigh = grid.nneigh;
54 
55 % compute vertex coordinates and scale
56 XX = grid.X(grid.VI(:));
57 XX = reshape(XX,size(grid.VI)); % nelements*4 matrix
58 YY = grid.Y(grid.VI(:));
59 YY = reshape(YY,size(grid.VI)); % nelements*4 matrix
60 
61 CXX = repmat(grid.CX(:),1,nneigh);
62 CYY = repmat(grid.CY(:),1,nneigh);
63 
64 % scale coordinates
65 XX = (XX - CXX) *params.shrink_factor + CXX;
66 YY = (YY - CYY) *params.shrink_factor + CYY;
67 
68 if params.plot_patch
69  p = patch(XX',YY',params.color);
70 else
71  li = [ 1:nneigh; 1:nneigh];
72  li = li(:);
73  li = [li(2:end);li(1)]; % => li = [1 2 2 3 3 4 4 1];
74 
75  XX = XX(:,li)'; % => 2nneigh X nelements matrix
76  XX = reshape(XX,2,nneigh*grid.nelements);
77  YY = YY(:,li)';
78  YY = reshape(YY,2,nneigh*grid.nelements);
79  p = line(XX,YY,'Color',params.color);
80 end;
81 
82 if params.axis_equal
83  axis equal;
84 end;
85 
86 if params.axis_tight
87 % DO NOT USE axis tight here!!! takes 1 minute with R2009a !!!!!!!
88  % axis tight;
89  xmin = min(XX(:));
90  xmax = max(XX(:));
91  ymin = min(YY(:));
92  ymax = max(YY(:));
93  set(gca,'Xlim',[xmin,xmax],'Ylim',[ymin, ymax]);
94 end;
95