DRAWCYLINDER Draw a cylinder. drawCylinder(CYL) Draws the cylinder CYL on the current axis. CYL is a 1-by-7 row vector in the form [x1 y1 z1 x2 y2 z2 r] where: * [x1 y1 z1] are the coordinates of starting point, * [x2 y2 z2] are the coordinates of ending point, * R is the radius of the cylinder drawCylinder(CYL, N) Uses N points for discretizating the circles of the cylinder. Default value is 32. drawCylinder(..., OPT) with OPT = 'open' (default) or 'closed', specify if the bases of the cylinder should be drawn. drawCylinder(..., 'FaceColor', COLOR) Specifies the color of the cylinder. Any couple of parameters name and value can be given as argument, and will be transfered to the 'surf' matlab function drawCylinder(..., 'FaceAlpha', ALPHA) Specifies the transparency of the cylinder and of the optionnal caps. drawCylinder(AX, ...) Specifies the axis to draw on. AX should be a valid axis handle. H = drawCylinder(...) Returns a handle to the patch representing the cylinder. Examples: % basic example figure; drawCylinder([0 0 0 10 20 30 5]); % draw hollow cylinders figure; drawCylinder([0 0 0 10 20 30 5], 'open'); % change cylinder color figure; drawCylinder([0 0 0 10 20 30 5], 'FaceColor', 'r'); % change cylinder color using graphical handle figure; h = drawCylinder([0 0 0 10 20 30 5]); set(h, 'facecolor', 'b'); % Draw three mutually intersecting cylinders p0 = [10 10 10]; p1 = p0 + 80 * [1 0 0]; p2 = p0 + 80 * [0 1 0]; p3 = p0 + 80 * [0 0 1]; figure; axis equal; axis([0 100 0 100 0 100]); hold on drawCylinder([p0 p1 10], 'FaceColor', 'r'); drawCylinder([p0 p2 10], 'FaceColor', 'g'); drawCylinder([p0 p3 10], 'FaceColor', 'b'); axis equal set(gcf, 'renderer', 'opengl') view([60 30]); light; % draw cube skeleton [v, e, f] = createCube; figure; axis equal; axis([-0.2 1.2 -0.2 1.2 -0.2 1.2]); hold on; view(3); cyls = [v(e(:,1), :) v(e(:,2),:) repmat(0.1, size(e, 1), 1)]; drawCylinder(cyls); light See also cylinderMesh, drawEllipseCylinder, drawSphere, drawLine3d, surf intersectLineCylinder, cylinderSurfaceArea
0001 function varargout = drawCylinder(varargin) 0002 %DRAWCYLINDER Draw a cylinder. 0003 % 0004 % drawCylinder(CYL) 0005 % Draws the cylinder CYL on the current axis. 0006 % CYL is a 1-by-7 row vector in the form [x1 y1 z1 x2 y2 z2 r] where: 0007 % * [x1 y1 z1] are the coordinates of starting point, 0008 % * [x2 y2 z2] are the coordinates of ending point, 0009 % * R is the radius of the cylinder 0010 % 0011 % drawCylinder(CYL, N) 0012 % Uses N points for discretizating the circles of the cylinder. Default 0013 % value is 32. 0014 % 0015 % drawCylinder(..., OPT) 0016 % with OPT = 'open' (default) or 'closed', specify if the bases of the 0017 % cylinder should be drawn. 0018 % 0019 % drawCylinder(..., 'FaceColor', COLOR) 0020 % Specifies the color of the cylinder. Any couple of parameters name and 0021 % value can be given as argument, and will be transfered to the 'surf' 0022 % matlab function 0023 % 0024 % drawCylinder(..., 'FaceAlpha', ALPHA) 0025 % Specifies the transparency of the cylinder and of the optionnal caps. 0026 % 0027 % drawCylinder(AX, ...) 0028 % Specifies the axis to draw on. AX should be a valid axis handle. 0029 % 0030 % H = drawCylinder(...) 0031 % Returns a handle to the patch representing the cylinder. 0032 % 0033 % 0034 % Examples: 0035 % % basic example 0036 % figure; drawCylinder([0 0 0 10 20 30 5]); 0037 % 0038 % % draw hollow cylinders 0039 % figure; drawCylinder([0 0 0 10 20 30 5], 'open'); 0040 % 0041 % % change cylinder color 0042 % figure; drawCylinder([0 0 0 10 20 30 5], 'FaceColor', 'r'); 0043 % 0044 % % change cylinder color using graphical handle 0045 % figure; 0046 % h = drawCylinder([0 0 0 10 20 30 5]); 0047 % set(h, 'facecolor', 'b'); 0048 % 0049 % % Draw three mutually intersecting cylinders 0050 % p0 = [10 10 10]; 0051 % p1 = p0 + 80 * [1 0 0]; 0052 % p2 = p0 + 80 * [0 1 0]; 0053 % p3 = p0 + 80 * [0 0 1]; 0054 % figure; axis equal; axis([0 100 0 100 0 100]); hold on 0055 % drawCylinder([p0 p1 10], 'FaceColor', 'r'); 0056 % drawCylinder([p0 p2 10], 'FaceColor', 'g'); 0057 % drawCylinder([p0 p3 10], 'FaceColor', 'b'); 0058 % axis equal 0059 % set(gcf, 'renderer', 'opengl') 0060 % view([60 30]); light; 0061 % 0062 % % draw cube skeleton 0063 % [v, e, f] = createCube; 0064 % figure; axis equal; axis([-0.2 1.2 -0.2 1.2 -0.2 1.2]); hold on; view(3); 0065 % cyls = [v(e(:,1), :) v(e(:,2),:) repmat(0.1, size(e, 1), 1)]; 0066 % drawCylinder(cyls); 0067 % light 0068 % 0069 % See also 0070 % cylinderMesh, drawEllipseCylinder, drawSphere, drawLine3d, surf 0071 % intersectLineCylinder, cylinderSurfaceArea 0072 % 0073 0074 % ------ 0075 % Author: David Legland 0076 % E-mail: david.legland@inrae.fr 0077 % Created: 2005-09-17 0078 % Copyright 2005-2024 INRA - TPV URPOI - BIA IMASTE 0079 0080 %% Input argument processing 0081 0082 % extract handle of axis to draw on 0083 [hAx, varargin] = parseAxisHandle(varargin{:}); 0084 0085 % input argument representing cylinders 0086 cyl = varargin{1}; 0087 varargin(1) = []; 0088 0089 % process the case of multiple cylinders 0090 if iscell(cyl) 0091 hCyls = gobjects(length(cyl), 1); 0092 for i = 1:length(cyl) 0093 hCyls(i) = drawCylinder(hAx, cyl{i}, varargin{:}); 0094 end 0095 if nargout > 0 0096 varargout{1} = hCyls; 0097 end 0098 return; 0099 elseif size(cyl, 1) > 1 0100 hCyls = gobjects(size(cyl, 1), 1); 0101 for i = 1:size(cyl, 1) 0102 hCyls(i) = drawCylinder(hAx, cyl(i, :), varargin{:}); 0103 end 0104 0105 if nargout > 0 0106 varargout{1} = hCyls; 0107 end 0108 return; 0109 end 0110 0111 % default values 0112 N = 32; 0113 closed = true; 0114 0115 % check number of discretization steps 0116 if ~isempty(varargin) 0117 var = varargin{1}; 0118 if isnumeric(var) 0119 N = var; 0120 varargin = varargin(2:end); 0121 end 0122 end 0123 0124 % check if cylinder must be closed or open 0125 if ~isempty(varargin) 0126 var = varargin{1}; 0127 if ischar(var) 0128 if strncmpi(var, 'open', 4) 0129 closed = false; 0130 varargin = varargin(2:end); 0131 elseif strncmpi(var, 'closed', 5) 0132 closed = true; 0133 varargin = varargin(2:end); 0134 end 0135 end 0136 end 0137 0138 faceColor = 'g'; 0139 ind = find(strcmpi(varargin, 'FaceColor'), 1, 'last'); 0140 if ~isempty(ind) 0141 faceColor = varargin{ind+1}; 0142 varargin(ind:ind+1) = []; 0143 end 0144 0145 % extract transparency 0146 alpha = 1; 0147 ind = find(strcmpi(varargin, 'FaceAlpha'), 1, 'last'); 0148 if ~isempty(ind) 0149 alpha = varargin{ind+1}; 0150 varargin(ind:ind+1) = []; 0151 end 0152 0153 % add default drawing options 0154 varargin = [{'FaceColor', faceColor, 'edgeColor', 'none', 'FaceAlpha', alpha} varargin]; 0155 0156 0157 %% Computation of mesh coordinates 0158 0159 % extreme points of cylinder 0160 p1 = cyl(1:3); 0161 p2 = cyl(4:6); 0162 0163 % radius of cylinder 0164 r = cyl(7); 0165 0166 % compute orientation angle of cylinder 0167 [theta, phi, rho] = cart2sph2d(p2 - p1); 0168 dphi = linspace(0, 2*pi, N+1); 0169 0170 % generate a cylinder oriented upwards 0171 x = repmat(cos(dphi) * r, [2 1]); 0172 y = repmat(sin(dphi) * r, [2 1]); 0173 z = repmat([0 ; rho], [1 length(dphi)]); 0174 0175 % transform points 0176 trans = localToGlobal3d(p1, theta, phi, 0); 0177 pts = transformPoint3d([x(:) y(:) z(:)], trans); 0178 0179 % reshape transformed points 0180 x2 = reshape(pts(:,1), size(x)); 0181 y2 = reshape(pts(:,2), size(x)); 0182 z2 = reshape(pts(:,3), size(x)); 0183 0184 0185 %% Display cylinder mesh 0186 0187 % plot the cylinder as a surface 0188 hCyl(1) = surf(hAx, x2, y2, z2, varargin{:}); 0189 0190 % eventually plot the ends of the cylinder 0191 if closed 0192 hCyl(2)=patch(hAx, x2(1,:)', y2(1,:)', z2(1,:)', faceColor, 'edgeColor', 'none', 'FaceAlpha', alpha); 0193 hCyl(3)=patch(hAx, x2(2,:)', y2(2,:)', z2(2,:)', faceColor, 'edgeColor', 'none', 'FaceAlpha', alpha); 0194 gh = hggroup(hAx); 0195 set(hCyl,'Parent',gh) 0196 hCyl = gh; 0197 end 0198 0199 % format ouptut 0200 if nargout == 1 0201 varargout{1} = hCyl; 0202 end