Home > matGeom > meshes3d > curveToMesh.m

curveToMesh

PURPOSE ^

Create a mesh surrounding a 3D curve.

SYNOPSIS ^

function [vertices, faces] = curveToMesh(curve, varargin)

DESCRIPTION ^

 Create a mesh surrounding a 3D curve.

   [V, F] = curveToMesh(CURVE)
   Computes the vertices and the faces of the mesh surrounding the
   specified 3D curve.

   [V, F] = curveToMesh(CURVE, THICKNESS)
   Specifies the thickness of the mesh (distance between mesh vertices and
   curve vertices). Default is 0.5.

   [V, F] = curveToMesh(CURVE, THICKNESS, NCORNERS)
   Also specifies the number of mesh vertices around each curve vertex.
   Default is 8.


   Example
     % Creates a tubular mesh around a trefoil knot curve
     t = linspace(0, 2*pi, 200)';
     x = sin(t) + 2 * sin(2 * t);
     y = cos(t) - 2 * cos(2 * t);
     z = -sin(3 * t);
     curve = [x, y, z];
     [v2, f2] = curveToMesh(curve, .5, 16);
     figure; 
     drawMesh(v2, f2);
     axis equal; view(3);
     axis([-4 4 -4 4 -2 2]);
  
   See also
     meshes3d, torusMesh, surfToMesh

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [vertices, faces] = curveToMesh(curve, varargin)
0002 % Create a mesh surrounding a 3D curve.
0003 %
0004 %   [V, F] = curveToMesh(CURVE)
0005 %   Computes the vertices and the faces of the mesh surrounding the
0006 %   specified 3D curve.
0007 %
0008 %   [V, F] = curveToMesh(CURVE, THICKNESS)
0009 %   Specifies the thickness of the mesh (distance between mesh vertices and
0010 %   curve vertices). Default is 0.5.
0011 %
0012 %   [V, F] = curveToMesh(CURVE, THICKNESS, NCORNERS)
0013 %   Also specifies the number of mesh vertices around each curve vertex.
0014 %   Default is 8.
0015 %
0016 %
0017 %   Example
0018 %     % Creates a tubular mesh around a trefoil knot curve
0019 %     t = linspace(0, 2*pi, 200)';
0020 %     x = sin(t) + 2 * sin(2 * t);
0021 %     y = cos(t) - 2 * cos(2 * t);
0022 %     z = -sin(3 * t);
0023 %     curve = [x, y, z];
0024 %     [v2, f2] = curveToMesh(curve, .5, 16);
0025 %     figure;
0026 %     drawMesh(v2, f2);
0027 %     axis equal; view(3);
0028 %     axis([-4 4 -4 4 -2 2]);
0029 %
0030 %   See also
0031 %     meshes3d, torusMesh, surfToMesh
0032  
0033 % ------
0034 % Author: David Legland
0035 % e-mail: david.legland@inra.fr
0036 % Created: 2015-01-07,    using Matlab 8.4.0.150421 (R2014b)
0037 % Copyright 2015 INRA - Cepia Software Platform.
0038 
0039 radius = .1;
0040 if nargin > 1
0041     radius = varargin{1};
0042 end
0043 
0044 nCorners = 8;
0045 if nargin > 2
0046     nCorners = varargin{2};
0047 end
0048 
0049 nNodes = size(curve, 1);
0050 nVerts = nNodes * nCorners;
0051 
0052 vertices = zeros(nVerts, 3);
0053 
0054 % create reference corners, that will be rotated and translated
0055 t = linspace(0, 2*pi, nCorners + 1)';
0056 t(end) = [];
0057 baseCorners = radius * [cos(t) sin(t) zeros(size(t))];
0058 
0059 for iNode = 1:nNodes
0060     % coordinate of current node
0061     node = curve(iNode, :);
0062     
0063     % compute local tangent vector
0064     iNext = mod(iNode, nNodes) + 1;
0065     tangentVector = normalizeVector3d(curve(iNext, :) - node);
0066 
0067     % convert to spherical coordinates
0068     [theta, phi, rho] = cart2sph2(tangentVector); %#ok<ASGLU>
0069     
0070     % apply transformation to place corners around current node
0071     rotY = createRotationOy(theta);
0072     rotZ = createRotationOz(phi);
0073     trans = createTranslation3d(node);
0074     transformMatrix = trans * rotZ * rotY;
0075     corners = transformPoint3d(baseCorners, transformMatrix);
0076     
0077     % concatenate with other corners
0078     vertices( (1:nCorners) + (iNode - 1) * nCorners, :) = corners;
0079 end
0080 
0081 % indices of vertices
0082 inds = (1:nVerts)';
0083 add1 = repmat([ones(nCorners-1, 1) ; 1-nCorners], nNodes, 1);
0084 
0085 % generate faces
0086 faces = [inds ...
0087     mod(inds + add1 - 1, nVerts) + 1 ...
0088     mod(inds + nCorners + add1 - 1, nVerts) + 1 ...
0089     mod(inds + nCorners - 1, nVerts) + 1];
0090

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019