REVOLUTIONSURFACE Create a surface of revolution from a planar curve. usage [X Y Z] = revolutionSurface(C1, C2, N); create the surface of revolution of parametrized function (xt, yt), with N+1 equally spaced slices, around the Oz axis. It assumed that C1 corresponds to the x coordinate, and that C2 corresponds to the Oz coordinate. [X Y Z] = revolutionSurface(CURVE, N); is the same, but generating curve is given in a single parameter CURVE, which is a [Nx2] array of 2D points. [X Y Z] = revolutionSurface(..., THETA) where THETA is a vector, uses values of THETA for computing revolution angles. [X Y Z] = revolutionSurface(..., LINE); where LINE is a 1x4 array, specifes the revolution axis in the coordinate system of the curve. LINE is a row vector of 4 parameters, containing [x0 y0 dx dy], where (x0,y0) is the origin of the line and (dx,dy) is a direction vector of the line. The resulting revolution surface still has Oz axis as symmetry axis. It can be transformed using transformPoint3d function. Surface can be displayed using : H = surf(X, Y, Z); H is a handle to the created patch. revolutionSurface(...); by itself, directly shows the created patch. Example % draws a piece of torus circle = circleAsPolygon([10 0 3], 50); [x y z] = revolutionSurface(circle, linspace(0, 4*pi/3, 50)); surf(x, y, z); axis equal; See also surf, transformPoint3d, drawSphere, drawTorus, drawEllipsoid surfature (on Matlab File Exchange) ------ Author: David Legland e-mail: david.legland@grignon.inra.fr Created: 2004-04-09 Copyright 2005 INRA - CEPIA Nantes - MIAJ Jouy-en-Josas.
0001 function varargout = revolutionSurface(varargin) 0002 %REVOLUTIONSURFACE Create a surface of revolution from a planar curve. 0003 % 0004 % usage 0005 % [X Y Z] = revolutionSurface(C1, C2, N); 0006 % create the surface of revolution of parametrized function (xt, yt), 0007 % with N+1 equally spaced slices, around the Oz axis. 0008 % It assumed that C1 corresponds to the x coordinate, and that C2 0009 % corresponds to the Oz coordinate. 0010 % 0011 % [X Y Z] = revolutionSurface(CURVE, N); 0012 % is the same, but generating curve is given in a single parameter CURVE, 0013 % which is a [Nx2] array of 2D points. 0014 % 0015 % [X Y Z] = revolutionSurface(..., THETA) 0016 % where THETA is a vector, uses values of THETA for computing revolution 0017 % angles. 0018 % 0019 % [X Y Z] = revolutionSurface(..., LINE); 0020 % where LINE is a 1x4 array, specifes the revolution axis in the 0021 % coordinate system of the curve. LINE is a row vector of 4 parameters, 0022 % containing [x0 y0 dx dy], where (x0,y0) is the origin of the line and 0023 % (dx,dy) is a direction vector of the line. 0024 % The resulting revolution surface still has Oz axis as symmetry axis. It 0025 % can be transformed using transformPoint3d function. 0026 % Surface can be displayed using : 0027 % H = surf(X, Y, Z); 0028 % H is a handle to the created patch. 0029 % 0030 % revolutionSurface(...); 0031 % by itself, directly shows the created patch. 0032 % 0033 % Example 0034 % % draws a piece of torus 0035 % circle = circleAsPolygon([10 0 3], 50); 0036 % [x y z] = revolutionSurface(circle, linspace(0, 4*pi/3, 50)); 0037 % surf(x, y, z); 0038 % axis equal; 0039 % 0040 % 0041 % 0042 % See also 0043 % surf, transformPoint3d, drawSphere, drawTorus, drawEllipsoid 0044 % surfature (on Matlab File Exchange) 0045 % 0046 % 0047 % ------ 0048 % Author: David Legland 0049 % e-mail: david.legland@grignon.inra.fr 0050 % Created: 2004-04-09 0051 % Copyright 2005 INRA - CEPIA Nantes - MIAJ Jouy-en-Josas. 0052 0053 % based on function cylinder from matlab 0054 % 31/06/2006 fix bug when passing 3 parameters 0055 % 20/04/2007 rewrite processing of input parameters, add psb to specify 0056 % revolution axis 0057 % 24/10/2008 fix angle vector 0058 % 29/07/2010 doc update 0059 0060 0061 %% Initialisations 0062 0063 % default values 0064 0065 % use revolution using the full unit circle, decomposed into 24 angular 0066 % segments (thus, some vertices correspond to particular angles 30°, 0067 % 45°...) 0068 theta = linspace(0, 2*pi, 25); 0069 0070 % use planar vertical axis as default revolution axis 0071 revol = [0 0 0 1]; 0072 0073 % extract the generating curve 0074 var = varargin{1}; 0075 if size(var, 2)==1 0076 xt = var; 0077 yt = varargin{2}; 0078 varargin(1:2) = []; 0079 else 0080 xt = var(:,1); 0081 yt = var(:,2); 0082 varargin(1) = []; 0083 end 0084 0085 % extract optional parameters: angles, axis of revolution 0086 % parameters are identified from their length 0087 while ~isempty(varargin) 0088 var = varargin{1}; 0089 0090 if length(var) == 4 0091 % axis of rotation in the base plane 0092 revol = var; 0093 0094 elseif length(var) == 1 0095 % number of points -> create row vector of angles 0096 theta = linspace(0, 2*pi, var); 0097 0098 else 0099 % use all specified angle values 0100 theta = var(:)'; 0101 0102 end 0103 varargin(1) = []; 0104 end 0105 0106 0107 %% Create revolution surface 0108 0109 % ensure length is enough 0110 m = length(xt); 0111 if m==1 0112 xt = [xt xt]; 0113 end 0114 0115 % ensure x and y are vertical vectors 0116 xt = xt(:); 0117 yt = yt(:); 0118 0119 % transform xt and yt to replace in the reference of the revolution axis 0120 tra = createTranslation(-revol(1:2)); 0121 rot = createRotation(pi/2 - lineAngle(revol)); 0122 [xt, yt] = transformPoint(xt, yt, tra*rot); 0123 0124 % compute surface vertices 0125 x = xt * cos(theta); 0126 y = xt * sin(theta); 0127 z = yt * ones(size(theta)); 0128 0129 0130 %% Process output arguments 0131 0132 % format output depending on how many output parameters are required 0133 if nargout == 0 0134 % draw the revolution surface 0135 surf(x, y, z); 0136 0137 elseif nargout == 1 0138 % draw the surface and return a handle to the shown structure 0139 h = surf(x, y, z); 0140 varargout{1} = h; 0141 0142 elseif nargout == 3 0143 % return computed mesh 0144 varargout = {x, y, z}; 0145 end 0146 0147