source: issm/trunk/src/m/model/plot/plot_contour.m@ 8298

Last change on this file since 8298 was 8298, checked in by seroussi, 14 years ago

changed grid to node in matlab

File size: 6.3 KB
Line 
1function plot_contour(md,datain,options);
2%PLOT_CONTOUR - plot contours of a given field
3%
4% Usage:
5% plot_contour(md,data,options);
6%
7% See also: PLOTMODEL
8
9%process data and model
10[x y z index is2d]=processmesh(md,[],options);
11[data datatype]=processdata(md,datain,options);
12
13%check is2d
14if ~is2d,
15 error('plot_contour error message: contour not supported for 3d meshes, project on a layer');
16end
17
18%first, process data: must be on nodes
19if datatype==1,
20 %elements -> take average
21 data=averaging(md,data,0);
22elseif datatype==2,
23 %nodes -> do nothing
24elseif datatype==3,
25 %quiver -> take norm
26 data=sqrt(sum(datain.*datain,2));
27else
28 error('datatype not supported yet');
29end
30
31%prepare colors
32if exist(options,'contouronly')
33 %remove the previous plots
34 cla
35end
36color=getfieldvalue(options,'contourcolor','y');
37linewidth=getfieldvalue(options,'linewidth',1);
38
39%get contours levels
40contourlevels=getfieldvalue(options,'contourlevels');
41if isnumeric(contourlevels),
42 levels=round_ice(linspace(max(data),min(data),contourlevels),2);
43else
44 levels=[];
45 for i=1:length(contourlevels)
46 levels(end+1)=contourlevels{i};
47 end
48 levels=sort(unique(levels),'descend');
49end
50numlevels=length(levels);
51
52%initialization of some variables
53numberofelements=size(index,1);
54elementslist=1:numberofelements;
55c=[];
56h=[];
57
58%get unique edges in mesh
59%1: list of edges
60edges=[index(:,[1,2]); index(:,[2,3]); index(:,[3,1])];
61%2: find unique edges
62[edges,I,J]=unique(sort(edges,2),'rows');
63%3: unique edge numbers
64vec=J;
65%4: unique edges numbers in each triangle (2 triangles sharing the same edge will have
66% the same edge number)
67edges_tria=[vec(elementslist), vec(elementslist+numberofelements), vec(elementslist+2*numberofelements)];
68
69%segments [nodes1 nodes2]
70Seg1=index(:,[1 2]);
71Seg2=index(:,[2 3]);
72Seg3=index(:,[3 1]);
73
74%segment numbers [1;4;6;...]
75Seg1_num=edges_tria(:,1);
76Seg2_num=edges_tria(:,2);
77Seg3_num=edges_tria(:,3);
78
79%value of data on each tips of the segments
80Data1=data(Seg1);
81Data2=data(Seg2);
82Data3=data(Seg3);
83
84%get the ranges for each segment
85Range1=sort(Data1,2);
86Range2=sort(Data2,2);
87Range3=sort(Data3,2);
88
89for i=1:numlevels
90
91 level=levels(i);
92
93 %find the segments that contain this value
94 pos1=(Range1(:,1)<level & Range1(:,2)>level);
95 pos2=(Range2(:,1)<level & Range2(:,2)>level);
96 pos3=(Range3(:,1)<level & Range3(:,2)>level);
97
98 %get elements
99 poselem12=(pos1 & pos2);
100 poselem13=(pos1 & pos3);
101 poselem23=(pos2 & pos3);
102 poselem=find(poselem12 | poselem13 | poselem23);
103 numelems=length(poselem);
104
105 %if no element has been flagged, skip to the next level
106 if numelems==0,
107 continue,
108 end
109
110 %go through the elements and build the coordinates for each segment (1 by element)
111 x1=zeros(numelems,1);
112 x2=zeros(numelems,1);
113 y1=zeros(numelems,1);
114 y2=zeros(numelems,1);
115 edge_l=zeros(numelems,2);
116
117 for j=1:numelems,
118
119 weight1=(level-Data1(poselem(j),1))/(Data1(poselem(j),2)-Data1(poselem(j),1));
120 weight2=(level-Data2(poselem(j),1))/(Data2(poselem(j),2)-Data2(poselem(j),1));
121 weight3=(level-Data3(poselem(j),1))/(Data3(poselem(j),2)-Data3(poselem(j),1));
122
123 if poselem12(poselem(j));
124
125 x1(j)=x(Seg1(poselem(j),1))+weight1*(x(Seg1(poselem(j),2))-x(Seg1(poselem(j),1)));
126 x2(j)=x(Seg2(poselem(j),1))+weight2*(x(Seg2(poselem(j),2))-x(Seg2(poselem(j),1)));
127 y1(j)=y(Seg1(poselem(j),1))+weight1*(y(Seg1(poselem(j),2))-y(Seg1(poselem(j),1)));
128 y2(j)=y(Seg2(poselem(j),1))+weight2*(y(Seg2(poselem(j),2))-y(Seg2(poselem(j),1)));
129 edge_l(j,1)=Seg1_num(poselem(j));
130 edge_l(j,2)=Seg2_num(poselem(j));
131
132 elseif poselem13(poselem(j)),
133
134 x1(j)=x(Seg1(poselem(j),1))+weight1*(x(Seg1(poselem(j),2))-x(Seg1(poselem(j),1)));
135 x2(j)=x(Seg3(poselem(j),1))+weight3*(x(Seg3(poselem(j),2))-x(Seg3(poselem(j),1)));
136 y1(j)=y(Seg1(poselem(j),1))+weight1*(y(Seg1(poselem(j),2))-y(Seg1(poselem(j),1)));
137 y2(j)=y(Seg3(poselem(j),1))+weight3*(y(Seg3(poselem(j),2))-y(Seg3(poselem(j),1)));
138 edge_l(j,1)=Seg1_num(poselem(j));
139 edge_l(j,2)=Seg3_num(poselem(j));
140
141 elseif poselem23(poselem(j)),
142
143 x1(j)=x(Seg2(poselem(j),1))+weight2*(x(Seg2(poselem(j),2))-x(Seg2(poselem(j),1)));
144 x2(j)=x(Seg3(poselem(j),1))+weight3*(x(Seg3(poselem(j),2))-x(Seg3(poselem(j),1)));
145 y1(j)=y(Seg2(poselem(j),1))+weight2*(y(Seg2(poselem(j),2))-y(Seg2(poselem(j),1)));
146 y2(j)=y(Seg3(poselem(j),1))+weight3*(y(Seg3(poselem(j),2))-y(Seg3(poselem(j),1)));
147 edge_l(j,1)=Seg2_num(poselem(j));
148 edge_l(j,2)=Seg3_num(poselem(j));
149 else
150 %it shoud not go here
151 end
152 end
153
154 %now that we have the segments, we must try to connect them...
155
156 %loop over the subcontours
157 while ~isempty(edge_l),
158
159 %take the right edge of the second segment and connect it to the next segments if any
160 e1=edge_l(1,1); e2=edge_l(1,2);
161 xc=[x1(1);x2(1)]; yc=[y1(1);y2(1)];
162
163 %erase the lines corresponding to this edge
164 edge_l(1,:)=[];
165 x1(1)=[]; x2(1)=[];
166 y1(1)=[]; y2(1)=[];
167
168 [ro1,co1]=find(edge_l==e1);
169
170 while ~isempty(ro1)
171
172 if co1==1,
173 xc=[x2(ro1);xc]; yc=[y2(ro1);yc];
174
175 %next edge:
176 e1=edge_l(ro1,2);
177
178 else
179 xc=[x1(ro1);xc]; yc=[y1(ro1);yc];
180
181 %next edge:
182 e1=edge_l(ro1,1);
183 end
184
185 %erase the lines of this
186 edge_l(ro1,:)=[];
187 x1(ro1)=[]; x2(ro1)=[];
188 y1(ro1)=[]; y2(ro1)=[];
189
190 %next connection
191 [ro1,co1]=find(edge_l==e1);
192 end
193
194 %same thing the other way (to the right)
195 [ro2,co2]=find(edge_l==e2);
196
197 while ~isempty(ro2)
198
199 if co2==1,
200 xc=[xc;x2(ro2)]; yc=[yc;y2(ro2)];
201
202 %next edge:
203 e2=edge_l(ro2,2);
204 else
205 xc=[xc;x1(ro2)]; yc=[yc;y1(ro2)];
206
207 %next edge:
208 e2=edge_l(ro2,1);
209 end
210
211 %erase the lines of this
212 edge_l(ro2,:)=[];
213 x1(ro2)=[]; x2(ro2)=[];
214 y1(ro2)=[]; y2(ro2)=[];
215
216 %next connection
217 [ro2,co2]=find(edge_l==e2);
218 end
219
220 %we now have one subcontour ready to be plotted
221 zc=level*ones(length(xc)+1,1);
222 if getfieldvalue(options,'contouronly',0),
223 h=[h;patch('Xdata',[xc;NaN],'Ydata',[yc;NaN],'Zdata',zc,'Cdata',zc,'facecolor','none','edgecolor','flat','linewidth',linewidth)];
224 hold on
225 else
226 h=[h;patch('Xdata',[xc;NaN],'Ydata',[yc;NaN],'facecolor','none','edgecolor',color,'linewidth',linewidth)];
227 hold on
228 end
229
230 % Update the CS data structure as per "contours.m"
231 % so that clabel works
232 c = horzcat(c,[level, xc'; length(xc), yc']);
233
234 end
235end
236
237%labels?
238if (~strcmpi(getfieldvalue(options,'contourticks','on'),'off') & ~isempty(c) & ~isempty(h))
239 if exist(options,'contouronly')
240 clabel(c,h);
241 else
242 clabel(c,h,'color',color,'FontSize',14);
243 end
244end
Note: See TracBrowser for help on using the repository browser.