Home > matGeom > geom3d > drawEllipseCylinder.m

drawEllipseCylinder

PURPOSE ^

DRAWELLIPSECYLINDER Draw a cylinder with ellipse cross-section.

SYNOPSIS ^

function varargout = drawEllipseCylinder(cyl, varargin)

DESCRIPTION ^

DRAWELLIPSECYLINDER Draw a cylinder with ellipse cross-section.

   drawEllipseCylinder(CYL)
   draws the cylinder CYL on the current axis.
   CYL is a cylinder defined by [x1 y1 z1 x2 y2 z2 r1 r2 roll], with:
   * [x1 y2 z1] are coordinates of starting point,
   * [x2 y2 z2] are coordinates of ending point, 
   * R1 and R2 are the lengths of the ellipse semi axes, and
   * ROLL is the rotation of the cylinder around its main axis (in
      degrees)

   drawEllipseCylinder(CYL, N)
   uses N points for discretisation of angle. Default value is 32.

   drawEllipseCylinder(..., OPT)
   with OPT = 'open' (default) or 'closed', specify if bases of the
   cylinder should be drawn.

   drawEllipseCylinder(..., '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

   H = drawEllipseCylinder(...)
   returns a handle to the patch representing the cylinder.


   Example:
     figure; drawEllipseCylinder([0 0 0 10 20 30 5 2]);

     figure; drawEllipseCylinder([0 0 0 10 20 30 5 2], 'open');

     figure; drawEllipseCylinder([0 0 0 10 20 30 5 2], 'FaceColor', 'r');

     figure;
     h = drawEllipseCylinder([0 0 0 10 20 30 5 2]);
     set(h, 'facecolor', 'b');

     % Draw three mutually intersecting elliptic cylinders
     p1 = [30 0 0];
     p2 = [0 30 0];
     p3 = [0 0 30];
     radii = [20 10];
     figure;
     drawEllipseCylinder([-p1 p1 radii 0], 'FaceColor', 'r');
     hold on
     drawEllipseCylinder([-p2 p2 radii 90], 'FaceColor', 'g');
     drawEllipseCylinder([-p3 p3 radii 90], 'FaceColor', 'b');
     axis equal
     set(gcf, 'renderer', 'opengl')
     view([60 30]); light;

   See Also:
   drawCylinder, drawSphere, cylinderMesh, drawLine3d, surf

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = drawEllipseCylinder(cyl, varargin)
0002 %DRAWELLIPSECYLINDER Draw a cylinder with ellipse cross-section.
0003 %
0004 %   drawEllipseCylinder(CYL)
0005 %   draws the cylinder CYL on the current axis.
0006 %   CYL is a cylinder defined by [x1 y1 z1 x2 y2 z2 r1 r2 roll], with:
0007 %   * [x1 y2 z1] are coordinates of starting point,
0008 %   * [x2 y2 z2] are coordinates of ending point,
0009 %   * R1 and R2 are the lengths of the ellipse semi axes, and
0010 %   * ROLL is the rotation of the cylinder around its main axis (in
0011 %      degrees)
0012 %
0013 %   drawEllipseCylinder(CYL, N)
0014 %   uses N points for discretisation of angle. Default value is 32.
0015 %
0016 %   drawEllipseCylinder(..., OPT)
0017 %   with OPT = 'open' (default) or 'closed', specify if bases of the
0018 %   cylinder should be drawn.
0019 %
0020 %   drawEllipseCylinder(..., 'FaceColor', COLOR)
0021 %   Specifies the color of the cylinder. Any couple of parameters name and
0022 %   value can be given as argument, and will be transfered to the 'surf'
0023 %   matlab function
0024 %
0025 %   H = drawEllipseCylinder(...)
0026 %   returns a handle to the patch representing the cylinder.
0027 %
0028 %
0029 %   Example:
0030 %     figure; drawEllipseCylinder([0 0 0 10 20 30 5 2]);
0031 %
0032 %     figure; drawEllipseCylinder([0 0 0 10 20 30 5 2], 'open');
0033 %
0034 %     figure; drawEllipseCylinder([0 0 0 10 20 30 5 2], 'FaceColor', 'r');
0035 %
0036 %     figure;
0037 %     h = drawEllipseCylinder([0 0 0 10 20 30 5 2]);
0038 %     set(h, 'facecolor', 'b');
0039 %
0040 %     % Draw three mutually intersecting elliptic cylinders
0041 %     p1 = [30 0 0];
0042 %     p2 = [0 30 0];
0043 %     p3 = [0 0 30];
0044 %     radii = [20 10];
0045 %     figure;
0046 %     drawEllipseCylinder([-p1 p1 radii 0], 'FaceColor', 'r');
0047 %     hold on
0048 %     drawEllipseCylinder([-p2 p2 radii 90], 'FaceColor', 'g');
0049 %     drawEllipseCylinder([-p3 p3 radii 90], 'FaceColor', 'b');
0050 %     axis equal
0051 %     set(gcf, 'renderer', 'opengl')
0052 %     view([60 30]); light;
0053 %
0054 %   See Also:
0055 %   drawCylinder, drawSphere, cylinderMesh, drawLine3d, surf
0056 
0057 %   ---------
0058 %   author : David Legland
0059 %   INRA - TPV URPOI - BIA IMASTE
0060 %   created the 27/02/2014
0061 
0062 
0063 %   HISTORY
0064 
0065 
0066 %% Input argument processing
0067 
0068 if iscell(cyl)
0069     res = zeros(length(cyl), 1);
0070     for i = 1:length(cyl)
0071         res(i) = drawEllipseCylinder(cyl{i}, varargin{:});
0072     end
0073     
0074     if nargout > 0
0075         varargout{1} = res;
0076     end    
0077     return;
0078 end
0079 
0080 % default values
0081 N = 32;
0082 closed = true;
0083 
0084 % check number of discretization steps
0085 if ~isempty(varargin)
0086     var = varargin{1};
0087     if isnumeric(var)
0088         N = var;
0089         varargin = varargin(2:end);
0090     end
0091 end
0092 
0093 % check if cylinder must be closed or open
0094 if ~isempty(varargin)
0095     var = varargin{1};
0096     if ischar(var)
0097         if strncmpi(var, 'open', 4)
0098             closed = false;
0099             varargin = varargin(2:end);
0100         elseif strncmpi(var, 'closed', 5)
0101             closed = true;
0102             varargin = varargin(2:end);
0103         end
0104     end
0105 end
0106 
0107 
0108 %% Computation of mesh coordinates
0109 
0110 % extreme points of cylinder
0111 p1 = cyl(1:3);
0112 p2 = cyl(4:6);
0113 
0114 % radius of cylinder
0115 r1 = cyl(7);
0116 r2 = cyl(8);
0117 roll = 0;
0118 if size(cyl, 2) > 8
0119     roll = cyl(9);
0120 end
0121 
0122 % compute orientation angle of cylinder (in degrees)
0123 [theta, phi, rho] = cart2sph2d(p2 - p1);
0124 dphi = linspace(0, 2*pi, N+1);
0125 
0126 % generate a cylinder oriented upwards
0127 x = repmat(cos(dphi) * r1, [2 1]);
0128 y = repmat(sin(dphi) * r2, [2 1]);
0129 z = repmat([0 ; rho], [1 length(dphi)]);
0130 
0131 % transform points
0132 trans   = localToGlobal3d(p1, theta, phi, roll);
0133 pts     = transformPoint3d([x(:) y(:) z(:)], trans);
0134 
0135 % reshape transformed points
0136 x2 = reshape(pts(:,1), size(x));
0137 y2 = reshape(pts(:,2), size(x));
0138 z2 = reshape(pts(:,3), size(x));
0139 
0140 
0141 %% Display cylinder mesh
0142 
0143 % add default drawing options
0144 varargin = [{'FaceColor', 'g', 'edgeColor', 'none'} varargin];
0145 
0146 % plot the cylinder as a surface
0147 hSurf = surf(x2, y2, z2, varargin{:});
0148 
0149 % eventually plot the ends of the cylinder
0150 if closed
0151     ind = find(strcmpi(varargin, 'facecolor'), 1, 'last');
0152     if isempty(ind)
0153         color = 'k';
0154     else
0155         color = varargin{ind+1};
0156     end
0157 
0158     patch(x2(1,:)', y2(1,:)', z2(1,:)', color, 'edgeColor', 'none');
0159     patch(x2(2,:)', y2(2,:)', z2(2,:)', color, 'edgeColor', 'none');
0160 end
0161 
0162 % format ouptut
0163 if nargout == 1
0164     varargout{1} = hSurf;
0165 end

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