0001 function varargout = mars_orthviews(action,varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150 global st;
0151
0152 if isempty(st), reset_st; end;
0153
0154 spm('Pointer','watch');
0155
0156 if nargin == 0, action = ''; end;
0157 action = lower(action);
0158
0159 switch lower(action),
0160 case 'image',
0161 H = specify_image(varargin{1});
0162 if ~isempty(H)
0163 if length(varargin)>=2, st.vols{H}.area = varargin{2}; end;
0164 if isempty(st.bb), st.bb = maxbb; end;
0165 bbox;
0166 redraw(H);
0167 end;
0168 varargout{1} = H;
0169
0170 case 'bb',
0171 if length(varargin)> 0 & all(size(varargin{1})==[2 3]), st.bb = varargin{1}; end;
0172 bbox;
0173 redraw_all;
0174
0175 case 'redraw',
0176 redraw_all;
0177 eval(st.callback);
0178 if isfield(st,'registry'),
0179 spm_XYZreg('SetCoords',st.centre,st.registry.hReg,st.registry.hMe);
0180 end;
0181
0182 case 'reposition',
0183 if length(varargin)<1, tmp = findcent;
0184 else, tmp = varargin{1}; end;
0185 if length(tmp)==3, st.centre = tmp(:); end;
0186 redraw_all;
0187 eval(st.callback);
0188 if isfield(st,'registry'),
0189 spm_XYZreg('SetCoords',st.centre,st.registry.hReg,st.registry.hMe);
0190 end;
0191
0192 case 'setcoords',
0193 st.centre = varargin{1};
0194 st.centre = st.centre(:);
0195 redraw_all;
0196 eval(st.callback);
0197
0198 case 'space',
0199 if length(varargin)<1,
0200 st.Space = eye(4);
0201 st.bb = maxbb;
0202 redraw_all;
0203 else,
0204 space(varargin{1});
0205 redraw_all;
0206 end;
0207 bbox;
0208
0209 case 'maxbb',
0210 st.bb = maxbb;
0211 bbox;
0212 redraw_all;
0213
0214 case 'resolution',
0215 resolution(varargin{1});
0216 bbox;
0217 redraw_all;
0218
0219 case 'window',
0220 if length(varargin)<2,
0221 win = 'auto';
0222 elseif length(varargin{2})==2,
0223 win = varargin{2};
0224 end;
0225 for i=valid_handles(varargin{1}),
0226 st.vols{i}.window = win;
0227 end;
0228 redraw(varargin{1});
0229
0230 case 'delete',
0231 my_delete(varargin{1});
0232
0233 case 'move',
0234 move(varargin{1},varargin{2});
0235
0236
0237 case 'reset',
0238 my_reset;
0239
0240 case 'pos',
0241 if isempty(varargin),
0242 H = st.centre(:);
0243 else,
0244 H = pos(varargin{1});
0245 end;
0246 varargout{1} = H;
0247
0248 case 'interp',
0249 st.hld = varargin{1};
0250 redraw_all;
0251
0252 case 'xhairs',
0253 xhairs(varargin{1});
0254
0255 case 'register',
0256 register(varargin{1});
0257
0258 case 'addblobs',
0259 addblobs(varargin{1}, varargin{2},varargin{3},varargin{4});
0260 redraw(varargin{1});
0261
0262 case 'addcolouredblobs',
0263 addcolouredblobs(varargin{1}, varargin{2},varargin{3},varargin{4},varargin{5});
0264
0265 case 'addcolouredmatrix',
0266 addcolouredmatrix(varargin{1}, varargin{2},varargin{3},varargin{4});
0267
0268 case 'addimage',
0269 addimage(varargin{1}, varargin{2});
0270 redraw(varargin{1});
0271
0272 case 'addcolouredimage',
0273 addcolouredimage(varargin{1}, varargin{2},varargin{3});
0274
0275 case 'addtruecolourimage',
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287 img_flt = mars_veropts('get_img_ext');
0288
0289 if nargin < 2
0290 varargin(1) = {1};
0291 end
0292 if nargin < 3
0293 varargin(2) = {spm_get(1, img_flt, 'Image with activation signal')};
0294 end
0295 if ischar(varargin{2}),varargin{2} = spm_vol(varargin{2});end
0296 if nargin < 4
0297 actc = [];
0298 while isempty(actc)
0299 actc = getcmap(spm_input('Colourmap for activation image', '+1','s'));
0300 end
0301 varargin(3) = {actc};
0302 end
0303 if nargin < 5
0304 varargin(4) = {0.4};
0305 end
0306 if nargin < 6
0307 varargin(5) = {max([eps maxval(varargin{2})])};
0308 end
0309 if nargin < 7
0310 varargin(6) = {min([0 minval(varargin{2})])};
0311 end
0312
0313 addtruecolourimage(varargin{1}, varargin{2},varargin{3}, varargin{4}, ...
0314 varargin{5}, varargin{6});
0315 redraw(varargin{1});
0316
0317 case 'rmblobs',
0318 rmblobs(varargin{1});
0319 redraw(varargin{1});
0320
0321 otherwise,
0322 warning('Unknown action string')
0323 end;
0324
0325 spm('Pointer');
0326 return;
0327
0328
0329
0330
0331 function addblobs(handle, xyz, t, mat)
0332 global st
0333 for i=valid_handles(handle),
0334 if ~isempty(xyz),
0335 rcp = round(xyz);
0336 dim = max(rcp,[],2)';
0337 off = rcp(1,:) + dim(1)*(rcp(2,:)-1 + dim(2)*(rcp(3,:)-1));
0338 vol = zeros(dim)+NaN;
0339 vol(off) = t;
0340 vol = reshape(vol,dim);
0341 st.vols{i}.blobs=cell(1,1);
0342 if st.mode == 0,
0343 axpos = get(st.vols{i}.ax{2}.ax,'Position');
0344 else,
0345 axpos = get(st.vols{i}.ax{1}.ax,'Position');
0346 end;
0347 ax = axes('Parent',st.fig,'Position',[(axpos(1)+axpos(3)+0.1) (axpos(2)+0.005) 0.05 (axpos(4)-0.01)],...
0348 'Box','on');
0349 mx = max([eps max(t)]);
0350 mn = min([0 min(t)]);
0351 image([0 1],[mn mx],[1:64]' + 64,'Parent',ax);
0352 set(ax,'YDir','normal','XTickLabel',[]);
0353 st.vols{i}.blobs{1} = struct('vol',vol,'mat',mat,'cbar',ax,'max',mx, 'min',mn);
0354 end;
0355 end;
0356 return;
0357
0358
0359 function addimage(handle, fname)
0360 global st
0361 for i=valid_handles(handle),
0362 vol = spm_vol(fname);
0363 mat = vol.mat;
0364 st.vols{i}.blobs=cell(1,1);
0365 if st.mode == 0,
0366 axpos = get(st.vols{i}.ax{2}.ax,'Position');
0367 else,
0368 axpos = get(st.vols{i}.ax{1}.ax,'Position');
0369 end;
0370 ax = axes('Parent',st.fig,'Position',[(axpos(1)+axpos(3)+0.05) (axpos(2)+0.005) 0.05 (axpos(4)-0.01)],...
0371 'Box','on');
0372 mx = max([eps maxval(vol)]);
0373 mn = min([0 minval(vol)]);
0374 image([0 1],[mn mx],[1:64]' + 64,'Parent',ax);
0375 set(ax,'YDir','normal','XTickLabel',[]);
0376 st.vols{i}.blobs{1} = struct('vol',vol,'mat',mat,'cbar',ax,'max',mx);
0377 end;
0378 return;
0379
0380
0381 function addcolouredblobs(handle, xyz, t, mat,colour)
0382 global st
0383 for i=valid_handles(handle),
0384 if ~isempty(xyz),
0385 rcp = round(xyz);
0386 dim = max(rcp,[],2)';
0387 off = rcp(1,:) + dim(1)*(rcp(2,:)-1 + dim(2)*(rcp(3,:)-1));
0388 vol = zeros(dim)+NaN;
0389 vol(off) = t;
0390 vol = reshape(vol,dim);
0391 if ~isfield(st.vols{i},'blobs'),
0392 st.vols{i}.blobs=cell(1,1);
0393 bset = 1;
0394 else,
0395 bset = length(st.vols{i}.blobs)+1;
0396 end;
0397 axpos = get(st.vols{i}.ax{2}.ax,'Position');
0398 mx = max([eps maxval(vol)]);
0399 mn = min([0 minval(vol)]);
0400 st.vols{i}.blobs{bset} = struct('vol',vol,'mat',mat,'max',mx,'min',mn,'colour',colour);
0401 end;
0402 end;
0403 return;
0404
0405
0406 function addcolouredmatrix(handle, vol, mat,colour)
0407 global st
0408 for i=valid_handles(handle),
0409 if ~isempty(vol),
0410 if ~isfield(st.vols{i},'blobs'),
0411 st.vols{i}.blobs=cell(1,1);
0412 bset = 1;
0413 else,
0414 bset = length(st.vols{i}.blobs)+1;
0415 end;
0416 axpos = get(st.vols{i}.ax{2}.ax,'Position');
0417 mx = max([eps maxval(vol)]);
0418 mn = min([0 minval(vol)]);
0419 st.vols{i}.blobs{bset} = struct('vol',vol,'mat',mat,'max',mx,'min',mn,'colour',colour);
0420 end;
0421 end;
0422 return;
0423
0424
0425 function addcolouredimage(handle, fname,colour)
0426 global st
0427 for i=valid_handles(handle),
0428
0429 vol = spm_vol(fname);
0430 mat = vol.mat;
0431 if ~isfield(st.vols{i},'blobs'),
0432 st.vols{i}.blobs=cell(1,1);
0433 bset = 1;
0434 else,
0435 bset = length(st.vols{i}.blobs)+1;
0436 end;
0437 axpos = get(st.vols{i}.ax{2}.ax,'Position');
0438 mx = max([eps maxval(vol)]);
0439 mn = min([0 minval(vol)]);
0440 st.vols{i}.blobs{bset} = struct('vol',vol,'mat',mat,'max',mx,'min',mn,'colour',colour);
0441 end;
0442 return;
0443
0444
0445 function addtruecolourimage(handle,vol,colourmap,prop,mx,mn)
0446
0447 global st
0448 mat = vol.mat;
0449 if isstruct(vol) & isfield(vol, 'vol'), vol = vol.vol;end
0450 for i=valid_handles(handle),
0451 if ~isfield(st.vols{i},'blobs'),
0452 st.vols{i}.blobs=cell(1,1);
0453 bset = 1;
0454 else,
0455 bset = length(st.vols{i}.blobs)+1;
0456 end;
0457
0458 c = struct('cmap', colourmap,'prop',prop);
0459 st.vols{i}.blobs{bset} = struct('vol',vol,'mat',mat,'max',mx,'min',mn,'colour',c);
0460 end;
0461 return;
0462
0463
0464 function rmblobs(handle)
0465 global st
0466 for i=valid_handles(handle),
0467 if isfield(st.vols{i},'blobs'),
0468 for j=1:length(st.vols{i}.blobs),
0469 if isfield(st.vols{i}.blobs{j},'cbar') & ishandle(st.vols{i}.blobs{j}.cbar),
0470 delete(st.vols{i}.blobs{j}.cbar);
0471 end;
0472 end;
0473 st.vols{i} = rmfield(st.vols{i},'blobs');
0474 end;
0475 end;
0476 return;
0477
0478
0479 function register(hreg)
0480 global st
0481 tmp = uicontrol('Position',[0 0 1 1],'Visible','off','Parent',st.fig);
0482 h = valid_handles(1:24);
0483 if ~isempty(h),
0484 tmp = st.vols{h(1)}.ax{1}.ax;
0485 st.registry = struct('hReg',hreg,'hMe', tmp);
0486 spm_XYZreg('Add2Reg',st.registry.hReg,st.registry.hMe, 'mars_orthviews');
0487 else,
0488 warning('Nothing to register with');
0489 end;
0490 st.centre = spm_XYZreg('GetCoords',st.registry.hReg);
0491 st.centre = st.centre(:);
0492 return;
0493
0494
0495 function xhairs(arg1),
0496 global st
0497 st.xhairs = 0;
0498 opt = 'on';
0499 if ~strcmp(arg1,'on'),
0500 opt = 'off';
0501 else,
0502 st.xhairs = 1;
0503 end;
0504 for i=valid_handles(1:24),
0505 for j=1:3,
0506 set(st.vols{i}.ax{j}.lx,'Visible',opt);
0507 set(st.vols{i}.ax{j}.ly,'Visible',opt);
0508 end;
0509 end;
0510 return;
0511
0512
0513 function H = pos(arg1)
0514 global st
0515 H = [];
0516 for arg1=valid_handles(arg1),
0517 is = inv(st.vols{arg1}.premul*st.vols{arg1}.mat);
0518 H = is(1:3,1:3)*st.centre(:) + is(1:3,4);
0519 end;
0520 return;
0521
0522
0523 function my_reset
0524 global st
0525 if ~isempty(st) & isfield(st,'registry') & ishandle(st.registry.hMe),
0526 delete(st.registry.hMe); st = rmfield(st,'registry');
0527 end;
0528 my_delete(1:24);
0529 reset_st;
0530 return;
0531
0532
0533 function my_delete(arg1)
0534 global st
0535 for i=valid_handles(arg1),
0536 kids = get(st.fig,'Children');
0537 for j=1:3,
0538 if any(kids == st.vols{i}.ax{j}.ax),
0539 set(get(st.vols{i}.ax{j}.ax,'Children'),'DeleteFcn','');
0540 delete(st.vols{i}.ax{j}.ax);
0541 end;
0542 end;
0543 st.vols{i} = [];
0544 end;
0545 return;
0546
0547
0548 function resolution(arg1)
0549 global st
0550 res = arg1/mean(svd(st.Space(1:3,1:3)));
0551 Mat = diag([res res res 1]);
0552 st.Space = st.Space*Mat;
0553 st.bb = st.bb/res;
0554 return;
0555
0556
0557 function move(handle,pos)
0558 global st
0559 for handle = valid_handles(handle),
0560 st.vols{handle}.area = pos;
0561 end;
0562 bbox;
0563
0564 return;
0565
0566
0567 function bb = maxbb
0568 global st
0569 mn = [Inf Inf Inf];
0570 mx = -mn;
0571 for i=valid_handles(1:24),
0572 bb = [[1 1 1];st.vols{i}.dim(1:3)];
0573 c = [ bb(1,1) bb(1,2) bb(1,3) 1
0574 bb(1,1) bb(1,2) bb(2,3) 1
0575 bb(1,1) bb(2,2) bb(1,3) 1
0576 bb(1,1) bb(2,2) bb(2,3) 1
0577 bb(2,1) bb(1,2) bb(1,3) 1
0578 bb(2,1) bb(1,2) bb(2,3) 1
0579 bb(2,1) bb(2,2) bb(1,3) 1
0580 bb(2,1) bb(2,2) bb(2,3) 1]';
0581 tc = st.Space\(st.vols{i}.premul*st.vols{i}.mat)*c;
0582 tc = tc(1:3,:)';
0583 mx = max([tc ; mx]);
0584 mn = min([tc ; mn]);
0585 end;
0586 bb = [mn ; mx];
0587 return;
0588
0589
0590 function space(arg1)
0591 global st
0592 if ~isempty(st.vols{arg1})
0593 num = arg1;
0594 Mat = st.vols{num}.premul(1:3,1:3)*st.vols{num}.mat(1:3,1:3);
0595 Mat = diag([sqrt(sum(Mat.^2)) 1]);
0596 Space = (st.vols{num}.mat)/Mat;
0597 bb = [1 1 1;st.vols{num}.dim(1:3)];
0598 bb = [bb [1;1]];
0599 bb=bb*Mat';
0600 bb=bb(:,1:3);
0601 st.Space = Space;
0602 st.bb = bb;
0603 end;
0604 return;
0605
0606
0607 function H = specify_image(arg1, arg2)
0608 global st
0609 H=[];
0610 ok = 1;
0611 eval('V = spm_vol(arg1);','ok=0;');
0612 if ok == 0,
0613 fprintf('Can not use image "%s"\n', arg1);
0614 return;
0615 end;
0616
0617 ii = 1;
0618 while ~isempty(st.vols{ii}), ii = ii + 1; end;
0619
0620 DeleteFcn = ['mars_orthviews(''Delete'',' num2str(ii) ');'];
0621 V.ax = cell(3,1);
0622 for i=1:3,
0623 ax = axes('Visible','off','DrawMode','fast','Parent',st.fig,'DeleteFcn',DeleteFcn,...
0624 'YDir','normal');
0625 d = image(0,'Tag','Transverse','Parent',ax,...
0626 'DeleteFcn',DeleteFcn);
0627 set(ax,'Ydir','normal');
0628 lx = line(0,0,'Parent',ax,'DeleteFcn',DeleteFcn);
0629 ly = line(0,0,'Parent',ax,'DeleteFcn',DeleteFcn);
0630 if ~st.xhairs,
0631 set(lx,'Visible','off');
0632 set(ly,'Visible','off');
0633 end;
0634 V.ax{i} = struct('ax',ax,'d',d,'lx',lx,'ly',ly);
0635 end;
0636 V.premul = eye(4);
0637 V.window = 'auto';
0638 st.vols{ii} = V;
0639
0640 H = ii;
0641 return;
0642
0643
0644 function bbox
0645 global st
0646 Dims = diff(st.bb)'+1;
0647
0648 TD = Dims([1 2])';
0649 CD = Dims([1 3])';
0650 if st.mode == 0, SD = Dims([3 2])'; else, SD = Dims([2 3])'; end;
0651
0652 un = get(st.fig,'Units');set(st.fig,'Units','Pixels');sz=get(st.fig,'Position');set(st.fig,'Units',un);
0653 sz = sz(3:4);
0654 sz(2) = sz(2)-40;
0655
0656 for i=valid_handles(1:24),
0657 area = st.vols{i}.area(:);
0658 area = [area(1)*sz(1) area(2)*sz(2) area(3)*sz(1) area(4)*sz(2)];
0659 if st.mode == 0,
0660 sx = area(3)/(Dims(1)+Dims(3))/1.02;
0661 else,
0662 sx = area(3)/(Dims(1)+Dims(2))/1.02;
0663 end;
0664 sy = area(4)/(Dims(2)+Dims(3))/1.02;
0665 s = min([sx sy]);
0666
0667 offy = (area(4)-(Dims(2)+Dims(3))*1.02*s)/2 + area(2);
0668 sky = s*(Dims(2)+Dims(3))*0.02;
0669 if st.mode == 0,
0670 offx = (area(3)-(Dims(1)+Dims(3))*1.02*s)/2 + area(1);
0671 skx = s*(Dims(1)+Dims(3))*0.02;
0672 else,
0673 offx = (area(3)-(Dims(1)+Dims(2))*1.02*s)/2 + area(1);
0674 skx = s*(Dims(1)+Dims(2))*0.02;
0675 end;
0676
0677 DeleteFcn = ['mars_orthviews(''Delete'',' num2str(i) ');'];
0678
0679
0680 set(st.vols{i}.ax{1}.ax,'Units','pixels', ...
0681 'Position',[offx offy s*Dims(1) s*Dims(2)],...
0682 'Units','normalized','Xlim',[0 TD(1)]+0.5,'Ylim',[0 TD(2)]+0.5,...
0683 'Visible','on','XTick',[],'YTick',[]);
0684
0685
0686 set(st.vols{i}.ax{2}.ax,'Units','Pixels',...
0687 'Position',[offx offy+s*Dims(2)+sky s*Dims(1) s*Dims(3)],...
0688 'Units','normalized','Xlim',[0 CD(1)]+0.5,'Ylim',[0 CD(2)]+0.5,...
0689 'Visible','on','XTick',[],'YTick',[]);
0690
0691
0692 if st.mode == 0,
0693 set(st.vols{i}.ax{3}.ax,'Units','Pixels', 'Box','on',...
0694 'Position',[offx+s*Dims(1)+skx offy s*Dims(3) s*Dims(2)],...
0695 'Units','normalized','Xlim',[0 SD(1)]+0.5,'Ylim',[0 SD(2)]+0.5,...
0696 'Visible','on','XTick',[],'YTick',[]);
0697 else,
0698 set(st.vols{i}.ax{3}.ax,'Units','Pixels', 'Box','on',...
0699 'Position',[offx+s*Dims(1)+skx offy+s*Dims(2)+sky s*Dims(2) s*Dims(3)],...
0700 'Units','normalized','Xlim',[0 SD(1)]+0.5,'Ylim',[0 SD(2)]+0.5,...
0701 'Visible','on','XTick',[],'YTick',[]);
0702 end;
0703 end;
0704 return;
0705
0706
0707 function redraw_all
0708 global st
0709 redraw(1:24);
0710 return;
0711
0712 function mx = maxval(vol)
0713 if isstruct(vol) & isfield(vol, 'vol'), vol = vol.vol;end
0714 if isstruct(vol),
0715 mx = -Inf;
0716 for i=1:vol.dim(3),
0717 tmp = spm_slice_vol(vol,spm_matrix([0 0 i]),vol.dim(1:2),0);
0718 imx = max(tmp(find(isfinite(tmp))));
0719 if ~isempty(imx),mx = max(mx,imx);end
0720 end;
0721 else,
0722 mx = max(vol(find(isfinite(vol))));
0723 end;
0724
0725 function mn = minval(vol)
0726 if isstruct(vol) & isfield(vol, 'vol'), vol = vol.vol;end
0727 if isstruct(vol),
0728 mn = Inf;
0729 for i=1:vol.dim(3),
0730 tmp = spm_slice_vol(vol,spm_matrix([0 0 i]),vol.dim(1:2),0);
0731 imn = min(tmp(find(isfinite(tmp))));
0732 if ~isempty(imn),mn = min(mn,imn);end
0733 end;
0734 else,
0735 mn = min(vol(find(isfinite(vol))));
0736 end;
0737
0738
0739
0740 function redraw(arg1)
0741 global st
0742 bb = st.bb;
0743 Dims = diff(bb)'+1;
0744 is = inv(st.Space);
0745 cent = is(1:3,1:3)*st.centre(:) + is(1:3,4);
0746
0747 for i = valid_handles(arg1),
0748 M = st.vols{i}.premul*st.vols{i}.mat;
0749 TM0 = [ 1 0 0 -bb(1,1)+1
0750 0 1 0 -bb(1,2)+1
0751 0 0 1 -cent(3)
0752 0 0 0 1];
0753 TM = inv(TM0*(st.Space\M));
0754 TD = Dims([1 2]);
0755
0756 CM0 = [ 1 0 0 -bb(1,1)+1
0757 0 0 1 -bb(1,3)+1
0758 0 1 0 -cent(2)
0759 0 0 0 1];
0760 CM = inv(CM0*(st.Space\M));
0761 CD = Dims([1 3]);
0762
0763 if st.mode ==0,
0764 SM0 = [ 0 0 1 -bb(1,3)+1
0765 0 1 0 -bb(1,2)+1
0766 1 0 0 -cent(1)
0767 0 0 0 1];
0768 SM = inv(SM0*(st.Space\M)); SD = Dims([3 2]);
0769 else,
0770 SM0 = [ 0 1 0 -bb(1,2)+1
0771 0 0 1 -bb(1,3)+1
0772 1 0 0 -cent(1)
0773 0 0 0 1];
0774 SM0 = [ 0 -1 0 +bb(2,2)+1
0775 0 0 1 -bb(1,3)+1
0776 1 0 0 -cent(1)
0777 0 0 0 1];
0778 SM = inv(SM0*(st.Space\M));
0779 SD = Dims([2 3]);
0780 end;
0781
0782 ok=1;
0783 eval('imgt = (spm_slice_vol(st.vols{i},TM,TD,st.hld))'';','ok=0;');
0784 eval('imgc = (spm_slice_vol(st.vols{i},CM,CD,st.hld))'';','ok=0;');
0785 eval('imgs = (spm_slice_vol(st.vols{i},SM,SD,st.hld))'';','ok=0;');
0786 if (ok==0), fprintf('Image "%s" can not be resampled\n', st.vols{i}.fname);
0787 else,
0788 if strcmp(st.vols{i}.window,'auto'),
0789 mx = -Inf; mn = Inf;
0790 if ~isempty(imgt),
0791 mx = max([mx max(max(imgt))]);
0792 mn = min([mn min(min(imgt))]);
0793 end;
0794 if ~isempty(imgc),
0795 mx = max([mx max(max(imgc))]);
0796 mn = min([mn min(min(imgc))]);
0797 end;
0798 if ~isempty(imgs),
0799 mx = max([mx max(max(imgs))]);
0800 mn = min([mn min(min(imgs))]);
0801 end;
0802 if mx==mn, mx=mn+eps; end;
0803 else,
0804 mx = st.vols{i}.window(2);
0805 mn = st.vols{i}.window(1);
0806 r=min([mn mx]);imgt = max(imgt,r); r=max([mn mx]);imgt = min(imgt,r);
0807 r=min([mn mx]);imgc = max(imgc,r); r=max([mn mx]);imgc = min(imgc,r);
0808 r=min([mn mx]);imgs = max(imgs,r); r=max([mn mx]);imgs = min(imgs,r);
0809 end;
0810
0811 if isfield(st.vols{i},'blobs'),
0812 if ~isfield(st.vols{i}.blobs{1},'colour'),
0813
0814 scal = 64/(mx-mn);
0815 dcoff = -mn*scal;
0816 imgt = imgt*scal+dcoff;
0817 imgc = imgc*scal+dcoff;
0818 imgs = imgs*scal+dcoff;
0819
0820 vol = st.vols{i}.blobs{1}.vol;
0821 mat = st.vols{i}.premul*st.vols{i}.blobs{1}.mat;
0822 if isfield(st.vols{i}.blobs{1},'max'),
0823 mx = st.vols{i}.blobs{1}.max;
0824 else,
0825 mx = max([eps maxval(st.vols{i}.blobs{1}.vol)]);
0826 st.vols{i}.blobs{1}.max = mx;
0827 end;
0828 if isfield(st.vols{i}.blobs{1},'min'),
0829 mn = st.vols{i}.blobs{1}.min;
0830 else,
0831 mn = min([0 minval(st.vols{i}.blobs{1}.vol)]);
0832 st.vols{i}.blobs{1}.min = mn;
0833 end;
0834
0835 vol = st.vols{i}.blobs{1}.vol;
0836 M = st.vols{i}.premul*st.vols{i}.blobs{1}.mat;
0837 tmpt = spm_slice_vol(vol,inv(TM0*(st.Space\M)),TD,[0 NaN])';
0838 tmpc = spm_slice_vol(vol,inv(CM0*(st.Space\M)),CD,[0 NaN])';
0839 tmps = spm_slice_vol(vol,inv(SM0*(st.Space\M)),SD,[0 NaN])';
0840
0841 sc = 64/(mx-mn);
0842 off = 65.51-mn*sc;
0843 msk = find(isfinite(tmpt)); imgt(msk) = off+tmpt(msk)*sc;
0844 msk = find(isfinite(tmpc)); imgc(msk) = off+tmpc(msk)*sc;
0845 msk = find(isfinite(tmps)); imgs(msk) = off+tmps(msk)*sc;
0846
0847 cmap = get(st.fig,'Colormap');
0848 if size(cmap,1)~=128
0849 figure(st.fig)
0850 spm_figure('Colormap','gray-hot')
0851 end;
0852 elseif isstruct(st.vols{i}.blobs{1}.colour),
0853
0854
0855
0856
0857 gryc = [0:63]'*ones(1,3)/63;
0858 actc = ...
0859 st.vols{1}.blobs{1}.colour.cmap;
0860 actp = ...
0861 st.vols{1}.blobs{1}.colour.prop;
0862
0863
0864 imgt = scaletocmap(imgt,mn,mx,gryc,65);
0865 imgc = scaletocmap(imgc,mn,mx,gryc,65);
0866 imgs = scaletocmap(imgs,mn,mx,gryc,65);
0867 gryc = [gryc; 0 0 0];
0868
0869
0870 vol = st.vols{i}.blobs{1}.vol;
0871 mat = st.vols{i}.premul*st.vols{i}.blobs{1}.mat;
0872 if isfield(st.vols{i}.blobs{1},'max'),
0873 cmx = st.vols{i}.blobs{1}.max;
0874 else,
0875 cmx = max([eps maxval(st.vols{i}.blobs{1}.vol)]);
0876 end;
0877
0878
0879 vol = st.vols{i}.blobs{1}.vol;
0880 M = st.vols{i}.premul*st.vols{i}.blobs{1}.mat;
0881 tmpt = spm_slice_vol(vol,inv(TM0*(st.Space\M)),TD,[0 NaN])';
0882 tmpc = spm_slice_vol(vol,inv(CM0*(st.Space\M)),CD,[0 NaN])';
0883 tmps = spm_slice_vol(vol,inv(SM0*(st.Space\M)),SD,[0 NaN])';
0884
0885
0886 topc = size(actc,1)+1;
0887 tmpt = scaletocmap(tmpt,-cmx,cmx,actc,topc);
0888 tmpc = scaletocmap(tmpc,-cmx,cmx,actc,topc);
0889 tmps = scaletocmap(tmps,-cmx,cmx,actc,topc);
0890 actc = [actc; 0 0 0];
0891
0892
0893
0894 imgt = reshape(actc(tmpt(:),:)*actp+ ...
0895 gryc(imgt(:),:)*(1-actp), ...
0896 [size(imgt) 3]);
0897 imgc = reshape(actc(tmpc(:),:)*actp+ ...
0898 gryc(imgc(:),:)*(1-actp), ...
0899 [size(imgc) 3]);
0900 imgs = reshape(actc(tmps(:),:)*actp+ ...
0901 gryc(imgs(:),:)*(1-actp), ...
0902 [size(imgs) 3]);
0903
0904
0905 else,
0906
0907 scal = 1/(mx-mn);
0908 dcoff = -mn*scal;
0909 imgt = repmat(imgt*scal+dcoff,[1,1,3]);
0910 imgc = repmat(imgc*scal+dcoff,[1,1,3]);
0911 imgs = repmat(imgs*scal+dcoff,[1,1,3]);
0912
0913 for j=1:length(st.vols{i}.blobs),
0914 vol = st.vols{i}.blobs{j}.vol;
0915 mat = st.vols{i}.premul*st.vols{i}.blobs{j}.mat;
0916 if isfield(st.vols{i}.blobs{j},'colour'),
0917 colour = st.vols{i}.blobs{j}.colour;
0918 else,
0919 colour = [1 0 0];
0920 end;
0921 if isfield(st.vols{i}.blobs{j},'max'),
0922 mx = st.vols{i}.blobs{j}.max;
0923 else,
0924 mx = max([eps max(st.vols{i}.blobs{j}.vol(:))]);
0925 st.vols{i}.blobs{j}.max = mx;
0926 end;
0927 if isfield(st.vols{i}.blobs{j},'min'),
0928 mn = st.vols{i}.blobs{j}.min;
0929 else,
0930 mn = min([0 min(st.vols{i}.blobs{j}.vol(:))]);
0931 st.vols{i}.blobs{j}.min = mn;
0932 end;
0933
0934 vol = st.vols{i}.blobs{j}.vol;
0935 M = st.vols{i}.premul*st.vols{i}.blobs{j}.mat;
0936 tmpt = spm_slice_vol(vol,inv(TM0*(st.Space\M)),TD,[0 NaN])'/(mx-mn)+mn;
0937 tmpc = spm_slice_vol(vol,inv(CM0*(st.Space\M)),CD,[0 NaN])'/(mx-mn)+mn;
0938 tmps = spm_slice_vol(vol,inv(SM0*(st.Space\M)),SD,[0 NaN])'/(mx-mn)+mn;
0939 tmpt(find(~isfinite(tmpt))) = 0;
0940 tmpc(find(~isfinite(tmpc))) = 0;
0941 tmps(find(~isfinite(tmps))) = 0;
0942
0943 tmp = cat(3,tmpt*colour(1),tmpt*colour(2),tmpt*colour(3));
0944 imgt = (repmat(1-tmpt,[1 1 3]).*imgt+tmp);
0945 tmp = find(imgt<0); imgt(tmp)=0; tmp = find(imgt>1); imgt(tmp)=1;
0946
0947 tmp = cat(3,tmpc*colour(1),tmpc*colour(2),tmpc*colour(3));
0948 imgc = (repmat(1-tmpc,[1 1 3]).*imgc+tmp);
0949 tmp = find(imgc<0); imgc(tmp)=0; tmp = find(imgc>1); imgc(tmp)=1;
0950
0951 tmp = cat(3,tmps*colour(1),tmps*colour(2),tmps*colour(3));
0952 imgs = (repmat(1-tmps,[1 1 3]).*imgs+tmp);
0953 tmp = find(imgs<0); imgs(tmp)=0; tmp = find(imgs>1); imgs(tmp)=1;
0954 end;
0955 end;
0956 else,
0957 scal = 64/(mx-mn);
0958 dcoff = -mn*scal;
0959 imgt = imgt*scal+dcoff;
0960 imgc = imgc*scal+dcoff;
0961 imgs = imgs*scal+dcoff;
0962 end;
0963
0964 callback = 'mars_orthviews(''Reposition'');';
0965
0966 set(st.vols{i}.ax{1}.d,'ButtonDownFcn',callback, 'Cdata',imgt);
0967 set(st.vols{i}.ax{1}.lx,'ButtonDownFcn',callback,...
0968 'Xdata',[0 TD(1)]+0.5,'Ydata',[1 1]*(cent(2)-bb(1,2)+1));
0969 set(st.vols{i}.ax{1}.ly,'ButtonDownFcn',callback,...
0970 'Ydata',[0 TD(2)]+0.5,'Xdata',[1 1]*(cent(1)-bb(1,1)+1));
0971
0972 set(st.vols{i}.ax{2}.d,'ButtonDownFcn',callback, 'Cdata',imgc);
0973 set(st.vols{i}.ax{2}.lx,'ButtonDownFcn',callback,...
0974 'Xdata',[0 CD(1)]+0.5,'Ydata',[1 1]*(cent(3)-bb(1,3)+1));
0975 set(st.vols{i}.ax{2}.ly,'ButtonDownFcn',callback,...
0976 'Ydata',[0 CD(2)]+0.5,'Xdata',[1 1]*(cent(1)-bb(1,1)+1));
0977
0978 set(st.vols{i}.ax{3}.d,'ButtonDownFcn',callback,'Cdata',imgs);
0979 if st.mode ==0,
0980 set(st.vols{i}.ax{3}.lx,'ButtonDownFcn',callback,...
0981 'Xdata',[0 SD(1)]+0.5,'Ydata',[1 1]*(cent(2)-bb(1,2)+1));
0982 set(st.vols{i}.ax{3}.ly,'ButtonDownFcn',callback,...
0983 'Ydata',[0 SD(2)]+0.5,'Xdata',[1 1]*(cent(3)-bb(1,3)+1));
0984 else,
0985 set(st.vols{i}.ax{3}.lx,'ButtonDownFcn',callback,...
0986 'Xdata',[0 SD(1)]+0.5,'Ydata',[1 1]*(cent(3)-bb(1,3)+1));
0987 set(st.vols{i}.ax{3}.ly,'ButtonDownFcn',callback,...
0988 'Ydata',[0 SD(2)]+0.5,'Xdata',[1 1]*(bb(2,2)+1-cent(2)));
0989 end;
0990 end;
0991 end;
0992 drawnow;
0993 return;
0994
0995
0996 function centre = findcent
0997 global st
0998 obj = get(st.fig,'CurrentObject');
0999 centre = [];
1000 cent = [];
1001 cp = [];
1002 for i=valid_handles(1:24),
1003 for j=1:3,
1004 if ~isempty(obj),
1005 if any([st.vols{i}.ax{j}.d ...
1006 st.vols{i}.ax{j}.lx ...
1007 st.vols{i}.ax{j}.ly]== obj)
1008 cp = get(get(obj,'Parent'),'CurrentPoint');
1009 elseif (st.vols{i}.ax{j}.ax == obj),
1010 cp = get(obj,'CurrentPoint');
1011 end;
1012 end;
1013 if ~isempty(cp),
1014 cp = cp(1,1:2);
1015 is = inv(st.Space);
1016 cent = is(1:3,1:3)*st.centre(:) + is(1:3,4);
1017 switch j,
1018 case 1,
1019 cent([1 2])=[cp(1)+st.bb(1,1)-1 cp(2)+st.bb(1,2)-1];
1020 case 2,
1021 cent([1 3])=[cp(1)+st.bb(1,1)-1 cp(2)+st.bb(1,3)-1];
1022 case 3,
1023 if st.mode ==0,
1024 cent([3 2])=[cp(1)+st.bb(1,3)-1 cp(2)+st.bb(1,2)-1];
1025 else,
1026 cent([2 3])=[st.bb(2,2)+1-cp(1) cp(2)+st.bb(1,3)-1];
1027 end;
1028 end;
1029 break;
1030 end;
1031 end;
1032 if ~isempty(cent), break; end;
1033 end;
1034 if ~isempty(cent), centre = st.Space(1:3,1:3)*cent(:) + st.Space(1:3,4); end;
1035 return;
1036
1037
1038 function handles = valid_handles(handles)
1039 global st;
1040 handles = handles(:)';
1041 handles = handles(find(handles<=24 & handles>=1 & ~rem(handles,1)));
1042 for h=handles,
1043 if isempty(st.vols{h}), handles(find(handles==h))=[]; end;
1044 end;
1045 return;
1046
1047
1048 function reset_st
1049 global st
1050 fig = spm_figure('FindWin','Graphics');
1051 bb = [ [-78 78]' [-112 76]' [-50 85]' ];
1052 st = struct('n', 0, 'vols',[], 'bb',bb,'Space',eye(4),'centre',[0 0 0],'callback',';','xhairs',1,'hld',1,'fig',fig,'mode',1);
1053 st.vols = cell(24,1);
1054 return;
1055
1056
1057 function img = scaletocmap(inpimg,mn,mx,cmap,miscol)
1058 if nargin < 5, miscol=1;end
1059 cml = size(cmap,1);
1060 scf = (cml-1)/(mx-mn);
1061 img = round((inpimg-mn)*scf)+1;
1062 img(find(img<1))=1;
1063 img(find(img>cml))=cml;
1064 img(~isfinite(img)) = miscol;
1065
1066
1067 function cmap = getcmap(acmapname)
1068
1069 if ~isempty(acmapname)
1070 cmap = evalin('base',acmapname,'[]');
1071 if isempty(cmap)
1072 [p f e] = fileparts(acmapname);
1073 acmat = fullfile(p, [f '.mat']);
1074 if exist(acmat, 'file')
1075 s = struct2cell(load(acmat));
1076 cmap = s{1};
1077 end
1078 end
1079 end
1080 if size(cmap, 2)~=3
1081 warning('Colormap was not an N by 3 matrix')
1082 cmap = [];
1083 end
1084 return