0001 function [vertices2, faces2] = subdivideMesh(vertices, faces, n)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027 edges = [];
0028 faceEdgeIndices = [];
0029
0030 if isstruct(vertices)
0031
0032 mesh = vertices;
0033 n = faces;
0034
0035
0036 vertices = mesh.vertices;
0037 faces = mesh.faces;
0038 if isfield(mesh, 'edges')
0039 edges = mesh.edges;
0040 end
0041 if isfield(mesh, 'faceEdges')
0042 faceEdgeIndices = mesh.faceEdges;
0043 end
0044
0045 end
0046
0047 if ~isnumeric(faces) || size(faces, 2) ~= 3
0048 error('Requires a triangular mesh');
0049 end
0050
0051
0052 if isempty(edges)
0053 edges = meshEdges(faces);
0054 end
0055 nEdges = size(edges, 1);
0056
0057
0058 if isempty(faceEdgeIndices)
0059 faceEdgeIndices = meshFaceEdges(vertices, edges, faces);
0060 end
0061
0062
0063
0064
0065
0066 t = linspace(0, 1, n + 1)';
0067 coef2 = t(2:end-1);
0068 coef1 = 1 - t(2:end-1);
0069
0070
0071 vertices2 = vertices;
0072
0073
0074 edgeNewVertexIndices = zeros(nEdges, n-1);
0075
0076
0077 for iEdge = 1:nEdges
0078
0079 v1 = vertices(edges(iEdge, 1), :);
0080 v2 = vertices(edges(iEdge, 2), :);
0081
0082
0083 newPoints = coef1 * v1 + coef2 * v2;
0084
0085
0086 edgeNewVertexIndices(iEdge,:) = size(vertices2, 1) + (1:n-1);
0087 vertices2 = [vertices2 ; newPoints];
0088 end
0089
0090
0091
0092
0093 faces2 = zeros(0, 3);
0094
0095 nFaces = size(faces, 1);
0096 for iFace = 1:nFaces
0097
0098 face = faces(iFace, :);
0099 iv1 = face(1);
0100 iv2 = face(2);
0101 iv3 = face(3);
0102
0103
0104 faceEdges = faceEdgeIndices{iFace};
0105 ie1 = faceEdges(1);
0106 ie2 = faceEdges(2);
0107 ie3 = faceEdges(3);
0108
0109
0110 edge1NewVertexIndices = edgeNewVertexIndices(ie1, :);
0111 edge2NewVertexIndices = edgeNewVertexIndices(ie2, :);
0112 edge3NewVertexIndices = edgeNewVertexIndices(ie3, :);
0113
0114
0115 if edges(ie1, 1) ~= iv1
0116 edge1NewVertexIndices = edge1NewVertexIndices(end:-1:1);
0117 end
0118 if edges(ie3, 1) ~= iv1
0119 edge3NewVertexIndices = edge3NewVertexIndices(end:-1:1);
0120 end
0121
0122
0123 topVertexInds = [edge1NewVertexIndices(1) edge3NewVertexIndices(1)];
0124 newFace = [iv1 topVertexInds];
0125 faces2 = [faces2; newFace];
0126
0127
0128 for iStrip = 2:n-1
0129
0130 ivr1 = edge1NewVertexIndices(iStrip);
0131 ivr2 = edge3NewVertexIndices(iStrip);
0132
0133
0134 v1 = vertices2(ivr1, :);
0135 v2 = vertices2(ivr2, :);
0136
0137
0138 t = linspace(0, 1, iStrip+1)';
0139 coef2 = t(2:end-1);
0140 coef1 = 1 - t(2:end-1);
0141 newPoints = coef1 * v1 + coef2 * v2;
0142
0143
0144 newInds = size(vertices2, 1) + (1:iStrip-1);
0145 botVertexInds = [ivr1 newInds ivr2];
0146
0147
0148 vertices2 = [vertices2 ; newPoints];
0149
0150
0151 for k = 1:iStrip-1
0152 newFace = [topVertexInds(k) botVertexInds(k+1) topVertexInds(k+1)];
0153 faces2 = [faces2; newFace];
0154 end
0155
0156
0157 for k = 1:iStrip
0158 newFace = [topVertexInds(k) botVertexInds(k) botVertexInds(k+1)];
0159 faces2 = [faces2; newFace];
0160 end
0161
0162
0163 topVertexInds = botVertexInds;
0164 end
0165
0166
0167 if edges(ie2, 1) ~= iv2
0168 edge2NewVertexIndices = edge2NewVertexIndices(end:-1:1);
0169 end
0170
0171
0172 botVertexInds = [iv2 edge2NewVertexIndices iv3];
0173
0174
0175 for k = 1:n-1
0176 newFace = [topVertexInds(k) botVertexInds(k+1) topVertexInds(k+1)];
0177 faces2 = [faces2; newFace];
0178 end
0179
0180
0181 for k = 1:n
0182 newFace = [topVertexInds(k) botVertexInds(k) botVertexInds(k+1)];
0183 faces2 = [faces2; newFace];
0184 end
0185
0186 end
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201