-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMouse3D.m
More file actions
executable file
·191 lines (175 loc) · 5.38 KB
/
Mouse3D.m
File metadata and controls
executable file
·191 lines (175 loc) · 5.38 KB
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
function out = Mouse3D(varargin)
% function Mouse3D
%
% Activates 3D mouse navigation on all 3D figures.
% Allows the use of a 3Dconnexion 'magellan' 3Dmouse in
% Matlab figure windows. Uses a com interface to the
% 3Dmouse service (TDxInput.Device), and uses a timer
% object (Mouse3dtimer) to poll the 3Dmouse.
%
% Use, Mouse3D to start and stop the service.
%
% Alternate syntax
% out = Mouse3D(cmd)
% where cmd can be ;
% 'start' : start 3D mouse navigation, returns com handle
% 'stop' : stop 3D mouse navigation, returns 0
% 'get' : returns 3dmouse position and rotation structure
%
% Example:
% logo;
% Mouse3D;
%
% Gus - 2008
persistent srv busycntr
period = 0.01; % Timer frequency, should match frame rate on your graphics card
slowdown = 25; % slowdown factor for inactive mouse
timertag = 'Mouse3dtimer'; % Unique tag for timer object
% check inputs
if nargin==0,
cmd = 0;
else
% convert string inputs to numeric eqiv
if ischar(varargin{1}),
switch lower(varargin{1}),
case('view');
cmd = 1;
case('busy')
cmd = 2;
case('rotate')
cmd = 3;
case('start'),
cmd = 4;
case('stop'),
cmd = 5;
case('gettimer');
cmd = 6;
case('get'),
cmd = 7;
otherwise
cmd = 0;
end;
else
cmd = varargin{1};
end;
end;
%% #Activities#
tim = timerfind('tag',timertag);
switch lower(cmd),
case(1), % #view#
% starts timer and returns a handle to the timer
disp('start')
start(tim);
out = 1;
case(2), % #busy#
% counts each time the timer calls and the function is still busy
busycntr = busycntr + 1;
out = busycntr;
if busycntr<10,
% restarts the timer as it stops by default
start(tim);
else
% causes the timer to stop if too busy
warning('Mouse3D:stop','Mouse3D stopped');
mouse3D_stop(srv,tim);
end;
case(3), % #rotate#
% polls the mouse and calls the relevant camera operations
busycntr = 0;
dt = 10*(1+busycntr)*period;
out = mouse3D_rotate(srv,dt,period,slowdown,tim);
case(4), % #start#
% connect to com object and setup timer object
disp('start');
srv = mouse3D_start(period,timertag);
busycntr = 0;
out = srv;
case(5), % #stop#
% stop and cleanup
srv = mouse3D_stop(srv,tim);
out = 0;
case(6), % #gettimer#
% return handle to timer
out = timerfind('tag',timertag);
case(7), % #get#
% get position from the mouse
out = mouse3D_get(srv);
otherwise
if isempty(srv),
[srv,tim] = mouse3D_start(period,timertag);
out = srv;
disp('Mouse3D started!');
start(tim);
elseif strcmpi(get(timerfind('tag',timertag),'Running'),'off'),
out = mouse3D_view;
else
srv = mouse3D_stop(srv,tim);
out = 0;
disp('Mouse3D stopped!');
end;
end;
%% ---------------------------------------------------------
% polls the mouse and callls the relavent camera operations
function out = mouse3D_rotate(srv,dt,period,slowdown,tim)
m3d = mouse3D_get(srv);
% checks for mouse activity and slows down the time if there is none
% needed to prevent lockup
if (m3d.len==0 && m3d.ang==0),
if (tim.Period == period),
stop(tim); % must stop timer before changing period
tim.Period = period*slowdown;
start(tim);
end;
else
% speedup timer if it was resting
if (tim.Period ~= period),
stop(tim);
tim.Period = period;
start(tim);
end;
%pos = sign(m3d.pos).*(m3d.pos/1600).^2;
%rot = sign(m3d.rot).*(m3d.rot*m3d.ang/1600).^2;
pos = (m3d.pos/1600);
rot = (m3d.rot*m3d.ang/1600);
camorbit(30*dt*rot(3),30*dt*rot(1),'camera');
VA = camva;
campan(-VA*dt*pos(1),VA*dt*pos(3),'camera');
camroll(30*dt*rot(2));
VA = max(1e-6,VA+5*dt*pos(2));
camva(VA);
drawnow;
end;
out = 0;
%% ---------------------------------------------------------
% get position from the mouse
function out = mouse3D_get(srv)
out.period = srv.get('Sensor').get('Period');
opos = srv.get('Sensor').get('Translation');
out.pos = [get(opos,'X') get(opos,'Y') get(opos,'Z')];
out.len = get(opos,'Length');
orot = srv.get('Sensor').get('Rotation');
out.rot = [get(orot,'X') get(orot,'Y') get(orot,'Z')];
out.ang = get(orot,'Angle');
%% ---------------------------------------------------------
% connect to com object and setup timer object
function [srv tim] = mouse3D_start(period,timertag)
srv = actxserver('TDxInput.Device');
tim = timerfind('tag',timertag);
if isempty(tim),
tim = timer;
% set(tim, 'TimerFcn','Mouse3D(3);', 'ErrorFcn', 'Mouse3D(2);', 'BusyMode','error', 'ExecutionMode','FixedRate', 'StartDelay',period, 'period',period, 'tag',timertag);
set(tim, 'TimerFcn','Mouse3D(3);', 'BusyMode','drop', 'ExecutionMode','FixedRate', 'StartDelay',period, 'period',period, 'tag',timertag);
else
stop(tim);
end;
% srv.invoke('Connect'); % This line does not seem to be needed
%% ---------------------------------------------------------
% Stop timer and release com object
function srv = mouse3D_stop(srv,tim)
% srv.invoke('Disconnect'); % This line does not seem to be needed
if ~isempty(tim),
stop(tim);
delete(tim);
end;
release(srv);
srv = [];