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: crawCylinder, drawDome, drawSphere
0001 function varargout = drawCapsule(varargin) 0002 % 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 % crawCylinder, drawDome, drawSphere 0078 % 0079 0080 % --------- 0081 % author: Moritz Schappler 0082 % created the 27/07/2013 0083 % 0084 0085 % HISTORY 0086 % 2013-07-27 initial version as copy of drawCylinder 0087 % 2020-05-18 changes based on current version of geom3d 0088 0089 0090 %% Input argument processing 0091 0092 % parse axis handle 0093 if isAxisHandle(varargin{1}) 0094 hAx = varargin{1}; 0095 varargin(1) = []; 0096 else 0097 hAx = gca; 0098 end 0099 0100 % input argument representing capsules 0101 cap = varargin{1}; 0102 varargin(1) = []; 0103 0104 % process the case of multiple capsules 0105 if iscell(cap) 0106 hCaps = gobjects(length(cap), 1); 0107 for i = 1:length(cap) 0108 hCaps(i) = drawCapsule(hAx, cap{i}, varargin{:}); 0109 end 0110 if nargout > 0 0111 varargout{1} = hCaps; 0112 end 0113 return; 0114 elseif size(cap, 1) > 1 0115 hCaps = gobjects(size(cap, 1), 3); 0116 for i = 1:size(cap, 1) 0117 hCaps(i,:) = drawCapsule(hAx, cap(i, :), varargin{:}); 0118 end 0119 0120 if nargout > 0 0121 varargout{1} = hCaps; 0122 end 0123 return; 0124 end 0125 0126 faceColor = 'g'; 0127 ind = find(strcmpi(varargin, 'FaceColor'), 1, 'last'); 0128 if ~isempty(ind) 0129 faceColor = varargin{ind+1}; 0130 varargin(ind:ind+1) = []; 0131 end 0132 0133 % extract transparency 0134 alpha = 1; 0135 ind = find(strcmpi(varargin, 'FaceAlpha'), 1, 'last'); 0136 if ~isempty(ind) 0137 alpha = varargin{ind+1}; 0138 varargin(ind:ind+1) = []; 0139 end 0140 0141 % add default drawing options 0142 varargin = [{'FaceColor', faceColor, 'edgeColor', 'none', 'FaceAlpha', alpha} varargin]; 0143 0144 % adjust drawing options for the cylinder. Options nPhi and nTheta may only 0145 % be given to the function drawDome, not drawCylinder 0146 options_cyl = ['open', varargin]; 0147 ind = find(strcmpi(options_cyl, 'nPhi'), 1, 'last'); 0148 if ~isempty(ind) 0149 ind = ind(1); 0150 nPhi = options_cyl{ind+1}; 0151 options_cyl(ind:ind+1) = []; 0152 options_cyl = [nPhi, options_cyl]; 0153 end 0154 ind = find(strcmpi(options_cyl, 'nTheta'), 1, 'last'); 0155 if ~isempty(ind) 0156 options_cyl(ind:ind+1) = []; 0157 end 0158 0159 hold on 0160 if all(cap(1:3) == cap(4:6)) 0161 % the capsule is only a sphere. take arbitrary axis to be able to plot 0162 cap(4:6) = cap(1:3)+eps*([0 0 1]); 0163 h1 = 0; 0164 else 0165 h1 = drawCylinder(cap, options_cyl{:}); 0166 end 0167 h2 = drawDome(cap([1:3,7]), (cap(1:3)-cap(4:6)), varargin{:}); 0168 h3 = drawDome(cap([4:6,7]), -(cap(1:3)-cap(4:6)), varargin{:}); 0169 0170 % return handles 0171 if nargout == 1 0172 varargout{1} = [h1, h2, h3]; 0173 end