-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathremeshTriangleMesh.m
42 lines (35 loc) · 1.22 KB
/
remeshTriangleMesh.m
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
function vtk = remeshTriangleMesh(vtk, numPoints, extraFlags)
if nargin < 3 || isempty(extraFlags)
extraFlags = '';
end
if ~isempty(numPoints)
extraFlags = sprintf('%s -v %i', extraFlags, round(numPoints));
end
% make sure the source mesh contains at least 4 times as many triangles as
% the target mesh (needed for good results with Instant Meshes)
numPointsSource = size(vtk.cells,1);
numSubdiv = max(1-floor(log(numPointsSource/numPoints)/log(4)),0);
if numSubdiv
try
vtk = vtkLinearSubdivisionFilter(vtk, numSubdiv);
catch
warning('Skipping subdivision before remeshing.');
end
end
tmpfile = [tempname '.ply'];
vtkWrite(vtk, tmpfile);
instantmeshes = 'instantmeshes';
[noAliasFound,~] = system(sprintf('which %s', instantmeshes));
if noAliasFound
instantmeshes = '/Applications/Instant Meshes.app/Contents/MacOS/Instant Meshes';
end
if ~exist(instantmeshes, 'file')
instantmeshes = '/Volumes/ServerApps/Instant Meshes.app/Contents/MacOS/Instant Meshes';
end
if ~exist(instantmeshes, 'file')
error('Instant Meshes could not be found.');
end
system(sprintf('''%s'' %s -o %s -r 6 -p 6 -S 0 -d %s', instantmeshes, tmpfile, tmpfile, extraFlags));
vtk = vtkRead(tmpfile);
delete(tmpfile);
end