-
Notifications
You must be signed in to change notification settings - Fork 0
/
wasg.m
581 lines (565 loc) · 21.7 KB
/
wasg.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
function WASSGS
% CUED Wing Analysis Surface Generator
% RGM - developed for SA1 2020
% based on 'ManipulateData.m' written by Lindo Ouseph.
%
% Manipulate an aerofoil section with the trailing edge fixed at
% (x,y)=(1,0) interactively. The aerofoil shape is constructed using
% splines based on a list of nodes.
%
% WASG is designed to be run on your main SA1 folder, one level above
% the 'Geometry' folder.
%
% List of functions:
% left mouse click - select a node, or create a new one if clicking on
% the aerofoil between points.
% click and drag left mouse button - move the selected point.
% arrow keys - move the selected point at a rate 'delta'.
% right mouse click - delete the node clicked on.
% u - undo last action (up to 20).
% r - redo, cancel last undo.
% z - zoom, create a secondary magnified figure, centred on the last
% cursor position, that the control is
% temporarily transferred to. Pressing z again closes the second
% figure and returns control to the primary one. Functions 'l', 's',
% 'b', and 't' are not available while in zoom mode.
% l - load aerofoil from a '.surf' file.
% s - save aerofoil into a '.surf' file.
% b - back up aerofoil layout into a memory buffer.
% t - restore aerofoil from backup 'b'.
% d - delta, reset the rate of displacement of nodes when using the arrow
% keys.
clear, close all
b=0;p=0;
delta=1e-4;
max_undo = 20;
xmax = 1.0;
xmin = 0.0;
ymax = .2;
ymin = -.2;
pathin=[pwd,'/Geometry/'];
[filein,pathin]=uigetfile([pathin '*.surf']);
y=load([pathin,filein]);
x=y(2:end-1,1);
y=y(2:end-1,2);
L=length(x);
I=(x-1).^2+y.^2; I=find(I==max(I)); I=I(1);
Xbk=x;
Ybk=y;hZ=[];
axisZ=[];
aZ=[];
deltaZ=.2*delta;
deltxt=8e-3;
deltxtZ=1e-3;
Xbk=x;Ybk=y;Lmax=L;
xundo=[];yundo=[];
Lundo=[];Iundo=[];
xredo=[];yredo=[];
Lredo=[];Iredo=[];
Res=get(0); Res=Res.ScreenSize;
h=figure('units','normalized',...
'outerposition',[.05 .2 1.35*Res(4)/Res(3) .6],...
'DockControls','off',...
'MenuBar','none',...
'name','Wing Analysis Section Generator (Matlab version)',...
'NumberTitle','off',...
'WindowKeyPressFcn',@Key,...
'windowbuttondownfcn',@Down,...
'WindowButtonMotionFcn',@Move,...
'WindowButtonUpFcn',@Up,...
'DeleteFcn',@figDelete);
a=axes('position',[.10,.10,.87,.87]);
[xs ys] = splinefit([1;x;1],[0;y;0],0);
plot(xs,ys,'k', ...
[1;x],[0;y],'.k', ...
'markersize',13,'markerfacecolor','k');
axis equal
axis([xmin xmax ymin ymax])
uicontrol('style','text','Fontsize',10, ...
'position',[1 190 80 150],...
'string',{'u-undo';'r-redo';'z-zoom';'l-load';'s-save'; ...
'b-back up';'t-restore';'d-delta'},...
'foregroundcolor','k');
%%% @Down - what happens when a mouse button is pressed
function Down(varargin);
xundo=[[x;0*(length(x)+1:Lmax)'],xundo(:,1:min([size(xundo,2),max_undo-1]))];
yundo=[[y;0*(length(y)+1:Lmax)'],yundo(:,1:min([size(yundo,2),max_undo-1]))];
Lundo=[L,Lundo(1:min([length(Lundo),max_undo-1]))];
Iundo=[I,Iundo(1:min([length(Iundo),max_undo-1]))];
p=get(a,'currentpoint');
[V2 I]=min((x-p(1)).^2+(y-p(3)).^2);
xx=x;
yy=y;
xx(I)=[];
yy(I)=[];
mindist2=min((xx-x(I)).^2+(yy-y(I)).^2);
switch varargin{1}.SelectionType
%%% Mouse left-click:
case 'normal'
%%% Add a knot if left-click is close to aerofoil contour:
if V2>min(1e-4,mindist2)
[xs ys] = splinefit([1;x;1],[0;y;0],1);
ppk = 100; % needs to be the same in 'splinefit.m'
[V2 Is]=min((xs-p(1)).^2+(ys-p(3)).^2);
if V2<1e-4
xI=xs(Is);yI=ys(Is);
I = ceil(Is/ppk);
L=L+1;
Lmax=max([L,Lmax]);
x(I+1:L)=x(I:end);
y(I+1:L)=y(I:end);
x(I)=xI;
y(I)=yI;
xundo=[xundo;0*(size(xundo,1)+1:Lmax)'*(1:size(xundo,2))];
yundo=[yundo;0*(size(yundo,1)+1:Lmax)'*(1:size(yundo,2))];
xredo=[];
yredo=[];
Lredo=[];
Iredo=[];
b=1;
[xs ys] = splinefit([1;x;1],[0;y;0],0);
try delete(t);end;
plot(xs,ys,'k', ...
[1;x],[0;y],'.k', ...
x(I),y(I),'xk','markersize',13)
axis equal
axis([0 1 -.2 .2])
t=text(x(I)+deltxt,y(I)-deltxt, ...
['(',num2str(x(I),'%8.4f'),',',num2str(y(I),'%8.4f'),')'], ...
'color','k','fontweight','bold');
drawnow
end
%%% Select a knot if left-click is close to it:
else
b=1;
%[xs ys] = splinefit([1;x;1],[0;y;0],0);%redundant?
try delete(t);end;
plot(xs,ys,'k', ...
[1;x],[0;y],'.k', ...
x(I),y(I),'xk','markersize',13)
axis equal
axis([0 1 -.2 .2])
t=text(x(I)+deltxt,y(I)-deltxt, ...
['(',num2str(x(I),'%8.4f'),',',num2str(y(I),'%8.4f'),')'], ...
'color','k','fontweight','bold');
drawnow
end
%%% Mouse right click:
case 'alt'
%%% Delete a knot if right-click is close to it:
if V2<min([1e-4,mindist2])
L=L-1;
x=xx;
y=yy;
xredo=[];
yredo=[];
Lredo=[];
Iredo=[];
[xs ys] = splinefit([1;x;1],[0;y;0],0);
try delete(t);end;
plot(xs,ys,'k', ...
[1;x],[0;y],'.k','markersize',13)
axis equal
axis([0 1 -.2 .2])
drawnow
end
end
end
%%% @Up - what happens when a mouse button is released
function Up(varargin);
b=0;
end
%%% @Move - what happens when the mouse moves
%%% (while holding the left button clicked for b=true)
function Move(varargin)
if b
p=get(a,'currentpoint');
x(I)=max([min([p(1),xmax]),xmin]);
y(I)=max([min([p(3),ymax]),ymin]);
xredo=[];
yredo=[];
[xs ys] = splinefit([1;x;1],[0;y;0],0);
try delete(t);end;
plot(xs,ys,'k', ...
[1;x],[0;y],'.k', ...
x(I),y(I),'xk','markersize',13)
axis equal
axis([0 1 -.2 .2])
t=text(x(I)+deltxt,y(I)-deltxt, ...
['(',num2str(x(I),'%8.4f'),',',num2str(y(I),'%8.4f'),')'], ...
'color','k','fontweight','bold');
drawnow
end
end
%%% @Key - the various keyboard options
function Key(varargin);
if ~delta;delta=mean(abs(diff(y)));end
% to be done: if varargin{2}.Modifier=='control'
switch varargin{2}.Key
%%% 'l' load a new .surf file
case 'l'
[filein,pathin]=uigetfile([pathin '*.surf']);
y=load([pathin,filein]);
x=y(2:end-1,1);
y=y(2:end-1,2);
L=length(x);
I=(x-1).^2+y.^2; I=find(I==max(I)); I=I(1);
Xbk=x;
Ybk=y;
%%% 's' save into a .surf file:
case 's'
dataout=[[1;x;1],[0;y;0]];
[fileout,pathout]=uiputfile([pathin '*.surf']);
save([pathout fileout],'dataout','-ascii')
%%% 'b' backup configuration
case 'b'
Xbk=x;
Ybk=y;
disp('knot position backed up')
%%% 't' restore last backup
case 't'
x=Xbk;
y=Ybk;
L=length(x);
I=1;
disp('knot position restored')
%%% 'u' undo last action
case 'u'
if isempty(xundo)
disp('undo max reached')
else
Lredo=[L,Lredo(1:min([length(Lredo),max_undo-1]))];
Iredo=[I,Iredo(1:min([length(Iredo),max_undo-1]))];
xredo=[[x;0*(length(x)+1:Lmax)'],xredo(:,1:min([size(xredo,2),max_undo-1]))];
yredo=[[y;0*(length(y)+1:Lmax)'],yredo(:,1:min([size(yredo,2),max_undo-1]))];
L=Lundo(1);
I=Iundo(1);
x=xundo(1:L,1);
y=yundo(1:L,1);
Lundo=Lundo(2:end);
Iundo=Iundo(2:end);
xundo=xundo(:,2:end);
yundo=yundo(:,2:end);
end
%%% 'r' redo - cancel last undo
case 'r'
if isempty(xredo)
disp('last action reached')
else
Lundo=[L,Lundo(1:min([length(Lundo),max_undo-1]))];
Iundo=[I,Iundo(1:min([length(Iundo),max_undo-1]))];
xundo=[[x;0*(length(x)+1:Lmax)'],xundo(:,1:min([size(xundo,2),max_undo-1]))];
yundo=[[y;0*(length(x)+1:Lmax)'],yundo(:,1:min([size(yundo,2),max_undo-1]))];
L=Lredo(1);
I=Iredo(1);
x=xredo(1:L,1);
y=yredo(1:L,1);
Lredo=Lredo(2:end);
Iredo=Iredo(2:end);
xredo=xredo(:,2:end);
yredo=yredo(:,2:end);
end
%%% 'z' zoomed view
case 'z'
p=get(a,'currentpoint');
axisZ=[p(1)-.05 p(1)+.05 p(3)-.03 p(3)+.03];
try delete(t);end;
set(h,'WindowKeyPressFcn',@emptyFunctionHandle,...
'windowbuttondownfcn',@emptyFunctionHandle,...
'WindowButtonMotionFcn',@emptyFunctionHandle,...
'WindowButtonUpFcn',@emptyFunctionHandle)
hZ=figure('units','normalized',...
'outerposition',[.3 .3 1.05*Res(4)/Res(3) .7],...
'DockControls','off',...
'MenuBar','none',...
'name','zoomed view',...
'NumberTitle','off',...
'WindowKeyPressFcn',@KeyZ,...
'windowbuttondownfcn',@DownZ,...
'WindowButtonMotionFcn',@MoveZ,...
'WindowButtonUpFcn',@UpZ,...
'DeleteFcn',@figDeleteZ);
aZ=axes('position',[.10,.10,.87,.87]);
plot(xs,ys,'k', ...
[1;x],[0;y],'.k', ...
x(I),y(I),'xk','markersize',13)
axis equal
axis(axisZ)
drawnow
%%% press 'd' to change the rate of displacement of a knot
%%% when using the arrow keys instead of holding left mouse
%%% button
case 'd'
delta=str2double(inputdlg('enter delta'));
deltaZ=.2*delta;
case 'leftarrow'
x(I)=max([x(I)-delta,xmin]);
case 'rightarrow'
x(I)=min([x(I)+delta,xmax]);
case 'downarrow'
y(I)=max([y(I)-delta,ymin]);
case 'uparrow'
y(I)=min([y(I)+delta,ymax]);
end
[xs ys] = splinefit([1;x;1],[0;y;0],0);
set(0, 'CurrentFigure', h)
try delete(t);end;
plot(xs,ys,'k', ...
[1;x],[0;y],'.k', ...
x(I),y(I),'xk','markersize',13)
axis equal
axis([0 1 -.2 .2])
t=text(x(I)+deltxt,y(I)-deltxt, ...
['(',num2str(x(I),'%8.4f'),',',num2str(y(I),'%8.4f'),')'], ...
'color','k','fontweight','bold');
drawnow
end
%%%%%%%%%%%%%% FUNCTIONS FOR ZOOMED FIGURE %%%%%%%%%%%%%%
%%% @Down - what happens when a mouse button is pressed
function DownZ(varargin);
xundo=[[x;0*(length(x)+1:Lmax)'],xundo(:,1:min([size(xundo,2),max_undo-1]))];
yundo=[[y;0*(length(y)+1:Lmax)'],yundo(:,1:min([size(yundo,2),max_undo-1]))];
Lundo=[L,Lundo(1:min([length(Lundo),max_undo-1]))];
Iundo=[I,Iundo(1:min([length(Iundo),max_undo-1]))];
p=get(aZ,'currentpoint');
[V2 I]=min((x-p(1)).^2+(y-p(3)).^2);
xx=x;
yy=y;
xx(I)=[];
yy(I)=[];
mindist2=min((xx-x(I)).^2+(yy-y(I)).^2);
switch varargin{1}.SelectionType
%%% Mouse left-click:
case 'normal'
%%% Add a knot if left-click is close to aerofoil contour:
if V2>min(1e-6,mindist2)
[xs ys] = splinefit([1;x;1],[0;y;0],1);
ppk = 100; % needs to be the same in 'splinefit.m'
[V2 Is]=min((xs-p(1)).^2+(ys-p(3)).^2);
if V2<1e-6
xI=xs(Is);yI=ys(Is);
I = ceil(Is/ppk);
L=L+1;
Lmax=max([L,Lmax]);
x(I+1:L)=x(I:end);
y(I+1:L)=y(I:end);
x(I)=xI;
y(I)=yI;
xundo=[xundo;0*(size(xundo,1)+1:Lmax)'*(1:size(xundo,2))];
yundo=[yundo;0*(size(yundo,1)+1:Lmax)'*(1:size(yundo,2))];
xredo=[];
yredo=[];
b=1;
[xs ys] = splinefit([1;x;1],[0;y;0],0);%redundant?
set(0, 'CurrentFigure', h)
plot(xs,ys,'k', ...
[1;x],[0;y],'.k', ...
x(I),y(I),'xk','markersize',13);
axis equal
axis([0 1 -.2 .2])
%drawnow
figure(hZ)
try delete(t);end;
plot(xs,ys,'k', ...
[1;x],[0;y],'.k', ...
x(I),y(I),'xk', ...
'markersize',13);
axis equal
axis(axisZ)
t=text(x(I)+deltxtZ,y(I)-deltxtZ, ...
['(',num2str(x(I),'%8.4f'),',',num2str(y(I),'%8.4f'),')'], ...
'color','k','fontweight','bold');
drawnow
end
%%% Select a knot if left-click is close to it:
else
b=1;
[xs ys] = splinefit([1;x;1],[0;y;0],0);%redundant?
set(0, 'CurrentFigure', h)
plot(xs,ys,'k', ...
[1;x],[0;y],'.k', ...
x(I),y(I),'xk', ...
'markersize',13);
axis equal
axis([0 1 -.2 .2])
%drawnow
figure(hZ)
try delete(t);end;
plot(xs,ys,'k', ...
[1;x],[0;y],'.k', ...
x(I),y(I),'xk', ...
'markersize',13);
axis equal
axis(axisZ)
t=text(x(I)+deltxtZ,y(I)-deltxtZ, ...
['(',num2str(x(I),'%8.4f'),',',num2str(y(I),'%8.4f'),')'], ...
'color','k','fontweight','bold');
drawnow
end
%%% Mouse right click:
case 'alt'
%%% Delete a knot if right-click is close to it:
if V2<min([1e-6,mindist2])
L=L-1;
x=xx;
y=yy;
xredo=[];
yredo=[];
[xs ys] = splinefit([1;x;1],[0;y;0],0);
set(0, 'CurrentFigure', h)
plot(xs,ys,'k', ...
[1;x],[0;y],'.k', ...
'markersize',13);
axis equal
axis([0 1 -.2 .2])
%drawnow
figure(hZ)
try delete(t);end;
plot(xs,ys,'k', ...
[1;x],[0;y],'.k', ...
'markersize',13);
axis equal
axis(axisZ)
drawnow
end
end
end
%%% @Up - what happens when a mouse button is released
function UpZ(varargin);
b=0;
end
%%% @Move - what happens when the mouse moves
%%% (while holding the left button clicked for b=true)
function MoveZ(varargin)
p=get(aZ,'currentpoint');
if b
x(I)=max([min([p(1),xmax]),xmin]);
y(I)=max([min([p(3),ymax]),ymin]);
xredo=[];
yredo=[];
[xs ys] = splinefit([1;x;1],[0;y;0],1);
set(0, 'CurrentFigure', h)
plot(xs,ys,'k', ...
[1;x],[0;y],'.k', ...
x(I),y(I),'xk','markersize',13);
axis equal
axis([0 1 -.2 .2])
%drawnow
set(0, 'CurrentFigure', hZ)
try delete(t);end;
plot(xs,ys,'k', ...
[1;x],[0;y],'.k', ...
x(I),y(I),'xk', ...
'markersize',13);
axis equal
axis(axisZ)
t=text(x(I)+deltxtZ,y(I)-deltxtZ, ...
['(',num2str(x(I),'%8.4f'),',',num2str(y(I),'%8.4f'),')'], ...
'color','k','fontweight','bold');
drawnow
end
end
%%% @Key - the various keyboard options
function KeyZ(varargin);
if ~delta;delta=mean(abs(diff(y)));end
% to be done: if varargin{2}.Modifier=='control'
switch varargin{2}.Key
%%% 'u' undo last action
case 'u'
if isempty(xundo)
disp('undo max reached')
else
Lredo=[L,Lredo(1:min([length(Lredo),max_undo-1]))];
Iredo=[I,Iredo(1:min([length(Iredo),max_undo-1]))];
xredo=[[x;0*(length(x)+1:Lmax)'],xredo(:,1:min([size(xredo,2),max_undo-1]))];
yredo=[[y;0*(length(y)+1:Lmax)'],yredo(:,1:min([size(yredo,2),max_undo-1]))];
L=Lundo(1);
I=Iundo(min([length(Iundo),2]));
x=xundo(1:L,1);
y=yundo(1:L,1);
Lundo=Lundo(2:end);
Iundo=Iundo(2:end);
xundo=xundo(:,2:end);
yundo=yundo(:,2:end);
end
%%% 'r' redo - cancel last undo
case 'r'
if isempty(xredo)
disp('last action reached')
else
Lundo=[L,Lundo(1:min([length(Lundo),max_undo-1]))];
Iundo=[I,Iundo(1:min([length(Iundo),max_undo-1]))];
xundo=[[x;0*(length(x)+1:Lmax)'],xundo(:,1:min([size(xundo,2),max_undo-1]))];
yundo=[[y;0*(length(x)+1:Lmax)'],yundo(:,1:min([size(yundo,2),max_undo-1]))];
L=Lredo(1);
I=Iredo(1);
x=xredo(1:L,1);
y=yredo(1:L,1);
Lredo=Lredo(2:end);
Iredo=Iredo(2:end);
xredo=xredo(:,2:end);
yredo=yredo(:,2:end);
end
case 'z'
close(hZ)
set(h,'WindowKeyPressFcn',@Key,...
'windowbuttondownfcn',@Down,...
'WindowButtonMotionFcn',@Move,...
'WindowButtonUpFcn',@Up)
return
%%% press 'd' to change the rate of displacement of a knot
%%% when using the arrow keys instead of holding left mouse
%%% button
case 'd'
delta=str2double(inputdlg('enter delta'));
deltaZ=.2*delta;
case 'leftarrow'
x(I)=max([x(I)-delta,xmin]);
case 'rightarrow'
x(I)=min([x(I)+delta,xmax]);
case 'downarrow'
y(I)=max([y(I)-delta,ymin]);
case 'uparrow'
y(I)=min([y(I)+delta,ymax]);
end
if I<1
I=1;
elseif I>L
I=L;
end
[xs ys] = splinefit([1;x;1],[0;y;0],1);
set(0, 'CurrentFigure', h)
plot(xs,ys,'k', ...
[1;x],[0;y],'.k', ...
x(I),y(I),'xk', ...
'markersize',13);
axis equal
axis([0 1 -.2 .2])
%drawnow
set(0, 'CurrentFigure', hZ)
try delete(t);end;
plot(xs,ys,'k', ...
[1;x],[0;y],'.k', ...
x(I),y(I),'xk', ...
'markersize',13);
axis equal
axis(axisZ)
t=text(x(I)+deltxtZ,y(I)-deltxtZ, ...
['(',num2str(x(I),'%8.4f'),',',num2str(y(I),'%8.4f'),')'], ...
'color','k','fontweight','bold');
drawnow
end
function figDelete(varargin)
try close(hZ);end;
end
function figDeleteZ(varargin)
set(h,'WindowKeyPressFcn',@Key,...
'Windowbuttondownfcn',@Down,...
'WindowButtonMotionFcn',@Move,...
'WindowButtonUpFcn',@Up)
return
end
%%%% empty function to make figure 'h' inactive while 'hZ' is active
function emptyFunctionHandle(varargin)
end
end