-
Notifications
You must be signed in to change notification settings - Fork 326
/
select3dtool.m
111 lines (85 loc) · 2.65 KB
/
select3dtool.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
function select3dtool(arg)
%SELECT3DTOOL A simple tool for interactively obtaining 3-D coordinates
%
% SELECT3DTOOL(FIG) Specify figure handle
%
% Example:
% surf(peaks);
% select3dtool;
% % click on surface
if nargin<1
arg = gcf;
end
if ~ishandle(arg)
feval(arg);
return;
end
%% initialize gui %%
fig = arg;
figure(fig);
uistate = uiclearmode(fig);
[tool, htext] = createUI;
hmarker1 = line('marker','o','markersize',10,'markerfacecolor','k','erasemode','xor','visible','off');
hmarker2 = line('marker','o','markersize',10,'markerfacecolor','r','erasemode','xor','visible','off');
state.uistate = uistate;
state.text = htext;
state.tool = tool;
state.fig = fig;
state.marker1 = hmarker1;
state.marker2 = hmarker2;
setappdata(fig,'select3dtool',state);
setappdata(state.tool,'select3dhost',fig);
set(fig,'windowbuttondownfcn','select3dtool(''click'')');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function off
state = getappdata(gcbf,'select3dtool');
if ~isempty(state)
delete(state.tool);
end
fig = getappdata(gcbf,'select3dhost');
if ~isempty(fig) & ishandle(fig)
state = getappdata(fig,'select3dtool');
uirestore(state.uistate);
delete(state.marker1);
delete(state.marker2);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function click
[p v vi] = select3d;
state = getappdata(gcbf,'select3dtool');
if ~ishandle(state.text)
state.text = createUI;
end
if ~ishandle(state.marker1)
state.marker1 = [];
end
if ~ishandle(state.marker2)
state.marker2 = [];
end
setappdata(state.fig,'select3dtool',state);
if isempty(v)
v = [nan nan nan];
vi = nan;
set(state.marker2,'visible','off');
else
set(state.marker2,'visible','on','xdata',v(1),'ydata',v(2),'zdata',v(3));
end
if isempty(p)
p = [nan nan nan];
set(state.marker1,'visible','off');
else
set(state.marker1,'visible','on','xdata',p(1),'ydata',p(2),'zdata',p(3));
end
% Update tool and markers
set(state.text,'string',createString(p(1),p(2),p(3),v(1),v(2),v(3),vi));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [fig, h] = createUI
pos = [200 200 200 200];
% Create selection tool %
fig = figure('handlevisibility','off','menubar','none','resize','off',...
'numbertitle','off','name','Select 3-D Tool','position',pos,'deletefcn','select3dtool(''off'')');
h = uicontrol('style','text','parent',fig,'string',createString(0,0,0,0,0,0,0),...
'units','norm','position',[0 0 1 1],'horizontalalignment','left');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [str] = createString(px,py,pz,vx,vy,vz,vi)
str = sprintf(' Position:\n X %f\n Y: %f\n Z: %f \n\n Vertex:\n X: %f\n Y: %f\n Z: %f \n\n Vertex Index:\n %d',px,py,pz,vx,vy,vz,vi);