function gear3d(varargin)
%GEAR3D GUI example of 3D gear.
% GEAR3D will pop up a GUI example of 3D gear. The gear rolls on a geared
% ground based on the mouse location. It uses the x-location of the mouse
% pointer for the gear location. Use the arrow keys to change the view.
% Press SPACEBAR to reset the view. This was inspired by Mike Agostini's
% 3D Clock submission in FEX:
%
% http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=9671&objectType=file
%
% The gear is made of 2 surface objects, and the ground is a single surface
% object. Since the moving part (gear) only contains 2 objects, the
% animation update is quite smooth. It uses OpenGL rendering.
%
% GEAR3D accepts 3 optional arguments that specify the number of teeth on the
% gear, number of spokes, and the gear ratio between the ground and the gear.
%
% Example:
% GEAR3D('teeth', 30) - default is 50.
% GEAR3D('spokes', 4) - default is 8.
% GEAR3D('ratio', 2) - default is 3. This also determines how big
% the ground radius is.
% Only positive integers are allowed.
%
% This has no practical use. I made it as an exercise for 3d animation.
% You have been warned. This was created in R13SP1, so nested function is
% not utilized. Nested function will improve how surface data are passed
% between functions.
%
% VERSIONS:
% v1.0 - first version
% v1.1 - fixed gear meshing problem. (Feb 2, 2006)
% v1.2 - changed to pass parameters instead of using handles structure
% (for speed). also inlined the stripped down version of the ROTATE
% function to speed up animation (Feb 25, 2006)
%
% Jiro Doke
% Feb 3, 2006
%--------------------------------------------------------------------------
% Default values
%--------------------------------------------------------------------------
% Number of teeth
numteeth = 50;
% Number of spokes
numspokes = 8;
% Gear ratio
rr = 3;
%--------------------------------------------------------------------------
% Optional arguments
%--------------------------------------------------------------------------
if mod(nargin, 2) == 1
error('Optional arguments must come in pairs.');
end
if nargin
opt = varargin(1:2:end);
val = varargin(2:2:end);
validOpts = {'teeth', 'spokes', 'ratio'};
for iArg = 1:length(opt)
id = strmatch(lower(opt{iArg}), validOpts);
if isempty(id)
error('Invalid option. Valid options: ''teeth'', ''spokes'', ''ratio''');
else
switch strmatch(lower(opt{iArg}), validOpts)
case 1
if isnumeric(val{iArg}) & length(val{iArg}) == 1
numteeth = round(abs(val{iArg}));
end
case 2
if isnumeric(val{iArg}) & length(val{iArg}) == 1
numspokes = round(abs(val{iArg}));
end
case 3
if isnumeric(val{iArg}) & length(val{iArg}) == 1
rr = round(abs(val{iArg}));
end
end
end
end
end
%--------------------------------------------------------------------------
% Construct gear and ground
%--------------------------------------------------------------------------
% Gear part radii
r1 = 1;
r2 = 3;
r3 = 10;
r4 = 11;
r5 = 13;
r6 = 12;
% Radius to the center of gear teeth
r = (r5 + r6) / 2;
% Height of gear teeth
h = r5 - r6;
% Radius to the center of ground teeth
l = r * rr;
% Ground radii
l1 = l - h/2;
l2 = l + h/2;
l3 = l2 + 2;
% Gear
[x0,y0,z0] = cylinder([r6], numteeth*4); % teeth groove
[x1,y1,z1] = cylinder([r1, r1, r2, r2, r1], numteeth*4); % spokes
[x2,y2,z2] = cylinder([r3, r3, r4, r4, r5, r5, r4, r4, r3], numteeth*4); % gear teeth
z1([1, 4, 5], :) = 2;
z1(2:3, :) = -2;
z2([1, 8, 9], :) = 2;
z2(2:3, :) = -2;
z2(4:5, :) = -1;
z2(6:7, :) = 1;
x2(5:6,1:4:end) = x0(1:2,1:4:end);
x2(5:6,2:4:end) = x0(1:2,2:4:end);
y2(5:6,1:4:end) = y0(1:2,1:4:end);
y2(5:6,2:4:end) = y0(1:2,2:4:end);
% Spoke
interval = round(length(x1) / numspokes);
for id = 1:4
x1(3:4, id:interval:end) = x2(1:2, id:interval:end);
y1(3:4, id:interval:end) = y2(1:2, id:interval:end);
end
% Ground
[x3, y3, z3] = cylinder([l2, l2, l3, l3, l2], numteeth * rr * 4);
[x4, y4, z4] = cylinder(l1, numteeth * rr * 4);
z3([1 4 5], :) = 4;
z3([2 3], :) = -4;
x3([1 2 5], 1:4:end) = x4([1 1 1], 1:4:end);
x3([1 2 5], 2:4:end) = x4([1 1 1], 2:4:end);
y3([1 2 5], 1:4:end) = y4([1 1 1], 1:4:end);
y3([1 2 5], 2:4:end) = y4([1 1 1], 2:4:end);
% Make ground a half circle
len = round(length(x3) / 2);
x3(:, len:end) = [];
y3(:, len:end) = [];
z3(:, len:end) = [];
%--------------------------------------------------------------------------
% GUI
%--------------------------------------------------------------------------
% Create figure;
fH = findobj('Type', 'figure', 'Tag', 'solidmodelGUI');
if ishandle(fH)
close(fH);
end
fH = figure(...
'Name' , sprintf('3D Gear: View = [%3.0f, %3.0f]', 0, -50), ...
'NumberTitle', 'off', ...
'Units' , 'normalized', ...
'Position' , [.1, .1, .8, .8], ...
'Visible' , 'off', ...
'Tag' , 'solidmodelGUI');
axes(...
'Units' , 'normalized', ...
'Position', [0, .9, 1, .1], ...
'XLim' , [-1, 1], ...
'YLim' , [-1, 1], ...
'Visible' , 'off');
text(0, 0, ...
{'Move the mouse left and right within the figure window to move the gear.',...
'Use ARROW keys to change the view. SPACE to reset view.'}, ...
'HorizontalAlignment' , 'center', ...
'VerticalAlignment' , 'middle', ...
'Color' , 'red');
axH = axes('Units', 'normalized', ...
'Position', [0, 0, 1, .9]);
% Gear
%
% Spokes
gearH(1) = surface(x1,y1,z1, ...
'EdgeColor', [.3, .3, .3], ...
'EdgeAlpha', 0, ...
'FaceColor', [.5, .5, 1]);
% Gear Teeth
gearH(2) = surface(x2,y2,z2, ...
'EdgeColor', [.3, .3, .3], ...
'EdgeAlpha', .1, ...
'FaceColor', [.5, .5, 1]);
% Ground
surface(x3,y3,z3, ...
'EdgeColor', [.3, .3, .3], ...
'EdgeAlpha', .1, ...
'FaceColor', [.5, 1, .5], ...
'FaceAlpha', .5);
set(axH, 'View', [0, -50]);
axis equal manual off;
set(axH, 'CameraViewAngleMode', 'manual');
% Add lighting
light('Position', [50 , 0, -20]);
light('Position', [-50, 0, -20]);
% Calculate size once and pass in as parameter
sz1 = size(x1);
sz2 = size(x2);
set(fH, ...
'Visible' , 'on', ...
'KeyPressFcn' , {@myKeyPressFcn, axH}, ...
'WindowButtonMotionFcn', {@myMotionFcn, gearH, x1, x2, y1, y2, r, l, sz1, sz2});
% Set initial position of gear
myMotionFcn(gcf, [], gearH, x1, x2, y1, y2, r, l, sz1, sz2);
%--------------------------------------------------------------------------
% myKeyPressFcn - process key presses to rotate view
%--------------------------------------------------------------------------
function myKeyPressFcn(obj, edata, axH)
k = get(obj, 'CurrentKey');
aZeL = get(axH, 'View');
switch lower(k)
case 'uparrow'
aZeL(2) = min([aZeL(2) + 10, 90]);
case 'downarrow'
% -90 doesn't work well when rotated left or right
aZeL(2) = max([aZeL(2) - 10, -89.9999]);
case 'leftarrow'
aZeL(1) = max([aZeL(1) - 10, -90]);
case 'rightarrow'
aZeL(1) = min([aZeL(1) + 10, 90]);
case 'space'
aZeL = [0, -50];
end
set(axH, 'View', aZeL);
set(obj, 'Name', sprintf('3D Gear: View = [%3.0f, %3.0f]', aZeL));
%--------------------------------------------------------------------------
% myMotionFcn - move mouse to move gear
%--------------------------------------------------------------------------
function myMotionFcn(obj, edata, gearH, x1, x2, y1, y2, r, l, sz1, sz2)
pt = get(obj, 'CurrentPoint');
d = l - r;