-
Notifications
You must be signed in to change notification settings - Fork 714
/
ft_select_point3d.m
169 lines (150 loc) · 4.67 KB
/
ft_select_point3d.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
function [selected] = ft_select_point3d(bnd, varargin)
% FT_SELECT_POINT3D helper function for selecting one or multiple points on a 3D mesh
% using the mouse. It returns a list of the [x y z] coordinates of the selected
% points.
%
% Use as
% [selected] = ft_select_point3d(bnd, ...)
%
% Optional input arguments should come in key-value pairs and can include
% 'multiple' = true/false, make multiple selections, pressing "q" on the keyboard finalizes the selection (default = false)
% 'nearest' = true/false (default = true)
% 'marker' = character or empty, for example '.', 'o' or 'x' (default = [])
% 'markersize' = scalar, the size of the marker (default = 10)
% 'markercolor' = character, for example 'r', 'b' or 'g' (default = 'k')
%
% Example
% [pos, tri] = mesh_sphere(162);
% bnd.pos = pos;
% bnd.tri = tri;
% ft_plot_mesh(bnd)
% camlight
% ... do something here
%
% See also FT_SELECT_BOX, FT_SELECT_CHANNEL, FT_SELECT_POINT, FT_SELECT_RANGE, FT_SELECT_VOXEL
% This file is part of FieldTrip, see http:https://www.fieldtriptoolbox.org
% for the documentation and details.
%
% FieldTrip is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% FieldTrip is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with FieldTrip. If not, see <http:https://www.gnu.org/licenses/>.
%
% $Id$
% get optional input arguments
nearest = ft_getopt(varargin, 'nearest', true);
multiple = ft_getopt(varargin, 'multiple', false);
marker = ft_getopt(varargin, 'marker', []);
markersize = ft_getopt(varargin, 'markersize', 10);
markercolor = ft_getopt(varargin, 'markercolor', 'k');
% ensure that it is boolean
nearest = istrue(nearest);
multiple = istrue(multiple);
% get the object handles
h = get(gca, 'children');
% select the correct objects
iscorrect = false(size(h));
for i=1:length(h)
try
pos = get(h(i),'vertices');
tri = get(h(i),'faces');
if ~isempty(bnd) && isequal(bnd.pos, pos) && isequal(bnd.tri, tri)
% it is the same object that the user has plotted before
iscorrect(i) = true;
elseif isempty(bnd)
% assume that it is the same object that the user has plotted before
iscorrect(i) = true;
end
end
end
h = h(iscorrect);
if isempty(h) && ~isempty(bnd)
figure
ft_plot_mesh(bnd);
camlight
selected = ft_select_point3d(bnd, varargin{:});
return
end
if length(h)>1
ft_warning('using the first patch object in the figure');
h = h(1);
end
selected = zeros(0,3);
% everything is added to the current figure
holdflag = ishold;
if ~holdflag
hold on
end
done = false;
az = 0;
el = 0;
view(az,el);
while ~done
k = waitforbuttonpress;
[p, v, vi, facev, facei] = select3d(h);
if k == 1 %checks if waitforbuttonpress was a key
key = get(gcf,'CurrentCharacter'); % which key was pressed (if any)?
if strcmp(key, 'q')
% finished selecting points
done = true;
elseif strcmp(key, 'r')
% remove last point
if ~isempty(selected)
if ~isempty(marker)
delete(findobj('marker', '*'));
hs = plot3(selected(1:end-1,1), selected(1:end-1,2), selected(1:end-1,3), [markercolor marker]);
set(hs, 'MarkerSize', markersize);
end
fprintf('removed latest point at [%f %f %f]\n', selected(end,1), selected(end,2), selected(end,3));
selected = selected(1:end-1,:);
end
elseif strcmp(key,'+')
zoom(1.1)
elseif strcmp(key,'-')
zoom(0.9)
elseif strcmp(key,'w')
az = az+6;
view(az,el)
elseif strcmp(key,'a')
el = el+6;
view(az,el)
elseif strcmp(key,'s')
az = az-6;
view(az,el)
elseif strcmp(key,'d')
el = el-6;
view(az,el)
end
elseif ~isempty(p)
% a new point was selected
if nearest
selected(end+1,:) = v;
else
selected(end+1,:) = p;
end % if nearest
if multiple
fprintf('selected points at\n');
disp(selected);
else
fprintf('selected point at [%f %f %f]\n', selected(end,1), selected(end,2), selected(end,3));
end
if ~isempty(marker)
hs = plot3(selected(end,1), selected(end,2), selected(end,3), [markercolor marker]);
set(hs, 'MarkerSize', markersize);
end
end
if ~multiple
done = true;
end
end
if ~holdflag
hold off
end