rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
plot.m
1 function p = plot(grid,params)
2 %function p = plot(grid,params)
3 % plot method for a cubegrid.
4 %
5 % A line plot is performed. Currently implementation for more than 3D not
6 % implemented, as this would be a line-mess
7 %
8 % Return values:
9 % p: this is the list of handles to the graphics primitives
10 %
11 % optional fields of params:
12 % color : RGB vector
13 % shrink_factor: if this flag is given, the elements are plotted shrinked
14 % plot_level: if this flag is nonzero, a level plot is performed
15 % level : this integer must be specified in case of level-plot
16 % plot_patch : if this flag is set (only 2D and 3D) the plot is done
17 % by colored patches
18 % axis_equal : if this flag is set, set axis to equal scale
19 %
20 % Bernard Haasdonk 1.3.2007
21 
22 if nargin==1
23  params = [];
24 end;
25 
26 if ~(isfield(params,'shrink_factor'))
27  params.shrink_factor = 1.0;
28 end;
29 
30 if ~(isfield(params,'plot_level'))
31  params.plot_level = 0;
32 end;
33 
34 if ~(isfield(params,'level'))
35  params.level = 0;
36 end;
37 
38 if ~(isfield(params,'plot_patch'))
39  params.plot_patch = 0;
40 end;
41 
42 if ~(isfield(params,'axis_equal'))
43  params.axis_equal = 0;
44 end;
45 
46 if ~(isfield(params,'color'))
47  params.color = [0,0,1];
48 end;
49 
50 if ~(isfield(params,'LineStyle'))
51  params.LineStyle = '-'
52 end;
53 
54 dim = grid.dimension;
55 
56 % determine elements to be plotted
57 if (params.plot_level)
58  ids = find(grid.level==params.level);
59 else
60  ids = find(grid.isleaf);
61 end;
62 
63 if isempty(ids)
64  disp('no elements on current level or grid empty')
65 end;
66 
67 if ~params.plot_patch % i.e. line mode required
68  % generate list of local coordinates to be connected
69  % idea: plot n-1 d bottom cube, its shifted top version and the
70  % connecting lines:
71  % dim = 1 => [1 2]
72  % dim = 2 => [1 2; 3,4; 1 3; 2 4]
73  % dim = 3 => [1 2; 3,4; 1 3; 2 4, 5 6; 7,8; 5 7; 6 8; 1 5 ; 2 6 ;
74  % 3 7 ; 4 8]
75  li = [1 2];
76  for i = 2:dim
77  % assume li is local coordinate li of lower n-1 d patch
78  li = [li; li + 2^(i-1)];
79  % now li is local coordinate li of lower and upper n-1 d patch
80  li = [li; (1:2^(i-1))' ,(1:2^(i-1))'+2^(i-1)];
81  end;
82 else % generate patch-list in local coordinates
83  switch dim
84  case 2
85  li = [1 2 4 3];
86  case 3
87  li = [1 2 4 3; ...
88  5 6 8 7; ...
89  1 2 6 5; ...
90  2 4 8 6; ...
91  4 3 7 8; ...
92  3 1 5 7 ];
93  otherwise
94  error('patch mode requires dim= 2 or 3!');
95  end;
96 end;
97 
98 Xtotal = [];
99 Ytotal = [];
100 Ztotal = [];
101 
102 X = zeros(size(li,2),size(li,1));
103 Y = zeros(size(li,2),size(li,1));
104 if dim >= 3
105  Z = zeros(size(li,2),size(li,1));
106 else
107  Z = [];
108 end;
109 
110 % stupid plotting: loop over all elements. should be vectorized sometime
111 
112 for nel = ids(:)'
113  % collect globalcoordinates of points for element
114  ve = grid.vertexindex(nel,:);
115  co = grid.vertex(ve,:); % => co is nlocalpoints x dimension matrix
116  % midpoint of element as matrix
117  cog = repmat(mean(co),size(co,1),1);
118  % scale coordinates
119  co = (co - cog)*params.shrink_factor + cog;
120 
121  % generate coordinate lists to be plotted
122  % X,Y,Z: 2 x nlines matrix
123  switch grid.dimension
124  case 1
125  % only x-coord varying
126  X(1,:) = co(li(:,1),1);
127  X(2,:) = co(li(:,2),1);
128  case 2
129  % x and y-coord varying
130  X = reshape(co(li',1),size(X));
131  Y = reshape(co(li',2),size(Y));
132  case 3
133  % x and y- and z-coord varying
134  X = reshape(co(li',1),size(X));
135  Y = reshape(co(li',2),size(Y));
136  Z = reshape(co(li',3),size(Z));
137  end;
138  Xtotal = [Xtotal, X];
139  Ytotal = [Ytotal, Y];
140  Ztotal = [Ztotal, Z];
141 end;
142 
143 switch grid.dimension
144  case 1
145  p = line(Xtotal,Ytotal,'Color',params.color,'LineStyle',params.LineStyle);
146  case 2
147  if params.plot_patch
148  p = patch(Xtotal,Ytotal,params.color);
149  else
150  p = line(Xtotal,Ytotal,'Color',params.color,'LineStyle',params.LineStyle);
151  end;
152  case 3
153  if params.plot_patch
154  p = patch(Xtotal,Ytotal,Ztotal,params.color);
155  else
156  p = line(Xtotal,Ytotal,Ztotal,'Color',params.color,'LineStyle',params.LineStyle);
157  end;
158  view(3);
159  otherwise
160  disp('plot for dimension>3 not yet implemented!')
161  return;
162 end;
163 
164 if params.axis_equal
165  axis equal;
166 end;
167 
168 end
169 
level
level(id) equals refinement level of element id
Definition: cubegrid.m:56
A hierarchical cubegrid of arbitrary dimension.
Definition: cubegrid.m:17