1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
## Copyright (C) 2024 David Legland
## All rights reserved.
##
## Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions are met:
##
## 1 Redistributions of source code must retain the above copyright notice,
## this list of conditions and the following disclaimer.
## 2 Redistributions in binary form must reproduce the above copyright
## notice, this list of conditions and the following disclaimer in the
## documentation and/or other materials provided with the distribution.
##
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS''
## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
## ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
## SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
## CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
##
## The views and conclusions contained in the software and documentation are
## those of the authors and should not be interpreted as representing official
## policies, either expressed or implied, of the copyright holders.
function varargout = subdivideMesh(vertices, faces, n)
%SUBDIVIDEMESH Subdivides each face of the mesh.
%
% [V2 F2] = subdivideMesh(V, F, N)
% Subdivides the mesh specified by (V,F) such that each face F is divided
% into N^2 smaller faces.
%
% Example
% [v, f] = createOctahedron;
% figure; drawMesh(v, f); view(3);
% [v2, f2] = subdivideMesh(v, f, 4);
% figure; drawMesh(v2, f2); view(3)
%
% See also
% meshes3d, drawMesh
%
% ------
% Author: David Legland
% E-mail: david.legland@inrae.fr
% Created: 2013-08-22, using Matlab 7.9.0.529 (R2009b)
% Copyright 2013-2023 INRA - Cepia Software Platform
%% Initialisations
% vertex to vertex edges, will be computed if not provided within mesh
% structure
edges = [];
% The face-to-edge adjacency information is necessary for associating new
% faces to vertices (will be computed if not found)
faceEdgeIndices = [];
% if mesh is provided as structure, retrieve all possible data
if isstruct(vertices)
% get relevant inputs
mesh = vertices;
n = faces;
% parse fields from a mesh structure
vertices = mesh.vertices;
faces = mesh.faces;
if isfield(mesh, 'edges')
edges = mesh.edges;
end
% The face-to-edge adjacency information is necessary for associating
% new faces to vertices
% (will be computed if not found)
if isfield(mesh, 'faceEdges')
faceEdgeIndices = mesh.faceEdges;
end
end
if ~isnumeric(faces) || size(faces, 2) ~= 3
error('Requires a triangular mesh');
end
% compute the edge array
if isempty(edges)
edges = meshEdges(faces);
end
nEdges = size(edges, 1);
% compute index of edges around each face if not already provided
if isempty(faceEdgeIndices)
faceEdgeIndices = meshFaceEdges(vertices, edges, faces);
end
%% Process Edges
% Create new vertices on existing edges. Each edge is subdivided into n new
% edges, creating (n-1) new vertices.
% positions to interpolate vertex positions
t = linspace(0, 1, n + 1)';
coef2 = t(2:end-1);
coef1 = 1 - t(2:end-1);
% initialise the array of new vertices
vertices2 = vertices;
% keep an array containing index of new vertices for each original edge
edgeNewVertexIndices = zeros(nEdges, n-1);
% create new vertices on each edge
for iEdge = 1:nEdges
% extract each extremity as a point
v1 = vertices(edges(iEdge, 1), :);
v2 = vertices(edges(iEdge, 2), :);
% compute new points
newPoints = coef1 * v1 + coef2 * v2;
% add new vertices, and keep their indices
edgeNewVertexIndices(iEdge,:) = size(vertices2, 1) + (1:n-1);
vertices2 = [vertices2 ; newPoints]; %#ok<AGROW>
end
%% Process faces
% Subdivide each face, by processing 'strips' on parallel faces. Each strip
% rely on two vertices of two edges of the original mesh.
% create result array (will grow during face iteration)
faces2 = zeros(0, 3);
% iterate on faces of original mesh
nFaces = size(faces, 1);
for iFace = 1:nFaces
% compute index of each corner vertex
face = faces(iFace, :);
iv1 = face(1);
iv2 = face(2);
iv3 = face(3);
% compute index of each edge
faceEdges = faceEdgeIndices{iFace};
ie1 = faceEdges(1);
ie2 = faceEdges(2);
ie3 = faceEdges(3);
% indices of new vertices on edges
edge1NewVertexIndices = edgeNewVertexIndices(ie1, :);
edge2NewVertexIndices = edgeNewVertexIndices(ie2, :);
edge3NewVertexIndices = edgeNewVertexIndices(ie3, :);
% keep vertex 1 as reference for edges 1 and 3
if edges(ie1, 1) ~= iv1
edge1NewVertexIndices = edge1NewVertexIndices(end:-1:1);
end
if edges(ie3, 1) ~= iv1
edge3NewVertexIndices = edge3NewVertexIndices(end:-1:1);
end
% create the first new face, on 'top' of the original face
topVertexInds = [edge1NewVertexIndices(1) edge3NewVertexIndices(1)];
newFace = [iv1 topVertexInds];
faces2 = [faces2; newFace]; %#ok<AGROW>
% iterate over middle strips
for iStrip = 2:n-1
% index of extreme vertices of current row
ivr1 = edge1NewVertexIndices(iStrip);
ivr2 = edge3NewVertexIndices(iStrip);
% extreme vertices as points
v1 = vertices2(ivr1, :);
v2 = vertices2(ivr2, :);
% create additional vertices within the bottom row of the strip
t = linspace(0, 1, iStrip+1)';
coef2 = t(2:end-1);
coef1 = 1 - t(2:end-1);
newPoints = coef1 * v1 + coef2 * v2;
% compute indices of new vertices in result array
newInds = size(vertices2, 1) + (1:iStrip-1);
botVertexInds = [ivr1 newInds ivr2];
% add new vertices
vertices2 = [vertices2 ; newPoints]; %#ok<AGROW>
% create top faces of current strip
for k = 1:iStrip-1
newFace = [topVertexInds(k) botVertexInds(k+1) topVertexInds(k+1)];
faces2 = [faces2; newFace]; %#ok<AGROW>
end
% create bottom faces of current strip
for k = 1:iStrip
newFace = [topVertexInds(k) botVertexInds(k) botVertexInds(k+1)];
faces2 = [faces2; newFace]; %#ok<AGROW>
end
% bottom vertices of current strip are top vertices of next strip
topVertexInds = botVertexInds;
end
% for edge 2, keep vertex 2 of the current face as reference
if edges(ie2, 1) ~= iv2
edge2NewVertexIndices = edge2NewVertexIndices(end:-1:1);
end
% consider new vertices together with extremities
botVertexInds = [iv2 edge2NewVertexIndices iv3];
% create top faces for last strip
for k = 1:n-1
newFace = [topVertexInds(k) botVertexInds(k+1) topVertexInds(k+1)];
faces2 = [faces2; newFace]; %#ok<AGROW>
end
% create bottom faces for last strip
for k = 1:n
newFace = [topVertexInds(k) botVertexInds(k) botVertexInds(k+1)];
faces2 = [faces2; newFace]; %#ok<AGROW>
end
end
%% Post-processing
% setup output arguments
varargout = formatMeshOutput(nargout, vertices2, faces2);
|