24小时热门版块排行榜    

CyRhmU.jpeg
查看: 7196  |  回复: 62
【奖励】 本帖被评价52次,作者qudingfeng增加金币 39.2

qudingfeng

至尊木虫 (知名作家)


[资源] Matlab交互式三维齿轮绘制和动画演示(非原创 感觉不错 拿来和大家一起分享)

CODE:
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:
%
%
%   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;

% Get center location of gear
x0 = -d + pt(1) * d * 2;
y0 = sqrt(abs(d^2 - x0^2));

% Get amount of gear rotation
th1 = atan2(x0, y0) - pi/2;
th = (th1 - (th1 * (l / r)));

%--------------------------------------------------------------------------
% Rotate gear (from ROTATE)
%--------------------------------------------------------------------------
% Calculate rotation matrix
cosa = cos(-th);
sina = sin(-th);
rot  = [cosa, -sina; sina, cosa]';

newxy1   = [x1(:), y1(:)];
newxy2   = [x2(:), y2(:)];
newxy1   = newxy1 * rot;
newxy2   = newxy2 * rot;
newx1    = x0 + reshape(newxy1(:, 1), sz1);
newy1    = y0 + reshape(newxy1(:, 2), sz1);
newx2    = x0 + reshape(newxy2(:, 1), sz2);
newy2    = y0 + reshape(newxy2(:, 2), sz2);

% Set new position for the gear
set(gearH, {'XData', 'YData'}, {newx1, newy1; newx2, newy2});

[ Last edited by xiegangmai on 2012-9-18 at 10:13 ]
回复此楼

» 本帖附件资源列表

  • 欢迎监督和反馈:小木虫仅提供交流平台,不对该内容负责。
    本内容由用户自主发布,如果其内容涉及到知识产权问题,其责任在于用户本人,如对版权有异议,请联系邮箱:xiaomuchong@tal.com
  • 附件 1 : intergear.m
  • 2012-09-18 10:02:17, 8.55 K

» 收录本帖的淘帖专辑推荐

matlab学习专辑 模拟与仿真 杂货铺子 软件与检测
我喜欢 MATLAB

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
回帖支持 ( 显示支持度最高的前 50 名 )

qudingfeng

至尊木虫 (知名作家)


贴的程序有点问题 表情不知怎么被我弄上去了 大家下载附件吧
2楼2012-09-18 10:04:48
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zyszby

铁虫 (初入文坛)


★★★★★ 五星级,优秀推荐

谢谢楼主了。
学习齿轮中。。。
以后的方向,现在的初学者:)
8楼2012-12-08 02:20:27
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

tanhao365

新虫 (小有名气)


★★★★★ 五星级,优秀推荐

贴出来的程序问题不少,下载的那个下不下来,金币不够
10楼2013-01-21 14:35:21
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

楼小佳

银虫 (正式写手)


内容不错,挺好的
14楼2013-04-05 15:56:16
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

烟尘客

金虫 (初入文坛)


★★★★★ 五星级,优秀推荐

顶一下,感谢分享!非常感谢
15楼2013-04-14 14:24:07
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

focus2012

新虫 (初入文坛)


★★★ 三星级,支持鼓励

粗略一看没看懂……
16楼2013-05-16 11:10:01
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

njlmx9718

银虫 (初入文坛)


★★★ 三星级,支持鼓励

不能运行啊,显示错误:[muchong.com][1575311]intergear
[muchong.com][1575311]intergear
           |
Error: Unbalanced or unexpected parenthesis or bracket.
17楼2013-05-25 01:41:29
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

hnmczzg

银虫 (小有名气)


引用回帖:
17楼: Originally posted by njlmx9718 at 2013-05-25 01:41:29
不能运行啊,显示错误:intergear
intergear
           |
Error: Unbalanced or unexpected parenthesis or bracket.

将文件名修改后就可以运行了。
18楼2013-06-04 10:03:52
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

george1990

金虫 (小有名气)


★★★★★ 五星级,优秀推荐

这种帖子,得顶啊

[ 发自小木虫客户端 ]
21楼2013-11-12 01:16:51
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

那西撒斯之罪

铁虫 (初入文坛)


★★★ 三星级,支持鼓励

这画的是直齿轮、斜齿轮还是弧齿?????
22楼2013-11-27 19:24:52
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

CSU_HUSTer

新虫 (正式写手)


★ 一星级,一般

