Create a 3D mesh representing a torus. [V, F] = torusMesh(TORUS) Converts the torus in TORUS into a face-vertex quadrangular mesh. TORUS is given by [XC YC ZY R1 R2 THETA PHI] where (XC YZ ZC) is the center of the torus, R1 is the main radius, R2 is the radius of the torus section, and (THETA PHI) is the angle of the torus normal vector (both in degrees). [V, F] = torusMesh(TORUS, 'nTheta', NT, 'nPhi', NP) Creates a mesh using NP circles, each circle being discretized with NT vertices. Default are 60 for both parameters. [V, F] = torusMesh() Creates a mesh representing a default torus. Example [v, f] = torusMesh([50 50 50 30 10 30 45]); figure; drawMesh(v, f, 'linestyle', 'none'); view(3); axis equal; lighting gouraud; light; See also meshes3d, drawTorus, revolutionSurface, cylinderMesh, sphereMesh drawMesh
0001 function varargout = torusMesh(torus, varargin) 0002 % Create a 3D mesh representing a torus. 0003 % 0004 % [V, F] = torusMesh(TORUS) 0005 % Converts the torus in TORUS into a face-vertex quadrangular mesh. 0006 % TORUS is given by [XC YC ZY R1 R2 THETA PHI] 0007 % where (XC YZ ZC) is the center of the torus, R1 is the main radius, R2 0008 % is the radius of the torus section, and (THETA PHI) is the angle of the 0009 % torus normal vector (both in degrees). 0010 % 0011 % [V, F] = torusMesh(TORUS, 'nTheta', NT, 'nPhi', NP) 0012 % Creates a mesh using NP circles, each circle being discretized with NT 0013 % vertices. Default are 60 for both parameters. 0014 % 0015 % [V, F] = torusMesh() 0016 % Creates a mesh representing a default torus. 0017 % 0018 % Example 0019 % [v, f] = torusMesh([50 50 50 30 10 30 45]); 0020 % figure; drawMesh(v, f, 'linestyle', 'none'); 0021 % view(3); axis equal; 0022 % lighting gouraud; light; 0023 % 0024 % 0025 % See also 0026 % meshes3d, drawTorus, revolutionSurface, cylinderMesh, sphereMesh 0027 % drawMesh 0028 0029 % ------ 0030 % Author: David Legland 0031 % e-mail: david.legland@inrae.fr 0032 % Created: 2012-10-25, using Matlab 7.9.0.529 (R2009b) 0033 % Copyright 2012 INRA - Cepia Software Platform. 0034 0035 % HISTORY 0036 % 2013-04-30 add support for empty argument 0037 0038 0039 %% Extract data for torus 0040 0041 % check input number 0042 if nargin == 0 0043 torus = [0 0 0 30 10 0 0]; 0044 elseif ischar(torus) 0045 varargin = [{torus} varargin]; 0046 torus = [0 0 0 30 10 0 0]; 0047 end 0048 0049 if isnumeric(torus) && size(torus, 2) ~= 7 0050 error('First argument must be a numeric row vector with 7 elements'); 0051 end 0052 0053 center = torus(1:3); 0054 r1 = torus(4); 0055 r2 = torus(5); 0056 0057 if size(torus, 2) >= 7 0058 normal = torus(6:7); 0059 end 0060 0061 0062 %% Extract data for discretisation 0063 0064 % number 0065 nTheta = 60; 0066 nPhi = 60; 0067 while length(varargin) > 1 0068 argName = varargin{1}; 0069 switch lower(argName) 0070 case 'ntheta' 0071 nTheta = varargin{2}; 0072 case 'nphi' 0073 nPhi = varargin{2}; 0074 otherwise 0075 error('Unknown optional argument: %s', argName); 0076 end 0077 0078 varargin(1:2) = []; 0079 end 0080 0081 0082 %% Discretize torus 0083 0084 % create base circle (duplicate last vertex to manage mesh periodicity) 0085 circle = circleToPolygon([r1 0 r2], nTheta); 0086 circle = circle([1:end 1], :); 0087 % create rotation angle list (duplicate last one to manage mesh periodicity) 0088 phiList = linspace(0, 2*pi, nPhi + 1); 0089 [x, y, z] = revolutionSurface(circle, phiList); 0090 0091 % transform torus 0092 trans = localToGlobal3d([center normal]); 0093 [x, y, z] = transformPoint3d(x, y, z, trans); 0094 0095 % convert to FV mesh 0096 [vertices, faces] = surfToMesh(x, y, z, 'xPeriodic', true, 'yPeriodic', true); 0097 0098 % format output 0099 varargout = formatMeshOutput(nargout, vertices, faces);