1 |
function outmat=sample_field_2d(fem_grid_struct,nx,ny,Q,box_coords) |
---|
2 |
%SAMPLE_FIELD_2D Mouse-driven 2-D FEM Field Sampler |
---|
3 |
% SAMPLE_FIELD_2D is a mouse-driven 2-D field sampler prompts |
---|
4 |
% the user to draw a box on the current figure, and |
---|
5 |
% returns an array of coordinates and interpolated |
---|
6 |
% field values. The input field array can be any number |
---|
7 |
% of columns wide (scalar, vector, etc.) but must be as |
---|
8 |
% long as the node coordinate lists in fem_grid_struct. |
---|
9 |
% I.e., the fields to interpolate will usually come from |
---|
10 |
% the FEM domain itself. |
---|
11 |
% |
---|
12 |
% INPUT: fem_grid_struct - FEM domain structure on which the field |
---|
13 |
% to sample is related (from LOADGRID, see FEM_GRID_STRUCT). |
---|
14 |
% nx,ny - number of points in the x and y direction at which to |
---|
15 |
% sample the fields Q. (REQUIRED, INTEGERS) |
---|
16 |
% Q - the fields to sample. This array must be the same length as |
---|
17 |
% the node coordinate arrays in the valid fem_grid_struct |
---|
18 |
% passed in, but the number of columns is arbitrary. |
---|
19 |
% |
---|
20 |
% box_coords - optional 4x1 vector defining the sample region. |
---|
21 |
% The 4 values are [X1 Y1 X2 Y2], and if defined |
---|
22 |
% they take precedence over the mouse-driven facility. |
---|
23 |
% |
---|
24 |
% OUTPUT: S - an array containing the sample points and sample values. |
---|
25 |
% If nx or ny==1, then S contains the sample points and |
---|
26 |
% sample values in columns. Otherwise, S is an |
---|
27 |
% [nx X ny X ncol] array, where ncol is the number of |
---|
28 |
% columns in the input array Q and the first 2 levels of |
---|
29 |
% S are the X and Y coordinates (X=S(:,:,1), Y=S(:,:,2)). |
---|
30 |
% Points not located within FEM domain are assigned NaN's |
---|
31 |
% as field values. |
---|
32 |
% |
---|
33 |
% To contour the results in the case where nx and ny > 1, |
---|
34 |
% call MATLAB's contourf routine with S appropriately |
---|
35 |
% sampled. For example: |
---|
36 |
% >> contourf(S(:,:,1),S(:,:,2),S(:,:,3)) |
---|
37 |
% |
---|
38 |
% CALL: S=sample_field_2d(fem_grid_struct,nx,ny,Q) |
---|
39 |
% |
---|
40 |
% Written by Brian O. Blanton |
---|
41 |
% Summer 1998 |
---|
42 |
% |
---|
43 |
|
---|
44 |
% Process incoming arguments |
---|
45 |
if nargin~=4 & nargin~=5 |
---|
46 |
error('Incorrect number of argument to SAMPLE_FIELD_2D.'); |
---|
47 |
end |
---|
48 |
|
---|
49 |
% Make sure first argument is a valid fem_grid_struct |
---|
50 |
if ~is_valid_struct(fem_grid_struct) |
---|
51 |
error(' fem_grid_struct to SAMPLE_FIELD_2D invalid.') |
---|
52 |
end |
---|
53 |
|
---|
54 |
if (~isint(nx)|~isint(ny))&(nx~=0|ny~=0) |
---|
55 |
error('2nd and 3rd arguments to SAMPLE_FIELD_2D MUST be non-0 integers.'); |
---|
56 |
end |
---|
57 |
|
---|
58 |
% 4th arg must me a vector with same length as fem_grid_struct.x |
---|
59 |
[nrow,ncol]=size(Q); |
---|
60 |
if nrow~=length(fem_grid_struct.x) |
---|
61 |
error('Input field to SAMPLE_FIELD_2D MUST be same length as fem_grid_struct.x'); |
---|
62 |
end |
---|
63 |
|
---|
64 |
% An optional 5th argument must be 4x1 or 1x4 |
---|
65 |
if nargin==5 |
---|
66 |
[m,n]=size(box_coords); |
---|
67 |
if (m*n)~=4 |
---|
68 |
error('Box_Coords must be 4x1 or 1x4 in SAMPLE_FIELD_2D.') |
---|
69 |
end |
---|
70 |
if m~=1&n~=1 |
---|
71 |
error('Box_Coords must be 4x1 or 1x4 in SAMPLE_FIELD_2D.') |
---|
72 |
end |
---|
73 |
% Further box coord checks?? |
---|
74 |
end |
---|
75 |
|
---|
76 |
|
---|
77 |
% Delete previously drawn objects grids |
---|
78 |
delete(findobj(gca,'Tag','Sample_Field_Box')) |
---|
79 |
delete(findobj(gca,'Tag','Sample_Field_Points')) |
---|
80 |
|
---|
81 |
currfig=gcf; |
---|
82 |
figure(currfig); |
---|
83 |
|
---|
84 |
% Get Grid dimensions |
---|
85 |
if ~exist('box_coords') |
---|
86 |
disp('Click and drag mouse to define sample points'); |
---|
87 |
waitforbuttonpress; |
---|
88 |
Pt1=get(gca,'CurrentPoint'); |
---|
89 |
rbbox([get(gcf,'CurrentPoint') 0 0],get(gcf,'CurrentPoint')); |
---|
90 |
Pt2=get(gca,'CurrentPoint'); |
---|
91 |
curraxes=gca; |
---|
92 |
else |
---|
93 |
Pt1=[box_coords(1) box_coords(2) NaN; |
---|
94 |
box_coords(1) box_coords(2) NaN]; |
---|
95 |
Pt2=[box_coords(3) box_coords(4) NaN; |
---|
96 |
box_coords(3) box_coords(4) NaN]; |
---|
97 |
end |
---|
98 |
|
---|
99 |
% Draw box around sampling grid |
---|
100 |
line([Pt1(1) Pt2(1) Pt2(1) Pt1(1) Pt1(1)],... |
---|
101 |
[Pt1(3) Pt1(3) Pt2(3) Pt2(3) Pt1(3)],'Tag','Sample_Field_Box') |
---|
102 |
|
---|
103 |
xstart=Pt1(1); |
---|
104 |
ystart=Pt1(3); |
---|
105 |
xend=Pt2(1); |
---|
106 |
yend=Pt2(3); |
---|
107 |
|
---|
108 |
% If nx or ny==1, then the output is a diagonal line |
---|
109 |
% from (xstart,ystart) to (xend,yend) |
---|
110 |
|
---|
111 |
if nx==1 | ny==1 |
---|
112 |
n=max(nx,ny); |
---|
113 |
X=linspace(xstart,xend,n); |
---|
114 |
Y=linspace(ystart,yend,n); |
---|
115 |
else |
---|
116 |
x=linspace(xstart,xend,nx); |
---|
117 |
y=linspace(ystart,yend,ny); |
---|
118 |
X=x(:)*(ones(size(y(:)'))); |
---|
119 |
X=X'; |
---|
120 |
Y=y(:)*(ones(size(x(:)'))); |
---|
121 |
end |
---|
122 |
|
---|
123 |
line(X,Y,'Marker','.','MarkerSize',14,'Color','r',... |
---|
124 |
'LineStyle','none','Tag','Sample_Field_Points') |
---|
125 |
|
---|
126 |
% Call interp_scalar to do the work; once for each column of Q |
---|
127 |
S=NaN*ones(length(X(:)),ncol); |
---|
128 |
for i=1:ncol |
---|
129 |
S(:,i)=interp_scalar(fem_grid_struct,Q(:,i),X(:),Y(:)); |
---|
130 |
end |
---|
131 |
|
---|
132 |
if nx==1 | ny==1 |
---|
133 |
outmat=[X(:) Y(:) S]; |
---|
134 |
else |
---|
135 |
outmat=NaN*ones(ny,nx,ncol); |
---|
136 |
outmat(:,:,1)=X; |
---|
137 |
outmat(:,:,2)=Y; |
---|
138 |
outmat(:,:,3:ncol+2)=reshape(S,ny,nx,ncol); |
---|
139 |
end |
---|
140 |
% |
---|
141 |
% Brian O. Blanton |
---|
142 |
% Department of Marine Sciences |
---|
143 |
% 15-1A Venable Hall |
---|
144 |
% CB# 3300 |
---|
145 |
% Uni. of North Carolina |
---|
146 |
% Chapel Hill, NC |
---|
147 |
% 27599-3300 |
---|
148 |
% |
---|
149 |
% 919-962-4466 |
---|
150 |
% blanton@marine.unc.edu |
---|
151 |
% Summer 1998 |
---|