-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProfile.m
337 lines (274 loc) · 11.3 KB
/
Profile.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
classdef Profile < hgsetget
% Profile - GUI Element for manipulating a plot
%
% This class creates a plot object that can be changed by dragging
% control points around. This isn't a contour, but rather a simple
% profile. It was designed explicitly for DNSCreator generation of
% radial and twist profiles.
properties
XData % X Coordinates of the reference points
YData % Y Coordinates of the reference points
Parent % Axes which will contain the plot
MarkerSize = 20; % Size of the Markers to use
LineWidth = 2; % Width of the line connecting points
Color = 'r'; % Color of the profile curve
XLimits = [-inf inf]; % Constrains on X position
YLimits = [-inf inf]; % Constrains on Y position
end
properties (Hidden)
hPoints % Handle to the control points
hLine % Handle to the line object
hGroup % Handle to the whole ensemble
end
properties (Dependent)
Points % Returns the coordinates of the profile
end
events
EVT_Changed % Event to be triggered on changes
end
methods
%--- Getter Functions ---%
function res = get.Points(self)
res = [self.XData(:), self.YData(:)];
end
%--- Getter Functions ---%
function set.Points(self, val)
self.XData = val(:,1);
self.YData = val(:,2);
refresh(self);
end
%--- Constructor ---%
function self = Profile(xdata, ydata, parent, varargin)
% Profile - Constructor for profile object
%
% USAGE:
% self = Profile(xdata, ydata, parent)
%
% INPUTS:
% xdata: [M x 1], X Coordinates of control points
% ydata: [M x 1], Y Coordinates of control points
% parent: Handle, Axes in which to place all elements
%
% OUTPUTS:
% self: Object, Handle to the graphics object to manipulate
% display and return values
self.XData = xdata;
self.YData = ydata;
if ~exist('parent', 'var'); parent = gca; end
% If any addition inputs were provided, go ahead and set them
if numel(varargin); set(self,varargin{:}); end
self.Parent = parent;
% Initialize the actual graphics object
self.init()
% Ensure that the plots are up-to-date
self.refresh()
end
function clickLine(self)
% clickLine - Callback for clicking on the line to add point
%
% USAGE:
% self.clickLine()
cp = get(gca, 'currentpoint');
% Add the point ta the end
self.XData(end+1) = cp(1,1);
self.YData(end+1) = cp(1,2);
% Sort to ensure monotonicity
[self.XData, sortind] = sort(self.XData);
self.YData = sort(self.YData);
[~,ind] = max(sortind);
% Now allow the user to start dragging without clicking again
self.dragPoint(ind);
% Update display (of course)
refresh(self);
end
function clickPoint(self)
% clickPoint - Callback for clicking a control point
% Get the current point in the axes
cp = get(gca, 'currentpoint');
% Determine the closest neighbor
[~,ind] = min(abs(self.XData - cp(1)));
switch lower(get(gcf, 'selectiontype'))
case 'normal' % Drag the point
self.dragPoint(ind);
case 'alt' % Remove the point
self.deletePoint(ind);
notify(self, 'EVT_Changed');
case 'open' % Specify Location
self.specifyPoint(ind);
notify(self, 'EVT_Changed');
end
end
function deletePoint(self, ind)
% deletePoint - Removes the specified point from the profile
%
% USAGE:
% self.deletePoint(ind)
%
% INPUTS:
% ind: Integer, Index of the point to remove
% Don't allow the user to remove end points as there must be at
% least two points
if ind == numel(self.XData) || ind == 1
return;
end
self.XData(ind) = [];
self.YData(ind) = [];
% Update display
refresh(self);
end
function drag(self, validx, validy, ind)
% drag - Point drag callback executed when mouse moves
%
% USAGE:
% self.drag(validx, validy, ind)
%
% INPUTS:
% validx: Functionhandle, function used to validate the new
% X position
% validy: Functionhandle, function used to validate the new
% Y position
% ind: Integer, index of the point that we are dragging
cp = get(gca, 'currentpoint');
self.XData(ind) = validx(cp(1,1));
self.YData(ind) = validy(cp(1,2));
% Update the display to reflect the drag
refresh(self)
end
function dragPoint(self, ind)
% dragPoint - Allows the user to drag the specified point
%
% USAGE:
% self.dragPoint(ind)
%
% INPUTS:
% ind: Integer, Index of the point to drag around
% Create some constraints on where the point can be dragged to
if ind == 1
validx = @(x)min(self.XData);
elseif ind == numel(self.XData)
validx = @(x)max(self.XData);
else
% Points cannot be reordered
validx = @(x)self.constrain(x, self.XData(ind-1),...
self.XData(ind+1));
end
validy = @(y)min(max(self.YLimits(1), y), self.YLimits(2));
fig = ancestor(self.Parent, 'figure');
% Remember the old figure properties so we can reset them
opts.WindowButtonMotionFcn = get(fig, 'WindowButtonMotionFcn');
opts.WindowButtonUpFcn = get(fig, 'WindowButtonUpFcn');
% Set Motion Callbacks for drag event
set(fig,'WindowButtonUpFcn', @(varargin)self.undrag(fig, opts));
set(fig,'WindowButtonMotionFcn', @(s,e)self.drag(validx,validy,ind));
end
function refresh(self)
% refresh - Redraws the profile according to current points
%
% USAGE:
% self.refresh()
%
set(self.hPoints, 'xdata', self.XData, 'ydata', self.YData)
% Interpolate the line
xx = linspace(self.XData(1),self.XData(end),1000);
pp = spline(self.XData, self.YData);
yy = ppval(pp, xx);
set(self.hLine, 'xdata', xx, 'ydata', yy)
end
function specifyPoint(self, ind)
% specifyPoint - Allows the user to define EXACT point location
%
% USAGE:
% self.specifyPoint(ind)
%
% INPUTS:
% ind: Integer, Index of the point that we are going to
% move to the specified location
% If the user selects an end point only allow them to move it
% up and down
if ind == 1 || ind == numel(self.XData)
prompt = {'Y:'};
def = {num2str(self.YData(ind))};
else
prompt = {'X:','Y:'};
def = {num2str(self.XData(ind)), num2str(self.YData(ind))};
end
title = 'Specify Coordinate';
lines = 1;
% Prompt the user for the new point location
answer = inputdlg(prompt, title, lines, def);
% If the user hits cancel, then just skip the rest
if isempty(answer); return; end
% Ensure the points are valid
if numel(answer) == 2
x = eval(answer{1});
if x < self.XLimits(1) || x > self.XLimits(2)
errordlg(sprintf('X Values must be between %d and %d', ...
self.XLimits(1), self.XLimits(2)));
return;
end
self.XData(ind) = x;
end
y = eval(answer{end});
if y < self.YLimits(1) || y > self.YLimits(2)
errordlg(sprintf('Y Values must be between %d and %d', ...
self.YLimits(1), self.YLimits(2)));
return;
end
self.YData(ind) = y;
% Make sure that we have a monotonic function by sorting the
% results
[self.XData, sortind] = sort(self.XData);
self.YData = self.YData(sortind);
% Update the display to reflect changes
refresh(self);
end
function undrag(self, hfig, opts)
% undrag - Helper function for stopping drag events
%
% USAGE:
% self.undrag(fig, opts)
%
% INPUTS:
% fig: Handle, handle to the figure controlling the
% current drag event
% opts: Properties and values to set for this figure
set(hfig, opts)
% Trigger the change event so listeners can update
notify(self, 'EVT_Changed');
end
end
methods (Static)
function x = constrain(x, xlow, xhigh)
% constrain - Helper function for constraining coordinates
if x <= xlow
x = xlow + eps;
elseif x >= xhigh
x = xhigh - eps;
end
end
end
methods (Access = 'private')
function init(self)
% init - Initialize all GUI components
%
% This function should only be called on object creation.
%
% USAGE:
% self.init()
self.hGroup = hggroup('Parent', self.Parent);
self.hLine = line(NaN, NaN, 'Parent', self.hGroup,...
'linestyle', '-',...
'marker', 'none',...
'color', self.Color,...
'linewidth', self.LineWidth);
self.hPoints = line(NaN, NaN, 'Parent', self.hGroup,...
'linestyle', 'none',...
'marker', '.',...
'markersize', 20,...
'color', self.Color,...
'Markersize', self.MarkerSize);
set(self.hPoints, 'buttondownfcn', @(s,e)self.clickPoint());
set(self.hLine, 'buttondownfcn', @(s,e)self.clickLine());
end
end
end