NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/gliderproc/trunk/MATLAB/plots/prstuff/zoomp.m

Revision 495 (checked in by cbc, 12 years ago)

Initial import of Stark code.

Line 
1 function zoomp(m)
2 % ZOOM  Zoom in and out on a 2-D plot.
3 %       ZOOM ON turns zoom on for the current figure.  Click
4 %       the left mouse button to zoom in on the point under the
5 %       mouse.  Click the right mouse button to zoom out
6 %       (shift-click on the Macintosh).  Each time you click,
7 %       the axes limits will be changed by a factor of 2 (in or out).
8 %       You can also click and drag to zoom into an area.
9 %       
10 %       ZOOM OFF turns zoom off. ZOOM with no arguments
11 %       toggles the zoom status.  ZOOM OUT returns the plot
12 %       to its initial (full) zoom.
13
14 %       Clay M. Thompson 1-25-93
15 %       Revised 11 Jan 94 by Steven L. Eddins
16 %       Copyright (c) 1984-94 by The MathWorks, Inc.
17 %       $Revision: 1.10 $  $Date: 1994/02/14 17:00:34 $
18
19 %       Note: zoom uses the userdata of the zlabel of the axis and
20 %       the figure buttondown and buttonmotion functions
21
22 %       Modification so that this version of zoom works with Prmenu
23
24 if any(get(gca,'view')~=[0 90]), error('Only works for 2-D plots'); end
25
26 getlimits = 1; rbbox_mode = 0;
27 if nargin==0, % Toggle buttondown function
28    if strcmp(get(gcf,'windowbuttondownfcn'),'zoomp(''down'')'),
29       set(gcf,'windowbuttondownfcn','lastouch',...
30           'windowbuttonupfcn','', ...
31           'windowbuttonmotionfcn','');
32    else
33       set(gcf,'windowbuttondownfcn','zoomp(''down'')', ...
34               'windowbuttonupfcn','1;', ...
35               'windowbuttonmotionfcn','', ...
36               'interruptible','yes');
37       set(gca,'interruptible','yes')
38       figure(gcf)
39    end
40    return
41
42 elseif nargin==1, % Process call backs
43   if isstr(m),
44     m = lower(m);
45     if strcmp(m,'down'),
46       % Activate axis that is clicked in
47       ax = get(gcf,'Children');
48       ZOOM_found = 0;
49       for i=1:length(ax),
50         if strcmp(get(ax(i),'Type'),'axes'),
51           ZOOM_Pt1 = get(ax(i),'CurrentPoint');
52           xlim = get(ax(i),'XLim');
53           ylim = get(ax(i),'YLim');
54           if (xlim(1) <= ZOOM_Pt1(1,1) & ZOOM_Pt1(1,1) <= xlim(2) & ...
55               ylim(1) <= ZOOM_Pt1(1,2) & ZOOM_Pt1(1,2) <= ylim(2))
56             ZOOM_found = 1;
57             axes(ax(i))
58             break
59           end
60         end
61       end
62
63       if ZOOM_found==0, return, end
64
65       % Check for selection type
66       selection_type = get(gcf,'SelectionType');
67       if (strcmp(selection_type, 'normal'))
68         % Zoom in
69         m = 1;
70       elseif (strcmp(selection_type, 'open'))
71         % Zoom all the way out
72         zoomp('out');
73         return;
74       else
75         % Zoom partially out
76         m = -1;
77       end
78      
79       ZOOM_Pt1 = get(gca,'currentpoint');
80       ZOOM_Pt2 = ZOOM_Pt1;
81       center = ZOOM_Pt1(1,1:2);
82      
83       if (m == 1)
84         % Zoom in
85         rbbox([get(gcf,'currentpoint') 0 0],get(gcf,'currentpoint'))
86         ZOOM_Pt2 = get(gca,'currentpoint');
87
88         % Note the currenpoint is set by having a non-trivial up function.
89         if min(abs(ZOOM_Pt1(1,1:2)-ZOOM_Pt2(1,1:2))) >= ...
90               min(.01*[diff(get(gca,'xlim')) diff(get(gca,'ylim'))]),
91           % determine axis from rbbox
92           a = [ZOOM_Pt1(1,1:2);ZOOM_Pt2(1,1:2)]; a = [min(a);max(a)];
93           rbbox_mode = 1;
94         end
95       end
96       limits = get(get(gca,'ZLabel'),'UserData');
97       if all(size(limits)==[1 4]), % Do simple checking of userdata
98         if all(limits([1 3])<limits([2 4])),
99           getlimits = 0;
100         else
101           getlimits = -1; % Don't munge data
102         end
103       else
104         if isempty(limits), getlimits = 1; else getlimits = -1; end
105       end
106
107     elseif strcmp(m,'on'),
108       set(gcf,'windowbuttondownfcn','zoomp(''down'')', ...
109               'windowbuttonupfcn','1;', ...
110               'windowbuttonmotionfcn','',...
111               'interruptible','yes');
112       set(gca,'interruptible','yes')
113       figure(gcf)       
114       return
115
116     elseif strcmp(m,'off'),
117       set(gcf,'WindowButtonDownFcn','lastouch',...
118           'WindowButtonUpFcn','',...
119           'WindowButtonMotionFcn','');
120 % Addition by BOB to reset FEDAR functions, if FEDAR is running
121       figs=findobj(0,'Type','figure');
122       ffigs=findobj(figs,'Tag','FEDAR Plot F');
123       if ~isempty(ffigs)
124          set(ffigs,'WindowButtonDownFcn','CallBacks(95)');
125          set(ffigs,'Interruptible','yes')
126       end       
127       return
128
129     elseif strcmp(m,'out'),
130       limits = get(get(gca,'ZLabel'),'UserData');
131       if all(size(limits)==[1 4]), % Do simple checking of userdata
132         if all(limits([1 3])<limits([2 4])),
133           getlimits = 0;
134         else
135           getlimits = -1; % Don't munge data
136         end
137       else
138         if isempty(limits), getlimits = 1; else getlimits = -1; end
139       end
140
141       center = [sum(get(gca,'Xlim'))/2 sum(get(gca,'Ylim'))/2];
142       m = -inf; % Zoom totally out
143     else
144       error(['Unknown option: ',m,'.']);
145     end
146
147   else
148     error('Only takes the strings ''on'',''off'', or ''out''.')
149   end
150 end
151
152 % Determine which IMAGE coordinate system is being used.
153 s = [version '    ']; k = find(s<46 & s>58);
154 if ~isempty(k), s = s(1:min(k)); end
155 [ver,count,msg,next] = sscanf(s,'%f',1);
156 if ver > 4.1,
157   useNew = 1;
158 elseif ver < 4.1,
159   useNew = 0;
160 else
161   if s(next)>='a', useNew = 1; else useNew = 0; end
162 end
163  
164 if getlimits~=0,  % Get min and max info for axis children
165
166   if isempty(get(get(gca,'ZLabel'),'userdata')),
167     % Use quick method if possible
168     xlim = get(gca,'xlim'); xmin = xlim(1); xmax = xlim(2);
169     ylim = get(gca,'ylim'); ymin = ylim(1); ymax = ylim(2);
170
171   else
172
173     % Use slow method only if someone else is using the userdata
174     h = get(gca,'Children');
175     xmin = inf; xmax = -inf; ymin = inf; ymax = -inf;
176     for i=1:length(h),
177       t = get(h(i),'Type');
178       if ~strcmp(t,'text'),
179         if strcmp(t,'image') & useNew, % Determine axis limits for image
180           x = get(h(i),'Xdata'); y = get(h(i),'Ydata');
181           x = [min(min(x)) max(max(x))];
182           y = [min(min(y)) max(max(y))];
183           [ma,na] = size(get(h(i),'Cdata'));
184           if na>1, dx = diff(x)/(na-1); else dx = 1; end
185           if ma>1, dy = diff(y)/(ma-1); else dy = 1; end
186           x = x + [-dx dx]/2; y = y + [-dy dy]/2;
187         else
188           x = get(h(i),'Xdata'); y = get(h(i),'Ydata');
189         end
190         xmin = min(xmin,min(min(x)));
191         xmax = max(xmax,max(max(x)));
192         ymin = min(ymin,min(min(y)));
193         ymax = max(ymax,max(max(y)));
194       end
195     end
196
197     % Use automatic limits if in use.
198     if strcmp(get(gca,'xLimMode'),'auto'),
199       xlim = get(gca,'xlim'); xmin = xlim(1); xmax = xlim(2);
200     end
201     if strcmp(get(gca,'yLimMode'),'auto'),
202       ylim = get(gca,'ylim'); ymin = ylim(1); ymax = ylim(2);
203     end
204   end
205
206   limits = [xmin xmax ymin ymax];
207   if getlimits~=-1, % Don't munge existing userdata.
208     set(get(gca,'ZLabel'),'UserData',limits); % Store limits in ZLabel userdata
209   end
210 end
211
212 %
213 % Actual zoom operation
214 %
215 if rbbox_mode,
216   axis(a(:)')
217 else
218   xmin = limits(1); xmax = limits(2); ymin = limits(3); ymax = limits(4);
219   if m==(-inf),
220     dx = xmax-xmin;
221     dy = ymax-ymin;
222   else
223     dx = diff(get(gca,'Xlim'))*(2.^(-m-1)); dx = min(dx,xmax-xmin);
224     dy = diff(get(gca,'Ylim'))*(2.^(-m-1)); dy = min(dy,ymax-ymin);
225   end
226
227   % Limit zoom.
228   center = max(center,[xmin ymin] + [dx dy]);
229   center = min(center,[xmax ymax] - [dx dy]);
230   axis([max(xmin,center(1)-dx) min(xmax,center(1)+dx) ...
231        max(ymin,center(2)-dy) min(ymax,center(2)+dy)])
232 end
Note: See TracBrowser for help on using the browser.