TORUSMESH 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 %TORUSMESH 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-2024 INRA - Cepia Software Platform 0034 0035 %% Extract data for torus 0036 0037 % check input number 0038 if nargin == 0 0039 torus = [0 0 0 30 10 0 0]; 0040 elseif ischar(torus) 0041 varargin = [{torus} varargin]; 0042 torus = [0 0 0 30 10 0 0]; 0043 end 0044 0045 if isnumeric(torus) && size(torus, 2) ~= 7 0046 error('First argument must be a numeric row vector with 7 elements'); 0047 end 0048 0049 center = torus(1:3); 0050 r1 = torus(4); 0051 r2 = torus(5); 0052 0053 if size(torus, 2) >= 7 0054 normal = torus(6:7); 0055 end 0056 0057 0058 %% Extract data for discretisation 0059 0060 % number 0061 nTheta = 60; 0062 nPhi = 60; 0063 while length(varargin) > 1 0064 argName = varargin{1}; 0065 switch lower(argName) 0066 case 'ntheta' 0067 nTheta = varargin{2}; 0068 case 'nphi' 0069 nPhi = varargin{2}; 0070 otherwise 0071 error('Unknown optional argument: %s', argName); 0072 end 0073 0074 varargin(1:2) = []; 0075 end 0076 0077 0078 %% Discretize torus 0079 0080 % create base circle (duplicate last vertex to manage mesh periodicity) 0081 circle = circleToPolygon([r1 0 r2], nTheta); 0082 circle = circle([1:end 1], :); 0083 % create rotation angle list (duplicate last one to manage mesh periodicity) 0084 phiList = linspace(0, 2*pi, nPhi + 1); 0085 [x, y, z] = revolutionSurface(circle, phiList); 0086 0087 % transform torus 0088 trans = localToGlobal3d([center normal]); 0089 [x, y, z] = transformPoint3d(x, y, z, trans); 0090 0091 % convert to FV mesh 0092 [vertices, faces] = surfToMesh(x, y, z, 'xPeriodic', true, 'yPeriodic', true); 0093 0094 % format output 0095 varargout = formatMeshOutput(nargout, vertices, faces);