Home > matGeom > geom3d > drawCapsule.m

drawCapsule

PURPOSE ^

DRAWCAPSULE Draw a capsule.

SYNOPSIS ^

function varargout = drawCapsule(varargin)

DESCRIPTION ^

DRAWCAPSULE Draw a capsule.

   drawCapsule(CAP)
   Draws the capsule CAP on the current axis. 
   CAP 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 and the two semi-spheres at the ends

   drawCapsule(CAP, N)
   Uses N points for discretizating the circles of the cylinder and the
   semi-spheres (domes). Default value is 32. 

   drawCapsule(..., 'FaceColor', COLOR)
   Specifies the color of the capsule. Any couple of parameters name and
   value can be given as argument, and will be transfered to the 'surf'
   matlab function

   drawCapsule(..., 'FaceAlpha', ALPHA)
   Specifies the transparency of the capsule and of the semi-spheres.
 
   drawCapsule(..., NAME, VALUE);
   Specifies one or several options using parameter name-value pairs.
   Available options are usual drawing options, as well as:
   'nPhi'    the number of arcs used for drawing the meridians
             (for the semi-spheres and the cylinder)
   'nTheta'  the number of circles used for drawing the parallels
             (only for the semi-spheres at the ends of the capsule)

   drawCapsule(AX, ...)
   Specifies the axis to draw on. AX should be a valid axis handle.

   H = drawCapsule(...)
   Returns a handle to the patch representing the capsule.


   Examples:
   % basic example
     figure; drawCapsule([0 0 0 10 20 30 5]);

   % change capsule color
     figure; drawCapsule([0 0 0 10 20 30 5], 'FaceColor', 'r');

   % change capsule color using graphical handle
     figure;
     h = drawCapsule([0 0 0 10 20 30 5]);
     set(h, 'facecolor', 'b');

   % Draw three mutually intersecting capsules
     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
     drawCapsule([p0 p1 10], 'FaceColor', 'r');
     drawCapsule([p0 p2 10], 'FaceColor', 'g');
     drawCapsule([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);
     caps = [v(e(:,1), :) v(e(:,2),:) repmat(0.1, size(e, 1), 1)];
     drawCapsule(caps);
     light
 
   % Draw a capsule with high resolution
     figure;
     h = drawCapsule([10,20,10,50,70,40,6], 'nPhi', 360, 'nTheta', 180);
     l = light; view(3);
     

   See also
     drawCylinder, drawDome, drawSphere

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = drawCapsule(varargin)
0002 %DRAWCAPSULE Draw a capsule.
0003 %
0004 %   drawCapsule(CAP)
0005 %   Draws the capsule CAP on the current axis.
0006 %   CAP 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 and the two semi-spheres at the ends
0010 %
0011 %   drawCapsule(CAP, N)
0012 %   Uses N points for discretizating the circles of the cylinder and the
0013 %   semi-spheres (domes). Default value is 32.
0014 %
0015 %   drawCapsule(..., 'FaceColor', COLOR)
0016 %   Specifies the color of the capsule. Any couple of parameters name and
0017 %   value can be given as argument, and will be transfered to the 'surf'
0018 %   matlab function
0019 %
0020 %   drawCapsule(..., 'FaceAlpha', ALPHA)
0021 %   Specifies the transparency of the capsule and of the semi-spheres.
0022 %
0023 %   drawCapsule(..., NAME, VALUE);
0024 %   Specifies one or several options using parameter name-value pairs.
0025 %   Available options are usual drawing options, as well as:
0026 %   'nPhi'    the number of arcs used for drawing the meridians
0027 %             (for the semi-spheres and the cylinder)
0028 %   'nTheta'  the number of circles used for drawing the parallels
0029 %             (only for the semi-spheres at the ends of the capsule)
0030 %
0031 %   drawCapsule(AX, ...)
0032 %   Specifies the axis to draw on. AX should be a valid axis handle.
0033 %
0034 %   H = drawCapsule(...)
0035 %   Returns a handle to the patch representing the capsule.
0036 %
0037 %
0038 %   Examples:
0039 %   % basic example
0040 %     figure; drawCapsule([0 0 0 10 20 30 5]);
0041 %
0042 %   % change capsule color
0043 %     figure; drawCapsule([0 0 0 10 20 30 5], 'FaceColor', 'r');
0044 %
0045 %   % change capsule color using graphical handle
0046 %     figure;
0047 %     h = drawCapsule([0 0 0 10 20 30 5]);
0048 %     set(h, 'facecolor', 'b');
0049 %
0050 %   % Draw three mutually intersecting capsules
0051 %     p0 = [10 10 10];
0052 %     p1 = p0 + 80 * [1 0 0];
0053 %     p2 = p0 + 80 * [0 1 0];
0054 %     p3 = p0 + 80 * [0 0 1];
0055 %     figure; axis equal; axis([0 100 0 100 0 100]); hold on
0056 %     drawCapsule([p0 p1 10], 'FaceColor', 'r');
0057 %     drawCapsule([p0 p2 10], 'FaceColor', 'g');
0058 %     drawCapsule([p0 p3 10], 'FaceColor', 'b');
0059 %     axis equal
0060 %     set(gcf, 'renderer', 'opengl')
0061 %     view([60 30]); light;
0062 %
0063 %   % draw cube skeleton
0064 %     [v, e, f] = createCube;
0065 %     figure; axis equal; axis([-0.2 1.2 -0.2 1.2 -0.2 1.2]); hold on; view(3);
0066 %     caps = [v(e(:,1), :) v(e(:,2),:) repmat(0.1, size(e, 1), 1)];
0067 %     drawCapsule(caps);
0068 %     light
0069 %
0070 %   % Draw a capsule with high resolution
0071 %     figure;
0072 %     h = drawCapsule([10,20,10,50,70,40,6], 'nPhi', 360, 'nTheta', 180);
0073 %     l = light; view(3);
0074 %
0075 %
0076 %   See also
0077 %     drawCylinder, drawDome, drawSphere
0078 %
0079 
0080 % ------
0081 % Author: Moritz Schappler
0082 % E-mail: N/A
0083 % Created: 2013-07-27
0084 % Copyright 2013-2024
0085 
0086 %% Input argument processing
0087 
0088 % extract handle of axis to draw on
0089 [hAx, varargin] = parseAxisHandle(varargin{:});
0090 
0091 % input argument representing capsules
0092 cap = varargin{1};
0093 varargin(1) = [];
0094 
0095 % process the case of multiple capsules
0096 if iscell(cap)
0097     hCaps = gobjects(length(cap), 1);
0098     for i = 1:length(cap)
0099         hCaps(i) = drawCapsule(hAx, cap{i}, varargin{:});
0100     end
0101     if nargout > 0
0102         varargout{1} = hCaps;
0103     end    
0104     return;
0105 elseif size(cap, 1) > 1
0106     hCaps = gobjects(size(cap, 1), 3);
0107     for i = 1:size(cap, 1)
0108         hCaps(i,:) = drawCapsule(hAx, cap(i, :), varargin{:});
0109     end
0110     
0111     if nargout > 0
0112         varargout{1} = hCaps;
0113     end    
0114     return;
0115 end
0116 
0117 faceColor = 'g';
0118 ind = find(strcmpi(varargin, 'FaceColor'), 1, 'last');
0119 if ~isempty(ind)
0120     faceColor = varargin{ind+1};
0121     varargin(ind:ind+1) = [];
0122 end
0123 
0124 % extract transparency
0125 alpha = 1;
0126 ind = find(strcmpi(varargin, 'FaceAlpha'), 1, 'last');
0127 if ~isempty(ind)
0128     alpha = varargin{ind+1};
0129     varargin(ind:ind+1) = [];
0130 end
0131 
0132 % add default drawing options
0133 varargin = [{'FaceColor', faceColor, 'edgeColor', 'none', 'FaceAlpha', alpha} varargin];
0134 
0135 % adjust drawing options for the cylinder. Options nPhi and nTheta may only
0136 % be given to the function drawDome, not drawCylinder
0137 options_cyl = ['open', varargin];
0138 ind = find(strcmpi(options_cyl, 'nPhi'), 1, 'last');
0139 if ~isempty(ind)
0140     ind = ind(1);
0141     nPhi = options_cyl{ind+1};
0142     options_cyl(ind:ind+1) = [];
0143     options_cyl = [nPhi, options_cyl];
0144 end
0145 ind = find(strcmpi(options_cyl, 'nTheta'), 1, 'last');
0146 if ~isempty(ind)
0147     options_cyl(ind:ind+1) = [];
0148 end
0149 
0150 % save hold state
0151 holdState = ishold(hAx);
0152 hold(hAx, 'on');
0153 
0154 if all(cap(1:3) == cap(4:6))
0155     % the capsule is only a sphere. take arbitrary axis to be able to plot
0156     cap(4:6) = cap(1:3)+eps*([0 0 1]);
0157     h1 = 0;
0158 else
0159     h1 = drawCylinder(cap, options_cyl{:});
0160 end
0161 h2 = drawDome(cap([1:3,7]),  (cap(1:3)-cap(4:6)), varargin{:});
0162 h3 = drawDome(cap([4:6,7]), -(cap(1:3)-cap(4:6)), varargin{:});
0163 
0164 % restore hold state
0165 if ~holdState
0166     hold(hAx, 'off');
0167 end
0168 
0169 % return handles
0170 if nargout == 1
0171     varargout{1} = [h1, h2, h3];
0172 end

Generated on Thu 21-Nov-2024 11:30:22 by m2html © 2003-2022