Home > matGeom > geom3d > drawCylinder.m

drawCylinder

PURPOSE ^

Draw a cylinder.

SYNOPSIS ^

function varargout = drawCylinder(varargin)

DESCRIPTION ^

 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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = drawCylinder(varargin)
0002 % 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 %   INRA - TPV URPOI - BIA IMASTE
0077 %   created the 17/09/2005
0078 %
0079 
0080 %   HISTORY
0081 %   2006/12/14 bug for coordinate conversion, vectorize transforms
0082 %   04/01/2007 better input processing, manage end caps of cylinder
0083 %   19/06/2009 use function localToGlobal3d, add docs
0084 %   2011-06-29 use sph2cart2d, code cleanup
0085 %   2018-01-02 add transparency managements
0086 
0087 
0088 %% Input argument processing
0089 
0090 % parse axis handle
0091 if isAxisHandle(varargin{1})
0092     hAx = varargin{1};
0093     varargin(1) = [];
0094 else
0095     hAx = gca;
0096 end
0097 
0098 % input argument representing cylinders
0099 cyl = varargin{1};
0100 varargin(1) = [];
0101 
0102 % process the case of multiple cylinders
0103 if iscell(cyl)
0104     hCyls = gobjects(length(cyl), 1);
0105     for i = 1:length(cyl)
0106         hCyls(i) = drawCylinder(hAx, cyl{i}, varargin{:});
0107     end
0108     if nargout > 0
0109         varargout{1} = hCyls;
0110     end    
0111     return;
0112 elseif size(cyl, 1) > 1
0113     hCyls = gobjects(size(cyl, 1), 1);
0114     for i = 1:size(cyl, 1)
0115         hCyls(i) = drawCylinder(hAx, cyl(i, :), varargin{:});
0116     end
0117     
0118     if nargout > 0
0119         varargout{1} = hCyls;
0120     end    
0121     return;
0122 end
0123 
0124 % default values
0125 N = 32;
0126 closed = true;
0127 
0128 % check number of discretization steps
0129 if ~isempty(varargin)
0130     var = varargin{1};
0131     if isnumeric(var)
0132         N = var;
0133         varargin = varargin(2:end);
0134     end
0135 end
0136 
0137 % check if cylinder must be closed or open
0138 if ~isempty(varargin)
0139     var = varargin{1};
0140     if ischar(var)
0141         if strncmpi(var, 'open', 4)
0142             closed = false;
0143             varargin = varargin(2:end);
0144         elseif strncmpi(var, 'closed', 5)
0145             closed = true;
0146             varargin = varargin(2:end);
0147         end
0148     end
0149 end
0150 
0151 faceColor = 'g';
0152 ind = find(strcmpi(varargin, 'FaceColor'), 1, 'last');
0153 if ~isempty(ind)
0154     faceColor = varargin{ind+1};
0155     varargin(ind:ind+1) = [];
0156 end
0157 
0158 % extract transparency
0159 alpha = 1;
0160 ind = find(strcmpi(varargin, 'FaceAlpha'), 1, 'last');
0161 if ~isempty(ind)
0162     alpha = varargin{ind+1};
0163     varargin(ind:ind+1) = [];
0164 end
0165 
0166 % add default drawing options
0167 varargin = [{'FaceColor', faceColor, 'edgeColor', 'none', 'FaceAlpha', alpha} varargin];
0168 
0169 
0170 %% Computation of mesh coordinates
0171 
0172 % extreme points of cylinder
0173 p1 = cyl(1:3);
0174 p2 = cyl(4:6);
0175 
0176 % radius of cylinder
0177 r = cyl(7);
0178 
0179 % compute orientation angle of cylinder
0180 [theta, phi, rho] = cart2sph2d(p2 - p1);
0181 dphi = linspace(0, 2*pi, N+1);
0182 
0183 % generate a cylinder oriented upwards
0184 x = repmat(cos(dphi) * r, [2 1]);
0185 y = repmat(sin(dphi) * r, [2 1]);
0186 z = repmat([0 ; rho], [1 length(dphi)]);
0187 
0188 % transform points
0189 trans   = localToGlobal3d(p1, theta, phi, 0);
0190 pts     = transformPoint3d([x(:) y(:) z(:)], trans);
0191 
0192 % reshape transformed points
0193 x2 = reshape(pts(:,1), size(x));
0194 y2 = reshape(pts(:,2), size(x));
0195 z2 = reshape(pts(:,3), size(x));
0196 
0197 
0198 %% Display cylinder mesh
0199 
0200 % plot the cylinder as a surface
0201 hCyl(1) = surf(hAx, x2, y2, z2, varargin{:});
0202 
0203 % eventually plot the ends of the cylinder
0204 if closed
0205     hCyl(2)=patch(hAx, x2(1,:)', y2(1,:)', z2(1,:)', faceColor, 'edgeColor', 'none', 'FaceAlpha', alpha);
0206     hCyl(3)=patch(hAx, x2(2,:)', y2(2,:)', z2(2,:)', faceColor, 'edgeColor', 'none', 'FaceAlpha', alpha);
0207     gh = hggroup(hAx);
0208     set(hCyl,'Parent',gh)
0209     hCyl = gh;
0210 end
0211 
0212 % format ouptut
0213 if nargout == 1
0214     varargout{1} = hCyl;
0215 end

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019