1 |
function CS=contoursurf(arg1,arg2,arg3,arg4); |
---|
2 |
|
---|
3 |
% CONTOURSURF contouring over non-rectangular surface. |
---|
4 |
% |
---|
5 |
% This is an extension of contourc. |
---|
6 |
% CONTOURSURF calculates the contour matrix C for use by EXTCONTOUR |
---|
7 |
% to draw the actual contour plot. |
---|
8 |
% C = CONTOURSURF(Z) computes the contour matrix for a contour plot |
---|
9 |
% of matrix Z treating the values in Z as heights above a plane. |
---|
10 |
% C = CONTOURSURF(X,Y,Z), where X and Y are vectors, specifies the X- |
---|
11 |
% and Y-axes for the contour computation. X and Y can also be matrices of |
---|
12 |
% the same size as Z, in which case they specify a surface in an |
---|
13 |
% identical manner as SURFACE. |
---|
14 |
% CONTOURSURF(Z,N) and CONTOURSURF(X,Y,Z,N) compute N contour lines, |
---|
15 |
% overriding the default automatic value. |
---|
16 |
% CONTOURSURF(Z,V) and CONTOURSURF(X,Y,Z,V) compute LENGTH(V) contour |
---|
17 |
% lines at the values specified in vector V. |
---|
18 |
% CONTOURSURF([X Y],N,Z) contours the triangular mesh specified by |
---|
19 |
% node location vectors X,Y with height vector Z, where N is a 3xn matrix |
---|
20 |
% containing the node indices for n triangles. |
---|
21 |
% |
---|
22 |
% The contour matrix C is a two row matrix of contour lines. Each |
---|
23 |
% contiguous drawing segment contains the value of the contour, |
---|
24 |
% the number of (x,y) drawing pairs, and the pairs themselves. |
---|
25 |
% The segments are appended end-to-end as |
---|
26 |
% |
---|
27 |
% C = [level1 x1 x2 x3 ... level2 x2 x2 x3 ...; |
---|
28 |
% pairs1 y1 y2 y3 ... pairs2 y2 y2 y3 ...] |
---|
29 |
% |
---|
30 |
% See also EXTCONTOUR. |
---|
31 |
|
---|
32 |
% Author: R. Pawlowicz (IOS) rich@ios.bc.ca |
---|
33 |
% 12/12/94 |
---|
34 |
|
---|
35 |
|
---|
36 |
|
---|
37 |
if (nargin <=2 ), |
---|
38 |
numarg_for_call='arg1'; |
---|
39 |
for ii=2:nargin, |
---|
40 |
numarg_for_call=[numarg_for_call ',arg' int2str(ii)]; |
---|
41 |
end; |
---|
42 |
zz=arg1; |
---|
43 |
else |
---|
44 |
if min(size(arg3))==1 & any(size(arg2)==3), % fall-through for triangular mesh |
---|
45 |
if (nargin==3), CS=contourtri(arg1,arg3,arg2); |
---|
46 |
else CS=contourtri(arg1,arg3,arg2,arg4); |
---|
47 |
end; |
---|
48 |
return; |
---|
49 |
end; |
---|
50 |
numarg_for_call='arg3'; |
---|
51 |
for ii=4:nargin, |
---|
52 |
numarg_for_call=[numarg_for_call ',arg' int2str(ii)]; |
---|
53 |
end; |
---|
54 |
zz=arg3; |
---|
55 |
end; |
---|
56 |
|
---|
57 |
eval(['CS=contourc(' numarg_for_call ');']); |
---|
58 |
[Ny,Nx]=size(zz); |
---|
59 |
|
---|
60 |
% Find data values and check curve orientation. |
---|
61 |
|
---|
62 |
ii=ones(1,size(CS,2)); |
---|
63 |
|
---|
64 |
if size(CS,2)==0, return; end; |
---|
65 |
|
---|
66 |
k=1; |
---|
67 |
while (k < size(CS,2)), |
---|
68 |
nl=CS(2,k); |
---|
69 |
|
---|
70 |
% Now this is a little bit of magic needed to make the filled contours |
---|
71 |
% work. Essentially I draw the *closed* contours so that the "high" side is |
---|
72 |
% always on the right. To test this, I take the cross product of the |
---|
73 |
% first vector with a vector to a corner point and test the sign |
---|
74 |
% against the elevation change. There are several special cases: |
---|
75 |
% (1) If the contour line goes through a point (which happen when -Infs |
---|
76 |
% are around), and (2) when the contour level equals the level on the high |
---|
77 |
% side (this always seems to happen in 'simple test' cases!). We take |
---|
78 |
% care of (1) by choosing other points, and we take care of (2) by adding |
---|
79 |
% eps to the data before comparing with the contour data. |
---|
80 |
|
---|
81 |
if ( CS(:,k+1)==CS(:,k+nl) & nl>1 ), |
---|
82 |
lev=CS(1,k); |
---|
83 |
x1=CS(1,k+1); y1=CS(2,k+1); |
---|
84 |
x2=CS(1,k+2); y2=CS(2,k+2); |
---|
85 |
vx1=x2-x1; vy1=y2-y1; |
---|
86 |
cpx=round(x1); cpy=round(y1); |
---|
87 |
if ( [cpx cpy]==[x1 y1] ), |
---|
88 |
cpx=round(x2); cpy=round(y2); |
---|
89 |
if ( [cpx cpy]==[x2 y2]), |
---|
90 |
if ( ~([cpx cpy]==round([x1 y1])) ), |
---|
91 |
cpx=round(x1); |
---|
92 |
else |
---|
93 |
cpx=round(x1)+y2-y1; |
---|
94 |
cpy=round(y1)-x2+x1; |
---|
95 |
end; |
---|
96 |
end; |
---|
97 |
end; |
---|
98 |
vx2=cpx-x1; vy2=cpy-y1; |
---|
99 |
% if (sign(zz(cpy,cpx)-lev+epslev)==0) disp('lev=0'); end; |
---|
100 |
% if (sign(vx1*vy2-vx2*vy1)==0) disp('cross=0'); end; |
---|
101 |
if ( sign(zz(cpy,cpx)-lev+eps) == sign(vx1*vy2-vx2*vy1) ), |
---|
102 |
CS(:,k+[1:nl])=fliplr(CS(:,k+[1:nl])); |
---|
103 |
end; |
---|
104 |
end; |
---|
105 |
ii(k)=0; |
---|
106 |
k=k+1+nl; |
---|
107 |
end; |
---|
108 |
|
---|
109 |
% Data from integer coords to data coords. There are 3 cases |
---|
110 |
% (1) Matrix X/Y |
---|
111 |
% (2) Vector X/Y |
---|
112 |
% (3) no X/Y. (do nothing); |
---|
113 |
|
---|
114 |
if (nargin>2 & min(size(arg1))>1 ), |
---|
115 |
|
---|
116 |
X=CS(1,ii)'; Y=CS(2,ii)'; |
---|
117 |
cX=ceil(X); fX=floor(X); |
---|
118 |
cY=ceil(Y); fY=floor(Y); |
---|
119 |
|
---|
120 |
Ibl=cY+(fX-1)*Ny; Itl=fY+(fX-1)*Ny; |
---|
121 |
Itr=fY+(cX-1)*Ny; Ibr=cY+(cX-1)*Ny; |
---|
122 |
|
---|
123 |
dy=cY-Y; dx=X-fX; |
---|
124 |
|
---|
125 |
% Correct for possible conflicts in matlabs [1 1 1 ] indexing. This |
---|
126 |
% probably will *never* happen in real life, but turns up annoyingly |
---|
127 |
% often in "simple" test cases. |
---|
128 |
if (Nx*Ny == length(Ibl) ), |
---|
129 |
Ibl=[1;Ibl]; Itl=[1;Itl]; |
---|
130 |
Itr=[1;Itr]; Ibr=[1;Ibr]; |
---|
131 |
dx=[0;dx]; dy=[0;dy]; |
---|
132 |
Csave=CS(:,1); |
---|
133 |
ii(1)=1; |
---|
134 |
end; |
---|
135 |
|
---|
136 |
CS(1,(ii)) = [ arg1(Ibl).*(1-dx).*(1-dy) + arg1(Itl).*(1-dx).*dy ... |
---|
137 |
+ arg1(Itr).*dx.*dy + arg1(Ibr).*dx.*(1-dy)]'; |
---|
138 |
CS(2,(ii)) = [ arg2(Ibl).*(1-dx).*(1-dy) + arg2(Itl).*(1-dx).*dy ... |
---|
139 |
+ arg2(Itr).*dx.*dy + arg2(Ibr).*dx.*(1-dy) ]'; |
---|
140 |
|
---|
141 |
if (exist('Csave')), |
---|
142 |
CS(:,1)=Csave; |
---|
143 |
end; |
---|
144 |
|
---|
145 |
elseif (nargin>2 & min(size(arg1))==1 ), |
---|
146 |
X=CS(1,ii); Y=CS(2,ii); |
---|
147 |
cX=ceil(X); fX=floor(X); |
---|
148 |
cY=ceil(Y); fY=floor(Y); |
---|
149 |
|
---|
150 |
dy=cY-Y; dx=X-fX; |
---|
151 |
|
---|
152 |
if (size(arg1,2)==1), |
---|
153 |
CS(1,ii)=[arg1(fX)'.*(1-dx)+arg1(cX)'.*dx]; |
---|
154 |
else |
---|
155 |
CS(1,ii)=[arg1(fX).*(1-dx)+arg1(cX).*dx]; |
---|
156 |
end; |
---|
157 |
|
---|
158 |
if (size(arg2,2)==1), |
---|
159 |
CS(2,ii)=[arg2(fY)'.*dy+arg2(cY)'.*(1-dy)]; |
---|
160 |
else |
---|
161 |
CS(2,ii)=[arg2(fY).*dy+arg2(cY).*(1-dy)]; |
---|
162 |
end; |
---|
163 |
|
---|
164 |
end; |
---|
165 |
|
---|
166 |
|
---|
167 |
|
---|