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

root/gliderproc/trunk/MATLAB/plots/blinksafe.m

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

Initial import of Stark code.

Line 
1 function vis = blinksafe(varargin)
2
3 % blinksafe -- Toggle data points in (x, y, z) plot.
4 %  blinksafe('demo') demonstrates itself, using random (x, y)
5 %   data plotted with EraseMode = 'xor'.
6 %  blinksafe('on') enables blinking, in which a mouse click
7 %   on a plotted (x, y, z) point makes the point invisible
8 %   and marks the site with a distinctive dot.  The z-value
9 %   at that point is set to NaN, and its original value is
10 %   remembered for later restoration.  The proximity of the
11 %   click to a point is determined in the (x, y) plane only.
12 %  blinksafe(h) turns blinking on for just the lines specified
13 %   by the handles h.
14 %  blinksafe('off') turns blinking off and removes the markers.
15 %   Points invisible at this stage will remain invisible,
16 %   since the corresponding z values will now be NaN.
17 %  blinksafe('clear') restores all the plotted data to visibility.
18 %  vis = blinksafe(h) returns the visibility-vector (1 = visible;
19 %   0 = not visible) for the line whose handle is h.
20  %
21 % Calls: none
22
23 % Copyright (C) 2000 Dr. Charles R. Denham, ZYDECO.
24 %  All Rights Reserved.
25 %   Disclosure without explicit written consent from the
26 %    copyright owner does not constitute publication.
27  
28 % Version of 12-Jan-2000 09:44:58.
29 % Updated    18-Jan-2000 15:15:47.
30
31 BLINKER_TAG = ['_' mfilename '_'];
32 BLINKER_MARKER = '*';
33 BLINKER_SIZE = get(0, 'DefaultLineMarkerSize');
34 BLINKER_COLOR = [1 0 0];
35 BLINKER_BUTTONDOWNFCN = mfilename;
36
37 % Demonstration.
38
39 if nargin > 0 & isequal(varargin{1}, 'demo')
40         help(mfilename)
41         x = linspace(0, 1, 101);
42         y  = sin(3*pi*x);
43         noise = 2*(rand(size(x)) - 0.5);
44         noise(abs(noise) < 0.95) = 0;
45         h = plot(x, y+noise, '-o', ...
46                                 'MarkerSize', 4, 'EraseMode', 'xor');
47         eval(mfilename)
48         if exist('zoomsafe', 'file') == 2
49                 eval('zoomsafe')
50         end
51         set(gcf, 'Name', [mfilename ' demo'])
52         if nargout > 0, vis = h; end
53         return
54 end
55
56 % Get visibility of a line.
57 %  Requires a handle and an output variable.
58
59 if nargout > 0 & nargin > 0
60         vis = [];
61         h = varargin{1};
62         if ischar(h), h = eval(h); end
63         if ishandle(h)
64                 if isequal(get(h, 'Type'), 'line')
65                         if ~isequal(get(h, 'Tag'), BLINKER_TAG)
66                                 vis = isfinite(get(h, 'ZData'));
67                         end
68                 end
69         end
70         return
71 end
72
73 % Actions: on, off, clear.
74
75 if nargin > 0 & isempty(gcbo)
76         if ischar(varargin{1})
77                 theAction = varargin{1};
78                 switch theAction
79                 case 'on'
80                         varargin = {};
81                 case 'off'
82                         h = findobj(gcf, 'Tag', BLINKER_TAG);
83                         if any(h), delete(h), end
84                         return
85                 case 'clear'
86                         h = findobj(gcf, 'Tag', BLINKER_TAG);
87                         if any(h)
88                                 for i = 1:length(h)
89                                         u = get(h(i), 'UserData');
90                                         z = get(u.handle, 'ZData');
91                                         z(u.index) = u.z;
92                                         set(u.handle, 'ZData', z)
93                                 end
94                                 delete(h)
95                         end
96                         return
97                 otherwise
98                         disp([' ## No such action: "' theAction '"'])
99                         return
100                 end
101         end
102 end
103
104 % Initialize.
105
106 if isempty(gcbo)
107         if isempty(varargin)
108                 h = findobj(gcf, 'Type', 'line');
109                 varargin{1} = h;
110         end
111         h = varargin{1};
112         set(h, 'ButtonDownFcn', BLINKER_BUTTONDOWNFCN)
113         for i = 1:length(h)
114                 z = get(h(i), 'ZData');
115                 if isempty(z)
116                         x = get(h(i), 'XData');
117                         z = zeros(size(x));
118                         set(h(i), 'ZData', z);
119                 end
120         end
121         return
122 end
123
124 % Process a click.  If clicking on the data, make
125 %  the closest data point invisible.  If clicking
126 %  on a "blinker", restore the visibility of the
127 %  corresponding point.
128
129 if isempty(varargin), varargin{1} = 'ButtonDownFcn'; end
130
131 if length(varargin) > 0 & ischar(varargin{1})
132         theEvent = varargin{1};
133         switch lower(theEvent)
134         case 'buttondownfcn'
135                 switch get(gcbo, 'Tag')
136                 case '_blinksafe_'   % Restore visibility; delete the blinker.
137                         u = get(gcbo, 'UserData');
138                         z = get(u.handle, 'ZData');
139                         z(u.index) = u.z;
140                         set(u.handle, 'ZData', z);
141                         delete(gcbo)
142                 otherwise   % Make invisible; create a blinker.
143                         p = get(gca, 'CurrentPoint');
144                         x0 = p(1, 1);
145                         y0 = p(1, 2);
146                         x = get(gcbo, 'XData');
147                         y = get(gcbo, 'YData');
148                        
149                         dx = diff(get(gca, 'XLim'));   % Scale to pixels.
150                         dy = diff(get(gca, 'YLim'));
151                         theOldUnits = get(gca, 'Units');
152                         set(gca, 'Units', 'pixels');
153                         thePosition = get(gca, 'Position');
154                         theWidth = thePosition(3);
155                         theHeight = thePosition(4);
156                         dx = dx / theWidth;
157                         dy = dy / theHeight;
158                         set(gca, 'Units', theOldUnits)
159                        
160                         d = abs((x*dy+sqrt(-1)*y*dx - (x0*dy+sqrt(-1)*y0*dx)));
161                        
162                         index = find(d == min(d));
163                         if any(index)
164                                 index = index(1);
165                                 z = get(gcbo, 'ZData');
166                                 if isempty(z), z = zeros(size(x)); end
167                                 if isfinite(z(index)) & 0
168                                         u.handle = gcbo;
169                                         u.index = index;
170                                         u.z = z(index);
171                                         theMarker = get(gcbo, 'Marker');
172                                         if isequal(theMarker, BLINKER_MARKER)
173                                                 BLINKER_MARKER = 'o';
174                                         end
175                                         h = line(x(index), y(index),...
176                                                         'Marker', BLINKER_MARKER, ...
177                                                         'MarkerSize', BLINKER_SIZE, ...
178                                                         'MarkerFaceColor', 'none', ...
179                                                         'MarkerEdgeColor', BLINKER_COLOR, ...
180                                                         'EraseMode', 'xor', ...
181                                                         'ButtonDownFcn', BLINKER_BUTTONDOWNFCN, ...
182                                                         'UserData', u, ...
183                                                         'Tag', BLINKER_TAG);
184                                         z(index) = nan;
185                                         set(gcbo, 'ZData', z)
186                                 else
187                                         h = make_blinker(gcbo, index);
188                                 end
189                         end
190                 end
191         otherwise
192                 disp(['## No such event: ' theEvent])
193         end
194 end
195
196 % ---------- make_blinker ---------- %
197
198 function theResult = make_blinker(h, index)
199
200 % make_blinker -- Create a blinker.
201 %  make_blinker(h, index) places a blinker
202 %   on line h at the given index.
203  
204 % Copyright (C) 2000 Dr. Charles R. Denham, ZYDECO.
205 %  All Rights Reserved.
206 %   Disclosure without explicit written consent from the
207 %    copyright owner does not constitute publication.
208  
209 % Version of 18-Jan-2000 14:11:55.
210 % Updated    18-Jan-2000 14:11:55.
211
212 BLINKER_TAG = ['_' mfilename '_'];
213 BLINKER_MARKER = '*';
214 BLINKER_SIZE = get(0, 'DefaultLineMarkerSize');
215 BLINKER_COLOR = [1 0 0];
216 BLINKER_BUTTONDOWNFCN = mfilename;
217
218 result = zeros(size(h));
219
220 for i = 1:length(h)
221         x = get(h(i), 'XData');
222         y = get(h(i), 'YData');
223         z = get(h(i), 'ZData');
224         if isempty(z)
225                 z = zeros(size(x));
226         end
227         theMarker = get(h(i), 'Marker');
228         if isequal(theMarker, BLINKER_MARKER)
229                 BLINKER_MARKER = 'o';
230         end
231         if ~isequal(get(gcbo, 'Marker'), 'none')
232                 BLINKER_SIZE = 2 + get(gcbo, 'MarkerSize');
233         end
234        
235         if isfinite(z(index))
236                 u.handle = h(i);
237                 u.index = index;
238                 u.z = z(index);
239                 result(i) = line(x(index), y(index),...
240                                 'Marker', BLINKER_MARKER, ...
241                                 'MarkerSize', BLINKER_SIZE, ...
242                                 'MarkerFaceColor', 'none', ...
243                                 'MarkerEdgeColor', BLINKER_COLOR, ...
244                                 'EraseMode', 'xor', ...
245                                 'ButtonDownFcn', BLINKER_BUTTONDOWNFCN, ...
246                                 'UserData', u, ...
247                                 'Tag', BLINKER_TAG);
248                 z(index) = nan;
249                 set(h(i), 'ZData', z)
250         end
251 end
252
253 if nargout > 0, theResult = result; end
Note: See TracBrowser for help on using the browser.