-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfcplot.m
More file actions
1709 lines (1472 loc) · 98.1 KB
/
fcplot.m
File metadata and controls
1709 lines (1472 loc) · 98.1 KB
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function fh = fcplot(datalist,plottype,varargin)
% Color plot of the data in datalist
% (returns handle to the figure window)
%
% Plottype: 'qxy' (standard), 'angles', 'qeplane', 'qxqyen'
% P. Steffens 08/2011 - 01/2026
if nargin <2, plottype='QXY'; end
%% Create plot structure
plotstruct = makeplotstruct(datalist,plottype,varargin{:});
if isempty(plotstruct), return; end
%% Open new window and create contents
% fighandle = figure;
plotstruct.axeshandle = readinput('axeshandle',varargin);
newwindow = isempty(plotstruct.axeshandle);
if ~newwindow
if ~ishandle(plotstruct.axeshandle) || ~strcmpi(get(plotstruct.axeshandle,'type'),'axes'), fprintf('Error: "axeshandle" is not valid.\n'); return; end
plotstruct.figurehandle = get(plotstruct.axeshandle,'parent');
else
plotstruct.figurehandle = figure('Position',[50 50 550 550],'Toolbar','figure');%,'Color',[1,1,1]);
plotstruct.axeshandle = axes('OuterPosition',[0 0 1 .95]);
end
fh = plotstruct.figurehandle;
% Test if 3D
if any(strcmpi(plotstruct.type,{'qxqyen','qxyz'})), threedim = true; else threedim = false; end
% Callback procedures, to be asociated to the menu and contextmenu entries
% (These are the "shorter" callbacks, they are simply defined as single
% strings. The longer callbacks are defined as separate procedures below.)
callback_linscale = ['plotstruct = guidata(gcf); plotstruct.linlog = ''LIN''; ' ...
'if isfield(plotstruct,''interpolatedimage''), plotstruct=rmfield(plotstruct,''interpolatedimage''); end; guidata(gcf,plotstruct); doplot(plotstruct,''keepview''); clear plotstruct;' ];
callback_logscale = ['plotstruct = guidata(gcf); plotstruct.linlog = ''LOG''; ' ...
'if isfield(plotstruct,''interpolatedimage''), plotstruct=rmfield(plotstruct,''interpolatedimage''); end; guidata(gcf,plotstruct); doplot(plotstruct,''keepview''); '...
'mrgl=mergelist(plotstruct.datalist,''valuelist''); if min(mrgl(:,1))<=0, fprintf(''Note: to plot on logarithmic scale, data are shifted to positive range by adding a constant.\n''); end; clear plotstruct; clear mrgl;'];
callback_direct = 'plotstruct = guidata(gcf); plotstruct.presentation = ''voronoi''; guidata(gcf,plotstruct); doplot(plotstruct,''keepview''); clear plotstruct;' ;
callback_interp = 'plotstruct = guidata(gcf); plotstruct.presentation = ''linear''; guidata(gcf,plotstruct); doplot(plotstruct,''keepview''); clear plotstruct;' ;
callback_grid = 'plotstruct = guidata(gcf); plotstruct.showcells = ~plotstruct.showcells; guidata(gcf,plotstruct); doplot(plotstruct,''keepview''); clear plotstruct;' ;
callback_coord = 'plotstruct = guidata(gcf); plotstruct.showcoordpoints = ~plotstruct.showcoordpoints; guidata(gcf,plotstruct); doplot(plotstruct,''keepview''); clear plotstruct;' ;
callback_vectors = 'plotstruct = guidata(gcf); plotstruct.showvectors = ~plotstruct.showvectors; guidata(gcf,plotstruct); doplot(plotstruct,''keepview''); clear plotstruct;' ;
callback_hklgrid = 'plotstruct = guidata(gcf); plotstruct.showgrid = ~plotstruct.showgrid; guidata(gcf,plotstruct); doplot(plotstruct,''keepview''); clear plotstruct;' ;
callback_edges = 'plotstruct = guidata(gcf); plotstruct.showedges = ~plotstruct.showedges; guidata(gcf,plotstruct); doplot(plotstruct,''keepview''); clear plotstruct;' ;
callback_save = ['plotstruct = guidata(gcf); [file,path] = uiputfile(''*.*'',''Save 2D Flatcone data''); ' ...
'if file~=0, sdat=[plotstruct.coordlist, mergelist(plotstruct.datalist,''valuelist'')]; save([path,file],''sdat'',''-ascii''); end; clear plotstruct;' ];
callback_scansave = ['plotstruct = guidata(gcf); [file,path] = uiputfile(''*.*'',''Save Flatcone scan data (simple format)''); ' ...
'if file~=0, sdat=[plotstruct.scandata.x, plotstruct.scandata.y, plotstruct.scandata.dy]; save([path,file],''sdat'',''-ascii''); end' ];
callback_np = ['plotstruct = guidata(gcf); np = inputdlg(''Enter number of scan points'',''Scan definition'',1,{num2str(plotstruct.scandef.np)}); ' ...
'if ~isempty(np), plotstruct.scandef.np = str2num(np{1}); end; guidata(gcf, plotstruct); if isfield(plotstruct,''scandata''), doscanplot(plotstruct); end ' ];
callback_xax_auto = 'plotstruct = guidata(gcf); plotstruct.scandef.xaxiscoord = ''AUTO''; guidata(gcf,plotstruct); doscanplot(plotstruct);' ;
callback_xax_qm = 'plotstruct = guidata(gcf); plotstruct.scandef.xaxiscoord = ''QMOD''; guidata(gcf,plotstruct); doscanplot(plotstruct);' ;
callback_xax_qx = 'plotstruct = guidata(gcf); plotstruct.scandef.xaxiscoord = ''QX''; guidata(gcf,plotstruct); doscanplot(plotstruct);' ;
callback_xax_qy = 'plotstruct = guidata(gcf); plotstruct.scandef.xaxiscoord = ''QY''; guidata(gcf,plotstruct); doscanplot(plotstruct);' ;
callback_xax_qh = 'plotstruct = guidata(gcf); plotstruct.scandef.xaxiscoord = ''QH''; guidata(gcf,plotstruct); doscanplot(plotstruct);' ;
callback_xax_qk = 'plotstruct = guidata(gcf); plotstruct.scandef.xaxiscoord = ''QK''; guidata(gcf,plotstruct); doscanplot(plotstruct);' ;
callback_xax_ql = 'plotstruct = guidata(gcf); plotstruct.scandef.xaxiscoord = ''QL''; guidata(gcf,plotstruct); doscanplot(plotstruct);' ;
callback_xax_qm = 'plotstruct = guidata(gcf); plotstruct.scandef.xaxiscoord = ''QM''; guidata(gcf,plotstruct); doscanplot(plotstruct);' ;
callback_xax_en = 'plotstruct = guidata(gcf); plotstruct.scandef.xaxiscoord = ''EN''; guidata(gcf,plotstruct); doscanplot(plotstruct);' ;
callcack_replotscan ='plotstruct = guidata(gcf); doscanplot(plotstruct);' ;
% Create menu and contextmenu (identical)
menuentry = uimenu('Label','FLATCONE');
contextmenu = uicontextmenu;
for f=[menuentry, contextmenu]
uimenu(f,'Label','Logarithmic scale','Callback',callback_logscale); % Change color scale to logarithmic
uimenu(f,'Label','Linear scale','Callback',callback_linscale); % Change color scale to linear
uimenu(f,'Label','Adjust Color Scale','Callback','colorlimits(gcbf);'); % Open window for coloraxis limits
uimenu(f,'Label','Direct','Callback', callback_direct, 'Separator','on'); % Plot cells (voronoi)
uimenu(f,'Label','Interpolated','Callback', callback_interp); % Interpolated plot
uimenu(f,'Label','Set interpolation options','Callback',@setinterpolationoptions); % Dialog for interpolation options
smen3 = uimenu(f,'Label','Graphical elements', 'Separator','on');
uimenu(smen3,'Label','Data cells on/off','Callback', callback_grid); %Show cell boundaries
uimenu(smen3,'Label','Measured points on/off','Callback', callback_coord); % Show coordinates of measured points
uimenu(smen3,'Label','Orienting vectors on/off','Callback', callback_vectors);% Show vectors ax,bx
uimenu(smen3,'Label','HKL-grid on/off','Callback', callback_hklgrid); % Show underlying HKL-grid
uimenu(smen3,'Label','Slice edges on/off','Callback', callback_edges);
smen1 = uimenu(f,'Label','Caculate intersections');
uimenu(smen1,'Label','Powder lines','Callback',@show_powder); % Open dialog to choose powder lines to plot
uimenu(smen1,'Label','Integer H,K,L planes','Callback',@callback_HKLintersect);
uimenu(smen1,'Label','Delete all','Callback',@callback_deleteintersect); % Remove all linedefs and planedefs
uimenu(f,'Label','Delete data point by click', 'Callback', @start_clickdelete, 'Separator', 'on'); % Delete point by mouseclick
uimenu(f,'Label','Select region to delete', 'Callback', @start_deleteregion); % Select rectangular region to delete
uimenu(f,'Label','Define a scan (interpolation)','Callback', @preparescan, 'Separator', 'on'); % Do the necessary to extract a 1D-Scan (interpolation)
uimenu(f,'Label','Define a scan (integration)','Callback', @prepareintegration); % Do the necessary to extract a 1D-Scan (integration)
uimenu(f,'Label','Define a scan (projection)','Callback', @prepareprojection); % Do the necessary to extract a 1D-Scan (projection)
smen4 = uimenu(f,'Label','Cut planes (in 3d)', 'Enable', 'off');
uimenu(smen4,'Label','New (numerical input)','Callback',@addaplane);
uimenu(smen4,'Label','New vertical (x)','Callback',@addaplane);
uimenu(smen4,'Label','New vertical (y)','Callback',@addaplane);
uimenu(smen4,'Label','Edit','Callback',[]);
uimenu(smen4,'Label','Delete','Callback',[]);
uimenu(smen4,'Label','Link to window (on/off)','Callback',[], 'Separator', 'on');
uimenu(smen4,'Label','Preview (on/off)','Callback',[]);
uimenu(smen4,'Label','Keep vertical (on/off)','Callback',[]);
uimenu(smen4,'Label','Do interpolation (per slice)','Callback',[], 'Separator', 'on');
uimenu(smen4,'Label','Do interpolation (global)','Callback',[]);
uimenu(smen4,'Label','Do integration (per slice)','Callback',[]);
% uimenu(smen4,'Label','Do integration (global)','Callback',[]);
uimenu(smen4,'Label','Set parameters','Callback',[], 'Separator', 'on');
uimenu(f,'Label','Add a vertical cut plane','Callback', @addaplane, 'Enable', 'off'); % Insert a cut plane
uimenu(f,'Label','Save data to file', 'Callback', callback_save, 'Separator', 'on'); % Save 2D data to file
smen2 = uimenu(f,'Label','Change axes', 'Separator', 'on');
uimenu(smen2,'Label','Qx,Qy reciprocal Angstrom','Callback', @changeaxes); % Change coordinate system
uimenu(smen2,'Label','Angles 2Theta/Psi','Callback', @changeaxes);
uimenu(smen2,'Label','Reciprocal lattice units','Callback', @changeaxes);
if strcmpi(plotstruct.type,'qeplane')
set(findobj('Label','Change axes', 'Parent', f), 'Enable', 'off');
set(findobj('Label','HKL-grid on/off', 'Parent', smen3), 'Enable', 'off');
set(findobj('Label','Orienting vectors on/off', 'Parent', smen3), 'Enable', 'off');
end
if threedim
% set(findobj('Label','Interpolated', 'Parent', f), 'Enable', 'off');
% set(findobj('Label','Delete data point by click', 'Parent', f), 'Enable', 'off');
% set(findobj('Label','Select region to delete', 'Parent', f), 'Enable', 'off');
set(findobj('Label','Orienting vectors on/off', 'Parent', smen3), 'Enable', 'off');
set(findobj('Label','HKL-grid on/off', 'Parent', smen3), 'Enable', 'off');
% set(findobj('Label','Define a scan (interpolation)', 'Parent', f), 'Enable', 'off');
% set(findobj('Label','Define a scan (integration)', 'Parent', f), 'Enable', 'off');
set(findobj('Label','Define a scan (projection)', 'Parent', f), 'Enable', 'off');
set(findobj('Label','Add a vertical cut plane', 'Parent', f), 'Enable', 'on');
set(findobj('Label','Change axes', 'Parent', f), 'Enable', 'off');
% set(findobj('Label','Angles 2Theta/Psi', 'Parent', f), 'Enable', 'off');
end
end
scsavemenu = [];
scexportmen = [];
set(plotstruct.axeshandle,'UIContextMenu',contextmenu);
% Callback to show mouse coordinates
if size(plotstruct.vertexlist,2)<3 % 2D-plot
set(plotstruct.figurehandle, 'WindowButtonMotionFcn', @showpos);
plotstruct.poshandle = uicontrol('Style', 'Text', 'Units', 'Normalized', 'Position', [.65,0,.34,.05], 'String', '', ...
'Horizontalalignment', 'right', 'BackgroundColor', get(plotstruct.figurehandle,'Color'));
end
%%
% Save plotstruct as variable associated with this window
% (so that it can be retrieved later by guidata(..) for further use (replotting etc.)
guidata(plotstruct.figurehandle,plotstruct);
%% Plot
doplot(plotstruct);
% Title, Information line, etc.
title([' \fontsize{10}', plotstruct.scaninfo2, ['\fontsize{11} \bf ' plotstruct.scaninfo]]);
%uicontrol('Style','Text', 'Units', 'Normalized', 'Position',[0,.93,1,.07], ...
% 'Tag', 'Titlebar2', 'String',plotstruct.scaninfo2);
% [normalizeto, normval] = getoption('normalizeto', 'normval', 'check', varargin);
if isfield(plotstruct,'properties') && isfield(plotstruct.properties,'normalization')
annotation('Textbox','Units', 'pixels','Position',[1,1,550,25], 'String', ['Norm: ' plotstruct.properties.normalization], ...
'Horizontalalignment', 'left', 'linestyle', 'none');
end
%% Callback to show mouse pointer coordinates
function showpos(src,evnt)
if threedim, return; end
try
cp = get(plotstruct.axeshandle,'CurrentPoint');
xrange = get(plotstruct.axeshandle,'xlim');
yrange = get(plotstruct.axeshandle,'ylim');
if strcmpi(plotstruct.type, 'QEPLANE')
if cp(1,1)>xrange(1) && cp(1,1)<xrange(2) && cp(1,2)>yrange(1) && cp(1,2)<yrange(2)
set(plotstruct.poshandle, 'String', ['|q| = ', num2str(cp(1,1),'%5.3f'), ', E = ', num2str(cp(1,2),'%5.2f'), ' '] );
else
set(plotstruct.poshandle, 'String', '');
end
elseif strcmpi(plotstruct.type, 'QXY')
if cp(1,1)>xrange(1) && cp(1,1)<xrange(2) && cp(1,2)>yrange(1) && cp(1,2)<yrange(2)
UB = UBmatrix( plotstruct.sampleinfo.lattice, plotstruct.sampleinfo.ax, plotstruct.sampleinfo.bx);
[H, K, L] = calcHKL( cp(1,1), cp(1,2), plotstruct.datalist{1}.QVERT, UB );
set(plotstruct.poshandle, 'String', {['qxy = [', num2str(cp(1,1),'%5.3f'), ', ', num2str(cp(1,2),'%5.3f'), '] '] , ...
['HKL = ' num2str(H,'%6.3f') ', ' num2str(K,'%6.3f') ', ' num2str(L,'%6.3f') ' ' ] } );
else
set(plotstruct.poshandle, 'String', '');
end
elseif strcmpi(plotstruct.type, 'HKLvectors')
if cp(1,1)>xrange(1) && cp(1,1)<xrange(2) && cp(1,2)>yrange(1) && cp(1,2)<yrange(2)
UB = UBmatrix( plotstruct.sampleinfo.lattice, plotstruct.sampleinfo.ax, plotstruct.sampleinfo.bx);
hkl = plotstruct.basisvectors.origin + cp(1,1)*plotstruct.basisvectors.vector1 + cp(1,2)*plotstruct.basisvectors.vector2;
qq = UB*hkl(:);
set(plotstruct.poshandle, 'String', {['qxy = [', num2str(qq(1),'%5.3f'), ', ', num2str(qq(2),'%5.3f'), '] '] , ...
['HKL = ' num2str(hkl(1),'%6.3f') ', ' num2str(hkl(2),'%6.3f') ', ' num2str(hkl(3),'%6.3f') ' ' ] } );
else
set(plotstruct.poshandle, 'String', '');
end
elseif strcmpi(plotstruct.type, 'ANGLES')
if cp(1,1)>xrange(1) && cp(1,1)<xrange(2) && cp(1,2)>yrange(1) && cp(1,2)<yrange(2)
set(plotstruct.poshandle, 'String', ['Angles = [', num2str(cp(1,1),'%5.2f'), ', ', num2str(cp(1,2),'%5.2f'), '] '] );
else
set(plotstruct.poshandle, 'String', '');
end
end
catch
end
end
%% Callback routines for definition of scan line
function mouseclick1(src,evnt)
% Callback for Mouseclick in the 2D-Plot; starts defining a scan line
if strcmp(get(gcf,'SelectionType'),'normal')
plotstruct = guidata(gcf);
set(gcf,'pointer','circle');
cp = get(plotstruct.axeshandle,'CurrentPoint');
xstart = cp(1,1);ystart = cp(1,2);
plotstruct.scandef.xdat = [xstart,xstart];
plotstruct.scandef.ydat = [ystart,ystart];
if isempty(plotstruct.scanlinehandle),
plotstruct.scanlinehandle = line('XData',xstart,'YData',ystart,'Marker','p','color','r');
else
set(plotstruct.scanlinehandle,'XData',xstart,'YData',ystart,'Marker','p','color','r');
end
try %#ok<ALIGN>
delete(plotstruct.projectlinehandle);
delete(plotstruct.pp1handle);
delete(plotstruct.pp2handle);
delete(plotstruct.projectframehandle);
catch end
plotstruct.projectlinehandle=[]; plotstruct.pp1handle=[]; plotstruct.pp2handle=[]; plotstruct.projectframehandle=[];
set(gcf,'WindowButtonMotionFcn',@mousemove1)
set(gcf,'WindowButtonUpFcn',@mouseup1)
end
function mousemove1(src,evnt)
showpos;
cp = get(plotstruct.axeshandle,'CurrentPoint');
plotstruct.scandef.xdat = [xstart,cp(1,1)];
plotstruct.scandef.ydat = [ystart,cp(1,2)];
set(plotstruct.scanlinehandle, 'XData', plotstruct.scandef.xdat, 'YData', plotstruct.scandef.ydat); drawnow expose
end
function mouseup1(src,evnt)
% Reset behaviour of figure
set(src,'Pointer','arrow')
set(src,'WindowButtonMotionFcn',@showpos)
set(src,'WindowButtonUpFcn','')
% Set callback routine for line movement
set(plotstruct.scanlinehandle, 'ButtonDownFcn', @mouseclick_line);
%** {1}
plotstruct.scandef.np = estimateNP([plotstruct.scandef.xdat', plotstruct.scandef.ydat'], plotstruct.vertexlist, plotstruct.datalist{1}.faces);
guidata(gcf, plotstruct);
doscanplot(plotstruct);
set(scsavemenu, 'Enable', 'on');
set(scexportmen, 'Enable', 'on');
end
end %mouseclick1
%% Callback routines for movement of scan line
function mouseclick_line(src,event)
if strcmp(get(gcf,'SelectionType'),'normal')
plotstruct = guidata(gcf);
% Get current mouse position
cp = get(plotstruct.axeshandle,'CurrentPoint');
xstart = cp(1,1);ystart = cp(1,2);
oldpointer = get(gcf,'pointer');
set(gcf,'pointer','fleur');
set(gcf,'WindowButtonMotionFcn',@mousemove_sl)
set(gcf,'WindowButtonUpFcn',@mouseup_sl)
end
function mousemove_sl(src,event)
showpos;
cp = get(plotstruct.axeshandle,'CurrentPoint');
% Shift start and end points like mouse movement (relative to start values)
plotstruct.scandef.xdat = plotstruct.scandef.xdat + cp(1,1) - xstart;
plotstruct.scandef.ydat = plotstruct.scandef.ydat + cp(1,2) - ystart;
xstart = cp(1,1); ystart = cp(1,2);
set(plotstruct.scanlinehandle, 'XData', plotstruct.scandef.xdat, 'YData', plotstruct.scandef.ydat); drawnow
end
function mouseup_sl(src,evnt)
set(src,'WindowButtonMotionFcn',@showpos)
set(src,'WindowButtonUpFcn','')
set(gcf,'pointer', oldpointer')
guidata(gcf, plotstruct);
doscanplot(plotstruct);
end
end %mouseclick_line
%% Callback routines for definition of projection
function mouseclick2(src,evnt)
% Callback for Mouseclick in the 2D-Plot; starts defining a projection line
if strcmp(get(gcf,'SelectionType'),'normal')
plotstruct = guidata(gcf);
set(gcf,'pointer','circle');
cp = get(plotstruct.axeshandle,'CurrentPoint');
xstart = cp(1,1);ystart = cp(1,2);
framevec = [cp(1,1), cp(1,2)];
try %#ok<ALIGN>
delete(plotstruct.scanlinehandle);
delete(plotstruct.projectlinehandle);
delete(plotstruct.pp1handle);
delete(plotstruct.pp2handle);
delete(plotstruct.projectframehandle);
catch end %#ok<*SEPEX>
plotstruct.scanlinehandle=[];
plotstruct.projectlinehandle = line('XData',xstart,'YData',ystart,'Marker','none','color','r');
plotstruct.projectframehandle = line('XData',xstart,'YData',ystart,'Marker','none','color','r');
plotstruct.pp1handle = line('XData',xstart,'YData',ystart,'Marker','o','MarkerFaceColor','r','color','r');
plotstruct.pp2handle = line('XData',xstart,'YData',ystart,'Marker','o','MarkerFaceColor','r','color','r');
set(gcf,'WindowButtonMotionFcn',@mousemove2)
set(gcf,'WindowButtonUpFcn',@mouseup2)
end
function mousemove2(src,evnt)
showpos;
cp = get(plotstruct.axeshandle,'CurrentPoint');
plotstruct.scandef.xdat = [xstart,cp(1,1)];
plotstruct.scandef.ydat = [ystart,cp(1,2)];
set(plotstruct.projectlinehandle, 'XData', plotstruct.scandef.xdat, 'YData', plotstruct.scandef.ydat);
yl = get(plotstruct.axeshandle,'ylim'); xl = get(plotstruct.axeshandle,'xlim');
vec = [plotstruct.scandef.xdat(2)-plotstruct.scandef.xdat(1), plotstruct.scandef.ydat(2)-plotstruct.scandef.ydat(1)];
framevec = [-vec(2) / (yl(2)-yl(1))*(xl(2)-xl(1)), vec(1) / (xl(2)-xl(1))*(yl(2)-yl(1))] * min(0.5 , 0.25*sqrt(sum([vec(1)/(xl(2)-xl(1)), vec(2)/(yl(2)-yl(1))].^2)) );
plotstruct.scandef.framevec = framevec;
set(plotstruct.projectframehandle, 'XData', [xstart+framevec(1), cp(1,1)+framevec(1), cp(1,1)-framevec(1), xstart-framevec(1), xstart+framevec(1)], ...
'YData', [ystart+framevec(2), cp(1,2)+framevec(2), cp(1,2)-framevec(2), ystart-framevec(2), ystart+framevec(2)]);
set(plotstruct.pp1handle, 'XData', cp(1,1), 'YData', cp(1,2));
set(plotstruct.pp2handle, 'XData', xstart+framevec(1), 'YData', ystart+framevec(2));
drawnow
end
function mouseup2(src,evnt)
% Reset behaviour of figure
set(src,'Pointer','arrow')
set(src,'WindowButtonMotionFcn',@showpos)
set(src,'WindowButtonUpFcn','')
% Set callback routine for line movement
set(plotstruct.projectlinehandle, 'ButtonDownFcn', @mouseclick_projectline);
set(plotstruct.pp1handle, 'ButtonDownFcn', @mouseclick_pp1);
set(plotstruct.pp2handle, 'ButtonDownFcn', @mouseclick_pp2);
% ** ({1})
plotstruct.scandef.np = estimateNP([plotstruct.scandef.xdat', plotstruct.scandef.ydat'], plotstruct.vertexlist, plotstruct.datalist{1}.faces);
guidata(gcf, plotstruct);
doscanplot(plotstruct);
set(scsavemenu, 'Enable', 'on');
set(scexportmen, 'Enable', 'on');
end
end %mouseclick2
%% Callback routines for movement, rotation and resize of projection
function mouseclick_projectline(src,event) %#ok<*INUSD>
if strcmp(get(gcf,'SelectionType'),'normal')
plotstruct = guidata(gcf);
% Get current mouse position
cp = get(plotstruct.axeshandle,'CurrentPoint');
xstart = cp(1,1);ystart = cp(1,2);
oldpointer = get(gcf,'pointer');
set(gcf,'pointer','fleur');
set(gcf,'WindowButtonMotionFcn',@mousemove_pl)
set(gcf,'WindowButtonUpFcn',@mouseup_pl)
end
function mousemove_pl(src,event)
showpos;
cp = get(plotstruct.axeshandle,'CurrentPoint');
% Shift all points of projection definition according to mouse movement (relative to start values)
plotstruct.scandef.xdat = plotstruct.scandef.xdat + cp(1,1) - xstart;
plotstruct.scandef.ydat = plotstruct.scandef.ydat + cp(1,2) - ystart;
xstart = cp(1,1); ystart = cp(1,2);
set(plotstruct.projectlinehandle, 'XData', plotstruct.scandef.xdat, 'YData', plotstruct.scandef.ydat);
framevec = plotstruct.scandef.framevec; %(Assign these variables only to make notation shorter in following lines)
xdat = plotstruct.scandef.xdat; ydat = plotstruct.scandef.ydat;
set(plotstruct.projectframehandle, 'XData', [xdat(1)+framevec(1), xdat(2)+framevec(1), xdat(2)-framevec(1), xdat(1)-framevec(1), xdat(1)+framevec(1)], ...
'YData', [ydat(1)+framevec(2), ydat(2)+framevec(2), ydat(2)-framevec(2), ydat(1)-framevec(2), ydat(1)+framevec(2)]);
set(plotstruct.pp1handle, 'XData', plotstruct.scandef.xdat(2), 'YData', plotstruct.scandef.ydat(2));
set(plotstruct.pp2handle, 'XData', plotstruct.scandef.xdat(1)+framevec(1), 'YData', plotstruct.scandef.ydat(1)+framevec(2));
drawnow
end
function mouseup_pl(src,evnt)
set(src,'WindowButtonMotionFcn',@showpos)
set(src,'WindowButtonUpFcn','')
set(gcf,'pointer',oldpointer)
guidata(gcf, plotstruct);
doscanplot(plotstruct);
end
end %mouseclick_projectline
function mouseclick_pp1(src,event) % Rotates the whole projection
if strcmp(get(gcf,'SelectionType'),'normal')
plotstruct = guidata(gcf);
set(gcf,'WindowButtonMotionFcn',@mousemove_pp1)
set(gcf,'WindowButtonUpFcn',@mouseup_pp1)
% Get current mouse position
cp = get(plotstruct.axeshandle,'CurrentPoint');
xstart = cp(1,1);ystart = cp(1,2);
end
function mousemove_pp1(src,event)
showpos;
cp = get(plotstruct.axeshandle,'CurrentPoint');
yl = get(plotstruct.axeshandle,'ylim'); xl = get(plotstruct.axeshandle,'xlim'); % to consider axis ratio
% Shift all points of projection definition according to mouse movement (relative to start values)
plotstruct.scandef.xdat(2) = cp(1,1);
plotstruct.scandef.ydat(2) = cp(1,2);
vec_alt = [xstart-plotstruct.scandef.xdat(1), ystart-plotstruct.scandef.ydat(1)] ./ [xl(2)-xl(1), yl(2)-yl(1)];
xstart = cp(1,1); ystart = cp(1,2);
vec_neu = [xstart-plotstruct.scandef.xdat(1), ystart-plotstruct.scandef.ydat(1)] ./ [xl(2)-xl(1), yl(2)-yl(1)];
% Determine the angle of rotation of the central line
cosa = vec_alt*vec_neu' / sqrt(sum(vec_alt.^2)) / sqrt(sum(vec_neu.^2));
signa = sign(vec_neu(2)*vec_alt(1)-vec_neu(1)*vec_alt(2));
% Rotate framevec accordingly
framevec = plotstruct.scandef.framevec ./ [xl(2)-xl(1), yl(2)-yl(1)] * [ cosa, signa*sqrt(1-cosa^2); -signa*sqrt(1-cosa^2), cosa] .* [xl(2)-xl(1), yl(2)-yl(1)];
plotstruct.scandef.framevec = framevec;
set(plotstruct.projectlinehandle, 'XData', plotstruct.scandef.xdat, 'YData', plotstruct.scandef.ydat);
xdat = plotstruct.scandef.xdat; ydat = plotstruct.scandef.ydat; %(Assign these variables only to make notation shorter in following lines)
set(plotstruct.projectframehandle, 'XData', [xdat(1)+framevec(1), xdat(2)+framevec(1), xdat(2)-framevec(1), xdat(1)-framevec(1), xdat(1)+framevec(1)], ...
'YData', [ydat(1)+framevec(2), ydat(2)+framevec(2), ydat(2)-framevec(2), ydat(1)-framevec(2), ydat(1)+framevec(2)]);
set(plotstruct.pp1handle, 'XData', plotstruct.scandef.xdat(2), 'YData', plotstruct.scandef.ydat(2));
set(plotstruct.pp2handle, 'XData', plotstruct.scandef.xdat(1)+framevec(1), 'YData', plotstruct.scandef.ydat(1)+framevec(2));
drawnow
end
function mouseup_pp1(src,evnt)
set(src,'WindowButtonMotionFcn',@showpos)
set(src,'WindowButtonUpFcn','')
guidata(gcf, plotstruct);
doscanplot(plotstruct);
end
end %mouseclick_pp1
function mouseclick_pp2(src,event) % Changes the frame for the projection
if strcmp(get(gcf,'SelectionType'),'normal')
plotstruct = guidata(gcf);
set(gcf,'WindowButtonMotionFcn',@mousemove_pp2)
set(gcf,'WindowButtonUpFcn',@mouseup_pp2)
end
function mousemove_pp2(src,event)
showpos;
cp = get(plotstruct.axeshandle,'CurrentPoint');
% Shift all points of projection definition according to mouse movement (relative to start values)
framevec = [cp(1,1)-plotstruct.scandef.xdat(1), cp(1,2)-plotstruct.scandef.ydat(1)];
plotstruct.scandef.framevec = framevec;
set(plotstruct.projectlinehandle, 'XData', plotstruct.scandef.xdat, 'YData', plotstruct.scandef.ydat);
xdat = plotstruct.scandef.xdat; ydat = plotstruct.scandef.ydat; %(Assign these variables only to make notation shorter in following lines)
set(plotstruct.projectframehandle, 'XData', [xdat(1)+framevec(1), xdat(2)+framevec(1), xdat(2)-framevec(1), xdat(1)-framevec(1), xdat(1)+framevec(1)], ...
'YData', [ydat(1)+framevec(2), ydat(2)+framevec(2), ydat(2)-framevec(2), ydat(1)-framevec(2), ydat(1)+framevec(2)]);
set(plotstruct.pp1handle, 'XData', plotstruct.scandef.xdat(2), 'YData', plotstruct.scandef.ydat(2));
set(plotstruct.pp2handle, 'XData', plotstruct.scandef.xdat(1)+framevec(1), 'YData', plotstruct.scandef.ydat(1)+framevec(2));
drawnow
end
function mouseup_pp2(src,evnt)
set(src,'WindowButtonMotionFcn',@showpos)
set(src,'WindowButtonUpFcn','')
guidata(gcf, plotstruct);
doscanplot(plotstruct);
end
end %mouseclick_pp1
%% Callback : Add a plane
function addaplane(src,evnt)
plotstruct = guidata(gcf);
newplanedef.normal = [1;0;0];
newplanedef.c = sum(xlim)/2;
newplanedef.properties = {'FaceColor','r','FaceAlpha',.35,'Tag','cutplane','ButtonDownFcn',@mouseclickonplane}; %** use options.m for this?
if ~isfield(plotstruct,'planedef'), plotstruct.planedef = {}; end
% open a figure for the cut
if isfield(plotstruct,'originaldata')
figure;
newplanedef.axes = axes(); % handle to axes
end
plotstruct.planedef = [plotstruct.planedef, newplanedef];
guidata(gcf, plotstruct);
doplot(plotstruct);
end %addaplane
%% Callback for plane movements
function mouseclickonplane(src,evnt)
% Callback for Mouseclick on a plane
if strcmp(get(gcf,'SelectionType'),'normal')
plotstruct = guidata(gcf);
cp = get(plotstruct.axeshandle,'CurrentPoint');
camPos= get(plotstruct.axeshandle, 'CameraPosition');
xyzlim = axis; % axis limits
corners = xyzlim([1,3,5;1,3,6;1,4,5;1,4,6;2,3,5;2,3,6;2,4,5;2,4,6]);
axunits = get(gca,'units'); set(gca,'units','pixels'); axpos = get(gca,'position'); set(gca,'units',axunits);
rot = rotationtoviewingplane(plotstruct.axeshandle);
cornersr = rot*corners';
pixelscale = [axpos(3)/(max(cornersr(1,:))-min(cornersr(1,:))), axpos(4)/(max(cornersr(2,:))-min(cornersr(2,:)))];
pointh=[]; lineh=[]; %handles for graphics during movement
% first, look on which plane is clicked
mindist = inf; plane=nan;
for npl = 1:length(plotstruct.planedef)
% Point at which the vieweing line intersects
pt = cp(1,:) + (plotstruct.planedef{npl}.c - cp(1,:)*plotstruct.planedef{npl}.normal)/((cp(2,:)-cp(1,:))*plotstruct.planedef{npl}.normal)*(cp(2,:)-cp(1,:));
if pt(1)<xyzlim(1) || pt(1)>xyzlim(2) || pt(2)<xyzlim(3) || pt(2)>xyzlim(4) || pt(3)<xyzlim(5) || pt(3)>xyzlim(6)
continue; end % if not in the visible range
if norm(pt-camPos)<mindist, plane=npl; mindist=norm(pt-camPos); hitpoint=pt; end
end
if isnan(plane), return; end
% Then, check if this place is "visible", i.e. no data in front of it
hitface = findface3d (plotstruct.axeshandle, plotstruct.coordlist, plotstruct.vertexlist, plotstruct.datalist);
if ~isnan(hitface) && norm(plotstruct.coordlist(hitface,:)-camPos)<mindist, return; end
% get handle to this plane object (may be different from calling obj.)
thisplaneh = findobj(get(plotstruct.axeshandle,'children'),'Tag','cutplane');
thisplaneh = thisplaneh(end+1-plane);
oldcolor = get(thisplaneh,'facecolor');
set(thisplaneh,'facecolor','b');
oldlimmode = get(plotstruct.axeshandle,'xlimmode');
axis manual; % fix axes limits
% Determine, if clicked on a border or somewhere in the middle
% According to that, do perpendicular movement or rotation
vertp = get(thisplaneh,'vertices');
facp = get(thisplaneh,'faces');
% determine distance of hitpoint to each border
borderdist = nan(1,numel(facp));
for nb= 1:numel(facp)
gvec = vertp(facp(nb),:)-vertp(facp(mod(nb,numel(facp))+1),:); %connection of the two endpoints, describes border line
distvec = (hitpoint-vertp(facp(nb),:)) - ((hitpoint-vertp(facp(nb),:))*gvec')/(gvec*gvec')*gvec;
% this distance in pixels
distvec = rot*distvec'; borderdist(nb) = sqrt((distvec(1)*pixelscale(1))^2+(distvec(2)*pixelscale(2))^2);
end
% determine distance of hitpoint to each corner
cornerdist = nan(1,size(vertp,1));
for nc= 1:size(vertp,1)
% this distance in pixels
distvec = rot*(hitpoint-vertp(nc,:))'; cornerdist(nc) = sqrt((distvec(1)*pixelscale(1))^2+(distvec(2)*pixelscale(2))^2);
end
if any(cornerdist<5) % if clicked on a corner
% move this cornerpoint, while keeping the two neighbors fixed (might also keep an edge fixed)
[~,nc] = min(cornerdist);
pointh(1) = plot3(vertp(nc,1),vertp(nc,2),vertp(nc,3),'.r','markersize',20);
ncneighb = facp( [mod(find(facp==nc),numel(facp))+1, mod(find(facp==nc)-2,numel(facp))+1] ); % neighbors are right and left in facp
pointh(2) = plot3(vertp(ncneighb(1),1),vertp(ncneighb(1),2),vertp(ncneighb(1),3),'.b','markersize',20);
pointh(3) = plot3(vertp(ncneighb(2),1),vertp(ncneighb(2),2),vertp(ncneighb(2),3),'.b','markersize',20);
% identify line on which to move (edge of the coord box)
mvdim = find([~any(abs(vertp(nc,1)-xyzlim(1:2))<1e-9); ~any(abs(vertp(nc,2)-xyzlim(3:4))<1e-9); ~any(abs(vertp(nc,3)-xyzlim(5:6))<1e-9)]);
if isempty(mvdim), mvdim=1; end; mvdim=mvdim(1);
set(gcf,'WindowButtonMotionFcn',@mousemovepl2);
elseif any(borderdist<5) % if clicked on a border
% move this edge, while keeping one point fixed
[~,nb] = min(borderdist); endp = [facp(nb), facp(mod(nb,numel(facp))+1)];
lineh = line(vertp(endp,1),vertp(endp,2),vertp(endp,3),'color','r','linewidth',3);
% point which is furthest away from this line
dist = -inf(1,size(vertp,1));
for nc = setdiff(1:size(vertp,1), endp)
dist(nc) = norm(cross(vertp(nc,:)-vertp(endp(1),:),vertp(endp(2),:)-vertp(endp(1),:))); % (not true distance; scaled)
end
[~,nc] = max(dist);
pointh = plot3(vertp(nc,1),vertp(nc,2),vertp(nc,3),'.b','markersize',20);
% identify face on which the line lies
cdim = find( abs(vertp(endp(1),:)-vertp(endp(2),:))<1e-9 & ... %(const. dim)
[any(abs(vertp(endp(1),1)-xyzlim(1:2))<1e-9), any(abs(vertp(endp(1),2)==xyzlim(3:4))<1e-9), any(abs(vertp(endp(1),3)==xyzlim(5:6))<1e-9)] ,1);
axface2d = sortrows(unique(corners(:,setdiff(1:3,cdim)),'rows'),[1,2]); axface2d=axface2d([1,2,4,3],:); % right order!
dvec=vertp(endp(2),:)-vertp(endp(1),:); %nvec2d=dvec(setdiff(1:3,cdim)); %nvec2d=[-nvec2d(2),nvec2d(1)];
set(gcf,'WindowButtonMotionFcn',@mousemovepl3);
else % if inside the plane: movement parallel to plane normal
% it becomes a (2d) problem in the viewing plane. hitpoint shall be moved along the normal vector
hitpr = rot*hitpoint';
vecpr = rot*(hitpoint'+plotstruct.planedef{plane}.normal);
set(gcf,'WindowButtonMotionFcn',@mousemovepl1);
end
set(gcf,'pointer','hand');
set(gcf,'WindowButtonUpFcn',@mouseuppl);
end
function moveok = moveplane(planedef) % helper function to do draw plane at new position
% calculate new intersection with this plane with the axes box
[planevectors, origin] = getplaneparameter(planedef.normal(:)', planedef.c);
% intersection (like in doplot.m)
[cutvertices, cutorder] = createmesh (corners, 1:8, planevectors', origin);
if isempty(cutvertices), moveok=false; return; end % cannot move outside
% transform projected ccordinates back to full system
cutvertices = cutvertices * planevectors' + repmat(origin(:)',size(cutvertices,1),1);
set(thisplaneh,'Vertices',cutvertices, 'Faces',cutorder);
moveok = true;
% update Color plot, if linked one exists
if hasfield(planedef,'axes') && ~isempty(planedef.axes)
[xgrid,ygrid,y] = integratepoints(plotstruct.originaldata, planedef, [.02,.1], .4); % **! set parameters !!
p=pcolor(planedef.axes,xgrid,ygrid,y); set(p,'linestyle','none'); caxis(planedef.axes,[0,.6]); xlim(planedef.axes,[-.5,3]); % ** !!
end
end
function mousemovepl1(src,evnt) % To shift the plane along its normal
showpos;
cp = get(plotstruct.axeshandle,'CurrentPoint');
cp2d = rot*cp(1,:)'; cp2d = cp2d(1:2)';
newpr = hitpr + (cp2d-hitpr(1:2)')*(vecpr(1:2)-hitpr(1:2))/norm(vecpr(1:2)-hitpr(1:2))^2 * (vecpr-hitpr); % for the lambda, work only in 2d
newp3d = rot \ newpr;
newplanedef = plotstruct.planedef{plane};
newplanedef.c = plotstruct.planedef{plane}.normal' * newp3d;
if moveplane(newplanedef), plotstruct.planedef{plane} = newplanedef; end % move, and update if ok
end
function mousemovepl2(src,evnt) % To shift point along line
showpos;
cp = get(plotstruct.axeshandle,'CurrentPoint');
cpr = rot*cp(1,:)'; mvdir=[0,0,0]; mvdir(mvdim)=1; mvdirr=rot*mvdir';
mvpoint = [get(pointh(1),'xdata'), get(pointh(1),'ydata'), get(pointh(1),'zdata')];
mvpointr = rot*mvpoint';
newpr = mvpointr + ((cpr(1:2)-mvpointr(1:2))'*mvdirr(1:2))/norm(mvdirr(1:2)) * mvdirr;
newp3d = rot \ newpr;
mvpoint(mvdim) = newp3d(mvdim);
newplanedef = plotstruct.planedef{plane};
[newplanedef.normal,newplanedef.c] = fithyperplane ([mvpoint; vertp(ncneighb,:)]);
if ~moveplane(newplanedef), return; end % move, and update if ok
plotstruct.planedef{plane} = newplanedef;
set(pointh(1),'xdata',mvpoint(1),'ydata',mvpoint(2),'zdata',mvpoint(3));
end
function mousemovepl3(src,evnt) % To shift edge
showpos;
cp = get(plotstruct.axeshandle,'CurrentPoint');
cpp = cp(1,:) + (cp(2,:)-cp(1,:))* (vertp(endp(1),cdim)-cp(1,cdim))/(cp(2,cdim)-cp(1,cdim)); % mousepoint on movement plane
% cpp+ lambda*dvec is new line, intersect this with the box's face
vertices = createmesh (axface2d, 1:4, dvec(setdiff(1:3,cdim)), cpp(setdiff(1:3,cdim))); % cut in 2d. * Maybe better use createintersection
if size(vertices,1)~=2, return; end
vertices = vertices*dvec + repmat(cpp,2,1); vertices(:,cdim) = vertp(endp(1),cdim); % transform proj. back and set 3rd dim
newplanedef = plotstruct.planedef{plane};
[newplanedef.normal,newplanedef.c] = fithyperplane ([vertices; vertp(nc,:)]);
if ~moveplane(newplanedef), return; end % move, and update if ok
plotstruct.planedef{plane} = newplanedef;
set(lineh,'xdata',vertices(:,1),'ydata',vertices(:,2),'zdata',vertices(:,3));
end
function mouseuppl(src,evnt)
% Reset behaviour of figure
set(src,'Pointer','arrow')
set(src,'WindowButtonMotionFcn',@showpos)
set(src,'WindowButtonUpFcn','')
set(thisplaneh,'facecolor',oldcolor);
delete(pointh); delete(lineh);
set(plotstruct.axeshandle,'xlimmode',oldlimmode,'ylimmode',oldlimmode,'zlimmode',oldlimmode); axis(xyzlim);
guidata(gcf, plotstruct);
% ** do a new extracted plot
end
end %mouseclickonplane
%% Subfunction: Prepare a scan
function preparescan(src,evnt)
plotstruct = guidata(gcf);
plotstruct.scandef.type = 'interpolation';
plotstruct.scandef.np = 20; % Standard value for number of points, changed later
plotstruct.scandef.xaxiscoord = 'AUTO';
guidata(gcf, plotstruct);
if threedim, createscanset; return; end % (in 3d mode, continue elsewhere)
% Creates a second set of axes in the figure
if isempty(plotstruct.scanaxeshandle)
createscanaxes;
end
% Set callback routine for mouse button
set(plotstruct.axeshandle,'ButtonDownFcn',@mouseclick1);
set(get(plotstruct.axeshandle,'Children'),'ButtonDownFcn',@mouseclick1);
end %preparescan
function prepareintegration(src,evnt)
plotstruct = guidata(gcf);
plotstruct.scandef.type = 'integration';
plotstruct.scandef.np = 20; % Standard value for number of points, changed later
plotstruct.scandef.xaxiscoord = 'AUTO';
guidata(gcf, plotstruct);
if threedim, createscanset; return; end % (in 3d mode, continue elsewhere)
% Creates a second set of axes in the figure
if isempty(plotstruct.scanaxeshandle)
createscanaxes;
end
% Set callback routine for mouse button
set(plotstruct.axeshandle,'ButtonDownFcn',@mouseclick2);
set(get(plotstruct.axeshandle,'Children'),'ButtonDownFcn',@mouseclick2);
end %prepareintegration
function prepareprojection(src,evnt)
% Creates a second set of axes in the figure
plotstruct = guidata(gcf);
plotstruct.scandef.type = 'projection';
plotstruct.scandef.xaxiscoord = 'AUTO';
guidata(gcf, plotstruct);
if isempty(plotstruct.scanaxeshandle)
createscanaxes;
end
% Set callback routine for mouse button
set(plotstruct.axeshandle,'ButtonDownFcn',@mouseclick2);
set(get(plotstruct.axeshandle,'Children'),'ButtonDownFcn',@mouseclick2);
end %prepareprojection
function createscanaxes
% Resize the figure window conveniently...
plotstruct = guidata(gcf);
scrsz = get(0,'ScreenSize'); % Screen size
figpos = get(gcf, 'Position'); % Figure window size
figpos(3) = min(scrsz(3), 2*figpos(3));
figpos(1) = min(figpos(1), scrsz(3)-figpos(3));
figpos(4) = figpos(3) / 2;
set(gcf, 'Position', figpos);
set(plotstruct.axeshandle,'Outerposition',[0,0,.5,.95]');
set(findobj('Tag','Titlebar2'),'Position',[0,.93,.5,.07]);
set(plotstruct.poshandle, 'Position', [.3, 0, .195, .05]);
drawnow;
% Create new axes for scan plot
plotstruct.scanaxeshandle = axes('OuterPosition',[.5,0,.5,.95]);
menuentry = uimenu('Label','Scan');
uimenu(menuentry, 'Label', 'Scan definition', 'Callback', @inputscandef);
uimenu(menuentry, 'Label', 'Set number of points', 'Callback', callback_np);
scanxaxis = uimenu(menuentry, 'Label', 'Choose coordinate for x-axis');
if strcmpi(plotstruct.type, 'qeplane')
uimenu(scanxaxis, 'Label', 'Automatic', 'Callback', callback_xax_auto);
uimenu(scanxaxis, 'Label', '|Q|', 'Callback', callback_xax_qm);
uimenu(scanxaxis, 'Label', 'En', 'Callback', callback_xax_en);
end
if strcmpi(plotstruct.type, 'qxy')
uimenu(scanxaxis, 'Label', 'Automatic (Qx/Qy)', 'Callback', callback_xax_auto);
uimenu(scanxaxis, 'Label', '|Q|', 'Callback', callback_xax_qm);
uimenu(scanxaxis, 'Label', 'Qx', 'Callback', callback_xax_qx);
uimenu(scanxaxis, 'Label', 'Qy', 'Callback', callback_xax_qy);
uimenu(scanxaxis, 'Label', 'H', 'Callback', callback_xax_qh);
uimenu(scanxaxis, 'Label', 'K', 'Callback', callback_xax_qk);
uimenu(scanxaxis, 'Label', 'L', 'Callback', callback_xax_ql);
end
uimenu(menuentry, 'Label', 'Fit Gauss (nfit)', 'Callback', @gaussfit, 'Separator', 'on');
uimenu(menuentry, 'Label', 'Fit Linear (nfit)', 'Callback', @linearfit);
uimenu(menuentry, 'Label', 'Open nfit window', 'Callback', @generalnfit);
scsavemenu = uimenu(menuentry, 'Label', 'Save scan data to file', 'Enable', 'off', 'Separator', 'on'); % Here, retain the handle to later enable it
uimenu(scsavemenu,'Label', 'Full format: (qx,qy,H,K,L,y,dy) + header', 'Callback', @savescan);
uimenu(scsavemenu,'Label', 'Simple format: as displayed (x, y, dy) ', 'Callback', callback_scansave);
scexportmen =uimenu(menuentry, 'Label', 'Export data to mfit window', 'Enable', 'off', 'Callback', @exportmfit);
uimenu(menuentry, 'Label', 'Replot', 'Callback', callcack_replotscan,'Separator', 'on' );
uimenu(menuentry, 'Label', 'Clear and reset window', 'Callback', @clearscan, 'Separator', 'on' );
% if strcmpi(plotstruct.type,'qeplane')
% set(findobj('Label','Scan definition', 'Parent', menuentry), 'Enable', 'off');
% end
% Save plotstruct
guidata(gcf,plotstruct);
end
%% Subfunction: Create a set of scans from multiple surfaces in 3D
function createscanset
% in 3d, the definition of a scan by mouse is not possible
% Go directly to the numerical input.
inputscandef([],[]); % (scan def. stored in plotstruct)
plotstruct = guidata(gcf);
if ~isfield(plotstruct.scandef,'xdat') || isempty(plotstruct.scandef.xdat) || ~isfield(plotstruct.scandef,'ydat') || isempty(plotstruct.scandef.ydat), return; end
[planedef.normal, planedef.c] = fithyperplane([ plotstruct.scandef.xdat(1), plotstruct.scandef.ydat(1), 0; ...
plotstruct.scandef.xdat(2), plotstruct.scandef.ydat(2), 0; ...
plotstruct.scandef.xdat(1), plotstruct.scandef.ydat(1), 1] );
% % The separate sclices have to be identified
% if ~isfield(plotstruct,'sectionlist') || isempty(plotstruct.sectionlist)
% [~,section] = finddistinctsections(plotstruct.datalist.faces);
% plotstruct.sectionlist = section;
% end
% section = plotstruct.sectionlist;
% nsecs = max(section);
% Initialize new data structure for result
newdata = plotstruct.datalist{1}; %.. on the basis of an existing, and delete contents
newdata.coordlist = [];
newdata.valuelist = [];
newdata.dataname = ['Separate ', plotstruct.scandef.type, ' of multiple datasets'];
newdata.raw = false;
if isfield(newdata,'delaunaytri'), newdata = rmfield(newdata,'delaunaytri'); end
newdata = rmfield(newdata,{'vertexlist','faces','monitorlist'});
if strcmpi(plotstruct.type,'qxyz')
newdata.coordtype = 'qxy';
% get vectors defining the new plane (cutplane)
newdata.constants = [newdata.constants, 'QVERT'];
newdata.QVERT = planedef.c;
avec = getplaneparameter([planedef.normal'; 0,0,1],[planedef.c;0]); % horizontal vector in cutplane
% ** try to make it point in right direction... ( to be generalized)
if (abs(avec(1))>abs(avec(2)) && avec(1)<0) || (abs(avec(2))>abs(avec(1)) && avec(2)<0), avec=-avec; end
bvec = cross(avec,planedef.normal);
UB = UBmatrix(plotstruct.sampleinfo.lattice, plotstruct.sampleinfo.ax, plotstruct.sampleinfo.bx );
newdata.sampleinfo.ax = makeinteger((UB\avec)',1e-4);
newdata.sampleinfo.bx = makeinteger((UB\bvec)',1e-4);
elseif strcmpi(plotstruct.type,'qxqyen'), newdata.coordtype = 'qeplane';
end
[stdgrid] = getoption('stdgrid'); stdgrid = stdgrid.(upper(plotstruct.type));
[ndata, ~, ~, pointinds, vertinds] = slicecounting(plotstruct.datalist);
% Principle (simplified): use first two coordinates, ignore third.
% ** Have to generalize this!
% ** Question: what to use as the x-axis??
% ** (at least for 3d-Q cuts this is not always ok!)
% For each of these slices
tempplotstruct = plotstruct;
tempplotstruct = rmfield(tempplotstruct,'datalist'); tempplotstruct.datalist{1} = plotstruct.datalist{1};
tempplotstruct.type = 'QXY';
% tempplotstruct.vertexlist = tempplotstruct.vertexlist(:,1:2);
if isfield(tempplotstruct.datalist{1},'delaunaytri'), tempplotstruct.datalist{1} = rmfield(tempplotstruct.datalist{1},'delaunaytri'); end
tempplotstruct.scandef.np = round(tempplotstruct.scandef.np / ndata); % ?
nonconstcount = 0;
for nsl=1:ndata
yval = plotstruct.coordlist(pointinds{nsl},3);
if max(yval)-min(yval) > stdgrid(3), nonconstcount = nonconstcount+1; end
yval = mean(yval);
tempplotstruct.datalist{1}.faces = plotstruct.datalist{nsl}.faces;
tempplotstruct.datalist{1}.coordlist = plotstruct.datalist{nsl}.coordlist;
tempplotstruct.datalist{1}.valuelist = plotstruct.datalist{nsl}.valuelist;
tempplotstruct.datalist{1}.monitorlist = plotstruct.datalist{nsl}.monitorlist;
tempplotstruct.coordlist = plotstruct.coordlist(pointinds{nsl},1:2);
tempplotstruct.datalist{1}.vertexlist = plotstruct.datalist{nsl}.vertexlist(:,1:2);
tempplotstruct.vertexlist = plotstruct.vertexlist(vertinds{nsl},1:2);
[scandata, xlabeltext] = makescandata(tempplotstruct);
validdata = isfinite(scandata.y);
newdata.coordlist = [newdata.coordlist; scandata.x(validdata), yval*ones(sum(validdata),1)];
newdata.valuelist = [newdata.valuelist; scandata.y(validdata), scandata.dy(validdata)];
end
if nonconstcount>0, fprintf('Warning: The third coordinate is not always constant. This is neglected in the calculation!\n'); end
% Add a this plane to plotstruct
planedef.properties = {'FaceColor','r','FaceAlpha',.35,'Tag','cutplane','ButtonDownFcn',@mouseclickonplane}; %** use options.m for this?
if ~isfield(plotstruct,'planedef'), plotstruct.planedef = {}; end
plotstruct.planedef = [plotstruct.planedef, planedef];
guidata(gcf, plotstruct);
doplot(plotstruct);
% ** Parameters to create the color plot?
fcplot({newdata},newdata.coordtype);
xlabel(xlabeltext);
end
%% Subfunction: Analyze input of Scan definition
function qxy = translateinput(text)
function errormessage
errordlg(['Format: In each line, give either two values in reciprocal Angstrom (for example: "qxy -2.5 3.2") or three HKL (ex.: "hkl 2 0 0").' ...
'For q-en plots, give two values for |q| (A-1) and En (meV), "qen 1.5 2.0".']);
end
qxy = [];
qvwarning = false;
for i = 1:length(text)
[st] = regexpi(text{i},'[A-z]*'); % Get text
if numel(st)~=1 || length(text{i})<(st+2), errormessage; return; end
switch upper(text{i}(st:(st+2)))
case 'QEN', coord='qen';
case 'QXY', coord='qxy';
case 'HKL', coord='hkl';
case 'THI', coord='thick'; if i<3, errormessage; return; end
otherwise, errormessage; return
end
[st,en] = regexpi(text{i},'-?\d*\.?\d*'); % Get numbers
switch coord
case {'qxy', 'qen'}
if numel(st)<2, errormessage; return; end % Need 2 numbers
qxy{i} = [str2double(text{i}(st(1):en(1))), str2double(text{i}(st(2):en(2)))]; %#ok<AGROW>
case 'hkl'
if numel(st)<3, errormessage; return; end % Need 3 numbers
hkl = [str2double(text{i}(st(1):en(1))), str2double(text{i}(st(2):en(2))), str2double(text{i}(st(3):en(3)))];
UB = UBmatrix( plotstruct.sampleinfo.lattice, plotstruct.sampleinfo.ax, plotstruct.sampleinfo.bx );
[xh, yh, zh] = calcQS(1,0,0, UB);
[xk, yk, zk] = calcQS(0,1,0, UB);
[xl, yl, zl] = calcQS(0,0,1, UB);
qxy{i} = [hkl(1)*xh + hkl(2)*xk + hkl(3)*xl, hkl(1)*yh + hkl(2)*yk + hkl(3)*yl]; %#ok<AGROW>
% ** Check if this is really ok **
% ** {1} !
maxdeviate = getoption('maxdeviate');
if isfield(plotstruct.datalist{1},'QVERT') && abs((hkl(1)*zh + hkl(2)*zk + hkl(3)*zl)-plotstruct.datalist{1}.QVERT) > maxdeviate.QVERT && ~qvwarning
warndlg('Attention: HKL entered are not consistent with vertical momentum transfer. Continue, but please check.','','modal');
qvwarning = true;
end
case 'thick'
if numel(st)<1, errormessage; return; end % Need 1 number
vec = qxy{2}-qxy{1};
qxy{i} = [qxy{1}(1), qxy{1}(2)] + [-vec(2), vec(1)] * str2double(text{i}(st(1):en(1)))/2/sqrt(sum(vec.^2)); %#ok<AGROW>
end
end
end %translateinput
%% Callback: Numerical Input of Scan Definition
function inputscandef(src,evnt)
plotstruct = guidata(gcf);
plotopt = getoption('plotopt');
if plotopt.preferHKL % use HKL instead Angstroms
UB = UBmatrix( plotstruct.sampleinfo.lattice, plotstruct.sampleinfo.ax, plotstruct.sampleinfo.bx );
try
[H1,K1,L1] = calcHKL(plotstruct.scandef.xdat(1), plotstruct.scandef.ydat(1), plotstruct.datalist.QVERT, UB);
[H2,K2,L2] = calcHKL(plotstruct.scandef.xdat(2), plotstruct.scandef.ydat(2), plotstruct.datalist.QVERT, UB);
H1=round(H1*1e4)/1e4; K1=round(K1*1e4)/1e4; L1=round(L1*1e4)/1e4; H2=round(H2*1e4)/1e4; K2=round(K2*1e4)/1e4; L2=round(L2*1e4)/1e4;
if any(strcmpi(plotstruct.scandef.type,{'Projection','Integration'}))
[H3,K3,L3] = calcHKL(plotstruct.scandef.xdat(1)+plotstruct.scandef.framevec(1), plotstruct.scandef.ydat(1)+plotstruct.scandef.framevec(2), plotstruct.datalist.QVERT, UB);
H3=round(H3*1e4)/1e4; K3=round(K3*1e4)/1e4; L3=round(L3*1e4)/1e4;
end
catch
plotopt.preferHKL = false;
end
end
if strcmpi(plotstruct.type, 'qxy'), stdcoord = 'qxy ';
elseif strcmpi(plotstruct.type, 'qeplane'), stdcoord = 'qen ';
elseif strcmpi(plotstruct.type,'qxqyen'), stdcoord = 'qxy';
end
if strcmpi(plotstruct.scandef.type,'Interpolation')
try
if plotopt.preferHKL && ~strcmpi(plotstruct.type, 'qeplane')
defaulttext = {['hkl ' num2str(H1) ' ' num2str(K1) ' ' num2str(L1)], ['hkl ' num2str(H2) ' ' num2str(K2) ' ' num2str(L2)] };
else
defaulttext = {[stdcoord num2str(plotstruct.scandef.xdat(1)) ' ' num2str(plotstruct.scandef.ydat(1))], ...
[stdcoord num2str(plotstruct.scandef.xdat(2)) ' ' num2str(plotstruct.scandef.ydat(2))] };
end
catch
defaulttext = {'', ''};
end
answ = inputdlg({'Start point', 'End point'}, 'Give scan definition', 1, defaulttext);
elseif any(strcmpi(plotstruct.scandef.type,{'Projection','Integration'}))
try
if plotopt.preferHKL && ~strcmpi(plotstruct.type, 'qeplane')
defaulttext = {['hkl ' num2str(H1) ' ' num2str(K1) ' ' num2str(L1)], ...
['hkl ' num2str(H2) ' ' num2str(K2) ' ' num2str(L2)], ...
['hkl ' num2str(H3) ' ' num2str(K3) ' ' num2str(L3)]};
else
defaulttext = {[stdcoord num2str(plotstruct.scandef.xdat(1)) ' ' num2str(plotstruct.scandef.ydat(1))], ...
[stdcoord num2str(plotstruct.scandef.xdat(2)) ' ' num2str(plotstruct.scandef.ydat(2))], ...
[stdcoord num2str(plotstruct.scandef.xdat(1)+plotstruct.scandef.framevec(1)) ' ' num2str(plotstruct.scandef.ydat(1)+plotstruct.scandef.framevec(2))],};
end
catch
defaulttext = {'', '', ''};
end
answ = inputdlg({'Start point', 'End point', 'Range: coord. of corner or thickness in axes units (ex.: "thick 0.5")'}, 'Give scan definition', 1, defaulttext);
end
plotstruct.scandef.xdat = [];
plotstruct.scandef.ydat = [];
if isempty(answ), return; end
qxy = translateinput(answ);
if isempty(qxy), return; end
% plotstruct.scandef.xdat = [qxy{1}(1), qxy{2}(1)];
% plotstruct.scandef.ydat = [qxy{1}(2), qxy{2}(2)];
if any(strcmpi(plotstruct.scandef.type,{'Projection','Integration'})), framevec = qxy{3}-qxy{1}; else framevec = [0,0]; end
if isfield(plotstruct,'qvert')
xydat = makeliststruct([qxy{1}; qxy{2}; framevec],[],'coordtype','qxy','sampleinfo',plotstruct.sampleinfo,'QVERT',plotstruct.qvert);
else
xydat = makeliststruct([qxy{1}; qxy{2}; framevec],[],'coordtype','qxy','sampleinfo',plotstruct.sampleinfo,'QVERT',0);
end
% Transform to figure coordinate system: ** make this more general.
% if hkl:
if strcmpi(plotstruct.type,'hklvectors'), xydat = coordtransform(xydat, 'hklvectors'); end
if isempty(xydat), return; end