多谢楼主    的     无私贡献
24楼2013-12-02 10:35:18
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

wangshsh-123

新虫 (初入文坛)


★★★★★ 五星级,优秀推荐

Matlab 太神奇了,学习学习!
25楼2013-12-02 23:46:08
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

CSU_HUSTer

新虫 (正式写手)


??? Error: The input character is not valid in MATLAB statements or expressions.

Error in ==> run at 57
          evalin('caller', [s ';']);


错误啊  什么情况
27楼2013-12-25 16:08:18
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

yulinboylove

铜虫 (初入文坛)


这个function怎么显示是错误的?
30楼2014-04-01 11:16:09
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

普勒贝尔

新虫 (初入文坛)


★★★★★ 五星级,优秀推荐

为什么.m文件无法直接运行啊
36楼2014-05-22 08:58:06
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

普勒贝尔

新虫 (初入文坛)


好吧,原来改一下文件名就可以了,很好的一个程序,感谢!
37楼2014-05-22 09:01:50
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

丁歌丁歌

铜虫 (初入文坛)


★★★★★ 五星级,优秀推荐

好东西,谢楼主
40楼2015-04-12 08:54:36
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

微笑的鱼飘絮

铁虫 (初入文坛)


★★★★★ 五星级,优秀推荐

顶一下,感谢分享!值得学习,不错!
46楼2017-03-09 16:43:44
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
简单回复
2012-09-18 10:15   回复  
2012-09-18 16:24   回复  
三星好评  顶一下,感谢分享!
shizijun5楼
2012-09-19 08:29   回复  
五星好评  顶一下,感谢分享!
bingyu3106楼
2012-09-20 16:07   回复  
三星好评  顶一下,感谢分享!
2012-10-01 10:17   回复  
五星好评  顶一下,感谢分享!
2012-12-09 14:31   回复  
五星好评  顶一下,感谢分享!
2013-01-23 14:06   回复  
五星好评  顶一下,感谢分享!
2013-02-23 23:11   回复  
五星好评  顶一下,感谢分享!
楼小佳13楼
2013-04-05 15:55   回复  
五星好评  顶一下,感谢分享!
2013-11-11 16:51   回复  
五星好评  顶一下,感谢分享!
Frangmuler20楼
2013-11-11 20:43   回复  
五星好评  顶一下,感谢分享!
includeput23楼
2013-11-30 23:34   回复  
三星好评  顶一下,感谢分享!
yaokara26楼
2013-12-03 22:26   回复  
五星好评  顶一下,感谢分享!
2014-03-31 12:45   回复  
五星好评  顶一下,感谢分享!
2014-04-01 11:12   回复  
顶一下,感谢分享!
myongdl31楼
2014-04-20 11:59   回复  
五星好评  顶一下,感谢分享!
wqstephen32楼
2014-04-23 20:10   回复  
五星好评  顶一下,感谢分享!
xmcjhm33楼
2014-04-30 16:39   回复  
五星好评  顶一下,感谢分享!
lky201334楼
2014-05-05 13:08   回复  
五星好评  顶一下,感谢分享!
2014-05-20 18:22   回复  
五星好评  顶一下,感谢分享!
2014-09-05 20:46   回复  
五星好评  顶一下,感谢分享!
dempana39楼
2015-03-01 15:54   回复  
三星好评  顶一下,感谢分享!
2015-04-12 20:20   回复  
五星好评  顶一下,感谢分享!
abc198861342楼
2015-04-13 19:37   回复  
五星好评  顶一下,感谢分享!
lhl80143楼
2016-03-10 23:03   回复  
五星好评  顶一下,感谢分享!
2016-10-29 21:53   回复  
五星好评  顶一下,感谢分享!
he62new45楼
2017-01-11 23:22   回复  
五星好评  顶一下,感谢分享!
2017-03-14 23:35   回复  
五星好评  顶一下,感谢分享!
2017-04-25 22:41   回复  
五星好评  顶一下,感谢分享!
baoya_yg49楼
2017-08-09 16:21   回复  
五星好评  顶一下,感谢分享!
2017-09-08 19:27   回复  
五星好评  顶一下,感谢分享!
相关版块跳转 我要订阅楼主 qudingfeng 的主题更新
☆ 无星级 ★ 一星级 ★★★ 三星级 ★★★★★ 五星级
普通表情 高级回复(可上传附件)
信息提示
请填处理意见